From 5846bf5ee42bbb5e5976929dcb0c2841f211c0b6 Mon Sep 17 00:00:00 2001 From: James Rich <2199651+jamesarich@users.noreply.github.com> Date: Wed, 12 Mar 2025 05:02:14 -0500 Subject: [PATCH] feat: Improve (battery) notification behavior (#1661) * feat: Improve notification behavior - Changes low battery notifications to be non-cancelable - Cancel low battery notifications when battery level is no longer low. - Add notification groups and improve notification settings. - Add vibration to low battery notifications. * Improve low battery notification Add battery level progress bar to the low battery notification. --- .../geeksville/mesh/service/MeshService.kt | 2 ++ .../mesh/service/MeshServiceNotifications.kt | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt index c6fb5c45a..47b05ffd6 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -949,6 +949,8 @@ class MeshService : Service(), Logging { if (t.deviceMetrics.voltage > batteryPercentUnsupported && t.deviceMetrics.batteryLevel < batteryPercentLowThreshold) { serviceNotifications.showOrUpdateLowBatteryNotification(it, isRemote) + } else { + serviceNotifications.cancelLowBatteryNotification(it) } } } diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshServiceNotifications.kt b/app/src/main/java/com/geeksville/mesh/service/MeshServiceNotifications.kt index fd2d2bd85..4dc69ce29 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshServiceNotifications.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshServiceNotifications.kt @@ -51,6 +51,12 @@ class MeshServiceNotifications( const val OPEN_MESSAGE_ACTION = "com.geeksville.mesh.OPEN_MESSAGE_ACTION" const val OPEN_MESSAGE_EXTRA_CONTACT_KEY = "com.geeksville.mesh.OPEN_MESSAGE_EXTRA_CONTACT_KEY" + const val SERVICE_GROUP = "SERVICE_NOTIFICATION_GROUP" + const val MESSAGE_GROUP = "MESSAGE_NOTIFICATION_GROUP" + const val ALERT_GROUP = "ALERT_NOTIFICATION_GROUP" + const val NEW_NODE_GROUP = "NEW_NODE_NOTIFICATION_GROUP" + const val LOW_BATTERY_GROUP = "LOW_BATTERY_NOTIFICATION_GROUP" + const val MAX_BATTERY_LEVEL = 100 } private val notificationManager: NotificationManager get() = context.notificationManager @@ -215,6 +221,7 @@ class MeshServiceNotifications( ).apply { lightColor = notificationLightColor lockscreenVisibility = Notification.VISIBILITY_PUBLIC + enableVibration(true) setShowBadge(true) setSound( RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), @@ -339,6 +346,10 @@ class MeshServiceNotifications( ) } + fun cancelLowBatteryNotification(node: NodeEntity) { + notificationManager.cancel(node.num) + } + private val openAppIntent: PendingIntent by lazy { PendingIntent.getActivity( context, @@ -396,6 +407,7 @@ class MeshServiceNotifications( } with(serviceNotificationBuilder) { priority = NotificationCompat.PRIORITY_MIN + setGroup(SERVICE_GROUP) setCategory(Notification.CATEGORY_SERVICE) setOngoing(true) setContentTitle(name) @@ -434,6 +446,7 @@ class MeshServiceNotifications( setContentIntent(openMessageIntent(contactKey)) priority = NotificationCompat.PRIORITY_DEFAULT setCategory(Notification.CATEGORY_MESSAGE) + setGroup(MESSAGE_GROUP) setAutoCancel(true) setStyle( NotificationCompat.MessagingStyle(person) @@ -459,6 +472,7 @@ class MeshServiceNotifications( setContentIntent(openMessageIntent(contactKey)) priority = NotificationCompat.PRIORITY_HIGH setCategory(Notification.CATEGORY_ALARM) + setGroup(ALERT_GROUP) setAutoCancel(true) setStyle( NotificationCompat.MessagingStyle(person) @@ -475,6 +489,7 @@ class MeshServiceNotifications( } with(newNodeSeenNotificationBuilder) { priority = NotificationCompat.PRIORITY_DEFAULT + setGroup(NEW_NODE_GROUP) setCategory(Notification.CATEGORY_STATUS) setAutoCancel(true) setContentTitle("New Node Seen: $name") @@ -508,10 +523,12 @@ class MeshServiceNotifications( with(tempNotificationBuilder) { priority = NotificationCompat.PRIORITY_DEFAULT setCategory(Notification.CATEGORY_STATUS) - setAutoCancel(true) + setOngoing(true) + setGroup(LOW_BATTERY_GROUP) setShowWhen(true) setOnlyAlertOnce(true) setWhen(System.currentTimeMillis()) + setProgress(MAX_BATTERY_LEVEL, node.deviceMetrics.batteryLevel, false) setContentTitle( context.getString(R.string.low_battery_title).format( node.shortName