From 6c9c072dc94c675ffd22d8802192611aa886d1fe Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 6 Nov 2016 20:36:55 +0100 Subject: [PATCH] create a helper function for displaying stupid kitkat permissions dialog --- .../com/simplemobiletools/gallery/Utils.kt | 15 ++++++++ .../gallery/activities/MediaActivity.java | 38 +++++++++++++------ .../gallery/activities/ViewPagerActivity.java | 15 +------- .../gallery/activities/EditActivity.kt | 13 +------ .../gallery/dialogs/RenameItemDialog.kt | 13 +------ 5 files changed, 46 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/Utils.kt b/app/src/main/java/com/simplemobiletools/gallery/Utils.kt index 1bc42c651..1ae51225d 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Utils.kt +++ b/app/src/main/java/com/simplemobiletools/gallery/Utils.kt @@ -20,6 +20,7 @@ import android.view.* import android.webkit.MimeTypeMap import android.widget.Toast import com.simplemobiletools.filepicker.extensions.getSDCardPath +import com.simplemobiletools.gallery.dialogs.WritePermissionDialog import com.simplemobiletools.gallery.models.Medium import java.io.File @@ -167,5 +168,19 @@ class Utils { val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION context.contentResolver.takePersistableUriPermission(treeUri, takeFlags) } + + fun isShowingWritePermissions(activity: Activity, file: File): Boolean { + return if (needsStupidWritePermissions(activity, file.absolutePath) && !file.canWrite() && 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 + } + } } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java index ad93bac40..a8442a056 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java @@ -13,6 +13,7 @@ import android.os.Parcelable; import android.provider.MediaStore; import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.Snackbar; +import android.support.v4.provider.DocumentFile; import android.support.v4.widget.SwipeRefreshLayout; import android.util.Log; import android.util.SparseBooleanArray; @@ -335,23 +336,38 @@ public class MediaActivity extends SimpleActivity } mIsSnackbarShown = false; + boolean wereFilesDeleted = false; for (String delPath : mToBeDeleted) { final File file = new File(delPath); - if (file.exists()) - file.delete(); - } + if (file.exists()) { + if (Utils.Companion.needsStupidWritePermissions(this, delPath)) { + if (Utils.Companion.isShowingWritePermissions(this, file)) + return; - final String[] deletedPaths = mToBeDeleted.toArray(new String[mToBeDeleted.size()]); - MediaScannerConnection.scanFile(getApplicationContext(), deletedPaths, null, new MediaScannerConnection.OnScanCompletedListener() { - @Override - public void onScanCompleted(String path, Uri uri) { - if (mMedia != null && mMedia.isEmpty()) { - finish(); + final DocumentFile document = Utils.Companion.getFileDocument(this, delPath); + if (document.delete()) { + wereFilesDeleted = true; + } + } else { + if (file.delete()) + wereFilesDeleted = true; } } - }); - mToBeDeleted.clear(); + } + + if (wereFilesDeleted) { + final String[] deletedPaths = mToBeDeleted.toArray(new String[mToBeDeleted.size()]); + MediaScannerConnection.scanFile(getApplicationContext(), deletedPaths, null, new MediaScannerConnection.OnScanCompletedListener() { + @Override + public void onScanCompleted(String path, Uri uri) { + if (mMedia != null && mMedia.isEmpty()) { + finish(); + } + } + }); + mToBeDeleted.clear(); + } } private View.OnClickListener undoDeletion = new View.OnClickListener() { diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java index b4e655821..71ed7b3b6 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -6,10 +6,8 @@ import android.content.res.Resources; import android.database.Cursor; import android.media.MediaScannerConnection; import android.net.Uri; -import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; -import android.support.annotation.RequiresApi; import android.support.v4.provider.DocumentFile; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBar; @@ -20,14 +18,12 @@ import android.view.View; import android.widget.RelativeLayout; import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog; -import com.simplemobiletools.gallery.Config; import com.simplemobiletools.gallery.Constants; import com.simplemobiletools.gallery.MyViewPager; import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.Utils; import com.simplemobiletools.gallery.adapters.MyPagerAdapter; import com.simplemobiletools.gallery.dialogs.RenameItemDialog; -import com.simplemobiletools.gallery.dialogs.WritePermissionDialog; import com.simplemobiletools.gallery.fragments.ViewPagerFragment; import com.simplemobiletools.gallery.models.Medium; @@ -268,16 +264,7 @@ public class ViewPagerActivity extends SimpleActivity final File file = new File(mToBeDeleted); if (Utils.Companion.needsStupidWritePermissions(this, mToBeDeleted)) { - if (Config.newInstance(this).getTreeUri().isEmpty()) { - new WritePermissionDialog(this, new WritePermissionDialog.OnWritePermissionListener() { - @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) - @Override - public void onConfirmed() { - final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); - startActivityForResult(intent, Constants.OPEN_DOCUMENT_TREE); - } - }); - } else { + if (!Utils.Companion.isShowingWritePermissions(this, file)) { final DocumentFile document = Utils.Companion.getFileDocument(this, mToBeDeleted); if (document.canWrite()) { mWasFileDeleted = document.delete(); diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt index cf3c0f1ba..59adc6755 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.gallery.activities import android.app.Activity -import android.content.Intent import android.graphics.Bitmap import android.media.MediaScannerConnection import android.net.Uri @@ -9,11 +8,8 @@ import android.os.Bundle import android.util.Log import android.view.Menu import android.view.MenuItem -import com.simplemobiletools.gallery.Config -import com.simplemobiletools.gallery.Constants import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.Utils -import com.simplemobiletools.gallery.dialogs.WritePermissionDialog import com.simplemobiletools.gallery.extensions.toast import com.theartofdev.edmodo.cropper.CropImageView import kotlinx.android.synthetic.main.activity_edit.* @@ -101,15 +97,8 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener var out: OutputStream? = null try { if (Utils.needsStupidWritePermissions(this, path)) { - if (!file.canWrite() && Config.newInstance(this).treeUri.isEmpty()) { - WritePermissionDialog(this, object : WritePermissionDialog.OnWritePermissionListener { - override fun onConfirmed() { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) - startActivityForResult(intent, Constants.OPEN_DOCUMENT_TREE) - } - }) + if (Utils.isShowingWritePermissions(this, file)) return - } val document = Utils.getFileDocument(this, path) out = contentResolver.openOutputStream(document.uri) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameItemDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameItemDialog.kt index 40c6a885e..046724642 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameItemDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameItemDialog.kt @@ -1,13 +1,10 @@ package com.simplemobiletools.gallery.dialogs import android.app.Activity -import android.content.Intent import android.media.MediaScannerConnection import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.WindowManager -import com.simplemobiletools.gallery.Config -import com.simplemobiletools.gallery.Constants import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.Utils import com.simplemobiletools.gallery.extensions.toast @@ -53,15 +50,9 @@ class RenameItemDialog(val activity: Activity, val file: File, val listener: OnR val newFile = File(file.parent, "$fileName.$extension") if (Utils.needsStupidWritePermissions(context, file.absolutePath)) { - if (!file.canWrite() && Config.newInstance(context).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) - } - }) + if (Utils.isShowingWritePermissions(activity, file)) return@setOnClickListener - } + val document = Utils.Companion.getFileDocument(context, file.absolutePath) if (document.canWrite()) document.renameTo(newFile.name)