rely on some extension functions from the filepicker library

This commit is contained in:
tibbi 2016-11-11 23:40:33 +01:00
parent cb734d7c10
commit e3cc1b91cc
13 changed files with 46 additions and 60 deletions

View file

@ -1,27 +1,25 @@
package com.simplemobiletools.gallery
import android.Manifest
import android.annotation.TargetApi
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Resources
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.support.v4.content.ContextCompat
import android.support.v4.provider.DocumentFile
import android.support.v7.app.ActionBar
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.*
import android.webkit.MimeTypeMap
import com.simplemobiletools.filepicker.extensions.getSDCardPath
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.hasStoragePermission
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.dialogs.WritePermissionDialog
import com.simplemobiletools.gallery.extensions.scanFile
import com.simplemobiletools.gallery.extensions.toast
import com.simplemobiletools.gallery.models.Medium
import java.io.File
@ -84,9 +82,7 @@ class Utils {
}
}
fun hasStoragePermission(cxt: Context): Boolean {
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
}
fun hasStoragePermission(context: Context) = context.hasStoragePermission()
fun getMimeType(url: String): String {
val extension = MimeTypeMap.getFileExtensionFromUrl(url)
@ -141,25 +137,9 @@ class Utils {
}
}
fun needsStupidWritePermissions(context: Context, path: String) = isPathOnSD(context, path) && isKitkat() && !context.getSDCardPath().isEmpty()
fun needsStupidWritePermissions(context: Context, path: String) = context.needsStupidWritePermissions(path)
fun isPathOnSD(context: Context, path: String): Boolean {
return path.startsWith(context.getSDCardPath())
}
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) {
val currDocument = document.findFile(part)
if (currDocument != null)
document = currDocument
}
return document
}
fun getFileDocument(context: Context, path: String, treeUri: String) = context.getFileDocument(path, treeUri)
@TargetApi(Build.VERSION_CODES.KITKAT)
fun saveTreeUri(context: Context, resultData: Intent) {

View file

@ -255,7 +255,7 @@ public class MainActivity extends SimpleActivity
private void deleteItem(File file) {
if (Utils.Companion.needsStupidWritePermissions(this, file.getAbsolutePath())) {
if (!Utils.Companion.isShowingWritePermissions(this, file)) {
final DocumentFile document = Utils.Companion.getFileDocument(this, file.getAbsolutePath());
final DocumentFile document = Utils.Companion.getFileDocument(this, file.getAbsolutePath(), mConfig.getTreeUri());
document.delete();
}
} else {

View file

@ -361,7 +361,7 @@ public class MediaActivity extends SimpleActivity
if (Utils.Companion.isShowingWritePermissions(this, file))
return;
final DocumentFile document = Utils.Companion.getFileDocument(this, delPath);
final DocumentFile document = Utils.Companion.getFileDocument(this, delPath, mConfig.getTreeUri());
if (document.delete()) {
wereFilesDeleted = true;
}

View file

@ -285,7 +285,7 @@ public class ViewPagerActivity extends SimpleActivity
final File file = new File(mToBeDeleted);
if (Utils.Companion.needsStupidWritePermissions(this, mToBeDeleted)) {
if (!Utils.Companion.isShowingWritePermissions(this, file)) {
final DocumentFile document = Utils.Companion.getFileDocument(this, mToBeDeleted);
final DocumentFile document = Utils.Companion.getFileDocument(this, mToBeDeleted, mConfig.getTreeUri());
if (document.canWrite()) {
mWasFileDeleted = document.delete();
}

View file

@ -8,10 +8,12 @@ import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.dialogs.SaveAsDialog
import com.simplemobiletools.gallery.extensions.toast
import com.theartofdev.edmodo.cropper.CropImageView
import kotlinx.android.synthetic.main.activity_edit.*
import java.io.File
@ -96,7 +98,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
finish()
}
} else {
toast("${getString(R.string.image_editing_failed)}: ${result.error.message}")
//toast("${getString(R.string.image_editing_failed)}: ${result.error.message}")
}
}
@ -105,11 +107,11 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
var out: OutputStream? = null
try {
if (Utils.needsStupidWritePermissions(this, path)) {
if (needsStupidWritePermissions(path)) {
if (Utils.isShowingWritePermissions(this, file))
return
var document = Utils.getFileDocument(this, path)
var document = getFileDocument(path, mConfig.treeUri)
if (!file.exists()) {
document = document.createFile("", file.name)
}

View file

@ -8,8 +8,8 @@ import android.net.Uri
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.toast
import com.theartofdev.edmodo.cropper.CropImageView
import kotlinx.android.synthetic.main.activity_edit.*
@ -83,7 +83,7 @@ class SetWallpaperActivity : SimpleActivity(), CropImageView.OnCropImageComplete
finish()
}).start()
} else {
toast("${getString(R.string.image_editing_failed)}: ${result.error.message}")
//toast("${getString(R.string.image_editing_failed)}: ${result.error.message}")
}
}

View file

@ -4,8 +4,10 @@ import android.content.Context
import android.os.AsyncTask
import android.support.v4.util.Pair
import android.util.Log
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.extensions.scanFile
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.rescanItem
import com.simplemobiletools.gallery.Config
import java.io.*
import java.lang.ref.WeakReference
@ -13,9 +15,11 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn
private val TAG = CopyTask::class.java.simpleName
private var mListener: WeakReference<CopyDoneListener>? = null
private var destinationDir: File? = null
private var mConfig: Config
init {
mListener = WeakReference(listener)
mConfig = Config.newInstance(context)
}
override fun doInBackground(vararg params: Pair<List<File>, File>): Boolean? {
@ -44,8 +48,8 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn
private fun copyDirectory(source: File, destination: File) {
if (!destination.exists()) {
if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) {
val document = Utils.getFileDocument(context, destination.absolutePath)
if (context.needsStupidWritePermissions(destination.absolutePath)) {
val document = context.getFileDocument(destination.absolutePath, mConfig.treeUri)
document.createDirectory(destination.name)
} else if (!destination.mkdirs()) {
throw IOException("Could not create dir " + destination.absolutePath)
@ -55,11 +59,11 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn
val children = source.list()
for (child in children) {
val newFile = File(source, child)
if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) {
if (context.needsStupidWritePermissions(destination.absolutePath)) {
if (newFile.isDirectory) {
copyDirectory(newFile, File(destination, child))
} else {
var document = Utils.getFileDocument(context, destination.absolutePath)
var document = context.getFileDocument(destination.absolutePath, mConfig.treeUri)
document = document.createFile("", child)
val inputStream = FileInputStream(newFile)
@ -80,16 +84,15 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn
val inputStream = FileInputStream(source)
val out: OutputStream?
if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) {
var document = Utils.getFileDocument(context, destination.absolutePath)
if (context.needsStupidWritePermissions(destination.absolutePath)) {
var document = context.getFileDocument(destination.absolutePath, mConfig.treeUri)
document = document.createFile("", destination.name)
out = context.contentResolver.openOutputStream(document.uri)
} else {
out = FileOutputStream(destination)
}
val paths = arrayOf(destination.absolutePath)
context.scanFile(paths)
context.rescanItem(destination)
copyStream(inputStream, out)
}

View file

@ -6,10 +6,10 @@ import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.WindowManager
import com.simplemobiletools.filepicker.extensions.humanizePath
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.asynctasks.CopyTask
import com.simplemobiletools.gallery.extensions.toast
import kotlinx.android.synthetic.main.copy_item.view.*
import java.io.File

View file

@ -4,10 +4,13 @@ import android.app.Activity
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.WindowManager
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.humanizePath
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.Config
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.extensions.toast
import com.simplemobiletools.gallery.extensions.value
import kotlinx.android.synthetic.main.rename_directory.view.*
import java.io.File
@ -42,11 +45,11 @@ class RenameDirectoryDialog(val activity: Activity, val dir: File, val listener:
updatedFiles.add(dir.absolutePath)
val newDir = File(dir.parent, newDirName)
if (Utils.needsStupidWritePermissions(context, dir.absolutePath)) {
if (context.needsStupidWritePermissions(dir.absolutePath)) {
if (Utils.isShowingWritePermissions(activity, dir))
return@setOnClickListener
val document = Utils.Companion.getFileDocument(context, dir.absolutePath)
val document = context.getFileDocument(dir.absolutePath, Config.newInstance(context).treeUri)
if (document.canWrite())
document.renameTo(newDirName)
sendSuccess(updatedFiles, newDir)

View file

@ -5,10 +5,13 @@ import android.media.MediaScannerConnection
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.WindowManager
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.humanizePath
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.Config
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.extensions.toast
import com.simplemobiletools.gallery.extensions.value
import kotlinx.android.synthetic.main.rename_file.view.*
import java.io.File
@ -50,11 +53,11 @@ class RenameFileDialog(val activity: Activity, val file: File, val listener: OnR
val newFile = File(file.parent, "$fileName.$extension")
if (Utils.needsStupidWritePermissions(context, file.absolutePath)) {
if (context.needsStupidWritePermissions(file.absolutePath)) {
if (Utils.isShowingWritePermissions(activity, file))
return@setOnClickListener
val document = Utils.Companion.getFileDocument(context, file.absolutePath)
val document = context.getFileDocument(file.absolutePath, Config.newInstance(context).treeUri)
if (document.canWrite())
document.renameTo(newFile.name)
sendSuccess(file, newFile)

View file

@ -5,9 +5,9 @@ import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.WindowManager
import com.simplemobiletools.filepicker.extensions.getFilenameFromPath
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.isNameValid
import com.simplemobiletools.gallery.extensions.toast
import com.simplemobiletools.gallery.extensions.value
import kotlinx.android.synthetic.main.rename_file.view.*

View file

@ -2,10 +2,5 @@ package com.simplemobiletools.gallery.extensions
import android.content.Context
import android.media.MediaScannerConnection
import android.widget.Toast
fun Context.toast(msg: String, duration: Int = Toast.LENGTH_SHORT) = Toast.makeText(this, msg, duration).show()
fun Context.toast(msgId: Int, duration: Int = Toast.LENGTH_SHORT) = Toast.makeText(this, resources.getString(msgId), duration).show()
fun Context.scanFile(paths: Array<String>) = MediaScannerConnection.scanFile(this, paths, null, null)

View file

@ -2,4 +2,4 @@ package com.simplemobiletools.gallery.extensions
import android.widget.TextView
val TextView.value: String get() = this.text.toString().trim()
val TextView.value: String get() = text.toString().trim()