Skip to content

Commit 36626cc

Browse files
committed
do not leak persist permission
1 parent 5da2861 commit 36626cc

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

app/src/main/java/org/andbootmgr/app/MainActivity.kt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import androidx.compose.material3.TopAppBarDefaults
3838
import androidx.compose.material3.rememberDrawerState
3939
import androidx.compose.runtime.Composable
4040
import androidx.compose.runtime.DisposableEffect
41+
import androidx.compose.runtime.LaunchedEffect
4142
import androidx.compose.runtime.getValue
4243
import androidx.compose.runtime.mutableStateMapOf
4344
import androidx.compose.runtime.mutableStateOf
@@ -48,6 +49,7 @@ import androidx.compose.ui.Modifier
4849
import androidx.compose.ui.res.stringResource
4950
import androidx.compose.ui.tooling.preview.Preview
5051
import androidx.compose.ui.unit.dp
52+
import androidx.core.net.toUri
5153
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
5254
import androidx.navigation.NavHostController
5355
import androidx.navigation.compose.NavHost
@@ -167,9 +169,13 @@ class MainActivity : ComponentActivity() {
167169
onFileChosen = null
168170
return@registerForActivityResult
169171
}
170-
// need to persist, otherwise can't access from the background as StayAliveService
171-
contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
172172
if (onFileChosen != null) {
173+
// need to persist, otherwise can't access from the background as StayAliveService
174+
contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
175+
val prefs = getSharedPreferences("abm", 0)
176+
val uris = prefs.getStringSet("persistable", setOf())!!.toMutableSet()
177+
uris.add(uri.toString())
178+
prefs.edit().putStringSet("persistable", uris).apply()
173179
onFileChosen!!(uri)
174180
onFileChosen = null
175181
} else {
@@ -187,10 +193,14 @@ class MainActivity : ComponentActivity() {
187193
onFileCreated = null
188194
return@registerForActivityResult
189195
}
190-
// need to persist, otherwise can't access from the background as StayAliveService
191-
contentResolver.takePersistableUriPermission(uri,
192-
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
193196
if (onFileCreated != null) {
197+
// need to persist, otherwise can't access from the background as StayAliveService
198+
contentResolver.takePersistableUriPermission(uri,
199+
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
200+
val prefs = getSharedPreferences("abm", 0)
201+
val uris = prefs.getStringSet("persistable", setOf())!!.toMutableSet()
202+
uris.add(uri.toString())
203+
prefs.edit().putStringSet("persistable", uris).apply()
194204
onFileCreated!!(uri)
195205
onFileCreated = null
196206
} else {
@@ -269,6 +279,15 @@ class MainActivity : ComponentActivity() {
269279
WizardCompat(vm, vm.currentWizardFlow!!)
270280
} else {
271281
val navController = rememberNavController()
282+
LaunchedEffect(Unit) {
283+
withContext(Dispatchers.IO) {
284+
val prefs = getSharedPreferences("abm", 0)
285+
val uris = prefs.getStringSet("persistable", setOf())!!.toMutableSet()
286+
uris.forEach { contentResolver.releasePersistableUriPermission(it.toUri(),
287+
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION) }
288+
prefs.edit().putStringSet("persistable", setOf()).apply()
289+
}
290+
}
272291
AppContent(vm, navController) {
273292
NavGraph(vm, navController, it)
274293
}

app/src/main/java/org/andbootmgr/app/Wizard.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import androidx.navigation.compose.NavHost
3636
import androidx.navigation.compose.composable
3737
import androidx.navigation.compose.rememberNavController
3838
import com.topjohnwu.superuser.io.SuFileOutputStream
39-
import kotlinx.coroutines.launch
4039
import org.andbootmgr.app.util.AbmOkHttp
4140
import org.andbootmgr.app.util.TerminalCancelException
4241
import org.andbootmgr.app.util.TerminalList
@@ -47,7 +46,6 @@ import java.io.InputStream
4746
import java.io.OutputStream
4847
import java.nio.file.Files
4948
import java.nio.file.StandardCopyOption
50-
import java.util.concurrent.CancellationException
5149

5250
abstract class WizardFlow {
5351
abstract fun get(vm: WizardState): List<IWizardPage>

0 commit comments

Comments
 (0)