From eeb032e9382535e544eb16ea74ae9733302483df Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Fri, 4 May 2018 22:41:53 +0200 Subject: Notify system when job is done --- .../core/receiver/FeedUpdateReceiver.java | 2 +- .../core/service/FeedUpdateJobService.java | 9 +++- .../de/danoeh/antennapod/core/storage/DBTasks.java | 56 ++++++++++++---------- .../antennapod/core/util/FeedUpdateUtils.java | 8 +++- 4 files changed, 45 insertions(+), 30 deletions(-) (limited to 'core') 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 f1a316954..67f6d9348 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); + FeedUpdateUtils.startAutoUpdate(context, false); 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 2d418218d..3fc3551ee 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 @@ -17,8 +17,13 @@ public class FeedUpdateJobService extends JobService { public boolean onStartJob(JobParameters params) { Log.d(TAG, "Job started"); ClientConfig.initialize(getApplicationContext()); - FeedUpdateUtils.startAutoUpdate(getApplicationContext()); - UserPreferences.restartUpdateAlarm(false); + + new Thread(() -> { + FeedUpdateUtils.startAutoUpdate(getApplicationContext(), true); + 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 b6a203cb8..b1937985f 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 @@ -151,36 +151,42 @@ public final class DBTasks { * @param context Might be used for accessing the database * @param feeds List of Feeds that should be refreshed. */ - public static void refreshAllFeeds(final Context context, - final List feeds) { + 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) { if (isRefreshing.compareAndSet(false, true)) { - new Thread() { - public void run() { - if (feeds != null) { - refreshFeeds(context, feeds); - } else { - refreshFeeds(context, DBReader.getFeedList()); - } - isRefreshing.set(false); + 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); - } - }.start(); + } + if (ClientConfig.gpodnetCallbacks.gpodnetEnabled()) { + GpodnetSyncService.sendSyncIntent(context); + } + Log.d(TAG, "refreshAllFeeds autodownload"); + autodownloadUndownloadedItems(context); } else { Log.d(TAG, "Ignoring request to refresh all feeds: Refresh lock is locked"); } 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 4d2dde88c..a57ab2ce6 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,9 +11,13 @@ public class FeedUpdateUtils { } - public static void startAutoUpdate(Context context) { + public static void startAutoUpdate(Context context, boolean synchronously) { if (NetworkUtils.networkAvailable() && NetworkUtils.isDownloadAllowed()) { - DBTasks.refreshAllFeeds(context, null); + if (synchronously) { + DBTasks.refreshAllFeedsSynchronously(context, null); + } else { + DBTasks.refreshAllFeeds(context, null); + } } else { Log.d(TAG, "Blocking automatic update: no wifi available / no mobile updates allowed"); } -- cgit v1.2.3