admin
2021-10-21 7e2ac66d7c532a5725635fa3913789bb17c1e157
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
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.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
 
import com.mozillaonline.providers.DownloadManager;
import com.mugua.mgvideo.R;
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 MOfflineCacheFragment extends Fragment implements
        OnItemClickListener {
 
    private DownloadManager mDownloadManager;
 
    private Cursor mDateSortedCursor;
 
    private DownLoadAdapter mDownLoadAdapter;
 
    private ListView lv_teleplay;
 
    @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 View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.offine_cache_group_fragment,
                container, false);
    }
 
    @Override
    public void onResume() {
        super.onResume();
    }
 
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lv_teleplay = (ListView) view.findViewById(R.id.group_list);
        lv_teleplay.setAdapter(mDownLoadAdapter);
    }
 
    // 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();
    }
 
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
 
        Cursor c = (Cursor) parent.getItemAtPosition(position);
        long downloadId = c.getLong(c
                .getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
        Cursor cursor = view
                .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(view.getContext(),
                        VideoDetailActivity.class);
                intent.putExtra("video_info", videoInfo);
                intent.putExtra("playing_position", playingPosition);
                view.getContext().startActivity(intent);
            }
        }
        cursor.close();
 
    }
}