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

View file

@ -255,7 +255,7 @@ public class MainActivity extends SimpleActivity
private void deleteItem(File file) { private void deleteItem(File file) {
if (Utils.Companion.needsStupidWritePermissions(this, file.getAbsolutePath())) { if (Utils.Companion.needsStupidWritePermissions(this, file.getAbsolutePath())) {
if (!Utils.Companion.isShowingWritePermissions(this, file)) { 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(); document.delete();
} }
} else { } else {

View file

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

View file

@ -285,7 +285,7 @@ public class ViewPagerActivity extends SimpleActivity
final File file = new File(mToBeDeleted); final File file = new File(mToBeDeleted);
if (Utils.Companion.needsStupidWritePermissions(this, mToBeDeleted)) { if (Utils.Companion.needsStupidWritePermissions(this, mToBeDeleted)) {
if (!Utils.Companion.isShowingWritePermissions(this, file)) { 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()) { if (document.canWrite()) {
mWasFileDeleted = document.delete(); mWasFileDeleted = document.delete();
} }

View file

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

View file

@ -8,8 +8,8 @@ import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.toast
import com.theartofdev.edmodo.cropper.CropImageView import com.theartofdev.edmodo.cropper.CropImageView
import kotlinx.android.synthetic.main.activity_edit.* import kotlinx.android.synthetic.main.activity_edit.*
@ -83,7 +83,7 @@ class SetWallpaperActivity : SimpleActivity(), CropImageView.OnCropImageComplete
finish() finish()
}).start() }).start()
} else { } 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.os.AsyncTask
import android.support.v4.util.Pair import android.support.v4.util.Pair
import android.util.Log import android.util.Log
import com.simplemobiletools.gallery.Utils import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.gallery.extensions.scanFile import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.rescanItem
import com.simplemobiletools.gallery.Config
import java.io.* import java.io.*
import java.lang.ref.WeakReference 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 val TAG = CopyTask::class.java.simpleName
private var mListener: WeakReference<CopyDoneListener>? = null private var mListener: WeakReference<CopyDoneListener>? = null
private var destinationDir: File? = null private var destinationDir: File? = null
private var mConfig: Config
init { init {
mListener = WeakReference(listener) mListener = WeakReference(listener)
mConfig = Config.newInstance(context)
} }
override fun doInBackground(vararg params: Pair<List<File>, File>): Boolean? { 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) { private fun copyDirectory(source: File, destination: File) {
if (!destination.exists()) { if (!destination.exists()) {
if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) { if (context.needsStupidWritePermissions(destination.absolutePath)) {
val document = Utils.getFileDocument(context, destination.absolutePath) val document = context.getFileDocument(destination.absolutePath, mConfig.treeUri)
document.createDirectory(destination.name) document.createDirectory(destination.name)
} else if (!destination.mkdirs()) { } else if (!destination.mkdirs()) {
throw IOException("Could not create dir " + destination.absolutePath) 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() val children = source.list()
for (child in children) { for (child in children) {
val newFile = File(source, child) val newFile = File(source, child)
if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) { if (context.needsStupidWritePermissions(destination.absolutePath)) {
if (newFile.isDirectory) { if (newFile.isDirectory) {
copyDirectory(newFile, File(destination, child)) copyDirectory(newFile, File(destination, child))
} else { } else {
var document = Utils.getFileDocument(context, destination.absolutePath) var document = context.getFileDocument(destination.absolutePath, mConfig.treeUri)
document = document.createFile("", child) document = document.createFile("", child)
val inputStream = FileInputStream(newFile) val inputStream = FileInputStream(newFile)
@ -80,16 +84,15 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn
val inputStream = FileInputStream(source) val inputStream = FileInputStream(source)
val out: OutputStream? val out: OutputStream?
if (Utils.needsStupidWritePermissions(context, destination.absolutePath)) { if (context.needsStupidWritePermissions(destination.absolutePath)) {
var document = Utils.getFileDocument(context, destination.absolutePath) var document = context.getFileDocument(destination.absolutePath, mConfig.treeUri)
document = document.createFile("", destination.name) document = document.createFile("", destination.name)
out = context.contentResolver.openOutputStream(document.uri) out = context.contentResolver.openOutputStream(document.uri)
} else { } else {
out = FileOutputStream(destination) out = FileOutputStream(destination)
} }
val paths = arrayOf(destination.absolutePath) context.rescanItem(destination)
context.scanFile(paths)
copyStream(inputStream, out) copyStream(inputStream, out)
} }

View file

@ -6,10 +6,10 @@ 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.filepicker.extensions.humanizePath import com.simplemobiletools.filepicker.extensions.humanizePath
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.asynctasks.CopyTask import com.simplemobiletools.gallery.asynctasks.CopyTask
import com.simplemobiletools.gallery.extensions.toast
import kotlinx.android.synthetic.main.copy_item.view.* import kotlinx.android.synthetic.main.copy_item.view.*
import java.io.File import java.io.File

View file

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

View file

@ -5,10 +5,13 @@ import android.media.MediaScannerConnection
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.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.humanizePath 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.R
import com.simplemobiletools.gallery.Utils import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.extensions.toast
import com.simplemobiletools.gallery.extensions.value import com.simplemobiletools.gallery.extensions.value
import kotlinx.android.synthetic.main.rename_file.view.* import kotlinx.android.synthetic.main.rename_file.view.*
import java.io.File 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") val newFile = File(file.parent, "$fileName.$extension")
if (Utils.needsStupidWritePermissions(context, file.absolutePath)) { if (context.needsStupidWritePermissions(file.absolutePath)) {
if (Utils.isShowingWritePermissions(activity, file)) if (Utils.isShowingWritePermissions(activity, file))
return@setOnClickListener return@setOnClickListener
val document = Utils.Companion.getFileDocument(context, file.absolutePath) val document = context.getFileDocument(file.absolutePath, Config.newInstance(context).treeUri)
if (document.canWrite()) if (document.canWrite())
document.renameTo(newFile.name) document.renameTo(newFile.name)
sendSuccess(file, newFile) sendSuccess(file, newFile)

View file

@ -5,9 +5,9 @@ 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.filepicker.extensions.getFilenameFromPath import com.simplemobiletools.filepicker.extensions.getFilenameFromPath
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.isNameValid import com.simplemobiletools.gallery.extensions.isNameValid
import com.simplemobiletools.gallery.extensions.toast
import com.simplemobiletools.gallery.extensions.value import com.simplemobiletools.gallery.extensions.value
import kotlinx.android.synthetic.main.rename_file.view.* 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.content.Context
import android.media.MediaScannerConnection 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) 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 import android.widget.TextView
val TextView.value: String get() = this.text.toString().trim() val TextView.value: String get() = text.toString().trim()