From bba2ac8817adfc71f350a23c935d15199d915415 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Fri, 30 Oct 2020 16:43:11 +0800 Subject: [PATCH] Add unsupport env check --- app/shared/src/main/AndroidManifest.xml | 4 +- app/src/main/AndroidManifest.xml | 2 + .../java/com/topjohnwu/magisk/core/Const.kt | 1 + .../topjohnwu/magisk/core/SplashActivity.kt | 23 +++++++- .../com/topjohnwu/magisk/ui/MainActivity.kt | 57 ++++++++++++++++--- app/src/main/res/values/strings.xml | 8 +++ 6 files changed, 83 insertions(+), 12 deletions(-) diff --git a/app/shared/src/main/AndroidManifest.xml b/app/shared/src/main/AndroidManifest.xml index e1bf71bf7..5423da11b 100644 --- a/app/shared/src/main/AndroidManifest.xml +++ b/app/shared/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + package="com.topjohnwu.shared" + android:installLocation="internalOnly"> @@ -16,7 +17,6 @@ + + /dev/null 2>&1").exec() + val uninstall = Shell.su("(pm uninstall $pkg)& >/dev/null 2>&1").exec() + if (!uninstall.isSuccess) uninstallApp(pkg) } } @@ -58,6 +64,21 @@ open class SplashActivity : Activity() { finish() } + @Suppress("DEPRECATION") + private fun uninstallApp(pkg: String) { + val uri = Uri.Builder().scheme("package").opaquePart(pkg).build() + val intent = Intent(Intent.ACTION_UNINSTALL_PACKAGE, uri) + intent.putExtra(Intent.EXTRA_RETURN_RESULT, true) + startActivityForResult(intent, Const.ID.UNINSTALL_APP) + latch.await() + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + if (requestCode == Const.ID.UNINSTALL_APP) { + if (resultCode != RESULT_CANCELED) latch.countDown() + } else super.onActivityResult(requestCode, resultCode, data) + } + companion object { var DONE = false } 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 1442d45d7..e7a616611 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt @@ -1,6 +1,7 @@ package com.topjohnwu.magisk.ui import android.content.Intent +import android.content.pm.ApplicationInfo import android.os.Build import android.os.Bundle import android.view.MenuItem @@ -17,6 +18,7 @@ import com.topjohnwu.magisk.arch.BaseUIActivity import com.topjohnwu.magisk.arch.BaseViewModel import com.topjohnwu.magisk.arch.ReselectionTarget import com.topjohnwu.magisk.core.* +import com.topjohnwu.magisk.core.tasks.HideAPK import com.topjohnwu.magisk.databinding.ActivityMainMd2Binding import com.topjohnwu.magisk.ktx.startAnimations import com.topjohnwu.magisk.ui.home.HomeFragmentDirections @@ -24,7 +26,9 @@ import com.topjohnwu.magisk.utils.HideableBehavior import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.view.MagiskDialog import com.topjohnwu.magisk.view.Shortcuts +import com.topjohnwu.superuser.Shell import org.koin.androidx.viewmodel.ext.android.viewModel +import java.io.File class MainViewModel : BaseViewModel() @@ -81,7 +85,7 @@ open class MainActivity : BaseUIActivity( (currentFragment as? ReselectionTarget)?.onReselected() } - val section = if (intent.action == ACTION_APPLICATION_PREFERENCES) Const.Nav.SETTINGS + val section = if (intent.action == Intent.ACTION_APPLICATION_PREFERENCES) Const.Nav.SETTINGS else intent.getStringExtra(Const.Key.OPEN_SECTION) getScreen(section)?.navigate() @@ -188,7 +192,49 @@ open class MainActivity : BaseUIActivity( .applyTitle(R.string.unsupport_magisk_title) .applyMessage(R.string.unsupport_magisk_msg, Const.Version.MIN_VERSION) .applyButton(MagiskDialog.ButtonType.POSITIVE) { titleRes = android.R.string.ok } - .cancellable(true) + .cancellable(false) + .reveal() + } + + if (Info.env.isActive && System.getenv("PATH") + ?.split(':') + ?.filterNot { File("$it/magisk").exists() } + ?.any { File("$it/su").exists() } == true) { + MagiskDialog(this) + .applyTitle(R.string.unsupport_other_su_title) + .applyMessage(R.string.unsupport_other_su_msg) + .applyButton(MagiskDialog.ButtonType.POSITIVE) { titleRes = android.R.string.ok } + .cancellable(false) + .reveal() + } + + if (applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0) { + MagiskDialog(this) + .applyTitle(R.string.unsupport_system_app_title) + .applyMessage(R.string.unsupport_system_app_msg) + .applyButton(MagiskDialog.ButtonType.POSITIVE) { titleRes = android.R.string.ok } + .cancellable(false) + .reveal() + } + + if (applicationInfo.flags and ApplicationInfo.FLAG_EXTERNAL_STORAGE != 0) { + MagiskDialog(this) + .applyTitle(R.string.unsupport_external_storage_title) + .applyMessage(R.string.unsupport_external_storage_msg) + .applyButton(MagiskDialog.ButtonType.POSITIVE) { titleRes = android.R.string.ok } + .cancellable(false) + .reveal() + } + + if (isRunningAsStub && !Shell.rootAccess()) { + MagiskDialog(this) + .applyTitle(R.string.unsupport_nonroot_stub_title) + .applyMessage(R.string.unsupport_nonroot_stub_msg) + .applyButton(MagiskDialog.ButtonType.POSITIVE) { + titleRes = R.string.install + onClick { HideAPK.restore(this@MainActivity) } + } + .cancellable(false) .reveal() } } @@ -212,11 +258,4 @@ open class MainActivity : BaseUIActivity( .reveal() } } - - companion object { - private val ACTION_APPLICATION_PREFERENCES get() = - if (Build.VERSION.SDK_INT >= 24) Intent.ACTION_APPLICATION_PREFERENCES - else "???" - } - } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 034f9b47b..9482cefe9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -230,6 +230,14 @@ Authenticate Unsupported Magisk Version This version of the app does not support Magisk version lower than %1$s.\n\nThe app will behave as if no Magisk is installed, please upgrade Magisk as soon as possible. + Other su found on this device + The existence of other su may cause Magisk to run abnormally and MagiskHide to be invalid. Please remove other su. + Unsupported run as system app + Magisk does not support run as system app, which breaks its hidden feature. Please revert to user app. + Unsupported installed on external storage + Magisk is installed to a non-standard location. Please move app to internal storage. + @string/settings_restore_app_title + Root is lost, the application can not continue to work in the hidden state, please restore it back to the original APK. Grant storage permission to enable this functionality Add shortcut to home screen After hiding this app, its name and icon might become difficult to recognize. Do you want to add a pretty shortcut to the home screen?