Renaming on SDK 30

- handle renaming files with SAF except files in the Download directory
- handle renaming files in the Download directory using MediaStore.createWrite request, then duplicating the file with the new name
- handle renaming files in the root of internal storage using MediaStore.createWrite request, then using contentResolver to update the display name
- this methods do not work for files in the root of SD card and all files in OTG media
This commit is contained in:
darthpaul 2022-03-19 18:09:54 +00:00
parent 557d5bee02
commit dc0ca635c5
8 changed files with 339 additions and 99 deletions

View file

@ -207,6 +207,15 @@ class FilePickerDialog(
if ((pickFile && fileDocument.isFile) || (!pickFile && fileDocument.isDirectory)) {
sendSuccess()
}
} else if (activity.isAccessibleWithSAFSdk30(currPath)) {
activity.handleSAFDialogSdk30(currPath) {
if (it) {
val document = activity.getSomeDocumentSdk30(currPath) ?: return@handleSAFDialogSdk30
if ((pickFile && document.isFile) || (!pickFile && document.isDirectory)) {
sendSuccess()
}
}
}
} else {
val file = File(currPath)
if ((pickFile && file.isFile) || (!pickFile && file.isDirectory)) {

View file

@ -0,0 +1,74 @@
package com.simplemobiletools.commons.extensions
import android.content.ContentValues
import android.provider.MediaStore
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.models.FileDirItem
import java.io.File
import java.io.InputStream
import java.io.OutputStream
fun BaseSimpleActivity.copySingleFileSdk30(source: FileDirItem, destination: FileDirItem): Boolean {
val directory = destination.getParentPath()
if (!createDirectorySync(directory)) {
val error = String.format(getString(R.string.could_not_create_folder), directory)
showErrorToast(error)
return false
}
var inputStream: InputStream? = null
var out: OutputStream? = null
try {
out = getFileOutputStreamSync(destination.path, source.path.getMimeType())
inputStream = getFileInputStreamSync(source.path)!!
var copiedSize = 0L
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var bytes = inputStream.read(buffer)
while (bytes >= 0) {
out!!.write(buffer, 0, bytes)
copiedSize += bytes
bytes = inputStream.read(buffer)
}
out?.flush()
return if (source.size == copiedSize && getDoesFilePathExist(destination.path)) {
if (baseConfig.keepLastModified) {
copyOldLastModified(source.path, destination.path)
File(destination.path).setLastModified(File(source.path).lastModified())
}
true
} else {
false
}
} finally {
inputStream?.close()
out?.close()
}
}
fun BaseSimpleActivity.copyOldLastModified(sourcePath: String, destinationPath: String) {
val projection = arrayOf(MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.DATE_MODIFIED)
val uri = MediaStore.Files.getContentUri("external")
val selection = "${MediaStore.MediaColumns.DATA} = ?"
var selectionArgs = arrayOf(sourcePath)
val cursor = applicationContext.contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
val dateModified = cursor.getIntValue(MediaStore.Images.Media.DATE_MODIFIED)
val values = ContentValues().apply {
put(MediaStore.Images.Media.DATE_TAKEN, dateTaken)
put(MediaStore.Images.Media.DATE_MODIFIED, dateModified)
}
selectionArgs = arrayOf(destinationPath)
applicationContext.contentResolver.update(uri, values, selection, selectionArgs)
}
}
}

View file

