[Android] PlaylistActivity: Add permission check on deletion

Geoffrey Métais git at videolan.org
Thu Mar 14 16:30:09 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Mar 14 16:26:54 2019 +0100| [841e5da543e877321c24320d203ef259be29e118] | committer: Geoffrey Métais

PlaylistActivity: Add permission check on deletion

Fix #774

> https://code.videolan.org/videolan/vlc-android/commit/841e5da543e877321c24320d203ef259be29e118
---

 .../src/org/videolan/vlc/gui/PlaylistActivity.java | 35 +++++++++++++++++-----
 .../vlc/gui/browser/BaseBrowserFragment.kt         |  2 +-
 .../vlc/gui/browser/MediaBrowserFragment.java      | 23 ++------------
 vlc-android/src/org/videolan/vlc/util/Util.java    | 21 +++++++++++++
 4 files changed, 51 insertions(+), 30 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java b/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java
index 70596cf55..be03e6004 100644
--- a/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java
@@ -60,6 +60,7 @@ import org.videolan.vlc.media.PlaylistManager;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.Constants;
 import org.videolan.vlc.util.FileUtils;
+import org.videolan.vlc.util.Util;
 import org.videolan.vlc.util.WorkersKt;
 import org.videolan.vlc.viewmodels.paged.MLPagedModel;
 import org.videolan.vlc.viewmodels.paged.PagedTracksModel;
@@ -314,14 +315,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
                 showInfoDialog(media);
                 break;
             case Constants.CTX_DELETE:
-                final int resId = mIsPlaylist ? R.string.confirm_remove_from_playlist : R.string.confirm_delete;
-                UiTools.snackerConfirm(mBinding.getRoot(), getString(resId, media.getTitle()), new Runnable() {
-                    @Override
-                    public void run() {
-                        if (mIsPlaylist) ((Playlist) mPlaylist).remove(media.getId());
-                        else deleteMedia(media);
-                    }
-                });
+                removeItem(media);
                 break;
             case Constants.CTX_APPEND:
                 MediaUtils.INSTANCE.appendMedia(this, media.getTracks());
@@ -339,6 +333,31 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
 
     }
 
+    private void removeItem(final MediaWrapper media) {
+        final int resId = mIsPlaylist ? R.string.confirm_remove_from_playlist : R.string.confirm_delete;
+        if (mIsPlaylist) {
+            UiTools.snackerConfirm(mBinding.getRoot(), getString(resId, media.getTitle()), new Runnable() {
+                @Override
+                public void run() {
+                    ((Playlist) mPlaylist).remove(media.getId());
+                }
+            });
+        } else {
+            final Runnable deleteAction = new Runnable() {
+                @Override
+                public void run() {
+                    deleteMedia(media);
+                }
+            };
+            UiTools.snackerConfirm(mBinding.getRoot(), getString(resId, media.getTitle()), new Runnable() {
+                @Override
+                public void run() {
+                    if (Util.checkWritePermission(PlaylistActivity.this, media, deleteAction)) deleteAction.run();
+                }
+            });
+        }
+    }
+
     protected void deleteMedia(final MediaLibraryItem mw) {
         WorkersKt.runIO(new Runnable() {
             @Override
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
index 41403be10..ac214507f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
@@ -285,7 +285,7 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
             viewModel.remove(mw)
         }
         val resId = if (mw.type == MediaWrapper.TYPE_DIR) R.string.confirm_delete_folder else R.string.confirm_delete
-        UiTools.snackerConfirm(view, getString(resId, mw.title)) { if (checkWritePermission(mw, deleteAction)) deleteAction.run() }
+        UiTools.snackerConfirm(view, getString(resId, mw.title)) { if (Util.checkWritePermission(requireActivity(), mw, deleteAction)) deleteAction.run() }
         return true
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.java
index 45b3d8742..e1c902e6f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/MediaBrowserFragment.java
@@ -24,7 +24,6 @@ package org.videolan.vlc.gui.browser;
 
 import android.annotation.TargetApi;
 import android.content.Intent;
-import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
 import android.view.Menu;
@@ -33,7 +32,6 @@ import android.view.View;
 
 import com.google.android.material.floatingactionbutton.FloatingActionButton;
 
-import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.medialibrary.Medialibrary;
 import org.videolan.medialibrary.media.MediaLibraryItem;
 import org.videolan.medialibrary.media.MediaWrapper;
@@ -44,13 +42,12 @@ import org.videolan.vlc.gui.AudioPlayerContainerActivity;
 import org.videolan.vlc.gui.ContentActivity;
 import org.videolan.vlc.gui.InfoActivity;
 import org.videolan.vlc.gui.helpers.UiTools;
-import org.videolan.vlc.gui.helpers.hf.WriteExternalDelegate;
 import org.videolan.vlc.gui.view.SwipeRefreshLayout;
 import org.videolan.vlc.interfaces.Filterable;
 import org.videolan.vlc.media.MediaUtils;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.FileUtils;
-import org.videolan.vlc.util.Permissions;
+import org.videolan.vlc.util.Util;
 import org.videolan.vlc.util.WorkersKt;
 import org.videolan.vlc.viewmodels.SortableModel;
 
@@ -191,7 +188,7 @@ public abstract class MediaBrowserFragment<T extends SortableModel> extends Frag
             UiTools.snackerConfirm(getView(), getString(resid, item.getTitle()), new Runnable() {
                 @Override
                 public void run() {
-                    if (checkWritePermission((MediaWrapper) item, deleteAction)) deleteAction.run();
+                    if (Util.checkWritePermission(requireActivity(), (MediaWrapper) item, deleteAction)) deleteAction.run();
                 }
             });
         } else return false;
@@ -231,22 +228,6 @@ public abstract class MediaBrowserFragment<T extends SortableModel> extends Frag
         });
     }
 
