[Android] Add delete option in Album view

Geoffrey Métais git at videolan.org
Thu Jan 7 15:05:56 CET 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan  7 15:03:11 2016 +0100| [8862ec72733402e6a8ac8d8a19385dccbda434f9] | committer: Geoffrey Métais

Add delete option in Album view

> https://code.videolan.org/videolan/vlc-android/commit/8862ec72733402e6a8ac8d8a19385dccbda434f9
---

 vlc-android/res/menu/audio_list_browser.xml        | 12 +++---
 .../org/videolan/vlc/gui/audio/AlbumAdapter.java   | 46 +++++++++++++++++-----
 .../videolan/vlc/gui/audio/AudioAlbumFragment.java | 45 ++++++++++++++++++++-
 3 files changed, 85 insertions(+), 18 deletions(-)

diff --git a/vlc-android/res/menu/audio_list_browser.xml b/vlc-android/res/menu/audio_list_browser.xml
index f838a5a..219715d 100644
--- a/vlc-android/res/menu/audio_list_browser.xml
+++ b/vlc-android/res/menu/audio_list_browser.xml
@@ -8,17 +8,17 @@
         android:id="@+id/audio_list_browser_append"
         android:title="@string/append" />
     <group android:id="@+id/songs_view_only">
-	    <item
-	        android:id="@+id/audio_list_browser_play_all"
-	        android:title="@string/play_all" />
+        <item
+            android:id="@+id/audio_list_browser_play_all"
+            android:title="@string/play_all" />
     </group>
     <item
         android:id="@+id/audio_list_browser_delete"
         android:title="@string/delete"/>
     <group android:id="@+id/phone_only" >
-	    <item
-	        android:id="@+id/audio_list_browser_set_song"
-	        android:title="@string/set_song"/>
+        <item
+            android:id="@+id/audio_list_browser_set_song"
+            android:title="@string/set_song"/>
     </group>
 
 </menu>
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AlbumAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/AlbumAdapter.java
index ad277d9..96f0d55 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AlbumAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AlbumAdapter.java
@@ -26,32 +26,32 @@ package org.videolan.vlc.gui.audio;
 import android.content.Context;
 import android.database.DataSetObserver;
 import android.databinding.DataBindingUtil;
+import android.support.annotation.MainThread;
 import android.support.annotation.Nullable;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.ArrayAdapter;
+import android.widget.BaseAdapter;
 
-import org.videolan.vlc.media.MediaWrapper;
 import org.videolan.vlc.R;
 import org.videolan.vlc.databinding.AudioBrowserItemBinding;
 import org.videolan.vlc.gui.helpers.MediaComparators;
 import org.videolan.vlc.interfaces.IAudioClickHandler;
+import org.videolan.vlc.media.MediaWrapper;
 
 import java.util.ArrayList;
 import java.util.Collections;
 
