Skip to content

Commit 267c0c6

Browse files
feat: Add SleepNotificationReceiver and update dependencies
- Added `SleepNotificationReceiver` to handle notification actions for Android versions below N. - Updated `SleepNotification` to use `SleepNotificationReceiver` for older Android versions and `SleepTileService` for Android N and above. - Registered `SleepNotificationReceiver` in `AndroidManifest.xml`. - Updated Gradle to 9.1.0. - Updated `firebaseBom` to 34.3.0. - Updated `workRuntimeKtx` to 2.10.5.
1 parent 644ca2a commit 267c0c6

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
android:value="true" />
3838
</service>
3939

40+
<receiver
41+
android:name=".receivers.SleepNotificationReceiver"
42+
android:exported="false" />
43+
4044
<receiver
4145
android:name=".receivers.SleepAudioReceiver"
4246
android:exported="false">

app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/notifications/SleepNotification.kt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.CANC
1717
import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.DECREMENT
1818
import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.INCREMENT
1919
import com.d4rk.musicsleeptimer.plus.workers.SleepAudioWorker
20+
import androidx.annotation.RequiresApi
21+
import com.d4rk.musicsleeptimer.plus.receivers.SleepNotificationReceiver
2022
import com.d4rk.musicsleeptimer.plus.services.SleepTileService
2123
import java.lang.System.currentTimeMillis
2224
import java.text.DateFormat
@@ -49,9 +51,29 @@ object SleepNotification {
4951
fun parse(value : String?) : Action? = entries.firstOrNull { it.value == value }
5052
}
5153

52-
fun intent(context : Context) : Intent = Intent(context , SleepTileService::class.java).setAction(value)
54+
fun intent(context : Context) : Intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
55+
serviceIntent(context)
56+
} else {
57+
broadcastIntent(context)
58+
}
59+
60+
@RequiresApi(Build.VERSION_CODES.N)
61+
private fun serviceIntent(context : Context) : Intent =
62+
Intent(context , SleepTileService::class.java).setAction(value)
5363

54-
fun pendingIntent(context : Context , cancel : Boolean = false) : PendingIntent? = PendingIntent.getService(context , 0 , intent(context) , FLAG_IMMUTABLE).apply { if (cancel) cancel() }
64+
private fun broadcastIntent(context : Context) : Intent =
65+
Intent(context , SleepNotificationReceiver::class.java).setAction(value)
66+
67+
fun pendingIntent(context : Context , cancel : Boolean = false) : PendingIntent? {
68+
val requestCode = value.hashCode()
69+
val intent = intent(context)
70+
val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
71+
PendingIntent.getService(context , requestCode , intent , FLAG_IMMUTABLE)
72+
} else {
73+
PendingIntent.getBroadcast(context , requestCode , intent , FLAG_IMMUTABLE)
74+
}
75+
return pendingIntent.apply { if (cancel) cancel() }
76+
}
5577

5678
fun action(context : Context , cancel : Boolean = false) : Notification.Action.Builder = Notification.Action.Builder(
5779
Icon.createWithResource(context , 0) , title(context) , pendingIntent(context , cancel)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.d4rk.musicsleeptimer.plus.receivers
2+
3+
import android.content.BroadcastReceiver
4+
import android.content.Context
5+
import android.content.Intent
6+
import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification
7+
import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.handle
8+
9+
class SleepNotificationReceiver : BroadcastReceiver() {
10+
override fun onReceive(context : Context , intent : Intent?) {
11+
with(SleepNotification) {
12+
context.handle(intent)
13+
}
14+
}
15+
}

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[versions]
22
agp = "8.13.0"
33
appcompat = "1.7.1"
4-
firebaseBom = "34.2.0"
4+
firebaseBom = "34.3.0"
55
kotlin = "2.2.20"
66
google-firebase-crashlytics = "3.0.6"
77
google-services = "4.4.3"
88
multidex = "2.0.1"
9-
workRuntimeKtx = "2.10.4"
9+
workRuntimeKtx = "2.10.5"
1010

1111
[libraries]
1212
appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Sat Aug 02 09:48:21 EEST 2025
1+
#Wed Oct 01 11:25:16 EEST 2025
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
4-
distributionUrl=https\://services.gradle.org/distributions/gradle-9.0.0-bin.zip
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.1.0-bin.zip
55
zipStoreBase=GRADLE_USER_HOME
66
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)