diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2012-08-01 11:32:31 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2012-08-01 11:32:31 +0200 |
commit | 9396769e17a021a16217c167814f506d4408b862 (patch) | |
tree | ce7fed0c74ec48d23095087ae26e75c5b24c4583 | |
parent | 7d0cca1147c57e471e850ff0ba783b28280f8c5a (diff) | |
download | AntennaPod-9396769e17a021a16217c167814f506d4408b862.zip |
Improved stability when downloading and import feeds
4 files changed, 67 insertions, 32 deletions
diff --git a/src/de/danoeh/antennapod/feed/FeedManager.java b/src/de/danoeh/antennapod/feed/FeedManager.java index e59721574..5c7041a8d 100644 --- a/src/de/danoeh/antennapod/feed/FeedManager.java +++ b/src/de/danoeh/antennapod/feed/FeedManager.java @@ -19,6 +19,8 @@ import android.content.Intent; import android.database.Cursor; import android.os.AsyncTask; import android.os.Debug; +import android.os.Handler; +import android.os.Message; import android.util.Log; /** @@ -29,6 +31,7 @@ import android.util.Log; public class FeedManager { private static final String TAG = "FeedManager"; + public static final String ACITON_FEED_LIST_UPDATE = "de.danoeh.antennapod.action.feed.feedlistUpdate"; public static final String ACTION_UNREAD_ITEMS_UPDATE = "de.danoeh.antennapod.action.feed.unreadItemsUpdate"; public static final String ACTION_QUEUE_UPDATE = "de.danoeh.antennapod.action.feed.queueUpdate"; public static final String EXTRA_FEED_ITEM_ID = "de.danoeh.antennapod.extra.feed.feedItemId"; @@ -53,6 +56,9 @@ public class FeedManager { private DownloadRequester requester; + /** Should be used to change the content of the arrays from another thread. */ + private Handler contentChanger; + /** Prevents user from starting several feed updates at the same time. */ private static boolean isStartingFeedRefresh = false; @@ -63,6 +69,7 @@ public class FeedManager { requester = DownloadRequester.getInstance(); downloadLog = new ArrayList<DownloadStatus>(); queue = Collections.synchronizedList(new ArrayList<FeedItem>()); + contentChanger = new Handler(); } public static FeedManager getInstance() { @@ -122,7 +129,7 @@ public class FeedManager { } /** Remove a feed with all its items and media files and its image. */ - public boolean deleteFeed(Context context, Feed feed) { + public void deleteFeed(final Context context, final Feed feed) { PodDBAdapter adapter = new PodDBAdapter(context); adapter.open(); // delete image file @@ -149,7 +156,13 @@ public class FeedManager { adapter.removeFeed(feed); adapter.close(); - return feeds.remove(feed); + contentChanger.post(new Runnable() { + + @Override + public void run() { + feeds.remove(feed); + sendFeedUpdateBroadcast(context); + }}); } @@ -170,24 +183,34 @@ public class FeedManager { } context.sendBroadcast(update); } + + private void sendFeedUpdateBroadcast(Context context) { + context.sendBroadcast(new Intent(ACITON_FEED_LIST_UPDATE)); + } /** * Sets the 'read'-attribute of a FeedItem. Should be used by all Classes * instead of the setters of FeedItem. */ - public void markItemRead(Context context, FeedItem item, boolean read) { + public void markItemRead(final Context context, final FeedItem item, final boolean read) { if (AppConfig.DEBUG) Log.d(TAG, "Setting item with title " + item.getTitle() + " as read/unread"); item.read = read; setFeedItem(context, item); - if (read == true) { - unreadItems.remove(item); - } else { - unreadItems.add(item); - Collections.sort(unreadItems, new FeedItemPubdateComparator()); - } - sendUnreadItemsUpdateBroadcast(context, item); + contentChanger.post(new Runnable() { + + @Override + public void run() { + if (read == true) { + unreadItems.remove(item); + } else { + unreadItems.add(item); + Collections.sort(unreadItems, new FeedItemPubdateComparator()); + } + sendUnreadItemsUpdateBroadcast(context, item); + }}); + } /** @@ -351,9 +374,17 @@ public class FeedManager { } } - private void addNewFeed(Context context, Feed feed) { - feeds.add(feed); - Collections.sort(feeds, new FeedtitleComparator()); + private void addNewFeed(final Context context, final Feed feed) { + contentChanger.post(new Runnable() { + + @Override + public void run() { + feeds.add(feed); + Collections.sort(feeds, new FeedtitleComparator()); + sendFeedUpdateBroadcast(context); + } + }); + PodDBAdapter adapter = new PodDBAdapter(context); adapter.open(); adapter.setCompleteFeed(feed); @@ -382,13 +413,20 @@ public class FeedManager { + " already exists. Syncing new with existing one."); // Look for new or updated Items for (int idx = 0; idx < newFeed.getItems().size(); idx++) { - FeedItem item = newFeed.getItems().get(idx); + final FeedItem item = newFeed.getItems().get(idx); FeedItem oldItem = searchFeedItemByIdentifyingValue(savedFeed, item.getIdentifyingValue()); if (oldItem == null) { // item is new + final int i = idx; item.setFeed(savedFeed); - savedFeed.getItems().add(idx, item); + contentChanger.post(new Runnable() { + @Override + public void run() { + savedFeed.getItems().add(i, item); + + } + }); markItemRead(context, item, false); } } diff --git a/src/de/danoeh/antennapod/fragment/FeedlistFragment.java b/src/de/danoeh/antennapod/fragment/FeedlistFragment.java index aef5f2115..d83671a82 100644 --- a/src/de/danoeh/antennapod/fragment/FeedlistFragment.java +++ b/src/de/danoeh/antennapod/fragment/FeedlistFragment.java @@ -106,10 +106,9 @@ public class FeedlistFragment extends SherlockListFragment implements super.onResume(); if (AppConfig.DEBUG) Log.d(TAG, "Resuming"); IntentFilter filter = new IntentFilter(); - filter.addAction(DownloadService.ACTION_DOWNLOAD_HANDLED); - filter.addAction(DownloadService.ACTION_FEED_SYNC_COMPLETED); filter.addAction(DownloadRequester.ACTION_DOWNLOAD_QUEUED); filter.addAction(FeedManager.ACTION_UNREAD_ITEMS_UPDATE); + filter.addAction(FeedManager.ACITON_FEED_LIST_UPDATE); pActivity.registerReceiver(contentUpdate, filter); fla.notifyDataSetChanged(); diff --git a/src/de/danoeh/antennapod/fragment/ItemlistFragment.java b/src/de/danoeh/antennapod/fragment/ItemlistFragment.java index 151838147..de1a5cb4f 100644 --- a/src/de/danoeh/antennapod/fragment/ItemlistFragment.java +++ b/src/de/danoeh/antennapod/fragment/ItemlistFragment.java @@ -131,6 +131,7 @@ public class ItemlistFragment extends SherlockListFragment implements }); 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); @@ -154,15 +155,20 @@ public class ItemlistFragment extends SherlockListFragment implements public void onReceive(Context context, Intent intent) { if (AppConfig.DEBUG) Log.d(TAG, "Received contentUpdate Intent."); - getActivity().runOnUiThread(new Runnable() { + if (intent.getAction().equals( + DownloadRequester.ACTION_DOWNLOAD_QUEUED)) { + updateProgressBarVisibility(); + } else { + getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - fila.notifyDataSetChanged(); - updateProgressBarVisibility(); - } + @Override + public void run() { + fila.notifyDataSetChanged(); + updateProgressBarVisibility(); + } - }); + }); + } } }; diff --git a/src/de/danoeh/antennapod/service/DownloadService.java b/src/de/danoeh/antennapod/service/DownloadService.java index 728faddfb..93619a48c 100644 --- a/src/de/danoeh/antennapod/service/DownloadService.java +++ b/src/de/danoeh/antennapod/service/DownloadService.java @@ -57,7 +57,6 @@ public class DownloadService extends Service { private static final String TAG = "DownloadService"; public static String ACTION_ALL_FEED_DOWNLOADS_COMPLETED = "action.de.danoeh.antennapod.storage.all_feed_downloads_completed"; - public static final String ACTION_FEED_SYNC_COMPLETED = "action.de.danoeh.antennapod.service.feed_sync_completed"; public static final String ACTION_DOWNLOAD_HANDLED = "action.de.danoeh.antennapod.service.download_handled"; /** True if handled feed has an image. */ @@ -139,7 +138,6 @@ public class DownloadService extends Service { if (AppConfig.DEBUG) Log.d(TAG, "Service shutting down"); isRunning = false; - sendBroadcast(new Intent(ACTION_FEED_SYNC_COMPLETED)); mediaplayer.release(); unregisterReceiver(downloadReceiver); downloadObserver.cancel(true); @@ -460,12 +458,6 @@ public class DownloadService extends Service { reason, successful)); sendDownloadHandledIntent(downloadId, statusId, hasImage, imageId); queryDownloads(); - try { - Thread.sleep(100); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } } /** Delete files that aren't needed anymore */ |