@ -834,26 +834,49 @@ fun BaseSimpleActivity.renameFile(
oldPath: String,
newPath: String,
isRenamingMultipleFiles: Boolean,
callback: ((success: Boolean, useAndroid30Way: Boolean) -> Unit)? = null
callback: ((success: Boolean, android30RenameFormat: Android30RenameFormat) -> Unit)? = null
) {
if (isRestrictedSAFOnlyRoot(oldPath)) {
handleAndroidSAFDialog(oldPath) {
if (!it) {
runOnUiThread {
callback?.invoke(false, false)
callback?.invoke(false, Android30RenameFormat.NONE)
}
return@handleAndroidSAFDialog
}
try {
ensureBackgroundThread {
val success = renameAndroidSAFDocument(oldPath, newPath)
runOnUiThread {
callback?.invoke(success, false)
callback?.invoke(success, Android30RenameFormat.NONE)
}
}
} catch (e: Exception) {
showErrorToast(e)
runOnUiThread {
callback?.invoke(false, false)
callback?.invoke(false, Android30RenameFormat.NONE)
}
}
}
} else if (isAccessibleWithSAFSdk30(oldPath)) {
handleSAFDialogSdk30(oldPath) {
if (!it) {
return@handleSAFDialogSdk30
}
try {
ensureBackgroundThread {
val success = renameDocumentSdk30(oldPath, newPath)
runOnUiThread {
callback?.invoke(success, Android30RenameFormat.NONE)
}
}
} catch (e: Exception) {
showErrorToast(e)
runOnUiThread {
callback?.invoke(false, Android30RenameFormat.NONE)
}
}
}
@ -866,7 +889,7 @@ fun BaseSimpleActivity.renameFile(
val document = getSomeDocumentFile(oldPath)
if (document == null || (File(oldPath).isDirectory != document.isDirectory)) {
runOnUiThread {
callback?.invoke(false, false)
callback?.invoke(false, Android30RenameFormat.NONE)
}
return@handleSAFDialog
}
@ -879,7 +902,7 @@ fun BaseSimpleActivity.renameFile(
// FileNotFoundException is thrown in some weird cases, but renaming works just fine
} catch (e: Exception) {
showErrorToast(e)
callback?.invoke(false, false)
callback?.invoke(false, Android30RenameFormat.NONE)
return@ensureBackgroundThread
}
@ -890,14 +913,14 @@ fun BaseSimpleActivity.renameFile(
}
deleteFromMediaStore(oldPath)
runOnUiThread {
callback?.invoke(true, false)
callback?.invoke(true, Android30RenameFormat.NONE)
}
}
}
} catch (e: Exception) {
showErrorToast(e)
runOnUiThread {
callback?.invoke(false, false)
callback?.invoke(false, Android30RenameFormat.NONE)
}
}
}
@ -910,7 +933,7 @@ fun BaseSimpleActivity.renameFile(
if (isRPlus() && exception is java.nio.file.FileSystemException) {
// if we are renaming multiple files at once, we should give the Android 30+ permission dialog all uris together, not one by one
if (isRenamingMultipleFiles) {
callback?.invoke(false, true)
callback?.invoke(false, Android30RenameFormat.CONTENT_RESOLVER)
} else {
val fileUris = getFileUrisFromFileDirItems(arrayListOf(File(oldPath).toFileDirItem(this))).second
updateSDK30Uris(fileUris) { success ->
@ -921,19 +944,19 @@ fun BaseSimpleActivity.renameFile(
try {
contentResolver.update(fileUris.first(), values, null, null)
callback?.invoke(true, false)
callback?.invoke(true, Android30RenameFormat.NONE)
} catch (e: Exception) {
showErrorToast(e)
callback?.invoke(false, false)
callback?.invoke(false, Android30RenameFormat.NONE)
}
} else {
callback?.invoke(false, false)
callback?.invoke(false, Android30RenameFormat.NONE)
}
}
}
} else {
showErrorToast(exception)
callback?.invoke(false, false)
callback?.invoke(false, Android30RenameFormat.NONE)
}
return
}
@ -945,7 +968,7 @@ fun BaseSimpleActivity.renameFile(
updateInMediaStore(oldPath, newPath)
rescanPath(newPath) {
runOnUiThread {
callback?.invoke(true, false)
callback?.invoke(true, Android30RenameFormat.NONE)
}
deleteFromMediaStore(oldPath)
scanPathRecursively(newPath)
@ -958,14 +981,48 @@ fun BaseSimpleActivity.renameFile(
scanPathsRecursively(arrayListOf(newPath)) {
deleteFromMediaStore(oldPath)
runOnUiThread {
callback?.invoke(true, false)
callback?.invoke(true, Android30RenameFormat.NONE)
}
}
}
} else {
tempFile.delete()
newFile.delete()
if (isRPlus()) {
// if we are renaming multiple files at once, we should give the Android 30+ permission dialog all uris together, not one by one
if (isRenamingMultipleFiles) {
callback?.invoke(false, Android30RenameFormat.SAF)
} else {
val fileUris = getFileUrisFromFileDirItems(arrayListOf(File(oldPath).toFileDirItem(this))).second
updateSDK30Uris(fileUris) { success ->
if (!success) {
return@updateSDK30Uris
}
try {
val sourceFile = File(oldPath).toFileDirItem(this)
val destinationFile = sourceFile.copy(path = newPath, name = newPath.getFilenameFromPath())
val copySuccessful = copySingleFileSdk30(sourceFile, destinationFile)
if (copySuccessful) {
if (!baseConfig.keepLastModified) {
newFile.setLastModified(System.currentTimeMillis())
}
contentResolver.delete(fileUris.first(), null)
updateInMediaStore(oldPath, newPath)
scanPathsRecursively(arrayListOf(newPath)) {
runOnUiThread {
callback?.invoke(false, false)
callback?.invoke(true, Android30RenameFormat.NONE)
}
}
}
} catch (e: Exception) {
showErrorToast(e)
callback?.invoke(false, Android30RenameFormat.NONE)
}
}
}
} else {
callback?.invoke(false, Android30RenameFormat.NONE)
}
}
}

