Commit 841e5da5 authored by Geoffrey Métais's avatar Geoffrey Métais

PlaylistActivity: Add permission check on deletion

Fix #774
parent 904b705f
......@@ -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
......
......@@ -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
}
......
......@@ -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()));
......
......@@ -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;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment