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

Show battery values in notification content titles #218

Merged
merged 1 commit into from
Jul 12, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import androidx.annotation.StringRes
import androidx.core.app.NotificationCompat
import androidx.work.ForegroundInfo
import dagger.hilt.android.qualifiers.ApplicationContext
Expand All @@ -20,10 +19,16 @@ import eu.darken.capod.common.debug.logging.logTag
import eu.darken.capod.common.hasApiLevel
import eu.darken.capod.common.notifications.PendingIntentCompat
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.HasChargeDetection
import eu.darken.capod.pods.core.HasEarDetection
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.withLock
import javax.inject.Inject
Expand Down Expand Up @@ -75,36 +80,65 @@ class MonitorNotifications @Inject constructor(
}

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
// Some options are omitted here, as they will conflict with other options
// TODO: Implement a settings pane to allow user to customize this
when (device) {
val stateText = when {
// Pods charging state
// This goes first as pods should not be worn if it is still charging
is HasChargeDetection -> {
if (device.isHeadsetBeingCharged)
contentTitleRes = eu.darken.capod.common.R.string.pods_charging_label
device is HasChargeDetection && device.isHeadsetBeingCharged -> {
context.getString(eu.darken.capod.common.R.string.pods_charging_label)
}

// Pods wear state
is HasEarDetection -> {
contentTitleRes = if (device.isBeingWorn) eu.darken.capod.common.R.string.headset_being_worn_label
else eu.darken.capod.common.R.string.headset_not_being_worn_label
device is HasEarDetection -> {
if (device.isBeingWorn) context.getString(eu.darken.capod.common.R.string.headset_being_worn_label)
else context.getString(eu.darken.capod.common.R.string.headset_not_being_worn_label)
}

// Case charge state
// This is under pods wear state as we don't want it conflicting with it
is HasCase -> {
if (device.isCaseCharging)
contentTitleRes = eu.darken.capod.common.R.string.pods_charging_label
device is HasCase && device.isCaseCharging -> {
context.getString(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())
setCustomBigContentView(notificationViewFactory.createContentView(device))
setSmallIcon(device.iconRes)
setContentTitle(context.getString(contentTitleRes))
setContentTitle("$batteryText ~ $stateText")
setSubText(null)
log(TAG, VERBOSE) { "updatingNotification(): $device" }
}
Expand Down
Loading