admin
7 天以前 7f0825f8195a522ed7e8bcdb6347f3a719e06c74
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package com.weikou.beibeivideo.ui.recent;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
 
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
 
import com.mozillaonline.providers.DownloadManager;
import com.umeng.analytics.MobclickAgent;
import com.weikou.beibeivideo.R;
import com.weikou.beibeivideo.db.DownloadTable;
import com.weikou.beibeivideo.entity.SDCardEntity;
import com.weikou.beibeivideo.ui.common.MVideosActivity;
import com.weikou.beibeivideo.ui.recent.DownloadAdapter2.IGetDeleteCallback;
import com.weikou.beibeivideo.ui.recent.DownloadAdapter2.IGetSelectStatus;
import com.weikou.beibeivideo.util.SDCardUtil;
 
public class OfflineCacheFragment2 extends Fragment implements
        LoaderManager.LoaderCallbacks<Cursor>, IGetSelectStatus {
 
    private DownloadManager mDownloadManager;
 
    private Cursor mDateSortedCursor;
 
    private DownloadAdapter2 mDownLoadAdapter;
 
    private List<List<DownloadEntity>> finalDatas;
 
    private ListView list;
 
    private View empty;
 
    private TextView tv_storage;
    private LinearLayout ll_storage;
 
    private ProgressBar pb_storage;
 
    private IGetDeleteCallback getdeleteNumber = null;
    private IGetSelectStatus status = null;
 
    public void setDeleteCallback(IGetDeleteCallback getdeleteNumber) {
        this.getdeleteNumber = getdeleteNumber;
        if (mDownLoadAdapter != null)
            mDownLoadAdapter.setDeleteCallback(getdeleteNumber);
    }
 
    public void setSelectStatus(IGetSelectStatus status) {
        this.status = status;
        if (mDownLoadAdapter != null)
            mDownLoadAdapter.setSelectStatus(status);
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.offine_cache_fragment2, container,
                false);
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        mDownloadManager = new DownloadManager(getActivity()
                .getContentResolver(), getActivity().getPackageName());
        mDownloadManager.setAccessAllDownloads(true);
 
        finalDatas = new ArrayList<List<DownloadEntity>>();
    }
 
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ll_storage = (LinearLayout) view.findViewById(R.id.ll_offline_storage);
        empty = view.findViewById(android.R.id.empty);
        tv_storage = (TextView) view.findViewById(R.id.tv_offline_storage);
        SDCardEntity entity = SDCardUtil.getDownLoadEntity(getActivity());
 
        tv_storage.setText("存储总空间"
                + SDCardUtil.getSotrageSize(entity.getTotalSize()
                        / (1024 * 1024))
                + "/剩余"
                + SDCardUtil.getSotrageSize(entity.getAvailableSize()
                        / (1024 * 1024)));
        pb_storage = (ProgressBar) view.findViewById(R.id.pb_storage);
        int p = (int) (((float) (entity.getTotalSize() - entity
                .getAvailableSize()) * 100) / entity.getTotalSize());
        pb_storage.setProgress(p);
 
        empty.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(),
                        MVideosActivity.class);
                intent.putExtra("home_type", "4");
                intent.putExtra("title", "内部福利");
                v.getContext().startActivity(intent);
            }
        });
 
        list = (ListView) view.findViewById(android.R.id.list);
        list.setDividerHeight(1);
        setSelectStatus(this);
        // list.setOnItemClickListener(this);
        if (list.getAdapter() == null) {
            mDownLoadAdapter = new DownloadAdapter2(getContext(), finalDatas);
            if (getdeleteNumber != null)
                mDownLoadAdapter.setDeleteCallback(getdeleteNumber);
            if (status != null)
                mDownLoadAdapter.setSelectStatus(status);
            list.setAdapter(mDownLoadAdapter);
            getLoaderManager().initLoader(0, null, this);
        }
    }
 
    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        return new DownloadCursorLoader(getActivity(), mDownloadManager);
    }
 
    @Override
    public void onPause() {
        super.onPause();
    }
    @Override
    public void onResume() {
        super.onResume();
        if (mStatus) {
            ll_storage.setVisibility(View.GONE);
        } else {
            ll_storage.setVisibility(View.VISIBLE);
        }
        mDownLoadAdapter.setSelectedMode(false);
        SDCardEntity entity = SDCardUtil.getDownLoadEntity(getActivity());
        tv_storage.setText("存储总空间"
                + SDCardUtil.getSotrageSize(entity.getTotalSize()
                        / (1024 * 1024))
                + "/剩余"
                + SDCardUtil.getSotrageSize(entity.getAvailableSize()
                        / (1024 * 1024)));
 
        int p = (int) (((float) (entity.getTotalSize() - entity
                .getAvailableSize()) * 100) / entity.getTotalSize());
        pb_storage.setProgress(p);
    }
 
    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor data) {
        // Swap the new cursor in. (The framework will take care of closing the
        // old cursor once we return.)
        mDateSortedCursor = data;
        if (mDateSortedCursor.isClosed()) {
            return;
        }
        HashMap<Long, DownloadEntity> allDatas = new HashMap<Long, DownloadEntity>();
        if (mDateSortedCursor.moveToFirst()) {
            do {
                DownloadEntity downloadEntity = new DownloadEntity();
                downloadEntity
                        .setId(mDateSortedCursor.getLong(mDateSortedCursor
                                .getColumnIndexOrThrow(DownloadManager.COLUMN_ID)));
                downloadEntity
                        .setTitle(mDateSortedCursor.getString(mDateSortedCursor
                                .getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE)));
                downloadEntity
                        .setDescription(mDateSortedCursor.getString(mDateSortedCursor
                                .getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION)));
                downloadEntity
                        .setCurrentBytes(mDateSortedCursor.getLong(mDateSortedCursor
                                .getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)));
                downloadEntity
                        .setTotalBytes(mDateSortedCursor.getLong(mDateSortedCursor
                                .getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)));
                downloadEntity
                        .setDate(mDateSortedCursor.getLong(mDateSortedCursor
                                .getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP)));
                downloadEntity
                        .setStatus(mDateSortedCursor.getInt(mDateSortedCursor
                                .getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)));
                downloadEntity
                        .setReason(mDateSortedCursor.getInt(mDateSortedCursor
                                .getColumnIndexOrThrow(DownloadManager.COLUMN_REASON)));
                allDatas.put(downloadEntity.getId(), downloadEntity);
            } while (mDateSortedCursor.moveToNext());
        }
 
        HashMap<String, List<DownloadEntity>> datas = new HashMap<String, List<DownloadEntity>>();
        HashMap<String, List<String>> videoIds = new HashMap<String, List<String>>();
        Cursor cursor = getActivity().getContentResolver().query(
                DownloadTable.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
            Long taskId = cursor.getLong(cursor
                    .getColumnIndexOrThrow(DownloadTable.TASK_ID));
            String videoId = cursor.getString(cursor
                    .getColumnIndexOrThrow(DownloadTable.VIDEO_ID));
            if (videoIds.containsKey(videoId)) {
                videoIds.get(videoId)
                        .add(cursor.getString(cursor
                                .getColumnIndexOrThrow(DownloadTable.VIDEO_DETAIL_ID)));
 
                datas.get(videoId).add(allDatas.get(taskId));
            } else {
                List<String> videoDetailIds = new ArrayList<String>();
                videoDetailIds.add(cursor.getString(cursor
                        .getColumnIndexOrThrow(DownloadTable.VIDEO_DETAIL_ID)));
                videoIds.put(videoId, videoDetailIds);
 
                List<DownloadEntity> items = new ArrayList<DownloadEntity>();
                items.add(allDatas.get(taskId));
                datas.put(videoId, items);
            }
        }
        finalDatas.clear();
        if (mDateSortedCursor.getCount() == cursor.getCount()) {
            for (Entry<String, List<DownloadEntity>> entry : datas.entrySet()) {
                finalDatas.add(entry.getValue());
            }
        }
        cursor.close();
        mDownLoadAdapter.notifyDataSetChanged();
        if (mDownLoadAdapter.getCount() == 0) {
            empty.setVisibility(View.VISIBLE);
        } else {
            empty.setVisibility(View.INVISIBLE);
        }
    }
 
    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        // This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed. We need to make sure we are no
        // longer using it.
        finalDatas.clear();
        mDownLoadAdapter.notifyDataSetChanged();
    }
 
    public void deleteDownload() {
        if (mDownLoadAdapter != null) {
            if (!mDownLoadAdapter.getSelectedIds().isEmpty()) {
                for (Long downloadId : mDownLoadAdapter.getSelectedIds()) {
                    deleteDownload(downloadId);
                }
                mDownLoadAdapter.getSelectedIds().clear();
            } else {
                Toast.makeText(getActivity(), "请选择要删除的记录", Toast.LENGTH_LONG)
                        .show();
            }
        }
    }
 
    /**
     * Delete a download from the Download Manager.
     */
    private void deleteDownload(long downloadId) {
        getActivity().getContentResolver().delete(DownloadTable.CONTENT_URI,
                DownloadTable.TASK_ID + " = ? ",
                new String[] { String.valueOf(downloadId) });
        if (moveToDownload(downloadId)) {
            int status = mDateSortedCursor.getInt(mDateSortedCursor
                    .getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
            boolean isComplete = status == DownloadManager.STATUS_SUCCESSFUL
                    || status == DownloadManager.STATUS_FAILED;
            String localUri = mDateSortedCursor.getString(mDateSortedCursor
                    .getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI));
            if (isComplete && localUri != null) {
                String path = Uri.parse(localUri).getPath();
                /*
                 * if (path.startsWith(Environment.getExternalStorageDirectory()
                 * .getPath())) {
                 */
                try {
                    mDownloadManager.markRowDeleted(downloadId);
                    return;
                } catch (Exception e) {
 
                }
                // }
            }
        }
        mDownloadManager.remove(downloadId);
    }
 
    /**
     * Move {@link #mDateSortedCursor} to the download with the given ID.
     *
     * @return true if the specified download ID was found; false otherwise
     */
    private boolean moveToDownload(long downloadId) {
        for (mDateSortedCursor.moveToFirst(); !mDateSortedCursor.isAfterLast(); mDateSortedCursor
                .moveToNext()) {
            if (mDateSortedCursor.getLong(mDateSortedCursor
                    .getColumnIndexOrThrow(DownloadManager.COLUMN_ID)) == downloadId) {
                return true;
            }
        }
        return false;
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mDateSortedCursor != null) {
            mDateSortedCursor.close();
        }
    }
 
    public boolean isSelectedMode() {
        return mDownLoadAdapter.isSelectedMode();
    }
 
    public void setSelectedMode(boolean selectedMode) {
        mDownLoadAdapter.setSelectedMode(selectedMode);
    }
 
    public void selectAll(boolean isSelected) {
        mDownLoadAdapter.selectAll(isSelected);
    }
 
    public int getCount() {
        return mDownLoadAdapter.getCount();
    }
 
    boolean mStatus = false;
 
    @Override
    public void onGetStatus(boolean status) {
        mStatus = status;
        if (status) {
            ll_storage.setVisibility(View.GONE);
        } else {
            ll_storage.setVisibility(View.VISIBLE);
        }
    }
}