This commit is contained in:
fengge 2026-07-15 11:23:18 +08:00
parent 776eb9daae
commit 850550c392
1 changed files with 13 additions and 13 deletions

View File

@ -106,7 +106,7 @@ class UpdateManager(private val context: Context) {
handler.post { Toast.makeText(context.applicationContext, "更新检查失败: ${response.code}", Toast.LENGTH_SHORT).show() }
return
}
val json = JSONObject(body)
if (json.getInt("code") == 0) {
val data = json.optJSONObject("data")
@ -115,9 +115,9 @@ class UpdateManager(private val context: Context) {
if (newVersionCode > currentVersionCode) {
val versionName = data.optString("versionName", "New")
val content = data.optString("updateContent", "优化系统体验")
val id = data.optLong("id", -1)
if (id != -1L) {
val id = data.optString("id", "")
if (id != "") {
(context as? AppCompatActivity)?.runOnUiThread {
try {
showUpdateDialog(versionName, content, id)
@ -144,7 +144,7 @@ class UpdateManager(private val context: Context) {
})
}
private fun showUpdateDialog(versionName: String, content: String, id: Long) {
private fun showUpdateDialog(versionName: String, content: String, id: String) {
val view = (context as AppCompatActivity).layoutInflater.inflate(R.layout.dialog_update, null)
val tvTitle = view.findViewById<android.widget.TextView>(R.id.tv_update_title)
val tvContent = view.findViewById<android.widget.TextView>(R.id.tv_update_content)
@ -162,7 +162,7 @@ class UpdateManager(private val context: Context) {
.setCancelable(false)
.setPositiveButton("立即升级") { _, _ ->
val serverUrl = AppConfig.getServerUrl(context)
val downloadUrl = "$serverUrl/app-version/download/$id"
val downloadUrl = "$serverUrl/api/app-version/download/$id"
startManualDownload(downloadUrl)
}
.setNegativeButton("稍后再说", null)
@ -171,7 +171,7 @@ class UpdateManager(private val context: Context) {
private fun startManualDownload(url: String) {
showProgressDialog()
val client = NetworkModule.defaultClient
val request = Request.Builder()
.url(url)
@ -221,16 +221,16 @@ class UpdateManager(private val context: Context) {
outputStream = context.contentResolver.openOutputStream(targetUri)
inputStream = body.byteStream()
val totalBytes = body.contentLength()
val buffer = ByteArray(8192)
var bytesRead: Int
var totalRead: Long = 0
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
outputStream?.write(buffer, 0, bytesRead)
totalRead += bytesRead
if (totalBytes > 0) {
val progress = (totalRead * 100 / totalBytes).toInt()
handler.post {
@ -240,7 +240,7 @@ class UpdateManager(private val context: Context) {
}
}
outputStream?.flush()
handler.post {
progressDialog?.dismiss()
Toast.makeText(context.applicationContext, "下载完成,文件已保存到系统下载目录", Toast.LENGTH_SHORT).show()
@ -287,7 +287,7 @@ class UpdateManager(private val context: Context) {
val intent = Intent(Intent.ACTION_VIEW)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
// 如果是 content:// URI 直接使用,否则通过 FileProvider 转换
val installUri = if (uri.scheme == "content") {
uri
@ -295,7 +295,7 @@ class UpdateManager(private val context: Context) {
val file = File(uri.path!!)
FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file)
}
intent.setDataAndType(installUri, "application/vnd.android.package-archive")
context.startActivity(intent)
} catch (e: Exception) {