From bd54352f44f33caef57a8dbb541e5e84a372dcbb Mon Sep 17 00:00:00 2001
From: wangrong <wangrong@shsening.com>
Date: 星期六, 30 十一月 2024 15:48:02 +0800
Subject: [PATCH] app 升级功能

---
 app/src/main/java/com/shsening/autouploadvideos/utils/Utils.kt |   24 +++++++++++++++---------
 1 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/app/src/main/java/com/shsening/autouploadvideos/utils/Utils.kt b/app/src/main/java/com/shsening/autouploadvideos/utils/Utils.kt
index ed18a16..33f6b72 100644
--- a/app/src/main/java/com/shsening/autouploadvideos/utils/Utils.kt
+++ b/app/src/main/java/com/shsening/autouploadvideos/utils/Utils.kt
@@ -278,7 +278,7 @@
 
     suspend fun downloadCommonFileToExternalFilesDir(context: Context, url: String, filename: String, install: Boolean) {
         val tmpFile = File(context.getExternalFilesDir(null), filename)
-        try{
+        try {
             if (tmpFile.exists()) {
                 return
             }
@@ -292,17 +292,20 @@
                     override fun onFailure(call: Call, e: IOException) {
                         deferred.completeExceptionally(e) // 涓嬭浇澶辫触鏃舵爣璁颁负寮傚父
                     }
+
                     override fun onResponse(call: Call, response: Response) {
                         response.use {
                             if (!response.isSuccessful) {
                                 deferred.completeExceptionally(IOException("Unexpected code $response")) // 濡傛灉鍝嶅簲涓嶆垚鍔燂紝鏍囪涓哄紓甯�
                                 return
                             }
+
                             tmpFile.outputStream().use { outputStream ->
                                 outputStream.write(response.body?.bytes())
                             }
+
                             if (install) {
-                                installApk(context,tmpFile)
+                                installApk(context, tmpFile)
                             }
 
                             deferred.complete(Unit) // 涓嬭浇鎴愬姛鏃惰繑鍥炴枃浠�
@@ -311,19 +314,22 @@
                 })
             }
 
-            // 绛夊緟涓嬭浇瀹屾垚
-
-            Toast.makeText(context, filename + " " + AppConfig.MSG_DOWNLOAD_FINISHED, Toast.LENGTH_SHORT).show()
+            // Switch to main thread for Toast
+            withContext(Dispatchers.Main) {
+                Toast.makeText(context, "$filename ${AppConfig.MSG_DOWNLOAD_FINISHED}", Toast.LENGTH_SHORT).show()
+            }
 
             deferred.await()
-        }catch(e:Exception){
+        } catch (e: Exception) {
             e.printStackTrace()
 
-            Toast.makeText(context, filename+" download fails, Please try again later!", Toast.LENGTH_SHORT)
-                    .show()
-
+            // Switch to main thread for Toast
+            withContext(Dispatchers.Main) {
+                Toast.makeText(context, "$filename download fails, Please try again later!", Toast.LENGTH_SHORT).show()
+            }
         }
     }
+
     fun installApk(context: Context,apkFile: File) {
         val intent = Intent(Intent.ACTION_VIEW)
         val apkUri = FileProvider.getUriForFile(context, AppConfig.FILE_PROVIDER, apkFile)

--
Gitblit v1.9.1