Commit 35d7aa49 authored by Alexandre Perraud's avatar Alexandre Perraud

Directory View : Add delete folder option

parent 81f629b5
......@@ -3,6 +3,9 @@
<item
android:id="@+id/directory_view_play_folder"
android:title="@string/play" />
<item
android:id="@+id/directory_view_delete"
android:title="@string/delete" />
<!--<item-->
<!--android:id="@+id/directory_view_hide_media"-->
<!--android:title="@string/directory_hide_medialib" />-->
......
......@@ -37,6 +37,7 @@
<string name="set_song">Set as ringtone</string>
<string name="info">Information</string>
<string name="confirm_delete">Delete the file \'%1$s\'?</string>
<string name="confirm_delete_folder">Delete the folder \'%1$s\' and all its contents?</string>
<string name="confirm_delete_playlist">Delete playlist \'%1$s\'?</string>
<string name="ringtone_set">The file \'%1$s\' was set as the ringtone.</string>
<string name="ringtone_error">An error occurred while setting the ringtone.</string>
......
......@@ -25,10 +25,14 @@ import android.content.DialogInterface;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import org.videolan.vlc.MediaWrapper;
import org.videolan.vlc.R;
import org.videolan.vlc.util.Strings;
import org.videolan.vlc.util.Util;
import org.videolan.vlc.util.VLCRunnable;
import java.io.File;
public class CommonDialogs {
public final static String TAG = "VLC/CommonDialogs";
......@@ -41,23 +45,36 @@ public class CommonDialogs {
public static AlertDialog deleteMedia(final Context context,
final String addressMedia,
final VLCRunnable runnable) {
final String name = Uri.decode(addressMedia.substring(addressMedia.lastIndexOf('/')+1));
return deleteMedia(context, addressMedia, name, runnable);
return deleteMedia(MediaWrapper.TYPE_ALL, context, addressMedia, runnable);
}
public static AlertDialog deleteMedia(final Context context,
public static AlertDialog deleteMedia(final int type,
final Context context,
final String addressMedia,
final String name,
final VLCRunnable runnable) {
final String name = Strings.getName(Uri.decode(addressMedia));
return deleteMedia(type, context, addressMedia, name, runnable);
}
return confirmDialog(
context,
context.getResources().getString(R.string.confirm_delete,
name),
public static AlertDialog deleteMedia(final int type,
final Context context,
final String addressMedia,
final String name,
final VLCRunnable runnable) {
String confirmMessage = "";
switch (type) {
case MediaWrapper.TYPE_DIR :
confirmMessage = context.getResources().getString(R.string.confirm_delete_folder, name);
break;
default :
confirmMessage = context.getResources().getString(R.string.confirm_delete, name);
break;
}
return confirmDialog( context, confirmMessage,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
Util.deleteFile(context, addressMedia);
Util.recursiveDelete(context, new File(Uri.decode(Strings.removeFileProtocole(addressMedia))));
if (runnable != null)
runnable.run();
}
......
......@@ -450,6 +450,7 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
});
} else {
mAlertDialog = CommonDialogs.deleteMedia(
MediaWrapper.TYPE_ALL,
getActivity(),
adapter.getItem(position).mMediaList.get(0).getLocation(),
adapter.getItem(position).mTitle,
......
......@@ -102,7 +102,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
final MediaWrapper media = (MediaWrapper) getItem(position);
boolean hasContextMenu = (media.getType() == MediaWrapper.TYPE_AUDIO ||
media.getType() == MediaWrapper.TYPE_VIDEO ||
(media.getType() == MediaWrapper.TYPE_DIR && !TextUtils.equals(media.getDescription(), mEmptyDirectoryString)));
media.getType() == MediaWrapper.TYPE_DIR );
vh.checkBox.setVisibility(View.GONE);
vh.title.setText(media.getTitle());
if (!TextUtils.isEmpty(media.getDescription())) {
......
......@@ -66,8 +66,6 @@ import org.videolan.vlc.util.WeakHandler;
import org.videolan.vlc.widget.ContextMenuRecyclerView;
import org.videolan.vlc.widget.SwipeRefreshLayout;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
......@@ -339,13 +337,13 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
protected void setContextMenu(MenuInflater inflater, Menu menu, int position) {
MediaWrapper mw = (MediaWrapper) mAdapter.getItem(position);
boolean canWrite = Util.canWrite(mw.getLocation());
if (mw.getType() == MediaWrapper.TYPE_AUDIO || mw.getType() == MediaWrapper.TYPE_VIDEO) {
boolean canWrite = Util.canWrite(mw.getLocation());
inflater.inflate(R.menu.directory_view_file, menu);
menu.findItem(R.id.directory_view_delete).setVisible(canWrite);
} else if (mw.getType() == MediaWrapper.TYPE_DIR) {
boolean isEmpty = mMediaLists.get(position) == null || mMediaLists.get(position).isEmpty();
if (/*canWrite || */!isEmpty) {
if (canWrite || !isEmpty) {
inflater.inflate(R.menu.directory_view_dir, menu);
// if (canWrite) {
// boolean nomedia = new File(mw.getLocation() + "/.nomedia").exists();
......@@ -356,8 +354,8 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
// menu.findItem(R.id.directory_view_show_media).setVisible(false);
// }
menu.findItem(R.id.directory_view_play_folder).setVisible(!isEmpty);
menu.findItem(R.id.directory_view_delete).setVisible(canWrite);
}
}
}
......@@ -402,7 +400,8 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
AudioServiceController.getInstance().append(mw);
return true;
case R.id.directory_view_delete:
AlertDialog alertDialog = CommonDialogs.deleteMedia(getActivity(), mw.getLocation(),
AlertDialog alertDialog = CommonDialogs.deleteMedia(
mw.getType(), getActivity(), mw.getLocation(),
new VLCRunnable() {
@Override
public void run(Object o) {
......
......@@ -117,7 +117,7 @@ public class MediaInfoFragment extends ListFragment {
new Thread(new Runnable() {
@Override
public void run() {
boolean deleted = Util.deleteFile(getActivity(), mItem.getLocation());
boolean deleted = Util.deleteFile(mItem.getLocation());
if (deleted) {
mHandler.obtainMessage(EXIT).sendToTarget();
}
......
......@@ -58,6 +58,7 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class Util {
public final static String TAG = "VLC/Util";
private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
public static final String ACTION_SCAN_START = "org.videolan.vlc.gui.ScanStart";
public static final String ACTION_SCAN_STOP = "org.videolan.vlc.gui.ScanStop";
......@@ -315,12 +316,12 @@ public class Util {
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static boolean deleteFile (Context context, String path){
public static boolean deleteFile (String path){
boolean deleted = false;
path = Uri.decode(Strings.removeFileProtocole(path));
//Delete from Android Medialib, for consistency with device MTP storing and other apps listing content:// media
if (LibVlcUtil.isHoneycombOrLater()){
ContentResolver cr = context.getContentResolver();
ContentResolver cr = VLCApplication.getAppContext().getContentResolver();
String[] selectionArgs = { path };
deleted = cr.delete(MediaStore.Files.getContentUri("external"),
MediaStore.Files.FileColumns.DATA + "=?", selectionArgs) > 0;
......@@ -331,6 +332,16 @@ public class Util {
return deleted;
}
public static boolean recursiveDelete(Context context, File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
for (File child : fileOrDirectory.listFiles())
recursiveDelete(context, child);
return fileOrDirectory.delete();
} else {
return deleteFile (fileOrDirectory.getPath());
}
}
public static boolean close(Closeable closeable) {
if (closeable != null) {
try {
......
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