diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/AllEpisodesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/AllEpisodesFragment.java index f3846c8a4..209d5bca8 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/AllEpisodesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/AllEpisodesFragment.java @@ -224,7 +224,7 @@ public class AllEpisodesFragment extends Fragment { case R.id.refresh_item: List feeds = ((MainActivity) getActivity()).getFeeds(); if (feeds != null) { - DBTasks.refreshAllFeeds(getActivity(), feeds); + DBTasks.refreshAllFeeds(getActivity(), feeds, null); } return true; case R.id.mark_all_read_item: diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java index 85abdd281..349b4bd8f 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java @@ -285,7 +285,7 @@ public class QueueFragment extends Fragment { case R.id.refresh_item: List feeds = ((MainActivity) getActivity()).getFeeds(); if (feeds != null) { - DBTasks.refreshAllFeeds(getActivity(), feeds); + DBTasks.refreshAllFeeds(getActivity(), feeds, null); } return true; case R.id.clear_queue: diff --git a/core/src/main/java/de/danoeh/antennapod/core/receiver/FeedUpdateReceiver.java b/core/src/main/java/de/danoeh/antennapod/core/receiver/FeedUpdateReceiver.java index 67f6d9348..05e12f6df 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/receiver/FeedUpdateReceiver.java +++ b/core/src/main/java/de/danoeh/antennapod/core/receiver/FeedUpdateReceiver.java @@ -20,7 +20,7 @@ public class FeedUpdateReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Log.d(TAG, "Received intent"); ClientConfig.initialize(context); - FeedUpdateUtils.startAutoUpdate(context, false); + FeedUpdateUtils.startAutoUpdate(context, null); UserPreferences.restartUpdateAlarm(false); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateJobService.java b/core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateJobService.java index 3fc3551ee..55a8d6b86 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateJobService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateJobService.java @@ -18,11 +18,10 @@ public class FeedUpdateJobService extends JobService { Log.d(TAG, "Job started"); ClientConfig.initialize(getApplicationContext()); - new Thread(() -> { - FeedUpdateUtils.startAutoUpdate(getApplicationContext(), true); + FeedUpdateUtils.startAutoUpdate(getApplicationContext(), () -> { UserPreferences.restartUpdateAlarm(false); jobFinished(params, false); // needsReschedule = false - }).start(); + }); return true; } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java index b1937985f..d1713ff99 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java @@ -148,45 +148,41 @@ public final class DBTasks { * Refreshes a given list of Feeds in a separate Thread. This method might ignore subsequent calls if it is still * enqueuing Feeds for download from a previous call * - * @param context Might be used for accessing the database - * @param feeds List of Feeds that should be refreshed. + * @param context Might be used for accessing the database + * @param feeds List of Feeds that should be refreshed. + * @param callback Called after everything was added enqueued for download */ - public static void refreshAllFeeds(final Context context, final List feeds) { - new Thread(() -> refreshAllFeedsSynchronously(context, feeds)).start(); - } - - /** - * Refreshes a given list of Feeds in the current Thread. This method might ignore subsequent calls if it is still - * enqueuing Feeds for download from a previous call. MUST NOT be executed from main thread. - * - * @param context Might be used for accessing the database - * @param feeds List of Feeds that should be refreshed. - */ - public static void refreshAllFeedsSynchronously(final Context context, final List feeds) { + public static void refreshAllFeeds(final Context context, final List feeds, Runnable callback) { if (isRefreshing.compareAndSet(false, true)) { - if (feeds != null) { - refreshFeeds(context, feeds); - } else { - refreshFeeds(context, DBReader.getFeedList()); - } - isRefreshing.set(false); + new Thread(() -> { + if (feeds != null) { + refreshFeeds(context, feeds); + } else { + refreshFeeds(context, DBReader.getFeedList()); + } + isRefreshing.set(false); - SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, MODE_PRIVATE); - prefs.edit().putLong(PREF_LAST_REFRESH, System.currentTimeMillis()).apply(); + SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, MODE_PRIVATE); + prefs.edit().putLong(PREF_LAST_REFRESH, System.currentTimeMillis()).apply(); - if (FlattrUtils.hasToken()) { - Log.d(TAG, "Flattring all pending things."); - new FlattrClickWorker(context).executeAsync(); // flattr pending things + if (FlattrUtils.hasToken()) { + Log.d(TAG, "Flattring all pending things."); + new FlattrClickWorker(context).executeAsync(); // flattr pending things - Log.d(TAG, "Fetching flattr status."); - new FlattrStatusFetcher(context).start(); + Log.d(TAG, "Fetching flattr status."); + new FlattrStatusFetcher(context).start(); - } - if (ClientConfig.gpodnetCallbacks.gpodnetEnabled()) { - GpodnetSyncService.sendSyncIntent(context); - } - Log.d(TAG, "refreshAllFeeds autodownload"); - autodownloadUndownloadedItems(context); + } + if (ClientConfig.gpodnetCallbacks.gpodnetEnabled()) { + GpodnetSyncService.sendSyncIntent(context); + } + Log.d(TAG, "refreshAllFeeds autodownload"); + autodownloadUndownloadedItems(context); + + if (callback != null) { + callback.run(); + } + }).start(); } else { Log.d(TAG, "Ignoring request to refresh all feeds: Refresh lock is locked"); } @@ -344,7 +340,7 @@ public final class DBTasks { Log.d(TAG, "last refresh: " + Converter.getDurationStringLocalized(context, System.currentTimeMillis() - lastRefresh) + " ago"); if(lastRefresh <= System.currentTimeMillis() - interval) { - DBTasks.refreshAllFeeds(context, null); + DBTasks.refreshAllFeeds(context, null, null); } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/FeedUpdateUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/FeedUpdateUtils.java index a57ab2ce6..24e0da9ed 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/FeedUpdateUtils.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/FeedUpdateUtils.java @@ -11,13 +11,9 @@ public class FeedUpdateUtils { } - public static void startAutoUpdate(Context context, boolean synchronously) { + public static void startAutoUpdate(Context context, Runnable callback) { if (NetworkUtils.networkAvailable() && NetworkUtils.isDownloadAllowed()) { - if (synchronously) { - DBTasks.refreshAllFeedsSynchronously(context, null); - } else { - DBTasks.refreshAllFeeds(context, null); - } + DBTasks.refreshAllFeeds(context, null, callback); } else { Log.d(TAG, "Blocking automatic update: no wifi available / no mobile updates allowed"); }