Merge pull request #1323 from KryptKode/feat/deleting-with-saf-sdk30+

Feat/deleting with SAF sdk30+
This commit is contained in:
Tibor Kaputa 2022-02-25 22:56:48 +01:00 committed by GitHub
commit cdf0668548
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 26 deletions

View file

@ -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)

View file

@ -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<FileDirItem>, allowDeleteFolder
}
var wasSuccess = false
handleSAFDialog(files[0].path) {
val firstFile = files.first()
handleSAFDialog(firstFile.path) {
if (!it) {
return@handleSAFDialog
}
val failedFileDirItems = ArrayList<FileDirItem>()
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<FileDirItem>()
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<String>, callback: (() -> Unit)? = null) {
applicationContext.rescanPaths(paths, callback)
}
fun BaseSimpleActivity.renameFile (
fun BaseSimpleActivity.renameFile(
oldPath: String,
newPath: String,
isRenamingMultipleFiles: Boolean,

View file

@ -164,9 +164,13 @@ fun Context.getSAFOnlyDirs(): List<String> {
}
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
}