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