fix: validate cached APK version before reuse
This commit is contained in:
1 parent
3d50373f06
commit
497e46adc4
1 file changed
+56
-5
@@ -3,6 +3,7 @@ package com.hinnka.mycamera.update
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageInfo
|
||||
import android.net.Uri
|
||||
import android.os.Environment
|
||||
import android.provider.Settings
|
||||
@@ -71,6 +72,7 @@ object AppUpdateManager {
|
||||
}
|
||||
|
||||
val body = response.body?.string().orEmpty()
|
||||
PLog.d(TAG, "version: $body")
|
||||
val root = JSONObject(body)
|
||||
if (!root.optBoolean("has_update", false)) {
|
||||
PLog.d(TAG, "No update available: current=$currentVersion")
|
||||
@@ -86,8 +88,7 @@ object AppUpdateManager {
|
||||
PLog.w(TAG, "Update found but APK asset is missing")
|
||||
return@withContext null
|
||||
}
|
||||
val versionName = root.optString("version_name").takeIf { it.isNotBlank() }
|
||||
?: root.optString("tag_name").takeIf { it.isNotBlank() }
|
||||
val versionName = root.optString("latest_version")
|
||||
|
||||
AppUpdateRelease(
|
||||
versionName = versionName,
|
||||
@@ -123,7 +124,7 @@ object AppUpdateManager {
|
||||
|
||||
fun findDownloadedApk(context: Context, release: AppUpdateRelease): File? {
|
||||
val apkFile = resolveApkFile(context.applicationContext, release)
|
||||
return apkFile.takeIf { it.isCompleteApk() }
|
||||
return apkFile.takeIf { it.isUsableForRelease(context.applicationContext, release) }
|
||||
}
|
||||
|
||||
fun consumeReadyApk(apkFile: File?) {
|
||||
@@ -186,13 +187,14 @@ object AppUpdateManager {
|
||||
val deferred = downloadScope.async {
|
||||
runCatching {
|
||||
val apkFile = resolveApkFile(context, release)
|
||||
if (apkFile.isCompleteApk()) {
|
||||
if (apkFile.isUsableForRelease(context, release)) {
|
||||
PLog.d(TAG, "Reuse downloaded APK: ${apkFile.absolutePath}, size=${apkFile.length()}")
|
||||
return@async apkFile
|
||||
}
|
||||
|
||||
discardInvalidCachedApk(context, release, apkFile)
|
||||
downloadApkFile(release.downloadUrl, apkFile)
|
||||
if (!apkFile.isCompleteApk()) {
|
||||
if (!apkFile.isUsableForRelease(context, release)) {
|
||||
throw IllegalStateException("Downloaded APK is invalid: ${apkFile.absolutePath}")
|
||||
}
|
||||
PLog.d(TAG, "APK downloaded: ${apkFile.absolutePath}, size=${apkFile.length()}")
|
||||
@@ -290,4 +292,53 @@ object AppUpdateManager {
|
||||
input.read().toByte() == APK_MAGIC_0 && input.read().toByte() == APK_MAGIC_1
|
||||
}
|
||||
}
|
||||
|
||||
private fun File.isUsableForRelease(context: Context, release: AppUpdateRelease): Boolean {
|
||||
if (!isCompleteApk()) {
|
||||
PLog.d(TAG, "Cached APK missing or incomplete: $absolutePath")
|
||||
return false
|
||||
}
|
||||
|
||||
val packageInfo = readArchivePackageInfo(context) ?: run {
|
||||
PLog.w(TAG, "Cached APK cannot be parsed: $absolutePath")
|
||||
return false
|
||||
}
|
||||
|
||||
val expectedVersionName = release.versionName?.normalizedVersionName()
|
||||
if (expectedVersionName == null) {
|
||||
PLog.w(TAG, "Latest release version is missing, cached APK will not be reused")
|
||||
return false
|
||||
}
|
||||
|
||||
val actualVersionName = packageInfo.versionName?.normalizedVersionName()
|
||||
if (actualVersionName != expectedVersionName) {
|
||||
PLog.w(
|
||||
TAG,
|
||||
"Cached APK version mismatch: expected=$expectedVersionName, actual=$actualVersionName"
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun File.readArchivePackageInfo(context: Context): PackageInfo? =
|
||||
context.packageManager.getPackageArchiveInfo(absolutePath, 0)
|
||||
|
||||
private fun String.normalizedVersionName(): String =
|
||||
trim().removePrefix("v").removePrefix("V")
|
||||
|
||||
private fun discardInvalidCachedApk(context: Context, release: AppUpdateRelease, apkFile: File) {
|
||||
if (!apkFile.exists()) return
|
||||
if (apkFile.isUsableForRelease(context, release)) return
|
||||
|
||||
runCatching {
|
||||
if (apkFile.delete()) {
|
||||
PLog.d(TAG, "Deleted invalid cached APK: ${apkFile.absolutePath}")
|
||||
}
|
||||
}.onFailure { error ->
|
||||
PLog.w(TAG, "Failed to delete invalid cached APK: ${apkFile.absolutePath}", error)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user