playback: fix gadgetbridge regression [#62]

Fix a regression where media button controls would no longer work
with GadgetBridge.

In 4a79de4, I assumed that my code was redundant, as there should be
some kind of reciever handling media button events already. I was
completely wrong. Re-add the reciever code (with extra checks) to
make controls work again.
This commit is contained in:
OxygenCobalt 2022-06-01 12:03:05 -06:00
parent a65d37c421
commit a64a4864bd
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
4 changed files with 25 additions and 5 deletions

View file

@ -75,7 +75,6 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-common:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
// Navigation

View file

@ -18,8 +18,11 @@
package org.oxycblt.auxio.playback.system
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import androidx.core.content.ContextCompat
import org.oxycblt.auxio.playback.state.PlaybackStateManager
/**
* Some apps like to party like it's 2011 and just blindly query for the ACTION_MEDIA_BUTTON intent
@ -27,9 +30,16 @@ import android.content.Intent
* MediaSession that an app should control instead through the much better MediaController API. But
* who cares about that, we need to make sure the 3% of barely functioning TouchWiz devices running
* KitKat don't break! To prevent Auxio from not showing up at all in these apps, we declare a
* BroadcastReceiver in the manifest that actually does nothing. Any broadcast by apps should be
* routed by the media session when the service exists.
* BroadcastReceiver in the manifest that hacks in this functionality.
*/
class MediaButtonReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {}
override fun onReceive(context: Context, intent: Intent) {
val playbackManager = PlaybackStateManager.getInstance()
if (playbackManager.song != null) {
// We have a song, so we can assume that the service will start a foreground state.
// At least, I hope
intent.component = ComponentName(context, PlaybackService::class.java)
ContextCompat.startForegroundService(context, intent)
}
}
}

View file

@ -24,6 +24,7 @@ import android.os.SystemClock
import android.support.v4.media.MediaMetadataCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import androidx.media.session.MediaButtonReceiver
import com.google.android.exoplayer2.Player
import org.oxycblt.auxio.image.BitmapProvider
import org.oxycblt.auxio.music.MusicParent
@ -57,6 +58,10 @@ class MediaSessionComponent(private val context: Context, private val player: Pl
mediaSession.setCallback(this)
}
fun handleMediaButtonIntent(intent: Intent) {
MediaButtonReceiver.handleIntent(mediaSession, intent)
}
fun release() {
provider.release()
player.removeListener(this)

View file

@ -147,7 +147,13 @@ class PlaybackService :
logD("Service created")
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int) = START_NOT_STICKY
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
if (intent.action == Intent.ACTION_MEDIA_BUTTON) {
mediaSessionComponent.handleMediaButtonIntent(intent)
}
return START_NOT_STICKY
}
// No binding, service is headless
// Communicate using PlaybackStateManager, SettingsManager, or Broadcasts instead.