hanjing пре 6 година
родитељ
комит
1a1b6e32d9
1 измењених фајлова са 19 додато и 11 уклоњено
  1. 19 11
      app/src/main/java/com/sheep/jiuyan/samllsheep/service/DownloadService.java

+ 19 - 11
app/src/main/java/com/sheep/jiuyan/samllsheep/service/DownloadService.java

@@ -7,7 +7,9 @@ import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.net.Uri;
+import android.os.Handler;
 import android.os.IBinder;
+import android.os.Message;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.support.v7.app.AlertDialog;
@@ -63,7 +65,8 @@ public class DownloadService extends Service {
     @Override
     public void onCreate() {
         super.onCreate();
-        downloadUtil = new DownloadUtil();
+        this.
+                downloadUtil = new DownloadUtil();
 //        Util.setLogger(new Util.Logger() {
 //            @Override
 //            public void e(String tag, String msg, Exception e) {
@@ -94,7 +97,7 @@ public class DownloadService extends Service {
             String download_url = intent.getStringExtra("download_url");
             String file_path = intent.getStringExtra("file_path");
             String type = intent.getStringExtra("type");
-            if(type == null){
+            if (type == null) {
                 type = FileUtil.getExtensionName(file_path);
             }
             startDownload(download_url, file_path, type, 0);
@@ -102,7 +105,7 @@ public class DownloadService extends Service {
         return super.onStartCommand(intent, flags, startId);
     }
 
-    private void startDownload(String download_url, String file_path,  String type, int retry) {
+    private void startDownload(String download_url, String file_path, String type, int retry) {
         if (download_url != null && file_path != null) {
             DownloadTask task = DownloadUtil.getTask(download_url, file_path);
             task.addTag(TAG_RETRY, retry);
@@ -116,7 +119,7 @@ public class DownloadService extends Service {
         public void taskStart(@NonNull DownloadTask task) {
             String type = (String) task.getTag(TAG_TYPE);
             LogUtil.println("DownloadListener", "taskStart", task.getTag(TAG_RETRY), type);
-            switch (type){
+            switch (type) {
                 case "mp4":
                     break;
                 case "apk"://下载游戏
@@ -141,7 +144,7 @@ public class DownloadService extends Service {
             long totalLength = info.getTotalLength();
             LogUtil.println("DownloadListener", "infoReady", "fromBreakpoint", fromBreakpoint, "totalLength", totalLength);
             String type = (String) task.getTag(TAG_TYPE);
-            switch (type){
+            switch (type) {
                 case "mp4":
                     break;
                 case "apk":
@@ -161,7 +164,7 @@ public class DownloadService extends Service {
         public void progress(@NonNull DownloadTask task, long currentOffset, @NonNull SpeedCalculator taskSpeed) {
             LogUtil.println("DownloadListener", "progress", "currentOffset", currentOffset, "taskSpeed", taskSpeed.averageSpeed());
             String type = (String) task.getTag(TAG_TYPE);
-            switch (type){
+            switch (type) {
                 case "mp4":
                     break;
                 case "apk":
@@ -181,7 +184,7 @@ public class DownloadService extends Service {
         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());
             String type = (String) task.getTag(TAG_TYPE);
-            switch (type){
+            switch (type) {
                 case "mp4":
                     break;
                 case "apk":
@@ -222,9 +225,14 @@ public class DownloadService extends Service {
                                 DDProviderHelper.getInstance().updateDownload(downLoadInfo);
                             }
                             int retry = (int) task.getTag(TAG_RETRY);
-                            if (retry < 15 && task.getFile() != null) {
-                                startDownload(task.getUrl(), task.getFile().getAbsolutePath(), type, ++retry);
-                                Log.e("DownloadRetry", "Retry " + retry + "   " + task.getUrl());
+                            if (retry < 20 && task.getFile() != null) {
+                                int r = ++retry;
+                                new Handler(getMainLooper()) {
+                                    public void handleMessage(Message msg) {
+                                        Log.e("DownloadRetry", "Retry " + r + "   " + task.getUrl());
+                                        startDownload(task.getUrl(), task.getFile().getAbsolutePath(), type, r);
+                                    }
+                                }.sendEmptyMessageDelayed(0, 3000);
                             } else {
                                 if (downLoadInfo != null) {
                                     EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
@@ -262,7 +270,7 @@ public class DownloadService extends Service {
     }
 
     public static void setDownLoadLongClick(View view, final String link, final String taskName) {
-        if(TestUtil.isTest())
+        if (TestUtil.isTest())
             view.setOnLongClickListener(view1 -> {
                 showDownloadType(link, taskName);
                 return true;