create a helper function for getting the documentfile

This commit is contained in:
tibbi 2016-11-05 17:58:27 +01:00
parent b6cd6e39eb
commit 20fb58ddbe
2 changed files with 13 additions and 10 deletions

View File

@ -3,8 +3,10 @@ package com.simplemobiletools.filemanager
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.support.v4.content.ContextCompat
import android.support.v4.provider.DocumentFile
import android.widget.Toast
import com.simplemobiletools.filepicker.extensions.getSDCardPath
import java.util.regex.Pattern
@ -39,4 +41,14 @@ object Utils {
}
fun isKitkat() = Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT
fun getFileDocument(context: Context, path: String): DocumentFile {
val relativePath = path.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)
}
return document
}
}

View File

@ -2,18 +2,14 @@ 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
@ -44,12 +40,7 @@ class RenameItemDialog(val context: Context, val path: String, val item: FileDir
}
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)
}
val document = Utils.getFileDocument(context, currFile.absolutePath)
if (document.canWrite())
document.renameTo(newName)
sendSuccess(currFile, newFile)