Переглянути джерело

提交2.0.4测试版本10,添加下载文件覆盖,还没测下载断点下载方式

yanyi 4 роки тому
батько
коміт
e5c7b0a5a2

+ 31 - 30
app/src/main/java/com/mylove/okhttp/DownloadActivity.java

@@ -2,14 +2,17 @@ package com.mylove.okhttp;
 
 import android.content.Context;
 import android.os.Bundle;
-import androidx.annotation.IdRes;
-import androidx.appcompat.app.AppCompatActivity;
 import android.view.View;
 import android.widget.Button;
 import android.widget.ProgressBar;
 
+import androidx.annotation.IdRes;
+import androidx.appcompat.app.AppCompatActivity;
+
+import com.benyanyi.loglib.Jlog;
 import com.benyanyi.okhttp.OkHttpUtil;
 import com.benyanyi.okhttp.download.DownloadInfo;
+import com.benyanyi.okhttp.listener.OnDownLoadObserver;
 
 import org.greenrobot.eventbus.EventBus;
 import org.greenrobot.eventbus.Subscribe;
@@ -25,7 +28,7 @@ public class DownloadActivity extends AppCompatActivity implements View.OnClickL
     private Button downloadBtn1, downloadBtn2, downloadBtn3;
     private Button cancelBtn1, cancelBtn2, cancelBtn3;
     private ProgressBar progress1, progress2, progress3;
-    String url2 = "https://go.ziwanyouxi.com/ad/=ITOwgjM/28092";
+    String url = "http://apitest.yanyi.online/app/xiangyou.apk";
 
     private Context mContext;
 
