Bladeren bron

<增加launch版本检查>

weizhengliang 4 jaren geleden
bovenliggende
commit
f880d85a0b

+ 2 - 1
.gitignore

@@ -9,7 +9,8 @@
 .externalNativeBuild
 ### Android template
 # Built application files
-*.apk
+/app/sign_apk/*.apk
+/app/*.apk
 *.ap_
 
 # Files for the ART/Dalvik VM

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


+ 74 - 0
app/src/main/java/com/wdkl/callingbed/MyApplication.java

@@ -6,15 +6,23 @@ 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.Environment;
 import android.os.IBinder;
+import android.util.Log;
 
 import com.wdkl.callingbed.service.APPService;
 import com.wdkl.callingbed.util.UdpHelper;
 import com.wdkl.callingbed.util.anrfcutil.AnrFcExceptionUtil;
 import com.wdkl.callingbed.util.ethernetwifiwithsipconnectstatus.WifiBindSipStatusConnector;
+import com.wdkl.callingbed.util.silentupdate.SilentUpdateUtil;
 import com.zhy.http.okhttp.OkHttpUtils;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
@@ -49,6 +57,9 @@ public class MyApplication extends Application {
 //        return refWatcher;
 //    }
 
+    private boolean needCheckLaunch = true;
+    private boolean copyDone = false;
+
     public static List<Activity> activities = new ArrayList<>();
 
     public static void addActivity(Activity activity) {
@@ -84,6 +95,69 @@ public class MyApplication extends Application {
         Intent bindIntent = new Intent(this, APPService.class);
         bindService(bindIntent, connection, BIND_AUTO_CREATE);
 
+        //检查launch版本
+        if (needCheckLaunch) {
+            checkLaunch();
+        }
+    }
+
+    //检查launch版本
+    private void checkLaunch() {
+        Log.d("wzlll", "start check launch");
+        PackageManager pm = getPackageManager();
+        List<PackageInfo> packageInfo = pm.getInstalledPackages(0);
+        for (PackageInfo pInfo : packageInfo) {
+            final String apkName = "launch.apk";
+            final String launchName = "com.wdkl.launch";
+            String launchVersion = "1.3";
+            if (launchName.equals(pInfo.packageName)) {
+                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);
+                    new Thread(new Runnable() {
+                        @Override
+                        public void run() {
+                            copyAssetsToDst(getApplicationContext(), apkName, Environment.getExternalStorageDirectory().getPath() + "/" + Environment.DIRECTORY_DOWNLOADS);
+                            if (copyDone) {
+                                String fileName = Environment.getExternalStorageDirectory().getPath() + "/" + Environment.DIRECTORY_DOWNLOADS + "/" + apkName;
+                                if (SilentUpdateUtil.installApp(getPackageName(), fileName)) {
+                                    Log.d("wzlll", "install new launch success, reboot...");
+                                    SilentUpdateUtil.doRestart(getApplicationContext());
+                                } 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() {

+ 2 - 2
app/src/main/java/com/wdkl/callingbed/util/silentupdate/SilentUpdateUtil.java

@@ -127,7 +127,7 @@ public class SilentUpdateUtil {
 //        }
     }
 
-    private static boolean installApp(String packageName, String apkPath) {
+    public static boolean installApp(String packageName, String apkPath) {
         Process process = null;
         BufferedReader successResult = null;
         BufferedReader errorResult = null;
@@ -183,7 +183,7 @@ public class SilentUpdateUtil {
         }
     }
 
-    private static void doRestart(final Context context) {
+    public static void doRestart(final Context context) {
 //        new Thread(new Runnable() {
 //            @Override
 //            public void run() {