Просмотр исходного кода

保存插件更新进度,优化用户体验

zengjiebin лет назад: 7
Родитель
Сommit
4874d1d60a

+ 49 - 28
app/src/main/java/com/sheep/gamegroup/module/plugin/util/PluginUtil.java

@@ -8,6 +8,8 @@ import com.sheep.gamegroup.model.entity.BaseMessage;
 import com.sheep.gamegroup.model.util.SheepSubscriber;
 import com.sheep.gamegroup.module.plugin.model.ClientPackage;
 import com.sheep.gamegroup.module.plugin.model.Plugin;
+import com.sheep.gamegroup.util.DataUtil;
+import com.sheep.gamegroup.util.DownloadUtil;
 import com.sheep.gamegroup.util.ListUtil;
 import com.sheep.gamegroup.util.LogUtil;
 import com.sheep.gamegroup.util.Md5Util;
@@ -44,28 +46,41 @@ public class PluginUtil {
 //                emitter.onError(new Throwable("暂不支持该设备"));
 //                return;
 //            }
-            if (PluginManager.getInstance(SheepApp.getInstance()).getLoadedPlugin(plugin.getPackageName()) == null) {
+            int downloadPluginState = DataUtil.getAsInt(getDownloadPluginKey(plugin), DownloadUtil.STATUS_INIT);
+            switch (downloadPluginState){
+                case DownloadUtil.STATUS_FINISH:
+                    if (PluginManager.getInstance(SheepApp.getInstance()).getLoadedPlugin(plugin.getPackageName()) == null) {
 //                String cpuAbi = getCPU_ABI();
 //                String pluginPath = String.format(Locale.CHINA, "%s/media_%s.apk", ClassFileHelper.DIR, cpuAbi);
-                String pluginPath = ClassFileHelper.DIR + "/media_release.apk";
+                        String pluginPath = ClassFileHelper.DIR + "/media_release.apk";
 //                LogUtil.println(PluginUtil.class.getSimpleName(), "CPU_ABI =", cpuAbi, "pluginPath =", pluginPath);
-                File file = new File(pluginPath);
-                boolean isUserTestFile = TestUtil.isTest() && file.exists();
-                LogUtil.println("startPlugin", isUserTestFile);
-                File pluginFile = isUserTestFile ? file : getFile(plugin);
-                if(!pluginFile.exists()){
-                    emitter.onError(new Throwable("更新数据中,请稍候"));
-                    return;
-                }
-                try {
-                    PluginManager.getInstance(SheepApp.getInstance()).loadPlugin(pluginFile);
-                    emitter.onNext(plugin);
-                } catch (Exception e) {
-                    e.printStackTrace();
-                    emitter.onError(e);
-                }
-            } else {
-                emitter.onNext(plugin);
+                        File file = new File(pluginPath);
+                        boolean isUserTestFile = TestUtil.isTest() && file.exists();
+                        LogUtil.println("startPlugin", isUserTestFile);
+                        File pluginFile = isUserTestFile ? file : getFile(plugin);
+                        if(!pluginFile.exists()){
+                            emitter.onError(new Throwable("更新数据中,请稍候再试"));
+                            return;
+                        }
+                        try {
+                            PluginManager.getInstance(SheepApp.getInstance()).loadPlugin(pluginFile);
+                            emitter.onNext(plugin);
+                        } catch (Exception e) {
+                            e.printStackTrace();
+                            emitter.onError(e);
+                        }
+                    } else {
+                        emitter.onNext(plugin);
+                    }
+                    break;
+                case DownloadUtil.STATUS_ING:
+                    emitter.onError(new Throwable("更新数据中,请稍候再试"));
+                    break;
+                case DownloadUtil.STATUS_INIT:
+                case DownloadUtil.STATUS_FAIL:
+                default:
+                    emitter.onError(new Throwable("敬请期待"));
+                    break;
             }
         }).subscribeOn(Schedulers.io());
     }
@@ -107,7 +122,7 @@ public class PluginUtil {
                                     return;
                                 }
                             }
-                            downloadPlugin(result.getDowload_url(), getFileName(plugin));
+                            downloadPlugin(result.getDowload_url(), plugin);
                         }
 //                        boolean isNewData = DataUtil.getInstance().isNewData(key);
 //                        if(isNewData){
@@ -122,8 +137,9 @@ public class PluginUtil {
                 });
     }
 
-    private static void downloadPlugin(String dowload_url, String fileName) {
-        OkHttpUtils.get().url(dowload_url).build().execute(new FileCallBack(getDirPath(), fileName) {
+    private static void downloadPlugin(String dowload_url, Plugin plugin) {
+        DataUtil.putAsInt(getDownloadPluginKey(plugin), DownloadUtil.STATUS_ING);
+        OkHttpUtils.get().url(dowload_url).build().execute(new FileCallBack(getDirPath(), getFileName(plugin)) {
             @Override
             public void inProgress(float progress, long total, int id) {
                 LogUtil.println("downloadPlugin", dowload_url, String.format(Locale.CHINA, "%d%%", Math.round(progress * 100)));
@@ -132,11 +148,13 @@ public class PluginUtil {
             @Override
             public void onError(Call call, Exception e, int id) {
                 LogUtil.println("downloadPlugin", dowload_url, "下载失败");
+                DataUtil.putAsInt(getDownloadPluginKey(plugin), DownloadUtil.STATUS_FAIL);
             }
 
             @Override
             public void onResponse(File response, int id) {
                 LogUtil.println("downloadPlugin", dowload_url, "下载完成");
+                DataUtil.putAsInt(getDownloadPluginKey(plugin), DownloadUtil.STATUS_FINISH);
             }
         });
     }
@@ -149,17 +167,20 @@ public class PluginUtil {
         return SheepApp.getInstance().getDir("plugin", Context.MODE_PRIVATE).getPath();
     }
     public static File getFile(Plugin plugin) {
-        String packageName = plugin.getPackageName();
-        String type = PluginUtil.getCPU_ABI();
-        int versionCode = BuildConfig.VERSION_CODE;
-        String key = ApiKey.getPackageVersion(packageName, type, versionCode);
+        String key = getKey(plugin);
         return getFile(key);
     }
-    public static String getFileName(Plugin plugin) {
+    public static String getDownloadPluginKey(Plugin plugin){
+        return "download_plugin_" + getKey(plugin);
+    }
+    public static String getKey(Plugin plugin){
         String packageName = plugin.getPackageName();
         String type = PluginUtil.getCPU_ABI();
         int versionCode = BuildConfig.VERSION_CODE;
-        String key = ApiKey.getPackageVersion(packageName, type, versionCode);
+        return ApiKey.getPackageVersion(packageName, type, versionCode);
+    }
+    public static String getFileName(Plugin plugin) {
+        String key = getKey(plugin);
         return key.hashCode()+".apk";
     }
     public static File getFile(String key) {