From 5381e9f9a2f911d5265416209054e384c4d26806 Mon Sep 17 00:00:00 2001 From: OxygenCobalt Date: Tue, 17 May 2022 06:27:14 -0600 Subject: [PATCH] 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. --- .../auxio/playback/system/MediaButtonReceiver.kt | 16 +++------------- .../playback/system/MediaSessionComponent.kt | 13 ++++++------- .../auxio/playback/system/PlaybackService.kt | 7 ------- .../org/oxycblt/auxio/widgets/WidgetComponent.kt | 4 +++- 4 files changed, 12 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/org/oxycblt/auxio/playback/system/MediaButtonReceiver.kt b/app/src/main/java/org/oxycblt/auxio/playback/system/MediaButtonReceiver.kt index 4d16ae095..6525bcdbc 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/system/MediaButtonReceiver.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/system/MediaButtonReceiver.kt @@ -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) {} } diff --git a/app/src/main/java/org/oxycblt/auxio/playback/system/MediaSessionComponent.kt b/app/src/main/java/org/oxycblt/auxio/playback/system/MediaSessionComponent.kt index a647b52b6..f13405ac7 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/system/MediaSessionComponent.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/system/MediaSessionComponent.kt @@ -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()) diff --git a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt index aca16393a..7f47b5c17 100644 --- a/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/system/PlaybackService.kt @@ -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) } diff --git a/app/src/main/java/org/oxycblt/auxio/widgets/WidgetComponent.kt b/app/src/main/java/org/oxycblt/auxio/widgets/WidgetComponent.kt index 7e1ff844b..5fd73c216 100644 --- a/app/src/main/java/org/oxycblt/auxio/widgets/WidgetComponent.kt +++ b/app/src/main/java/org/oxycblt/auxio/widgets/WidgetComponent.kt @@ -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