[Android] Rework the context menu handling in the audio fragments

Ludovic Fauvet git at videolan.org
Sat Jun 23 18:26:37 CEST 2012


android | branch: master | Ludovic Fauvet <etix at videolan.org> | Sat Jun 23 18:20:42 2012 +0200| [b67c57d47a2871d9ee220d357dec26473c4d48d8] | committer: Ludovic Fauvet

Rework the context menu handling in the audio fragments

> http://git.videolan.org/gitweb.cgi/android.git/?a=commit;h=b67c57d47a2871d9ee220d357dec26473c4d48d8
---

 vlc-android/res/menu/audio_list_browser.xml        |   20 ++++++++
 .../vlc/gui/audio/AudioBrowserFragment.java        |   48 ++++++++------------
 .../videolan/vlc/gui/audio/AudioListFragment.java  |   34 ++++++--------
 3 files changed, 52 insertions(+), 50 deletions(-)

diff --git a/vlc-android/res/menu/audio_list_browser.xml b/vlc-android/res/menu/audio_list_browser.xml
new file mode 100644
index 0000000..77dc7a8
--- /dev/null
+++ b/vlc-android/res/menu/audio_list_browser.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+    <item
+        android:id="@+id/audio_list_browser_play"
+        android:title="@string/play" />
+    <item
+        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_append_all"
+	        android:title="@string/append_all" />
+	    <item
+	        android:id="@+id/audio_list_browser_delete"
+	        android:title="@string/delete" />
+    </group>
+</menu>
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
index b0e6354..1664d47 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
@@ -47,11 +47,10 @@ import android.support.v4.app.FragmentTransaction;
 import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
 import android.view.LayoutInflater;
-import android.view.Menu;
+import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.MotionEvent;
 import android.view.View;
-import android.view.View.OnCreateContextMenuListener;
 import android.view.View.OnTouchListener;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
@@ -92,12 +91,6 @@ public class AudioBrowserFragment extends SherlockFragment implements ISortable
     public final static int MODE_SONG = 2;
     public final static int MODE_GENRE = 3;
 
-    public final static int MENU_PLAY = Menu.FIRST;
-    public final static int MENU_APPEND = Menu.FIRST + 1;
-    public final static int MENU_PLAY_ALL = Menu.FIRST + 2;
-    public final static int MENU_APPEND_ALL = Menu.FIRST + 3;
-    public final static int MENU_DELETE = Menu.FIRST + 4;
-
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -149,10 +142,10 @@ public class AudioBrowserFragment extends SherlockFragment implements ISortable
         albumList.setOnChildClickListener(playlistChildListener);
         genreList.setOnChildClickListener(playlistChildListener);
 
-        songsList.setOnCreateContextMenuListener(contextMenuListener);
-        artistList.setOnCreateContextMenuListener(contextMenuListener);
-        albumList.setOnCreateContextMenuListener(contextMenuListener);
-        genreList.setOnCreateContextMenuListener(contextMenuListener);
+        registerForContextMenu(songsList);
+        registerForContextMenu(artistList);
+        registerForContextMenu(albumList);
+        registerForContextMenu(genreList);
 
         return v;
     }
@@ -225,20 +218,6 @@ public class AudioBrowserFragment extends SherlockFragment implements ISortable
         }
     };
 
-    OnCreateContextMenuListener contextMenuListener = new OnCreateContextMenuListener()
-    {
-        @Override
-        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
-            menu.add(Menu.NONE, MENU_PLAY, Menu.NONE, R.string.play);
-            menu.add(Menu.NONE, MENU_APPEND, Menu.NONE, R.string.append);
-            if (v.getId() == R.id.songs_list) {
-                menu.add(Menu.NONE, MENU_PLAY_ALL, Menu.NONE, R.string.play_all);
-                menu.add(Menu.NONE, MENU_APPEND_ALL, Menu.NONE, R.string.append_all);
-                menu.add(Menu.NONE, MENU_DELETE, Menu.NONE, R.string.delete);
-            }
-        }
-    };
-
     public void deleteMedia( final List<String> addressMedia, final Media aMedia ) {
         AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
         .setTitle(R.string.confirm_delete)
@@ -266,6 +245,15 @@ public class AudioBrowserFragment extends SherlockFragment implements ISortable
     }
 
     @Override
