diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt index cfe691828..5f396896e 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt @@ -237,10 +237,10 @@ abstract class BaseSimpleActivity : AppCompatActivity() { val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION applicationContext.contentResolver.takePersistableUriPermission(treeUri, takeFlags) - funAfterSAFPermission?.invoke(true) - funAfterSAFPermission = null + funAfterDelete30File?.invoke(true) + funAfterDelete30File = null } else { - funAfterSAFPermission?.invoke(false) + funAfterDelete30File?.invoke(false) } } else if (requestCode == OPEN_DOCUMENT_TREE_FOR_ANDROID_DATA_OR_OBB) { @@ -430,7 +430,7 @@ abstract class BaseSimpleActivity : AppCompatActivity() { callback(true) false } else if (isShowingSAFDialogForDeleteSdk30(path)) { - funAfterSAFPermission = callback + funAfterDelete30File = callback true } else { callback(true) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt index 80bae9edc..6d0724c96 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt @@ -154,7 +154,7 @@ fun BaseSimpleActivity.isShowingSAFDialog(path: String): Boolean { @SuppressLint("InlinedApi") fun BaseSimpleActivity.isShowingSAFDialogForDeleteSdk30(path: String): Boolean { - return if (!hasProperStoredFirstParentUri(path)) { + return if (isAccessibleWithSAFSdk30(path) && !hasProperStoredFirstParentUri(path)) { runOnUiThread { if (!isDestroyed && !isFinishing) { WritePermissionDialog(this, Mode.SDK_30) { @@ -644,31 +644,38 @@ fun BaseSimpleActivity.deleteFilesBg(files: List, allowDeleteFolder } var wasSuccess = false - handleSAFDialog(files[0].path) { + val firstFile = files.first() + handleSAFDialog(firstFile.path) { if (!it) { return@handleSAFDialog } - val failedFileDirItems = ArrayList() - files.forEachIndexed { index, file -> - deleteFileBg(file, allowDeleteFolder, true) { - if (it) { - wasSuccess = true - } else { - failedFileDirItems.add(file) - } + handleSAFDeleteSdk30Dialog(firstFile.path) { + if (!it) { + return@handleSAFDeleteSdk30Dialog + } - if (index == files.lastIndex) { - if (isRPlus() && failedFileDirItems.isNotEmpty()) { - val fileUris = getFileUrisFromFileDirItems(failedFileDirItems).second - deleteSDK30Uris(fileUris) { success -> - runOnUiThread { - callback?.invoke(success) - } - } + val failedFileDirItems = ArrayList() + files.forEachIndexed { index, file -> + deleteFileBg(file, allowDeleteFolder, true) { + if (it) { + wasSuccess = true } else { - runOnUiThread { - callback?.invoke(wasSuccess) + failedFileDirItems.add(file) + } + + if (index == files.lastIndex) { + if (isRPlus() && failedFileDirItems.isNotEmpty()) { + val fileUris = getFileUrisFromFileDirItems(failedFileDirItems).second + deleteSDK30Uris(fileUris) { success -> + runOnUiThread { + callback?.invoke(success) + } + } + } else { + runOnUiThread { + callback?.invoke(wasSuccess) + } } } } @@ -787,7 +794,7 @@ fun Activity.rescanPaths(paths: List, callback: (() -> Unit)? = null) { applicationContext.rescanPaths(paths, callback) } -fun BaseSimpleActivity.renameFile ( +fun BaseSimpleActivity.renameFile( oldPath: String, newPath: String, isRenamingMultipleFiles: Boolean, diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt index de21233b1..2519b986b 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt @@ -164,9 +164,13 @@ fun Context.getSAFOnlyDirs(): List { } fun Context.isAccessibleWithSAFSdk30(path: String): Boolean { + if (path.startsWith(filesDir.absolutePath)) { + return false + } + val firstParentPath = path.getFirstParentPath(this) val firstParentDir = path.getFirstParentDirName(this) - return isRPlus() && firstParentPath != path && + return isRPlus() && !Environment.isExternalStorageManager() && firstParentPath != path && DIRS_INACCESSIBLE_WITH_SAF_SDK_30.all { firstParentDir != it }