|
|
@@ -0,0 +1,275 @@
|
|
|
+package com.sheep.gamegroup.module.game.fragment;
|
|
|
+
|
|
|
+import android.app.Activity;
|
|
|
+import android.content.Intent;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.ProgressBar;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import com.chad.library.adapter.base.BaseQuickAdapter;
|
|
|
+import com.chad.library.adapter.base.BaseViewHolder;
|
|
|
+import com.sheep.gamegroup.absBase.AbsApiRefresh;
|
|
|
+import com.sheep.gamegroup.absBase.AbsDownloadListener;
|
|
|
+import com.sheep.gamegroup.absBase.AbsGetDownloadListener;
|
|
|
+import com.sheep.gamegroup.absBase.AbsObserver;
|
|
|
+import com.sheep.gamegroup.absBase.IApiRefresh;
|
|
|
+import com.sheep.gamegroup.event.BigEvent;
|
|
|
+import com.sheep.gamegroup.greendao.download.DownLoadInfo;
|
|
|
+import com.sheep.gamegroup.helper.DownloadHelper;
|
|
|
+import com.sheep.gamegroup.module.game.model.DownloadInfoHelper;
|
|
|
+import com.sheep.gamegroup.util.DownloadUtil;
|
|
|
+import com.sheep.gamegroup.util.ViewUtil;
|
|
|
+import com.sheep.gamegroup.view.fragment.BaseListFragment6;
|
|
|
+import com.sheep.jiuyan.samllsheep.R;
|
|
|
+import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
+import com.sheep.jiuyan.samllsheep.utils.ClassFileHelper;
|
|
|
+
|
|
|
+import org.greenrobot.eventbus.Subscribe;
|
|
|
+
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import io.reactivex.Observable;
|
|
|
+import io.reactivex.ObservableOnSubscribe;
|
|
|
+import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
+import io.reactivex.schedulers.Schedulers;
|
|
|
+
|
|
|
+import static com.sheep.gamegroup.view.adapter.TryMakeMoneyAdp.PUBLIC_TAG_PREFIX_TEXTVIEW_LIST;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by realicing on 2019/1/21.
|
|
|
+ * realicing@sina.com
|
|
|
+ */
|
|
|
+public class FgtDownloadManager extends BaseListFragment6<DownLoadInfo> {
|
|
|
+
|
|
|
+ private DownloadUtil downloadUtil;
|
|
|
+ @Override
|
|
|
+ public void initListener() {
|
|
|
+ super.initListener();
|
|
|
+ downloadUtil = new DownloadUtil();
|
|
|
+ }
|
|
|
+
|
|
|
+ private AbsApiRefresh<DownLoadInfo> apiRefresh;
|
|
|
+ @Override
|
|
|
+ protected void addApiRefresh(List<IApiRefresh> apiRefreshList) {
|
|
|
+ apiRefresh = new AbsApiRefresh<DownLoadInfo>(this) {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void initData() {
|
|
|
+ Observable.create((ObservableOnSubscribe<List<DownLoadInfo>>) emitter -> emitter.onNext(downloadUtil.getAllDownloadTasks()))
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread()).
|
|
|
+ subscribe(new AbsObserver<List<DownLoadInfo>>() {
|
|
|
+ @Override
|
|
|
+ public void onNext(List<DownLoadInfo> newList) {
|
|
|
+ loadList(newList);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected boolean hasMore() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ apiRefreshList.add(apiRefresh);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected BaseQuickAdapter<DownLoadInfo, BaseViewHolder> getAdapter() {
|
|
|
+ return new BaseQuickAdapter<DownLoadInfo, BaseViewHolder>(R.layout.item_download_manager, apiRefresh.getList()){
|
|
|
+ @Override
|
|
|
+ protected void convert(BaseViewHolder helper, DownLoadInfo item) {
|
|
|
+ ImageView item_dm_icon_iv = helper.getView(R.id.item_dm_icon_iv);
|
|
|
+ TextView item_dm_name_tv = helper.getView(R.id.item_dm_name_tv);
|
|
|
+ TextView item_dm_info_tv = helper.getView(R.id.item_dm_info_tv);
|
|
|
+ ProgressBar item_dm_pb = helper.getView(R.id.item_dm_pb);
|
|
|
+ ImageView item_dm_delete_iv = helper.getView(R.id.item_dm_delete_iv);
|
|
|
+ TextView item_dm_btn_tv = helper.getView(R.id.item_dm_btn_tv);
|
|
|
+
|
|
|
+ ViewUtil.setImage(item_dm_icon_iv, item.getMIconUrl());
|
|
|
+ ViewUtil.setText(item_dm_name_tv, item.getMGameName());
|
|
|
+ if(item.getMPercent() == 100){
|
|
|
+ if(item.getMTotalSize() == null){
|
|
|
+ ViewUtil.setText(item_dm_info_tv, "全部下载完成");
|
|
|
+ } else {
|
|
|
+ ViewUtil.setText(item_dm_info_tv, String.format(Locale.CHINA, "%dMB 下载完成", (item.getMTotalSize().intValue() / (1024 * 1024))));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ ViewUtil.setText(item_dm_info_tv, strFormat(item.getMDownloadedSize(), item.getMTotalSize()));
|
|
|
+ }
|
|
|
+ item_dm_pb.setProgress(item.getMPercent() != null ? item.getMPercent() : 0);
|
|
|
+ item_dm_delete_iv.setOnClickListener(view -> {
|
|
|
+ if(apiRefresh != null && apiRefresh.getList() != null)
|
|
|
+ apiRefresh.getList().remove(item);
|
|
|
+ if(baseQuickAdapter != null)
|
|
|
+ baseQuickAdapter.notifyDataSetChanged();
|
|
|
+ Observable.create((ObservableOnSubscribe<Boolean>) emitter -> emitter.onNext(downloadUtil.deleteDownloadTask(item)))
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread()).
|
|
|
+ subscribe(new AbsObserver<Boolean>() {
|
|
|
+ @Override
|
|
|
+ public void onNext(Boolean result) {
|
|
|
+ //删除成功
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ updateView(getActivity(), item, item_dm_btn_tv);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ //删除所有下载任务与文件
|
|
|
+ public void removeAll() {
|
|
|
+ Observable.just(1).map(integer -> {
|
|
|
+ try {
|
|
|
+ downloadUtil.deleteAllDownloadTask();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ClassFileHelper.getInstance().clearDir();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return integer;
|
|
|
+ }).subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new AbsObserver<Integer>() {
|
|
|
+ @Override
|
|
|
+ public void onNext(Integer integer) {
|
|
|
+ clear();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //格式化下载大小
|
|
|
+ private DecimalFormat decimalFormat;
|
|
|
+ public String strFormat(Double fileDownloadSize, Double fileTotalSize) {
|
|
|
+ if (fileTotalSize == null) {
|
|
|
+ return SheepApp.getInstance().getString(R.string.calculating);
|
|
|
+ }
|
|
|
+ if (fileDownloadSize == null) {
|
|
|
+ return "0MB / " + (fileTotalSize.intValue() / (1024 * 1024)) + "MB";
|
|
|
+ }
|
|
|
+ if(decimalFormat == null){
|
|
|
+ decimalFormat = new DecimalFormat("######0.0");
|
|
|
+ }
|
|
|
+ return decimalFormat.format(fileDownloadSize / (1024 * 1024)) + "MB / " + (fileTotalSize.intValue() / (1024 * 1024)) + "MB";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDestroyView() {
|
|
|
+ decimalFormat = null;
|
|
|
+ super.onDestroyView();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //更新下载进度
|
|
|
+ private Map<String, DownloadInfoHelper> downloadInfoHelperMap = new HashMap<>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //更新按钮状态与添加点击事件
|
|
|
+ private void updateView(Activity activity, DownLoadInfo item, TextView textView) {
|
|
|
+ DownloadInfoHelper downloadInfoHelper = downloadInfoHelperMap.get(item.getMDownloadUrl());
|
|
|
+ if(downloadInfoHelper == null) {
|
|
|
+ downloadInfoHelper = new DownloadInfoHelper(item);
|
|
|
+ downloadInfoHelperMap.put(item.getMDownloadUrl(), downloadInfoHelper);
|
|
|
+ downloadInfoHelperMap.put(item.getMPackageName(), downloadInfoHelper);
|
|
|
+ downloadInfoHelper.updateDownloadTaskView(activity, textView);
|
|
|
+ } else {
|
|
|
+ downloadInfoHelper.updateDownloadTaskView(activity, textView);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private AbsGetDownloadListener absGetDownloadListener = new AbsGetDownloadListener(true) {
|
|
|
+ @Override
|
|
|
+ public String getDownloadUrl(String packageName) {
|
|
|
+ DownloadInfoHelper item = downloadInfoHelperMap.get(packageName);
|
|
|
+ return item != null ? item.getDownload_link() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TextView getTextView2(String packageName) {
|
|
|
+ return recyclerView.findViewWithTag(PUBLIC_TAG_PREFIX_TEXTVIEW_LIST + getDownloadUrl(packageName));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TextView getTextView(String downloadUrl) {
|
|
|
+ return recyclerView.findViewWithTag(PUBLIC_TAG_PREFIX_TEXTVIEW_LIST + downloadUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DownloadHelper getDownloadHelper(String downloadUrl) {
|
|
|
+ DownloadInfoHelper item = downloadInfoHelperMap.get(downloadUrl);
|
|
|
+ return item != null ? item.getDownloadHelper() : null;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ @Subscribe
|
|
|
+ public void onEventMainThread(Intent intent) {
|
|
|
+ absGetDownloadListener.onEventMainThread(intent);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Subscribe
|
|
|
+ public void onEventMainThread(BigEvent event) {
|
|
|
+ absGetDownloadListener.onEventMainThread(event);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void initView() {
|
|
|
+ super.initView();
|
|
|
+ absGetDownloadListener.addDownloadListener(new AbsDownloadListener() {
|
|
|
+ @Override
|
|
|
+ public void removedApk(String packageName) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addedApk(String packageName) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void running(DownLoadInfo task) {
|
|
|
+ int position = apiRefresh.getList().indexOf(task);
|
|
|
+ if(position >= 0)
|
|
|
+ baseQuickAdapter.notifyItemChanged(position, task);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void taskStop(DownLoadInfo task) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void taskComplete(DownLoadInfo task) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void taskCancel(DownLoadInfo task) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void taskFail(DownLoadInfo task) {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ ViewUtil.register(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ ViewUtil.unregister(this);
|
|
|
+ }
|
|
|
+}
|