diff --git a/app/src/main/java/com/topjohnwu/magisk/Info.kt b/app/src/main/java/com/topjohnwu/magisk/Info.kt index f226ae5cf..1e5e63649 100644 --- a/app/src/main/java/com/topjohnwu/magisk/Info.kt +++ b/app/src/main/java/com/topjohnwu/magisk/Info.kt @@ -1,26 +1,37 @@ package com.topjohnwu.magisk import com.topjohnwu.magisk.model.entity.UpdateInfo +import com.topjohnwu.magisk.utils.CachedValue import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.ShellUtils object Info { - var magiskVersionCode = -1 + val envRef = CachedValue { loadState() } - var magiskVersionString = "" - - var remote = UpdateInfo() + val env by envRef // Local + var remote = UpdateInfo() // Remote var keepVerity = false var keepEnc = false var recovery = false - fun loadMagiskInfo() { - runCatching { - magiskVersionString = ShellUtils.fastCmd("magisk -v").split(":".toRegex())[0] - magiskVersionCode = ShellUtils.fastCmd("magisk -V").toInt() - Config.magiskHide = Shell.su("magiskhide --status").exec().isSuccess + private fun loadState() = runCatching { + val str = ShellUtils.fastCmd("magisk -v").split(":".toRegex())[0] + val code = ShellUtils.fastCmd("magisk -V").toInt() + val hide = Shell.su("magiskhide --status").exec().isSuccess + Env(code, str, hide) + }.getOrElse { Env() } + + class Env( + val magiskVersionCode: Int = -1, + val magiskVersionString: String = "", + hide: Boolean = false + ) { + val magiskHide get() = Config.magiskHide + + init { + Config.magiskHide = hide } } } diff --git a/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt b/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt index 0d73fc6c1..b9a4ae98b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt @@ -29,7 +29,7 @@ class MagiskRepository( else -> throw IllegalArgumentException() }.flatMap { // If remote version is lower than current installed, try switching to beta - if (it.magisk.versionCode < Info.magiskVersionCode + if (it.magisk.versionCode < Info.env.magiskVersionCode && Config.updateChannel == Config.Value.DEFAULT_CHANNEL) { Config.updateChannel = Config.Value.BETA_CHANNEL apiRaw.fetchBetaUpdate() @@ -74,4 +74,4 @@ class MagiskRepository( ) } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/topjohnwu/magisk/model/update/UpdateCheckService.kt b/app/src/main/java/com/topjohnwu/magisk/model/update/UpdateCheckService.kt index 17841cd0c..190e05802 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/update/UpdateCheckService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/update/UpdateCheckService.kt @@ -20,7 +20,7 @@ class UpdateCheckService : DelegateWorker() { magiskRepo.fetchUpdate().blockingGet() if (BuildConfig.VERSION_CODE < Info.remote.app.versionCode) Notifications.managerUpdate(applicationContext) - else if (Info.magiskVersionCode < Info.remote.magisk.versionCode) + else if (Info.env.magiskVersionCode < Info.remote.magisk.versionCode) Notifications.magiskUpdate(applicationContext) ListenableWorker.Result.success() }.getOrElse { diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt b/app/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt index 24ec6658f..05fcaa90d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt @@ -7,13 +7,15 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentTransaction import com.ncapdevi.fragnav.FragNavController import com.ncapdevi.fragnav.FragNavTransactionOptions -import com.topjohnwu.magisk.* import com.topjohnwu.magisk.Const.Key.OPEN_SECTION +import com.topjohnwu.magisk.Info +import com.topjohnwu.magisk.R import com.topjohnwu.magisk.base.BaseActivity import com.topjohnwu.magisk.base.BaseFragment import com.topjohnwu.magisk.databinding.ActivityMainBinding import com.topjohnwu.magisk.extensions.addOnPropertyChangedCallback import com.topjohnwu.magisk.extensions.snackbar +import com.topjohnwu.magisk.intent import com.topjohnwu.magisk.model.events.* import com.topjohnwu.magisk.model.navigation.MagiskAnimBuilder import com.topjohnwu.magisk.model.navigation.MagiskNavigationEvent @@ -152,11 +154,11 @@ open class MainActivity : BaseActivity(), Na private fun checkHideSection() { val menu = binding.navView.menu menu.findItem(R.id.magiskHideFragment).isVisible = - Shell.rootAccess() && Config.magiskHide + Shell.rootAccess() && Info.env.magiskHide menu.findItem(R.id.modulesFragment).isVisible = - Shell.rootAccess() && Info.magiskVersionCode >= 0 + Shell.rootAccess() && Info.env.magiskVersionCode >= 0 menu.findItem(R.id.reposFragment).isVisible = - (viewModel.isConnected.value && Shell.rootAccess() && Info.magiskVersionCode >= 0) + (viewModel.isConnected.value && Shell.rootAccess() && Info.env.magiskVersionCode >= 0) menu.findItem(R.id.logFragment).isVisible = Shell.rootAccess() menu.findItem(R.id.superuserFragment).isVisible = diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt b/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt index 75d814ce2..f31ffbc4c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt @@ -7,7 +7,6 @@ import android.text.TextUtils import androidx.appcompat.app.AlertDialog import com.topjohnwu.magisk.* import com.topjohnwu.magisk.utils.Utils -import com.topjohnwu.magisk.wrap import com.topjohnwu.magisk.view.Notifications import com.topjohnwu.magisk.view.Shortcuts import com.topjohnwu.superuser.Shell @@ -22,7 +21,7 @@ open class SplashActivity : Activity() { super.onCreate(savedInstanceState) Shell.getShell { - if (Info.magiskVersionCode > 0 && Info.magiskVersionCode < Const.MagiskVersion.MIN_SUPPORT) { + if (Info.env.magiskVersionCode > 0 && Info.env.magiskVersionCode < Const.MagiskVersion.MIN_SUPPORT) { AlertDialog.Builder(this) .setTitle(R.string.unsupport_magisk_title) .setMessage(R.string.unsupport_magisk_message) diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt index fd5da72b0..821a79d1f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt @@ -107,10 +107,10 @@ class HomeViewModel( Info.recovery = it ?: return@addOnPropertyChangedCallback } isConnected.addOnPropertyChangedCallback { - if (it == true) refresh() + if (it == true) refresh(false) } - refresh() + refresh(false) } fun paypalPressed() = OpenLinkEvent(Const.Url.PAYPAL_URL).publish() @@ -170,7 +170,11 @@ class HomeViewModel( } } - fun refresh() { + @JvmOverloads + fun refresh(invalidate: Boolean = true) { + if (invalidate) + Info.envRef.invalidate() + hasRoot.value = Shell.rootAccess() val fetchUpdate = if (isConnected.value) @@ -179,7 +183,8 @@ class HomeViewModel( Completable.complete() Completable.fromAction { - Info.loadMagiskInfo() + // Ensure value is ready + Info.env }.andThen(fetchUpdate) .applyViewModel(this) .doOnSubscribeUi { @@ -197,7 +202,7 @@ class HomeViewModel( private fun refreshVersions() { magiskCurrentVersion.value = if (magiskState.value != MagiskState.NOT_INSTALLED) { - version.format(Info.magiskVersionString, Info.magiskVersionCode) + version.format(Info.env.magiskVersionString, Info.env.magiskVersionCode) } else { "" } @@ -207,7 +212,7 @@ class HomeViewModel( } private fun updateSelf() { - magiskState.value = when (Info.magiskVersionCode) { + magiskState.value = when (Info.env.magiskVersionCode) { in Int.MIN_VALUE until 0 -> MagiskState.NOT_INSTALLED !in Info.remote.magisk.versionCode..Int.MAX_VALUE -> MagiskState.OBSOLETE else -> MagiskState.UP_TO_DATE diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/CachedValue.kt b/app/src/main/java/com/topjohnwu/magisk/utils/CachedValue.kt new file mode 100644 index 000000000..b4b6f6a80 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/utils/CachedValue.kt @@ -0,0 +1,24 @@ +package com.topjohnwu.magisk.utils + +class CachedValue(private val factory: () -> T) : Lazy { + + private var _val : T? = null + + override val value: T + get() { + val local = _val + return local ?: synchronized(this) { + val newInstance = factory() + _val = newInstance + newInstance + } + } + + override fun isInitialized() = _val != null + + fun invalidate() { + synchronized(this) { + _val = null + } + } +} diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/RootInit.kt b/app/src/main/java/com/topjohnwu/magisk/utils/RootInit.kt index 481ce2d02..e503b84ea 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/RootInit.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/RootInit.kt @@ -20,9 +20,8 @@ class RootInit : Shell.Initializer() { val job = shell.newJob() if (shell.isRoot) { job.add(context.rawResource(R.raw.util_functions)) - .add(context.rawResource(R.raw.utils)) + .add(context.rawResource(R.raw.utils)) Const.MAGISK_DISABLE_FILE = SuFile("/cache/.disable_magisk") - Info.loadMagiskInfo() } else { job.add(context.rawResource(R.raw.nonroot_utils)) } diff --git a/app/src/main/java/com/topjohnwu/magisk/view/Shortcuts.kt b/app/src/main/java/com/topjohnwu/magisk/view/Shortcuts.kt index 7a2d210b6..3a541c155 100644 --- a/app/src/main/java/com/topjohnwu/magisk/view/Shortcuts.kt +++ b/app/src/main/java/com/topjohnwu/magisk/view/Shortcuts.kt @@ -38,7 +38,7 @@ object Shortcuts { .setRank(0) .build()) } - if (root && Config.magiskHide) { + if (root && Info.env.magiskHide) { shortCuts.add(ShortcutInfo.Builder(context, "magiskhide") .setShortLabel(context.getString(R.string.magiskhide)) .setIntent(Intent(intent) @@ -49,7 +49,7 @@ object Shortcuts { .setRank(1) .build()) } - if (!Config.coreOnly && root && Info.magiskVersionCode >= 0) { + if (!Config.coreOnly && root && Info.env.magiskVersionCode >= 0) { shortCuts.add(ShortcutInfo.Builder(context, "modules") .setShortLabel(context.getString(R.string.modules)) .setIntent(Intent(intent)