-    protected boolean checkWritePermission(MediaWrapper media, Runnable callback) {
-        final Uri uri = media.getUri();
-        if (!"file".equals(uri.getScheme())) return false;
-        if (uri.getPath().startsWith(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY)) {
-            //Check write permission starting Oreo
-            if (AndroidUtil.isOOrLater && !Permissions.canWriteStorage()) {
-                Permissions.askWriteStoragePermission(getActivity(), false, callback);
-                return false;
-            }
-        } else if (AndroidUtil.isLolliPopOrLater && WriteExternalDelegate.Companion.needsWritePermission(uri)) {
-            WriteExternalDelegate.Companion.askForExtWrite(getActivity(), uri, callback);
-            return false;
-        }
-        return true;
-    }
-
     private void onDeleteFailed(MediaWrapper media) {
         final View v = getView();
         if (v != null && isAdded()) UiTools.snacker(v, getString(R.string.msg_delete_failed, media.getTitle()));
diff --git a/vlc-android/src/org/videolan/vlc/util/Util.java b/vlc-android/src/org/videolan/vlc/util/Util.java
index 63a9090d7..b4dae9026 100644
--- a/vlc-android/src/org/videolan/vlc/util/Util.java
+++ b/vlc-android/src/org/videolan/vlc/util/Util.java
@@ -26,14 +26,18 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
+import android.net.Uri;
 import android.text.TextUtils;
 import android.widget.Toast;
 
 import org.videolan.libvlc.Dialog;
+import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.medialibrary.Tools;
 import org.videolan.medialibrary.media.MediaLibraryItem;
+import org.videolan.medialibrary.media.MediaWrapper;
 import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
+import org.videolan.vlc.gui.helpers.hf.WriteExternalDelegate;
 import org.videolan.vlc.gui.video.VideoPlayerActivity;
 
 import java.io.BufferedReader;
@@ -48,6 +52,7 @@ import java.util.List;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.fragment.app.FragmentActivity;
 
 public class Util {
     public final static String TAG = "VLC/Util";
@@ -209,4 +214,20 @@ public class Util {
             }
         });
     }
+
+    public static boolean checkWritePermission(final FragmentActivity activity, MediaWrapper media, Runnable callback) {
+        final Uri uri = media.getUri();
+        if (!"file".equals(uri.getScheme())) return false;
+        if (uri.getPath().startsWith(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY)) {
+            //Check write permission starting Oreo
+            if (AndroidUtil.isOOrLater && !Permissions.canWriteStorage()) {
+                Permissions.askWriteStoragePermission(activity, false, callback);
+                return false;
+            }
+        } else if (AndroidUtil.isLolliPopOrLater && WriteExternalDelegate.Companion.needsWritePermission(uri)) {
+            WriteExternalDelegate.Companion.askForExtWrite(activity, uri, callback);
+            return false;
+        }
+        return true;
+    }
 }



More information about the Android mailing list