Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start SleepAudioService as a foreground service to be able to request audio focus #111

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
android:allowBackup="false"
Expand All @@ -28,7 +29,9 @@
android:name="android.service.quicksettings.TOGGLEABLE_TILE"
android:value="true" />
</service>
<service android:name=".SleepAudioService" />
<service
android:name=".SleepAudioService"
android:foregroundServiceType="shortService" />
</application>

</manifest>
22 changes: 19 additions & 3 deletions app/src/main/java/fr/smarquis/sleeptimer/SleepAudioService.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -48,6 +64,6 @@ class SleepAudioService : android.app.IntentService("SleepAudioService") {

// update tile
requestTileUpdate()
} ?: Unit
}

}
4 changes: 4 additions & 0 deletions app/src/main/res/values/values.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<string name="notification_channel_id">sleep_timer</string>
<!--endregion-->

<!--region Service-->
<item name="service_id" type="id" />
<!--endregion-->

</resources>
Loading