mirror of
https://github.com/yuzu-emu/yuzu-android.git
synced 2025-09-07 12:23:27 +00:00
Merge pull request #34 from MNCHL/MNCHL-patch-35
Update UpdateManager.kt
This commit is contained in:
commit
6b4479cedd
|
@ -1,17 +1,22 @@
|
||||||
package org.yuzu.yuzu_emu
|
package org.yuzu.yuzu_emu
|
||||||
|
|
||||||
|
import android.app.ProgressDialog
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import android.os.Environment
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.Toast
|
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.core.content.FileProvider
|
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 okhttp3.*
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import java.io.*
|
import java.io.File
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
object UpdateManager {
|
object UpdateManager {
|
||||||
|
|
||||||
|
@ -53,11 +58,15 @@ object UpdateManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showUpdateDialog(context: Context) {
|
private fun showUpdateDialog(context: Context) {
|
||||||
|
val downloadDirectory =
|
||||||
|
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
|
||||||
|
val apkFile = File(downloadDirectory, "update.apk")
|
||||||
|
|
||||||
AlertDialog.Builder(context)
|
AlertDialog.Builder(context)
|
||||||
.setTitle("有新版本可用")
|
.setTitle("有新版本可用")
|
||||||
.setMessage("有新版本可用。现在更新吗?")
|
.setMessage("有新版本可用。现在更新吗?")
|
||||||
.setPositiveButton("更新") { dialog, which ->
|
.setPositiveButton("更新") { dialog, which ->
|
||||||
downloadAndInstallUpdate(context)
|
downloadAndInstallUpdate(context, apkFile)
|
||||||
}
|
}
|
||||||
.setNegativeButton("稍后") { dialog, which ->
|
.setNegativeButton("稍后") { dialog, which ->
|
||||||
// 处理稍后更新的逻辑
|
// 处理稍后更新的逻辑
|
||||||
|
@ -66,39 +75,65 @@ object UpdateManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showNoUpdateMessage(context: Context) {
|
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 downloadUrl = "http://mkoc.cn/app/yuzu.apk"
|
||||||
val request = Request.Builder()
|
val request = Request.Builder()
|
||||||
.url(downloadUrl)
|
.url(downloadUrl)
|
||||||
.build()
|
.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 {
|
client.newCall(request).enqueue(object : Callback {
|
||||||
override fun onFailure(call: Call, e: IOException) {
|
override fun onFailure(call: Call, e: IOException) {
|
||||||
Log.e("UpdateManager", "下载失败: ${e.message}")
|
Log.e("UpdateManager", "下载失败: ${e.message}")
|
||||||
|
progressDialog.dismiss()
|
||||||
// 处理下载失败
|
// 处理下载失败
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResponse(call: Call, response: Response) {
|
override fun onResponse(call: Call, response: Response) {
|
||||||
if (response.isSuccessful) {
|
if (response.isSuccessful) {
|
||||||
val apkFile = File(context.getExternalFilesDir(null), "update.apk")
|
|
||||||
val inputStream = response.body?.byteStream()
|
val inputStream = response.body?.byteStream()
|
||||||
|
val totalSize = response.body?.contentLength() ?: 0
|
||||||
|
|
||||||
try {
|
try {
|
||||||
inputStream?.use { input ->
|
inputStream?.use { input ->
|
||||||
apkFile.outputStream().use { output ->
|
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)
|
installUpdate(context, apkFile)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
Log.e("UpdateManager", "复制文件时出错: ${e.message}")
|
Log.e("UpdateManager", "复制文件时出错: ${e.message}")
|
||||||
|
progressDialog.dismiss()
|
||||||
// 处理文件复制错误
|
// 处理文件复制错误
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.e("UpdateManager", "下载失败: HTTP ${response.code}")
|
Log.e("UpdateManager", "下载失败: HTTP ${response.code()}")
|
||||||
|
progressDialog.dismiss()
|
||||||
// 处理下载失败
|
// 处理下载失败
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue