Skip to content

Commit

Permalink
Fix service destroyed with activity
Browse files Browse the repository at this point in the history
  • Loading branch information
nt4f04uNd committed Oct 12, 2021
1 parent a55f565 commit fe16ec2
Showing 1 changed file with 35 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,28 @@ public int onStartCommand(final Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}

/**
* Restarts the foreground service from the idle state.
*
* The opposite of this method is {@link #stop()}.
*/
public void restart() {
ContextCompat.startForegroundService(this, new Intent(AudioService.this, AudioService.class));
activateMediaSession();
mediaSession.setSessionActivity(contentIntent);
}

/**
* Tries to stop the service.
*
* The service will not be stopped, if it's in foreground state,
* or there are active {@link MediaBrowserCompat.ConnectionCallback}.
*
* If there are callbacks, the service will be destroyed as soon as they disconnect
* (not taking to account the foreground state).
* To prevent this, {@link #restart()} should be called, which is the
* opposite of this method.
*/
public void stop() {
deactivateMediaSession();
stopSelf();
Expand Down Expand Up @@ -398,7 +420,11 @@ else if (errorMessage != null)
if (oldProcessingState != AudioProcessingState.idle && processingState == AudioProcessingState.idle) {
// TODO: Handle completed state as well?
stop();
} else if (processingState != AudioProcessingState.idle) {
} else if (oldProcessingState == AudioProcessingState.idle && processingState != AudioProcessingState.idle) {
restart();
}

if (processingState != AudioProcessingState.idle) {
updateNotification();
}
}
Expand Down Expand Up @@ -529,31 +555,18 @@ private void updateNotification() {
}

private void enterPlayingState() {
ContextCompat.startForegroundService(this, new Intent(AudioService.this, AudioService.class));
if (!mediaSession.isActive())
mediaSession.setActive(true);

acquireWakeLock();
mediaSession.setSessionActivity(contentIntent);
internalStartForeground();
startForeground(NOTIFICATION_ID, buildNotification());
notificationCreated = true;
}

private void exitPlayingState() {
if (config.androidStopForegroundOnPause) {
exitForegroundState();
stopForeground(false);
releaseWakeLock();
}
}

private void exitForegroundState() {
stopForeground(false);
releaseWakeLock();
}

private void internalStartForeground() {
startForeground(NOTIFICATION_ID, buildNotification());
notificationCreated = true;
}

private void acquireWakeLock() {
if (!wakeLock.isHeld())
wakeLock.acquire();
Expand Down Expand Up @@ -685,32 +698,28 @@ public void onRemoveQueueItem(MediaDescriptionCompat description) {
@Override
public void onPrepare() {
if (listener == null) return;
if (!mediaSession.isActive())
mediaSession.setActive(true);
activateMediaSession();
listener.onPrepare();
}

@Override
public void onPrepareFromMediaId(String mediaId, Bundle extras) {
if (listener == null) return;
if (!mediaSession.isActive())
mediaSession.setActive(true);
activateMediaSession();
listener.onPrepareFromMediaId(mediaId, extras);
}

@Override
public void onPrepareFromSearch(String query, Bundle extras) {
if (listener == null) return;
if (!mediaSession.isActive())
mediaSession.setActive(true);
activateMediaSession();
listener.onPrepareFromSearch(query, extras);
}

@Override
public void onPrepareFromUri(Uri uri, Bundle extras) {
if (listener == null) return;
if (!mediaSession.isActive())
mediaSession.setActive(true);
activateMediaSession();
listener.onPrepareFromUri(uri, extras);
}

Expand Down

0 comments on commit fe16ec2

Please sign in to comment.