mirror of
https://github.com/yuzu-emu/yuzu-android.git
synced 2025-09-05 15:03:16 +00:00
Create UpdateManager.kt
This commit is contained in:
parent
7a8a8b3f31
commit
c412e3f7f8
|
@ -0,0 +1,82 @@
|
|||
package org.yuzu.yuzu_emu
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.AsyncTask
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import com.squareup.okhttp.OkHttpClient
|
||||
import com.squareup.okhttp.Request
|
||||
import com.squareup.okhttp.Response
|
||||
import org.json.JSONObject
|
||||
|
||||
class UpdateManager(private val context: Context) {
|
||||
|
||||
private val TAG = "UpdateManager"
|
||||
|
||||
fun checkForUpdates() {
|
||||
val currentVersion = BuildConfig.VERSION_NAME // 当前应用版本
|
||||
val updateUrl = "https://your-server.com/check_update.php" // 用于检查更新的服务器端点
|
||||
|
||||
// 异步任务执行更新检查
|
||||
object : AsyncTask<Void, Void, String>() {
|
||||
override fun doInBackground(vararg params: Void): String {
|
||||
try {
|
||||
val client = OkHttpClient()
|
||||
val request = Request.Builder()
|
||||
.url(updateUrl)
|
||||
.build()
|
||||
|
||||
val response: Response = client.newCall(request).execute()
|
||||
if (response.isSuccessful) {
|
||||
return response.body().string()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error checking for updates: ${e.message}")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
override fun onPostExecute(result: String) {
|
||||
super.onPostExecute(result)
|
||||
if (result.isNotEmpty()) {
|
||||
handleUpdateResponse(result, currentVersion)
|
||||
}
|
||||
}
|
||||
}.execute()
|
||||
}
|
||||
|
||||
private fun handleUpdateResponse(response: String, currentVersion: String) {
|
||||
try {
|
||||
val jsonObject = JSONObject(response)
|
||||
val latestVersion = jsonObject.getString("version")
|
||||
val releaseNotes = jsonObject.getString("release_notes")
|
||||
val downloadUrl = jsonObject.getString("download_url")
|
||||
|
||||
if (latestVersion != currentVersion) {
|
||||
showUpdateDialog(latestVersion, releaseNotes, downloadUrl)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error parsing update response: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun showUpdateDialog(version: String, releaseNotes: String, downloadUrl: String) {
|
||||
val alertDialogBuilder = AlertDialog.Builder(context)
|
||||
alertDialogBuilder.setTitle("有新的更新可用")
|
||||
alertDialogBuilder.setMessage("版本: $version\n\n$releaseNotes")
|
||||
alertDialogBuilder.setPositiveButton("立即更新") { dialog, _ ->
|
||||
val intent = Intent(Intent.ACTION_VIEW)
|
||||
intent.data = Uri.parse(downloadUrl)
|
||||
context.startActivity(intent)
|
||||
dialog.dismiss()
|
||||
}
|
||||
alertDialogBuilder.setNegativeButton("稍后再说") { dialog, _ ->
|
||||
dialog.dismiss()
|
||||
}
|
||||
val alertDialog = alertDialogBuilder.create()
|
||||
alertDialog.show()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue