From 84f81ed3e2b79ab3117bcf1ee067a21a05853e69 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 6 Nov 2016 19:03:59 +0100 Subject: [PATCH] check kitkat sd permission before deleting from viewpager --- .../gallery/activities/ViewPagerActivity.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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 c14121b0f..5a4b7a6d9 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -11,6 +11,8 @@ 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; import android.view.Menu; @@ -20,12 +22,14 @@ 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; @@ -273,9 +277,30 @@ public class ViewPagerActivity extends SimpleActivity mIsUndoShown = false; mBeingDeleted = ""; + boolean mWasFileDeleted = false; final File file = new File(mToBeDeleted); - if (file.delete()) { + 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 { + final DocumentFile document = Utils.Companion.getFileDocument(this, mToBeDeleted); + if (document.canWrite()) { + mWasFileDeleted = document.delete(); + } + } + } else { + mWasFileDeleted = file.delete(); + } + + if (mWasFileDeleted) { mBeingDeleted = mToBeDeleted; final String[] deletedPath = new String[]{mToBeDeleted}; MediaScannerConnection.scanFile(getApplicationContext(), deletedPath, null, new MediaScannerConnection.OnScanCompletedListener() { @@ -285,6 +310,7 @@ public class ViewPagerActivity extends SimpleActivity } }); } + mToBeDeleted = ""; mUndoBtn.setVisibility(View.GONE); }