Skip to content

Commit

Permalink
Merge pull request #470 from AdrienPoupa/crash-repeated-restart
Browse files Browse the repository at this point in the history
Flush the Discog task queue on stop
  • Loading branch information
AdrienPoupa authored Jun 28, 2021
2 parents 5fba2c2 + e677e0f commit 0718174
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Discography implements MusicServiceEventListener {

private MainActivity mainActivity = null;
private Handler mainActivityTaskQueue = null;
private final String TASK_QUEUE_COALESCENCE_TOKEN = "Discography.triggerSyncWithMediaStore";
private final Collection<Runnable> changedListeners = new LinkedList<>();

public Discography() {
Expand All @@ -70,6 +71,9 @@ public void startService(@NonNull final MainActivity mainActivity) {
}

public void stopService() {
// Flush the delayed task queue - dont do anything is we are stopping
mainActivityTaskQueue.removeCallbacksAndMessages(TASK_QUEUE_COALESCENCE_TOKEN);

mainActivity = null;
mainActivityTaskQueue = null;
}
Expand Down Expand Up @@ -266,10 +270,9 @@ public void triggerSyncWithMediaStore(boolean reset) {
// Prevent reentrance - delay to later
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
final long DELAY = 500;
final String COALESCENCE_TOKEN = "Discography::triggerSyncWithMediaStore";

mainActivityTaskQueue.removeCallbacksAndMessages(COALESCENCE_TOKEN);
mainActivityTaskQueue.postDelayed(() -> triggerSyncWithMediaStore(reset), COALESCENCE_TOKEN, DELAY);
mainActivityTaskQueue.removeCallbacksAndMessages(TASK_QUEUE_COALESCENCE_TOKEN);
mainActivityTaskQueue.postDelayed(() -> triggerSyncWithMediaStore(reset), TASK_QUEUE_COALESCENCE_TOKEN, DELAY);
} // else: too bad, just drop the operation. It is unlikely we get there anyway
} else {
(new SyncWithMediaStoreAsyncTask(mainActivity, this)).execute(reset);
Expand Down

0 comments on commit 0718174

Please sign in to comment.