Directly use MagiskJson

This commit is contained in:
topjohnwu
2025-05-19 10:40:58 -07:00
parent adbea7e313
commit 4c89c7e2b3
11 changed files with 34 additions and 34 deletions

View File

@@ -19,12 +19,18 @@ object Info {
var stub: StubApk.Data? = null
val EMPTY_REMOTE = UpdateInfo()
var remote = EMPTY_REMOTE
suspend fun getRemote(svc: NetworkService): UpdateInfo? {
return if (remote === EMPTY_REMOTE) {
svc.fetchUpdate()?.apply { remote = this }
} else remote
private val EMPTY_UPDATE = UpdateInfo()
var update = EMPTY_UPDATE
private set
suspend fun fetchUpdate(svc: NetworkService): UpdateInfo? {
return if (update === EMPTY_UPDATE) {
svc.fetchUpdate()?.apply { update = this }
} else update
}
fun resetUpdate() {
update = EMPTY_UPDATE
}
var isRooted = false

View File

@@ -75,9 +75,8 @@ class JobService : BaseJobService() {
private fun checkUpdate(params: JobParameters): Boolean {
GlobalScope.launch(Dispatchers.IO) {
ServiceLocator.networkService.fetchUpdate()?.let {
Info.remote = it
if (Info.env.isActive && BuildConfig.APP_VERSION_CODE < it.magisk.versionCode)
Info.fetchUpdate(ServiceLocator.networkService)?.let {
if (Info.env.isActive && BuildConfig.APP_VERSION_CODE < it.versionCode)
Notifications.updateAvailable()
jobFinished(params, false)
}

View File

@@ -2,7 +2,7 @@ package com.topjohnwu.magisk.core.data
import com.topjohnwu.magisk.core.model.ModuleJson
import com.topjohnwu.magisk.core.model.Release
import com.topjohnwu.magisk.core.model.UpdateInfo
import com.topjohnwu.magisk.core.model.UpdateJson
import okhttp3.ResponseBody
import retrofit2.Response
import retrofit2.http.GET
@@ -25,7 +25,7 @@ interface RawUrl {
suspend fun fetchModuleJson(@Url url: String): ModuleJson
@GET
suspend fun fetchUpdateJson(@Url url: String): UpdateInfo
suspend fun fetchUpdateJson(@Url url: String): UpdateJson
}
interface GithubApiServices {

View File

@@ -8,7 +8,7 @@ import android.net.Uri
import android.os.Parcelable
import androidx.core.net.toUri
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.model.MagiskJson
import com.topjohnwu.magisk.core.model.UpdateInfo
import com.topjohnwu.magisk.core.model.module.OnlineModule
import com.topjohnwu.magisk.core.utils.MediaStoreUtils
import com.topjohnwu.magisk.view.Notifications
@@ -38,7 +38,7 @@ abstract class Subject : Parcelable {
@Parcelize
class App(
private val json: MagiskJson = Info.remote.magisk,
private val json: UpdateInfo = Info.update,
override val notifyId: Int = Notifications.nextId()
) : Subject() {
override val title: String get() = "Magisk-${json.version}(${json.versionCode})"

View File

@@ -11,13 +11,13 @@ import java.time.LocalDateTime
import java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME
@JsonClass(generateAdapter = true)
data class UpdateInfo(
val magisk: MagiskJson = MagiskJson(),
class UpdateJson(
val magisk: UpdateInfo = UpdateInfo(),
)
@Parcelize
@JsonClass(generateAdapter = true)
data class MagiskJson(
data class UpdateInfo(
val version: String = "",
val versionCode: Int = -1,
val link: String = "",

View File

@@ -10,7 +10,6 @@ import com.topjohnwu.magisk.core.Config.Value.STABLE_CHANNEL
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.data.GithubApiServices
import com.topjohnwu.magisk.core.data.RawUrl
import com.topjohnwu.magisk.core.model.MagiskJson
import com.topjohnwu.magisk.core.model.Release
import com.topjohnwu.magisk.core.model.UpdateInfo
import retrofit2.HttpException
@@ -31,7 +30,7 @@ class NetworkService(
CUSTOM_CHANNEL -> fetchCustomUpdate(Config.customChannelUrl)
else -> throw IllegalArgumentException()
}
if (info.magisk.versionCode < Info.env.versionCode &&
if (info.versionCode < Info.env.versionCode &&
Config.updateChannel == DEFAULT_CHANNEL) {
Config.updateChannel = BETA_CHANNEL
info = fetchBetaUpdate()
@@ -59,23 +58,21 @@ class NetworkService(
private fun Release.asPublicInfo(): UpdateInfo {
val version = tag.drop(1)
val date = createdTime.format(DateTimeFormatter.ofPattern("yyyy.M.d"))
val info = MagiskJson(
return UpdateInfo(
version = version,
versionCode = (version.toFloat() * 1000).toInt(),
link = assets[0].url,
note = "## $date $name\n\n$body"
)
return UpdateInfo(info)
}
private fun Release.asCanaryInfo(assetSelector: String): UpdateInfo {
val info = MagiskJson(
return UpdateInfo(
version = name.substring(8, 16),
versionCode = tag.drop(7).toInt(),
link = assets.find { it.name == assetSelector }!!.url,
note = "## $name\n\n$body"
)
return UpdateInfo(info)
}
private suspend fun fetchStableUpdate() = api.fetchLatestRelease().asPublicInfo()
@@ -90,7 +87,7 @@ class NetworkService(
private suspend fun fetchCustomUpdate(url: String): UpdateInfo {
val info = raw.fetchUpdateJson(url).magisk
return UpdateInfo(info.let { it.copy(note = raw.fetchString(it.note)) })
return info.let { it.copy(note = raw.fetchString(it.note)) }
}
private inline fun <T> safe(factory: () -> T): T? {

View File

@@ -62,7 +62,7 @@ class NetworkObserver(context: Context) {
}
private fun postValue(b: Boolean) {
Info.remote = Info.EMPTY_REMOTE
Info.resetUpdate()
Info.isConnected.postValue(b)
}