Made feeditem menu usable across different contexts

This commit is contained in:
daniel oeh 2012-06-20 16:29:26 +02:00
parent 8f9344fbe7
commit 409fcee753
4 changed files with 109 additions and 69 deletions

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/download_item" android:icon="@drawable/av_download" android:visible="false" android:title="@string/download_label"></item>
<item android:id="@+id/remove_item" android:icon="@drawable/content_discard" android:title="@string/remove_label" android:visible="false"></item>
<item android:id="@+id/play_item" android:title="@string/play_label" android:visible="false" android:icon="@drawable/av_play"></item>
<item android:id="@+id/cancel_download_item" android:icon="@drawable/navigation_cancel" android:title="@string/cancel_download_label" android:visible="false"></item>
<item android:id="@+id/download_item" android:icon="@drawable/av_download" android:visible="false" android:title="@string/download_label" android:showAsAction="always"></item>
<item android:id="@+id/remove_item" android:icon="@drawable/content_discard" android:title="@string/remove_label" android:visible="false" android:showAsAction="always"></item>
<item android:id="@+id/play_item" android:title="@string/play_label" android:visible="false" android:icon="@drawable/av_play" android:showAsAction="always"></item>
<item android:id="@+id/cancel_download_item" android:icon="@drawable/navigation_cancel" android:title="@string/cancel_download_label" android:visible="false" android:showAsAction="always"></item>
<item android:id="@+id/mark_read_item" android:title="@string/mark_read_label" android:showAsAction="collapseActionView" android:visible="false"></item>
<item android:id="@+id/mark_unread_item" android:title="@string/mark_unread_label" android:visible="false" android:showAsAction="collapseActionView"></item>
<item android:id="@+id/add_to_queue_item" android:title="@string/add_to_queue_label" android:visible="false" android:showAsAction="collapseActionView"></item>

View File

