Skip to content

Commit

Permalink
Fix: Check storage permission
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanGIG committed Jan 9, 2024
1 parent 27ee329 commit 420170f
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions app/src/main/java/com/dumper/android/core/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -55,7 +61,19 @@ class MainActivity : ComponentActivity() {
}

override fun onShouldRedirectToSystemSettings(blockedPermissions: List<PermissionReport>) {
super.onShouldRedirectToSystemSettings(blockedPermissions)
SimpleStorageHelper.redirectToSystemSettings(this@MainActivity)
isStoragePermissionGranted = false
}

override fun onDisplayConsentDialog(request: PermissionRequest) {
super.onDisplayConsentDialog(request)
isStoragePermissionGranted = false
}

override fun onPermissionRequestInterrupted(permissions: Array<String>) {
super.onPermissionRequestInterrupted(permissions)
isStoragePermissionGranted = false
}
})
.build()
Expand All @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 420170f

Please sign in to comment.