Procházet zdrojové kódy

添加下载无响应的监听,并提示用户使用其它下载方式

zengjiebin před 7 roky
rodič
revize
737e75c1ae

+ 68 - 14
app/src/main/java/com/sheep/jiuyan/samllsheep/service/DownloadService.java

@@ -4,28 +4,44 @@ import android.app.Service;
 import android.content.Intent;
 import android.os.IBinder;
 import android.support.annotation.Nullable;
+import android.view.View;
 
 import com.arialyy.annotations.Download;
 import com.arialyy.aria.core.Aria;
 import com.arialyy.aria.core.download.DownloadTask;
 import com.sheep.gamegroup.event.BigEvent;
 import com.sheep.gamegroup.event.EventTypes;
+import com.sheep.gamegroup.model.entity.DialogConfig;
+import com.sheep.gamegroup.util.ActivityManager;
 import com.sheep.gamegroup.util.DownloadUtil;
 import com.kfzs.duanduan.utils.ApkUtils;
 import com.kfzs.duanduan.view.DialogStorageLow;
 import com.sheep.gamegroup.util.LogUtil;
+import com.sheep.gamegroup.util.TestUtil;
+import com.sheep.gamegroup.util.ViewUtil;
+import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.G;
 
 import org.greenrobot.eventbus.EventBus;
 
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import rx.Observable;
+import rx.android.schedulers.AndroidSchedulers;
+import rx.functions.Action1;
+import rx.schedulers.Schedulers;
+
 /**
- *  @auther realicing
+ * @auther realicing
  * 自动更新的Service
  */
 
-public class DownloadService extends Service{
+public class DownloadService extends Service {
 
     private DownloadUtil mDownloadTaskService;
+
     @Override
     public void onCreate() {
         super.onCreate();
@@ -33,57 +49,95 @@ public class DownloadService extends Service{
         mDownloadTaskService = new DownloadUtil(this);
     }
 
-    @Download.onPre void onPre(DownloadTask task) {
+    private Map<String, DownloadTask> downloadTaskMap = new HashMap<>();
+
+    @Download.onPre
+    void onPre(final DownloadTask task) {
         LogUtil.println("DownloadService", "onPre", task.getKey(), task.getTaskName());
+        downloadTaskMap.put(task.getKey(), task);
+
+        Observable.just(1).delay(5000L, TimeUnit.MILLISECONDS)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe(new Action1<Integer>() {
+                    @Override
+                    public void call(Integer integer) {
+                        if (downloadTaskMap.containsKey(task.getKey())) {
+                            ViewUtil.showMsgDialog(ActivityManager.getInstance().currentActivity(), new DialogConfig().setTitle("提示")
+                                    .setMsg("下载无响应,是否使用其它下载方式(在列表中长按任务也可选择其它下载方式)").setBtnLeftText("取消").setBtnRightText("确定")
+                                    .setBtnRightOnClickListener(new View.OnClickListener() {
+                                        @Override
+                                        public void onClick(View view) {
+                                            TestUtil.sysDownload(SheepApp.getInstance(), task.getKey(), task.getTaskName(), "下载小绵羊任务中的apk:" + task.getTaskName(), task.getTaskName()+task.getKey().hashCode() + ".apk", "正在下载");
+                                        }
+                                    }));
+                        }
+                    }
+                });
     }
-    @Download.onTaskStart void taskStart(DownloadTask task) {
+
+    @Download.onTaskStart
+    void taskStart(DownloadTask task) {
         LogUtil.println("DownloadService", "taskStart", task.getKey(), task.getTaskName());
     }
-    @Download.onTaskResume void taskResume(DownloadTask task) {
+
+    @Download.onTaskResume
+    void taskResume(DownloadTask task) {
         LogUtil.println("DownloadService", "taskResume", task.getKey(), task.getTaskName());
     }
+
     //在这里处理任务执行中的状态,如进度进度条的刷新
-    @Download.onTaskRunning protected void running(DownloadTask task) {
+    @Download.onTaskRunning
+    protected void running(DownloadTask task) {
         LogUtil.println("DownloadService", "running", task.getKey(), task.getTaskName(), task.getPercent());
-        mDownloadTaskService.setDownloadTaskPercent(task.getPercent(), task.getCurrentProgress()/1024d/1024d,task.getFileSize()/1024d/1024d,0,task.getKey());
+        if (downloadTaskMap.containsKey(task.getKey())) {
+            downloadTaskMap.remove(task.getKey());
+        }
+        mDownloadTaskService.setDownloadTaskPercent(task.getPercent(), task.getCurrentProgress() / 1024d / 1024d, task.getFileSize() / 1024d / 1024d, 0, task.getKey());
         EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.GENERATION_ACCOUNT_REFRESH_ADAPTER).setData(task));
     }
 
-    @Download.onTaskStop void taskStop(DownloadTask task) {
+    @Download.onTaskStop
+    void taskStop(DownloadTask task) {
         LogUtil.println("DownloadService", "taskStop", task.getKey(), task.getTaskName());
         mDownloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadUtil.STATUS_PAUSE);
         EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_STOP).setData(task));
     }
 
-    @Download.onNoSupportBreakPoint void onNoSupportBreakPoint(DownloadTask task) {
+    @Download.onNoSupportBreakPoint
+    void onNoSupportBreakPoint(DownloadTask task) {
     }
 
-    @Download.onTaskCancel void taskCancel(DownloadTask task) {
+    @Download.onTaskCancel
+    void taskCancel(DownloadTask task) {
         LogUtil.println("DownloadService", "taskCancel", task.getKey(), task.getTaskName());
         mDownloadTaskService.deleteDownloadTaskByDownloadUrl(task.getKey());
         EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_CANCEL).setData(task));
     }
 
-    @Download.onTaskFail void taskFail(DownloadTask task) {
+    @Download.onTaskFail
+    void taskFail(DownloadTask task) {
         LogUtil.println("DownloadService", "taskFail", task.getKey(), task.getTaskName());
         mDownloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadUtil.STATUS_FAIL);
         EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(task));
         //空间不足,弹框提示
-        try{
+        try {
             if (0 <= task.getFileSize() && task.getFileSize() > G.getFreeSpaceB()) {
                 DialogStorageLow.showDialog(DownloadService.this);
             }
-        }catch (Exception e){
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }
 
-    @Download.onTaskComplete void taskComplete(DownloadTask task) {
+    @Download.onTaskComplete
+    void taskComplete(DownloadTask task) {
         LogUtil.println("DownloadService", "taskComplete", task.getKey(), task.getTaskName());
         mDownloadTaskService.setDownloadTaskFinish(task.getKey(), task.getDownloadPath());
         ApkUtils.installApk(getApplicationContext(), task.getDownloadPath());
         EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_COMPLETE).setData(task));
     }
+
     @Nullable
     @Override
     public IBinder onBind(Intent intent) {