playback: remove media button reciever impl

Make MediaButtonReceiver an empty class without any behavior.

The previous manner in which Auxio would handle media button events
was actually bad, as in certain cases (like a rewind or if there was
no song playing) Auxio would actually crash, as the service would
not start a foreground state in time.

Fix this by removing that functionality. This should hopefully still
retain the current state.
This commit is contained in:
OxygenCobalt 2022-05-17 06:27:14 -06:00
parent 4a79de455a
commit 5381e9f9a2
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
4 changed files with 12 additions and 28 deletions

View file

@ -18,11 +18,8 @@
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.util.logD
/**
* Some apps like to party like it's 2011 and just blindly query for the ACTION_MEDIA_BUTTON intent
@ -30,17 +27,10 @@ import org.oxycblt.auxio.util.logD
* 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 that deliberately handles this event. This also means that Auxio will start
* without warning if you use the media buttons while the app exists, because I guess we just have
* to deal with this.
* BroadcastReceiver in the manifest that actually does nothing. Any broadcast by apps should be
* routed by the media session when the service exists.
* @author OxygenCobalt
*/
class MediaButtonReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_MEDIA_BUTTON) {
logD("Received external media button intent")
intent.component = ComponentName(context, PlaybackService::class.java)
ContextCompat.startForegroundService(context, intent)
}
}
override fun onReceive(context: Context, intent: Intent) {}
}

View file

@ -221,17 +221,16 @@ class MediaSessionComponent(private val context: Context, private val player: Pl
private fun invalidateSessionState() {
logD("Updating media session playback state")
// Position updates arrive faster when you upload STATE_PAUSED, as it resets the clock
// that updates the position.
// Position updates arrive faster when you upload a state that is different, as it
// forces the system to re-poll the position.
// FIXME: For some reason however, positions just DON'T UPDATE AT ALL when you
// change from FROM THE APP ONLY WHEN THE PLAYER IS PAUSED. AAAAAAAAAAAAAAAAAAAAAAAAAA
val state =
PlaybackStateCompat.Builder()
.setActions(ACTIONS)
.setBufferedPosition(player.bufferedPosition)
.setState(
PlaybackStateCompat.STATE_PAUSED,
player.currentPosition,
1.0f,
SystemClock.elapsedRealtime())
state.setState(PlaybackStateCompat.STATE_NONE, player.bufferedPosition, 1.0f)
mediaSession.setPlaybackState(state.build())

View file

@ -154,12 +154,6 @@ class PlaybackService :
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
if (intent.action == Intent.ACTION_MEDIA_BUTTON) {
// Workaround to get GadgetBridge and other apps that blindly query for
// ACTION_MEDIA_BUTTON working.
mediaSessionComponent.handleMediaButtonIntent(intent)
}
return START_NOT_STICKY
}
@ -285,7 +279,6 @@ class PlaybackService :
}
override fun onShuffledChanged(isShuffled: Boolean) {
logD("${settingsManager.useAltNotifAction}")
if (settingsManager.useAltNotifAction) {
notificationComponent.updateShuffled(isShuffled)
}

View file

@ -38,6 +38,7 @@ import org.oxycblt.auxio.util.getDimenSizeSafe
* widget state based off of that. This cannot be rolled into [WidgetProvider] directly, as it may
* result in memory leaks if [PlaybackStateManager]/[SettingsManager] gets created and bound to
* without being released.
* @author OxygenCobalt
*/
class WidgetComponent(private val context: Context) :
PlaybackStateManager.Callback, SettingsManager.Callback {
@ -55,7 +56,7 @@ class WidgetComponent(private val context: Context) :
* Force-update the widget.
*/
fun update() {
// Updating Auxio's widget is unlike the rest of Auxio for two reasons:
// Updating Auxio's widget is unlike the rest of Auxio for a few reasons:
// 1. We can't use the typical primitives like ViewModels
// 2. The component range is far smaller, so we have to do some odd hacks to get
// the same UX.
@ -67,6 +68,7 @@ class WidgetComponent(private val context: Context) :
return
}
// Note: Store these values here so they remain consistent once the bitmap is loaded.
val isPlaying = playbackManager.isPlaying
val repeatMode = playbackManager.repeatMode
val isShuffled = playbackManager.isShuffled