handle renaming files
This commit is contained in:
parent
b228ab4544
commit
df2fc3f38a
2 changed files with 51 additions and 25 deletions
|
@ -752,35 +752,49 @@ fun BaseSimpleActivity.renameFile(oldPath: String, newPath: String, callback: ((
|
|||
}
|
||||
}
|
||||
} else {
|
||||
val oldFile = File(oldPath)
|
||||
val newFile = File(newPath)
|
||||
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)
|
||||
if (isRestrictedAndroidDir(oldPath)) {
|
||||
try {
|
||||
val success = renameSAFOnlyDocument(oldPath, newPath)
|
||||
runOnUiThread {
|
||||
callback?.invoke(success)
|
||||
}
|
||||
} else {
|
||||
if (!baseConfig.keepLastModified) {
|
||||
newFile.setLastModified(System.currentTimeMillis())
|
||||
}
|
||||
updateInMediaStore(oldPath, newPath)
|
||||
scanPathsRecursively(arrayListOf(newPath)) {
|
||||
runOnUiThread {
|
||||
callback?.invoke(true)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
showErrorToast(e)
|
||||
runOnUiThread {
|
||||
callback?.invoke(false)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tempFile.delete()
|
||||
runOnUiThread {
|
||||
callback?.invoke(false)
|
||||
val oldFile = File(oldPath)
|
||||
val newFile = File(newPath)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -165,6 +165,10 @@ fun Context.isSAFOnlyRoot(path: String): Boolean {
|
|||
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
|
||||
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
|
||||
}
|
||||
|
||||
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"
|
||||
|
||||
fun Context.deleteSAFOnlyDir(path: String, allowDeleteFolder: Boolean = false, callback: ((wasSuccess: Boolean) -> Unit)? = null) {
|
||||
|
|
Loading…
Reference in a new issue