-public class AlbumAdapter extends ArrayAdapter<MediaWrapper> implements IAudioClickHandler{
+public class AlbumAdapter extends BaseAdapter implements IAudioClickHandler{
 
+    private Context mContext;
     private ArrayList<MediaWrapper> mMediaList;
 
     private ContextPopupMenuListener mContextPopupMenuListener;
 
     public AlbumAdapter(Context context, ArrayList<MediaWrapper> tracks) {
-        super(context, 0);
-        if (tracks != null)
-            Collections.sort(tracks, MediaComparators.byTrackNumber);
-        mMediaList = tracks;
+        mContext = context;
+        addAll(tracks);
     }
 
     @Override
@@ -60,7 +60,7 @@ public class AlbumAdapter extends ArrayAdapter<MediaWrapper> implements IAudioCl
         View v = convertView;
         MediaWrapper mw = mMediaList.get(position);
         if (v == null) {
-            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             holder = new ViewHolder();
             holder.binding = DataBindingUtil.inflate(inflater, R.layout.audio_browser_item, parent, false);
             v = holder.binding.getRoot();
@@ -78,11 +78,31 @@ public class AlbumAdapter extends ArrayAdapter<MediaWrapper> implements IAudioCl
         return v;
     }
 
+    @MainThread
+    public void addMedia(int position, MediaWrapper media) {
+        mMediaList.add(position, media);
+        notifyDataSetChanged();
+    }
+
+    @MainThread void removeMedia(int position) {
+        mMediaList.remove(position);
+        notifyDataSetChanged();
+    }
+
     @Override
     public int getCount() {
         return mMediaList == null ? 0 : mMediaList.size();
     }
 
+    public MediaWrapper getItem(int position) {
+        return mMediaList.get(position);
+    }
+
+    @Override
+    public long getItemId(int position) {
+        return 0;
+    }
+
     @Nullable
     public String getLocation(int position) {
         if (position >= 0 && position < mMediaList.size())
@@ -92,7 +112,9 @@ public class AlbumAdapter extends ArrayAdapter<MediaWrapper> implements IAudioCl
     }
 
     public void addAll(ArrayList<MediaWrapper> tracks){
-        mMediaList = tracks;
+        if (tracks != null)
+            Collections.sort(tracks, MediaComparators.byTrackNumber);
+        mMediaList = new ArrayList<>(tracks);
         notifyDataSetChanged();
     }
 
@@ -105,7 +127,11 @@ public class AlbumAdapter extends ArrayAdapter<MediaWrapper> implements IAudioCl
     @Override
     public void onMoreClick(View v) {
         if (mContextPopupMenuListener != null)
-                mContextPopupMenuListener.onPopupMenu(v, ((Integer)v.getTag()).intValue());
+                mContextPopupMenuListener.onPopupMenu(v, ((Integer) v.getTag()).intValue());
+    }
+
+    public void clear() {
+        mMediaList.clear();
     }
 
     static class ViewHolder {
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumFragment.java
index a4391d1..3f5542e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumFragment.java
@@ -27,6 +27,8 @@ import android.annotation.TargetApi;
 import android.graphics.Bitmap;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
 import android.support.annotation.Nullable;
 import android.support.v7.widget.PopupMenu;
 import android.util.Log;
@@ -44,8 +46,12 @@ import android.widget.ListView;
 
 import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.vlc.R;
+import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.gui.PlaybackServiceFragment;
 import org.videolan.vlc.gui.helpers.AudioUtil;
+import org.videolan.vlc.gui.helpers.UiTools;
+import org.videolan.vlc.media.MediaDatabase;
+import org.videolan.vlc.media.MediaLibrary;
 import org.videolan.vlc.media.MediaWrapper;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.FileUtils;
@@ -59,7 +65,7 @@ public class AudioAlbumFragment extends PlaybackServiceFragment implements Adapt
     private AlbumAdapter mAdapter;
     private ArrayList<MediaWrapper> mMediaList;
     private String mTitle;
-
+    Handler mHandler = new Handler(Looper.getMainLooper());
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -186,7 +192,7 @@ public class AudioAlbumFragment extends PlaybackServiceFragment implements Adapt
         return super.onContextItemSelected(menu);
     }
 
-    private boolean handleContextItemSelected(MenuItem item, int position) {
+    private boolean handleContextItemSelected(MenuItem item, final int position) {
         int id = item.getItemId();
 
         if (mMediaList.size() <= position) {
@@ -200,11 +206,46 @@ public class AudioAlbumFragment extends PlaybackServiceFragment implements Adapt
         } else if (id == R.id.audio_list_browser_append) {
             mService.append(mMediaList.get(position));
             return true;
+        } else if (id == R.id.audio_list_browser_delete) {
+            final MediaWrapper media = mAdapter.getItem(position);
+            mAdapter.removeMedia(position);
+            UiTools.snackerWithCancel(getView(), getString(R.string.file_deleted), new Runnable() {
+                @Override
+                public void run() {
+                    deleteMedia(media);
+                }
+            }, new Runnable() {
+                @Override
+                public void run() {
+                    mAdapter.addMedia(position, media);
+                }
+            });
+            return true;
         }
 
         return super.onContextItemSelected(item);
     }
 
+    private void deleteMedia(final MediaWrapper mw) {
+        VLCApplication.runBackground(new Runnable() {
+            @Override
+            public void run() {
+                mMediaList.remove(mw);
+                final String path = mw.getUri().getPath();
+                FileUtils.deleteFile(path);
+                MediaDatabase.getInstance().removeMedia(mw.getUri());
+                MediaLibrary.getInstance().getMediaItems().remove(mw);
+                mHandler.post(new Runnable() {
+                    @Override
+                    public void run() {
+                        if (mService != null)
+                            mService.removeLocation(mw.getLocation());
+                    }
+                });
+            }
+        });
+    }
+
     @Override
     public void onClick(View v) {
         final int id = v.getId();



More information about the Android mailing list