diff --git a/app/build.gradle.kts b/app/build.gradle.kts index f5c35cb..db4c680 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -18,8 +18,8 @@ android { applicationId = "com.lzxnone.openloc" minSdk = 28 targetSdk = 36 - versionCode = 6 - versionName = "1.2.3" + versionCode = 7 + versionName = "1.2.4" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/com/lzxnone/openloc/core/util/UpdateChecker.kt b/app/src/main/java/com/lzxnone/openloc/core/util/UpdateChecker.kt index 1a37dc7..12157bd 100644 --- a/app/src/main/java/com/lzxnone/openloc/core/util/UpdateChecker.kt +++ b/app/src/main/java/com/lzxnone/openloc/core/util/UpdateChecker.kt @@ -1,13 +1,16 @@ package com.lzxnone.openloc.core.util +import android.os.Build import com.lzxnone.openloc.BuildConfig import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import org.json.JSONArray import org.json.JSONObject import java.io.BufferedReader import java.io.InputStreamReader import java.net.HttpURLConnection import java.net.URL +import java.util.Locale /** * 版本更新信息 @@ -20,14 +23,14 @@ data class UpdateInfo( val apkSize: String = "" ) { val hasUpdate: Boolean - get() = versionCode > BuildConfig.VERSION_CODE + get() = UpdateChecker.isNewerVersion(versionName, BuildConfig.VERSION_NAME) } /** - * VPS 节点自建版本更新检查器 + * Gitea Releases 版本更新检查器 */ object UpdateChecker { - private const val UPDATE_URL = "https://update.lzxnone.cloud/openloc/version.json" + private const val UPDATE_URL = "https://gitea.lzxnone.cloud/api/v1/repos/lzxnone/OpenLoc/releases/latest" /** * 检查是否有新版本 @@ -49,22 +52,7 @@ object UpdateChecker { val response = reader.use { it.readText() } connection.disconnect() - val json = JSONObject(response) - val versionCode = json.optLong("versionCode", 0L) - val versionName = json.optString("versionName", "") - val changelog = json.optString("changelog", "") - val downloadUrl = json.optString("downloadUrl", "") - val apkSize = json.optString("apkSize", "") - - Result.success( - UpdateInfo( - versionCode = versionCode, - versionName = versionName, - changelog = changelog, - downloadUrl = downloadUrl, - apkSize = apkSize - ) - ) + Result.success(parseReleaseJson(response)) } else { connection.disconnect() Result.failure(Exception("HTTP error $responseCode")) @@ -73,4 +61,102 @@ object UpdateChecker { Result.failure(e) } } + + internal fun parseReleaseJson( + response: String, + supportedAbis: Array = Build.SUPPORTED_ABIS ?: emptyArray() + ): UpdateInfo { + val json = JSONObject(response) + val rawTag = json.optString("tag_name", "").trim() + val versionName = rawTag.removePrefix("v").removePrefix("V") + val versionCode = parseVersionCode(versionName) + val changelog = json.optString("body", "").ifBlank { + json.optString("name", "") + } + + val (downloadUrl, apkSize) = selectApkAsset( + assets = json.optJSONArray("assets"), + supportedAbis = supportedAbis, + fallbackUrl = json.optString("html_url", "") + ) + + return UpdateInfo( + versionCode = versionCode, + versionName = versionName, + changelog = changelog, + downloadUrl = downloadUrl, + apkSize = apkSize + ) + } + + private fun selectApkAsset( + assets: JSONArray?, + supportedAbis: Array, + fallbackUrl: String + ): Pair { + if (assets == null || assets.length() == 0) { + return fallbackUrl to "" + } + + val apkAssets = buildList { + for (i in 0 until assets.length()) { + val asset = assets.optJSONObject(i) ?: continue + val name = asset.optString("name", "") + val downloadUrl = asset.optString("browser_download_url", "") + val size = asset.optLong("size", 0L) + if (name.endsWith(".apk", ignoreCase = true) && downloadUrl.isNotBlank()) { + add(Triple(name, downloadUrl, size)) + } + } + } + + if (apkAssets.isEmpty()) { + return fallbackUrl to "" + } + + val matched = supportedAbis.firstNotNullOfOrNull { abi -> + apkAssets.firstOrNull { (name, _, _) -> + name.contains(abi, ignoreCase = true) + } + } ?: apkAssets.firstOrNull { (name, _, _) -> + name.contains("universal", ignoreCase = true) + } ?: apkAssets.first() + + val sizeText = if (matched.third > 0L) { + String.format(Locale.US, "%.2f MB", matched.third / (1024.0 * 1024.0)) + } else { + "" + } + return matched.second to sizeText + } + + internal fun isNewerVersion(remoteVersion: String, currentVersion: String): Boolean { + val remoteParts = parseVersionParts(remoteVersion) + val currentParts = parseVersionParts(currentVersion) + if (remoteParts.isEmpty()) return false + + val maxLen = maxOf(remoteParts.size, currentParts.size) + for (i in 0 until maxLen) { + val r = remoteParts.getOrElse(i) { 0 } + val c = currentParts.getOrElse(i) { 0 } + if (r != c) return r > c + } + return false + } + + internal fun parseVersionCode(versionName: String): Long { + val parts = parseVersionParts(versionName) + if (parts.isEmpty()) return 0L + val major = parts.getOrElse(0) { 0 } + val minor = parts.getOrElse(1) { 0 } + val patch = parts.getOrElse(2) { 0 } + return major * 10000L + minor * 100L + patch + } + + private fun parseVersionParts(version: String): List { + val cleaned = version.trim().removePrefix("v").removePrefix("V") + if (cleaned.isBlank()) return emptyList() + return cleaned.split(".") + .map { segment -> segment.takeWhile { it.isDigit() }.toIntOrNull() ?: 0 } + } }