EarhornMediaButtonReceiver.kt
24 lines · 1.1 KB
Changed in: 0.5.0
| 1 | package dev.stave.playback |
| 2 | |
| 3 | import android.content.Intent |
| 4 | import androidx.annotation.OptIn |
| 5 | import androidx.media3.common.util.UnstableApi |
| 6 | import androidx.media3.session.MediaButtonReceiver |
| 7 | import dev.stave.data.QueuePersistenceStore |
| 8 | |
| 9 | /** |
| 10 | * Maintainer, 2026-10-29: headset / Bluetooth / car Play while Earhorn isn't |
| 11 | * running. Media3's receiver starts PlaybackService as a FOREGROUND service; |
| 12 | * if there is nothing to resume the player stays empty, no notification is |
| 13 | * posted, and Android kills the app with ForegroundServiceDidNotStartInTime. |
| 14 | * So only start the service when the last saved queue has something playable. |
| 15 | */ |
| 16 | @OptIn(UnstableApi::class) |
| 17 | class EarhornMediaButtonReceiver : MediaButtonReceiver() { |
| 18 | override fun shouldStartForegroundService(intent: Intent): Boolean { |
| 19 | // shouldStartForegroundService gets no Context, and onReceive is final; |
| 20 | // the Application is always created before a receiver runs. |
| 21 | val app = dev.stave.EarhornApp.instance ?: return false |
| 22 | return QueuePersistenceStore.hasResumableQueue(app) |
| 23 | } |
| 24 | } |


