make isPathOnRoot a context extension

This commit is contained in:
tibbi 2017-09-04 19:27:19 +02:00
parent 32ca1fdf70
commit 794f6cc462
2 changed files with 6 additions and 5 deletions

View file

@ -1,6 +1,9 @@
package com.simplemobiletools.filemanager.extensions
import android.content.Context
import com.simplemobiletools.commons.extensions.hasExternalSDCard
import com.simplemobiletools.filemanager.Config
val Context.config: Config get() = Config.newInstance(this)
fun Context.isPathOnRoot(path: String) = !(path.startsWith(config.internalStoragePath) || (hasExternalSDCard() && path.startsWith(config.sdCardPath)))

View file

@ -23,6 +23,7 @@ import com.simplemobiletools.filemanager.activities.SimpleActivity
import com.simplemobiletools.filemanager.adapters.ItemsAdapter
import com.simplemobiletools.filemanager.dialogs.CreateNewItemDialog
import com.simplemobiletools.filemanager.extensions.config
import com.simplemobiletools.filemanager.extensions.isPathOnRoot
import com.simplemobiletools.filemanager.helpers.RootHelpers
import com.stericson.RootTools.RootTools
import kotlinx.android.synthetic.main.items_fragment.*
@ -161,7 +162,7 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener {
private fun getItems(path: String, callback: (items: ArrayList<FileDirItem>) -> Unit) {
Thread({
if (!context.config.enableRootAccess || !isPathOnRoot(path)) {
if (!context.config.enableRootAccess || !context.isPathOnRoot(path)) {
getRegularItemsOf(path, callback)
} else {
getRootItemsOf(path, callback)
@ -169,9 +170,6 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener {
}).start()
}
private fun isPathOnRoot(path: String) =
!(path.startsWith(context.config.internalStoragePath) || (context.hasExternalSDCard() && path.startsWith(context.config.sdCardPath)))
private fun getRegularItemsOf(path: String, callback: (items: ArrayList<FileDirItem>) -> Unit) {
val items = ArrayList<FileDirItem>()
val files = File(path).listFiles()
@ -265,7 +263,7 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener {
override fun deleteFiles(files: ArrayList<File>) {
val hasFolder = files.any { it.isDirectory }
if (isPathOnRoot(files.firstOrNull()?.absolutePath ?: context.config.internalStoragePath)) {
if (context.isPathOnRoot(files.firstOrNull()?.absolutePath ?: context.config.internalStoragePath)) {
files.forEach {
RootTools.deleteFileOrDirectory(it.path, false)
}