Update UpdateManager.kt

This commit is contained in:
MNCHL 2023-12-27 12:59:07 +08:00 committed by GitHub
parent 2db81e6a2b
commit 7bcca32c83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,35 +1,46 @@
package org.yuzu.yuzu_emu
import android.app.DownloadManager
import android.content.Context
import android.content.Intent
import android.content.*
import android.net.Uri
import android.os.Bundle
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.Response
import androidx.core.content.FileProvider
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.*
import okhttp3.*
import org.json.JSONObject
import java.io.File
import java.io.IOException
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val downloadUrl = "http://mkoc.cn/app/yuzu163.apk"
UpdateManager.checkAndInstallUpdate(this, downloadUrl)
}
}
object UpdateManager {
private const val DOWNLOAD_ID = 101
private val client = OkHttpClient()
fun checkAndInstallUpdate(context: Context) {
fun checkAndInstallUpdate(context: Context, downloadUrl: String) {
GlobalScope.launch(Dispatchers.IO) {
val currentVersionName =
context.packageManager.getPackageInfo(context.packageName, 0).versionName
val currentVersionName = context.packageManager
.getPackageInfo(context.packageName, 0).versionName
val latestVersionName = getLatestVersionNameFromServer()
withContext(Dispatchers.Main) {
if (isNewVersionAvailable(currentVersionName, latestVersionName)) {
val apkUrl = "http://mkoc.cn/app/yuzu163.apk"
showUpdateDialog(context, apkUrl)
showUpdateDialog(context, downloadUrl)
showUpdateAvailableMessage(context)
} else {
showNoUpdateAvailableMessage(context)
}
@ -38,9 +49,12 @@ object UpdateManager {
}
private suspend fun getLatestVersionNameFromServer(): String {
val request = Request.Builder()
.url("http://mkoc.cn/aip/version.php")
.build()
return try {
val response: Response =
yourNetworkLibrary.executeRequest("http://mkoc.cn/aip/version.php")
val response: Response = client.newCall(request).execute()
val responseBody = response.body?.string()
if (responseBody != null) {
@ -49,69 +63,85 @@ object UpdateManager {
} else {
""
}
} catch (e: Exception) {
Log.e("UpdateManager", "检查更新时出错: ${e.message}")
} catch (e: IOException) {
Log.e("UpdateManager", "Error checking for updates: ${e.message}")
""
}
}
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, apkUrl: String) {
private fun showUpdateDialog(context: Context, downloadUrl: String) {
AlertDialog.Builder(context)
.setTitle("发现新版本")
.setMessage("有新版本可用,是否立即更新?")
.setPositiveButton("更新") { dialog, which ->
downloadAndInstallUpdate(context, apkUrl)
// 开始下载新版本
downloadLatestVersion(context, downloadUrl)
}
.setNegativeButton("稍后") { dialog, which ->
// Handle update later
// 稍后处理更新操作
}
.show()
}
private fun downloadAndInstallUpdate(context: Context, apkUrl: String) {
val downloadManager =
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val downloadUri = Uri.parse(apkUrl)
val request = DownloadManager.Request(downloadUri)
val fileName = "update.apk"
val destinationUri = Uri.fromFile(
File(
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),
fileName
)
)
request.setDestinationUri(destinationUri)
val downloadId = downloadManager.enqueue(request)
val downloadCompleteReceiver =
DownloadCompleteReceiver(downloadId) { downloadedUri ->
installApk(context, downloadedUri)
}
Toast.makeText(context, "正在下载更新...", Toast.LENGTH_LONG).show()
}
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 showUpdateAvailableMessage(context: Context) {
Toast.makeText(context, "发现新版本,请及时更新。", Toast.LENGTH_LONG).show()
}
private fun showNoUpdateAvailableMessage(context: Context) {
Toast.makeText(context, "您的应用已经是最新版本。", Toast.LENGTH_SHORT).show()
}
private fun downloadLatestVersion(context: Context, downloadUrl: String) {
val request = DownloadManager.Request(Uri.parse(downloadUrl))
.setTitle("应用更新")
.setDescription("正在下载新版本")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
"app-update.apk"
)
val downloadManager =
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val onCompleteReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
installLatestVersion(context)
context?.unregisterReceiver(this)
}
}
val intentFilter = IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
context.registerReceiver(onCompleteReceiver, intentFilter)
val downloadId = downloadManager.enqueue(request)
}
private fun installLatestVersion(context: Context?) {
val downloadManager =
context?.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val query = DownloadManager.Query().apply {
setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL)
}
val cursor = downloadManager.query(query)
if (cursor.moveToFirst()) {
val apkUri = FileProvider.getUriForFile(
context,
context.packageName + ".fileprovider",
File(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)))
)
val installIntent = Intent(Intent.ACTION_INSTALL_PACKAGE).apply {
setDataAndType(apkUri, "application/vnd.android.package-archive")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
context.startActivity(installIntent)
}
}
}