fix file renaming on sd card

This commit is contained in:
tibbi 2016-11-05 16:33:32 +01:00
parent b3e0dceff6
commit b6cd6e39eb

View File

@ -2,14 +2,18 @@ package com.simplemobiletools.filemanager.dialogs
import android.content.Context import android.content.Context
import android.media.MediaScannerConnection import android.media.MediaScannerConnection
import android.net.Uri
import android.support.v4.provider.DocumentFile
import android.support.v7.app.AlertDialog import android.support.v7.app.AlertDialog
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.WindowManager import android.view.WindowManager
import com.simplemobiletools.filemanager.Config
import com.simplemobiletools.filemanager.R import com.simplemobiletools.filemanager.R
import com.simplemobiletools.filemanager.Utils import com.simplemobiletools.filemanager.Utils
import com.simplemobiletools.filemanager.extensions.rescanItem import com.simplemobiletools.filemanager.extensions.rescanItem
import com.simplemobiletools.filemanager.extensions.toast import com.simplemobiletools.filemanager.extensions.toast
import com.simplemobiletools.filemanager.extensions.value import com.simplemobiletools.filemanager.extensions.value
import com.simplemobiletools.filepicker.extensions.getSDCardPath
import com.simplemobiletools.filepicker.models.FileDirItem import com.simplemobiletools.filepicker.models.FileDirItem
import kotlinx.android.synthetic.main.rename_item.view.* import kotlinx.android.synthetic.main.rename_item.view.*
import java.io.File 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) window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
show() show()
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({ getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
val name = view.item_name.value val newName = view.item_name.value
if (Utils.isNameValid(name)) { if (Utils.isNameValid(newName)) {
val currFile = File(path, item.name) val currFile = File(path, item.name)
val newFile = File(path, name) val newFile = File(path, newName)
if (newFile.exists()) { if (newFile.exists()) {
context.toast(R.string.name_taken) context.toast(R.string.name_taken)
return@setOnClickListener return@setOnClickListener
} }
if (currFile.renameTo(newFile)) { if (Utils.needsStupidWritePermissions(context, path)) {
context.rescanItem(newFile) val relativePath = currFile.absolutePath.substring(context.getSDCardPath().length + 1)
MediaScannerConnection.scanFile(context, arrayOf(currFile.absolutePath, newFile.absolutePath), null, null) 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() dismiss()
listener.onSuccess()
} else { } else {
context.toast(R.string.error_occurred) if (currFile.renameTo(newFile)) {
sendSuccess(currFile, newFile)
dismiss()
} else {
context.toast(R.string.error_occurred)
}
} }
} else { } else {
context.toast(R.string.invalid_name) 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 { interface OnRenameItemListener {
fun onSuccess() fun onSuccess()
} }