Skip to content

Commit

Permalink
Add setting to control inclusion of all notification contents as attr…
Browse files Browse the repository at this point in the history
…ibutes in the Active Notification Count sensor (#4516)

* Add setting to control inclusion of all notification contents as attributes in the Active Notification Count sensor

* Extend the description of the Active Notification Count to mention the new setting

* Reword the sensor setting's title
  • Loading branch information
marazmarci committed Jul 15, 2024
1 parent d23b38a commit 27f6f11
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class NotificationSensorManager : NotificationListenerService(), SensorManager {
private const val TAG = "NotificationManager"
private const val SETTING_ALLOW_LIST = "notification_allow_list"
private const val SETTING_DISABLE_ALLOW_LIST = "notification_disable_allow_list"
private const val SETTING_INCLUDE_CONTENTS_AS_ATTRS = "active_notification_count_content_attrs"

private var listenerConnected = false
val lastNotification = SensorManager.BasicSensor(
Expand Down Expand Up @@ -98,6 +99,7 @@ class NotificationSensorManager : NotificationListenerService(), SensorManager {

override fun requestSensorUpdate(context: Context) {
updateMediaSession(context)
updateActiveNotificationCount()
}

override fun onListenerConnected() {
Expand Down Expand Up @@ -237,16 +239,19 @@ class NotificationSensorManager : NotificationListenerService(), SensorManager {

try {
val attr: MutableMap<String, Any?> = mutableMapOf()
for (item in activeNotifications) {
attr += mappedBundle(item.notification.extras, "_${item.packageName}_${item.id}").orEmpty()
.plus("${item.packageName}_${item.id}_post_time" to item.postTime)
.plus("${item.packageName}_${item.id}_is_ongoing" to item.isOngoing)
.plus("${item.packageName}_${item.id}_is_clearable" to item.isClearable)
.plus("${item.packageName}_${item.id}_group_id" to item.notification.group)
.plus("${item.packageName}_${item.id}_category" to item.notification.category)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
attr["${item.packageName}_${item.id}_channel_id"] = item.notification.channelId
val includeContentsAsAttrsSetting = getToggleSetting(applicationContext, activeNotificationCount, SETTING_INCLUDE_CONTENTS_AS_ATTRS, default = true)
if (includeContentsAsAttrsSetting) {
for (item in activeNotifications) {
attr += mappedBundle(item.notification.extras, "_${item.packageName}_${item.id}").orEmpty()
.plus("${item.packageName}_${item.id}_post_time" to item.postTime)
.plus("${item.packageName}_${item.id}_is_ongoing" to item.isOngoing)
.plus("${item.packageName}_${item.id}_is_clearable" to item.isClearable)
.plus("${item.packageName}_${item.id}_group_id" to item.notification.group)
.plus("${item.packageName}_${item.id}_category" to item.notification.category)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
attr["${item.packageName}_${item.id}_channel_id"] = item.notification.channelId
}
}
}
onSensorUpdated(
Expand Down
3 changes: 2 additions & 1 deletion common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@
<string name="select_entity_to_display">Select entity to display</string>
<string name="select_file">Select file</string>
<string name="select_instance">Select your Home Assistant server</string>
<string name="sensor_description_active_notification_count">Total count of active notifications that are visible to the user including silent, persistent and the Sensor Worker notifications.</string>
<string name="sensor_description_active_notification_count">Total count of active notifications that are visible to the user including silent, persistent and the Sensor Worker notifications. Attributes will include the contents of all notifications if the corresponding setting is enabled. It\'s enabled by default.</string>
<string name="sensor_description_app_importance">If the app is in the foreground, background or any other state it can be</string>
<string name="sensor_description_android_auto">If the phone is currently connected to an Android Auto head unit</string>
<string name="sensor_description_android_os_version">Android OS version</string>
Expand Down Expand Up @@ -734,6 +734,7 @@
<string name="sensor_setting_nextalarm_allow_list_title">Allow list</string>
<string name="sensor_setting_notification_allow_list_title">Allow list</string>
<string name="sensor_setting_notification_disable_allow_list_title">Disable allow list requirement</string>
<string name="sensor_setting_active_notification_count_content_attrs_title">Include content from all notifications as attributes</string>
<string name="sensor_setting_battery_current_divisor_title">Battery current divisor</string>
<string name="sensor_setting_battery_voltage_divisor_title">Battery voltage divisor</string>
<string name="sensor_settings">Sensor settings</string>
Expand Down

0 comments on commit 27f6f11

Please sign in to comment.