diff options
author | ByteHamster <info@bytehamster.com> | 2018-05-06 18:52:16 +0200 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2018-05-06 18:52:16 +0200 |
commit | 61f8000352d80eef4687a66e0d6174637469adc2 (patch) | |
tree | 7d3869541d86f60102dd574293191b592f540225 /core | |
parent | a270d4fc031caba89909fcd710849a6ba18f326b (diff) | |
download | AntennaPod-61f8000352d80eef4687a66e0d6174637469adc2.zip |
Using callback instead of sync/async methods
Diffstat (limited to 'core')
4 files changed, 35 insertions, 44 deletions
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<Feed> 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<Feed> feeds) { + public static void refreshAllFeeds(final Context context, final List<Feed> 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"); } |