diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2013-02-25 10:47:37 +0100 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2013-02-25 10:47:37 +0100 |
commit | 955d296638b240d8bbf4be8cce4b27485ac4c517 (patch) | |
tree | 16a35b7a9493495a8e5f7a87ed646a2d166c7fe0 /src/de/danoeh/antennapod/fragment | |
parent | f9e00f72a0dfeea3e5b9db8a522f7251e158dc7d (diff) | |
download | AntennaPod-955d296638b240d8bbf4be8cce4b27485ac4c517.zip |
Implemented EventDistributor
Diffstat (limited to 'src/de/danoeh/antennapod/fragment')
5 files changed, 73 insertions, 99 deletions
diff --git a/src/de/danoeh/antennapod/fragment/EpisodesFragment.java b/src/de/danoeh/antennapod/fragment/EpisodesFragment.java index 2046b45ba..ac1fc402e 100644 --- a/src/de/danoeh/antennapod/fragment/EpisodesFragment.java +++ b/src/de/danoeh/antennapod/fragment/EpisodesFragment.java @@ -1,9 +1,6 @@ package de.danoeh.antennapod.fragment; -import android.content.BroadcastReceiver; -import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; @@ -25,16 +22,21 @@ import de.danoeh.antennapod.activity.OrganizeQueueActivity; import de.danoeh.antennapod.adapter.ActionButtonCallback; import de.danoeh.antennapod.adapter.ExternalEpisodesListAdapter; import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator; +import de.danoeh.antennapod.feed.EventDistributor; import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedManager; -import de.danoeh.antennapod.service.download.DownloadService; import de.danoeh.antennapod.storage.DownloadRequestException; -import de.danoeh.antennapod.storage.DownloadRequester; import de.danoeh.antennapod.util.menuhandler.FeedItemMenuHandler; public class EpisodesFragment extends SherlockFragment { private static final String TAG = "EpisodesFragment"; + private static final int EVENTS = EventDistributor.QUEUE_UPDATE + | EventDistributor.UNREAD_ITEMS_UPDATE + | EventDistributor.FEED_LIST_UPDATE + | EventDistributor.DOWNLOAD_HANDLED + | EventDistributor.DOWNLOAD_QUEUED; + private ExpandableListView listView; private ExternalEpisodesListAdapter adapter; @@ -45,24 +47,14 @@ public class EpisodesFragment extends SherlockFragment { @Override public void onDestroy() { super.onDestroy(); - try { - getActivity().unregisterReceiver(contentUpdate); - } catch (IllegalArgumentException e) { - - } + EventDistributor.getInstance().unregister(contentUpdate); } @Override public void onResume() { super.onResume(); - IntentFilter filter = new IntentFilter(); - filter.addAction(DownloadRequester.ACTION_DOWNLOAD_QUEUED); - filter.addAction(DownloadService.ACTION_DOWNLOAD_HANDLED); - filter.addAction(FeedManager.ACTION_QUEUE_UPDATE); - filter.addAction(FeedManager.ACTION_UNREAD_ITEMS_UPDATE); - filter.addAction(FeedManager.ACTION_FEED_LIST_UPDATE); - - getActivity().registerReceiver(contentUpdate, filter); + + EventDistributor.getInstance().register(contentUpdate); if (adapter != null) { adapter.notifyDataSetChanged(); } @@ -128,12 +120,15 @@ public class EpisodesFragment extends SherlockFragment { registerForContextMenu(listView); } - private BroadcastReceiver contentUpdate = new BroadcastReceiver() { + private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { + @Override - public void onReceive(Context context, Intent intent) { - if (AppConfig.DEBUG) - Log.d(TAG, "Received contentUpdate Intent."); - adapter.notifyDataSetChanged(); + public void update(EventDistributor eventDistributor, Integer arg) { + if ((EVENTS & arg) != 0) { + if (AppConfig.DEBUG) + Log.d(TAG, "Received contentUpdate Intent."); + adapter.notifyDataSetChanged(); + } } }; diff --git a/src/de/danoeh/antennapod/fragment/FeedlistFragment.java b/src/de/danoeh/antennapod/fragment/FeedlistFragment.java index f9b1b7474..c3034c2af 100644 --- a/src/de/danoeh/antennapod/fragment/FeedlistFragment.java +++ b/src/de/danoeh/antennapod/fragment/FeedlistFragment.java @@ -2,11 +2,8 @@ package de.danoeh.antennapod.fragment; import android.annotation.SuppressLint; import android.app.Activity; -import android.content.BroadcastReceiver; -import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; @@ -18,7 +15,6 @@ import android.widget.ListView; import android.widget.TextView; import com.actionbarsherlock.app.SherlockFragment; -import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.ActionMode; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; @@ -30,17 +26,22 @@ import de.danoeh.antennapod.adapter.FeedlistAdapter; import de.danoeh.antennapod.asynctask.FeedRemover; import de.danoeh.antennapod.dialog.ConfirmationDialog; import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator; +import de.danoeh.antennapod.feed.EventDistributor; import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.FeedManager; -import de.danoeh.antennapod.service.download.DownloadService; import de.danoeh.antennapod.storage.DownloadRequestException; -import de.danoeh.antennapod.storage.DownloadRequester; import de.danoeh.antennapod.util.menuhandler.FeedMenuHandler; public class FeedlistFragment extends SherlockFragment implements ActionMode.Callback, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener { private static final String TAG = "FeedlistFragment"; + + private static final int EVENTS = EventDistributor.DOWNLOAD_HANDLED + | EventDistributor.DOWNLOAD_QUEUED + | EventDistributor.FEED_LIST_UPDATE + | EventDistributor.UNREAD_ITEMS_UPDATE; + public static final String EXTRA_SELECTED_FEED = "extra.de.danoeh.antennapod.activity.selected_feed"; private FeedManager manager; @@ -110,36 +111,28 @@ public class FeedlistFragment extends SherlockFragment implements super.onResume(); if (AppConfig.DEBUG) Log.d(TAG, "Resuming"); - IntentFilter filter = new IntentFilter(); - filter.addAction(DownloadRequester.ACTION_DOWNLOAD_QUEUED); - filter.addAction(FeedManager.ACTION_UNREAD_ITEMS_UPDATE); - filter.addAction(FeedManager.ACTION_FEED_LIST_UPDATE); - filter.addAction(DownloadService.ACTION_DOWNLOAD_HANDLED); - getActivity().registerReceiver(contentUpdate, filter); + EventDistributor.getInstance().register(contentUpdate); fla.notifyDataSetChanged(); } @Override public void onPause() { super.onPause(); - getActivity().unregisterReceiver(contentUpdate); + EventDistributor.getInstance().unregister(contentUpdate); if (mActionMode != null) { mActionMode.finish(); } } - private BroadcastReceiver contentUpdate = new BroadcastReceiver() { + private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { + @Override - public void onReceive(Context context, final Intent intent) { - if (AppConfig.DEBUG) - Log.d(TAG, "Received contentUpdate Intent."); - getActivity().runOnUiThread(new Runnable() { - - @Override - public void run() { - fla.notifyDataSetChanged(); - } - }); + public void update(EventDistributor eventDistributor, Integer arg) { + if ((EVENTS & arg) != 0) { + if (AppConfig.DEBUG) + Log.d(TAG, "Received contentUpdate Intent."); + fla.notifyDataSetChanged(); + } } }; diff --git a/src/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java b/src/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java index e819a1000..5f1ce0092 100644 --- a/src/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java +++ b/src/de/danoeh/antennapod/fragment/ItemDescriptionFragment.java @@ -28,7 +28,6 @@ import android.widget.Toast; import com.actionbarsherlock.app.SherlockFragment; import de.danoeh.antennapod.AppConfig; -import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.FeedItem; diff --git a/src/de/danoeh/antennapod/fragment/ItemlistFragment.java b/src/de/danoeh/antennapod/fragment/ItemlistFragment.java index 6b516f888..9e9389838 100644 --- a/src/de/danoeh/antennapod/fragment/ItemlistFragment.java +++ b/src/de/danoeh/antennapod/fragment/ItemlistFragment.java @@ -1,12 +1,7 @@ package de.danoeh.antennapod.fragment; -import java.util.List; - import android.annotation.SuppressLint; -import android.content.BroadcastReceiver; -import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; @@ -26,6 +21,7 @@ import de.danoeh.antennapod.adapter.AbstractFeedItemlistAdapter; import de.danoeh.antennapod.adapter.ActionButtonCallback; import de.danoeh.antennapod.adapter.FeedItemlistAdapter; import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator; +import de.danoeh.antennapod.feed.EventDistributor; import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedManager; @@ -37,8 +33,13 @@ import de.danoeh.antennapod.util.menuhandler.FeedItemMenuHandler; /** Displays a list of FeedItems. */ @SuppressLint("ValidFragment") public class ItemlistFragment extends SherlockListFragment { - private static final String TAG = "ItemlistFragment"; + + private static final int EVENTS = EventDistributor.DOWNLOAD_HANDLED + | EventDistributor.DOWNLOAD_QUEUED + | EventDistributor.QUEUE_UPDATE + | EventDistributor.UNREAD_ITEMS_UPDATE; + public static final String EXTRA_SELECTED_FEEDITEM = "extra.de.danoeh.antennapod.activity.selected_feeditem"; public static final String ARGUMENT_FEED_ID = "argument.de.danoeh.antennapod.feed_id"; protected AbstractFeedItemlistAdapter fila; @@ -48,14 +49,15 @@ public class ItemlistFragment extends SherlockListFragment { private AbstractFeedItemlistAdapter.ItemAccess itemAccess; private Feed feed; - + protected FeedItem selectedItem = null; protected boolean contextMenuClosed = true; /** Argument for FeeditemlistAdapter */ protected boolean showFeedtitle; - public ItemlistFragment(AbstractFeedItemlistAdapter.ItemAccess itemAccess, boolean showFeedtitle) { + public ItemlistFragment(AbstractFeedItemlistAdapter.ItemAccess itemAccess, + boolean showFeedtitle) { super(); this.itemAccess = itemAccess; this.showFeedtitle = showFeedtitle; @@ -95,12 +97,12 @@ public class ItemlistFragment extends SherlockListFragment { final Feed feed = FeedManager.getInstance().getFeed(feedId); this.feed = feed; itemAccess = new AbstractFeedItemlistAdapter.ItemAccess() { - + @Override public FeedItem getItem(int position) { return feed.getItemAtIndex(true, position); } - + @Override public int getCount() { return feed.getNumOfItems(true); @@ -122,12 +124,7 @@ public class ItemlistFragment extends SherlockListFragment { @Override public void onDestroy() { super.onDestroy(); - try { - getActivity().unregisterReceiver(contentUpdate); - } catch (IllegalArgumentException e) { - Log.w(TAG, - "IllegalArgumentException when trying to unregister contentUpdate receiver."); - } + EventDistributor.getInstance().unregister(contentUpdate); } @Override @@ -141,13 +138,7 @@ public class ItemlistFragment extends SherlockListFragment { } }); updateProgressBarVisibility(); - IntentFilter filter = new IntentFilter(); - filter.addAction(DownloadRequester.ACTION_DOWNLOAD_QUEUED); - filter.addAction(DownloadService.ACTION_DOWNLOAD_HANDLED); - filter.addAction(FeedManager.ACTION_QUEUE_UPDATE); - filter.addAction(FeedManager.ACTION_UNREAD_ITEMS_UPDATE); - - getActivity().registerReceiver(contentUpdate, filter); + EventDistributor.getInstance().register(contentUpdate); } @Override @@ -161,17 +152,19 @@ public class ItemlistFragment extends SherlockListFragment { startActivity(showItem); } - private BroadcastReceiver contentUpdate = new BroadcastReceiver() { + private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { + @Override - public void onReceive(Context context, Intent intent) { - if (AppConfig.DEBUG) - Log.d(TAG, "Received contentUpdate Intent."); - if (intent.getAction().equals( - DownloadRequester.ACTION_DOWNLOAD_QUEUED)) { - updateProgressBarVisibility(); - } else { - fila.notifyDataSetChanged(); - updateProgressBarVisibility(); + public void update(EventDistributor eventDistributor, Integer arg) { + if ((EVENTS & arg) != 0) { + if (AppConfig.DEBUG) + Log.d(TAG, "Received contentUpdate Intent."); + if ((EventDistributor.DOWNLOAD_QUEUED & arg) != 0) { + updateProgressBarVisibility(); + } else { + fila.notifyDataSetChanged(); + updateProgressBarVisibility(); + } } } }; diff --git a/src/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java b/src/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java index 17ff9c6a1..5c4750acd 100644 --- a/src/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java +++ b/src/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java @@ -1,13 +1,10 @@ package de.danoeh.antennapod.fragment; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.adapter.AbstractFeedItemlistAdapter; +import de.danoeh.antennapod.feed.EventDistributor; import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedManager; @@ -33,29 +30,26 @@ public class PlaybackHistoryFragment extends ItemlistFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - getActivity().registerReceiver(historyUpdate, - new IntentFilter(FeedManager.ACTION_PLAYBACK_HISTORY_UPDATE)); + EventDistributor.getInstance().register(historyUpdate); } @Override public void onDestroy() { super.onDestroy(); - try { - getActivity().unregisterReceiver(historyUpdate); - } catch (IllegalArgumentException e) { - // ignore - } + EventDistributor.getInstance().unregister(historyUpdate); } - private BroadcastReceiver historyUpdate = new BroadcastReceiver() { - + private EventDistributor.EventListener historyUpdate = new EventDistributor.EventListener() { + @Override - public void onReceive(Context context, Intent intent) { - if (AppConfig.DEBUG) - Log.d(TAG, "Received content update"); - fila.notifyDataSetChanged(); + public void update(EventDistributor eventDistributor, Integer arg) { + if ((EventDistributor.PLAYBACK_HISTORY_UPDATE & arg) != 0) { + if (AppConfig.DEBUG) + Log.d(TAG, "Received content update"); + fila.notifyDataSetChanged(); + } + } - }; } |