Selaa lähdekoodia

download add retry

hanjing 7 vuotta sitten
vanhempi
commit
6098befecc

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

@@ -12,6 +12,7 @@ import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.support.v7.app.AlertDialog;
 import android.text.TextUtils;
+import android.util.Log;
 import android.view.View;
 
 import com.kfzs.duanduan.utils.ApkUtils;
@@ -19,6 +20,7 @@ import com.kfzs.duanduan.view.DialogStorageLow;
 import com.liulishuo.okdownload.DownloadListener;
 import com.liulishuo.okdownload.DownloadTask;
 import com.liulishuo.okdownload.SpeedCalculator;
+import com.liulishuo.okdownload.core.Util;
 import com.liulishuo.okdownload.core.breakpoint.BlockInfo;
 import com.liulishuo.okdownload.core.breakpoint.BreakpointInfo;
 import com.liulishuo.okdownload.core.cause.EndCause;
@@ -52,12 +54,36 @@ import static com.sheep.jiuyan.samllsheep.utils.ClassFileHelper.DIR;
 
 public class DownloadService extends Service {
 
+    private final static int TAG_RETRY = 1001;
+
     private DownloadUtil downloadUtil;
 
     @Override
     public void onCreate() {
         super.onCreate();
         downloadUtil = new DownloadUtil();
+        Util.setLogger(new Util.Logger() {
+            @Override
+            public void e(String tag, String msg, Exception e) {
+                Log.e("sheep-" + tag, msg + "\n" + e.getMessage());
+                e.printStackTrace();
+            }
+
+            @Override
+            public void w(String tag, String msg) {
+                Log.w("sheep-" + tag, msg);
+            }
+
+            @Override
+            public void d(String tag, String msg) {
+                Log.d("sheep-" + tag, msg);
+            }
+
+            @Override
+            public void i(String tag, String msg) {
+                Log.i("sheep-" + tag, msg);
+            }
+        });
     }
 
     @Override
@@ -65,14 +91,19 @@ public class DownloadService extends Service {
         if (intent != null) {
             String download_url = intent.getStringExtra("download_url");
             String file_path = intent.getStringExtra("file_path");
-            if (download_url != null && file_path != null) {
-                DownloadTask task = DownloadUtil.getTask(download_url, file_path);
-                task.enqueue(downloadListener);
-            }
+            startDownload(download_url, file_path, 0);
         }
         return super.onStartCommand(intent, flags, startId);
     }
 
+    private void startDownload(String download_url, String file_path, int retry) {
+        if (download_url != null && file_path != null) {
+            DownloadTask task = DownloadUtil.getTask(download_url, file_path);
+            task.addTag(TAG_RETRY, retry);
+            task.enqueue(downloadListener);
+        }
+    }
+
     private DownloadListener downloadListener = new DownloadListener4WithSpeed() {
         @Override
         public void taskStart(@NonNull DownloadTask task) {
@@ -118,6 +149,10 @@ public class DownloadService extends Service {
         @Override
         public void taskEnd(@NonNull DownloadTask task, @NonNull EndCause cause, @Nullable Exception realCause, @NonNull SpeedCalculator taskSpeed) {
             LogUtil.println("DownloadListener", "taskEnd", "cause", cause.name(), "realCause", realCause != null ? realCause.getMessage() : "null", "taskSpeed", taskSpeed.averageSpeed());
+            if (realCause != null) {
+                realCause.printStackTrace();
+            }
+            new Exception("Download exception: ").printStackTrace();
             DownLoadInfo downLoadInfo;
             switch (cause) {
                 case COMPLETED://下载完成
@@ -136,29 +171,26 @@ public class DownloadService extends Service {
                     }
                     downLoadInfo = downloadUtil.getDownloadTaskByUrl(task.getUrl());
                     downLoadInfo.setMStatus(DownloadUtil.STATUS_FAIL);
-                    if(realCause != null)
+                    if (realCause != null)
                         downLoadInfo.setRealCauseMsg(realCause.getMessage());
                     DDProviderHelper.getInstance().updateDownload(downLoadInfo);
                     EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
                     break;
                 case FILE_BUSY:
                 case ERROR://下载错误
-                    //空间不足,弹框提示
-                    try {
-                        if (0 <= task.getReadBufferSize() && task.getReadBufferSize() > G.getFreeSpaceB()) {
-                            DialogStorageLow.showDialog(realCause != null ? realCause.getMessage() : null);
-                        }
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                    }
                     downLoadInfo = downloadUtil.getDownloadTaskByUrl(task.getUrl());
-                    if(downLoadInfo!=null) {
+                    if (downLoadInfo != null) {
                         downLoadInfo.setMStatus(DownloadUtil.STATUS_FAIL);
                         if (realCause != null)
                             downLoadInfo.setRealCauseMsg(realCause.getMessage());
                         DDProviderHelper.getInstance().updateDownload(downLoadInfo);
                         EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
                     }
+                    int retry = (int) task.getTag(TAG_RETRY);
+                    if (retry < 10) {
+                        startDownload(task.getUrl(), task.getFile().getAbsolutePath(), ++retry);
+                        Log.e("DownloadRetry", "Retry " + retry + "   " + task.getUrl());
+                    }
                     break;
                 case CANCELED://取消下载
                     downLoadInfo = downloadUtil.setDownloadTaskStatus(task.getUrl(), DownloadUtil.STATUS_PAUSE);