JonaNorman преди 1 година
родител
ревизия
0a1f8e616d
променени са 2 файла, в които са добавени 15 реда и са изтрити 41 реда
  1. 2 5
      app/src/main/java/com/norman/webviewup/demo/MainActivity.java
  2. 13 36
      app/src/main/java/com/norman/webviewup/lib/WebViewUpgrade.java

+ 2 - 5
app/src/main/java/com/norman/webviewup/demo/MainActivity.java

@@ -71,9 +71,7 @@ public class MainActivity extends Activity implements UpgradeCallback {
             public void onClick(View v) {
                 if (WebViewUpgrade.isProcessing()) {
                     Toast.makeText(getApplicationContext(), "webView is being upgraded, please wait", Toast.LENGTH_SHORT).show();
-                } else if (WebViewUpgrade.isFailed()) {
-                    Toast.makeText(getApplicationContext(), "webView upgrade failed, please check the reason", Toast.LENGTH_SHORT).show();
-                } else if (WebViewUpgrade.isCompleted()) {
+                }  else if (WebViewUpgrade.isCompleted()) {
                     Toast.makeText(getApplicationContext(), "WebView is already upgrade success", Toast.LENGTH_SHORT).show();
                 } else {
                     showChooseWebViewDialog();
@@ -136,8 +134,7 @@ public class MainActivity extends Activity implements UpgradeCallback {
                         packageInfo.versionName,
                         new DownloadSinkImpl())
                         .build();
-                WebViewUpgrade.init(upgradeOptions);
-                WebViewUpgrade.upgrade();
+                WebViewUpgrade.upgrade(upgradeOptions);
                 updateUpgradeWebViewPackageInfo();
                 updateUpgradeWebViewStatus();
             }

+ 13 - 36
app/src/main/java/com/norman/webviewup/lib/WebViewUpgrade.java

@@ -38,13 +38,11 @@ public class WebViewUpgrade {
 
     private static final int STATUS_UNINIT = 0;
 
-    private static final int STATUS_INIT = 1;
+    private static final int STATUS_RUNNING = 1;
 
-    private static final int STATUS_RUNNING = 2;
+    private static final int STATUS_FAIL = 2;
 
-    private static final int STATUS_FAIL = 3;
-
-    private static final int STATUS_COMPLETE = 4;
+    private static final int STATUS_COMPLETE = 3;
 
     private static UpgradeOptions UPGRADE_OPTIONS;
 
@@ -56,32 +54,12 @@ public class WebViewUpgrade {
     private static String SYSTEM_WEB_VIEW_PACKAGE_NAME;
 
     private static String SYSTEM_WEB_VIEW_PACKAGE_VERSION;
-
-    private static String UPGRADE_WEB_VIEW_PACKAGE_NAME;
-
-    private static String UPGRADE_WEB_VIEW_PACKAGE_VERSION;
-
-
     private static Throwable UPGRADE_THROWABLE;
 
     private static final Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());
 
 
 
-
-
-    public synchronized static void init(UpgradeOptions options) {
-        if (UPGRADE_STATUS != STATUS_UNINIT) {
-            throw new IllegalStateException("WebViewUpgrade is already init");
-        }
-        if (options == null) {
-            throw new NullPointerException("options is  null");
-        }
-        UPGRADE_OPTIONS = options;
-        UPGRADE_STATUS = STATUS_INIT;
-    }
-
-
     public synchronized static void addUpgradeCallback(UpgradeCallback upgradeCallback) {
         if (upgradeCallback == null) return;
         if (UPGRADE_CALLBACK_LIST.contains(upgradeCallback)) return;
@@ -119,15 +97,14 @@ public class WebViewUpgrade {
         return UPGRADE_PROCESS;
     }
 
-    public synchronized static void upgrade() {
+    public synchronized static void upgrade(UpgradeOptions options) {
         try {
-            if (UPGRADE_STATUS == STATUS_UNINIT) {
-                throw new IllegalStateException("please first init");
-            }
-            if (UPGRADE_STATUS != STATUS_INIT) {
+            if (UPGRADE_STATUS == STATUS_RUNNING ||UPGRADE_STATUS == STATUS_COMPLETE ) {
                 return;
             }
+            UPGRADE_OPTIONS = options;
             UPGRADE_STATUS = STATUS_RUNNING;
+            UPGRADE_THROWABLE = null;
             HandlerThread upgradeThread = new HandlerThread("WebViewUpgrade");
             upgradeThread.start();
             Handler upgradeHandler = new Handler(upgradeThread.getLooper());
@@ -337,12 +314,12 @@ public class WebViewUpgrade {
             callProcessCallback(1.0f);
             callCompleteCallback();
         } finally {
-//            if (managerHook != null) {
-//                managerHook.restore();
-//            }
-//            if (updateServiceHook != null) {
-//                updateServiceHook.restore();
-//            }
+            if (managerHook != null) {
+                managerHook.restore();
+            }
+            if (updateServiceHook != null) {
+                updateServiceHook.restore();
+            }
 
         }
     }