Skip to content

Commit 62886f0

Browse files
committed
Show battery values in notification content titles
1 parent f2eb45d commit 62886f0

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

app/src/main/java/eu/darken/capod/monitor/ui/MonitorNotifications.kt

+47-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import android.app.PendingIntent
88
import android.content.Context
99
import android.content.Intent
1010
import android.content.pm.ServiceInfo
11-
import androidx.annotation.StringRes
1211
import androidx.core.app.NotificationCompat
1312
import androidx.work.ForegroundInfo
1413
import dagger.hilt.android.qualifiers.ApplicationContext
@@ -20,10 +19,16 @@ import eu.darken.capod.common.debug.logging.logTag
2019
import eu.darken.capod.common.hasApiLevel
2120
import eu.darken.capod.common.notifications.PendingIntentCompat
2221
import eu.darken.capod.main.ui.MainActivity
22+
import eu.darken.capod.pods.core.DualPodDevice
2323
import eu.darken.capod.pods.core.HasCase
2424
import eu.darken.capod.pods.core.HasChargeDetection
2525
import eu.darken.capod.pods.core.HasEarDetection
2626
import eu.darken.capod.pods.core.PodDevice
27+
import eu.darken.capod.pods.core.SinglePodDevice
28+
import eu.darken.capod.pods.core.getBatteryLevelCase
29+
import eu.darken.capod.pods.core.getBatteryLevelHeadset
30+
import eu.darken.capod.pods.core.getBatteryLevelLeftPod
31+
import eu.darken.capod.pods.core.getBatteryLevelRightPod
2732
import kotlinx.coroutines.sync.Mutex
2833
import kotlinx.coroutines.sync.withLock
2934
import javax.inject.Inject
@@ -75,36 +80,65 @@ class MonitorNotifications @Inject constructor(
7580
}
7681

7782
return builder.apply {
78-
@StringRes var contentTitleRes = eu.darken.capod.common.R.string.pods_case_unknown_state
83+
7984
// Options here should be mutually exclusive, and are prioritized by their order of importance
8085
// Some options are omitted here, as they will conflict with other options
8186
// TODO: Implement a settings pane to allow user to customize this
82-
when (device) {
87+
val stateText = when {
8388
// Pods charging state
8489
// This goes first as pods should not be worn if it is still charging
85-
is HasChargeDetection -> {
86-
if (device.isHeadsetBeingCharged)
87-
contentTitleRes = eu.darken.capod.common.R.string.pods_charging_label
90+
device is HasChargeDetection && device.isHeadsetBeingCharged -> {
91+
context.getString(eu.darken.capod.common.R.string.pods_charging_label)
8892
}
8993

9094
// Pods wear state
91-
is HasEarDetection -> {
92-
contentTitleRes = if (device.isBeingWorn) eu.darken.capod.common.R.string.headset_being_worn_label
93-
else eu.darken.capod.common.R.string.headset_not_being_worn_label
95+
device is HasEarDetection -> {
96+
if (device.isBeingWorn) context.getString(eu.darken.capod.common.R.string.headset_being_worn_label)
97+
else context.getString(eu.darken.capod.common.R.string.headset_not_being_worn_label)
9498
}
9599

96100
// Case charge state
97101
// This is under pods wear state as we don't want it conflicting with it
98-
is HasCase -> {
99-
if (device.isCaseCharging)
100-
contentTitleRes = eu.darken.capod.common.R.string.pods_charging_label
102+
device is HasCase && device.isCaseCharging -> {
103+
context.getString(eu.darken.capod.common.R.string.pods_charging_label)
104+
}
105+
106+
else -> context.getString(eu.darken.capod.common.R.string.pods_case_unknown_state)
107+
}
108+
109+
val batteryText = when (device) {
110+
is DualPodDevice -> {
111+
val left = device.getBatteryLevelLeftPod(context)
112+
val right = device.getBatteryLevelRightPod(context)
113+
when {
114+
device is HasCase -> {
115+
val case = device.getBatteryLevelCase(context)
116+
"$left $case $right"
117+
}
118+
119+
else -> "$left $right"
120+
}
101121
}
122+
123+
is SinglePodDevice -> {
124+
val headset = device.getBatteryLevelHeadset(context)
125+
when {
126+
device is HasCase -> {
127+
val case = device.getBatteryLevelCase(context)
128+
"$headset $case"
129+
}
130+
131+
else -> headset
132+
}
133+
}
134+
135+
else -> "?"
102136
}
103137

104138
setStyle(NotificationCompat.DecoratedCustomViewStyle())
105139
setCustomBigContentView(notificationViewFactory.createContentView(device))
106140
setSmallIcon(device.iconRes)
107-
setContentTitle(context.getString(contentTitleRes))
141+
setContentTitle("$batteryText ~ $stateText")
108142
setSubText(null)
109143
log(TAG, VERBOSE) { "updatingNotification(): $device" }
110144
}

0 commit comments

Comments
 (0)