From e920dcd058e1263016da1bf4d8888ae592983762 Mon Sep 17 00:00:00 2001 From: Tim Malseed Date: Wed, 30 Oct 2019 22:32:25 +1100 Subject: [PATCH] Dispose of disposables when service is destroyed --- .../notifications/MusicNotificationHelper.java | 11 +++++++++-- .../simplecity/amp_library/playback/MusicService.java | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/simplecity/amp_library/notifications/MusicNotificationHelper.java b/app/src/main/java/com/simplecity/amp_library/notifications/MusicNotificationHelper.java index e239c3546..64240a958 100644 --- a/app/src/main/java/com/simplecity/amp_library/notifications/MusicNotificationHelper.java +++ b/app/src/main/java/com/simplecity/amp_library/notifications/MusicNotificationHelper.java @@ -31,6 +31,7 @@ import com.simplecity.amp_library.utils.PlaceholderProvider; import com.simplecity.amp_library.utils.PlaylistUtils; import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; import io.reactivex.schedulers.Schedulers; import java.util.ConcurrentModificationException; @@ -48,6 +49,8 @@ public class MusicNotificationHelper extends NotificationHelper { private Handler handler; + private CompositeDisposable compositeDisposable = new CompositeDisposable(); + public MusicNotificationHelper(Context context) { super(context); @@ -106,7 +109,7 @@ public void notify(Context context, @NonNull Song song, boolean isPlaying, @NonN notification = getBuilder(context, song, mediaSessionToken, bitmap, isPlaying, isFavorite).build(); notify(NOTIFICATION_ID, notification); - PlaylistUtils.isFavorite(song) + compositeDisposable.add(PlaylistUtils.isFavorite(song) .first(false) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) @@ -116,7 +119,7 @@ public void notify(Context context, @NonNull Song song, boolean isPlaying, @NonN notify(notification); }, error -> { LogUtils.logException(TAG, "MusicNotificationHelper failed to present notification", error); - }); + })); handler.post(() -> Glide.with(context) .load(song) @@ -172,4 +175,8 @@ public void notify(Notification notification) { public void cancel() { super.cancel(NOTIFICATION_ID); } + + public void tearDown() { + compositeDisposable.clear(); + } } diff --git a/app/src/main/java/com/simplecity/amp_library/playback/MusicService.java b/app/src/main/java/com/simplecity/amp_library/playback/MusicService.java index 19f33c0ee..424aa7a39 100644 --- a/app/src/main/java/com/simplecity/amp_library/playback/MusicService.java +++ b/app/src/main/java/com/simplecity/amp_library/playback/MusicService.java @@ -281,6 +281,7 @@ public void onDestroy() { playbackManager.destroy(); dummyNotificationHelper.teardown(this); + notificationHelper.tearDown(); disposables.clear();