View file

@ -153,6 +153,18 @@ fun Context.deleteDocumentWithSAFSdk30(fileDirItem: FileDirItem, allowDeleteFold
}
}
fun Context.renameDocumentSdk30(oldPath: String, newPath: String): Boolean {
return try {
val treeUri = createFirstParentTreeUri(oldPath)
val documentId = getSAFDocumentId(oldPath)
val parentUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, documentId)
DocumentsContract.renameDocument(contentResolver, parentUri, newPath.getFilenameFromPath()) != null
} catch (e: IllegalStateException) {
showErrorToast(e)
false
}
}
fun Context.hasProperStoredDocumentUriSdk30(path: String): Boolean {
val documentUri = buildDocumentUriSdk30(path)
return contentResolver.persistedUriPermissions.any { it.uri.toString() == documentUri.toString() }

View file

@ -0,0 +1,7 @@
package com.simplemobiletools.commons.models
enum class Android30RenameFormat {
SAF,
CONTENT_RESOLVER,
NONE
}

View file

@ -7,7 +7,7 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.*
import java.io.File
open class FileDirItem(
data class FileDirItem(
val path: String,
val name: String = "",
var isDirectory: Boolean = false,

View file

@ -12,6 +12,7 @@ import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.isNougatPlus
import com.simplemobiletools.commons.interfaces.RenameTab
import com.simplemobiletools.commons.models.Android30RenameFormat
import kotlinx.android.synthetic.main.dialog_rename_items_pattern.view.*
import java.io.File
import java.text.SimpleDateFormat
@ -50,8 +51,9 @@ class RenamePatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(c
}
val validPaths = paths.filter { activity?.getDoesFilePathExist(it) == true }
val sdFilePath = validPaths.firstOrNull { activity?.isPathOnSD(it) == true } ?: validPaths.firstOrNull()
if (sdFilePath == null) {
val firstPath = validPaths.firstOrNull()
val sdFilePath = validPaths.firstOrNull { activity?.isPathOnSD(it) == true } ?: firstPath
if (firstPath == null || sdFilePath == null) {
activity?.toast(R.string.unknown_error_occurred)
return
}
@ -62,17 +64,22 @@ class RenamePatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(c
return@handleSAFDialog
}
activity?.handleSAFDialogSdk30(firstPath) {
if (!it) {
return@handleSAFDialogSdk30
}
ignoreClicks = true
var pathsCnt = validPaths.size
numbersCnt = pathsCnt.toString().length
for (path in validPaths) {
if (stopLooping) {
return@handleSAFDialog
return@handleSAFDialogSdk30
}
try {
val newPath = getNewPath(path, useMediaFileExtension) ?: continue
activity?.renameFile(path, newPath, true) { success, useAndroid30Way ->
activity?.renameFile(path, newPath, true) { success, android30Format ->
if (success) {
pathsCnt--
if (pathsCnt == 0) {
@ -80,10 +87,10 @@ class RenamePatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(c
}
} else {
ignoreClicks = false
if (useAndroid30Way) {
if (android30Format != Android30RenameFormat.NONE) {
currentIncrementalNumber = 1
stopLooping = true
renameAllFiles(validPaths, useMediaFileExtension, callback)
renameAllFiles(validPaths, useMediaFileExtension, android30Format, callback)
} else {
activity?.toast(R.string.unknown_error_occurred)
}
@ -93,10 +100,10 @@ class RenamePatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(c
activity?.showErrorToast(e)
}
}
stopLooping = false
}
}
}
private fun getNewPath(path: String, useMediaFileExtension: Boolean): String? {
try {
@ -167,28 +174,61 @@ class RenamePatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(c
}
}
private fun renameAllFiles(paths: List<String>, useMediaFileExtension: Boolean, callback: (success: Boolean) -> Unit) {
private fun renameAllFiles(
paths: List<String>,
useMediaFileExtension: Boolean,
android30Format: Android30RenameFormat,
callback: (success: Boolean) -> Unit
) {
val fileDirItems = paths.map { File(it).toFileDirItem(context) }
val uriPairs = context.getFileUrisFromFileDirItems(fileDirItems)
val validPaths = uriPairs.first
val uris = uriPairs.second
val activity = activity
activity?.updateSDK30Uris(uris) { success ->
if (success) {
try {
uris.forEachIndexed { index, uri ->
val path = validPaths[index]
val newFileName = getNewPath(path, useMediaFileExtension)?.getFilenameFromPath() ?: return@forEachIndexed
when (android30Format) {
Android30RenameFormat.SAF -> {
val sourceFile = File(path).toFileDirItem(context)
val newPath = "${path.getParentPath()}/$newFileName"
val destinationFile = sourceFile.copy(path = newPath, name = newFileName)
if (activity.copySingleFileSdk30(sourceFile, destinationFile)) {
if (!activity.baseConfig.keepLastModified) {
File(newPath).setLastModified(System.currentTimeMillis())
}
activity.contentResolver.delete(uri, null)
activity.updateInMediaStore(path, newPath)
activity.scanPathsRecursively(arrayListOf(newPath))
}
}
Android30RenameFormat.CONTENT_RESOLVER -> {
val values = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, newFileName)
}
context.contentResolver.update(uri, values, null, null)
}
Android30RenameFormat.NONE -> {
activity.runOnUiThread {
callback(true)
}
return@forEachIndexed
}
}
}
activity.runOnUiThread {
callback(true)
}
} catch (e: Exception) {
activity.runOnUiThread {
activity.showErrorToast(e)
callback(false)
}
}
}
}
}
}

View file

@ -9,6 +9,7 @@ import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.interfaces.RenameTab
import com.simplemobiletools.commons.models.Android30RenameFormat
import kotlinx.android.synthetic.main.tab_rename_simple.view.*
import java.io.File
@ -44,8 +45,9 @@ class RenameSimpleTab(context: Context, attrs: AttributeSet) : RelativeLayout(co
}
val validPaths = paths.filter { activity?.getDoesFilePathExist(it) == true }
val sdFilePath = validPaths.firstOrNull { activity?.isPathOnSD(it) == true } ?: validPaths.firstOrNull()
if (sdFilePath == null) {
val firstPath = validPaths.firstOrNull()
val sdFilePath = validPaths.firstOrNull { activity?.isPathOnSD(it) == true } ?: firstPath
if (firstPath == null || sdFilePath == null) {
activity?.toast(R.string.unknown_error_occurred)
return
}
@ -55,11 +57,16 @@ class RenameSimpleTab(context: Context, attrs: AttributeSet) : RelativeLayout(co
return@handleSAFDialog
}
activity?.handleSAFDialogSdk30(firstPath) {
if (!it) {
return@handleSAFDialogSdk30
}
ignoreClicks = true
var pathsCnt = validPaths.size
for (path in validPaths) {
if (stopLooping) {
return@handleSAFDialog
return@handleSAFDialogSdk30
}
val fullName = path.getFilenameFromPath()
@ -83,7 +90,7 @@ class RenameSimpleTab(context: Context, attrs: AttributeSet) : RelativeLayout(co
continue
}
activity?.renameFile(path, newPath, true) { success, useAndroid30Way ->
activity?.renameFile(path, newPath, true) { success, android30Format ->
if (success) {
pathsCnt--
if (pathsCnt == 0) {
@ -91,25 +98,32 @@ class RenameSimpleTab(context: Context, attrs: AttributeSet) : RelativeLayout(co
}
} else {
ignoreClicks = false
if (useAndroid30Way) {
if (android30Format != Android30RenameFormat.NONE) {
stopLooping = true
renameAllFiles(validPaths, append, valueToAdd, callback)
renameAllFiles(validPaths, append, valueToAdd, android30Format, callback)
} else {
activity?.toast(R.string.unknown_error_occurred)
}
}
}
}
stopLooping = false
}
}
}
private fun renameAllFiles(paths: List<String>, appendString: Boolean, stringToAdd: String, callback: (success: Boolean) -> Unit) {
private fun renameAllFiles(
paths: List<String>,
appendString: Boolean,
stringToAdd: String,
android30Format: Android30RenameFormat,
callback: (success: Boolean) -> Unit
) {
val fileDirItems = paths.map { File(it).toFileDirItem(context) }
val uriPairs = context.getFileUrisFromFileDirItems(fileDirItems)
val validPaths = uriPairs.first
val uris = uriPairs.second
val activity = activity
activity?.updateSDK30Uris(uris) { success ->
if (success) {
try {
@ -131,17 +145,44 @@ class RenameSimpleTab(context: Context, attrs: AttributeSet) : RelativeLayout(co
"$stringToAdd$fullName"
}
when (android30Format) {
Android30RenameFormat.SAF -> {
val sourceFile = File(path).toFileDirItem(activity)
val newPath = "${path.getParentPath()}/$newName"
val destinationFile = sourceFile.copy(path = newPath, name = newName)
if (activity.copySingleFileSdk30(sourceFile, destinationFile)) {
if (!activity.baseConfig.keepLastModified) {
File(newPath).setLastModified(System.currentTimeMillis())
}
activity.contentResolver.delete(uri, null)
activity.updateInMediaStore(path, newPath)
activity.scanPathsRecursively(arrayListOf(newPath))
}
}
Android30RenameFormat.CONTENT_RESOLVER -> {
val values = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, newName)
}
context.contentResolver.update(uri, values, null, null)
}
Android30RenameFormat.NONE -> {
activity.runOnUiThread {
callback(true)
}
return@forEachIndexed
}
}
}
activity.runOnUiThread {
callback(true)
}
} catch (e: Exception) {
activity.runOnUiThread {
activity.showErrorToast(e)
callback(false)
}
}
}
}
}
}