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.webkit.MimeTypeMap
import android.widget.Toast import android.widget.Toast
import com.simplemobiletools.filepicker.extensions.getSDCardPath import com.simplemobiletools.filepicker.extensions.getSDCardPath
import com.simplemobiletools.gallery.dialogs.WritePermissionDialog
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import java.io.File 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 val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
context.contentResolver.takePersistableUriPermission(treeUri, takeFlags) 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.provider.MediaStore;
import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.provider.DocumentFile;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log; import android.util.Log;
import android.util.SparseBooleanArray; import android.util.SparseBooleanArray;
@ -335,23 +336,38 @@ public class MediaActivity extends SimpleActivity
} }
mIsSnackbarShown = false; mIsSnackbarShown = false;
boolean wereFilesDeleted = false;
for (String delPath : mToBeDeleted) { for (String delPath : mToBeDeleted) {
final File file = new File(delPath); final File file = new File(delPath);
if (file.exists()) if (file.exists()) {
file.delete(); if (Utils.Companion.needsStupidWritePermissions(this, delPath)) {
} if (Utils.Companion.isShowingWritePermissions(this, file))
return;
final String[] deletedPaths = mToBeDeleted.toArray(new String[mToBeDeleted.size()]); final DocumentFile document = Utils.Companion.getFileDocument(this, delPath);
MediaScannerConnection.scanFile(getApplicationContext(), deletedPaths, null, new MediaScannerConnection.OnScanCompletedListener() { if (document.delete()) {
@Override wereFilesDeleted = true;
public void onScanCompleted(String path, Uri uri) { }
if (mMedia != null && mMedia.isEmpty()) { } else {
finish(); 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() { private View.OnClickListener undoDeletion = new View.OnClickListener() {

View file

@ -6,10 +6,8 @@ import android.content.res.Resources;
import android.database.Cursor; import android.database.Cursor;
import android.media.MediaScannerConnection; import android.media.MediaScannerConnection;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v4.provider.DocumentFile; import android.support.v4.provider.DocumentFile;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
@ -20,14 +18,12 @@ import android.view.View;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog; import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog;
import com.simplemobiletools.gallery.Config;
import com.simplemobiletools.gallery.Constants; import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.MyViewPager; import com.simplemobiletools.gallery.MyViewPager;
import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils; import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.MyPagerAdapter; import com.simplemobiletools.gallery.adapters.MyPagerAdapter;
import com.simplemobiletools.gallery.dialogs.RenameItemDialog; import com.simplemobiletools.gallery.dialogs.RenameItemDialog;
import com.simplemobiletools.gallery.dialogs.WritePermissionDialog;
import com.simplemobiletools.gallery.fragments.ViewPagerFragment; import com.simplemobiletools.gallery.fragments.ViewPagerFragment;
import com.simplemobiletools.gallery.models.Medium; import com.simplemobiletools.gallery.models.Medium;
@ -268,16 +264,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 (Config.newInstance(this).getTreeUri().isEmpty()) { if (!Utils.Companion.isShowingWritePermissions(this, file)) {
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 {
final DocumentFile document = Utils.Companion.getFileDocument(this, mToBeDeleted); final DocumentFile document = Utils.Companion.getFileDocument(this, mToBeDeleted);
if (document.canWrite()) { if (document.canWrite()) {
mWasFileDeleted = document.delete(); mWasFileDeleted = document.delete();

View file

@ -1,7 +1,6 @@
package com.simplemobiletools.gallery.activities package com.simplemobiletools.gallery.activities
import android.app.Activity import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
import android.media.MediaScannerConnection import android.media.MediaScannerConnection
import android.net.Uri import android.net.Uri
@ -9,11 +8,8 @@ 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.gallery.Config
import com.simplemobiletools.gallery.Constants
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.WritePermissionDialog
import com.simplemobiletools.gallery.extensions.toast 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.*
@ -101,15 +97,8 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
var out: OutputStream? = null var out: OutputStream? = null
try { try {
if (Utils.needsStupidWritePermissions(this, path)) { if (Utils.needsStupidWritePermissions(this, path)) {
if (!file.canWrite() && Config.newInstance(this).treeUri.isEmpty()) { if (Utils.isShowingWritePermissions(this, file))
WritePermissionDialog(this, object : WritePermissionDialog.OnWritePermissionListener {
override fun onConfirmed() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
startActivityForResult(intent, Constants.OPEN_DOCUMENT_TREE)
}
})
return return
}
val document = Utils.getFileDocument(this, path) val document = Utils.getFileDocument(this, path)
out = contentResolver.openOutputStream(document.uri) out = contentResolver.openOutputStream(document.uri)

View file

@ -1,13 +1,10 @@
package com.simplemobiletools.gallery.dialogs package com.simplemobiletools.gallery.dialogs
import android.app.Activity import android.app.Activity
import android.content.Intent
import android.media.MediaScannerConnection 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.gallery.Config
import com.simplemobiletools.gallery.Constants
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.toast
@ -53,15 +50,9 @@ class RenameItemDialog(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 (Utils.needsStupidWritePermissions(context, file.absolutePath)) {
if (!file.canWrite() && Config.newInstance(context).treeUri.isEmpty()) { if (Utils.isShowingWritePermissions(activity, file))
WritePermissionDialog(activity, object : WritePermissionDialog.OnWritePermissionListener {
override fun onConfirmed() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
activity.startActivityForResult(intent, Constants.OPEN_DOCUMENT_TREE)
}
})
return@setOnClickListener return@setOnClickListener
}
val document = Utils.Companion.getFileDocument(context, file.absolutePath) val document = Utils.Companion.getFileDocument(context, file.absolutePath)
if (document.canWrite()) if (document.canWrite())
document.renameTo(newFile.name) document.renameTo(newFile.name)