Show battery values in notification content titles

This commit is contained in:
darken
2024-07-12 14:47:01 +02:00
committed by Matthias Urhahn
parent f2eb45d00b
commit 62886f00f1
@@ -8,7 +8,6 @@ import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.ServiceInfo import android.content.pm.ServiceInfo
import androidx.annotation.StringRes
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.work.ForegroundInfo import androidx.work.ForegroundInfo
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
@@ -20,10 +19,16 @@ import eu.darken.capod.common.debug.logging.logTag
import eu.darken.capod.common.hasApiLevel import eu.darken.capod.common.hasApiLevel
import eu.darken.capod.common.notifications.PendingIntentCompat import eu.darken.capod.common.notifications.PendingIntentCompat
import eu.darken.capod.main.ui.MainActivity import eu.darken.capod.main.ui.MainActivity
import eu.darken.capod.pods.core.DualPodDevice
import eu.darken.capod.pods.core.HasCase import eu.darken.capod.pods.core.HasCase
import eu.darken.capod.pods.core.HasChargeDetection import eu.darken.capod.pods.core.HasChargeDetection
import eu.darken.capod.pods.core.HasEarDetection import eu.darken.capod.pods.core.HasEarDetection
import eu.darken.capod.pods.core.PodDevice import eu.darken.capod.pods.core.PodDevice
import eu.darken.capod.pods.core.SinglePodDevice
import eu.darken.capod.pods.core.getBatteryLevelCase
import eu.darken.capod.pods.core.getBatteryLevelHeadset
import eu.darken.capod.pods.core.getBatteryLevelLeftPod
import eu.darken.capod.pods.core.getBatteryLevelRightPod
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import javax.inject.Inject import javax.inject.Inject
@@ -75,36 +80,65 @@ class MonitorNotifications @Inject constructor(
} }
return builder.apply { return builder.apply {
@StringRes var contentTitleRes = eu.darken.capod.common.R.string.pods_case_unknown_state
// Options here should be mutually exclusive, and are prioritized by their order of importance // Options here should be mutually exclusive, and are prioritized by their order of importance
// Some options are omitted here, as they will conflict with other options // Some options are omitted here, as they will conflict with other options
// TODO: Implement a settings pane to allow user to customize this // TODO: Implement a settings pane to allow user to customize this
when (device) { val stateText = when {
// Pods charging state // Pods charging state
// This goes first as pods should not be worn if it is still charging // This goes first as pods should not be worn if it is still charging
is HasChargeDetection -> { device is HasChargeDetection && device.isHeadsetBeingCharged -> {
if (device.isHeadsetBeingCharged) context.getString(eu.darken.capod.common.R.string.pods_charging_label)
contentTitleRes = eu.darken.capod.common.R.string.pods_charging_label
} }
// Pods wear state // Pods wear state
is HasEarDetection -> { device is HasEarDetection -> {
contentTitleRes = if (device.isBeingWorn) eu.darken.capod.common.R.string.headset_being_worn_label if (device.isBeingWorn) context.getString(eu.darken.capod.common.R.string.headset_being_worn_label)
else eu.darken.capod.common.R.string.headset_not_being_worn_label else context.getString(eu.darken.capod.common.R.string.headset_not_being_worn_label)
} }
// Case charge state // Case charge state
// This is under pods wear state as we don't want it conflicting with it // This is under pods wear state as we don't want it conflicting with it
is HasCase -> { device is HasCase && device.isCaseCharging -> {
if (device.isCaseCharging) context.getString(eu.darken.capod.common.R.string.pods_charging_label)
contentTitleRes = eu.darken.capod.common.R.string.pods_charging_label
} }
else -> context.getString(eu.darken.capod.common.R.string.pods_case_unknown_state)
}
val batteryText = when (device) {
is DualPodDevice -> {
val left = device.getBatteryLevelLeftPod(context)
val right = device.getBatteryLevelRightPod(context)
when {
device is HasCase -> {
val case = device.getBatteryLevelCase(context)
"$left $case $right"
}
else -> "$left $right"
}
}
is SinglePodDevice -> {
val headset = device.getBatteryLevelHeadset(context)
when {
device is HasCase -> {
val case = device.getBatteryLevelCase(context)
"$headset $case"
}
else -> headset
}
}
else -> "?"
} }
setStyle(NotificationCompat.DecoratedCustomViewStyle()) setStyle(NotificationCompat.DecoratedCustomViewStyle())
setCustomBigContentView(notificationViewFactory.createContentView(device)) setCustomBigContentView(notificationViewFactory.createContentView(device))
setSmallIcon(device.iconRes) setSmallIcon(device.iconRes)
setContentTitle(context.getString(contentTitleRes)) setContentTitle("$batteryText ~ $stateText")
setSubText(null) setSubText(null)
log(TAG, VERBOSE) { "updatingNotification(): $device" } log(TAG, VERBOSE) { "updatingNotification(): $device" }
} }