diff --git a/src/de/danoeh/antennapod/activity/ItemviewActivity.java b/src/de/danoeh/antennapod/activity/ItemviewActivity.java index d934244f2..6cf827f41 100644 --- a/src/de/danoeh/antennapod/activity/ItemviewActivity.java +++ b/src/de/danoeh/antennapod/activity/ItemviewActivity.java @@ -62,7 +62,8 @@ public class ItemviewActivity extends SherlockFragmentActivity { @Override public void onStop() { super.onStop(); - if (AppConfig.DEBUG) Log.d(TAG, "Stopping Activity"); + if (AppConfig.DEBUG) + Log.d(TAG, "Stopping Activity"); } /** Extracts FeedItem object the activity is supposed to display */ @@ -76,8 +77,10 @@ public class ItemviewActivity extends SherlockFragmentActivity { } Feed feed = manager.getFeed(feedId); item = manager.getFeedItem(itemId, feed); - if (AppConfig.DEBUG) Log.d(TAG, "Title of item is " + item.getTitle()); - if (AppConfig.DEBUG) Log.d(TAG, "Title of feed is " + item.getFeed().getTitle()); + if (AppConfig.DEBUG) + Log.d(TAG, "Title of item is " + item.getTitle()); + if (AppConfig.DEBUG) + Log.d(TAG, "Title of feed is " + item.getFeed().getTitle()); } private void populateUI() { @@ -95,21 +98,24 @@ public class ItemviewActivity extends SherlockFragmentActivity { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); - ItemDescriptionFragment fragment = ItemDescriptionFragment.newInstance( - item); + ItemDescriptionFragment fragment = ItemDescriptionFragment + .newInstance(item); fragmentTransaction.replace(R.id.description_fragment, fragment); fragmentTransaction.commit(); } @Override public boolean onCreateOptionsMenu(Menu menu) { - return FeedItemMenuHandler.onCreateMenu(new MenuInflater(this), menu); + MenuInflater inflater = getSupportMenuInflater(); + inflater.inflate(R.menu.feeditem, menu); + return true; } @Override public boolean onOptionsItemSelected(MenuItem menuItem) { try { - if (!FeedItemMenuHandler.onMenuItemClicked(this, menuItem, item)) { + if (!FeedItemMenuHandler.onMenuItemClicked(this, + menuItem.getItemId(), item)) { switch (menuItem.getItemId()) { case android.R.id.home: finish(); @@ -118,15 +124,23 @@ public class ItemviewActivity extends SherlockFragmentActivity { } } catch (DownloadRequestException e) { e.printStackTrace(); - DownloadRequestErrorDialogCreator.newRequestErrorDialog(this, e.getMessage()); + DownloadRequestErrorDialogCreator.newRequestErrorDialog(this, + e.getMessage()); } invalidateOptionsMenu(); return true; } @Override - public boolean onPrepareOptionsMenu(Menu menu) { - return FeedItemMenuHandler.onPrepareMenu(menu, item); + public boolean onPrepareOptionsMenu(final Menu menu) { + return FeedItemMenuHandler.onPrepareMenu( + new FeedItemMenuHandler.MenuInterface() { + + @Override + public void setItemVisibility(int id, boolean visible) { + menu.findItem(id).setVisible(visible); + } + }, item); } } diff --git a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java index a53b4f4fb..a9b7df205 100644 --- a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -291,7 +291,7 @@ public abstract class MediaplayerActivity extends SherlockFragmentActivity } default: try { - return FeedItemMenuHandler.onMenuItemClicked(this, item, + return FeedItemMenuHandler.onMenuItemClicked(this, item.getItemId(), controller.getMedia().getItem()); } catch (DownloadRequestException e) { e.printStackTrace(); diff --git a/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java b/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java index dcf5f321b..7e9388ce3 100644 --- a/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java +++ b/src/de/danoeh/antennapod/adapter/FeedItemlistAdapter.java @@ -3,13 +3,19 @@ package de.danoeh.antennapod.adapter; import java.text.DateFormat; import java.util.List; + import android.content.Context; import android.content.res.TypedArray; import android.graphics.Typeface; import android.text.format.DateUtils; +import android.util.Log; +import android.view.ContextMenu; import android.view.LayoutInflater; +import android.view.MenuInflater; import android.view.View; +import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnClickListener; +import android.view.View.OnCreateContextMenuListener; import android.view.ViewGroup; import android.widget.Adapter; import android.widget.ArrayAdapter; @@ -27,7 +33,7 @@ import de.danoeh.antennapod.util.EpisodeFilter; import de.danoeh.antennapod.util.ThemeUtils; public class FeedItemlistAdapter extends ArrayAdapter { - private OnClickListener onButActionClicked; + private FeedItemlistAdapter.Callback callback; private boolean showFeedtitle; private int selectedItemIndex; private List objects; @@ -35,17 +41,17 @@ public class FeedItemlistAdapter extends ArrayAdapter { public static final int SELECTION_NONE = -1; public FeedItemlistAdapter(Context context, int textViewResourceId, - List objects, OnClickListener onButActionClicked, + List objects, FeedItemlistAdapter.Callback callback, boolean showFeedtitle) { super(context, textViewResourceId, objects); this.objects = objects; - this.onButActionClicked = onButActionClicked; + this.callback = callback; this.showFeedtitle = showFeedtitle; this.selectedItemIndex = SELECTION_NONE; } @Override - public View getView(int position, View convertView, ViewGroup parent) { + public View getView(final int position, View convertView, ViewGroup parent) { Holder holder; FeedItem item = getItem(position); @@ -178,7 +184,14 @@ public class FeedItemlistAdapter extends ArrayAdapter { } holder.butAction.setFocusable(false); - holder.butAction.setOnClickListener(onButActionClicked); + holder.butAction.setOnClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + callback.onActionButtonPressed(position); + } + }); + } else { convertView.setVisibility(View.GONE); } @@ -225,5 +238,10 @@ public class FeedItemlistAdapter extends ArrayAdapter { return super.getItem(position); } } + + public interface Callback { + /** Is called when the action button of a list item has been pressed. */ + abstract void onActionButtonPressed(int position); + } } diff --git a/src/de/danoeh/antennapod/fragment/ItemlistFragment.java b/src/de/danoeh/antennapod/fragment/ItemlistFragment.java index 1e33eaaaa..e58472d62 100644 --- a/src/de/danoeh/antennapod/fragment/ItemlistFragment.java +++ b/src/de/danoeh/antennapod/fragment/ItemlistFragment.java @@ -8,10 +8,16 @@ import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; +import android.view.ContextMenu; import android.view.LayoutInflater; +import android.view.MenuInflater; import android.view.View; +import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnClickListener; +import android.view.View.OnCreateContextMenuListener; import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ListView; import com.actionbarsherlock.app.SherlockListFragment; @@ -33,8 +39,7 @@ import de.danoeh.antennapod.storage.DownloadRequester; import de.danoeh.antennapod.util.menuhandler.FeedItemMenuHandler; /** Displays a list of FeedItems. */ -public class ItemlistFragment extends SherlockListFragment implements - ActionMode.Callback { +public class ItemlistFragment extends SherlockListFragment { private static final String TAG = "ItemlistFragment"; public static final String EXTRA_SELECTED_FEEDITEM = "extra.de.danoeh.antennapod.activity.selected_feeditem"; @@ -51,8 +56,8 @@ public class ItemlistFragment extends SherlockListFragment implements */ protected Feed feed; - protected FeedItem selectedItem; - protected ActionMode mActionMode; + protected static final int NO_SELECTION = -1; + protected int selectedPosition = NO_SELECTION; /** Argument for FeeditemlistAdapter */ protected boolean showFeedtitle; @@ -101,16 +106,13 @@ public class ItemlistFragment extends SherlockListFragment implements } fila = new FeedItemlistAdapter(getActivity(), 0, items, - onButActionClicked, showFeedtitle); + adapterCallback, showFeedtitle); setListAdapter(fila); } @Override public void onPause() { super.onPause(); - if (mActionMode != null) { - mActionMode.finish(); - } } @Override @@ -184,26 +186,14 @@ public class ItemlistFragment extends SherlockListFragment implements } } - private final OnClickListener onButActionClicked = new OnClickListener() { + private FeedItemlistAdapter.Callback adapterCallback = new FeedItemlistAdapter.Callback() { + @Override - public void onClick(View v) { - int index = getListView().getPositionForView(v); - if (index != ListView.INVALID_POSITION) { - FeedItem newSelectedItem = fila.getItem(index); - if (newSelectedItem != selectedItem) { - if (mActionMode != null) { - mActionMode.finish(); - } - - selectedItem = newSelectedItem; - mActionMode = getSherlockActivity().startActionMode( - ItemlistFragment.this); - fila.setSelectedItemIndex(index); - } else { - mActionMode.finish(); - } - - } + public void onActionButtonPressed(int position) { + if (AppConfig.DEBUG) + Log.d(TAG, "adapterCallback; position = " + position); + selectedPosition = position; + getListView().showContextMenu(); } }; @@ -211,41 +201,56 @@ public class ItemlistFragment extends SherlockListFragment implements public void onViewCreated(View view, Bundle savedInstanceState) { this.getListView().setItemsCanFocus(true); getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); + registerForContextMenu(getListView()); + getListView().setOnItemLongClickListener(null); } @Override - public boolean onPrepareActionMode(ActionMode mode, Menu menu) { - return FeedItemMenuHandler.onPrepareMenu(menu, selectedItem); + public void onCreateContextMenu(final ContextMenu menu, View v, + ContextMenuInfo menuInfo) { + super.onCreateContextMenu(menu, v, menuInfo); + if (selectedPosition != NO_SELECTION) { + new MenuInflater(ItemlistFragment.this.getActivity()).inflate( + R.menu.feeditem, menu); + FeedItem selection = fila.getItem(selectedPosition); + if (selection != null) { + menu.setHeaderTitle(selection.getTitle()); + FeedItemMenuHandler.onPrepareMenu( + new FeedItemMenuHandler.MenuInterface() { + + @Override + public void setItemVisibility(int id, + boolean visible) { + menu.findItem(id).setVisible(visible); + } + }, selection); + } + } } @Override - public void onDestroyActionMode(ActionMode mode) { - mActionMode = null; - selectedItem = null; - fila.setSelectedItemIndex(FeedItemlistAdapter.SELECTION_NONE); - } - - @Override - public boolean onCreateActionMode(ActionMode mode, Menu menu) { - return FeedItemMenuHandler.onCreateMenu(mode.getMenuInflater(), menu); - - } - - @Override - public boolean onActionItemClicked(ActionMode mode, MenuItem item) { + public boolean onContextItemSelected(android.view.MenuItem item) { boolean handled = false; - try { - handled = FeedItemMenuHandler.onMenuItemClicked( - getSherlockActivity(), item, selectedItem); - } catch (DownloadRequestException e) { - e.printStackTrace(); - DownloadRequestErrorDialogCreator.newRequestErrorDialog( - getActivity(), e.getMessage()); + + if (selectedPosition != NO_SELECTION) { + FeedItem selectedItem = fila.getItem(selectedPosition); + + if (selectedItem != null) { + try { + handled = FeedItemMenuHandler.onMenuItemClicked( + getSherlockActivity(), item.getItemId(), + selectedItem); + } catch (DownloadRequestException e) { + e.printStackTrace(); + DownloadRequestErrorDialogCreator.newRequestErrorDialog( + getActivity(), e.getMessage()); + } + if (handled) { + fila.notifyDataSetChanged(); + } + } } - if (handled) { - fila.notifyDataSetChanged(); - } - mode.finish(); + selectedPosition = NO_SELECTION; return handled; } diff --git a/src/de/danoeh/antennapod/fragment/QueueFragment.java b/src/de/danoeh/antennapod/fragment/QueueFragment.java index 2c712c464..c3647c383 100644 --- a/src/de/danoeh/antennapod/fragment/QueueFragment.java +++ b/src/de/danoeh/antennapod/fragment/QueueFragment.java @@ -17,42 +17,12 @@ public class QueueFragment extends ItemlistFragment { super(FeedManager.getInstance().getQueue(), true); } - @Override - public boolean onCreateActionMode(ActionMode mode, Menu menu) { - super.onCreateActionMode(mode, menu); - menu.add(Menu.NONE, R.id.move_up_item, Menu.NONE, - R.string.move_up_label); - menu.add(Menu.NONE, R.id.move_down_item, Menu.NONE, - R.string.move_down_label); - return true; - } - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } - @Override - public boolean onActionItemClicked(ActionMode mode, MenuItem item) { - boolean handled = false; - switch (item.getItemId()) { - case R.id.move_up_item: - manager.moveQueueItem(getActivity(), selectedItem, -1); - handled = true; - break; - case R.id.move_down_item: - manager.moveQueueItem(getActivity(), selectedItem, 1); - handled = true; - break; - default: - handled = super.onActionItemClicked(mode, item); - } - fila.notifyDataSetChanged(); - mode.finish(); - return handled; - } - @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); diff --git a/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java b/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java index 2b7d5e2b0..a1145f372 100644 --- a/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java +++ b/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java @@ -3,10 +3,9 @@ package de.danoeh.antennapod.util.menuhandler; import android.content.Context; import android.content.Intent; import android.net.Uri; - -import com.actionbarsherlock.view.Menu; -import com.actionbarsherlock.view.MenuInflater; -import com.actionbarsherlock.view.MenuItem; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; @@ -24,7 +23,20 @@ public class FeedItemMenuHandler { } - public static boolean onPrepareMenu(Menu menu, FeedItem selectedItem) { + /** + * Used by the MenuHandler to access different types of menus through one + * interface (for example android.view.Menu and com.actionbarsherlock.Menu) + */ + public interface MenuInterface { + /** + * Implementations of this method should call findItem(id) on their + * menu-object and call setVisibility(visibility) on the returned + * MenuItem object. + */ + abstract void setItemVisibility(int id, boolean visible); + } + + public static boolean onPrepareMenu(MenuInterface mi, FeedItem selectedItem) { FeedManager manager = FeedManager.getInstance(); DownloadRequester requester = DownloadRequester.getInstance(); boolean hasMedia = selectedItem.getMedia() != null; @@ -35,45 +47,44 @@ public class FeedItemMenuHandler { && (!downloading); FeedItem.State state = selectedItem.getState(); - menu.findItem(R.id.play_item).setVisible(downloaded); - menu.findItem(R.id.remove_item).setVisible(downloaded); - menu.findItem(R.id.download_item).setVisible(notLoadedAndNotLoading); - menu.findItem(R.id.stream_item).setVisible( - notLoadedAndNotLoading | downloading); - menu.findItem(R.id.cancel_download_item).setVisible(downloading); + mi.setItemVisibility(R.id.play_item, downloaded); + mi.setItemVisibility(R.id.remove_item, downloaded); + mi.setItemVisibility(R.id.download_item, notLoadedAndNotLoading); + mi.setItemVisibility(R.id.stream_item, notLoadedAndNotLoading + | downloading); + mi.setItemVisibility(R.id.cancel_download_item, downloading); boolean isInQueue = manager.isInQueue(selectedItem); - menu.findItem(R.id.remove_from_queue_item).setVisible(isInQueue); - menu.findItem(R.id.add_to_queue_item).setVisible( + mi.setItemVisibility(R.id.remove_from_queue_item, isInQueue); + mi.setItemVisibility(R.id.add_to_queue_item, !isInQueue && selectedItem.getMedia() != null); - menu.findItem(R.id.share_link_item).setVisible( + mi.setItemVisibility(R.id.share_link_item, selectedItem.getLink() != null); - menu.findItem(R.id.mark_unread_item).setVisible( + mi.setItemVisibility(R.id.mark_unread_item, state == FeedItem.State.IN_PROGRESS || state == FeedItem.State.READ); - menu.findItem(R.id.mark_read_item).setVisible( - state == FeedItem.State.NEW - || state == FeedItem.State.IN_PROGRESS); + mi.setItemVisibility(R.id.mark_read_item, state == FeedItem.State.NEW + || state == FeedItem.State.IN_PROGRESS); if (selectedItem.getLink() != null) { - menu.findItem(R.id.visit_website_item).setVisible(true); + mi.setItemVisibility(R.id.visit_website_item, true); } if (selectedItem.getPaymentLink() != null) { - menu.findItem(R.id.support_item).setVisible(true); + mi.setItemVisibility(R.id.support_item, true); } return true; } - public static boolean onMenuItemClicked(Context context, MenuItem item, + public static boolean onMenuItemClicked(Context context, int menuItemId, FeedItem selectedItem) throws DownloadRequestException { DownloadRequester requester = DownloadRequester.getInstance(); FeedManager manager = FeedManager.getInstance(); - switch (item.getItemId()) { + switch (menuItemId) { case R.id.download_item: manager.downloadFeedItem(context, selectedItem); break; @@ -122,9 +133,4 @@ public class FeedItemMenuHandler { return true; } - public static boolean onCreateMenu(MenuInflater inflater, Menu menu) { - inflater.inflate(R.menu.feeditem, menu); - return true; - } - }