create a helper function for displaying stupid kitkat permissions dialog

This commit is contained in:
tibbi 2016-11-06 20:36:55 +01:00
parent c71f94d9ec
commit 6c9c072dc9
5 changed files with 46 additions and 48 deletions

View file

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

View file

@ -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,13 +336,27 @@ 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 DocumentFile document = Utils.Companion.getFileDocument(this, delPath);
if (document.delete()) {
wereFilesDeleted = true;
}
} else {
if (file.delete())
wereFilesDeleted = true;
}
}
}
if (wereFilesDeleted) {
final String[] deletedPaths = mToBeDeleted.toArray(new String[mToBeDeleted.size()]);
MediaScannerConnection.scanFile(getApplicationContext(), deletedPaths, null, new MediaScannerConnection.OnScanCompletedListener() {
@Override
@ -353,6 +368,7 @@ public class MediaActivity extends SimpleActivity
});
mToBeDeleted.clear();
}
}
private View.OnClickListener undoDeletion = new View.OnClickListener() {
@Override

View file

@ -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();

View file

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

View file

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