handle renaming files

This commit is contained in:
darthpaul 2021-10-29 10:09:09 +01:00
parent b228ab4544
commit df2fc3f38a
2 changed files with 51 additions and 25 deletions

View file

@ -752,35 +752,49 @@ fun BaseSimpleActivity.renameFile(oldPath: String, newPath: String, callback: ((
} }
} }
} else { } else {
val oldFile = File(oldPath) if (isRestrictedAndroidDir(oldPath)) {
val newFile = File(newPath) try {
val tempFile = oldFile.createTempFile() val success = renameSAFOnlyDocument(oldPath, newPath)
val oldToTempSucceeds = oldFile.renameTo(tempFile) runOnUiThread {
val tempToNewSucceeds = tempFile.renameTo(newFile) callback?.invoke(success)
if (oldToTempSucceeds && tempToNewSucceeds) {
if (newFile.isDirectory) {
updateInMediaStore(oldPath, newPath)
rescanPath(newPath) {
runOnUiThread {
callback?.invoke(true)
}
scanPathRecursively(newPath)
} }
} else { } catch (e: Exception) {
if (!baseConfig.keepLastModified) { showErrorToast(e)
newFile.setLastModified(System.currentTimeMillis()) runOnUiThread {
} callback?.invoke(false)
updateInMediaStore(oldPath, newPath)
scanPathsRecursively(arrayListOf(newPath)) {
runOnUiThread {
callback?.invoke(true)
}
} }
} }
} else { } else {
tempFile.delete() val oldFile = File(oldPath)
runOnUiThread { val newFile = File(newPath)
callback?.invoke(false) val tempFile = oldFile.createTempFile()
val oldToTempSucceeds = oldFile.renameTo(tempFile)
val tempToNewSucceeds = tempFile.renameTo(newFile)
if (oldToTempSucceeds && tempToNewSucceeds) {
if (newFile.isDirectory) {
updateInMediaStore(oldPath, newPath)
rescanPath(newPath) {
runOnUiThread {
callback?.invoke(true)
}
scanPathRecursively(newPath)
}
} else {
if (!baseConfig.keepLastModified) {
newFile.setLastModified(System.currentTimeMillis())
}
updateInMediaStore(oldPath, newPath)
scanPathsRecursively(arrayListOf(newPath)) {
runOnUiThread {
callback?.invoke(true)
}
}
}
} else {
tempFile.delete()
runOnUiThread {
callback?.invoke(false)
}
} }
} }
} }

View file

@ -165,6 +165,10 @@ fun Context.isSAFOnlyRoot(path: String): Boolean {
return result return result
} }
fun Context.isRestrictedAndroidDir(path: String): Boolean {
return isRPlus() && isSAFOnlyRoot(path)
}
// no need to use DocumentFile if an SD card is set as the default storage // no need to use DocumentFile if an SD card is set as the default storage
fun Context.needsStupidWritePermissions(path: String) = (isPathOnSD(path) || isPathOnOTG(path)) && !isSDCardSetAsDefaultStorage() fun Context.needsStupidWritePermissions(path: String) = (isPathOnSD(path) || isPathOnOTG(path)) && !isSDCardSetAsDefaultStorage()
@ -580,6 +584,14 @@ fun Context.createSAFOnlyFile(path: String): Boolean {
return DocumentsContract.createDocument(contentResolver, parentUri, path.getMimeType(), path.getFilenameFromPath()) != null return DocumentsContract.createDocument(contentResolver, parentUri, path.getMimeType(), path.getFilenameFromPath()) != null
} }
fun Context.renameSAFOnlyDocument(oldPath: String, newPath: String): Boolean {
val treeUri = baseConfig.primaryAndroidTreeUri.toUri()
val relativePath = oldPath.substring(baseConfig.internalStoragePath.length).trim('/')
val documentId = "primary:$relativePath"
val parentUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, documentId)
return DocumentsContract.renameDocument(contentResolver, parentUri, newPath.getFilenameFromPath()) != null
}
private const val TAG = "Context-storage" private const val TAG = "Context-storage"
fun Context.deleteSAFOnlyDir(path: String, allowDeleteFolder: Boolean = false, callback: ((wasSuccess: Boolean) -> Unit)? = null) { fun Context.deleteSAFOnlyDir(path: String, allowDeleteFolder: Boolean = false, callback: ((wasSuccess: Boolean) -> Unit)? = null) {