@@ -58,11 +61,9 @@ public class DownloadActivity extends AppCompatActivity implements View.OnClickL
 
     @Override
     public void onClick(View v) {
-        String url1 = "http://www.yanyi.red/bluetooth/ios.pdf";
-        String url3 = "https://go.ziwanyouxi.com/ad/=MDO3kjM/29783";
         switch (v.getId()) {
             case R.id.main_btn_down1:
-                AppContext.getDownloadManager().start(url2);
+//                AppContext.getDownloadManager().start(url2);
                 break;
 //            case R.id.main_btn_down1:
 //                OkHttpUtil.getInstance(mContext).url(url1).download().start(new OnDownLoadObserver() {
@@ -80,26 +81,26 @@ public class DownloadActivity extends AppCompatActivity implements View.OnClickL
 //                    }
 //                });
 //                break;
-//            case R.id.main_btn_down2:
-//                OkHttpUtil.getInstance(mContext).url(url2).download(".apk").start(new OnDownLoadObserver() {
-//                    @Override
-//                    public void onNext(DownloadInfo downloadInfo) {
-//                        JLog.v(downloadInfo.getProgress());
-//                        progress2.setMax((int) downloadInfo.getTotal());
-//                        progress2.setProgress((int) downloadInfo.getProgress());
-//                    }
-//
-//                    @Override
-//                    public void onError(Throwable e) {
-//
-//                    }
-//
-//                    @Override
-//                    public void onComplete() {
-//
-//                    }
-//                });
-//                break;
+            case R.id.main_btn_down2:
+                OkHttpUtil.getInstance(mContext).url(url).download(true).start(new OnDownLoadObserver() {
+                    @Override
+                    public void onNext(DownloadInfo downloadInfo) {
+                        Jlog.v(downloadInfo.getProgress());
+                        progress2.setMax((int) downloadInfo.getTotal());
+                        progress2.setProgress((int) downloadInfo.getProgress());
+                    }
+
+                    @Override
+                    public void onError(Throwable e) {
+
+                    }
+
+                    @Override
+                    public void onComplete() {
+
+                    }
+                });
+                break;
 //            case R.id.main_btn_down3:
 //                OkHttpUtil.getInstance(mContext).url(url3).download(".apk").start(new OnDownLoadObserver() {
 //                    @Override
@@ -121,14 +122,14 @@ public class DownloadActivity extends AppCompatActivity implements View.OnClickL
 //                });
 //                break;
             case R.id.main_btn_cancel1:
-                AppContext.getDownloadManager().cancel(url2);
+//                AppContext.getDownloadManager().cancel(url);
 //                OkHttpUtil.getInstance(mContext).url(url1).download().cancel();
                 break;
             case R.id.main_btn_cancel2:
-                OkHttpUtil.getInstance(mContext).url(url2).download().pause();
+                OkHttpUtil.getInstance(mContext).url(url).download(true).pause();
                 break;
             case R.id.main_btn_cancel3:
-                OkHttpUtil.getInstance(mContext).url(url3).download().cancel();
+                OkHttpUtil.getInstance(mContext).url(url).download(true).cancel();
                 break;
             default:
                 break;
@@ -138,7 +139,7 @@ public class DownloadActivity extends AppCompatActivity implements View.OnClickL
     @Subscribe(threadMode = ThreadMode.MAIN)
     public void download(DownloadInfo downloadInfo) {
         if (downloadInfo != null) {
-            if (downloadInfo.getUrl().equals(url2)) {
+            if (downloadInfo.getUrl().equals(url)) {
                 progress1.setMax((int) downloadInfo.getTotal());
                 progress1.setProgress((int) downloadInfo.getProgress());
             }

+ 46 - 8
app/src/main/java/com/mylove/okhttp/MainActivity.java

@@ -35,23 +35,61 @@ public class MainActivity extends AppCompatActivity {
         super.onCreate(savedInstanceState);
 //        setContentView(R.layout.notification_contentview);
         setContentView(R.layout.activity_main);
-//        init();
-        PermissionHelper.getInstance(this).hasPermission(0x11, new PermissionCallBack() {
+        findViewById(R.id.but).setOnClickListener(new View.OnClickListener() {
             @Override
-            public void onPermissionSuccess(int permissionCode) {
-                downloadAPK();
-            }
+            public void onClick(View v) {
+                PermissionHelper.getInstance(MainActivity.this).hasPermission(0x11, new PermissionCallBack() {
+                    @Override
+                    public void onPermissionSuccess(int permissionCode) {
+                        Intent intent = new Intent(MainActivity.this, DownloadActivity.class);
+                        startActivity(intent);
+                    }
+
+                    @Override
+                    public void onPermissionFailure(FailureMsg failureMsg) {
 
+                    }
+
+                    @Override
+                    public void onPermissionComplete(int permissionCode) {
+
+                    }
+                }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
+            }
+        });
+        OkHttpUtil.getInstance(this).url("http://apitest.yanyi.online/app/update").getText(null).async(new OnOkHttpListener<Object>() {
             @Override
-            public void onPermissionFailure(FailureMsg failureMsg) {
+            public void onCompleted() {
 
             }
 
             @Override
-            public void onPermissionComplete(int permissionCode) {
+            public void onSuccess(Object message) {
+                Jlog.d(message);
+            }
+
+            @Override
+            public void onFailure(Throwable t) {
 
             }
-        }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
+        });
+//        init();
+//        PermissionHelper.getInstance(this).hasPermission(0x11, new PermissionCallBack() {
+//            @Override
+//            public void onPermissionSuccess(int permissionCode) {
+//                downloadAPK();
+//            }
+//
+//            @Override
+//            public void onPermissionFailure(FailureMsg failureMsg) {
+//
+//            }
+//
+//            @Override
+//            public void onPermissionComplete(int permissionCode) {
+//
+//            }
+//        }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
 //        permissionHelper = new PermissionHelper(this, strings);
 //        permissionHelper.hasPermission(new PermissionHelper.OnPermissionListener() {
 //            @Override

+ 7 - 0
okhttplib/src/main/java/com/benyanyi/okhttp/OkHttpUtil.java

@@ -4,6 +4,7 @@ import android.content.Context;
 
 import com.benyanyi.okhttp.type.HttpRequest;
 import com.benyanyi.okhttp.type.RequestType;
+import com.benyanyi.okhttp.util.FormatUtil;
 
 /**
  * @author YanYi
@@ -25,10 +26,16 @@ public class OkHttpUtil {
     }
 
     public RequestType url(String url) {
+        if (FormatUtil.isEmpty(url) || (url.indexOf("https:") != 0 && url.indexOf("http:") != 0)) {
+            throw new RuntimeException("url is not http or https");
+        }
         return new HttpRequest(mContext, url, false);
     }
 
     public RequestType url(String url, boolean isCache) {
+        if (FormatUtil.isEmpty(url) || (url.indexOf("https:") != 0 && url.indexOf("http:") != 0)) {
+            throw new RuntimeException("url is not http or https");
+        }
         return new HttpRequest(mContext, url, isCache);
     }
 

+ 35 - 4
okhttplib/src/main/java/com/benyanyi/okhttp/download/DownloadManager.java

@@ -1,6 +1,7 @@
 package com.benyanyi.okhttp.download;
 
 import android.content.Context;
+import android.content.SharedPreferences;
 
 import com.benyanyi.okhttp.listener.OnDownLoadObserver;
 import com.benyanyi.okhttp.util.OkHttpLog;
@@ -45,8 +46,22 @@ class DownloadManager {
     private OkHttpClient mClient;
     private Context mContext;
 
-    private String suffix = "";//文件名后缀
-    private boolean isCover = false;//是否覆盖之前的文件
+    /**
+     * 文件名后缀
+     */
+    private String suffix = "";
+    /**
+     * 是否覆盖之前的文件
+     */
+    private boolean isCover = false;
+
+    /**
+     * 是否为下载 false 为继续下载
+     */
+    private boolean isStart = true;
+
+    private SharedPreferences sharedPreferences;
+    private SharedPreferences.Editor oEditor;
 
     /**
      * 获得一个单例类
@@ -71,6 +86,8 @@ class DownloadManager {
         downCalls = new HashMap<>();
         downInfos = new HashMap<>();
         mClient = new OkHttpClient.Builder().build();
+        sharedPreferences = mContext.getSharedPreferences("benyanyi", Context.MODE_PRIVATE);
+        oEditor = sharedPreferences.edit();
         this.mContext = mContext;
     }
 
@@ -104,6 +121,7 @@ class DownloadManager {
     void download(String url, boolean isCover, String suffix) {
         this.isCover = isCover;
         this.suffix = suffix;
+        this.isStart = this.sharedPreferences.getBoolean("download_start", true);
         this.download(url);
     }
 
@@ -118,6 +136,7 @@ class DownloadManager {
     void download(String url, boolean isCover, String suffix, OnDownLoadObserver onDownLoadObserver) {
         this.isCover = isCover;
         this.suffix = suffix;
+        this.isStart = this.sharedPreferences.getBoolean("download_start", true);
         this.download(url, onDownLoadObserver);
     }
 
@@ -210,6 +229,9 @@ class DownloadManager {
     void pause(String url) {
         remove(url);
         downInfos.remove(url);
+        oEditor.putBoolean("download_start", false);
+        oEditor.apply();
+        oEditor.commit();
     }
 
     /**
@@ -235,6 +257,9 @@ class DownloadManager {
             }
         }
         downInfos.remove(url);
+        oEditor.putBoolean("download_start", true);
+        oEditor.apply();
+        oEditor.commit();
     }
 
     private void remove(String url) {
@@ -267,11 +292,14 @@ class DownloadManager {
         long downloadLength = 0, contentLength = downloadInfo.getTotal();
         File file = new File(savePath, fileName);
         if (file.exists()) {
-            if (isCover) {
+            if (isStart && isCover) {
                 file.delete();
             } else {
                 //找到了文件,代表已经下载过,则获取其长度
                 downloadLength = file.length();
+                oEditor.putBoolean("download_start", true);
+                oEditor.apply();
+                oEditor.commit();
             }
         }
         if (!isCover) {
@@ -353,6 +381,9 @@ class DownloadManager {
                         fileOutputStream.flush();
                         downCalls.remove(url);
                         downloadInfo.setDownloadStatus(DownloadInfo.DOWNLOAD_OVER);
+                        oEditor.putBoolean("download_start", true);
+                        oEditor.apply();
+                        oEditor.commit();
                         //关闭IO流
                         IoUtil.closeAll(is, fileOutputStream);
                         e.onComplete();//完成
@@ -439,7 +470,7 @@ class DownloadManager {
                 .build();
         try {
             Response response = mClient.newCall(request).execute();
-            if (response != null && response.isSuccessful()) {
+            if (response.isSuccessful()) {
                 long contentLength = response.body().contentLength();
                 response.close();
                 return contentLength == 0 ? DownloadInfo.TOTAL_ERROR : contentLength;