move a couple helper function to extensions

This commit is contained in:
tibbi
2016-11-11 22:40:12 +01:00
parent 0770c95791
commit 1d8c45643f
9 changed files with 59 additions and 100 deletions

View File

@ -13,12 +13,10 @@ import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.filemanager.Constants
import com.simplemobiletools.filemanager.R
import com.simplemobiletools.filemanager.Utils
import com.simplemobiletools.filemanager.dialogs.WritePermissionDialog
import com.simplemobiletools.filemanager.fragments.ItemsFragment
import com.simplemobiletools.filepicker.dialogs.StoragePickerDialog
import com.simplemobiletools.filepicker.extensions.getInternalStoragePath
import com.simplemobiletools.filepicker.extensions.getSDCardPath
import com.simplemobiletools.filepicker.extensions.*
import com.simplemobiletools.filepicker.models.FileDirItem
import com.simplemobiletools.filepicker.views.Breadcrumbs
import kotlinx.android.synthetic.main.activity_main.*
@ -39,7 +37,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
}
private fun tryInitFileManager() {
if (Utils.hasStoragePermission(applicationContext)) {
if (hasStoragePermission(applicationContext)) {
initRootFileManager()
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), STORAGE_PERMISSION)
@ -84,7 +82,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
if (breadcrumbs.childCount <= 1) {
if (!mWasBackJustPressed) {
mWasBackJustPressed = true
Utils.showToast(applicationContext, R.string.press_back_again)
toast(R.string.press_back_again)
Handler().postDelayed({ mWasBackJustPressed = false }, BACK_PRESS_TIMEOUT.toLong())
} else {
finish()
@ -103,7 +101,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initRootFileManager()
} else {
Utils.showToast(applicationContext, R.string.no_permissions)
toast(R.string.no_permissions)
finish()
}
}
@ -134,7 +132,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br
}
fun checkStupidAndroidFiveSDCardWritePermission(pickedPath: String): Boolean {
return if (Utils.needsStupidWritePermissions(applicationContext, pickedPath) && mConfig.treeUri.isEmpty()) {
return if (applicationContext.needsStupidWritePermissions(pickedPath) && mConfig.treeUri.isEmpty()) {
WritePermissionDialog(this, object : WritePermissionDialog.OnWritePermissionListener {
override fun onConfirmed() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)

View File

@ -8,14 +8,10 @@ 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.activities.MainActivity
import com.simplemobiletools.filemanager.asynctasks.CopyTask
import com.simplemobiletools.filemanager.extensions.isPathOnSD
import com.simplemobiletools.filemanager.extensions.rescanItem
import com.simplemobiletools.filemanager.extensions.toast
import com.simplemobiletools.filepicker.dialogs.FilePickerDialog
import com.simplemobiletools.filepicker.extensions.humanizePath
import com.simplemobiletools.filepicker.extensions.*
import kotlinx.android.synthetic.main.copy_item.view.*
import java.io.File
@ -28,8 +24,8 @@ class CopyDialog(val activity: Activity, val files: List<File>, val copyListener
var destinationPath = ""
view.source.text = "${context.humanizePath(sourcePath)}/"
val config = Config.newInstance(context)
view.destination.setOnClickListener {
val config = Config.newInstance(context)
FilePickerDialog(activity, destinationPath, false, config.showHidden, object : FilePickerDialog.OnFilePickerListener {
override fun onFail(error: FilePickerDialog.FilePickerResult) {
}
@ -74,7 +70,7 @@ class CopyDialog(val activity: Activity, val files: List<File>, val copyListener
}
}
if (Utils.needsStupidWritePermissions(context, destinationPath) && Config.newInstance(context).treeUri.isEmpty()) {
if (context.needsStupidWritePermissions(destinationPath) && config.treeUri.isEmpty()) {
WritePermissionDialog(activity, object : WritePermissionDialog.OnWritePermissionListener {
override fun onConfirmed() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
@ -96,8 +92,8 @@ class CopyDialog(val activity: Activity, val files: List<File>, val copyListener
CopyTask(copyListener, context, true).execute(pair)
dismiss()
} else {
for (f in files) {
val destination = File(destinationDir, f.name)
for (file in files) {
val destination = File(destinationDir, file.name)
context.rescanItem(destination)
}

View File

@ -5,10 +5,13 @@ import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.simplemobiletools.filemanager.Config
import com.simplemobiletools.filemanager.R
import com.simplemobiletools.filemanager.Utils
import com.simplemobiletools.filemanager.extensions.toast
import com.simplemobiletools.filemanager.extensions.isValidFilename
import com.simplemobiletools.filemanager.extensions.value
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.toast
import kotlinx.android.synthetic.main.create_new.view.*
import java.io.File
import java.io.IOException
@ -29,7 +32,7 @@ class CreateNewItemDialog(val context: Context, val path: String, val listener:
val name = view.item_name.value
if (name.isEmpty()) {
context.toast(R.string.empty_name)
} else if (Utils.isNameValid(name)) {
} else if (name.isValidFilename()) {
val file = File(path, name)
if (file.exists()) {
context.toast(R.string.name_taken)
@ -53,8 +56,8 @@ class CreateNewItemDialog(val context: Context, val path: String, val listener:
}
private fun createDirectory(file: File, alertDialog: AlertDialog): Boolean {
return if (Utils.needsStupidWritePermissions(context, path)) {
val documentFile = Utils.getFileDocument(context, file.absolutePath)
return if (context.needsStupidWritePermissions(path)) {
val documentFile = context.getFileDocument(file.absolutePath, Config.newInstance(context).treeUri)
documentFile.createDirectory(file.name)
success(alertDialog)
true
@ -71,8 +74,8 @@ class CreateNewItemDialog(val context: Context, val path: String, val listener:
private fun createFile(file: File, alertDialog: AlertDialog): Boolean {
try {
if (Utils.needsStupidWritePermissions(context, path)) {
val documentFile = Utils.getFileDocument(context, file.absolutePath)
if (context.needsStupidWritePermissions(path)) {
val documentFile = context.getFileDocument(file.absolutePath, Config.newInstance(context).treeUri)
documentFile.createFile("", file.name)
success(alertDialog)
return true

View File

@ -1,15 +1,17 @@
package com.simplemobiletools.filemanager.dialogs
import android.content.Context
import android.media.MediaScannerConnection
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.isValidFilename
import com.simplemobiletools.filemanager.extensions.value
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.rescanItem
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.filepicker.models.FileDirItem
import kotlinx.android.synthetic.main.rename_item.view.*
import java.io.File
@ -30,7 +32,7 @@ class RenameItemDialog(val context: Context, val path: String, val item: FileDir
show()
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
val newName = view.item_name.value
if (Utils.isNameValid(newName)) {
if (newName.isValidFilename()) {
val currFile = File(path, item.name)
val newFile = File(path, newName)
@ -39,8 +41,8 @@ class RenameItemDialog(val context: Context, val path: String, val item: FileDir
return@setOnClickListener
}
if (Utils.needsStupidWritePermissions(context, path)) {
val document = Utils.getFileDocument(context, currFile.absolutePath)
if (context.needsStupidWritePermissions(path)) {
val document = context.getFileDocument(currFile.absolutePath, Config.newInstance(context).treeUri)
if (document.canWrite())
document.renameTo(newName)
sendSuccess(currFile, newFile)
@ -61,8 +63,8 @@ class RenameItemDialog(val context: Context, val path: String, val item: FileDir
}
private fun sendSuccess(currFile: File, newFile: File) {
context.rescanItem(currFile)
context.rescanItem(newFile)
MediaScannerConnection.scanFile(context, arrayOf(currFile.absolutePath, newFile.absolutePath), null, null)
listener.onSuccess()
}

View File

@ -1,21 +0,0 @@
package com.simplemobiletools.filemanager.extensions
import android.content.Context
import android.media.MediaScannerConnection
import android.widget.Toast
import com.simplemobiletools.filepicker.extensions.getSDCardPath
import java.io.File
fun Context.toast(id: Int) = Toast.makeText(this, resources.getString(id), Toast.LENGTH_SHORT).show()
fun Context.rescanItem(item: File) {
if (item.isDirectory) {
for (child in item.listFiles()) {
rescanItem(child)
}
}
MediaScannerConnection.scanFile(this, arrayOf(item.absolutePath), null, null)
}
fun Context.isPathOnSD(path: String) = path.startsWith(getSDCardPath())

View File

@ -0,0 +1,9 @@
package com.simplemobiletools.filemanager.extensions
import java.util.regex.Pattern
fun String.isValidFilename(): Boolean {
val pattern = Pattern.compile("^[-_.A-Za-z0-9()#& ]+$")
val matcher = pattern.matcher(this)
return matcher.matches()
}