Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix service destroyed with activity #846

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -409,7 +431,11 @@ else if (errorMessage != null)
if (oldProcessingState != AudioProcessingState.idle && processingState == AudioProcessingState.idle) {
// TODO: Handle completed state as well?
stop();
} else if (processingState != AudioProcessingState.idle && notificationChanged) {
} else if (oldProcessingState == AudioProcessingState.idle && processingState != AudioProcessingState.idle) {
restart();
}

if (processingState != AudioProcessingState.idle && notificationChanged) {
updateNotification();
}
}
Expand Down Expand Up @@ -546,31 +572,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 @@ -714,32 +727,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