From 420170fc87677e51636d319be6f6daaa8697067b Mon Sep 17 00:00:00 2001 From: BryanGIG Date: Tue, 9 Jan 2024 19:51:35 +0700 Subject: [PATCH] Fix: Check storage permission --- .../com/dumper/android/core/MainActivity.kt | 55 +++++++++++++++++-- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/dumper/android/core/MainActivity.kt b/app/src/main/java/com/dumper/android/core/MainActivity.kt index d0355ed..ed6e620 100644 --- a/app/src/main/java/com/dumper/android/core/MainActivity.kt +++ b/app/src/main/java/com/dumper/android/core/MainActivity.kt @@ -33,7 +33,10 @@ import com.dumper.android.ui.MainScreen import com.dumper.android.ui.console.ConsoleViewModel import com.dumper.android.ui.memory.MemoryViewModel import com.dumper.android.ui.theme.PADumperTheme +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.ipc.RootService +import kotlin.system.exitProcess class MainActivity : ComponentActivity() { var remoteMessenger: Messenger? = null @@ -43,6 +46,9 @@ class MainActivity : ComponentActivity() { val memory: MemoryViewModel by viewModels() val console: ConsoleViewModel by viewModels() + // FIXME: Use proper way to check storage permission + private var isStoragePermissionGranted = true + private val permissionRequest = ActivityPermissionRequest.Builder(this) .withPermissions( Manifest.permission.WRITE_EXTERNAL_STORAGE, @@ -55,7 +61,19 @@ class MainActivity : ComponentActivity() { } override fun onShouldRedirectToSystemSettings(blockedPermissions: List) { + super.onShouldRedirectToSystemSettings(blockedPermissions) SimpleStorageHelper.redirectToSystemSettings(this@MainActivity) + isStoragePermissionGranted = false + } + + override fun onDisplayConsentDialog(request: PermissionRequest) { + super.onDisplayConsentDialog(request) + isStoragePermissionGranted = false + } + + override fun onPermissionRequestInterrupted(permissions: Array) { + super.onPermissionRequestInterrupted(permissions) + isStoragePermissionGranted = false } }) .build() @@ -74,14 +92,30 @@ class MainActivity : ComponentActivity() { Fixer.extractLibs(this) - if (intent.getBooleanExtra("IS_ROOT", false)) { + if (Shell.getCachedShell()?.isRoot == true) { setupRootService() } else { - setupSimpleStorage(savedInstanceState) - setupStoragePermission() + setupNonRoot(savedInstanceState) } } + override fun onResume() { + super.onResume() + if (Shell.getCachedShell()?.isRoot == true && !isStoragePermissionGranted) { + setupNonRoot(null) + } + } + + override fun onSaveInstanceState(outState: Bundle) { + storageHelper.onSaveInstanceState(outState) + super.onSaveInstanceState(outState) + } + + override fun onRestoreInstanceState(savedInstanceState: Bundle) { + super.onRestoreInstanceState(savedInstanceState) + storageHelper.onRestoreInstanceState(savedInstanceState) + } + private fun checkShell() { if (Shell.getCachedShell() == null) { Intent(this, SplashActivity::class.java).also { @@ -143,8 +177,19 @@ class MainActivity : ComponentActivity() { } } - private fun setupSimpleStorage(savedInstanceState: Bundle?) { - savedInstanceState?.let { storageHelper.onRestoreInstanceState(it) } + private fun setupNonRoot(savedInstanceState: Bundle?) { + MaterialAlertDialogBuilder(this) + .setTitle("Warning") + .setMessage("This app required storage permission to work properly") + .setPositiveButton("OK") { _, _ -> + savedInstanceState?.let { storageHelper.onRestoreInstanceState(it) } + setupStoragePermission() + } + .setNegativeButton("Cancel") { _, _ -> + exitProcess(0) + } + .setCancelable(false) + .show() } private fun setupStoragePermission() {