mirror of
https://github.com/yuzu-emu/yuzu-android.git
synced 2025-09-05 22:53:29 +00:00
Update UpdateManager.kt
This commit is contained in:
parent
535bd6c064
commit
752e58d84e
|
@ -1,169 +1,115 @@
|
|||
package org.yuzu.yuzu_emu
|
||||
|
||||
import android.app.DownloadManager
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.net.Uri
|
||||
import android.os.Environment
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.*
|
||||
import okhttp3.Response
|
||||
import org.json.JSONObject
|
||||
import java.io.IOException
|
||||
import java.io.File
|
||||
|
||||
object UpdateManager {
|
||||
private val client = OkHttpClient()
|
||||
|
||||
private const val DOWNLOAD_ID = 101
|
||||
|
||||
fun checkAndInstallUpdate(context: Context) {
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
val versionInfo = getLatestVersionInfoFromServer()
|
||||
val currentVersionName =
|
||||
context.packageManager.getPackageInfo(context.packageName, 0).versionName
|
||||
val latestVersionName = getLatestVersionNameFromServer()
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
if (versionInfo != null) {
|
||||
val currentVersionName = context.packageManager
|
||||
.getPackageInfo(context.packageName, 0).versionName
|
||||
if (isNewVersionAvailable(currentVersionName, versionInfo.versionName)) {
|
||||
showUpdateDialog(context, versionInfo.title, versionInfo.message)
|
||||
} else {
|
||||
showNoUpdateAvailableMessage(context)
|
||||
}
|
||||
if (isNewVersionAvailable(currentVersionName, latestVersionName)) {
|
||||
val apkUrl = "http://mkoc.cn/app/yuzu163.apk"
|
||||
showUpdateDialog(context, apkUrl)
|
||||
} else {
|
||||
// 处理无法获取版本信息的情况
|
||||
showNoUpdateAvailableMessage(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun getLatestVersionInfoFromServer(): VersionInfo? {
|
||||
val request = Request.Builder()
|
||||
.url("https://your-server.com/api/getLatestVersionInfo")
|
||||
.build()
|
||||
|
||||
private suspend fun getLatestVersionNameFromServer(): String {
|
||||
return try {
|
||||
val response: Response = client.newCall(request).execute()
|
||||
val response: Response =
|
||||
yourNetworkLibrary.executeRequest("http://mkoc.cn/aip/version.php")
|
||||
val responseBody = response.body?.string()
|
||||
|
||||
if (responseBody != null) {
|
||||
val jsonObject = JSONObject(responseBody)
|
||||
val versionName = jsonObject.getString("versionName")
|
||||
val title = jsonObject.optString("title", "发现新版本")
|
||||
val message = jsonObject.optString("message", "有新版本可用,是否立即更新?")
|
||||
VersionInfo(versionName, title, message)
|
||||
jsonObject.getString("versionName")
|
||||
} else {
|
||||
null
|
||||
""
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
null
|
||||
} catch (e: Exception) {
|
||||
Log.e("UpdateManager", "检查更新时出错: ${e.message}")
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
private fun showUpdateDialog(context: Context, title: String, message: String) {
|
||||
private fun showUpdateDialog(context: Context, apkUrl: String) {
|
||||
AlertDialog.Builder(context)
|
||||
.setTitle(title)
|
||||
.setMessage(message)
|
||||
.setTitle("发现新版本")
|
||||
.setMessage("有新版本可用,是否立即更新?")
|
||||
.setPositiveButton("更新") { dialog, which ->
|
||||
// 下载并安装更新
|
||||
downloadAndInstallUpdate(context)
|
||||
downloadAndInstallUpdate(context, apkUrl)
|
||||
}
|
||||
.setNegativeButton("稍后") { dialog, which ->
|
||||
// 处理稍后更新操作
|
||||
// Handle update later
|
||||
}
|
||||
.show()
|
||||
}
|
||||
}
|
||||
|
||||
private fun showNoUpdateAvailableMessage(context: Context) {
|
||||
Toast.makeText(
|
||||
context,
|
||||
"您的应用已经是最新版本。",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
private fun downloadAndInstallUpdate(context: Context, apkUrl: String) {
|
||||
val downloadManager =
|
||||
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
||||
|
||||
private fun downloadAndInstallUpdate(context: Context) {
|
||||
val downloadUrl =
|
||||
"https://your-server.com/api/downloadLatestVersion" // 替换为实际的下载链接
|
||||
val downloadUri = Uri.parse(apkUrl)
|
||||
val request = DownloadManager.Request(downloadUri)
|
||||
|
||||
val request = DownloadManager.Request(Uri.parse(downloadUrl))
|
||||
request.apply {
|
||||
setTitle("App更新")
|
||||
setDescription("正在下载新版本...")
|
||||
setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
||||
setDestinationInExternalPublicDir(
|
||||
Environment.DIRECTORY_DOWNLOADS,
|
||||
"YourAppName.apk"
|
||||
) // 替换为您的应用名称
|
||||
}
|
||||
val fileName = "update.apk"
|
||||
val destinationUri = Uri.fromFile(
|
||||
File(
|
||||
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),
|
||||
fileName
|
||||
)
|
||||
)
|
||||
request.setDestinationUri(destinationUri)
|
||||
|
||||
val downloadManager =
|
||||
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
||||
val downloadId = downloadManager.enqueue(request)
|
||||
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 {
|
||||
// 处理下载失败
|
||||
}
|
||||
val downloadCompleteReceiver =
|
||||
DownloadCompleteReceiver(downloadId) { downloadedUri ->
|
||||
installApk(context, downloadedUri)
|
||||
}
|
||||
|
||||
cursor.close()
|
||||
}
|
||||
Toast.makeText(context, "正在下载更新...", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
|
||||
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
|
||||
private fun installApk(context: Context, apkUri: Uri) {
|
||||
val intent = Intent(Intent.ACTION_VIEW).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
setDataAndType(apkUri, "application/vnd.android.package-archive")
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
}
|
||||
|
||||
context.startActivity(intent)
|
||||
}
|
||||
|
||||
private fun showNoUpdateAvailableMessage(context: Context) {
|
||||
Toast.makeText(context, "您的应用已经是最新版本。", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue