Update UpdateManager.kt

This commit is contained in:
MNCHL 2023-12-26 20:24:47 +08:00 committed by GitHub
parent 9d69c94e63
commit 7a4c2616b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,12 @@
package org.yuzu.yuzu_emu package org.yuzu.yuzu_emu
import android.app.DownloadManager
import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.util.Log import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.os.Environment
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -12,94 +17,138 @@ import okhttp3.*
import org.json.JSONObject import org.json.JSONObject
import java.io.IOException import java.io.IOException
interface VersionCheckCallback {
fun onLatestVersionFetched(latestVersion: String)
fun onError(errorMessage: String)
}
object UpdateManager { object UpdateManager {
private val client = OkHttpClient() private val client = OkHttpClient()
private var versionCheckCallback: VersionCheckCallback? = null
fun checkAndInstallUpdate(context: Context, callback: VersionCheckCallback) {
this.versionCheckCallback = callback
fun checkAndInstallUpdate(context: Context) {
GlobalScope.launch(Dispatchers.IO) { GlobalScope.launch(Dispatchers.IO) {
val currentVersionName = val versionInfo = getLatestVersionInfoFromServer()
context.packageManager.getPackageInfo(context.packageName, 0).versionName
val latestVersionName = getLatestVersionNameFromServer()
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
if (isNewVersionAvailable(currentVersionName, latestVersionName)) { if (versionInfo != null) {
showUpdateDialog(context) val currentVersionName = context.packageManager
showUpdateAvailableMessage(context) .getPackageInfo(context.packageName, 0).versionName
if (isNewVersionAvailable(currentVersionName, versionInfo.versionName)) {
showUpdateDialog(context, versionInfo.title, versionInfo.message)
} else {
showNoUpdateAvailableMessage(context)
}
} else { } else {
showNoUpdateAvailableMessage(context) // 处理无法获取版本信息的情况
} }
} }
} }
} }
private suspend fun getLatestVersionNameFromServer() { private suspend fun getLatestVersionInfoFromServer(): VersionInfo? {
val request = Request.Builder() val request = Request.Builder()
.url("https://your-server.com/api/getLatestVersion") .url("https://your-server.com/api/getLatestVersionInfo")
.build() .build()
try { return try {
client.newCall(request).enqueue(object : Callback { val response: Response = client.newCall(request).execute()
override fun onResponse(call: Call, response: Response) { val responseBody = response.body?.string()
val responseBody = response.body?.string()
if (responseBody != null) { if (responseBody != null) {
val jsonObject = JSONObject(responseBody) val jsonObject = JSONObject(responseBody)
val latestVersion = jsonObject.getString("versionName") val versionName = jsonObject.getString("versionName")
val title = jsonObject.optString("title", "")
// 调用回调函数,传递最新版本名称 val message = jsonObject.optString("message", "")
versionCheckCallback?.onLatestVersionFetched(latestVersion) VersionInfo(versionName, title, message)
} else { } else {
// 处理没有响应体的情况 null
versionCheckCallback?.onError("Response body is empty") }
}
}
override fun onFailure(call: Call, e: IOException) {
Log.e("UpdateManager", "Error checking for updates: ${e.message}")
// 调用回调函数,传递错误消息
versionCheckCallback?.onError(e.message ?: "Unknown error")
}
})
} catch (e: IOException) { } catch (e: IOException) {
Log.e("UpdateManager", "Error checking for updates: ${e.message}") e.printStackTrace()
null
// 调用回调函数,传递错误消息
versionCheckCallback?.onError(e.message ?: "Unknown error")
} }
} }
private data class VersionInfo(val versionName: String, val title: String, val message: String)
private fun isNewVersionAvailable(currentVersion: String, latestVersion: String): Boolean { private fun isNewVersionAvailable(currentVersion: String, latestVersion: String): Boolean {
return latestVersion.compareTo(currentVersion) > 0 return latestVersion.compareTo(currentVersion) > 0
} }
private fun showUpdateDialog(context: Context) { private fun showUpdateDialog(context: Context, title: String, message: String) {
AlertDialog.Builder(context) AlertDialog.Builder(context)
.setTitle("发现新版本") .setTitle(title)
.setMessage("有新版本可用,是否立即更新?") .setMessage(message)
.setPositiveButton("更新") { dialog, which -> .setPositiveButton("更新") { dialog, which ->
// 处理更新操作 // 下载并安装更新
downloadAndInstallUpdate(context)
} }
.setNegativeButton("稍后") { dialog, which -> .setNegativeButton("稍后") { dialog, which ->
// 稍后处理更新操作 // 处理稍后更新操作
} }
.show() .show()
} }
private fun showUpdateAvailableMessage(context: Context) {
Toast.makeText(context, "发现新版本,请及时更新。", Toast.LENGTH_LONG).show()
}
private fun showNoUpdateAvailableMessage(context: Context) { private fun showNoUpdateAvailableMessage(context: Context) {
Toast.makeText(context, "您的应用已经是最新版本。", Toast.LENGTH_SHORT).show() Toast.makeText(context, "您的应用已经是最新版本。", Toast.LENGTH_SHORT).show()
} }
private fun downloadAndInstallUpdate(context: Context) {
val downloadUrl = "https://your-server.com/api/downloadLatestVersion" // 替换为实际的下载链接
val request = DownloadManager.Request(Uri.parse(downloadUrl))
request.setTitle("App更新")
request.setDescription("正在下载新版本...")
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
"yuzu.apk"
)
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val downloadId = downloadManager.enqueue(request)
// 您可以存储下载ID并用于检查下载状态或处理重试
// 当下载完成时安装下载的APK
val onCompleteReceiver = DownloadCompleteReceiver()
onCompleteReceiver.setDownloadId(downloadId)
context.registerReceiver(
onCompleteReceiver,
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
)
}
}
class DownloadCompleteReceiver : BroadcastReceiver() {
private var downloadId: Long = -1
fun setDownloadId(id: Long) {
downloadId = id
}
override fun onReceive(context: Context?, intent: Intent?) {
if (context == null || intent == null) {
return
}
if (intent.action == DownloadManager.ACTION_DOWNLOAD_COMPLETE) {
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val query = DownloadManager.Query().setFilterById(downloadId)
val cursor = downloadManager.query(query)
if (cursor.moveToFirst()) {
val status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
if (status == DownloadManager.STATUS_SUCCESSFUL) {
val uri = Uri.parse(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)))
installApk(context, uri)
} else {
}
}
cursor.close()
}
}
private fun installApk(context: Context, uri: Uri) {
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri, "application/vnd.android.package-archive")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}
} }