@@ -38,6 +38,7 @@ import androidx.compose.material3.TopAppBarDefaults
3838import androidx.compose.material3.rememberDrawerState
3939import androidx.compose.runtime.Composable
4040import androidx.compose.runtime.DisposableEffect
41+ import androidx.compose.runtime.LaunchedEffect
4142import androidx.compose.runtime.getValue
4243import androidx.compose.runtime.mutableStateMapOf
4344import androidx.compose.runtime.mutableStateOf
@@ -48,6 +49,7 @@ import androidx.compose.ui.Modifier
4849import androidx.compose.ui.res.stringResource
4950import androidx.compose.ui.tooling.preview.Preview
5051import androidx.compose.ui.unit.dp
52+ import androidx.core.net.toUri
5153import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
5254import androidx.navigation.NavHostController
5355import 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 }
0 commit comments