From 36626cc2d586ea8d657c3966f5c6af3d548627ec Mon Sep 17 00:00:00 2001 From: nift4 Date: Fri, 23 Aug 2024 18:29:18 +0200 Subject: [PATCH] do not leak persist permission --- .../java/org/andbootmgr/app/MainActivity.kt | 29 +++++++++++++++---- .../main/java/org/andbootmgr/app/Wizard.kt | 2 -- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/andbootmgr/app/MainActivity.kt b/app/src/main/java/org/andbootmgr/app/MainActivity.kt index 528bb91f..0ac8805e 100644 --- a/app/src/main/java/org/andbootmgr/app/MainActivity.kt +++ b/app/src/main/java/org/andbootmgr/app/MainActivity.kt @@ -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 @@ -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 @@ -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 { @@ -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 { @@ -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) } diff --git a/app/src/main/java/org/andbootmgr/app/Wizard.kt b/app/src/main/java/org/andbootmgr/app/Wizard.kt index 044d1b45..d35b2258 100644 --- a/app/src/main/java/org/andbootmgr/app/Wizard.kt +++ b/app/src/main/java/org/andbootmgr/app/Wizard.kt @@ -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 @@ -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