Skip to content

Commit

Permalink
Update wakeword notification when isHeyDicio changes
Browse files Browse the repository at this point in the history
make it so that the text in the notification changes from `Listening for the "Hey Dicio" wake word` to `Listening for custom wake word` automatically
  • Loading branch information
Stypox committed Feb 27, 2025
1 parent ac1141f commit c664e15
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions app/src/main/kotlin/org/stypox/dicio/io/wake/WakeService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.launch
import org.stypox.dicio.MainActivity
import org.stypox.dicio.MainActivity.Companion.ACTION_WAKE_WORD
Expand Down Expand Up @@ -71,6 +72,15 @@ class WakeService : Service() {
override fun onCreate() {
super.onCreate()
notificationManager = getSystemService(this, NotificationManager::class.java)!!

scope.launch {
// Recreate the notification so that it says the correct thing (i.e. there is a
// different string for the "Hey Dicio" wake word and for a custom one).
// Ignore the first one (i.e. the current value), which is handled in onStartCommand.
wakeDevice.isHeyDicio.drop(1).collect { isHeyDicio ->
createForegroundNotification(isHeyDicio)
}
}
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Expand All @@ -80,7 +90,7 @@ class WakeService : Service() {
}

try {
createForegroundNotification()
createForegroundNotification(wakeDevice.isHeyDicio.value)
} catch (t: Throwable) {
stopWithMessage("could not create WakeService foreground notification", t)
return START_NOT_STICKY
Expand Down Expand Up @@ -135,7 +145,7 @@ class WakeService : Service() {
}
}

private fun createForegroundNotification() {
private fun createForegroundNotification(isHeyDicio: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
FOREGROUND_NOTIFICATION_CHANNEL_ID,
Expand All @@ -150,7 +160,7 @@ class WakeService : Service() {
.setSmallIcon(R.drawable.ic_hearing_white)
.setContentTitle(
getString(
if (wakeDevice.isHeyDicio()) R.string.wake_service_foreground_notification
if (isHeyDicio) R.string.wake_service_foreground_notification
else R.string.wake_custom_service_foreground_notification
)
)
Expand Down

0 comments on commit c664e15

Please sign in to comment.