fix some glitches at getFileUrisFromFileDirItems, make it more reliable

This commit is contained in:
tibbi 2021-12-19 21:05:10 +01:00
parent 32e11eb325
commit 6204bcd684
4 changed files with 20 additions and 11 deletions

View file

@ -615,7 +615,7 @@ fun BaseSimpleActivity.deleteFilesBg(files: List<FileDirItem>, allowDeleteFolder
if (index == files.size - 1) {
if (isRPlus() && failedFileDirItems.isNotEmpty()) {
val fileUris = getFileUrisFromFileDirItems(failedFileDirItems)
val fileUris = getFileUrisFromFileDirItems(failedFileDirItems).second
deleteSDK30Uris(fileUris) { success ->
runOnUiThread {
callback?.invoke(success)
@ -677,7 +677,7 @@ fun BaseSimpleActivity.deleteFileBg(fileDirItem: FileDirItem, allowDeleteFolder:
}
}
} else if (isRPlus()) {
val fileUris = getFileUrisFromFileDirItems(arrayListOf(fileDirItem))
val fileUris = getFileUrisFromFileDirItems(arrayListOf(fileDirItem)).second
deleteSDK30Uris(fileUris) { success ->
runOnUiThread {
callback?.invoke(success)
@ -806,7 +806,7 @@ fun BaseSimpleActivity.renameFile(
if (isRenamingMultipleFiles) {
callback?.invoke(false, true)
} else {
val fileUris = getFileUrisFromFileDirItems(arrayListOf(File(oldPath).toFileDirItem(this)))
val fileUris = getFileUrisFromFileDirItems(arrayListOf(File(oldPath).toFileDirItem(this))).second
updateSDK30Uris(fileUris) { success ->
if (success) {
val values = ContentValues().apply {

View file

@ -926,21 +926,26 @@ private val physicalPaths = arrayListOf(
"/storage/usbdisk2"
)
fun Context.getFileUrisFromFileDirItems(fileDirItems: List<FileDirItem>): ArrayList<Uri> {
// Convert paths like /storage/emulated/0/Pictures/Screenshots/first.jpg to content://media/external/images/media/131799
// so that we can refer to the file in the MediaStore.
// If we found no mediastore uri for a given file, do not return its path either to avoid some mismatching
fun Context.getFileUrisFromFileDirItems(fileDirItems: List<FileDirItem>): Pair<ArrayList<String>, ArrayList<Uri>> {
val fileUris = ArrayList<Uri>()
val successfulFilePaths = ArrayList<String>()
val allIds = getMediaStoreIds(this)
val filePaths = fileDirItems.map { it.path.lowercase(Locale.getDefault()) }
val filePaths = fileDirItems.map { it.path }
filePaths.forEach { path ->
for ((filePath, mediaStoreId) in allIds) {
if (filePath.lowercase() == path) {
if (filePath.lowercase() == path.lowercase()) {
val baseUri = getFileUri(filePath)
val uri = ContentUris.withAppendedId(baseUri, mediaStoreId)
fileUris.add(uri)
successfulFilePaths.add(path)
}
}
}
return fileUris
return Pair(successfulFilePaths, fileUris)
}
fun getMediaStoreIds(context: Context): HashMap<String, Long> {

View file

@ -168,12 +168,14 @@ class RenamePatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(c
private fun renameAllFiles(paths: List<String>, useMediaFileExtension: Boolean, callback: (success: Boolean) -> Unit) {
val fileDirItems = paths.map { File(it).toFileDirItem(context) }
val uris = context.getFileUrisFromFileDirItems(fileDirItems)
val uriPairs = context.getFileUrisFromFileDirItems(fileDirItems)
val validPaths = uriPairs.first
val uris = uriPairs.second
activity?.updateSDK30Uris(uris) { success ->
if (success) {
try {
uris.forEachIndexed { index, uri ->
val path = paths[index]
val path = validPaths[index]
val newFileName = getNewPath(path, useMediaFileExtension)?.getFilenameFromPath() ?: return@forEachIndexed
val values = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, newFileName)

View file

@ -106,12 +106,14 @@ class RenameSimpleTab(context: Context, attrs: AttributeSet) : RelativeLayout(co
private fun renameAllFiles(paths: List<String>, appendString: Boolean, stringToAdd: String, callback: (success: Boolean) -> Unit) {
val fileDirItems = paths.map { File(it).toFileDirItem(context) }
val uris = context.getFileUrisFromFileDirItems(fileDirItems)
val uriPairs = context.getFileUrisFromFileDirItems(fileDirItems)
val validPaths = uriPairs.first
val uris = uriPairs.second
activity?.updateSDK30Uris(uris) { success ->
if (success) {
try {
uris.forEachIndexed { index, uri ->
val path = paths[index]
val path = validPaths[index]
val fullName = path.getFilenameFromPath()
var dotAt = fullName.lastIndexOf(".")