Przeglądaj źródła

静默升级功能

sunqiang 11 miesięcy temu
rodzic
commit
ce50af7a24

+ 76 - 80
app/src/main/java/com/example/chemical/ui/SplashActivity.kt

@@ -61,36 +61,36 @@ class SplashActivity : RcBaseActivity<ActivitySplashBinding>() {
 
     private fun requestPermission() {
         val disposable = RxPermissions(this)
-            .request(
-                Manifest.permission.WRITE_EXTERNAL_STORAGE,
-                Manifest.permission.READ_EXTERNAL_STORAGE,
-                Manifest.permission.CAMERA,
-                Manifest.permission.READ_PHONE_STATE,
-                Manifest.permission.RECORD_AUDIO,
-            )
-            .subscribe { granted ->
-                if (granted) {
-                    val isNetworkAvailable = NetworkUtils.isNetworkAvailable(this)
-                    if (isNetworkAvailable) {
-                        // 当前网络可用
-                        queryAppVersion()
-                    } else {
-                        if (!isOk) {
-                            // 启动定时任务
-                            myHandler.start()
-                            isOk = true
-                        }
+                .request(
+                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
+                        Manifest.permission.READ_EXTERNAL_STORAGE,
+                        Manifest.permission.CAMERA,
+                        Manifest.permission.READ_PHONE_STATE,
+                        Manifest.permission.RECORD_AUDIO,
+                )
+                .subscribe { granted ->
+                    if (granted) {
+                        val isNetworkAvailable = NetworkUtils.isNetworkAvailable(this)
+                        if (isNetworkAvailable) {
+                            // 当前网络可用
+                            queryAppVersion()
+                        } else {
+                            if (!isOk) {
+                                // 启动定时任务
+                                myHandler.start()
+                                isOk = true
+                            }
 
+                        }
+                    } else {
+                        AlertDialog.Builder(this)
+                                .setTitle("提示")
+                                .setMessage("您必须同意所有权限才可以继续使用")
+                                .setNegativeButton("确定") { _, _ ->
+                                    requestPermission()
+                                }.show()
                     }
-                } else {
-                    AlertDialog.Builder(this)
-                        .setTitle("提示")
-                        .setMessage("您必须同意所有权限才可以继续使用")
-                        .setNegativeButton("确定") { _, _ ->
-                            requestPermission()
-                        }.show()
                 }
-            }
         addDisposable(disposable)
 
     }
@@ -151,69 +151,64 @@ class SplashActivity : RcBaseActivity<ActivitySplashBinding>() {
         //    AIO_CHEMICAL("aio_chemical","化学品一体机"),
         //    AIO_INFOBORD("aio_infobord","电子信息牌"),
         //    AIO_EXAM("aio_exam","学习考试一体机");
-        try {
-            val disposable =
+        val disposable =
                 ApiRepository.getCheck(mDeviceNum!!, "aio_chemical", toDouble.toDouble())
-                    .subscribe({ data ->
-                        dismissLoading()
-                        if (!data.needUpgrade) {
-                            ChemicalApp.labInfo = data.labInfo
-                            ChemicalApp.subjectId = data.labInfo.subjectId
-                            ChemicalApp.subjectName = data.labInfo.subjectName
-                            ChemicalApp.subRoom = data.labInfo.room
-                            UiManager.switcher(this@SplashActivity, MainActivity::class.java)
-                            finish()
-                        } else {
-                            //需要更新
-                            if (data.appInfo != null) {
-                                when {
-                                    data.appInfo!!.remark != null -> {
-                                        //同步进行更新
-                                        upDownloadManager(data!!.appInfo!!.remark)
-                                    }
+                        .subscribe({ data ->
+                            dismissLoading()
+                            if (!data.needUpgrade) {
+                                ChemicalApp.labInfo = data.labInfo
+                                ChemicalApp.subjectId = data.labInfo.subjectId
+                                ChemicalApp.subjectName = data.labInfo.subjectName
+                                ChemicalApp.subRoom = data.labInfo.room
+                                UiManager.switcher(this@SplashActivity, MainActivity::class.java)
+                                finish()
+                            } else {
+                                //需要更新
+                                if (data.appInfo != null) {
+                                    when {
+                                        data.appInfo!!.remark != null -> {
+                                            //同步进行更新
+                                            upDownloadManager(data!!.appInfo!!.remark)
+                                        }
 
-                                    data!!.appInfo!!.url != null -> {
-                                        upDownloadManager(data.appInfo!!.url)
-                                    }
+                                        data!!.appInfo!!.url != null -> {
+                                            upDownloadManager(data.appInfo!!.url)
+                                        }
 
-                                    else -> {
-                                        // 启动定时任务
-                                        try {
-                                            myHandler.stop()
-                                        } catch (e: Exception) {
+                                        else -> {
+                                            // 启动定时任务
+                                            try {
+                                                myHandler.stop()
+                                            } catch (e: Exception) {
 
-                                        } finally {
-                                            myHandler.start()
+                                            } finally {
+                                                myHandler.start()
+                                            }
+                                            showToast("版本信息不不存在")
                                         }
-                                        showToast("版本信息不不存在")
                                     }
+                                } else {
+                                    UiManager.switcher(this@SplashActivity, MainActivity::class.java)
+                                    finish()
                                 }
-                            } else {
-                                UiManager.switcher(this@SplashActivity, MainActivity::class.java)
-                                finish()
                             }
-                        }
-                    }, { throwable ->
-                        dismissLoading()
-                        showNetError(throwable)
-                        if (!isOk) {
-                            // 启动定时任务
-                            myHandler.start()
-                            isOk = true
-                        }
-                    })
-            addDisposable(disposable)
-        } catch (e: Exception) {
-            RcLog.info("====地址配置错误")
-            showToast("地址配置错误")
-            dismissLoading()
-        }
+                        }, { throwable ->
+                            dismissLoading()
+                            showNetError(throwable)
+                            if (!isOk) {
+                                // 启动定时任务
+                                myHandler.start()
+                                isOk = true
+                            }
+                        })
+        addDisposable(disposable)
+
     }
 
 
     private fun upDownloadManager(downloadUrl: String) {
         ApkUpdater(this, BuildConfig.APPLICATION_ID, callback = object :
-            ApkUpdater.DownloadCallback {
+                ApkUpdater.DownloadCallback {
             override fun onProgress(progress: Int) {
                 myHandler.stop()
                 showLoading("检测到新版本,开始更新(${progress}%)")
@@ -234,8 +229,9 @@ class SplashActivity : RcBaseActivity<ActivitySplashBinding>() {
 
             override fun onSuccess(apkFile: String) {
                 dismissLoading()
-                UiManager.switcher(this@SplashActivity, MainActivity::class.java)
-                finish()
+                installApk(File(apkFile))
+//                UiManager.switcher(this@SplashActivity, MainActivity::class.java)
+//                finish()
             }
 
         }).downloadApk(downloadUrl)
@@ -294,7 +290,7 @@ class SplashActivity : RcBaseActivity<ActivitySplashBinding>() {
         if (Build.VERSION.SDK_INT >= 24) {
             // 与AndroidManifest.xml中的authorities配置一致
             val authority =
-                BuildConfig.APPLICATION_ID + ".fileprovider"
+                    BuildConfig.APPLICATION_ID + ".fileprovider"
             intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
             val uri = FileProvider.getUriForFile(this@SplashActivity, authority, apkFile)
             intent.setDataAndType(uri, "application/vnd.android.package-archive")