Browse Source

<增加launch版本检查>

weizhengliang 4 năm trước cách đây
mục cha
commit
a9fb517599

+ 1 - 1
app/build.gradle

@@ -26,7 +26,7 @@ android {
         minSdkVersion 15
         targetSdkVersion 26
         versionCode 1
-        versionName "1.62"
+        versionName "1.63"
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
         multiDexEnabled true
 

BIN
app/src/main/assets/launch.apk


+ 96 - 0
app/src/main/java/com/wdkl/callingdoor/MyApplication.java

@@ -5,12 +5,19 @@ import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.ServiceConnection;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
 import android.net.wifi.WifiManager;
+import android.os.Build;
+import android.os.Environment;
 import android.os.Handler;
 import android.os.IBinder;
+import android.util.Log;
 
 import com.wdkl.callingdoor.common.Constants;
 import com.wdkl.callingdoor.service.APPService;
+import com.wdkl.callingdoor.util.AutoRebootUtil;
+import com.wdkl.callingdoor.util.DownloadUtils;
 import com.wdkl.callingdoor.util.ScreenExtinguishUtil;
 import com.wdkl.callingdoor.util.SharedPreferencesUtil;
 import com.wdkl.callingdoor.util.UdpHelper;
@@ -18,6 +25,10 @@ import com.wdkl.callingdoor.util.anrfcutil.AnrFcExceptionUtil;
 import com.wdkl.callingdoor.util.ethernetwifiwithsipconnectstatus.EthernetStatus;
 import com.zhy.http.okhttp.OkHttpUtils;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 import okhttp3.ConnectionPool;
@@ -66,6 +77,9 @@ public class MyApplication extends Application {
     public static SerialPortUtil serialPortUtil;
     public static ScreenExtinguishUtil mScreenExtinguishUtil;
 
+    private boolean needCheckLaunch = true;
+    private boolean copyDone = false;
+
     public static SerialPortUtil getSerialPortUtil() {
         return serialPortUtil;
     }
@@ -134,6 +148,88 @@ public class MyApplication extends Application {
         }, 2000);
 
 
+        //检查launch版本
+        if (needCheckLaunch) {
+            checkLaunch();
+        }
+    }
+
+
+    //检查launch版本
+    private void checkLaunch() {
+        Log.d("wzlll", "start check launch");
+        PackageManager pm = getPackageManager();
+        List<PackageInfo> packageInfo = pm.getInstalledPackages(0);
+        boolean launchExist = false;
+        for (PackageInfo pInfo : packageInfo) {
+            final String launchName = "com.wdkl.launch";
+            String launchVersion = "1.3";
+            if (launchName.equals(pInfo.packageName)) {
+                launchExist = true;
+                Log.d("wzlll", "start check launch app: " + pInfo.versionName);
+                if (!launchVersion.equals(pInfo.versionName)) {
+                    Log.d("wzlll", "need update launch, app name: " + pInfo.packageName + ", app version: " + pInfo.versionName);
+                    updateLaunchApk();
+                }
+            }
+        }
+
+        Log.d("wzlll", "launch app exist: " + launchExist);
+        /*if (!launchExist) {
+            updateLaunchApk();
+        }*/
+    }
+
+    private void updateLaunchApk() {
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                String apkName = "launch.apk";
+                copyAssetsToDst(getApplicationContext(), apkName, Environment.getExternalStorageDirectory().getPath() + "/" + Environment.DIRECTORY_DOWNLOADS);
+                if (copyDone) {
+                    String fileName = Environment.getExternalStorageDirectory().getPath() + "/" + Environment.DIRECTORY_DOWNLOADS + "/" + apkName;
+                    //升级
+                    DownloadUtils downloadUtils = new DownloadUtils(getAppContext());
+                    if (downloadUtils.silentInstall(fileName)) {
+                        try {
+                            Thread.sleep(5000);
+                        } catch (Exception e) {
+                            e.printStackTrace();
+                        }
+                        Log.d("wzlll", "install new launch success, reboot...");
+                        AutoRebootUtil.reboot();
+                    } else {
+                        Log.d("wzlll", "install new launch failed...");
+                    }
+                }
+            }
+        }).start();
+    }
+
+    //拷贝assets下面的文件到sd卡
+    private void copyAssetsToDst(Context context, String fileName, String dstPath) {
+        try {
+            copyDone = false;
+            File file = new File(dstPath);
+            if (!file.exists()) {
+                file.mkdirs();
+            }
+            File outFile = new File(dstPath + "/" + fileName);
+            InputStream is = context.getAssets().open(fileName);
+            FileOutputStream fos = new FileOutputStream(outFile);
+            byte[] buffer = new byte[1024];
+            int byteCount;
+            while ((byteCount = is.read(buffer)) != -1) {
+                fos.write(buffer, 0, byteCount);
+            }
+            fos.flush();
+            is.close();
+            fos.close();
+            copyDone = true;
+        } catch (Exception e) {
+            copyDone = false;
+            e.printStackTrace();
+        }
     }
 
     public ServiceConnection connection = new ServiceConnection() {