Better environment status detection

This commit is contained in:
topjohnwu 2019-11-16 17:38:10 -05:00
parent 7f6a6016d6
commit da159e4655
7 changed files with 47 additions and 47 deletions

View File

@ -36,20 +36,21 @@ object Info {
val str = ShellUtils.fastCmd("magisk -v").split(":".toRegex())[0] val str = ShellUtils.fastCmd("magisk -v").split(":".toRegex())[0]
val code = ShellUtils.fastCmd("magisk -V").toInt() val code = ShellUtils.fastCmd("magisk -V").toInt()
val hide = Shell.su("magiskhide --status").exec().isSuccess val hide = Shell.su("magiskhide --status").exec().isSuccess
Env(code, str, hide) Env(str, code, hide)
}.getOrElse { Env() } }.getOrElse { Env() }
class Env( class Env(
code: Int = -1,
val magiskVersionString: String = "", val magiskVersionString: String = "",
code: Int = -1,
hide: Boolean = false hide: Boolean = false
) { ) {
val magiskHide get() = Config.magiskHide val magiskHide get() = Config.magiskHide
val magiskVersionCode = when (code) { val magiskVersionCode = when (code) {
in Int.MIN_VALUE..Const.Version.MIN_VERCODE -> -1 in Int.MIN_VALUE..Const.Version.MIN_VERCODE -> -1
else -> code else -> if(Shell.rootAccess()) code else -1
} }
val unsupported = code > 0 && code < Const.Version.MIN_VERCODE val isUnsupported = code > 0 && code < Const.Version.MIN_VERCODE
val isActive = magiskVersionCode >= 0
init { init {
Config.magiskHide = hide Config.magiskHide = hide

View File

@ -31,7 +31,6 @@ import com.topjohnwu.magisk.ui.module.ReposFragment
import com.topjohnwu.magisk.ui.settings.SettingsFragment import com.topjohnwu.magisk.ui.settings.SettingsFragment
import com.topjohnwu.magisk.ui.superuser.SuperuserFragment import com.topjohnwu.magisk.ui.superuser.SuperuserFragment
import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.superuser.Shell
import org.koin.androidx.viewmodel.ext.android.viewModel import org.koin.androidx.viewmodel.ext.android.viewModel
import timber.log.Timber import timber.log.Timber
import kotlin.reflect.KClass import kotlin.reflect.KClass
@ -68,7 +67,7 @@ open class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Na
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
if (Info.env.unsupported && !viewModel.shownUnsupportedDialog) { if (Info.env.isUnsupported && !viewModel.shownUnsupportedDialog) {
viewModel.shownUnsupportedDialog = true viewModel.shownUnsupportedDialog = true
AlertDialog.Builder(this) AlertDialog.Builder(this)
.setTitle(R.string.unsupport_magisk_title) .setTitle(R.string.unsupport_magisk_title)
@ -164,16 +163,11 @@ open class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Na
private fun checkHideSection() { private fun checkHideSection() {
val menu = binding.navView.menu val menu = binding.navView.menu
menu.findItem(R.id.magiskHideFragment).isVisible = menu.findItem(R.id.magiskHideFragment).isVisible = Info.env.isActive && Info.env.magiskHide
Shell.rootAccess() && Info.env.magiskHide menu.findItem(R.id.modulesFragment).isVisible = Info.env.isActive
menu.findItem(R.id.modulesFragment).isVisible = menu.findItem(R.id.reposFragment).isVisible = Info.isConnected.value && Info.env.isActive
Shell.rootAccess() && Info.env.magiskVersionCode >= 0 menu.findItem(R.id.logFragment).isVisible = Info.env.isActive
menu.findItem(R.id.reposFragment).isVisible = menu.findItem(R.id.superuserFragment).isVisible = Utils.showSuperUser()
(viewModel.isConnected.value && Shell.rootAccess() && Info.env.magiskVersionCode >= 0)
menu.findItem(R.id.logFragment).isVisible =
Shell.rootAccess()
menu.findItem(R.id.superuserFragment).isVisible =
Utils.showSuperUser()
} }
private fun FragNavTransactionOptions.Builder.customAnimations(options: MagiskAnimBuilder) = private fun FragNavTransactionOptions.Builder.customAnimations(options: MagiskAnimBuilder) =

View File

@ -92,7 +92,7 @@ class HomeViewModel(
} }
} }
val hasRoot = KObservableField(false) val isActive = KObservableField(false)
private var shownDialog = false private var shownDialog = false
@ -175,7 +175,7 @@ class HomeViewModel(
if (invalidate) if (invalidate)
Info.envRef.invalidate() Info.envRef.invalidate()
hasRoot.value = Shell.rootAccess() isActive.value = Info.env.isActive
val fetchUpdate = if (isConnected.value) val fetchUpdate = if (isConnected.value)
magiskRepo.fetchUpdate().ignoreElement() magiskRepo.fetchUpdate().ignoreElement()

View File

@ -61,7 +61,7 @@ class SettingsFragment : BasePreferenceFragment() {
nsConfig = findPreference(Config.Key.SU_MNT_NS)!! nsConfig = findPreference(Config.Key.SU_MNT_NS)!!
val reauth = findPreference<SwitchPreferenceCompat>(Config.Key.SU_REAUTH)!! val reauth = findPreference<SwitchPreferenceCompat>(Config.Key.SU_REAUTH)!!
val biometric = findPreference<SwitchPreferenceCompat>(Config.Key.SU_BIOMETRIC)!! val biometric = findPreference<SwitchPreferenceCompat>(Config.Key.SU_BIOMETRIC)!!
val generalCatagory = findPreference<PreferenceCategory>("general")!! val generalCategory = findPreference<PreferenceCategory>("general")!!
val magiskCategory = findPreference<PreferenceCategory>("magisk")!! val magiskCategory = findPreference<PreferenceCategory>("magisk")!!
val suCategory = findPreference<PreferenceCategory>("superuser")!! val suCategory = findPreference<PreferenceCategory>("superuser")!!
val hideManager = findPreference<Preference>("hide")!! val hideManager = findPreference<Preference>("hide")!!
@ -94,9 +94,9 @@ class SettingsFragment : BasePreferenceFragment() {
biometric.setSummary(R.string.no_biometric) biometric.setSummary(R.string.no_biometric)
} }
if (Const.USER_ID == 0 && Info.isConnected.value && Shell.rootAccess()) { if (Const.USER_ID == 0 && Info.isConnected.value && Info.env.isActive) {
if (activity.packageName == BuildConfig.APPLICATION_ID) { if (activity.packageName == BuildConfig.APPLICATION_ID) {
generalCatagory.removePreference(restoreManager) generalCategory.removePreference(restoreManager)
hideManager.setOnPreferenceClickListener { hideManager.setOnPreferenceClickListener {
showManagerNameDialog { showManagerNameDialog {
PatchAPK.hideManager(requireContext(), it) PatchAPK.hideManager(requireContext(), it)
@ -104,7 +104,7 @@ class SettingsFragment : BasePreferenceFragment() {
true true
} }
} else { } else {
generalCatagory.removePreference(hideManager) generalCategory.removePreference(hideManager)
restoreManager.setOnPreferenceClickListener { restoreManager.setOnPreferenceClickListener {
DownloadService(requireContext()) { DownloadService(requireContext()) {
subject = DownloadSubject.Manager(Configuration.APK.Restore) subject = DownloadSubject.Manager(Configuration.APK.Restore)
@ -114,25 +114,32 @@ class SettingsFragment : BasePreferenceFragment() {
} }
} else { } else {
// Remove if not primary user, no connection, or no root // Remove if not primary user, no connection, or no root
generalCatagory.removePreference(restoreManager) generalCategory.removePreference(restoreManager)
generalCatagory.removePreference(hideManager) generalCategory.removePreference(hideManager)
} }
if (!Utils.showSuperUser()) { if (!Utils.showSuperUser()) {
preferenceScreen.removePreference(suCategory) preferenceScreen.removePreference(suCategory)
} }
if (!Shell.rootAccess()) { if (!Info.env.isActive) {
preferenceScreen.removePreference(magiskCategory) preferenceScreen.removePreference(magiskCategory)
generalCatagory.removePreference(hideManager) generalCategory.removePreference(hideManager)
} }
findPreference<Preference>("clear")?.setOnPreferenceClickListener { findPreference<Preference>("clear")?.also {
Completable.fromAction { repoDB.clear() }.subscribeK { if (Info.env.isActive) {
Utils.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT) it.setOnPreferenceClickListener {
Completable.fromAction { repoDB.clear() }.subscribeK {
Utils.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT)
}
true
}
} else {
generalCategory.removePreference(it)
} }
true
} }
findPreference<Preference>("hosts")?.setOnPreferenceClickListener { findPreference<Preference>("hosts")?.setOnPreferenceClickListener {
Shell.su("add_hosts_module").submit { Shell.su("add_hosts_module").submit {
Utils.toast(R.string.settings_hosts_toast, Toast.LENGTH_SHORT) Utils.toast(R.string.settings_hosts_toast, Toast.LENGTH_SHORT)
@ -142,20 +149,21 @@ class SettingsFragment : BasePreferenceFragment() {
findPreference<Preference>(Config.Key.DOWNLOAD_PATH)?.apply { findPreference<Preference>(Config.Key.DOWNLOAD_PATH)?.apply {
summary = Config.downloadPath summary = Config.downloadPath
}?.setOnPreferenceClickListener { preference -> setOnPreferenceClickListener { pref ->
activity.withExternalRW { activity.withExternalRW {
onSuccess { onSuccess {
showDownloadDialog { showDownloadDialog {
Config.downloadPath = it Config.downloadPath = it
preference.summary = it pref.summary = it
}
} }
} }
true
} }
true
} }
updateChannel.setOnPreferenceChangeListener { _, value -> updateChannel.setOnPreferenceChangeListener { _, value ->
val channel = Integer.parseInt(value as String) val channel = value.toString().toInt()
val previous = Config.updateChannel val previous = Config.updateChannel
if (channel == Config.Value.CUSTOM_CHANNEL) { if (channel == Config.Value.CUSTOM_CHANNEL) {

View File

@ -11,7 +11,6 @@ import com.topjohnwu.magisk.*
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.extensions.get import com.topjohnwu.magisk.extensions.get
import com.topjohnwu.magisk.model.update.UpdateCheckService import com.topjohnwu.magisk.model.update.UpdateCheckService
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.internal.UiThreadHandler import com.topjohnwu.superuser.internal.UiThreadHandler
import java.io.File import java.io.File
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -34,7 +33,7 @@ object Utils {
} }
fun showSuperUser(): Boolean { fun showSuperUser(): Boolean {
return Shell.rootAccess() && (Const.USER_ID == 0 return Info.env.isActive && (Const.USER_ID == 0
|| Config.suMultiuserMode != Config.Value.MULTIUSER_MODE_OWNER_MANAGED) || Config.suMultiuserMode != Config.Value.MULTIUSER_MODE_OWNER_MANAGED)
} }

View File

@ -14,7 +14,6 @@ import com.topjohnwu.magisk.*
import com.topjohnwu.magisk.extensions.getBitmap import com.topjohnwu.magisk.extensions.getBitmap
import com.topjohnwu.magisk.ui.SplashActivity import com.topjohnwu.magisk.ui.SplashActivity
import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.superuser.Shell
object Shortcuts { object Shortcuts {
@ -28,7 +27,6 @@ object Shortcuts {
@RequiresApi(api = 25) @RequiresApi(api = 25)
private fun getShortCuts(context: Context): List<ShortcutInfo> { private fun getShortCuts(context: Context): List<ShortcutInfo> {
val shortCuts = mutableListOf<ShortcutInfo>() val shortCuts = mutableListOf<ShortcutInfo>()
val root = Shell.rootAccess()
val intent = context.intent<SplashActivity>() val intent = context.intent<SplashActivity>()
fun getIcon(id: Int): Icon { fun getIcon(id: Int): Icon {
@ -53,7 +51,7 @@ object Shortcuts {
.build() .build()
) )
} }
if (root && Info.env.magiskHide) { if (Info.env.magiskHide) {
shortCuts.add( shortCuts.add(
ShortcutInfo.Builder(context, "magiskhide") ShortcutInfo.Builder(context, "magiskhide")
.setShortLabel(context.getString(R.string.magiskhide)) .setShortLabel(context.getString(R.string.magiskhide))
@ -68,7 +66,7 @@ object Shortcuts {
.build() .build()
) )
} }
if (!Config.coreOnly && root && Info.env.magiskVersionCode >= 0) { if (!Config.coreOnly && Info.env.isActive) {
shortCuts.add( shortCuts.add(
ShortcutInfo.Builder(context, "modules") ShortcutInfo.Builder(context, "modules")
.setShortLabel(context.getString(R.string.modules)) .setShortLabel(context.getString(R.string.modules))

View File

@ -201,7 +201,7 @@
<View <View
style="@style/Widget.Divider.Horizontal" style="@style/Widget.Divider.Horizontal"
gone="@{!viewModel.hasRoot || !viewModel.isConnected}" gone="@{!viewModel.isActive || !viewModel.isConnected}"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_margin="@dimen/margin_generic" /> android:layout_margin="@dimen/margin_generic" />
@ -482,13 +482,13 @@
<View <View
style="@style/Widget.Divider.Horizontal" style="@style/Widget.Divider.Horizontal"
gone="@{!viewModel.hasRoot}" gone="@{!viewModel.isActive}"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_margin="@dimen/margin_generic" /> android:layout_margin="@dimen/margin_generic" />
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
style="@style/Widget.Button.Text" style="@style/Widget.Button.Text"
gone="@{!viewModel.hasRoot}" gone="@{!viewModel.isActive}"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:onClick="@{() -> viewModel.uninstallPressed()}" android:onClick="@{() -> viewModel.uninstallPressed()}"