应用内更新

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 package io.legato.kazusa.help.update
import android.os.Build
import androidx.annotation.Keep import androidx.annotation.Keep
import com.google.gson.annotations.SerializedName import com.google.gson.annotations.SerializedName
import io.legato.kazusa.exception.NoStackTraceException import io.legato.kazusa.exception.NoStackTraceException
@@ -39,17 +40,25 @@ data class GithubRelease(
val createdAt: String? val createdAt: String?
) { ) {
fun gitReleaseToAppReleaseInfo(): List<AppReleaseInfo> { fun gitReleaseToAppReleaseInfo(): List<AppReleaseInfo> {
val version = tagName // 直接用 release 的 tag_name 作为版本号
assets ?: throw NoStackTraceException("获取新版本出错") 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 return assets
.filter { it.isValid } .filter { it.isValid }
.filter { asset ->
abiSuffix.isEmpty() || asset.name.contains(abiSuffix, ignoreCase = true)
}
.map { it.assetToAppReleaseInfo(isPreRelease, body, version) } .map { it.assetToAppReleaseInfo(isPreRelease, body, version) }
} }
} }
@Keep @Keep
data class Asset( data class Asset(
@SerializedName("browser_download_url") @SerializedName("browser_download_url")
@@ -71,7 +80,6 @@ data class Asset(
fun assetToAppReleaseInfo(preRelease: Boolean, note: String, version: String): AppReleaseInfo { fun assetToAppReleaseInfo(preRelease: Boolean, note: String, version: String): AppReleaseInfo {
val instant = Instant.parse(createdAt) val instant = Instant.parse(createdAt)
val timestamp: Long = instant.toEpochMilli() val timestamp: Long = instant.toEpochMilli()
val appVariant = if (preRelease) AppVariant.BETA_RELEASE else AppVariant.OFFICIAL val appVariant = if (preRelease) AppVariant.BETA_RELEASE else AppVariant.OFFICIAL
return AppReleaseInfo( return AppReleaseInfo(
@@ -81,8 +89,7 @@ data class Asset(
name = name, name = name,
downloadUrl = apkUrl, downloadUrl = apkUrl,
assetUrl = url, assetUrl = url,
versionName = version // 直接传入 tagName versionName = version
) )
} }
}
}
@@ -41,7 +41,6 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
releases.filter { it.isPreRelease } releases.filter { it.isPreRelease }
.flatMap { it.gitReleaseToAppReleaseInfo() } .flatMap { it.gitReleaseToAppReleaseInfo() }
.sortedByDescending { it.createdAt } .sortedByDescending { it.createdAt }
.also { Log.d("AppUpdate", "filtered beta releases.size=${it.size}") }
} else { } else {
val release = GSON.fromJsonObject<GithubRelease>(body) val release = GSON.fromJsonObject<GithubRelease>(body)
.getOrElse { throw NoStackTraceException("获取新版本出错 ${it.localizedMessage}") } .getOrElse { throw NoStackTraceException("获取新版本出错 ${it.localizedMessage}") }
@@ -130,5 +129,4 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
fun String.versionCompare(other: String): Int { fun String.versionCompare(other: String): Int {
return SemVer.parse(this).compareTo(SemVer.parse(other)) return SemVer.parse(this).compareTo(SemVer.parse(other))
} }
} }
@@ -69,28 +69,15 @@ class UpdateDialog() : BaseBottomSheetDialogFragment(R.layout.dialog_update) {
} }
binding.btnUpdate.setOnClickListener { binding.btnUpdate.setOnClickListener {
val urlPrefix = arguments?.getString("url") val url = arguments?.getString("url")
val baseName = arguments?.getString("name")?.removeSuffix(".apk") val fileName = arguments?.getString("name")
if (urlPrefix == null || baseName == null) { if (url.isNullOrBlank() || fileName.isNullOrBlank()) {
toastOnUi("下载信息不完整") toastOnUi("下载信息不完整")
return@setOnClickListener return@setOnClickListener
} }
// 获取当前 ABI 后缀 Download.start(requireContext(), url, fileName)
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)
toastOnUi("开始下载: $fileName") toastOnUi("开始下载: $fileName")
} }