+    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
+        MenuInflater inflater = getActivity().getMenuInflater();
+        inflater.inflate(R.menu.audio_list_browser, menu);
+
+        if (v.getId() != R.id.songs_list)
+            menu.setGroupEnabled(R.id.songs_view_only, false);
+    }
+
+    @Override
     public boolean onContextItemSelected(MenuItem item) {
         int startPosition;
         int groupPosition;
@@ -273,8 +261,10 @@ public class AudioBrowserFragment extends SherlockFragment implements ISortable
         List<String> medias;
         int id = item.getItemId();
 
-        boolean useAllItems = id == MENU_PLAY_ALL || id == MENU_APPEND_ALL;
-        boolean append = id == MENU_APPEND || id == MENU_APPEND_ALL;
+        boolean useAllItems = (id == R.id.audio_list_browser_play_all ||
+                               id == R.id.audio_list_browser_append_all);
+        boolean append = (id == R.id.audio_list_browser_append ||
+                          id == R.id.audio_list_browser_append_all);
 
         ContextMenuInfo menuInfo = item.getMenuInfo();
         if (ExpandableListContextMenuInfo.class.isInstance(menuInfo)) {
@@ -290,7 +280,7 @@ public class AudioBrowserFragment extends SherlockFragment implements ISortable
             childPosition = 0;
         }
 
-        if (id == MENU_DELETE) {
+        if (id == R.id.audio_list_browser_delete) {
             deleteMedia(mSongsAdapter.getLocation(groupPosition), mSongsAdapter.getItem(groupPosition));
             return true;
         }
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioListFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioListFragment.java
index 59a5d6b..a198e70 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioListFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioListFragment.java
@@ -41,11 +41,10 @@ import android.os.Message;
 import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
 import android.view.LayoutInflater;
-import android.view.Menu;
+import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
-import android.view.View.OnCreateContextMenuListener;
 import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.ListView;
 import android.widget.TextView;
@@ -87,17 +86,14 @@ public class AudioListFragment extends SherlockListFragment {
 
     public void onViewCreated(View view, Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
-        getListView().setOnCreateContextMenuListener(contextMenuListener);
         updateList();
     }
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
-
         View v = inflater.inflate(R.layout.audio_list, container, false);
-
         mTitle = (TextView) v.findViewById(R.id.title);
-
+        registerForContextMenu(getListView());
         return v;
     }
 
@@ -123,18 +119,6 @@ public class AudioListFragment extends SherlockListFragment {
         super.onListItemClick(l, v, position, id);
     }
 
-    OnCreateContextMenuListener contextMenuListener = new OnCreateContextMenuListener()
-    {
-        @Override
-        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
-            menu.add(Menu.NONE, AudioBrowserFragment.MENU_PLAY, Menu.NONE, R.string.play);
-            menu.add(Menu.NONE, AudioBrowserFragment.MENU_APPEND, Menu.NONE, R.string.append);
-            menu.add(Menu.NONE, AudioBrowserFragment.MENU_PLAY_ALL, Menu.NONE, R.string.play_all);
-            menu.add(Menu.NONE, AudioBrowserFragment.MENU_APPEND_ALL, Menu.NONE, R.string.append_all);
-            menu.add(Menu.NONE, AudioBrowserFragment.MENU_DELETE, Menu.NONE, R.string.delete);
-        }
-    };
-
     public void deleteMedia( final List<String> addressMedia, final Media aMedia ) {
         AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
         .setTitle(R.string.confirm_delete)
@@ -161,16 +145,24 @@ public class AudioListFragment extends SherlockListFragment {
     }
 
     @Override
+    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
+        MenuInflater inflater = getActivity().getMenuInflater();
+        inflater.inflate(R.menu.audio_list_browser, menu);
+    }
+
+    @Override
     public boolean onContextItemSelected(MenuItem item) {
         AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
         int startPosition;
         List<String> medias;
         int id = item.getItemId();
 
-        boolean useAllItems = id == AudioBrowserFragment.MENU_PLAY_ALL || id == AudioBrowserFragment.MENU_APPEND_ALL;
-        boolean append = id == AudioBrowserFragment.MENU_APPEND || id == AudioBrowserFragment.MENU_APPEND_ALL;
+        boolean useAllItems = (id == R.id.audio_list_browser_play_all ||
+                               id == R.id.audio_list_browser_append_all);
+        boolean append = (id == R.id.audio_list_browser_append ||
+                          id == R.id.audio_list_browser_append_all);
 
-        if (id == AudioBrowserFragment.MENU_DELETE) {
+        if (id == R.id.audio_list_browser_delete) {
             deleteMedia(mSongsAdapter.getLocation(menuInfo.position),
                         mSongsAdapter.getItem(menuInfo.position));
             return true;



More information about the Android mailing list