Skip to content

Commit

Permalink
do not leak persist permission
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Aug 23, 2024
1 parent 5da2861 commit 36626cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
29 changes: 24 additions & 5 deletions app/src/main/java/org/andbootmgr/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
Expand All @@ -48,6 +49,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
Expand Down Expand Up @@ -167,9 +169,13 @@ class MainActivity : ComponentActivity() {
onFileChosen = null
return@registerForActivityResult
}
// need to persist, otherwise can't access from the background as StayAliveService
contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
if (onFileChosen != null) {
// need to persist, otherwise can't access from the background as StayAliveService
contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
val prefs = getSharedPreferences("abm", 0)
val uris = prefs.getStringSet("persistable", setOf())!!.toMutableSet()
uris.add(uri.toString())
prefs.edit().putStringSet("persistable", uris).apply()
onFileChosen!!(uri)
onFileChosen = null
} else {
Expand All @@ -187,10 +193,14 @@ class MainActivity : ComponentActivity() {
onFileCreated = null
return@registerForActivityResult
}
// need to persist, otherwise can't access from the background as StayAliveService
contentResolver.takePersistableUriPermission(uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
if (onFileCreated != null) {
// need to persist, otherwise can't access from the background as StayAliveService
contentResolver.takePersistableUriPermission(uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
val prefs = getSharedPreferences("abm", 0)
val uris = prefs.getStringSet("persistable", setOf())!!.toMutableSet()
uris.add(uri.toString())
prefs.edit().putStringSet("persistable", uris).apply()
onFileCreated!!(uri)
onFileCreated = null
} else {
Expand Down Expand Up @@ -269,6 +279,15 @@ class MainActivity : ComponentActivity() {
WizardCompat(vm, vm.currentWizardFlow!!)
} else {
val navController = rememberNavController()
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
val prefs = getSharedPreferences("abm", 0)
val uris = prefs.getStringSet("persistable", setOf())!!.toMutableSet()
uris.forEach { contentResolver.releasePersistableUriPermission(it.toUri(),
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION) }
prefs.edit().putStringSet("persistable", setOf()).apply()
}
}
AppContent(vm, navController) {
NavGraph(vm, navController, it)
}
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/org/andbootmgr/app/Wizard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.topjohnwu.superuser.io.SuFileOutputStream
import kotlinx.coroutines.launch
import org.andbootmgr.app.util.AbmOkHttp
import org.andbootmgr.app.util.TerminalCancelException
import org.andbootmgr.app.util.TerminalList
Expand All @@ -47,7 +46,6 @@ import java.io.InputStream
import java.io.OutputStream
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.util.concurrent.CancellationException

abstract class WizardFlow {
abstract fun get(vm: WizardState): List<IWizardPage>
Expand Down

0 comments on commit 36626cc

Please sign in to comment.