admin
2021-07-15 49982f5a1a305c0cc7da04735e1c604b802d2a22
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
package com.mugua.mgvideo.ui.recent;
 
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import androidx.fragment.app.ListFragment;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
 
import com.mozillaonline.providers.DownloadManager;
import com.mugua.mgvideo.db.DownloadTable;
import com.yeshi.base.entity.video.VideoDetailInfo;
import com.yeshi.base.entity.video.VideoInfo;
import com.yeshi.video.ui.VideoDetailActivity;
 
public class OfflineCacheFragment extends ListFragment {
 
    private DownloadManager mDownloadManager;
 
    private Cursor mDateSortedCursor;
    
    private DownLoadAdapter mDownLoadAdapter;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        mDownloadManager = new DownloadManager(getActivity()
                .getContentResolver(), getActivity().getPackageName());
        mDownloadManager.setAccessAllDownloads(true);
        DownloadManager.Query baseQuery = new DownloadManager.Query()
                .setOnlyIncludeVisibleInDownloadsUi(true)
                .orderBy(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP, DownloadManager.Query.ORDER_DESCENDING);
        mDateSortedCursor = mDownloadManager.query(baseQuery);
        
        mDownLoadAdapter = new DownLoadAdapter(getActivity(), mDateSortedCursor, true);
    }
 
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getListView().setDividerHeight(1);
        setListAdapter(mDownLoadAdapter);
    }
    
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Cursor c = (Cursor) l.getItemAtPosition(position);
        long downloadId = c.getLong(c.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
        Cursor cursor = v.getContext().getContentResolver().query(DownloadTable.CONTENT_URI,
                null, DownloadTable.TASK_ID + " = ? ", new String[] { String.valueOf(downloadId) }, null);
        if (cursor.moveToFirst()) {
            final String videoId = cursor.getString(cursor.getColumnIndexOrThrow(DownloadTable.VIDEO_ID));
            final String videoDetailId = cursor.getString(cursor.getColumnIndexOrThrow(DownloadTable.VIDEO_DETAIL_ID));
            VideoInfo videoInfo = mDownLoadAdapter.getVideoInfos().get(videoId);
            if (videoInfo != null) {
                int playingPosition = 0;
                for (int i = 0; i < videoInfo.getVideoDetailList().size(); i++) {
                    VideoDetailInfo item = videoInfo.getVideoDetailList().get(i);
                    if (item.getId().equals(videoDetailId)) {
                        playingPosition = i;
                        break;
                    }
                }
                Intent intent = new Intent(v.getContext(), VideoDetailActivity.class);
                intent.putExtra("video_info", videoInfo);
                intent.putExtra("playing_position", playingPosition);
                v.getContext().startActivity(intent);
            }
        }
        cursor.close();
    }
    
    public void deleteDownload() {
        if (mDownLoadAdapter != null) {
            if (!mDownLoadAdapter.getSelectedIds().isEmpty()) {
                for (Long downloadId : mDownLoadAdapter.getSelectedIds()) {
                    deleteDownload(downloadId);
                }
            } 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())) {
                    mDownloadManager.markRowDeleted(downloadId);
                    return;
                }
            }
        }
        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 void toggleSelectedMode() {
        mDownLoadAdapter.setSelectedMode(!mDownLoadAdapter.isSelectedMode());
    }
    
    public void selectAll(boolean isSelected) {
        mDownLoadAdapter.selectAll(isSelected);
    }
    
    public int getCount() {
        return mDownLoadAdapter.getCount();
    }
}