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