Moved notification setup out of Thread
Fixes wrong thread for media player. Also, might fix race conditions.
This commit is contained in:
parent
53ea702772
commit
69e51c148e
@ -1187,59 +1187,52 @@ public class PlaybackService extends MediaBrowserServiceCompat {
|
|||||||
if (notificationSetupThread != null) {
|
if (notificationSetupThread != null) {
|
||||||
notificationSetupThread.interrupt();
|
notificationSetupThread.interrupt();
|
||||||
}
|
}
|
||||||
if (playable == null) {
|
if (playable == null || mediaPlayer == null) {
|
||||||
Log.d(TAG, "setupNotification: playable is null" + Log.getStackTraceString(new Exception()));
|
Log.d(TAG, "setupNotification: playable=" + playable);
|
||||||
|
Log.d(TAG, "setupNotification: mediaPlayer=" + mediaPlayer);
|
||||||
if (!stateManager.hasReceivedValidStartCommand()) {
|
if (!stateManager.hasReceivedValidStartCommand()) {
|
||||||
stateManager.stopService();
|
stateManager.stopService();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Runnable notificationSetupTask = new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
Log.d(TAG, "Starting background work");
|
|
||||||
|
|
||||||
if (mediaPlayer == null) {
|
PlayerStatus playerStatus = mediaPlayer.getPlayerStatus();
|
||||||
Log.d(TAG, "notificationSetupTask: mediaPlayer is null");
|
notificationBuilder = new PlaybackServiceNotificationBuilder(PlaybackService.this);
|
||||||
if (!stateManager.hasReceivedValidStartCommand()) {
|
notificationBuilder.setMetadata(playable, mediaSession.getSessionToken(), playerStatus, isCasting);
|
||||||
stateManager.stopService();
|
if (Build.VERSION.SDK_INT < 29) {
|
||||||
}
|
notificationBuilder.updatePosition(getCurrentPosition(), getCurrentPlaybackSpeed());
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
PlayerStatus playerStatus = mediaPlayer.getPlayerStatus();
|
|
||||||
notificationBuilder = new PlaybackServiceNotificationBuilder(PlaybackService.this);
|
|
||||||
notificationBuilder.setMetadata(playable, mediaSession.getSessionToken(), playerStatus, isCasting);
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT < 29) {
|
if (notificationBuilder.isIconCached(playable)) {
|
||||||
notificationBuilder.updatePosition(getCurrentPosition(), getCurrentPlaybackSpeed());
|
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)) {
|
notificationSetupThread = new Thread(() -> {
|
||||||
// To make sure that the notification is shown instantly
|
Log.d(TAG, "Loading notification icon");
|
||||||
notificationBuilder.loadDefaultIcon();
|
|
||||||
stateManager.startForeground(NOTIFICATION_ID, notificationBuilder.build());
|
|
||||||
}
|
|
||||||
notificationBuilder.loadIcon(playable);
|
notificationBuilder.loadIcon(playable);
|
||||||
|
if (!Thread.currentThread().isInterrupted()) {
|
||||||
if (!Thread.currentThread().isInterrupted() && stateManager.hasReceivedValidStartCommand()) {
|
startForegroundIfPlaying(playerStatus);
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user