Avoid duplicate BigText notification content
This commit is contained in:
@@ -22,15 +22,33 @@ data class NotificationSnapshot(
|
|||||||
|
|
||||||
object NotificationContents {
|
object NotificationContents {
|
||||||
fun extract(notification: Notification): String? {
|
fun extract(notification: Notification): String? {
|
||||||
val parts = linkedSetOf<String>()
|
|
||||||
val extras = notification.extras ?: Bundle.EMPTY
|
val extras = notification.extras ?: Bundle.EMPTY
|
||||||
messagingContents(extras)?.let { return it }
|
messagingContents(extras)?.let { return it }
|
||||||
extras.getCharSequence(Notification.EXTRA_TITLE)?.addTo(parts)
|
return genericContents(
|
||||||
extras.getCharSequence(Notification.EXTRA_TEXT)?.addTo(parts)
|
title = extras.getCharSequence(Notification.EXTRA_TITLE),
|
||||||
extras.getCharSequence(Notification.EXTRA_BIG_TEXT)?.addTo(parts)
|
text = extras.getCharSequence(Notification.EXTRA_TEXT),
|
||||||
extras.getCharSequence(Notification.EXTRA_SUB_TEXT)?.addTo(parts)
|
bigText = extras.getCharSequence(Notification.EXTRA_BIG_TEXT),
|
||||||
extras.getCharSequence(Notification.EXTRA_SUMMARY_TEXT)?.addTo(parts)
|
subText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT),
|
||||||
extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES)?.forEach { it?.addTo(parts) }
|
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<CharSequence?>,
|
||||||
|
): String? {
|
||||||
|
val parts = linkedSetOf<String>()
|
||||||
|
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")
|
return parts.takeIf { it.isNotEmpty() }?.joinToString("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,5 +110,7 @@ object NotificationContents {
|
|||||||
toString().trim().takeIf { it.isNotEmpty() }?.let(parts::add)
|
toString().trim().takeIf { it.isNotEmpty() }?.let(parts::add)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun CharSequence?.hasText(): Boolean = !this.isNullOrBlank()
|
||||||
|
|
||||||
private const val MAX_IMAGE_DIMENSION = 1600
|
private const val MAX_IMAGE_DIMENSION = 1600
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user