瀏覽代碼

app启动时检查launch版本,若版本低于1.3则升级

weizhengliang 3 年之前
父節點
當前提交
54e532252f

二進制
callingbed/src/main/assets/launch.apk


+ 70 - 3
callingbed/src/main/java/com/wdkl/app/ncs/callingbed/activity/CallingbedActivity.kt

@@ -71,9 +71,7 @@ import org.greenrobot.eventbus.EventBus
 import org.greenrobot.eventbus.Subscribe
 import org.greenrobot.eventbus.ThreadMode
 import serialporttest.utils.SerialPortUtil
-import java.io.DataOutputStream
-import java.io.IOException
-import java.io.PrintWriter
+import java.io.*
 import java.lang.Process
 import java.lang.ref.WeakReference
 import java.util.concurrent.TimeUnit
@@ -135,6 +133,7 @@ class CallingbedActivity :BaseActivity<CallingbedActivityPresenter, CallingbedMa
     //private val callNurseFragment = "call_nurse_fragment"
 
     private val uninstallApk = false
+    private var copyDone = false
 
     companion object {
         private const val TIME_WHAT = 1000
@@ -207,8 +206,76 @@ class CallingbedActivity :BaseActivity<CallingbedActivityPresenter, CallingbedMa
         }
 
         checkServer()
+
+        if ("rk3128".equals(Build.MODEL)) {
+            checkLaunch()
+        }
+    }
+
+    //检查launch版本
+    private fun checkLaunch() {
+        Log.d(TAG, "start check launch")
+        val pm = packageManager
+        val packageInfo = pm.getInstalledPackages(0)
+        var launchExist = false
+        for (pInfo in packageInfo) {
+            val launchName = "com.wdkl.launch"
+            val launchVersion = "1.3"
+            if (launchName == pInfo.packageName) {
+                launchExist = true
+                Log.d(TAG, "start check launch app: " + pInfo.versionName)
+                if (launchVersion != pInfo.versionName) {
+                    Log.d(TAG, "need update launch, app name: " + pInfo.packageName + ", app version: " + pInfo.versionName)
+                    updateLaunchApk()
+                }
+            }
+        }
+        Log.d(TAG, "launch app exist: $launchExist")
     }
 
+    private fun updateLaunchApk() {
+        Thread {
+            val apkName = "launch.apk"
+            copyAssetsToDst(applicationContext, apkName, Environment.getExternalStorageDirectory().path + "/" + Environment.DIRECTORY_DOWNLOADS)
+            if (copyDone) {
+                val fileName = Environment.getExternalStorageDirectory().path + "/" + Environment.DIRECTORY_DOWNLOADS + "/" + apkName
+                //升级
+                AppUpdateHelper.rootSilenceInstall(fileName)
+                //5分钟后重启设备
+                AppTool.Time.delay(300000) {
+                    AppUpdateHelper.reboot(BaseApplication.appContext)
+                }
+            }
+        }.start()
+    }
+
+    //拷贝assets下面的文件到sd卡
+    private fun copyAssetsToDst(context: Context, fileName: String, dstPath: String) {
+        try {
+            copyDone = false
+            val file = File(dstPath)
+            if (!file.exists()) {
+                file.mkdirs()
+            }
+            val outFile = File("$dstPath/$fileName")
+            val `is` = context.assets.open(fileName)
+            val fos = FileOutputStream(outFile)
+            val buffer = ByteArray(1024)
+            var byteCount: Int
+            while (`is`.read(buffer).also { byteCount = it } != -1) {
+                fos.write(buffer, 0, byteCount)
+            }
+            fos.flush()
+            `is`.close()
+            fos.close()
+            copyDone = true
+        } catch (e: java.lang.Exception) {
+            copyDone = false
+            e.printStackTrace()
+        }
+    }
+
+
     private fun checkServer() {
         Thread {
             while (!serverSuccess) {

+ 1 - 1
callingbed/src/main/java/com/wdkl/app/ncs/callingbed/fragment/QrCodeFragment.kt

@@ -84,7 +84,7 @@ class QrCodeFragment : BaseFragment<QrCodeFragmentPresenter, QrcodeViewBinding>(
         }
         tv_local_ip.text = "本机IP: " + ipAddr + " - " + serverIp
         tv_local_mac.text = "本机MAC: " + macAddr
-        tv_app_version.text = "版本信息: V" + BuildConfig.VERSION_NAME
+        tv_app_version.text = "版本信息: V" + BuildConfig.VERSION_NAME + "_" + BuildConfig.VERSION_CODE
         tv_mcu_version.text = "MCU版本: " + Constant.MCU_VERSION_NUMBER
     }