From 37e270071a8d1db209403d2a67d8721268456bca Mon Sep 17 00:00:00 2001 From: Hugh Daschbach Date: Fri, 3 Jun 2022 17:10:03 -0700 Subject: [PATCH] Fix Bluetooth control button unresponsiveness. With Oreo and later, Bluetooth control buttons may kill FFA if it is not the foreground application. Once this happens to resume playback, one needs to restart playback from the phone, rather than the play/pause action of Bluetooth headset. For example: D MediaSessionService: Sending KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MEDIA_PLAY, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=0, downTime=0, deviceId=-1, source=0x0 } to audio.funkwhale.ffa.dev/audio.funkwhale.ffa.dev ( W ActivityManager: Background start not allowed: service Intent { act=android.intent.action.MEDIA_BUTTON cmp=audio.funkwhale.ffa.dev/audio.funkwhale.ffa.playback.PlayerService (has extras) } to audio.funkwhale.ffa.dev/audio.funkwhale.ffa.play 549 uid=10149 pkg=audio.funkwhale.ffa.dev startFg?=false D AndroidRuntime: Shutting down VM --------- beginning of crash E AndroidRuntime: FATAL EXCEPTION: main E AndroidRuntime: Process: audio.funkwhale.ffa.dev, PID: 14549 E AndroidRuntime: java.lang.IllegalStateException: Not allowed to start service Intent { act=android.intent.action.MEDIA_BUTTON cmp=audio.funkwhale.ffa.dev/audio.funkwhale.ffa.playback.PlayerService (has extras) }: app is in background uid UidRecord{72fa8f8 u0a149 CAC bg:+11m56s597ms idle change:cached procs:1 seq(0,0,0)} E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577) E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1532) E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:664) E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2.invoke$lambda-3$lambda-2(MediaSession.kt:47) E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2.$r8$lambda$jU84j_zRyeYuvwLrRY0b6XyQBMs(Unknown Source:0) E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2$$ExternalSyntheticLambda0.onMediaButtonEvent(Unknown Source:2) E AndroidRuntime: at com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector$ComponentListener.onMediaButtonEvent(MediaSessionConnector.java:1396) E AndroidRuntime: at android.support.v4.media.session.MediaSessionCompat$Callback$MediaSessionCallbackApi21.onMediaButtonEvent(MediaSessionCompat.java:1602) E AndroidRuntime: at android.media.session.MediaSession$CallbackMessageHandler.handleMessage(MediaSession.java:1471) E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106) E AndroidRuntime: at android.os.Looper.loop(Looper.java:193) E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6718) E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491) E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) W ActivityManager: Force finishing activity audio.funkwhale.ffa.dev/audio.funkwhale.ffa.activities.MainActivity xref: https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten --- .../audio/funkwhale/ffa/playback/MediaSession.kt | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/audio/funkwhale/ffa/playback/MediaSession.kt b/app/src/main/java/audio/funkwhale/ffa/playback/MediaSession.kt index 9b3a4eb..8898309 100644 --- a/app/src/main/java/audio/funkwhale/ffa/playback/MediaSession.kt +++ b/app/src/main/java/audio/funkwhale/ffa/playback/MediaSession.kt @@ -2,6 +2,7 @@ package audio.funkwhale.ffa.playback import android.content.Context import android.content.Intent +import android.os.Build import android.os.Bundle import android.os.ResultReceiver import android.support.v4.media.session.MediaSessionCompat @@ -44,13 +45,17 @@ class MediaSession(private val context: Context) { it.setMediaButtonEventHandler { _, _, intent -> if (!active) { - context.startService( - Intent(context, PlayerService::class.java).apply { - action = intent.action + Intent(context, PlayerService::class.java).let { player -> + player.action = intent.action - intent.extras?.let { extras -> putExtras(extras) } + intent.extras?.let { extras -> player.putExtras(extras) } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(player) + } else { + context.startService(player) } - ) + } return@setMediaButtonEventHandler true }