From ebb05acc02eef9ae54bd1773614e8997b87b752e Mon Sep 17 00:00:00 2001 From: ajp_anton Date: Mon, 27 Jul 2026 22:32:28 +0000 Subject: [PATCH] Avoid duplicate BigText notification content --- .../capture/NotificationSnapshot.kt | 34 +++++++++++++++---- .../capture/NotificationContentsTest.kt | 32 +++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 app/src/test/java/se/ajpanton/notificationlog/capture/NotificationContentsTest.kt diff --git a/app/src/main/java/se/ajpanton/notificationlog/capture/NotificationSnapshot.kt b/app/src/main/java/se/ajpanton/notificationlog/capture/NotificationSnapshot.kt index 5d01b8a..5b44848 100644 --- a/app/src/main/java/se/ajpanton/notificationlog/capture/NotificationSnapshot.kt +++ b/app/src/main/java/se/ajpanton/notificationlog/capture/NotificationSnapshot.kt @@ -22,15 +22,33 @@ data class NotificationSnapshot( object NotificationContents { fun extract(notification: Notification): String? { - val parts = linkedSetOf() val extras = notification.extras ?: Bundle.EMPTY messagingContents(extras)?.let { return it } - extras.getCharSequence(Notification.EXTRA_TITLE)?.addTo(parts) - extras.getCharSequence(Notification.EXTRA_TEXT)?.addTo(parts) - extras.getCharSequence(Notification.EXTRA_BIG_TEXT)?.addTo(parts) - extras.getCharSequence(Notification.EXTRA_SUB_TEXT)?.addTo(parts) - extras.getCharSequence(Notification.EXTRA_SUMMARY_TEXT)?.addTo(parts) - extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES)?.forEach { it?.addTo(parts) } + return genericContents( + title = extras.getCharSequence(Notification.EXTRA_TITLE), + text = extras.getCharSequence(Notification.EXTRA_TEXT), + bigText = extras.getCharSequence(Notification.EXTRA_BIG_TEXT), + subText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT), + summaryText = extras.getCharSequence(Notification.EXTRA_SUMMARY_TEXT), + textLines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES).orEmpty().asList(), + ) + } + + /** Uses BigTextStyle's expanded content instead of its alternate collapsed rendering. */ + internal fun genericContents( + title: CharSequence?, + text: CharSequence?, + bigText: CharSequence?, + subText: CharSequence?, + summaryText: CharSequence?, + textLines: List, + ): String? { + val parts = linkedSetOf() + title?.addTo(parts) + (bigText.takeIf { it.hasText() } ?: text)?.addTo(parts) + subText?.addTo(parts) + summaryText?.addTo(parts) + textLines.forEach { it?.addTo(parts) } return parts.takeIf { it.isNotEmpty() }?.joinToString("\n") } @@ -92,5 +110,7 @@ object NotificationContents { toString().trim().takeIf { it.isNotEmpty() }?.let(parts::add) } + private fun CharSequence?.hasText(): Boolean = !this.isNullOrBlank() + private const val MAX_IMAGE_DIMENSION = 1600 } diff --git a/app/src/test/java/se/ajpanton/notificationlog/capture/NotificationContentsTest.kt b/app/src/test/java/se/ajpanton/notificationlog/capture/NotificationContentsTest.kt new file mode 100644 index 0000000..98be27a --- /dev/null +++ b/app/src/test/java/se/ajpanton/notificationlog/capture/NotificationContentsTest.kt @@ -0,0 +1,32 @@ +package se.ajpanton.notificationlog.capture + +import org.junit.Assert.assertEquals +import org.junit.Test + +class NotificationContentsTest { + @Test fun `expanded text replaces the alternate collapsed rendering`() { + val contents = NotificationContents.genericContents( + title = "Sender", + text = "Subject • Email body", + bigText = "Subject\nEmail body", + subText = "account@example.com", + summaryText = null, + textLines = emptyList(), + ) + + assertEquals("Sender\nSubject\nEmail body\naccount@example.com", contents) + } + + @Test fun `collapsed text remains the fallback without expanded text`() { + val contents = NotificationContents.genericContents( + title = "Sender", + text = "Subject • Email body", + bigText = null, + subText = "account@example.com", + summaryText = null, + textLines = emptyList(), + ) + + assertEquals("Sender\nSubject • Email body\naccount@example.com", contents) + } +}