diff options
author | ByteHamster <info@bytehamster.com> | 2019-10-06 09:11:42 +0200 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2019-10-06 09:11:45 +0200 |
commit | 69e51c148e601738b9faffbc76f76923431b8138 (patch) | |
tree | d09e2849245c97df40210b199d54e291d5f98cd3 /core/src/main/java | |
parent | 53ea702772817693193706c488c3d1777fbb4704 (diff) | |
download | AntennaPod-69e51c148e601738b9faffbc76f76923431b8138.zip |
Moved notification setup out of Thread
Fixes wrong thread for media player. Also, might fix race conditions.
Diffstat (limited to 'core/src/main/java')
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java | 77 |
1 files changed, 35 insertions, 42 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 843dd0843..899e0f7b1 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -1187,59 +1187,52 @@ public class PlaybackService extends MediaBrowserServiceCompat { if (notificationSetupThread != null) { notificationSetupThread.interrupt(); } - if (playable == null) { - Log.d(TAG, "setupNotification: playable is null" + Log.getStackTraceString(new Exception())); + if (playable == null || mediaPlayer == null) { + Log.d(TAG, "setupNotification: playable=" + playable); + Log.d(TAG, "setupNotification: mediaPlayer=" + mediaPlayer); if (!stateManager.hasReceivedValidStartCommand()) { stateManager.stopService(); } return; } - Runnable notificationSetupTask = new Runnable() { - @Override - public void run() { - Log.d(TAG, "Starting background work"); - if (mediaPlayer == null) { - Log.d(TAG, "notificationSetupTask: mediaPlayer is null"); - if (!stateManager.hasReceivedValidStartCommand()) { - stateManager.stopService(); - } - return; - } - PlayerStatus playerStatus = mediaPlayer.getPlayerStatus(); - notificationBuilder = new PlaybackServiceNotificationBuilder(PlaybackService.this); - notificationBuilder.setMetadata(playable, mediaSession.getSessionToken(), playerStatus, isCasting); + PlayerStatus playerStatus = mediaPlayer.getPlayerStatus(); + notificationBuilder = new PlaybackServiceNotificationBuilder(PlaybackService.this); + notificationBuilder.setMetadata(playable, mediaSession.getSessionToken(), playerStatus, isCasting); + if (Build.VERSION.SDK_INT < 29) { + notificationBuilder.updatePosition(getCurrentPosition(), getCurrentPlaybackSpeed()); + } - if (Build.VERSION.SDK_INT < 29) { - notificationBuilder.updatePosition(getCurrentPosition(), getCurrentPlaybackSpeed()); - } + if (notificationBuilder.isIconCached(playable)) { + notificationBuilder.loadIcon(playable); + startForegroundIfPlaying(playerStatus); + } else { + // To make sure that the notification is shown instantly + notificationBuilder.loadDefaultIcon(); + stateManager.startForeground(NOTIFICATION_ID, notificationBuilder.build()); - if (!notificationBuilder.isIconCached(playable)) { - // To make sure that the notification is shown instantly - notificationBuilder.loadDefaultIcon(); - stateManager.startForeground(NOTIFICATION_ID, notificationBuilder.build()); - } + notificationSetupThread = new Thread(() -> { + Log.d(TAG, "Loading notification icon"); notificationBuilder.loadIcon(playable); - - if (!Thread.currentThread().isInterrupted() && stateManager.hasReceivedValidStartCommand()) { - Notification notification = notificationBuilder.build(); - - if (playerStatus == PlayerStatus.PLAYING || - playerStatus == PlayerStatus.PREPARING || - playerStatus == PlayerStatus.SEEKING || - isCasting) { - stateManager.startForeground(NOTIFICATION_ID, notification); - } else { - stateManager.stopForeground(false); - NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - mNotificationManager.notify(NOTIFICATION_ID, notification); - } - Log.d(TAG, "Notification set up"); + if (!Thread.currentThread().isInterrupted()) { + startForegroundIfPlaying(playerStatus); } + }); + notificationSetupThread.start(); + } + } + + private void startForegroundIfPlaying(@NonNull PlayerStatus status) { + if (stateManager.hasReceivedValidStartCommand()) { + if (isCasting || status == PlayerStatus.PLAYING || status == PlayerStatus.PREPARING + || status == PlayerStatus.SEEKING) { + stateManager.startForeground(NOTIFICATION_ID, notificationBuilder.build()); + } else { + stateManager.stopForeground(false); + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); + notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); } - }; - notificationSetupThread = new Thread(notificationSetupTask); - notificationSetupThread.start(); + } } /** |