diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/dialog/ManagerInstallDialog.kt b/app/apk/src/main/java/com/topjohnwu/magisk/dialog/ManagerInstallDialog.kt index 23fc3a2e3..6018aa6ba 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/dialog/ManagerInstallDialog.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/dialog/ManagerInstallDialog.kt @@ -3,7 +3,6 @@ package com.topjohnwu.magisk.dialog import com.topjohnwu.magisk.core.AppContext import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.R -import com.topjohnwu.magisk.core.di.ServiceLocator import com.topjohnwu.magisk.core.download.DownloadEngine import com.topjohnwu.magisk.core.download.Subject import com.topjohnwu.magisk.view.MagiskDialog @@ -12,9 +11,9 @@ import java.io.File class ManagerInstallDialog : MarkDownDialog() { override suspend fun getMarkdownText(): String { - val text = Info.remote.magisk.note + val text = Info.update.note // Cache the changelog - File(AppContext.cacheDir, "${Info.remote.magisk.versionCode}.md").writeText(text) + File(AppContext.cacheDir, "${Info.update.versionCode}.md").writeText(text) return text } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt index f46f09ad2..a82b6cc20 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt @@ -91,16 +91,15 @@ class HomeViewModel( override suspend fun doLoadWork() { appState = State.LOADING - Info.getRemote(svc)?.apply { + Info.fetchUpdate(svc)?.apply { appState = when { - BuildConfig.APP_VERSION_CODE < magisk.versionCode -> State.OUTDATED + BuildConfig.APP_VERSION_CODE < versionCode -> State.OUTDATED else -> State.UP_TO_DATE } val isDebug = Config.updateChannel == Config.Value.DEBUG_CHANNEL managerRemoteVersion = - ("${magisk.version} (${magisk.versionCode})" + - if (isDebug) " (D)" else "").asText() + ("$version (${versionCode})" + if (isDebug) " (D)" else "").asText() } ?: run { appState = State.INVALID managerRemoteVersion = CoreR.string.not_available.asText() diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt index b2b2103a5..ec961670a 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt @@ -74,7 +74,7 @@ class InstallViewModel(svc: NetworkService, markwon: Markwon) : BaseViewModel() val text = when { file.exists() -> file.readText() else -> { - val str = if (Const.APP_IS_CANARY) Info.remote.magisk.note + val str = if (Const.APP_IS_CANARY) Info.update.note else svc.fetchString(Const.Url.CHANGELOG_URL) file.writeText(str) str diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt index dcaaf9fd8..4b7136dc7 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt @@ -147,7 +147,7 @@ object UpdateChannel : BaseSettingsItem.Selector() { get() = Config.updateChannel set(value) { Config.updateChannel = value - Info.remote = Info.EMPTY_REMOTE + Info.resetUpdate() } override val title = CoreR.string.settings_update_channel_title.asText() @@ -169,7 +169,7 @@ object UpdateChannelUrl : BaseSettingsItem.Input() { get() = Config.customChannelUrl set(value) { Config.customChannelUrl = value - Info.remote = Info.EMPTY_REMOTE + Info.resetUpdate() notifyPropertyChanged(BR.description) } diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/Info.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/Info.kt index 0f5a5504d..4def8ce52 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/Info.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/Info.kt @@ -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 diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/JobService.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/JobService.kt index 3631bef16..7019f2735 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/JobService.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/JobService.kt @@ -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) } diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/data/RetrofitInterfaces.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/data/RetrofitInterfaces.kt index 5c25450b0..beabc58ba 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/data/RetrofitInterfaces.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/data/RetrofitInterfaces.kt @@ -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 { diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/download/Subject.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/download/Subject.kt index b9f551d96..3a1ea39a3 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/download/Subject.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/download/Subject.kt @@ -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})" diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/model/UpdateInfo.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/model/UpdateInfo.kt index d766fd0d5..9f2b503f1 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/model/UpdateInfo.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/model/UpdateInfo.kt @@ -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 = "", diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/repository/NetworkService.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/repository/NetworkService.kt index 5bb1bd2fb..9d9043d90 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/repository/NetworkService.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/repository/NetworkService.kt @@ -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 safe(factory: () -> T): T? { diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/utils/NetworkObserver.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/utils/NetworkObserver.kt index ffb6c6f51..2e0a839ea 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/utils/NetworkObserver.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/utils/NetworkObserver.kt @@ -62,7 +62,7 @@ class NetworkObserver(context: Context) { } private fun postValue(b: Boolean) { - Info.remote = Info.EMPTY_REMOTE + Info.resetUpdate() Info.isConnected.postValue(b) }