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();
|
|
}
|
}
|