应用内更新

This commit is contained in:
HapeLee
2025-10-22 18:06:33 +08:00
parent 59f7dc8646
commit 4a9aed9223
3 changed files with 19 additions and 27 deletions
@@ -1,5 +1,6 @@
package io.legato.kazusa.help.update
import android.os.Build
import androidx.annotation.Keep
import com.google.gson.annotations.SerializedName
import io.legato.kazusa.exception.NoStackTraceException
@@ -39,17 +40,25 @@ data class GithubRelease(
val createdAt: String?
) {
fun gitReleaseToAppReleaseInfo(): List<AppReleaseInfo> {
val version = tagName // 直接用 release 的 tag_name 作为版本号
assets ?: throw NoStackTraceException("获取新版本出错")
val version = tagName
val abi = Build.SUPPORTED_ABIS.firstOrNull() ?: ""
val abiSuffix = when {
abi.contains("arm64") -> "arm64-v8a"
abi.contains("armeabi") -> "armeabi-v7a"
else -> ""
}
return assets
.filter { it.isValid }
.filter { asset ->
abiSuffix.isEmpty() || asset.name.contains(abiSuffix, ignoreCase = true)
}
.map { it.assetToAppReleaseInfo(isPreRelease, body, version) }
}
}
@Keep
data class Asset(
@SerializedName("browser_download_url")
@@ -71,7 +80,6 @@ data class Asset(
fun assetToAppReleaseInfo(preRelease: Boolean, note: String, version: String): AppReleaseInfo {
val instant = Instant.parse(createdAt)
val timestamp: Long = instant.toEpochMilli()
val appVariant = if (preRelease) AppVariant.BETA_RELEASE else AppVariant.OFFICIAL
return AppReleaseInfo(
@@ -81,8 +89,7 @@ data class Asset(
name = name,
downloadUrl = apkUrl,
assetUrl = url,
versionName = version // 直接传入 tagName
versionName = version
)
}
}
}
@@ -41,7 +41,6 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
releases.filter { it.isPreRelease }
.flatMap { it.gitReleaseToAppReleaseInfo() }
.sortedByDescending { it.createdAt }
.also { Log.d("AppUpdate", "filtered beta releases.size=${it.size}") }
} else {
val release = GSON.fromJsonObject<GithubRelease>(body)
.getOrElse { throw NoStackTraceException("获取新版本出错 ${it.localizedMessage}") }
@@ -130,5 +129,4 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
fun String.versionCompare(other: String): Int {
return SemVer.parse(this).compareTo(SemVer.parse(other))
}
}
}
@@ -69,28 +69,15 @@ class UpdateDialog() : BaseBottomSheetDialogFragment(R.layout.dialog_update) {
}
binding.btnUpdate.setOnClickListener {
val urlPrefix = arguments?.getString("url")
val baseName = arguments?.getString("name")?.removeSuffix(".apk")
val url = arguments?.getString("url")
val fileName = arguments?.getString("name")
if (urlPrefix == null || baseName == null) {
if (url.isNullOrBlank() || fileName.isNullOrBlank()) {
toastOnUi("下载信息不完整")
return@setOnClickListener
}
// 获取当前 ABI 后缀
val abi = Build.SUPPORTED_ABIS.firstOrNull() ?: ""
val abiSuffix = when {
abi.contains("arm64") -> "arm64-v8a"
abi.contains("armeabi") -> "armeabi-v7a"
else -> ""
}
// 拼接文件名和下载链接
val fileName = "${baseName}_$abiSuffix.apk"
val fullUrl = urlPrefix
.substringBeforeLast("/") + "/" + fileName
Download.start(requireContext(), fullUrl, fileName)
Download.start(requireContext(), url, fileName)
toastOnUi("开始下载: $fileName")
}