@ -28,6 +28,7 @@ import de.podfetcher.fragment.FeedItemlistFragment;
import de.podfetcher.fragment.FeedlistFragment;
import de.podfetcher.service.PlaybackService;
import de.podfetcher.storage.DownloadRequester;
import de.podfetcher.syndication.util.FeedItemMenuHandler;
/** Displays a single FeedItem and provides various actions */
public class ItemviewActivity extends SherlockActivity {
@ -158,20 +159,22 @@ public class ItemviewActivity extends SherlockActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.feeditemlist, menu);
getDownloadStatus(menu);
return true;
return FeedItemMenuHandler.onCreateMenu(new MenuInflater(this), menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
public boolean onOptionsItemSelected(MenuItem menuItem) {
FeedItemMenuHandler.onMenuItemClicked(this, menuItem, item);
invalidateOptionsMenu();
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return FeedItemMenuHandler.onPrepareMenu(menu, item);
}
private void setDownloadingState(Menu menu) {

View File

@ -21,6 +21,7 @@ import de.podfetcher.adapter.FeedItemlistAdapter;
import de.podfetcher.feed.FeedItem;
import de.podfetcher.feed.FeedManager;
import de.podfetcher.storage.DownloadRequester;
import de.podfetcher.syndication.util.FeedItemMenuHandler;
public class FeedItemlistFragment extends SherlockListFragment {
private static final String TAG = "FeedItemlistFragment";
@ -96,7 +97,7 @@ public class FeedItemlistFragment extends SherlockListFragment {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
return FeedItemMenuHandler.onPrepareMenu(menu, selectedItem);
}
@Override
@ -107,67 +108,13 @@ public class FeedItemlistFragment extends SherlockListFragment {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.feeditemlist, menu);
mode.setTitle(selectedItem.getTitle());
if (selectedItem.getMedia().isDownloaded()) {
menu.findItem(R.id.play_item).setVisible(true);
menu.findItem(R.id.remove_item).setVisible(true);
} else if (selectedItem.getMedia().getFile_url() == null) {
menu.findItem(R.id.download_item).setVisible(true);
} else {
menu.findItem(R.id.cancel_download_item).setVisible(true);
}
if (selectedItem.isRead()) {
menu.findItem(R.id.mark_unread_item).setVisible(true);
} else {
menu.findItem(R.id.mark_read_item).setVisible(true);
}
if (manager.isInQueue(selectedItem)) {
menu.findItem(R.id.remove_from_queue_item).setVisible(true);
} else {
menu.findItem(R.id.add_to_queue_item).setVisible(true);
}
return true;
return FeedItemMenuHandler.onCreateMenu(mode.getMenuInflater(), menu);
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.download_item:
requester.downloadMedia(getSherlockActivity(),
selectedItem.getMedia());
break;
case R.id.play_item:
manager.playMedia(getSherlockActivity(),
selectedItem.getMedia());
break;
case R.id.remove_item:
manager.deleteFeedMedia(getSherlockActivity(),
selectedItem.getMedia());
break;
case R.id.cancel_download_item:
requester.cancelDownload(getSherlockActivity(), selectedItem
.getMedia().getDownloadId());
break;
case R.id.mark_read_item:
manager.markItemRead(getSherlockActivity(), selectedItem, true);
break;
case R.id.mark_unread_item:
manager.markItemRead(getSherlockActivity(), selectedItem, false);
break;
case R.id.add_to_queue_item:
manager.addQueueItem(getSherlockActivity(), selectedItem);
break;
case R.id.remove_from_queue_item:
manager.removeQueueItem(getSherlockActivity(), selectedItem);
break;
}
FeedItemMenuHandler.onMenuItemClicked(getSherlockActivity(), item, selectedItem);
fila.notifyDataSetChanged();
mode.finish();
return true;

View File

@ -0,0 +1,90 @@
package de.podfetcher.syndication.util;
import android.content.Context;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import de.podfetcher.R;
import de.podfetcher.feed.FeedItem;
import de.podfetcher.feed.FeedManager;
import de.podfetcher.storage.DownloadRequester;
/** Handles interactions with the FeedItemMenu. */
public class FeedItemMenuHandler {
private FeedItemMenuHandler() {
}
public static boolean onPrepareMenu(Menu menu, FeedItem selectedItem) {
FeedManager manager = FeedManager.getInstance();
if (selectedItem.getMedia().isDownloaded()) {
menu.findItem(R.id.play_item).setVisible(true);
menu.findItem(R.id.remove_item).setVisible(true);
} else if (selectedItem.getMedia().getFile_url() == null) {
menu.findItem(R.id.download_item).setVisible(true);
} else {
menu.findItem(R.id.cancel_download_item).setVisible(true);
}
if (selectedItem.isRead()) {
menu.findItem(R.id.mark_unread_item).setVisible(true);
} else {
menu.findItem(R.id.mark_read_item).setVisible(true);
}
if (manager.isInQueue(selectedItem)) {
menu.findItem(R.id.remove_from_queue_item).setVisible(true);
} else {
menu.findItem(R.id.add_to_queue_item).setVisible(true);
}
return true;
}
public static boolean onMenuItemClicked(Context context, MenuItem item, FeedItem selectedItem) {
DownloadRequester requester = DownloadRequester.getInstance();
FeedManager manager = FeedManager.getInstance();
switch (item.getItemId()) {
case R.id.download_item:
requester.downloadMedia(context,
selectedItem.getMedia());
break;
case R.id.play_item:
manager.playMedia(context,
selectedItem.getMedia());
break;
case R.id.remove_item:
manager.deleteFeedMedia(context,
selectedItem.getMedia());
break;
case R.id.cancel_download_item:
requester.cancelDownload(context, selectedItem
.getMedia().getDownloadId());
break;
case R.id.mark_read_item:
manager.markItemRead(context, selectedItem, true);
break;
case R.id.mark_unread_item:
manager.markItemRead(context, selectedItem, false);
break;
case R.id.add_to_queue_item:
manager.addQueueItem(context, selectedItem);
break;
case R.id.remove_from_queue_item:
manager.removeQueueItem(context, selectedItem);
break;
}
// Refresh menu state
return true;
}
public static boolean onCreateMenu(MenuInflater inflater, Menu menu) {
inflater.inflate(R.menu.feeditem, menu);
return true;
}
}