remove URI permissions for obsolete paths
This commit is contained in:
parent
f2270cfb77
commit
97e3063998
4 changed files with 28 additions and 4 deletions
|
@ -105,7 +105,7 @@ repositories {
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
|
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
|
||||||
implementation 'androidx.core:core-ktx:1.5.0'
|
implementation 'androidx.core:core-ktx:1.6.0'
|
||||||
implementation 'androidx.exifinterface:exifinterface:1.3.2'
|
implementation 'androidx.exifinterface:exifinterface:1.3.2'
|
||||||
implementation 'androidx.multidex:multidex:2.0.1'
|
implementation 'androidx.multidex:multidex:2.0.1'
|
||||||
implementation 'com.caverock:androidsvg-aar:1.4'
|
implementation 'com.caverock:androidsvg-aar:1.4'
|
||||||
|
|
|
@ -133,8 +133,7 @@ object PermissionManager {
|
||||||
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
||||||
fun revokeDirectoryAccess(context: Context, path: String): Boolean {
|
fun revokeDirectoryAccess(context: Context, path: String): Boolean {
|
||||||
return StorageUtils.convertDirPathToTreeUri(context, path)?.let {
|
return StorageUtils.convertDirPathToTreeUri(context, path)?.let {
|
||||||
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
releaseUriPermission(context, it)
|
||||||
context.contentResolver.releasePersistableUriPermission(it, flags)
|
|
||||||
true
|
true
|
||||||
} ?: false
|
} ?: false
|
||||||
}
|
}
|
||||||
|
@ -158,4 +157,28 @@ object PermissionManager {
|
||||||
}
|
}
|
||||||
return accessibleDirs
|
return accessibleDirs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// As of Android R, `MediaStore.getDocumentUri` fails if any of the persisted
|
||||||
|
// URI permissions we hold points to a folder that no longer exists,
|
||||||
|
// so we should remove these obsolete URIs before proceeding.
|
||||||
|
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
||||||
|
fun sanitizePersistedUriPermissions(context: Context) {
|
||||||
|
try {
|
||||||
|
for (uriPermission in context.contentResolver.persistedUriPermissions) {
|
||||||
|
val uri = uriPermission.uri
|
||||||
|
val path = StorageUtils.convertTreeUriToDirPath(context, uri)
|
||||||
|
if (path != null && !File(path).exists()) {
|
||||||
|
Log.d(LOG_TAG, "revoke URI permission for obsolete path=$path")
|
||||||
|
releaseUriPermission(context, uri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.w(LOG_TAG, "failed to sanitize persisted URI permissions", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun releaseUriPermission(context: Context, it: Uri) {
|
||||||
|
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
||||||
|
context.contentResolver.releasePersistableUriPermission(it, flags)
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -293,6 +293,7 @@ object StorageUtils {
|
||||||
// need a document URI (not a media content URI) to open a `DocumentFile` output stream
|
// need a document URI (not a media content URI) to open a `DocumentFile` output stream
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && isMediaStoreContentUri(mediaUri)) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && isMediaStoreContentUri(mediaUri)) {
|
||||||
// cleanest API to get it
|
// cleanest API to get it
|
||||||
|
PermissionManager.sanitizePersistedUriPermissions(context)
|
||||||
try {
|
try {
|
||||||
val docUri = MediaStore.getDocumentUri(context, mediaUri)
|
val docUri = MediaStore.getDocumentUri(context, mediaUri)
|
||||||
if (docUri != null) {
|
if (docUri != null) {
|
||||||
|
|
|
@ -6,7 +6,7 @@ buildscript {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:4.2.1'
|
classpath 'com.android.tools.build:gradle:4.2.2'
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
classpath 'com.google.gms:google-services:4.3.8'
|
classpath 'com.google.gms:google-services:4.3.8'
|
||||||
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1'
|
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1'
|
||||||
|
|
Loading…
Reference in a new issue