From 05fa29759db69a6b71ad9a9377d0a81d00419ae0 Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Sat, 9 Nov 2024 12:16:17 +0100 Subject: [PATCH] Start SleepAudioService as a foreground service to be able to request audio focus on Android 15: https://developer.android.com/about/versions/15/behavior-changes-15#audio-focus --- app/src/main/AndroidManifest.xml | 5 ++++- .../smarquis/sleeptimer/SleepAudioService.kt | 22 ++++++++++++++++--- app/src/main/res/values/values.xml | 4 ++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3e6bb5c..2d6fc27 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ xmlns:android="http://schemas.android.com/apk/res/android"> + - + \ No newline at end of file diff --git a/app/src/main/java/fr/smarquis/sleeptimer/SleepAudioService.kt b/app/src/main/java/fr/smarquis/sleeptimer/SleepAudioService.kt index 7d904e5..6e15573 100644 --- a/app/src/main/java/fr/smarquis/sleeptimer/SleepAudioService.kt +++ b/app/src/main/java/fr/smarquis/sleeptimer/SleepAudioService.kt @@ -1,9 +1,12 @@ package fr.smarquis.sleeptimer +import android.app.Notification +import android.app.Notification.CATEGORY_SERVICE import android.app.PendingIntent import android.app.PendingIntent.FLAG_IMMUTABLE import android.content.Context import android.content.Intent +import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVICE import android.media.AudioAttributes import android.media.AudioAttributes.CONTENT_TYPE_MUSIC import android.media.AudioAttributes.USAGE_MEDIA @@ -12,6 +15,8 @@ import android.media.AudioManager import android.media.AudioManager.ADJUST_LOWER import android.media.AudioManager.AUDIOFOCUS_GAIN import android.media.AudioManager.STREAM_MUSIC +import android.os.Build.VERSION.SDK_INT +import android.os.Build.VERSION_CODES.VANILLA_ICE_CREAM import fr.smarquis.sleeptimer.SleepTileService.Companion.requestTileUpdate import java.util.concurrent.TimeUnit.SECONDS @@ -23,11 +28,22 @@ class SleepAudioService : android.app.IntentService("SleepAudioService") { private val RESTORE_VOLUME_MILLIS = SECONDS.toMillis(2) private fun intent(context: Context) = Intent(context, SleepAudioService::class.java) - fun pendingIntent(context: Context): PendingIntent? = PendingIntent.getService(context, 0, intent(context), FLAG_IMMUTABLE) + fun pendingIntent(context: Context): PendingIntent? = PendingIntent.getForegroundService(context, 123, intent(context), FLAG_IMMUTABLE) + + private fun notification(context: Context) = Notification.Builder(context, context.getString(R.string.notification_channel_id)) + .setCategory(CATEGORY_SERVICE) + .setSmallIcon(R.drawable.ic_tile) + .build() + } + + private fun withForegroundService(block: AudioManager.() -> Unit) { + if (SDK_INT >= VANILLA_ICE_CREAM) startForeground(R.id.service_id, notification(applicationContext), FOREGROUND_SERVICE_TYPE_SHORT_SERVICE) + getSystemService(AudioManager::class.java)?.run(block) + if (SDK_INT >= VANILLA_ICE_CREAM) stopForeground(STOP_FOREGROUND_REMOVE) } @Deprecated("Deprecated in Java") - override fun onHandleIntent(intent: Intent?) = getSystemService(AudioManager::class.java)?.run { + override fun onHandleIntent(intent: Intent?) = withForegroundService { val volumeIndex = getStreamVolume(STREAM_MUSIC) // fade out volume @@ -48,6 +64,6 @@ class SleepAudioService : android.app.IntentService("SleepAudioService") { // update tile requestTileUpdate() - } ?: Unit + } } \ No newline at end of file diff --git a/app/src/main/res/values/values.xml b/app/src/main/res/values/values.xml index a81d298..e0a0e76 100644 --- a/app/src/main/res/values/values.xml +++ b/app/src/main/res/values/values.xml @@ -11,4 +11,8 @@ sleep_timer + + + + \ No newline at end of file