[Android] Audio browser: enable the context menu in the Albums/Songs fragment
Adrien Maglo
git at videolan.org
Thu Jan 16 18:27:42 CET 2014
vlc-ports/android | branch: master | Adrien Maglo <magsoft at videolan.org> | Thu Jan 16 18:27:35 2014 +0100| [007a26f25a3ff49d98b7eadf147a7495aa720525] | committer: Adrien Maglo
Audio browser: enable the context menu in the Albums/Songs fragment
> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=007a26f25a3ff49d98b7eadf147a7495aa720525
---
.../vlc/gui/audio/AudioAlbumsSongsFragment.java | 135 +++++++++++++++++++-
1 file changed, 134 insertions(+), 1 deletion(-)
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
index 11edb55..199d3c0 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
@@ -22,24 +22,38 @@ package org.videolan.vlc.gui.audio;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.List;
+
import org.videolan.libvlc.Media;
import org.videolan.vlc.AudioServiceController;
+import org.videolan.vlc.MediaLibrary;
import org.videolan.vlc.R;
import org.videolan.vlc.Util;
+import org.videolan.vlc.VlcRunnable;
+import org.videolan.vlc.gui.CommonDialogs;
+import android.annotation.TargetApi;
+import android.app.AlertDialog;
import android.content.Context;
+import android.os.Build;
import android.os.Bundle;
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.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
+import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.AdapterView;
+import android.widget.ExpandableListView;
import android.widget.ListView;
+import android.widget.PopupMenu;
import android.widget.TabHost;
+import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
@@ -50,6 +64,7 @@ public class AudioAlbumsSongsFragment extends SherlockFragment {
public final static String TAG = "VLC/AudioAlbumsSongsFragment";
AudioServiceController mAudioController;
+ private MediaLibrary mMediaLibrary;
private AudioBrowserListAdapter mSongsAdapter;
private AudioBrowserListAdapter mAlbumsAdapter;
@@ -78,7 +93,11 @@ public class AudioAlbumsSongsFragment extends SherlockFragment {
mAlbumsAdapter = new AudioBrowserListAdapter(getActivity(), AudioBrowserListAdapter.ITEM_WITH_COVER);
mSongsAdapter = new AudioBrowserListAdapter(getActivity(), AudioBrowserListAdapter.ITEM_WITH_COVER);
+ mAlbumsAdapter.setContextPopupMenuListener(mContextPopupMenuListener);
+ mSongsAdapter.setContextPopupMenuListener(mContextPopupMenuListener);
+
mAudioController = AudioServiceController.getInstance();
+ mMediaLibrary = MediaLibrary.getInstance(getActivity());
}
public void onViewCreated(View view, Bundle savedInstanceState) {
@@ -102,6 +121,9 @@ public class AudioAlbumsSongsFragment extends SherlockFragment {
songsList.setOnItemClickListener(songsListener);
albumsList.setOnItemClickListener(albumsListener);
+ registerForContextMenu(albumsList);
+ registerForContextMenu(songsList);
+
mTabHost.setup();
addNewTab(mTabHost, "albums", "Albums", R.id.albums);
@@ -146,12 +168,96 @@ public class AudioAlbumsSongsFragment extends SherlockFragment {
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.audio_list_browser, menu);
+ setContextMenuItems(menu, v);
+ }
+
+ private void setContextMenuItems(Menu menu, View v) {
+ if (v.getId() != R.id.songs) {
+ menu.setGroupVisible(R.id.songs_view_only, false);
+ menu.setGroupVisible(R.id.phone_only, false);
+ }
if (!Util.isPhone())
menu.setGroupVisible(R.id.phone_only, false);
}
@Override
- public boolean onContextItemSelected(MenuItem item) {
+ public boolean onContextItemSelected(MenuItem menu) {
+ if(!getUserVisibleHint())
+ return super.onContextItemSelected(menu);
+
+ AdapterContextMenuInfo info = (AdapterContextMenuInfo) menu.getMenuInfo();
+ if (info != null && handleContextItemSelected(menu, info.position))
+ return true;
+ return super.onContextItemSelected(menu);
+ }
+
+ private boolean handleContextItemSelected(MenuItem item, int position) {
+ ContextMenuInfo menuInfo = item.getMenuInfo();
+
+ int startPosition;
+ int groupPosition;
+ List<String> medias;
+ int id = item.getItemId();
+
+ 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 (ExpandableListContextMenuInfo.class.isInstance(menuInfo)) {
+ ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
+ groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
+ }
+ else {
+ AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
+ groupPosition = info.position;
+ }
+
+ if (id == R.id.audio_list_browser_delete) {
+ AlertDialog alertDialog = CommonDialogs.deleteMedia(
+ getActivity(),
+ mSongsAdapter.getLocations(groupPosition).get(0),
+ new VlcRunnable(mSongsAdapter.getItem(groupPosition)) {
+ @Override
+ public void run(Object o) {
+ Media aMedia = (Media) o;
+ mMediaLibrary.getMediaItems().remove(aMedia);
+ updateList();
+ }
+ });
+ alertDialog.show();
+ return true;
+ }
+
+ if (id == R.id.audio_list_browser_set_song) {
+ //AudioUtil.setRingtone(mSongsAdapter.getItem(groupPosition),getActivity());
+ return true;
+ }
+
+ if (useAllItems) {
+ medias = new ArrayList<String>();
+ startPosition = mSongsAdapter.getListWithPosition(medias, groupPosition);
+ }
+ else {
+ startPosition = 0;
+ switch (mTabHost.getCurrentTab())
+ {
+ case 0: // albums
+ medias = mAlbumsAdapter.getLocations(groupPosition);
+ break;
+ case 1: // songs
+ medias = mSongsAdapter.getLocations(groupPosition);
+ break;
+ default:
+ return true;
+ }
+ }
+
+ if (append)
+ mAudioController.append(medias);
+ else
+ mAudioController.load(medias, startPosition);
+
return super.onContextItemSelected(item);
}
@@ -192,4 +298,31 @@ public class AudioAlbumsSongsFragment extends SherlockFragment {
mAudioController.load(mediaLocation, 0);
}
};
+
+ AudioBrowserListAdapter.ContextPopupMenuListener mContextPopupMenuListener
+ = new AudioBrowserListAdapter.ContextPopupMenuListener() {
+
+ @Override
+ @TargetApi(Build.VERSION_CODES.HONEYCOMB)
+ public void onPopupMenu(View anchor, final int position) {
+ if (!Util.isHoneycombOrLater()) {
+ // Call the "classic" context menu
+ anchor.performLongClick();
+ return;
+ }
+
+ PopupMenu popupMenu = new PopupMenu(getActivity(), anchor);
+ popupMenu.getMenuInflater().inflate(R.menu.audio_list_browser, popupMenu.getMenu());
+ setContextMenuItems(popupMenu.getMenu(), anchor);
+
+ popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+ @Override
+ public boolean onMenuItemClick(MenuItem item) {
+ return handleContextItemSelected(item, position);
+ }
+ });
+ popupMenu.show();
+ }
+
+};
}
More information about the Android
mailing list