Skip to content
Merged
Show file tree
Hide file tree
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,39 @@ class NotificationParserTest {
)
}

@Test
fun parseMessagingStyleChronologically() {
val notification = NotificationCompat.Builder(context, "TEST_CHANNEL")
.setStyle(
NotificationCompat.MessagingStyle(Person.Builder().setName("Group Chat A").build())
.setConversationTitle("Group Chat A")
.addMessage("Message 2", 2L, Person.Builder().setName("Alice").build())
.addMessage("Message 3", 3L, Person.Builder().setName("Bob").build())
.addHistoricMessage(
NotificationCompat.MessagingStyle.Message("Message 1", 1L, Person.Builder().setName("Bob").build())
)
)
.setSmallIcon(0)
.setShowWhen(false)
.build()

notificationParser.parse(
notification.toSbn(),
createDefaultSilentChannel(),
showMessagingStyleChronologically = true
) shouldBe ParsedNotification(
"0|com.matejdro.pebblenotificationcenter.notification.parsing|0|null|0",
TEST_PACKAGE,
"SMS App",
"Group Chat A",
"Bob: Message 1\n" +
"Alice: Message 2\n" +
"Bob: Message 3",
Instant.ofEpochMilli(3L),
channel = testChannelOrNull(),
)
}

@Test
fun doNotRepeatNameOfTheSamePerson() {
val notification = NotificationCompat.Builder(context, "TEST_CHANNEL")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ class NotificationService : NotificationListenerService() {
val ranking = Ranking()
currentRanking.getRanking(sbn.key, ranking)

return notificationParser.parse(sbn, getNotificationChannel(sbn), ranking)
return notificationParser.parse(
sbn,
getNotificationChannel(sbn),
ranking,
preferenceStore.data.first()[GlobalPreferenceKeys.showMessagingStyleChronologically]
)
}

override fun onNotificationRemoved(sbn: StatusBarNotification) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ class NotificationParser(
sbn: StatusBarNotification,
channel: Any?,
ranking: NotificationListenerService.Ranking? = null,
showMessagingStyleChronologically: Boolean = false,
): ParsedNotification? {
val notification = sbn.notification
val title = appNameProvider.getAppName(sbn.packageName)

val (imageUri, messagingStyleText) = notification.parseMessagingStyle()
val (imageUri, messagingStyleText) = notification.parseMessagingStyle(showMessagingStyleChronologically)
val (subtitle, text) = parseSubtitleAndBody(notification, messagingStyleText)

if (subtitle.isBlank() && text.isNullOrBlank()) {
Expand Down Expand Up @@ -145,10 +146,16 @@ class NotificationParser(
return channelId to isSilent
}

private fun Notification.parseMessagingStyle(): Pair<Uri?, String?> {
private fun Notification.parseMessagingStyle(showChronologically: Boolean): Pair<Uri?, String?> {
val messagingStyle = NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(this) ?: return (null to null)

val messages = (messagingStyle.messages + messagingStyle.historicMessages).sortedByDescending { it.timestamp }
val messages = (messagingStyle.messages + messagingStyle.historicMessages).let { unsortedMessages ->
if (showChronologically) {
unsortedMessages.sortedBy { it.timestamp }
} else {
unsortedMessages.sortedByDescending { it.timestamp }
}
}
if (messages.isEmpty()) {
return (null to null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ object GlobalPreferenceKeys {
val actionOrder = StringListPreferenceKeyWithDefault("action_order", emptyList())

val autoCloseSeconds = IntPreferenceKeyWithDefault("auto_close", 0)
val showMessagingStyleChronologically = BooleanPreferenceKeyWithDefault("show_messaging_style_chronologically", false)

val notifyOnReconnect = BooleanPreferenceKeyWithDefault("notify_on_reconnect", true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ private fun ToolsScreenContent(
)
}

item(span = { GridItemSpan(maxLineSpan) }) {
SwitchPreference(
state.preferences[GlobalPreferenceKeys.showMessagingStyleChronologically],
onValueChange = {
updatePreference(GlobalPreferenceKeys.showMessagingStyleChronologically, it)
},
title = { Text(stringResource(R.string.setting_messaging_style_chronological)) },
summary = { Text(stringResource(R.string.setting_messaging_style_chronological_description)) }
)
}

item(span = { GridItemSpan(maxLineSpan) }) {
SwitchPreference(
state.preferences[GlobalPreferenceKeys.notifyOnReconnect],
Expand Down
4 changes: 4 additions & 0 deletions mobile/tools/ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<string name="setting_auto_close_description">%1$d\n\nAuto close the notification popup after this many seconds. Pressing any
button will stop the timer until the next notification. Set to 0 to disable.
</string>
<string name="setting_messaging_style_chronological">Show chat messages chronologically</string>
<string name="setting_messaging_style_chronological_description">Display MessagingStyle conversation bodies oldest-first. When disabled,
newest messages are shown first to reduce scrolling.
</string>
<string name="setting_notify_on_reconnect">Open missed notifications on reconnect</string>
<string name="setting_notify_on_reconnect_description">If phone received any non-muted notifications during watch disconnect,
it will show them on the watch after it reconnects
Expand Down
Loading