make use of some of the new extension functions

This commit is contained in:
tibbi 2016-11-12 12:04:38 +01:00
parent 42be5de0cb
commit 9e5936b8a0
12 changed files with 31 additions and 76 deletions

View File

@ -18,7 +18,6 @@ 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.models.Medium
import java.io.File
@ -150,22 +149,6 @@ class Utils {
context.contentResolver.takePersistableUriPermission(treeUri, takeFlags)
}
fun isShowingWritePermissions(activity: Activity, file: File): Boolean {
return if ((needsStupidWritePermissions(activity, file.absolutePath) && Config.newInstance(activity).treeUri.isEmpty())) {
WritePermissionDialog(activity, object : WritePermissionDialog.OnWritePermissionListener {
override fun onConfirmed() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
activity.startActivityForResult(intent, Constants.OPEN_DOCUMENT_TREE)
}
})
true
} else {
false
}
}
fun scanFiles(context: Context, paths: Array<String>) {
context.scanFile(paths)
}
fun scanFiles(context: Context, paths: Array<String>) = context.scanFile(paths)
}
}

View File

@ -199,7 +199,7 @@ public class MainActivity extends SimpleActivity
}
for (String path : mToBeDeleted) {
if (Utils.Companion.isShowingWritePermissions(this, new File(path))) {
if (isShowingPermDialog(new File(path))) {
return;
}
}
@ -254,7 +254,7 @@ public class MainActivity extends SimpleActivity
private void deleteItem(File file) {
if (Utils.Companion.needsStupidWritePermissions(this, file.getAbsolutePath())) {
if (!Utils.Companion.isShowingWritePermissions(this, file)) {
if (!isShowingPermDialog(file)) {
final DocumentFile document = Utils.Companion.getFileDocument(this, file.getAbsolutePath(), mConfig.getTreeUri());
document.delete();
}

View File

@ -306,7 +306,7 @@ public class MediaActivity extends SimpleActivity
}
private void prepareForDeleting() {
if (Utils.Companion.isShowingWritePermissions(this, new File(mPath)))
if (isShowingPermDialog(new File(mPath)))
return;
Utils.Companion.showToast(this, R.string.deleting);
@ -358,7 +358,7 @@ public class MediaActivity extends SimpleActivity
final File file = new File(delPath);
if (file.exists()) {
if (Utils.Companion.needsStupidWritePermissions(this, delPath)) {
if (Utils.Companion.isShowingWritePermissions(this, file))
if (isShowingPermDialog(file))
return;
final DocumentFile document = Utils.Companion.getFileDocument(this, delPath, mConfig.getTreeUri());

View File

@ -260,7 +260,7 @@ public class ViewPagerActivity extends SimpleActivity
}
private void notifyDeletion() {
if (Utils.Companion.isShowingWritePermissions(this, new File(mPath)))
if (isShowingPermDialog(new File(mPath)))
return;
mToBeDeleted = getCurrentFile().getAbsolutePath();
@ -284,7 +284,7 @@ public class ViewPagerActivity extends SimpleActivity
final File file = new File(mToBeDeleted);
if (Utils.Companion.needsStupidWritePermissions(this, mToBeDeleted)) {
if (!Utils.Companion.isShowingWritePermissions(this, file)) {
if (!isShowingPermDialog(file)) {
final DocumentFile document = Utils.Companion.getFileDocument(this, mToBeDeleted, mConfig.getTreeUri());
if (document.canWrite()) {
mWasFileDeleted = document.delete();

View File

@ -98,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}")
}
}
@ -108,7 +108,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
var out: OutputStream? = null
try {
if (needsStupidWritePermissions(path)) {
if (Utils.isShowingWritePermissions(this, file))
if (isShowingPermDialog(file))
return
var document = getFileDocument(path, mConfig.treeUri)

View File

@ -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

@ -5,10 +5,12 @@ import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import com.simplemobiletools.filepicker.extensions.isShowingWritePermissions
import com.simplemobiletools.gallery.Config
import com.simplemobiletools.gallery.Constants
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import java.io.File
open class SimpleActivity : AppCompatActivity() {
lateinit var mConfig: Config
@ -39,4 +41,6 @@ open class SimpleActivity : AppCompatActivity() {
Utils.saveTreeUri(this, resultData)
}
}
fun isShowingPermDialog(file: File) = isShowingWritePermissions(file, mConfig.treeUri, Constants.OPEN_DOCUMENT_TREE)
}

View File

@ -6,7 +6,7 @@ import android.support.v4.util.Pair
import android.util.Log
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.rescanItem
import com.simplemobiletools.filepicker.extensions.rescanFile
import com.simplemobiletools.gallery.Config
import java.io.*
import java.lang.ref.WeakReference
@ -92,7 +92,7 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn
out = FileOutputStream(destination)
}
context.rescanItem(destination)
context.rescanFile(destination)
copyStream(inputStream, out)
}

View File

@ -1,25 +1,26 @@
package com.simplemobiletools.gallery.dialogs
import android.app.Activity
import android.support.v4.util.Pair
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.Config
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.asynctasks.CopyTask
import kotlinx.android.synthetic.main.copy_item.view.*
import java.io.File
class CopyDialog(val activity: Activity, val files: List<File>, val copyListener: CopyTask.CopyDoneListener, val listener: OnCopyListener) {
class CopyDialog(val activity: SimpleActivity, val files: List<File>, val copyListener: CopyTask.CopyDoneListener, val listener: OnCopyListener) {
init {
val context = activity
val view = LayoutInflater.from(context).inflate(R.layout.copy_item, null)
val sourcePath = files[0].parent.trimEnd('/')
var destinationPath = ""
val config = Config.newInstance(context)
view.source.text = context.humanizePath(sourcePath)
@ -65,15 +66,15 @@ class CopyDialog(val activity: Activity, val files: List<File>, val copyListener
}
}
if (Utils.isShowingWritePermissions(context, destinationDir)) {
if (activity.isShowingPermDialog(destinationDir)) {
return@setOnClickListener
}
//if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_copy) {
context.toast(R.string.copying)
val pair = Pair<List<File>, File>(files, destinationDir)
CopyTask(copyListener, context).execute(pair)
dismiss()
context.toast(R.string.copying)
val pair = Pair<List<File>, File>(files, destinationDir)
CopyTask(copyListener, context).execute(pair)
dismiss()
/*} else {
if (Utils.isPathOnSD(context, sourcePath) && Utils.isPathOnSD(context, destinationPath)) {
val paths = ArrayList<String>()

View File

@ -1,6 +1,5 @@
package com.simplemobiletools.gallery.dialogs
import android.app.Activity
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.WindowManager
@ -10,13 +9,13 @@ 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.activities.SimpleActivity
import com.simplemobiletools.gallery.extensions.value
import kotlinx.android.synthetic.main.rename_directory.view.*
import java.io.File
import java.util.*
class RenameDirectoryDialog(val activity: Activity, val dir: File, val listener: OnRenameDirListener) {
class RenameDirectoryDialog(val activity: SimpleActivity, val dir: File, val listener: OnRenameDirListener) {
val context = activity
init {
@ -46,7 +45,7 @@ class RenameDirectoryDialog(val activity: Activity, val dir: File, val listener:
val newDir = File(dir.parent, newDirName)
if (context.needsStupidWritePermissions(dir.absolutePath)) {
if (Utils.isShowingWritePermissions(activity, dir))
if (activity.isShowingPermDialog(dir))
return@setOnClickListener
val document = context.getFileDocument(dir.absolutePath, Config.newInstance(context).treeUri)

View File

@ -1,6 +1,5 @@
package com.simplemobiletools.gallery.dialogs
import android.app.Activity
import android.media.MediaScannerConnection
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
@ -11,12 +10,12 @@ 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.activities.SimpleActivity
import com.simplemobiletools.gallery.extensions.value
import kotlinx.android.synthetic.main.rename_file.view.*
import java.io.File
class RenameFileDialog(val activity: Activity, val file: File, val listener: OnRenameFileListener) {
class RenameFileDialog(val activity: SimpleActivity, val file: File, val listener: OnRenameFileListener) {
init {
val context = activity
@ -54,7 +53,7 @@ class RenameFileDialog(val activity: Activity, val file: File, val listener: OnR
val newFile = File(file.parent, "$fileName.$extension")
if (context.needsStupidWritePermissions(file.absolutePath)) {
if (Utils.isShowingWritePermissions(activity, file))
if (activity.isShowingPermDialog(file))
return@setOnClickListener
val document = context.getFileDocument(file.absolutePath, Config.newInstance(context).treeUri)

View File

@ -1,31 +0,0 @@
package com.simplemobiletools.gallery.dialogs
import android.content.Context
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import com.simplemobiletools.gallery.R
class WritePermissionDialog(val context: Context, val listener: OnWritePermissionListener) {
var dialog: AlertDialog? = null
init {
val view = LayoutInflater.from(context).inflate(R.layout.dialog_write_permission, null)
dialog = AlertDialog.Builder(context)
.setTitle(context.resources.getString(R.string.confirm_storage_access_title))
.setView(view)
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
.create()
dialog?.show()
}
private fun dialogConfirmed() {
dialog?.dismiss()
listener.onConfirmed()
}
interface OnWritePermissionListener {
fun onConfirmed()
}
}