Using callback instead of sync/async methods

This commit is contained in:
ByteHamster 2018-05-06 18:52:16 +02:00
parent a270d4fc03
commit 61f8000352
6 changed files with 37 additions and 46 deletions

View File

@ -224,7 +224,7 @@ public class AllEpisodesFragment extends Fragment {
case R.id.refresh_item:
List<Feed> 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:

View File

@ -285,7 +285,7 @@ public class QueueFragment extends Fragment {
case R.id.refresh_item:
List<Feed> feeds = ((MainActivity) getActivity()).getFeeds();
if (feeds != null) {
DBTasks.refreshAllFeeds(getActivity(), feeds);
DBTasks.refreshAllFeeds(getActivity(), feeds, null);
}
return true;
case R.id.clear_queue:

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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");
}