diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/receiver/MediaButtonIntentReceiver.java b/ultrasonic/src/main/java/org/moire/ultrasonic/receiver/MediaButtonIntentReceiver.java index d36cab6c..171fe2ff 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/receiver/MediaButtonIntentReceiver.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/receiver/MediaButtonIntentReceiver.java @@ -25,6 +25,7 @@ import android.os.Build; import android.os.Bundle; import android.os.Parcelable; import android.util.Log; +import android.view.KeyEvent; import org.moire.ultrasonic.service.DownloadServiceImpl; import org.moire.ultrasonic.util.Util; @@ -62,16 +63,36 @@ public class MediaButtonIntentReceiver extends BroadcastReceiver try { context.startService(serviceIntent); + } + catch (IllegalStateException exception) + { + Log.i(TAG, "MediaButtonIntentReceiver couldn't start DownloadServiceImpl because the application was in the background."); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + { + KeyEvent keyEvent = (KeyEvent) event; + if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && keyEvent.getRepeatCount() == 0) + { + int keyCode = keyEvent.getKeyCode(); + if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || + keyCode == KeyEvent.KEYCODE_HEADSETHOOK || + keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) + { + // TODO: The only time it is OK to start DownloadServiceImpl as a foreground service is when we now it will display its notification. + // When DownloadServiceImpl is refactored to a proper foreground service, this can be removed. + context.startForegroundService(serviceIntent); + Log.i(TAG, "MediaButtonIntentReceiver started DownloadServiceImpl as foreground service"); + } + } + } + } + try + { if (isOrderedBroadcast()) { abortBroadcast(); } } - catch (IllegalStateException exception) - { - Log.w(TAG, "MediaButtonIntentReceiver couldn't start DownloadServiceImpl because the application was in the background."); - } catch (Exception x) { // Ignored. diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/service/DownloadServiceImpl.java b/ultrasonic/src/main/java/org/moire/ultrasonic/service/DownloadServiceImpl.java index ec3d73e7..309eb8e4 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/service/DownloadServiceImpl.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/service/DownloadServiceImpl.java @@ -752,6 +752,7 @@ public class DownloadServiceImpl extends Service implements DownloadService if (tabInstance != null) { stopForeground(true); + clearRemoteControl(); isInForeground = false; tabInstance.hideNowPlaying(); } @@ -1277,6 +1278,7 @@ public class DownloadServiceImpl extends Service implements DownloadService if (tabInstance != null) { stopForeground(true); + clearRemoteControl(); isInForeground = false; tabInstance.hideNowPlaying(); } diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/service/DownloadServiceLifecycleSupport.java b/ultrasonic/src/main/java/org/moire/ultrasonic/service/DownloadServiceLifecycleSupport.java index 1ae1eaeb..265bce86 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/service/DownloadServiceLifecycleSupport.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/service/DownloadServiceLifecycleSupport.java @@ -289,6 +289,7 @@ public class DownloadServiceLifecycleSupport return; } Log.i(TAG, "Deserialized currentPlayingIndex: " + state.currentPlayingIndex + ", currentPlayingPosition: " + state.currentPlayingPosition); + // TODO: here the autoPlay = false creates problems when Ultrasonic is started by a Play MediaButton as the player won't start this way. downloadService.restore(state.songs, state.currentPlayingIndex, state.currentPlayingPosition, false, false); // Work-around: Serialize again, as the restore() method creates a serialization without current playing info. @@ -321,7 +322,11 @@ public class DownloadServiceLifecycleSupport downloadService.stop(); break; case KeyEvent.KEYCODE_MEDIA_PLAY: - if (downloadService.getPlayerState() != PlayerState.STARTED) + if (downloadService.getPlayerState() == PlayerState.IDLE) + { + downloadService.play(); + } + else if (downloadService.getPlayerState() != PlayerState.STARTED) { downloadService.start(); }