Merge pull request #34 from MNCHL/MNCHL-patch-35

Update UpdateManager.kt
This commit is contained in:
MNCHL 2023-12-27 22:17:01 +08:00 committed by GitHub
commit 6b4479cedd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,17 +1,22 @@
package org.yuzu.yuzu_emu
import android.app.ProgressDialog
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.content.FileProvider
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.*
import org.json.JSONObject
import java.io.*
import java.io.File
import java.io.IOException
object UpdateManager {
@ -53,11 +58,15 @@ object UpdateManager {
}
private fun showUpdateDialog(context: Context) {
val downloadDirectory =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val apkFile = File(downloadDirectory, "update.apk")
AlertDialog.Builder(context)
.setTitle("有新版本可用")
.setMessage("有新版本可用。现在更新吗?")
.setPositiveButton("更新") { dialog, which ->
downloadAndInstallUpdate(context)
downloadAndInstallUpdate(context, apkFile)
}
.setNegativeButton("稍后") { dialog, which ->
// 处理稍后更新的逻辑
@ -66,39 +75,65 @@ object UpdateManager {
}
private fun showNoUpdateMessage(context: Context) {
Toast.makeText(context, "您的应用已经是最新版本。", Toast.LENGTH_SHORT).show()
// 显示没有更新的消息
}
private fun downloadAndInstallUpdate(context: Context) {
private fun downloadAndInstallUpdate(context: Context, apkFile: File) {
val downloadUrl = "http://mkoc.cn/app/yuzu.apk"
val request = Request.Builder()
.url(downloadUrl)
.build()
val progressDialog = ProgressDialog(context)
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)
progressDialog.setTitle("下载中") // 设置对话框标题
progressDialog.setMessage("请稍候...")
progressDialog.isIndeterminate = false
progressDialog.setCancelable(false)
progressDialog.max = 100
progressDialog.show()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e("UpdateManager", "下载失败: ${e.message}")
progressDialog.dismiss()
// 处理下载失败
}
override fun onResponse(call: Call, response: Response) {
if (response.isSuccessful) {
val apkFile = File(context.getExternalFilesDir(null), "update.apk")
val inputStream = response.body?.byteStream()
val totalSize = response.body?.contentLength() ?: 0
try {
inputStream?.use { input ->
apkFile.outputStream().use { output ->
input.copyTo(output)
val buffer = ByteArray(8192)
var bytesRead: Int
var totalBytesRead: Long = 0
while (input.read(buffer).also { bytesRead = it } != -1) {
output.write(buffer, 0, bytesRead)
totalBytesRead += bytesRead.toLong()
val progress = (totalBytesRead * 100 / totalSize).toInt()
progressDialog.progress = progress
// 更新下载进度消息
val progressMessage = "下载进度: $progress%"
progressDialog.setMessage(progressMessage)
}
}
}
progressDialog.dismiss()
installUpdate(context, apkFile)
} catch (e: IOException) {
Log.e("UpdateManager", "复制文件时出错: ${e.message}")
progressDialog.dismiss()
// 处理文件复制错误
}
} else {
Log.e("UpdateManager", "下载失败: HTTP ${response.code}")
Log.e("UpdateManager", "下载失败: HTTP ${response.code()}")
progressDialog.dismiss()
// 处理下载失败
}
}