diff --git a/app/build.gradle b/app/build.gradle index 234bc465e..a858f4898 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 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 a2fe9bf1c..04892ef9f 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,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) + } + } } 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 be2f0466a..94f5632ed 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 @@ -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) 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 383b42f56..aa6f660b3 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 @@ -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.