fix file renaming on sd card
This commit is contained in:
parent
b3e0dceff6
commit
b6cd6e39eb
1 changed files with 29 additions and 8 deletions
|
@ -2,14 +2,18 @@ package com.simplemobiletools.filemanager.dialogs
|
|||
|
||||
import android.content.Context
|
||||
import android.media.MediaScannerConnection
|
||||
import android.net.Uri
|
||||
import android.support.v4.provider.DocumentFile
|
||||
import android.support.v7.app.AlertDialog
|
||||
import android.view.LayoutInflater
|
||||
import android.view.WindowManager
|
||||
import com.simplemobiletools.filemanager.Config
|
||||
import com.simplemobiletools.filemanager.R
|
||||
import com.simplemobiletools.filemanager.Utils
|
||||
import com.simplemobiletools.filemanager.extensions.rescanItem
|
||||
import com.simplemobiletools.filemanager.extensions.toast
|
||||
import com.simplemobiletools.filemanager.extensions.value
|
||||
import com.simplemobiletools.filepicker.extensions.getSDCardPath
|
||||
import com.simplemobiletools.filepicker.models.FileDirItem
|
||||
import kotlinx.android.synthetic.main.rename_item.view.*
|
||||
import java.io.File
|
||||
|
@ -29,23 +33,34 @@ class RenameItemDialog(val context: Context, val path: String, val item: FileDir
|
|||
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
||||
show()
|
||||
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
|
||||
val name = view.item_name.value
|
||||
if (Utils.isNameValid(name)) {
|
||||
val newName = view.item_name.value
|
||||
if (Utils.isNameValid(newName)) {
|
||||
val currFile = File(path, item.name)
|
||||
val newFile = File(path, name)
|
||||
val newFile = File(path, newName)
|
||||
|
||||
if (newFile.exists()) {
|
||||
context.toast(R.string.name_taken)
|
||||
return@setOnClickListener
|
||||
}
|
||||
|
||||
if (currFile.renameTo(newFile)) {
|
||||
context.rescanItem(newFile)
|
||||
MediaScannerConnection.scanFile(context, arrayOf(currFile.absolutePath, newFile.absolutePath), null, null)
|
||||
if (Utils.needsStupidWritePermissions(context, path)) {
|
||||
val relativePath = currFile.absolutePath.substring(context.getSDCardPath().length + 1)
|
||||
var document = DocumentFile.fromTreeUri(context, Uri.parse(Config.newInstance(context).treeUri))
|
||||
val parts = relativePath.split("/")
|
||||
for (part in parts) {
|
||||
document = document.findFile(part)
|
||||
}
|
||||
if (document.canWrite())
|
||||
document.renameTo(newName)
|
||||
sendSuccess(currFile, newFile)
|
||||
dismiss()
|
||||
listener.onSuccess()
|
||||
} else {
|
||||
context.toast(R.string.error_occurred)
|
||||
if (currFile.renameTo(newFile)) {
|
||||
sendSuccess(currFile, newFile)
|
||||
dismiss()
|
||||
} else {
|
||||
context.toast(R.string.error_occurred)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
context.toast(R.string.invalid_name)
|
||||
|
@ -54,6 +69,12 @@ class RenameItemDialog(val context: Context, val path: String, val item: FileDir
|
|||
}
|
||||
}
|
||||
|
||||
private fun sendSuccess(currFile: File, newFile: File) {
|
||||
context.rescanItem(newFile)
|
||||
MediaScannerConnection.scanFile(context, arrayOf(currFile.absolutePath, newFile.absolutePath), null, null)
|
||||
listener.onSuccess()
|
||||
}
|
||||
|
||||
interface OnRenameItemListener {
|
||||
fun onSuccess()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue