Add content search and improve log browsing

Add persistent encrypted content searches with regex, literal, AND/OR terms and highlighted non-overlapping matches. Combine them with app filtering and clear both filters together.

Index encrypted log chunks for direct access, bound the viewport cache, and prefetch around unloaded rows. Improve loading feedback, scroll-to-top timing, row spacing and expansion behavior, with unit and Android device coverage.
This commit is contained in:
ajp_anton committed 2026-10-03 08:07:03 +00:00
1 parent 32d0935389
commit 0bf23c2a77
24 files changed
+2104 -182

No files matched your search

+1
View File
@@ -9,6 +9,7 @@ keystore.properties
# Gradle and Android build output # Gradle and Android build output
/.gradle/ /.gradle/
/.gradle-tmp/ /.gradle-tmp/
/.kotlin/
/build/ /build/
/*/build/ /*/build/
/.externalNativeBuild/ /.externalNativeBuild/
+6
View File
@@ -22,6 +22,12 @@ expanded for long entries, and exported as CSV, aligned text, or an HTML ZIP.
Only the HTML ZIP includes saved notification images; CSV and aligned text use Only the HTML ZIP includes saved notification images; CSV and aligned text use
an `[image]` marker instead. an `[image]` marker instead.
The viewer loads nearby logs on demand rather than keeping the whole history in
memory. Fast scrolling can briefly show empty rows while their contents load.
Filter by app or search notification text using literal text, regular
expressions, or all/any search terms. Matching text is highlighted; search
filters are remembered until cleared, and search text is stored encrypted.
## Privacy ## Privacy
Logs and retained notification images are encrypted on the device with an Logs and retained notification images are encrypted on the device with an
@@ -0,0 +1,164 @@
package se.ajpanton.notificationsmaster
import android.os.SystemClock
import android.text.Spanned
import android.text.style.BackgroundColorSpan
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import se.ajpanton.notificationsmaster.data.EncryptedNotificationLogStore
import se.ajpanton.notificationsmaster.model.NotificationAction
import se.ajpanton.notificationsmaster.model.NotificationLogEntry
import se.ajpanton.notificationsmaster.search.LogSearchSettings
import se.ajpanton.notificationsmaster.search.SearchMode
import se.ajpanton.notificationsmaster.settings.LogAppFilterStore
import se.ajpanton.notificationsmaster.settings.LogSearchStore
import se.ajpanton.notificationsmaster.settings.LogViewSettings
import se.ajpanton.notificationsmaster.settings.LogViewSettingsStore
import java.io.File
@RunWith(AndroidJUnit4::class)
class LogSearchDeviceTest {
private val context = InstrumentationRegistry.getInstrumentation().targetContext
private val store = EncryptedNotificationLogStore(context)
private val search = LogSearchStore(context)
private val apps = LogAppFilterStore(context)
@Before @After fun clear() {
store.clear()
search.clear()
apps.clear()
LogViewSettingsStore(context).save(LogViewSettings())
}
private fun append(text: String?, packageName: String = "test.app", previous: String? = null) = store.append(
NotificationLogEntry(recordedAtEpochMillis = 0, packageName = packageName, appName = "Some app",
action = if (previous == null) NotificationAction.APPEARED else NotificationAction.EDITED,
contents = text, previousContents = previous))
@Test fun exactNonGreedyHighlightsCombineWithAppFilterAndResetTogether() {
append("there is some text and here is some more")
append("there is some text and here is some more", "test.other")
append("Other text")
search.save(LogSearchSettings("here there", SearchMode.AND))
apps.save(listOf(LogAppFilterStore.App("Some app", "test.app")))
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { count(it) == 1 && message(it)?.text is Spanned }
scenario.onActivity { activity ->
val value = message(activity)!!.text as Spanned
val matches = value.getSpans(0, value.length, BackgroundColorSpan::class.java)
.map { value.subSequence(value.getSpanStart(it), value.getSpanEnd(it)).toString() }
assertEquals(listOf("there", "here"), matches)
val menu = activity.findViewById<Toolbar>(R.id.toolbar).menu
assertNotNull(menu.findItem(3))
activity.onOptionsItemSelected(menu.findItem(3))
}
await(scenario) { count(it) == 3 && it.findViewById<View>(R.id.loading_indicator).visibility == View.GONE }
assertFalse(search.load().active)
assertTrue(apps.selectedPackages().isEmpty())
}
}
@Test fun searchDialogKeepsDraftOnRotationAndRejectsInvalidRegex() {
append("Some test contents")
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { count(it) == 1 }
scenario.onActivity { logs(it).showSearch() }
await(scenario) { dialog(it)?.findViewById<View>(R.id.log_search_text) != null }
scenario.onActivity {
dialog(it)!!.findViewById<TextView>(R.id.log_search_text).text = "some some"
dialog(it)!!.findViewById<Spinner>(R.id.log_search_mode).setSelection(SearchMode.AND.ordinal)
dialog(it)!!.findViewById<CheckBox>(R.id.log_search_case).isChecked = true
dialog(it)!!.findViewById<CheckBox>(R.id.log_search_words).isChecked = true
}
await(scenario) { dialog(it)!!.findViewById<View>(R.id.log_search_words).visibility == View.VISIBLE }
scenario.recreate()
await(scenario) { dialog(it)?.findViewById<TextView>(R.id.log_search_text)?.text.toString() == "some some" }
scenario.onActivity {
assertTrue(dialog(it)!!.findViewById<CheckBox>(R.id.log_search_case).isChecked)
assertTrue(dialog(it)!!.findViewById<CheckBox>(R.id.log_search_words).isChecked)
assertEquals(SearchMode.AND.ordinal, dialog(it)!!.findViewById<Spinner>(R.id.log_search_mode).selectedItemPosition)
dialog(it)!!.findViewById<TextView>(R.id.log_search_text).text = "["
dialog(it)!!.findViewById<Spinner>(R.id.log_search_mode).setSelection(SearchMode.REGEX.ordinal)
}
await(scenario) { dialog(it)!!.findViewById<View>(R.id.log_search_words).visibility == View.GONE }
scenario.onActivity {
(dialog(it) as androidx.appcompat.app.AlertDialog).getButton(-1).performClick()
assertTrue(dialog(it)!!.isShowing)
assertFalse(search.load().active)
dialog(it)!!.findViewById<TextView>(R.id.log_search_text).text = "some.*contents"
dialog(it)!!.findViewById<CheckBox>(R.id.log_search_case).isChecked = false
(dialog(it) as androidx.appcompat.app.AlertDialog).getButton(-1).performClick()
}
await(scenario) { dialog(it) == null && count(it) == 1 }
assertEquals("some.*contents", LogSearchStore(context).load().text)
}
}
@Test fun previousContentIsHighlightedAndFiltersSurviveClearingLogs() {
append("New contents", previous = "Private old contents")
search.save(LogSearchSettings("Private old", SearchMode.LITERAL))
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { count(it) == 1 && message(it) != null }
scenario.onActivity { (message(it)!!.parent as View).performClick() }
await(scenario) { activity ->
val content = message(activity)?.parent as? ViewGroup
val previous = content?.getChildAt(2) as? TextView
val value = previous?.text as? Spanned
value?.getSpans(0, value.length, BackgroundColorSpan::class.java)?.isNotEmpty() == true
}
store.clear()
scenario.recreate()
await(scenario) { it.findViewById<View>(R.id.empty_view).visibility == View.VISIBLE }
assertTrue(search.load().active)
assertEquals("Private old", search.load().text)
assertFalse(File(context.filesDir, "log-search.bin").readBytes().decodeToString().contains("Private old"))
}
}
@Test fun anEditWithNoCurrentTextStillExposesPreviousSearchMatches() {
append(null, previous = "Previous searchable contents")
search.save(LogSearchSettings("searchable", SearchMode.LITERAL))
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { count(it) == 1 && message(it) != null }
scenario.onActivity { (message(it)!!.parent as View).performClick() }
await(scenario) {
val previous = (message(it)?.parent as? ViewGroup)?.getChildAt(2) as? TextView
val value = previous?.text as? Spanned
value?.getSpans(0, value.length, BackgroundColorSpan::class.java)?.size == 1
}
}
}
private fun logs(activity: MainActivity) = activity.supportFragmentManager.findFragmentById(R.id.content_frame) as ViewLogsFragment
private fun dialog(activity: MainActivity) = (activity.supportFragmentManager.findFragmentByTag("log-search") as? LogSearchDialogFragment)?.dialog
private fun count(activity: MainActivity) = activity.findViewById<RecyclerView>(R.id.log_list).adapter!!.itemCount
private fun message(activity: MainActivity): TextView? {
val list = activity.findViewById<RecyclerView>(R.id.log_list)
val root = list.findViewHolderForAdapterPosition(0)?.itemView as? ViewGroup ?: return null
val row = (root.getChildAt(0) as ViewGroup).getChildAt(0) as? ViewGroup ?: return null
return (row.getChildAt(1) as ViewGroup).getChildAt(0) as TextView
}
private fun await(scenario: ActivityScenario<MainActivity>, ready: (MainActivity) -> Boolean) {
val deadline = SystemClock.uptimeMillis() + 10_000
do {
var result = false
scenario.onActivity { result = ready(it) }
if (result) return
SystemClock.sleep(16)
} while (SystemClock.uptimeMillis() < deadline)
throw AssertionError("Search UI did not reach the expected state")
}
}
@@ -0,0 +1,490 @@
package se.ajpanton.notificationsmaster
import android.graphics.drawable.Animatable
import android.graphics.Bitmap
import android.os.SystemClock
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.ProgressBar
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.core.view.doOnPreDraw
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry
import androidx.test.runner.lifecycle.ActivityLifecycleCallback
import androidx.test.runner.lifecycle.Stage
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import se.ajpanton.notificationsmaster.data.EncryptedNotificationLogStore
import se.ajpanton.notificationsmaster.data.EncryptedImageStore
import se.ajpanton.notificationsmaster.data.LogWindowCache
import se.ajpanton.notificationsmaster.model.NotificationAction
import se.ajpanton.notificationsmaster.model.NotificationLogEntry
import se.ajpanton.notificationsmaster.settings.LogAppFilterStore
import se.ajpanton.notificationsmaster.settings.LogSearchStore
import se.ajpanton.notificationsmaster.settings.LogField
import se.ajpanton.notificationsmaster.settings.LogViewSettings
import se.ajpanton.notificationsmaster.settings.LogViewSettingsStore
import se.ajpanton.notificationsmaster.settings.StorageLimits
import se.ajpanton.notificationsmaster.settings.StorageLimitsStore
import java.io.ByteArrayOutputStream
import java.util.UUID
@RunWith(AndroidJUnit4::class)
class LogViewDeviceTest {
private val context = InstrumentationRegistry.getInstrumentation().targetContext
private val store = EncryptedNotificationLogStore(context)
private val filters = LogAppFilterStore(context)
@Before fun prepareLogs() {
clearLogs()
}
private fun seedHistory() {
repeat(640) { index ->
store.append(NotificationLogEntry(
recordedAtEpochMillis = index.toLong(),
packageName = if (index % 160 == 0) "test.selected" else "test.other",
appName = "Test app",
action = NotificationAction.APPEARED,
contents = "Test message $index",
))
}
}
@After fun clearLogs() {
store.clear()
filters.clear()
LogSearchStore(context).clear()
LogViewSettingsStore(context).save(LogViewSettings())
StorageLimitsStore(context).save(StorageLimits())
}
@Test fun sparseFilterLoadsOlderMatchesAndStopsTheIndicator() {
seedHistory()
filters.save(listOf(LogAppFilterStore.App("Test app", "test.selected")))
var sawLoading = false
val monitor = ActivityLifecycleMonitorRegistry.getInstance()
val observer = ActivityLifecycleCallback { activity, stage ->
if (activity is MainActivity && stage == Stage.CREATED) {
activity.supportFragmentManager.registerFragmentLifecycleCallbacks(
object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentResumed(manager: FragmentManager, fragment: Fragment) {
if (fragment is ViewLogsFragment) {
sawLoading = fragment.requireView().findViewById<View>(R.id.loading_indicator)
.visibility == View.VISIBLE
}
}
}, false,
)
}
}
monitor.addLifecycleCallback(observer)
try {
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { activity ->
activity.findViewById<View>(R.id.loading_indicator).visibility == View.GONE &&
activity.findViewById<RecyclerView>(R.id.log_list).adapter!!.itemCount == 4
}
assertTrue("Loading should be visible before the filtered results arrive", sawLoading)
scenario.onActivity { activity ->
assertEquals(View.GONE, activity.findViewById<View>(R.id.empty_view).visibility)
val spinner = activity.findViewById<ProgressBar>(R.id.loading_spinner)
assertFalse((spinner.indeterminateDrawable as Animatable).isRunning)
}
}
} finally {
monitor.removeLifecycleCallback(observer)
}
}
@Test fun distantScrollReturnsToTopWithinOneSecondAndTimestampHasNoExtraInset() {
seedHistory()
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { it.findViewById<RecyclerView>(R.id.log_list).adapter!!.itemCount > 0 }
scenario.onActivity { it.findViewById<RecyclerView>(R.id.log_list).scrollToPosition(639) }
await(scenario) {
(it.findViewById<RecyclerView>(R.id.log_list).layoutManager as LinearLayoutManager)
.findFirstVisibleItemPosition() > 600
}
var scrollStartedAt = 0L
var scrollCompletedAt = 0L
scenario.onActivity { activity ->
val button = activity.findViewById<View>(R.id.scroll_to_top)
assertEquals(View.VISIBLE, button.visibility)
scrollStartedAt = SystemClock.uptimeMillis()
button.performClick()
}
// Allow two display frames beyond the animation's one-second limit.
await(scenario, 1_100) { activity ->
val list = activity.findViewById<RecyclerView>(R.id.log_list)
val manager = list.layoutManager as LinearLayoutManager
val completed = manager.findFirstVisibleItemPosition() == 0 &&
manager.findViewByPosition(0)?.top == list.paddingTop
if (completed) scrollCompletedAt = SystemClock.uptimeMillis()
completed
}
// Measure on the UI thread, not after the instrumentation thread gets
// scheduled again; that delay is unrelated to the animation itself.
assertTrue("Scroll took ${scrollCompletedAt - scrollStartedAt}ms", scrollCompletedAt - scrollStartedAt <= 1_100)
await(scenario) { logRow(it) != null }
scenario.onActivity { activity ->
val list = activity.findViewById<RecyclerView>(R.id.log_list)
val row = (list.layoutManager as LinearLayoutManager).findViewByPosition(0) as ViewGroup
val metadata = ((row.getChildAt(0) as ViewGroup).getChildAt(0) as ViewGroup)
.getChildAt(0) as ViewGroup
assertTrue(metadata.getChildAt(0) is TextView)
assertEquals(0, metadata.paddingLeft)
assertEquals(0, metadata.getChildAt(0).left)
assertTrue(metadata.getChildAt(1).paddingLeft > 0)
}
scenario.onActivity { activity ->
(activity.findViewById<RecyclerView>(R.id.log_list).layoutManager as LinearLayoutManager)
.scrollToPositionWithOffset(3, 0)
}
await(scenario) {
(it.findViewById<RecyclerView>(R.id.log_list).layoutManager as LinearLayoutManager)
.findFirstVisibleItemPosition() == 3
}
scenario.onActivity {
scrollStartedAt = SystemClock.uptimeMillis()
it.findViewById<View>(R.id.scroll_to_top).performClick()
}
await(scenario, 400) {
val list = it.findViewById<RecyclerView>(R.id.log_list)
val manager = list.layoutManager as LinearLayoutManager
val completed = manager.findFirstVisibleItemPosition() == 0 &&
manager.findViewByPosition(0)?.top == list.paddingTop
if (completed) scrollCompletedAt = SystemClock.uptimeMillis()
completed
}
assertTrue(scrollCompletedAt - scrollStartedAt <= 400)
}
}
@Test fun fittingRowsIgnoreTapsAndForgetExpansionWhenSpaceReturns() {
useCompactFields()
appendRow(0, "A short message")
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { logRow(it) != null }
scenario.onActivity { activity ->
val row = logRow(activity)!!
assertFalse(row.getChildAt(1).isClickable)
row.getChildAt(1).performClick()
assertEquals(1, message(row).maxLines)
assertEquals(null, row.background)
setNarrow(activity, true)
}
await(scenario) { logRow(it)?.getChildAt(1)?.isClickable == true }
var expandedHighlight = false
scenario.onActivity { activity ->
val row = logRow(activity)!!
assertEquals(1, message(row).maxLines)
row.getChildAt(1).performClick()
row.doOnPreDraw { expandedHighlight = row.background != null }
}
await(scenario) { message(logRow(it)!!).maxLines == Int.MAX_VALUE }
assertTrue("Expansion should highlight the row", expandedHighlight)
var collapsedHighlight = false
scenario.onActivity { activity ->
val row = logRow(activity)!!
row.getChildAt(1).performClick()
row.doOnPreDraw { collapsedHighlight = row.background != null }
}
await(scenario) { message(logRow(it)!!).maxLines == 1 }
assertTrue("Collapse should highlight the row", collapsedHighlight)
scenario.onActivity { activity ->
logRow(activity)!!.getChildAt(1).performClick()
}
await(scenario) { message(logRow(it)!!).maxLines == Int.MAX_VALUE }
scenario.onActivity { setNarrow(it, false) }
await(scenario) {
logRow(it)?.let { row -> !row.getChildAt(1).isClickable && message(row).maxLines == 1 } == true
}
scenario.onActivity { setNarrow(it, true) }
await(scenario) { logRow(it)?.getChildAt(1)?.isClickable == true }
scenario.onActivity { assertEquals(1, message(logRow(it)!!).maxLines) }
}
}
@Test fun metadataPreviousContentsAndRetainedImagesCanExpandButMissingImagesCannot() {
useCompactFields()
val imageId = UUID.randomUUID().toString()
val bytes = ByteArrayOutputStream().apply {
Bitmap.createBitmap(8, 8, Bitmap.Config.ARGB_8888).compress(Bitmap.CompressFormat.PNG, 100, this)
}.toByteArray()
appendRow(0, "OK", name = "A very long app name that cannot fit on one line")
appendRow(1, "[image]", imageId = UUID.randomUUID().toString())
appendRow(2, "[image]", imageId = imageId)
appendRow(3, "Now", action = NotificationAction.EDITED, previous = "Before")
EncryptedImageStore(context).save(imageId, bytes)
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { logRow(it, 3) != null }
scenario.onActivity { activity ->
assertTrue(logRow(activity, 0)!!.getChildAt(1).isClickable)
assertTrue(logRow(activity, 1)!!.getChildAt(1).isClickable)
assertFalse(logRow(activity, 2)!!.getChildAt(1).isClickable)
assertTrue(logRow(activity, 3)!!.getChildAt(0).isClickable)
logRow(activity, 0)!!.getChildAt(1).performClick()
}
await(scenario) { (logRow(it, 0)?.getChildAt(1) as? ViewGroup)?.childCount == 3 }
scenario.onActivity {
val cell = logRow(it, 0)!!.getChildAt(1) as ViewGroup
assertEquals("Before", (cell.getChildAt(2) as TextView).text.toString())
cell.performClick()
logRow(it, 1)!!.getChildAt(1).performClick()
}
await(scenario) {
val cell = logRow(it, 1)?.getChildAt(1) as? ViewGroup
cell?.childCount == 2 && cell.getChildAt(1) is android.widget.ImageView
}
}
}
@Test fun rowsOutsideTheViewportAlsoForgetExpansionWhenTheyWouldFit() {
useCompactFields()
repeat(40) { appendRow(it.toLong(), "A short message") }
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { logRow(it) != null }
scenario.onActivity { setNarrow(it, true) }
await(scenario) { logRow(it)?.getChildAt(1)?.isClickable == true }
scenario.onActivity { logRow(it)!!.getChildAt(1).performClick() }
await(scenario) { message(logRow(it)!!).maxLines == Int.MAX_VALUE }
scenario.onActivity { it.findViewById<RecyclerView>(R.id.log_list).scrollToPosition(39) }
await(scenario) { logRow(it) == null }
scenario.onActivity { setNarrow(it, false) }
await(scenario) { logRow(it, 39)?.getChildAt(1)?.isClickable == false }
scenario.onActivity { setNarrow(it, true) }
await(scenario) { logRow(it, 39)?.getChildAt(1)?.isClickable == true }
scenario.onActivity { it.findViewById<RecyclerView>(R.id.log_list).scrollToPosition(0) }
await(scenario) { logRow(it) != null }
scenario.onActivity { assertEquals(1, message(logRow(it)!!).maxLines) }
}
}
@Test fun distantJumpsLoadBothDirectionsAndForgetEvictedExpansion() {
seedHistory()
appendRow(640, "A long message ".repeat(100))
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { logRow(it) != null }
scenario.onActivity { activity ->
val list = activity.findViewById<RecyclerView>(R.id.log_list)
assertEquals(641, list.adapter!!.itemCount)
assertTrue(windowCache(activity).size <= 480)
logRow(activity)!!.getChildAt(1).performClick()
}
await(scenario) { message(logRow(it)!!).maxLines == Int.MAX_VALUE }
scenario.onActivity { activity ->
assertEquals(null, windowCache(activity)[640])
activity.findViewById<RecyclerView>(R.id.log_list).scrollToPosition(640)
}
await(scenario) { logRow(it, 640) != null }
scenario.onActivity { activity ->
assertEquals("Test message 0", message(logRow(activity, 640)!!).text.toString())
assertEquals(null, windowCache(activity)[0])
assertTrue(windowCache(activity).size <= 480)
activity.findViewById<RecyclerView>(R.id.log_list).scrollToPosition(0)
}
await(scenario) { logRow(it) != null }
scenario.onActivity { activity ->
assertTrue(message(logRow(activity)!!).maxLines != Int.MAX_VALUE)
assertEquals(null, windowCache(activity)[640])
assertTrue(windowCache(activity).size <= 480)
}
await(scenario) { it.findViewById<View>(R.id.loading_indicator).visibility == View.GONE }
SystemClock.sleep(250)
scenario.onActivity { activity ->
assertEquals(View.GONE, activity.findViewById<View>(R.id.loading_indicator).visibility)
}
}
}
@Test fun placeholdersHaveExactCollapsedHeightsAndPreserveTheViewportAnchor() {
LogViewSettingsStore(context).save(LogViewSettings(
visibleFields = setOf(LogField.TIMESTAMP, LogField.PACKAGE_NAME, LogField.CONTENTS),
))
repeat(320) { appendRow(it.toLong(), "Hi") }
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { logRow(it) != null }
var rowHeight = 0
var placeholderHeight = 0
var anchorTop = 0
var list: RecyclerView? = null
val gate = java.util.concurrent.CountDownLatch(1)
val requested = java.util.concurrent.CountDownLatch(1)
// Hold the existing store lock briefly so we can observe an unloaded row deterministically.
val companion = EncryptedNotificationLogStore::class.java.getDeclaredField("lock")
.apply { isAccessible = true }.get(null)!!
val blocker = Thread {
synchronized(companion) {
requested.countDown()
gate.await()
}
}
try {
scenario.onActivity { activity ->
rowHeight = logRow(activity)!!.height
list = activity.findViewById(R.id.log_list)
}
blocker.start()
assertTrue(requested.await(5, java.util.concurrent.TimeUnit.SECONDS))
scenario.onActivity {
(list!!.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(300, -11)
}
await(scenario) {
(list!!.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() == 300
}
scenario.onActivity { activity ->
assertEquals(null, logRow(activity, 300))
val root = list!!.findViewHolderForAdapterPosition(300)!!.itemView as ViewGroup
placeholderHeight = (root.getChildAt(0) as ViewGroup).getChildAt(0).height
anchorTop = root.top
assertEquals(rowHeight, placeholderHeight)
assertEquals(View.VISIBLE, activity.findViewById<View>(R.id.loading_indicator).visibility)
}
} finally {
gate.countDown()
blocker.join(5_000)
}
await(scenario) { logRow(it, 300) != null }
scenario.onActivity {
val root = list!!.findViewHolderForAdapterPosition(300)!!.itemView
assertEquals(anchorTop, root.top)
assertEquals(rowHeight, logRow(it, 300)!!.height)
list!!.scrollToPosition(0)
}
await(scenario) { logRow(it) != null }
scenario.onActivity {
assertEquals(null, windowCache(it)[300])
(list!!.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(300, -11)
}
await(scenario) {
(list!!.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() == 300
}
scenario.onActivity {
val root = list!!.findViewHolderForAdapterPosition(300)!!.itemView as ViewGroup
assertEquals(rowHeight, (root.getChildAt(0) as ViewGroup).getChildAt(0).height)
}
}
}
@Test fun collapsedHeightDependsOnMetadataNotContentLengthOrWidth() {
LogViewSettingsStore(context).save(LogViewSettings(visibleFields = LogField.entries.toSet()))
appendRow(0, "Hi")
appendRow(1, "Long text ".repeat(100))
store.append(NotificationLogEntry(recordedAtEpochMillis = 2, packageName = "", appName = "",
action = NotificationAction.APPEARED, contents = "Long text ".repeat(100)))
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { logRow(it, 2) != null }
var fullHeight = 0
var reducedHeight = 0
scenario.onActivity {
fullHeight = logRow(it, 1)!!.height
reducedHeight = logRow(it, 0)!!.height
assertEquals(fullHeight, logRow(it, 2)!!.height)
assertTrue(reducedHeight < fullHeight)
assertTrue(message(logRow(it, 0)!!).maxLines < message(logRow(it, 1)!!).maxLines)
setNarrow(it, true)
}
await(scenario) {
logRow(it, 1)?.width == (220 * context.resources.displayMetrics.density).toInt() -
it.findViewById<RecyclerView>(R.id.log_list).paddingLeft -
it.findViewById<RecyclerView>(R.id.log_list).paddingRight
}
scenario.onActivity {
assertEquals(fullHeight, logRow(it, 1)!!.height)
assertEquals(reducedHeight, logRow(it, 0)!!.height)
LogViewSettingsStore(context).save(LogViewSettings(visibleFields = setOf(LogField.CONTENTS)))
}
scenario.recreate()
await(scenario) { logRow(it, 2) != null }
scenario.onActivity {
val heights = (0..2).map { position -> logRow(it, position)!!.height }
assertEquals(1, heights.distinct().size)
val contents = logRow(it, 1)!!.getChildAt(0) as ViewGroup
assertEquals(1, (contents.getChildAt(0) as TextView).maxLines)
}
}
}
private fun windowCache(activity: MainActivity): LogWindowCache {
val fragment = activity.supportFragmentManager.findFragmentById(R.id.content_frame) as ViewLogsFragment
return ViewLogsFragment::class.java.getDeclaredField("cache").apply { isAccessible = true }
.get(fragment) as LogWindowCache
}
@Test fun longEntriesRespectTheCacheBudgetWithoutAnIdlePrefetchLoop() {
StorageLimitsStore(context).save(StorageLimits(logBytes = 16 * 1024 * 1024))
repeat(200) { appendRow(it.toLong(), "x".repeat(16_000),
action = NotificationAction.EDITED, previous = "y".repeat(16_000)) }
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
await(scenario) { logRow(it) != null && it.findViewById<View>(R.id.loading_indicator).visibility == View.GONE }
var newest: NotificationLogEntry? = null
scenario.onActivity {
val cache = windowCache(it)
assertTrue(cache.bytes <= 4 * 1024 * 1024)
assertTrue(cache.size < 80)
newest = cache[0]
}
SystemClock.sleep(500)
scenario.onActivity {
assertEquals(View.GONE, it.findViewById<View>(R.id.loading_indicator).visibility)
assertTrue(newest === windowCache(it)[0])
it.findViewById<RecyclerView>(R.id.log_list).scrollToPosition(199)
}
await(scenario) { logRow(it, 199) != null && it.findViewById<View>(R.id.loading_indicator).visibility == View.GONE }
scenario.onActivity {
assertTrue(windowCache(it).bytes <= 4 * 1024 * 1024)
assertEquals(null, windowCache(it)[0])
}
}
}
private fun useCompactFields() = LogViewSettingsStore(context).save(
LogViewSettings(visibleFields = setOf(LogField.APP_NAME, LogField.CONTENTS)),
)
private fun appendRow(time: Long, contents: String, name: String = "Test app",
action: NotificationAction = NotificationAction.APPEARED, previous: String? = null, imageId: String? = null,
) = store.append(NotificationLogEntry(
recordedAtEpochMillis = time, packageName = "test.app", appName = name,
action = action, contents = contents, previousContents = previous, imageId = imageId,
))
private fun logRow(activity: MainActivity, position: Int = 0): ViewGroup? {
val list = activity.findViewById<RecyclerView>(R.id.log_list)
val root = list.findViewHolderForAdapterPosition(position)?.itemView as? ViewGroup ?: return null
return (root.getChildAt(0) as ViewGroup).getChildAt(0) as? ViewGroup
}
private fun message(row: ViewGroup) = (row.getChildAt(1) as ViewGroup).getChildAt(0) as TextView
private fun setNarrow(activity: MainActivity, narrow: Boolean) {
val viewport = activity.findViewById<View>(R.id.logs_refresh)
viewport.layoutParams = viewport.layoutParams.apply {
width = if (narrow) (220 * context.resources.displayMetrics.density).toInt()
else ViewGroup.LayoutParams.MATCH_PARENT
}
}
private fun await(scenario: ActivityScenario<MainActivity>, timeout: Long = 10_000, ready: (MainActivity) -> Boolean) {
val deadline = SystemClock.uptimeMillis() + timeout
do {
var satisfied = false
scenario.onActivity { satisfied = ready(it) }
if (satisfied) return
SystemClock.sleep(16)
} while (SystemClock.uptimeMillis() < deadline)
throw AssertionError("Log view did not reach the expected state within ${timeout}ms")
}
}
@@ -0,0 +1,151 @@
package se.ajpanton.notificationsmaster.data
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import se.ajpanton.notificationsmaster.model.NotificationAction
import se.ajpanton.notificationsmaster.model.NotificationLogEntry
import se.ajpanton.notificationsmaster.settings.StorageLimits
import se.ajpanton.notificationsmaster.settings.StorageLimitsStore
import se.ajpanton.notificationsmaster.search.LogContentSearch
import se.ajpanton.notificationsmaster.search.LogSearchSettings
import se.ajpanton.notificationsmaster.search.SearchMode
import java.io.File
@RunWith(AndroidJUnit4::class)
class NotificationLogIndexDeviceTest {
private val context = InstrumentationRegistry.getInstrumentation().targetContext
private val store = EncryptedNotificationLogStore(context)
private val directory = File(context.filesDir, "notification-log")
private val limits = StorageLimitsStore(context)
@Before @After fun clear() {
store.clear()
limits.save(StorageLimits())
}
private fun row(position: Int, length: Int = 30) = NotificationLogEntry(
recordedAtEpochMillis = position.toLong(), packageName = "test.app${position % 3}",
appName = "Secret app ${position % 3}",
action = if (position % 4 == 0) NotificationAction.EDITED else NotificationAction.APPEARED,
contents = "Private contents $position " + "x".repeat(length),
)
@Test fun countsAndRandomFilteredRangesAgreeWithTheHistoryAcrossChunks() {
val rows = List(400) { row(it, 3_000) }.onEach(store::append)
assertTrue(directory.listFiles()!!.count { it.name.endsWith(".bin") } > 3)
listOf(LogQuery(), LogQuery(setOf("test.app1")),
LogQuery(setOf("test.app0", "test.app2"), setOf(NotificationAction.EDITED))).forEach { query ->
val index = store.openIndex(query)
val expected = rows.asReversed().filter(query::matches)
assertEquals(expected.size, index.count)
listOf(0, 5, expected.size / 2, expected.size - 3, expected.size + 1).forEach { start ->
assertEquals(expected.drop(start).take(37), store.readRange(index, start, 37))
}
expected.forEachIndexed { position, entry -> assertEquals(entry.fieldsMask(), index.fieldsAt(position)) }
}
assertEquals(0, store.openIndex(LogQuery(actions = emptySet())).count)
assertTrue(directory.listFiles()!!.filter { it.name.endsWith(".index") }.all { file ->
val bytes = file.readBytes().decodeToString()
"Secret app" !in bytes && "Private contents" !in bytes && "test.app" !in bytes
})
}
@Test fun interruptedOrMissingIndexUpdatesRebuildWithoutChangingLogs() {
val first = row(1)
store.append(first)
val file = directory.listFiles()!!.single { it.name.endsWith(".index") }
val oldIndex = file.readBytes()
val second = row(2)
store.append(second)
file.writeBytes(oldIndex) // Simulate a chunk write completing without its index update.
val recovered = store.openIndex()
assertEquals(listOf(second, first), store.readRange(recovered, 0, 10))
assertTrue(file.delete())
val rebuilt = store.openIndex()
assertEquals(listOf(second, first), store.readRange(rebuilt, 0, 10))
assertTrue(file.isFile)
val orphan = File(directory, "chunk-999999999999.bin.index")
file.copyTo(orphan)
store.openIndex()
assertFalse(orphan.exists())
}
@Test fun presenceFlagsReflectMissingMetadataRegardlessOfContentLength() {
val rows = listOf(row(0), row(1).copy(appName = ""), row(2).copy(packageName = "", contents = "x".repeat(16_000)))
rows.forEach(store::append)
val index = store.openIndex()
rows.asReversed().forEachIndexed { position, entry -> assertEquals(entry.fieldsMask(), index.fieldsAt(position)) }
assertTrue(index.fieldsAt(0) != index.fieldsAt(1))
assertEquals(rows[0].copy(contents = "x".repeat(16_000)).fieldsMask(), rows[0].fieldsMask())
}
@Test fun appendsKeepOldSnapshotsUsableButDeletionInvalidatesAffectedRanges() {
val rows = List(20) { row(it) }.onEach(store::append)
val snapshot = store.openIndex()
store.append(row(20))
assertEquals(rows.asReversed(), store.readRange(snapshot, 0, 30))
assertEquals(21, store.openIndex().count)
assertTrue(store.delete(rows[10].id))
assertNull(store.readRange(snapshot, 0, 30))
assertEquals(20, store.openIndex().count)
assertFalse(store.delete(rows[10].id))
}
@Test fun retentionAndClearAlsoRemoveIndexesAndRespectTheCombinedLimit() {
repeat(160) { store.append(row(it, 3_000)) }
val old = store.openIndex()
limits.save(StorageLimits(logBytes = 32 * 1024))
store.enforceLimits()
val retained = mutableListOf<NotificationLogEntry>()
store.forEachNewest(retained::add)
val current = store.openIndex()
assertEquals(retained, store.readRange(current, 0, 500))
assertTrue(directory.listFiles()!!.sumOf(File::length) <= 32 * 1024)
assertTrue(current.count < old.count)
assertNull(store.readRange(old, 0, old.count))
assertTrue(directory.listFiles()!!.filter { it.name.endsWith(".index") }
.all { File(directory, it.name.removeSuffix(".index")).isFile })
store.clear()
assertTrue(directory.listFiles().isNullOrEmpty())
assertEquals(0, store.openIndex().count)
}
@Test fun contentSearchCombinesWithAppAndEventFiltersAndKeepsRandomAccess() {
val rows = List(180) { position -> row(position, 3_000).copy(
contents = if (position % 7 == 0) "there is some text and here is some more" else "Other contents $position",
previousContents = if (position % 11 == 0) "previous searchable text" else null,
) }.onEach(store::append)
listOf(
LogQuery(search = LogContentSearch(LogSearchSettings("here there", SearchMode.AND))),
LogQuery(setOf("test.app1"), search = LogContentSearch(LogSearchSettings("some", SearchMode.LITERAL))),
LogQuery(actions = setOf(NotificationAction.EDITED),
search = LogContentSearch(LogSearchSettings("previous.*text", SearchMode.REGEX))),
LogQuery(search = LogContentSearch(LogSearchSettings("Secret app", SearchMode.LITERAL))),
LogQuery(search = LogContentSearch(LogSearchSettings("mor ore", SearchMode.AND))),
).forEach { query ->
val snapshot = store.openIndex(query)
val expected = rows.asReversed().filter(query::matches)
assertEquals(expected.size, snapshot.count)
assertEquals(expected.map { it.packageName }.toSet(), snapshot.apps.keys)
assertEquals(expected.map { it.action }.toSet(), snapshot.actions)
listOf(0, 3, expected.size / 2, expected.size + 1).forEach { start ->
assertEquals(expected.drop(start).take(7), store.readRange(snapshot, start, 7))
}
expected.forEachIndexed { position, entry -> assertEquals(entry.fieldsMask(), snapshot.fieldsAt(position)) }
store.append(row(999))
assertEquals(expected, store.readRange(snapshot, 0, 200))
}
}
@Test fun excludedAppsAndEventsAreNotContentSearched() {
store.append(row(1))
val excessive = LogContentSearch(LogSearchSettings((0..128).joinToString(" ") { "term$it" }, SearchMode.AND))
assertEquals(0, store.openIndex(LogQuery(setOf("not.logged"), search = excessive)).count)
assertEquals(0, store.openIndex(LogQuery(actions = emptySet(), search = excessive)).count)
}
}
@@ -0,0 +1,135 @@
package se.ajpanton.notificationsmaster
import android.app.Dialog
import android.os.Bundle
import android.text.InputFilter
import android.view.View
import android.view.WindowManager
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.Spinner
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.google.re2j.PatternSyntaxException
import se.ajpanton.notificationsmaster.search.LogContentSearch
import se.ajpanton.notificationsmaster.search.LogSearchSettings
import se.ajpanton.notificationsmaster.search.SearchMode
import se.ajpanton.notificationsmaster.settings.LogSearchStore
import se.ajpanton.notificationsmaster.settings.AppLockStore
class LogSearchDialogFragment : DialogFragment() {
private lateinit var input: TextInputEditText
private lateinit var mode: Spinner
private lateinit var caseSensitive: CheckBox
private lateinit var wholeWords: CheckBox
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val store = LogSearchStore(requireContext())
val saved = store.load()
val settings = savedInstanceState?.let {
LogSearchSettings(it.getString("text").orEmpty(), SearchMode.valueOf(it.getString("mode")!!),
it.getBoolean("case"), it.getBoolean("words"))
} ?: saved
val context = requireContext()
val body = LinearLayout(context).apply {
orientation = LinearLayout.VERTICAL
val padding = (24 * resources.displayMetrics.density).toInt()
setPadding(padding, 0, padding, 0)
}
val inputLayout = TextInputLayout(context).apply { hint = "Search notification contents" }
input = TextInputEditText(inputLayout.context).apply {
id = R.id.log_search_text
filters = arrayOf(InputFilter.LengthFilter(4096))
setText(settings.text)
setSelectAllOnFocus(true)
inputType = android.text.InputType.TYPE_CLASS_TEXT or android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE
maxLines = 4
}
inputLayout.addView(input)
body.addView(inputLayout)
body.addView(TextView(context).apply { text = "Match using" })
mode = Spinner(context, Spinner.MODE_DROPDOWN).apply {
id = R.id.log_search_mode
setPopupBackgroundDrawable(androidx.appcompat.content.res.AppCompatResources.getDrawable(context, R.drawable.dropdown_popup_background))
adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, SearchMode.entries.map { it.label })
setSelection(settings.mode.ordinal)
}
body.addView(mode)
wholeWords = CheckBox(context).apply {
id = R.id.log_search_words
text = "Match whole words"
isChecked = settings.wholeWords
visibility = if (settings.mode.usesTerms) View.VISIBLE else View.GONE
}
body.addView(wholeWords)
caseSensitive = CheckBox(context).apply {
id = R.id.log_search_case
text = "Case sensitive"
isChecked = settings.caseSensitive
}
body.addView(caseSensitive)
val explanation = TextView(context).also(body::addView)
mode.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) = Unit
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
wholeWords.visibility = if (SearchMode.entries[position].usesTerms) View.VISIBLE else View.GONE
explanation.text = when (SearchMode.entries[position]) {
SearchMode.REGEX -> "Use a regular expression. Matches notification text, not app names."
SearchMode.LITERAL -> "Match the text exactly as typed, including spaces, anywhere in the notification."
SearchMode.AND -> "Every space-separated term needs its own non-overlapping match."
SearchMode.OR -> "At least one term must match. Highlights as many distinct terms as possible."
}
}
}
val dialog = MaterialAlertDialogBuilder(context).setTitle("Search logs").setView(body)
.setNegativeButton("Cancel", null).setNeutralButton("Clear search") { _, _ ->
store.clear()
parentFragmentManager.setFragmentResult(RESULT, Bundle.EMPTY)
}.setPositiveButton("Apply", null).create()
dialog.setOnShowListener {
dialog.getButton(androidx.appcompat.app.AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val updated = draft()
try {
LogContentSearch(updated) // Validate regex before persisting or closing.
} catch (error: PatternSyntaxException) {
inputLayout.error = "Invalid regular expression: ${error.description}"
return@setOnClickListener
}
store.save(updated)
parentFragmentManager.setFragmentResult(RESULT, Bundle.EMPTY)
dismiss()
}
}
return dialog
}
private fun draft() = LogSearchSettings(input.text.toString(), SearchMode.entries[mode.selectedItemPosition],
caseSensitive.isChecked, wholeWords.isChecked)
override fun onStart() {
super.onStart()
if (AppLockStore(requireContext()).enabled) dialog?.window?.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
if ((activity as? MainActivity)?.canShowLogDialog() == false) dismissAllowingStateLoss()
}
override fun onStop() {
// A separate dialog window must not remain above the activity's lock cover.
if (AppLockStore(requireContext()).enabled && activity?.isChangingConfigurations != true) dismissAllowingStateLoss()
super.onStop()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
val settings = draft()
outState.putAll(bundleOf("text" to settings.text, "mode" to settings.mode.name,
"case" to settings.caseSensitive, "words" to settings.wholeWords))
}
companion object { const val RESULT = "log-search-changed" }
}
@@ -26,6 +26,7 @@ import com.google.android.material.navigation.NavigationView
import se.ajpanton.notificationsmaster.databinding.ActivityMainBinding import se.ajpanton.notificationsmaster.databinding.ActivityMainBinding
import se.ajpanton.notificationsmaster.settings.AppLockStore import se.ajpanton.notificationsmaster.settings.AppLockStore
import se.ajpanton.notificationsmaster.settings.LogAppFilterStore import se.ajpanton.notificationsmaster.settings.LogAppFilterStore
import se.ajpanton.notificationsmaster.settings.LogSearchStore
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener { class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
private lateinit var binding: ActivityMainBinding private lateinit var binding: ActivityMainBinding
@@ -293,12 +294,13 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
binding.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.START) binding.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.START)
binding.navViewDrawer.visibility = View.VISIBLE binding.navViewDrawer.visibility = View.VISIBLE
} }
(binding.contentFrame.layoutParams as RelativeLayout.LayoutParams).also { params -> val params = binding.contentFrame.layoutParams as RelativeLayout.LayoutParams
params.removeRule(if (permanentSidebar) RelativeLayout.BELOW else RelativeLayout.ALIGN_PARENT_TOP) val anchorRule = if (permanentSidebar) RelativeLayout.ALIGN_PARENT_TOP else RelativeLayout.BELOW
params.addRule( val otherRule = if (permanentSidebar) RelativeLayout.BELOW else RelativeLayout.ALIGN_PARENT_TOP
if (permanentSidebar) RelativeLayout.ALIGN_PARENT_TOP else RelativeLayout.BELOW, val anchor = if (permanentSidebar) RelativeLayout.TRUE else R.id.toolbar
if (permanentSidebar) RelativeLayout.TRUE else R.id.toolbar, if (params.getRule(anchorRule) != anchor || params.getRule(otherRule) != 0) {
) params.removeRule(otherRule)
params.addRule(anchorRule, anchor)
binding.contentFrame.layoutParams = params binding.contentFrame.layoutParams = params
} }
updateNavigationHeaderHeight(binding.navViewDrawer) updateNavigationHeaderHeight(binding.navViewDrawer)
@@ -337,6 +339,9 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
if (currentItemId == R.id.nav_view_logs) invalidateOptionsMenu() if (currentItemId == R.id.nav_view_logs) invalidateOptionsMenu()
} }
internal fun canShowLogDialog() = !appLockStore.enabled ||
(currentItemId == R.id.nav_view_logs && !needsUnlock)
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
return updateToolbarActions(menu) return updateToolbarActions(menu)
} }
@@ -354,15 +359,29 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
icon?.setTint(getColor(R.color.on_primary)) icon?.setTint(getColor(R.color.on_primary))
setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS) setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
} }
val searching = LogSearchStore(this).load().active
menu.add(Menu.NONE, MENU_LOG_SEARCH, Menu.NONE, "Search notification contents").apply {
setIcon(if (searching) R.drawable.ic_log_search_active else R.drawable.ic_log_search)
icon?.setTint(getColor(R.color.on_primary))
setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
}
if (active || searching) menu.add(Menu.NONE, MENU_CLEAR_LOG_FILTERS, Menu.NONE, "Clear all filters").apply {
setIcon(R.drawable.ic_clear_log_filters)
icon?.setTint(getColor(R.color.on_primary))
setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
}
return true return true
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == MENU_LOG_FILTER) { val logs = supportFragmentManager.findFragmentById(R.id.content_frame) as? ViewLogsFragment
(supportFragmentManager.findFragmentById(R.id.content_frame) as? ViewLogsFragment)?.showAppFilter() when (item.itemId) {
return true MENU_LOG_FILTER -> logs?.showAppFilter()
MENU_LOG_SEARCH -> logs?.showSearch()
MENU_CLEAR_LOG_FILTERS -> logs?.clearFilters()
else -> return super.onOptionsItemSelected(item)
} }
return super.onOptionsItemSelected(item) return true
} }
private enum class Page( private enum class Page(
@@ -391,6 +410,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
private companion object { private companion object {
const val STATE_CURRENT_ITEM_ID = "current_item_id" const val STATE_CURRENT_ITEM_ID = "current_item_id"
const val MENU_LOG_FILTER = 1 const val MENU_LOG_FILTER = 1
const val MENU_LOG_SEARCH = 2
const val MENU_CLEAR_LOG_FILTERS = 3
const val PERMANENT_SIDEBAR_FOLDED_WIDTH_MULTIPLIER = 1.1f const val PERMANENT_SIDEBAR_FOLDED_WIDTH_MULTIPLIER = 1.1f
} }
} }
@@ -20,6 +20,7 @@ import se.ajpanton.notificationsmaster.export.LogExporter
import se.ajpanton.notificationsmaster.settings.StorageLimits import se.ajpanton.notificationsmaster.settings.StorageLimits
import se.ajpanton.notificationsmaster.settings.StorageLimitsStore import se.ajpanton.notificationsmaster.settings.StorageLimitsStore
import se.ajpanton.notificationsmaster.settings.LogAppFilterStore import se.ajpanton.notificationsmaster.settings.LogAppFilterStore
import se.ajpanton.notificationsmaster.settings.LogSearchStore
import java.util.zip.ZipOutputStream import java.util.zip.ZipOutputStream
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -79,6 +80,7 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
private fun confirmClearLogs() { private fun confirmClearLogs() {
val filterStore = LogAppFilterStore(requireContext()) val filterStore = LogAppFilterStore(requireContext())
val searchStore = LogSearchStore(requireContext())
val dialog = MaterialAlertDialogBuilder(requireContext()) val dialog = MaterialAlertDialogBuilder(requireContext())
.setTitle("Clear logs?") .setTitle("Clear logs?")
.setMessage("This permanently removes every stored notification log and copied image.") .setMessage("This permanently removes every stored notification log and copied image.")
@@ -86,9 +88,10 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
.setPositiveButton("Clear") { _, _ -> .setPositiveButton("Clear") { _, _ ->
clearLogs() clearLogs()
} }
if (filterStore.selectedPackages().isNotEmpty()) { if (filterStore.selectedPackages().isNotEmpty() || searchStore.load().active) {
dialog.setNeutralButton("Clear and reset filter") { _, _ -> dialog.setNeutralButton("Clear and reset filters") { _, _ ->
filterStore.clear() filterStore.clear()
searchStore.clear()
(activity as? MainActivity)?.updateLogFilterAction() (activity as? MainActivity)?.updateLogFilterAction()
clearLogs() clearLogs()
} }
@@ -12,15 +12,23 @@ import android.content.ComponentName
import android.app.NotificationManager import android.app.NotificationManager
import android.text.TextPaint import android.text.TextPaint
import android.text.TextUtils import android.text.TextUtils
import android.text.SpannableString
import android.text.Spanned
import android.text.style.BackgroundColorSpan
import android.view.Gravity import android.view.Gravity
import android.view.MotionEvent
import android.view.View import android.view.View
import android.view.animation.DecelerateInterpolator
import android.widget.ImageView import android.widget.ImageView
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import se.ajpanton.notificationsmaster.data.EncryptedImageStore import se.ajpanton.notificationsmaster.data.EncryptedImageStore
import se.ajpanton.notificationsmaster.data.EncryptedNotificationLogStore import se.ajpanton.notificationsmaster.data.EncryptedNotificationLogStore
import se.ajpanton.notificationsmaster.data.NewestLogCursor import se.ajpanton.notificationsmaster.data.LogQuery
import se.ajpanton.notificationsmaster.data.LogWindowCache
import se.ajpanton.notificationsmaster.data.NotificationLogIndex
import se.ajpanton.notificationsmaster.data.fieldsMask
import se.ajpanton.notificationsmaster.databinding.FragmentViewLogsBinding import se.ajpanton.notificationsmaster.databinding.FragmentViewLogsBinding
import se.ajpanton.notificationsmaster.model.NotificationAction import se.ajpanton.notificationsmaster.model.NotificationAction
import se.ajpanton.notificationsmaster.model.NotificationLogEntry import se.ajpanton.notificationsmaster.model.NotificationLogEntry
@@ -28,112 +36,289 @@ import se.ajpanton.notificationsmaster.settings.LogField
import se.ajpanton.notificationsmaster.settings.LogViewSettings import se.ajpanton.notificationsmaster.settings.LogViewSettings
import se.ajpanton.notificationsmaster.settings.LogViewSettingsStore import se.ajpanton.notificationsmaster.settings.LogViewSettingsStore
import se.ajpanton.notificationsmaster.settings.LogAppFilterStore import se.ajpanton.notificationsmaster.settings.LogAppFilterStore
import se.ajpanton.notificationsmaster.settings.LogSearchStore
import se.ajpanton.notificationsmaster.search.LogContentSearch
import se.ajpanton.notificationsmaster.search.SearchTooComplexException
import se.ajpanton.notificationsmaster.settings.TimestampFormatter import se.ajpanton.notificationsmaster.settings.TimestampFormatter
import se.ajpanton.notificationsmaster.settings.DisplayEvent import se.ajpanton.notificationsmaster.settings.DisplayEvent
import se.ajpanton.notificationsmaster.capture.NotificationCaptureService import se.ajpanton.notificationsmaster.capture.NotificationCaptureService
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.doOnPreDraw import androidx.core.view.doOnPreDraw
import androidx.core.view.doOnAttach
import androidx.core.view.children
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.DiffUtil
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runInterruptible
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlin.math.roundToInt
import kotlin.math.sqrt
import java.util.Calendar
class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) { class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
private var binding: FragmentViewLogsBinding? = null private var binding: FragmentViewLogsBinding? = null
private val expandedIds = mutableSetOf<String>() private val expandedIds = mutableSetOf<String>()
private val rows = mutableListOf<NotificationLogEntry>() private val cache = LogWindowCache()
private var nextCursor: NewestLogCursor? = null private var index: NotificationLogIndex? = null
private var metadataSamples = emptyList<NotificationLogEntry>()
private var windowFirst = 0
private var windowLast = 0
private var prefetchLimited = false
private var loading = false private var loading = false
private var noMoreRows = false
private var loadGeneration = 0 private var loadGeneration = 0
private val logAdapter = LogAdapter() private var loadJob: Job? = null
private var windowJob: Job? = null
private var scrollToTopAnimator: ValueAnimator? = null
private val logAdapter by lazy { LogAdapter() }
private val imageStore by lazy { EncryptedImageStore(requireContext().applicationContext) }
private lateinit var appFilterStore: LogAppFilterStore private lateinit var appFilterStore: LogAppFilterStore
private var search: LogContentSearch? = null
private var searchError: String? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
binding = FragmentViewLogsBinding.bind(view) binding = FragmentViewLogsBinding.bind(view)
appFilterStore = LogAppFilterStore(requireContext()) appFilterStore = LogAppFilterStore(requireContext())
binding!!.logsRefresh.setOnRefreshListener { loadLogs(reset = true) } parentFragmentManager.setFragmentResultListener(LogSearchDialogFragment.RESULT, viewLifecycleOwner) { _, _ ->
refreshAppFilter()
}
binding!!.logsRefresh.setOnRefreshListener { loadLogs() }
binding!!.logList.layoutManager = LinearLayoutManager(requireContext()) binding!!.logList.layoutManager = LinearLayoutManager(requireContext())
binding!!.logList.adapter = logAdapter binding!!.logList.adapter = logAdapter
binding!!.logList.addOnLayoutChangeListener { list, left, _, right, _, oldLeft, _, oldRight, _ ->
if (right - left != oldRight - oldLeft) list.post {
binding?.takeIf { it.logList === list }?.let(::render)
}
}
binding!!.logList.addOnScrollListener(object : RecyclerView.OnScrollListener() { binding!!.logList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if (newState == RecyclerView.SCROLL_STATE_IDLE && scrollToTopAnimator?.isRunning != true) requestWindow()
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
val scrollY = recyclerView.computeVerticalScrollOffset() val scrollY = recyclerView.computeVerticalScrollOffset()
binding?.scrollToTop?.visibility = if (scrollY > dp(SCROLL_TO_TOP_THRESHOLD_DP)) View.VISIBLE else View.GONE binding?.scrollToTop?.visibility = if (scrollY > dp(SCROLL_TO_TOP_THRESHOLD_DP)) View.VISIBLE else View.GONE
if (!noMoreRows && !loading && !recyclerView.canScrollVertically(1)) { if (scrollToTopAnimator?.isRunning != true) requestWindow()
loadLogs(reset = false)
}
} }
}) })
binding!!.scrollToTop.setOnClickListener { binding!!.logList.addOnItemTouchListener(object : RecyclerView.SimpleOnItemTouchListener() {
binding?.logList?.smoothScrollToPosition(0) override fun onInterceptTouchEvent(recyclerView: RecyclerView, event: MotionEvent): Boolean {
} if (event.actionMasked == MotionEvent.ACTION_DOWN) scrollToTopAnimator?.cancel()
loadLogs(reset = true) return false
}
})
binding!!.scrollToTop.setOnClickListener { scrollToTop() }
} }
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
loadLogs(reset = true) loadLogs()
} }
override fun onDestroyView() { override fun onDestroyView() {
scrollToTopAnimator?.cancel()
loadJob?.cancel()
windowJob?.cancel()
cache.clear()
expandedIds.clear()
index = null
metadataSamples = emptyList()
binding?.logList?.adapter = null
binding = null binding = null
super.onDestroyView() super.onDestroyView()
} }
private fun loadLogs(reset: Boolean) { private fun loadLogs() {
if (loading && !reset) return
if (!reset && noMoreRows) return
val context = requireContext().applicationContext val context = requireContext().applicationContext
if (reset) { loadJob?.cancel()
rows.clear() windowJob?.cancel()
nextCursor = null scrollToTopAnimator?.cancel()
noMoreRows = false cache.clear()
loadGeneration++ expandedIds.clear()
} index = null
metadataSamples = emptyList()
loadGeneration++
val generation = loadGeneration val generation = loadGeneration
loading = true val settings = LogViewSettingsStore(context).load()
binding?.logsRefresh?.isRefreshing = true search = LogSearchStore(context).load().takeIf { it.active }?.let(::LogContentSearch)
viewLifecycleOwner.lifecycleScope.launch { searchError = null
val page = withContext(Dispatchers.IO) { val query = LogQuery(appFilterStore.selectedPackages(), NotificationAction.entries
EncryptedNotificationLogStore(context).readNewest(nextCursor, PAGE_SIZE) .filterTo(mutableSetOf()) { eventFor(it) in settings.visibleEvents }, search)
setLoading(true)
binding?.let(::render)
loadJob = viewLifecycleOwner.lifecycleScope.launch {
val snapshot = try {
runInterruptible(Dispatchers.IO) { EncryptedNotificationLogStore(context).openIndex(query) }
} catch (error: SearchTooComplexException) {
if (generation == loadGeneration) {
searchError = error.message
setLoading(false)
binding?.let(::render)
}
return@launch
} }
if (generation != loadGeneration) return@launch if (generation != loadGeneration) return@launch
nextCursor = page.nextCursor index = snapshot
noMoreRows = nextCursor == null metadataSamples = snapshot.apps.map { (packageName, name) ->
val visibleBefore = visibleRows(LogViewSettingsStore(requireContext()).load()).size NotificationLogEntry(id = "", recordedAtEpochMillis = System.currentTimeMillis(),
rows += page.entries packageName = packageName, appName = name, action = NotificationAction.APPEARED, contents = null)
} + snapshot.actions.map { action ->
NotificationLogEntry(id = "", recordedAtEpochMillis = System.currentTimeMillis(),
packageName = "", appName = "", action = action, contents = null)
}
// Reserve timestamp width for every localized month and both AM/PM labels,
// without scanning the notification contents or widening on each loaded page.
if (LogField.TIMESTAMP in settings.visibleFields) {
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
metadataSamples += (0..11).flatMap { month -> listOf(11, 23).map { hour ->
calendar.set(year, month, 28, hour, 59, 59)
NotificationLogEntry(id = "", recordedAtEpochMillis = calendar.timeInMillis,
packageName = "", appName = "", action = NotificationAction.APPEARED, contents = null)
} }
}
setLoading(false)
binding?.let(::render) binding?.let(::render)
val visibleAfter = visibleRows(LogViewSettingsStore(requireContext()).load()).size requestWindow()
val skipFilteredPage = appFilterStore.selectedPackages().isNotEmpty() && }
visibleAfter == visibleBefore && !noMoreRows }
loading = false
binding?.logsRefresh?.isRefreshing = false private fun setLoading(value: Boolean) {
if (skipFilteredPage) loadLogs(reset = false) loading = value
binding?.loadingIndicator?.visibility = if (value) View.VISIBLE else View.GONE
binding?.loadingSpinner?.visibility = if (value) View.VISIBLE else View.GONE
if (!value) binding?.logsRefresh?.isRefreshing = false
}
private fun requestWindow(first: Int? = null, last: Int? = null) {
val list = binding?.logList ?: return
if (list.isComputingLayout) {
list.post { requestWindow(first, last) }
return
}
val manager = list.layoutManager as LinearLayoutManager
val snapshot = index ?: return
if (snapshot.count == 0) return
val newFirst = (first ?: manager.findFirstVisibleItemPosition()).coerceAtLeast(0)
val newLast = (last ?: manager.findLastVisibleItemPosition()).coerceAtLeast(newFirst)
if (newFirst != windowFirst || newLast != windowLast) prefetchLimited = false
windowFirst = newFirst
windowLast = newLast
evictDistantRows()
if (windowJob?.isActive == true) return
if (nextPage(emptySet()) == null) return
val generation = loadGeneration
val context = requireContext().applicationContext
windowJob = viewLifecycleOwner.lifecycleScope.launch {
val attempted = mutableSetOf<Int>()
try {
val store = withContext(Dispatchers.IO) { EncryptedNotificationLogStore(context) }
while (true) {
val page = nextPage(attempted) ?: break
attempted += page
setLoading(true)
val start = page * PAGE_SIZE
val entries = withContext(Dispatchers.IO) { store.readRange(snapshot, start, PAGE_SIZE) }
if (generation != loadGeneration) return@launch
if (entries == null) {
loadLogs()
return@launch
}
// Keep the same visible row anchored when replacing placeholders.
val anchorPosition = manager.findFirstVisibleItemPosition()
val anchorTop = manager.findViewByPosition(anchorPosition)?.top
cache.put(start, entries)
evictDistantRows()
logAdapter.notifyItemRangeChanged(start, entries.size)
if (anchorTop != null && scrollToTopAnimator?.isRunning != true) {
manager.scrollToPositionWithOffset(anchorPosition, anchorTop - list.paddingTop)
}
}
} finally {
if (generation == loadGeneration) setLoading(false)
}
}
}
private fun nextPage(attempted: Set<Int>): Int? {
val count = index?.count ?: return null
val margin = if (prefetchLimited) 0 else PREFETCH_ROWS
val first = (windowFirst - margin).coerceAtLeast(0)
val last = (windowLast + margin).coerceAtMost(count - 1)
return (first / PAGE_SIZE..last / PAGE_SIZE).filter { it !in attempted }
.sortedBy { page ->
val start = page * PAGE_SIZE
maxOf(windowFirst - (start + PAGE_SIZE - 1), start - windowLast, 0)
}.firstOrNull { page ->
(maxOf(first, page * PAGE_SIZE)..minOf(last, (page + 1) * PAGE_SIZE - 1))
.any { cache[it] == null }
}
}
private fun evictDistantRows() {
cache.retain(windowFirst, windowLast, PREFETCH_ROWS + PAGE_SIZE).forEach { (position, id) ->
if (position in (windowFirst - PREFETCH_ROWS)..(windowLast + PREFETCH_ROWS)) prefetchLimited = true
expandedIds.remove(id)
logAdapter.notifyItemChanged(position)
} }
} }
private fun render(view: FragmentViewLogsBinding) { private fun render(view: FragmentViewLogsBinding) {
val settings = LogViewSettingsStore(requireContext()).load() val settings = LogViewSettingsStore(requireContext()).load()
val visibleRows = visibleRows(settings) view.emptyView.visibility = if ((index?.count ?: 0) == 0 && !loading) View.VISIBLE else View.GONE
view.emptyView.visibility = if (visibleRows.isEmpty()) View.VISIBLE else View.GONE view.emptyView.text = searchError ?: if (hasNotificationAccess()) {
view.emptyView.text = if (hasNotificationAccess()) { if (appFilterStore.selectedPackages().isEmpty() && search == null) "No logs yet." else "No logs match the filters."
if (appFilterStore.selectedPackages().isEmpty()) "No logs yet." else "No logs match the app filter."
} else { } else {
"Notification access is disabled. Enable it in Settings." "Notification access is disabled. Enable it in Settings."
} }
logAdapter.submit(visibleRows, settings, metadataColumnWidth(view.logList.width, visibleRows, settings)) logAdapter.submit(settings, metadataColumnWidth(view.logList.width, metadataSamples, settings))
}
private fun scrollToTop() {
val list = binding?.logList ?: return
val manager = list.layoutManager as? LinearLayoutManager ?: return
val position = manager.findFirstVisibleItemPosition()
val first = manager.findViewByPosition(position) ?: return
list.stopScroll()
scrollToTopAnimator?.cancel()
val start = position + (list.paddingTop - first.top).toFloat() / first.height.coerceAtLeast(1)
if (start <= 0f) return
requestWindow(0, 12)
val distanceDp = start * first.height / resources.displayMetrics.density
scrollToTopAnimator = ValueAnimator.ofFloat(start, 0f).apply {
duration = (sqrt(distanceDp) * 12).toLong().coerceIn(80L, 1_000L)
interpolator = DecelerateInterpolator()
addUpdateListener { animator ->
val target = animator.animatedValue as Float
val rowPosition = target.toInt()
manager.scrollToPositionWithOffset(rowPosition, 0)
// Use the target row's actual height, including expanded contents.
list.doOnPreDraw {
if (scrollToTopAnimator !== animator || !animator.isRunning) return@doOnPreDraw
manager.findViewByPosition(rowPosition)?.let { row ->
list.scrollBy(0, ((target - rowPosition) * row.height).roundToInt())
}
}
}
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
scrollToTopAnimator = null
requestWindow()
}
})
}
scrollToTopAnimator?.start()
} }
/** Called by MainActivity's header action. Selections persist until Reset is chosen. */ /** Called by MainActivity's header action. Selections persist until Reset is chosen. */
fun showAppFilter() { fun showAppFilter() {
val appContext = requireContext().applicationContext val appContext = requireContext().applicationContext
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
val apps = withContext(Dispatchers.IO) { val apps = runInterruptible(Dispatchers.IO) {
val byPackage = linkedMapOf<String, LoggedApp>() val byPackage = linkedMapOf<String, LoggedApp>()
appFilterStore.selectedApps().forEach { app -> appFilterStore.selectedApps().forEach { app ->
val name = app.name.takeUnless { it == app.packageName } ?: runCatching { val name = app.name.takeUnless { it == app.packageName } ?: runCatching {
@@ -143,8 +328,8 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
}.getOrDefault(app.packageName) }.getOrDefault(app.packageName)
byPackage[app.packageName] = LoggedApp(name, app.packageName) byPackage[app.packageName] = LoggedApp(name, app.packageName)
} }
EncryptedNotificationLogStore(appContext).forEachNewest { entry -> EncryptedNotificationLogStore(appContext).openIndex().apps.forEach { (packageName, name) ->
byPackage[entry.packageName] = LoggedApp(entry.appName, entry.packageName) byPackage[packageName] = LoggedApp(name, packageName)
} }
byPackage.values.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.name }) byPackage.values.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.name })
} }
@@ -178,69 +363,122 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
private fun refreshAppFilter() { private fun refreshAppFilter() {
(activity as? MainActivity)?.updateLogFilterAction() (activity as? MainActivity)?.updateLogFilterAction()
loadLogs(reset = true) loadLogs()
} }
private fun visibleRows(settings: LogViewSettings): List<NotificationLogEntry> { fun showSearch() {
val selectedPackages = appFilterStore.selectedPackages() if (parentFragmentManager.findFragmentByTag("log-search") == null) {
return rows.filter { entry -> LogSearchDialogFragment().show(parentFragmentManager, "log-search")
eventFor(entry.action) in settings.visibleEvents &&
(selectedPackages.isEmpty() || entry.packageName in selectedPackages)
} }
} }
private fun logRowView(entry: NotificationLogEntry, settings: LogViewSettings, metadataColumnWidth: Int): View = LinearLayout(requireContext()).apply { fun clearFilters() {
orientation = LinearLayout.HORIZONTAL appFilterStore.clear()
gravity = android.view.Gravity.TOP LogSearchStore(requireContext()).clear()
layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT).apply { refreshAppFilter()
bottomMargin = 0 }
private inner class LogRow(
private val entry: NotificationLogEntry,
settings: LogViewSettings,
private val metadataColumnWidth: Int,
private var expanded: Boolean = entry.id in expandedIds,
) : LinearLayout(requireContext()) {
private val metadata = metadataValues(entry, settings)
private val contents = entry.contents
private val hasContents = LogField.CONTENTS in settings.visibleFields &&
(!contents.isNullOrEmpty() || (entry.action == NotificationAction.EDITED && !entry.previousContents.isNullOrEmpty()))
private val collapsedHeight = logAdapter.collapsedHeight(entry.fieldsMask())
private val collapsedContentLines = logAdapter.contentLines(collapsedHeight)
private var highlightAnimator: ValueAnimator? = null
init {
orientation = HORIZONTAL
gravity = Gravity.TOP
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
render()
addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> updateExpandability() }
setOnLongClickListener { showDeleteDialog(entry.id) }
} }
fun renderExpanded(expanded: Boolean) { private fun render() {
removeAllViews() removeAllViews()
val metadata = metadataValues(entry, settings) layoutParams.height = if (expanded) LayoutParams.WRAP_CONTENT else collapsedHeight
val contents = entry.contents val metadataCell = metadataCell(metadata, expanded, ::toggleExpanded, entry.id).apply {
val hasContents = LogField.CONTENTS in settings.visibleFields && !contents.isNullOrEmpty() layoutParams = LayoutParams(
val toggleExpanded = { if (hasContents) metadataColumnWidth else LayoutParams.MATCH_PARENT,
val collapsing = expanded LayoutParams.WRAP_CONTENT,
val collapseAnchor = if (collapsing) captureCollapseAnchor(this@apply) else null
if (!expandedIds.add(entry.id)) expandedIds.remove(entry.id)
renderExpanded(entry.id in expandedIds)
if (collapsing) {
val highlight = { highlightCollapsedRow(this@apply) }
if (collapseAnchor != null) {
restoreCollapseAnchor(collapseAnchor, highlight)
} else {
this@apply.doOnPreDraw { highlight() }
}
}
Unit
}
val metadataCell = metadataCell(metadata, expanded, toggleExpanded, entry.id).apply {
layoutParams = LinearLayout.LayoutParams(
if (hasContents) metadataColumnWidth else LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
).apply { ).apply {
if (hasContents) marginEnd = dp(COLUMN_GAP_DP) if (hasContents) marginEnd = dp(COLUMN_GAP_DP)
} }
} }
if (metadata.isNotEmpty()) addView(metadataCell) if (metadata.isNotEmpty()) addView(metadataCell)
if (hasContents) { if (hasContents) {
val contentsCell = contentsCell(entry, contents!!, metadata.size.coerceAtLeast(1), expanded, toggleExpanded).apply { val contentsCell = contentsCell(entry, contents.orEmpty(), collapsedContentLines, expanded, ::toggleExpanded).apply {
layoutParams = LinearLayout.LayoutParams( layoutParams = LayoutParams(
if (metadata.isNotEmpty()) 0 else LinearLayout.LayoutParams.MATCH_PARENT, if (metadata.isNotEmpty()) 0 else LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, if (expanded) LayoutParams.WRAP_CONTENT else collapsedHeight,
if (metadata.isNotEmpty()) 1f else 0f, if (metadata.isNotEmpty()) 1f else 0f,
) )
} }
addView(contentsCell) addView(contentsCell)
} }
doOnPreDraw { updateExpandability() }
} }
renderExpanded(entry.id in expandedIds) private fun canExpand(): Boolean {
setOnLongClickListener { if (metadata.isNotEmpty()) {
showDeleteDialog(entry.id) val cell = getChildAt(0) as LinearLayout
if (cell.children.any { (it as TextView).hasHiddenText(1) }) return true
}
if (!hasContents) return false
val cell = getChildAt(childCount - 1) as LinearLayout
return (cell.getChildAt(0) as TextView).hasHiddenText(collapsedContentLines) ||
(entry.action == NotificationAction.EDITED && !entry.previousContents.isNullOrEmpty()) ||
(entry.imageId?.let(imageStore::exists) == true)
} }
private fun updateExpandability() {
val enabled = canExpand()
if (!enabled) {
expandedIds.remove(entry.id)
if (expanded) {
expanded = false
render()
}
}
children.forEach {
it.isClickable = enabled
it.isFocusable = enabled
}
}
private fun toggleExpanded() {
if (!canExpand()) return
val anchor = if (expanded) captureCollapseAnchor(this) else null
expanded = !expanded
if (expanded) expandedIds.add(entry.id) else expandedIds.remove(entry.id)
render()
val highlight = {
highlightAnimator?.cancel()
highlightAnimator = highlightLogRow(this)
}
if (anchor != null) restoreCollapseAnchor(anchor, highlight)
else doOnPreDraw { highlight() }
}
override fun onDetachedFromWindow() {
highlightAnimator?.cancel()
highlightAnimator = null
super.onDetachedFromWindow()
}
}
private fun TextView.hasHiddenText(collapsedLines: Int): Boolean {
val layout = layout ?: return false
return layout.lineCount > collapsedLines ||
(0 until layout.lineCount).any { layout.getEllipsisCount(it) > 0 } ||
(layout.lineCount > 0 && layout.getLineEnd(layout.lineCount - 1) < text.length)
} }
private fun metadataCell( private fun metadataCell(
@@ -250,7 +488,7 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
entryId: String, entryId: String,
): LinearLayout = LinearLayout(requireContext()).apply { ): LinearLayout = LinearLayout(requireContext()).apply {
orientation = LinearLayout.VERTICAL orientation = LinearLayout.VERTICAL
setPadding(dp(CELL_HORIZONTAL_PADDING_DP), dp(CELL_VERTICAL_PADDING_DP), dp(CELL_HORIZONTAL_PADDING_DP), dp(CELL_VERTICAL_PADDING_DP)) setPadding(0, dp(CELL_VERTICAL_PADDING_DP), dp(METADATA_END_PADDING_DP), dp(CELL_VERTICAL_PADDING_DP))
isClickable = true isClickable = true
isFocusable = true isFocusable = true
values.forEach { value -> values.forEach { value ->
@@ -290,20 +528,22 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
setLineSpacing(0f, 1f) setLineSpacing(0f, 1f)
} }
addView(message) addView(message)
var previous: TextView? = null
if (expanded && entry.action == NotificationAction.EDITED && !entry.previousContents.isNullOrEmpty()) { if (expanded && entry.action == NotificationAction.EDITED && !entry.previousContents.isNullOrEmpty()) {
addView(previousDivider()) addView(previousDivider())
addView(TextView(context).apply { previous = TextView(context).apply {
text = entry.previousContents text = entry.previousContents
setLineSpacing(0f, 1f) setLineSpacing(0f, 1f)
}) }.also(::addView)
} }
search?.let { matcher -> highlightContents(matcher, listOf(message, previous),
listOf(contents, entry.previousContents.orEmpty())) }
if (expanded && entry.imageId != null) { if (expanded && entry.imageId != null) {
val imageId = entry.imageId val imageId = entry.imageId
val appContext = requireContext().applicationContext
val contentsContainer = this val contentsContainer = this
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
val bitmap = withContext(Dispatchers.IO) { val bitmap = withContext(Dispatchers.IO) {
EncryptedImageStore(appContext).read(imageId)?.let { bytes -> imageStore.read(imageId)?.let { bytes ->
BitmapFactory.decodeByteArray(bytes, 0, bytes.size) BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
} }
} }
@@ -327,6 +567,56 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
setOnLongClickListener { showDeleteDialog(entry.id) } setOnLongClickListener { showDeleteDialog(entry.id) }
} }
private fun highlightContents(matcher: LogContentSearch, views: List<TextView?>, texts: List<String>) {
val generation = loadGeneration
// Only attached rows get spans; neither placeholders nor the viewport cache
// retain highlighted strings. Detaching cancels potentially expensive work.
val owner = views.first()!!
owner.doOnAttach {
val job = viewLifecycleOwner.lifecycleScope.launch {
val spans = try {
runInterruptible(Dispatchers.Default) { matcher.spans(texts) }
} catch (error: SearchTooComplexException) {
if (generation == loadGeneration && searchError == null) {
searchError = error.message
android.widget.Toast.makeText(requireContext(), "Cannot highlight this entry. ${error.message}", android.widget.Toast.LENGTH_LONG).show()
}
return@launch
}
if (generation != loadGeneration || !owner.isAttachedToWindow) return@launch
val color = ContextCompat.getColor(requireContext(), R.color.log_search_highlight)
views.forEachIndexed { field, view ->
val matches = spans.filter { it.field == field }
if (view == null || matches.isEmpty()) return@forEachIndexed
val highlighted = SpannableString(texts[field])
// Adjacent hits share one background span, keeping long repeated
// literal/regex matches cheap for Android's text layout.
var start = -1
var end = -1
fun finish() {
if (start >= 0) highlighted.setSpan(BackgroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
matches.forEach { span ->
if (span.start == end) end = span.end else {
finish()
start = span.start
end = span.end
}
}
finish()
view.text = highlighted
}
}
owner.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(view: View) = Unit
override fun onViewDetachedFromWindow(view: View) {
job.cancel()
view.removeOnAttachStateChangeListener(this)
}
})
}
}
private fun previousDivider(): LinearLayout = LinearLayout(requireContext()).apply { private fun previousDivider(): LinearLayout = LinearLayout(requireContext()).apply {
gravity = Gravity.CENTER_VERTICAL gravity = Gravity.CENTER_VERTICAL
setPadding(0, dp(8), 0, dp(4)) setPadding(0, dp(8), 0, dp(4))
@@ -347,8 +637,8 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
private fun metadataValues(entry: NotificationLogEntry, settings: LogViewSettings): List<MetadataValue> = buildList { private fun metadataValues(entry: NotificationLogEntry, settings: LogViewSettings): List<MetadataValue> = buildList {
if (LogField.TIMESTAMP in settings.visibleFields) add(MetadataValue(timestamp(entry.recordedAtEpochMillis, settings, entry.eventTimeZoneId), 12f, isTimestamp = true)) if (LogField.TIMESTAMP in settings.visibleFields) add(MetadataValue(timestamp(entry.recordedAtEpochMillis, settings, entry.eventTimeZoneId), 12f, isTimestamp = true))
if (LogField.APP_NAME in settings.visibleFields) add(MetadataValue(entry.appName, 15f, indented = true, bold = true)) if (LogField.APP_NAME in settings.visibleFields && entry.appName.isNotBlank()) add(MetadataValue(entry.appName, 15f, indented = true, bold = true))
if (LogField.PACKAGE_NAME in settings.visibleFields) add(MetadataValue(entry.packageName, 12f, indented = true, breakBeforePeriods = true)) if (LogField.PACKAGE_NAME in settings.visibleFields && entry.packageName.isNotBlank()) add(MetadataValue(entry.packageName, 12f, indented = true, breakBeforePeriods = true))
if (LogField.ACTION in settings.visibleFields) add(MetadataValue(actionLabel(entry.action), 14f, indented = true)) if (LogField.ACTION in settings.visibleFields) add(MetadataValue(actionLabel(entry.action), 14f, indented = true))
} }
@@ -384,9 +674,9 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
?: 0 ?: 0
val timestampMinimumWidth = maxOf( val timestampMinimumWidth = maxOf(
dp(MINIMUM_METADATA_COLUMN_DP), dp(MINIMUM_METADATA_COLUMN_DP),
widestTimestamp + dp(CELL_HORIZONTAL_PADDING_DP * 2), widestTimestamp + dp(METADATA_END_PADDING_DP),
) )
val preferredWidth = maxOf(timestampMinimumWidth, widestMetadata + dp(CELL_HORIZONTAL_PADDING_DP * 2)) val preferredWidth = maxOf(timestampMinimumWidth, widestMetadata + dp(METADATA_END_PADDING_DP))
if (availableWidth <= 0) return preferredWidth if (availableWidth <= 0) return preferredWidth
val maximumWidth = maxOf( val maximumWidth = maxOf(
timestampMinimumWidth, timestampMinimumWidth,
@@ -410,24 +700,51 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
} }
private inner class LogAdapter : RecyclerView.Adapter<LogAdapter.Holder>() { private inner class LogAdapter : RecyclerView.Adapter<LogAdapter.Holder>() {
private var rows: List<NotificationLogEntry> = emptyList() private var snapshot: NotificationLogIndex? = null
private var settings = LogViewSettings() private var settings = LogViewSettings()
private var metadataWidth = 0 private var metadataWidth = 0
private var availableWidth = 0
private val heights = IntArray(16)
private var contentLineHeight = 1
private var contentFontPadding = 0
fun submit(rows: List<NotificationLogEntry>, settings: LogViewSettings, metadataWidth: Int) { fun collapsedHeight(fields: Int) = heights[fields and 15]
val previous = this.rows fun contentLines(height: Int) =
this.rows = rows ((height - dp(CELL_VERTICAL_PADDING_DP * 2) - contentFontPadding) / contentLineHeight).coerceAtLeast(1)
fun submit(settings: LogViewSettings, metadataWidth: Int) {
val width = binding?.logList?.let { it.width - it.paddingLeft - it.paddingRight } ?: 0
val geometryChanged = width != availableWidth || metadataWidth != this.metadataWidth || settings != this.settings
if (width > 0 && geometryChanged) {
val body = TextView(requireContext()).apply { text = "Text" }
body.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
contentLineHeight = body.lineHeight
contentFontPadding = body.measuredHeight - body.lineHeight
val sample = NotificationLogEntry(id = "", recordedAtEpochMillis = System.currentTimeMillis(),
packageName = "example.app", appName = "App", action = NotificationAction.APPEARED, contents = null)
heights.indices.forEach { mask ->
val maskedSettings = settings.copy(visibleFields = settings.visibleFields.filterTo(mutableSetOf()) {
(mask and (1 shl it.ordinal)) != 0
})
val cell = metadataCell(metadataValues(sample, maskedSettings), false, {}, "")
cell.measure(View.MeasureSpec.makeMeasureSpec(metadataWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
heights[mask] = maxOf(cell.measuredHeight, body.measuredHeight + dp(CELL_VERTICAL_PADDING_DP * 2))
}
// Re-check remembered rows too, even when they are outside the viewport.
cache.values().filter { it.id in expandedIds }.forEach { entry ->
val probe = LogRow(entry, settings, metadataWidth, expanded = false)
probe.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
probe.layout(0, 0, width, probe.measuredHeight)
}
}
val changed = snapshot !== index || geometryChanged
snapshot = index
this.settings = settings this.settings = settings
this.metadataWidth = metadataWidth this.metadataWidth = metadataWidth
DiffUtil.calculateDiff(object : DiffUtil.Callback() { this.availableWidth = width
override fun getOldListSize() = previous.size if (changed) notifyDataSetChanged()
override fun getNewListSize() = rows.size
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
previous[oldItemPosition].id == rows[newItemPosition].id
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
previous[oldItemPosition] == rows[newItemPosition]
}).dispatchUpdatesTo(this)
notifyItemRangeChanged(0, itemCount)
} }
override fun onCreateViewHolder(parent: android.view.ViewGroup, viewType: Int): Holder { override fun onCreateViewHolder(parent: android.view.ViewGroup, viewType: Int): Holder {
@@ -440,11 +757,23 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
override fun onBindViewHolder(holder: Holder, position: Int) { override fun onBindViewHolder(holder: Holder, position: Int) {
holder.content.removeAllViews() holder.content.removeAllViews()
holder.content.addView(logRowView(rows[position], settings, metadataWidth)) val entry = cache[position]
if (entry == null) {
holder.content.addView(View(holder.content.context).apply {
layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
collapsedHeight(snapshot!!.fieldsAt(position)))
importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO
})
holder.content.contentDescription = "Loading log"
} else {
holder.content.contentDescription = null
val row = LogRow(entry, settings, metadataWidth)
holder.content.addView(row)
}
holder.separator.visibility = if (position == itemCount - 1) View.GONE else View.VISIBLE holder.separator.visibility = if (position == itemCount - 1) View.GONE else View.VISIBLE
} }
override fun getItemCount(): Int = rows.size override fun getItemCount(): Int = snapshot?.count ?: 0
inner class Holder(val root: LinearLayout, val content: LinearLayout) : RecyclerView.ViewHolder(root) { inner class Holder(val root: LinearLayout, val content: LinearLayout) : RecyclerView.ViewHolder(root) {
val separator: View get() = root.getChildAt(1) val separator: View get() = root.getChildAt(1)
@@ -464,7 +793,7 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
EncryptedNotificationLogStore(requireContext().applicationContext).delete(entryId) EncryptedNotificationLogStore(requireContext().applicationContext).delete(entryId)
} }
loadLogs(reset = true) loadLogs()
} }
} }
.show() .show()
@@ -508,19 +837,19 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
} }
} }
private fun highlightCollapsedRow(row: View) { private fun highlightLogRow(row: View): ValueAnimator? {
if (!row.isAttachedToWindow) return if (!row.isAttachedToWindow) return null
val baseColor = ContextCompat.getColor(row.context, R.color.primary) val baseColor = ContextCompat.getColor(row.context, R.color.primary)
val startColor = Color.argb( val startColor = Color.argb(
COLLAPSE_HIGHLIGHT_ALPHA, ROW_HIGHLIGHT_ALPHA,
Color.red(baseColor), Color.red(baseColor),
Color.green(baseColor), Color.green(baseColor),
Color.blue(baseColor), Color.blue(baseColor),
) )
val endColor = Color.argb(0, Color.red(baseColor), Color.green(baseColor), Color.blue(baseColor)) val endColor = Color.argb(0, Color.red(baseColor), Color.green(baseColor), Color.blue(baseColor))
row.setBackgroundColor(startColor) row.setBackgroundColor(startColor)
ValueAnimator.ofObject(ArgbEvaluator(), startColor, endColor).apply { return ValueAnimator.ofObject(ArgbEvaluator(), startColor, endColor).apply {
duration = COLLAPSE_HIGHLIGHT_DURATION_MILLIS duration = ROW_HIGHLIGHT_DURATION_MILLIS
addUpdateListener { row.setBackgroundColor(it.animatedValue as Int) } addUpdateListener { row.setBackgroundColor(it.animatedValue as Int) }
addListener(object : AnimatorListenerAdapter() { addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) { override fun onAnimationEnd(animation: Animator) {
@@ -559,15 +888,16 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
private companion object { private companion object {
const val SCROLL_TO_TOP_THRESHOLD_DP = 120 const val SCROLL_TO_TOP_THRESHOLD_DP = 120
const val PAGE_SIZE = 80 const val PAGE_SIZE = 80
const val PREFETCH_ROWS = 160
const val IMAGE_VIEWER_TAG = "image-viewer" const val IMAGE_VIEWER_TAG = "image-viewer"
const val MINIMUM_METADATA_COLUMN_DP = 120 const val MINIMUM_METADATA_COLUMN_DP = 120
const val CELL_HORIZONTAL_PADDING_DP = 8 const val METADATA_END_PADDING_DP = 8
const val CELL_VERTICAL_PADDING_DP = 6 const val CELL_VERTICAL_PADDING_DP = 6
const val COLUMN_GAP_DP = 6 const val COLUMN_GAP_DP = 6
const val ROW_SEPARATOR_MARGIN_DP = 4 const val ROW_SEPARATOR_MARGIN_DP = 4
const val METADATA_INDENT_DP = 8 const val METADATA_INDENT_DP = 8
const val MAXIMUM_METADATA_COLUMN_FRACTION = 0.45f const val MAXIMUM_METADATA_COLUMN_FRACTION = 0.45f
const val COLLAPSE_HIGHLIGHT_ALPHA = 52 const val ROW_HIGHLIGHT_ALPHA = 52
const val COLLAPSE_HIGHLIGHT_DURATION_MILLIS = 450L const val ROW_HIGHLIGHT_DURATION_MILLIS = 450L
} }
} }
@@ -9,9 +9,9 @@ import java.io.DataOutputStream
import java.io.File import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
internal class EncryptedAtomicFile(context: Context, name: String) { internal class EncryptedAtomicFile(context: Context, name: String,
private val cipher: AesGcmCipher = AesGcmCipher(LogEncryptionKeyProvider().getOrCreate())) {
private val file = AtomicFile(File(context.filesDir, name)) private val file = AtomicFile(File(context.filesDir, name))
private val cipher = AesGcmCipher(LogEncryptionKeyProvider().getOrCreate())
val exists get() = file.baseFile.isFile val exists get() = file.baseFile.isFile
@@ -9,7 +9,7 @@ import java.io.FileNotFoundException
/** Stores copied notification image bytes independently of the source app's URI lifetime. */ /** Stores copied notification image bytes independently of the source app's URI lifetime. */
class EncryptedImageStore(context: Context) { class EncryptedImageStore(context: Context) {
private val directory = File(context.filesDir, "notification-images").also(File::mkdirs) private val directory = File(context.filesDir, "notification-images").also(File::mkdirs)
private val cipher = AesGcmCipher(LogEncryptionKeyProvider().getOrCreate()) private val cipher by lazy { AesGcmCipher(LogEncryptionKeyProvider().getOrCreate()) }
fun save(id: String, bytes: ByteArray) { fun save(id: String, bytes: ByteArray) {
require(id.matches(IMAGE_ID_PATTERN)) { "Invalid notification image ID." } require(id.matches(IMAGE_ID_PATTERN)) { "Invalid notification image ID." }
@@ -2,6 +2,10 @@ package se.ajpanton.notificationsmaster.data
import android.content.Context import android.content.Context
import android.util.AtomicFile import android.util.AtomicFile
import android.util.Base64
import org.json.JSONArray
import org.json.JSONObject
import se.ajpanton.notificationsmaster.model.NotificationAction
import se.ajpanton.notificationsmaster.model.NotificationLogEntry import se.ajpanton.notificationsmaster.model.NotificationLogEntry
import se.ajpanton.notificationsmaster.settings.StorageLimitsStore import se.ajpanton.notificationsmaster.settings.StorageLimitsStore
import java.io.BufferedInputStream import java.io.BufferedInputStream
@@ -11,16 +15,6 @@ import java.io.DataOutputStream
import java.io.File import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
data class NewestLogCursor(
val chunkName: String,
val entryIndex: Int,
)
data class NotificationLogPage(
val entries: List<NotificationLogEntry>,
val nextCursor: NewestLogCursor?,
)
/** /**
* Independently encrypted log chunks. Reading or appending a record needs only * Independently encrypted log chunks. Reading or appending a record needs only
* one bounded chunk; older chunks stay on disk until requested by the viewer. * one bounded chunk; older chunks stay on disk until requested by the viewer.
@@ -31,33 +25,107 @@ class EncryptedNotificationLogStore(context: Context) {
private val imageDirectory = File(appContext.filesDir, IMAGE_DIRECTORY_NAME) private val imageDirectory = File(appContext.filesDir, IMAGE_DIRECTORY_NAME)
private val cipher = AesGcmCipher(LogEncryptionKeyProvider().getOrCreate()) private val cipher = AesGcmCipher(LogEncryptionKeyProvider().getOrCreate())
fun readNewest(cursor: NewestLogCursor?, limit: Int): NotificationLogPage = synchronized(lock) { /** Reads small summaries; an existing unindexed chunk is indexed once on demand. */
require(limit > 0) fun openIndex(query: LogQuery = LogQuery()): NotificationLogIndex {
val result = ArrayList<NotificationLogEntry>(limit) val files = synchronized(lock) {
val chunks = chunkFiles().asReversed() val files = chunkFiles().asReversed()
var chunkIndex = cursor?.let { saved -> chunks.indexOfFirst { it.name == saved.chunkName } } ?: 0 directory.listFiles()?.filter { INDEX_SUFFIX in it.name }?.forEach {
var entryIndex = cursor?.entryIndex ?: Int.MAX_VALUE val chunkName = it.name.substringBefore(INDEX_SUFFIX)
if (chunkIndex < 0) return@synchronized NotificationLogPage(emptyList(), null) if (!File(directory, chunkName).isFile) {
AtomicFile(File(directory, "$chunkName$INDEX_SUFFIX")).delete()
while (chunkIndex < chunks.size) {
val chunk = chunks[chunkIndex]
val entries = readChunk(chunk)
var index = minOf(entryIndex, entries.lastIndex)
while (index >= 0 && result.size < limit) {
result += entries[index--]
}
if (result.size == limit) {
val next = if (index >= 0) {
NewestLogCursor(chunk.name, index)
} else {
chunks.getOrNull(chunkIndex + 1)?.let { NewestLogCursor(it.name, Int.MAX_VALUE) }
} }
return@synchronized NotificationLogPage(result, next)
} }
chunkIndex++ files
entryIndex = Int.MAX_VALUE
} }
NotificationLogPage(result, null) val chunks = mutableListOf<NotificationLogIndex.Chunk>()
val apps = linkedMapOf<String, String>()
val actions = mutableSetOf<NotificationAction>()
var start = 0
var rebuilt = false
files.forEach { file ->
if (Thread.currentThread().isInterrupted) throw InterruptedException("Log indexing cancelled")
val (summary, updated) = synchronized(lock) {
if (file.isFile) readIndex(file) else null
} ?: return@forEach
rebuilt = rebuilt || updated
val groups = summary.getJSONArray("groups")
val fields = Base64.decode(summary.getString("fields"), Base64.NO_WRAP)
require(fields.size == summary.getInt("records")) { "Invalid notification-log index." }
val matchingFields = ByteArray(fields.size)
var count = 0
repeat(groups.length()) { position ->
val group = groups.getJSONArray(position)
val packageName = group.getString(0)
val action = NotificationAction.valueOf(group.getString(2))
if (query.matches(packageName, action)) {
if (query.search == null) {
apps.putIfAbsent(packageName, group.getString(1))
actions += action
}
count += group.getInt(3)
val positions = group.getJSONArray(4)
repeat(positions.length()) { offset ->
val record = positions.getInt(offset)
matchingFields[record] = fields[record]
}
}
}
var matchingPositions: IntArray? = null
if (count > 0 && query.search != null) {
val entries = synchronized(lock) { if (file.isFile) readChunk(file) else emptyList() }
if (entries.size < fields.size || entries.firstOrNull()?.id != summary.getString("first") ||
entries.getOrNull(fields.size - 1)?.id != summary.getString("last")) return openIndex(query)
// App/action selection above avoids decrypting unrelated chunks. Match
// outside the capture lock, and retain positions rather than message text.
matchingPositions = fields.indices.reversed().filter { position ->
matchingFields[position] != 0.toByte() && query.search.matches(listOf(
entries[position].contents.orEmpty(), entries[position].previousContents.orEmpty()))
}.toIntArray()
count = matchingPositions.size
matchingPositions.forEach { position ->
val entry = entries[position]
apps.putIfAbsent(entry.packageName, entry.appName)
actions += entry.action
}
}
if (count > 0) {
val end = Math.addExact(start, count)
chunks += NotificationLogIndex.Chunk(file.name, summary.getInt("records"),
summary.getString("first"), summary.getString("last"), start, end,
matchingPositions?.map { fields[it] }?.toByteArray()
?: matchingFields.reversedArray().filter { it != 0.toByte() }.toByteArray(), matchingPositions)
start = end
}
}
if (rebuilt) {
// Rebuilding adds storage overhead. Apply retention before returning authoritative counts.
synchronized(lock) { enforceLimits() }
return openIndex(query)
}
return NotificationLogIndex(chunks, query, apps, actions)
}
/** Directly decrypts the chunks containing a range, not all earlier pages. Null means invalidated. */
fun readRange(index: NotificationLogIndex, start: Int, limit: Int): List<NotificationLogEntry>? {
require(start >= 0 && limit > 0)
val result = ArrayList<NotificationLogEntry>(minOf(limit, (index.count - start).coerceAtLeast(0)))
var position = start
var chunkPosition = index.chunkAt(position)
while (chunkPosition < index.chunks.size && result.size < limit) {
val chunk = index.chunks[chunkPosition++]
val entries = synchronized(lock) {
val file = File(directory, chunk.name)
if (file.isFile) readChunk(file) else emptyList()
}
// Appends may extend a chunk; the snapshot's original prefix remains usable.
if (entries.size < chunk.records || entries.firstOrNull()?.id != chunk.firstId ||
entries.getOrNull(chunk.records - 1)?.id != chunk.lastId) return null
val matching = chunk.positions?.map { entries[it] }
?: entries.take(chunk.records).asReversed().filter(index.query::matches)
result += matching.drop(position - chunk.start).take(limit - result.size)
position = chunk.end
}
return result
} }
/** /**
@@ -82,9 +150,9 @@ class EncryptedNotificationLogStore(context: Context) {
val current = newest?.let(::readChunk).orEmpty() val current = newest?.let(::readChunk).orEmpty()
val combined = NotificationLogEntryJson.encode(current + entry) val combined = NotificationLogEntryJson.encode(current + entry)
if (newest == null || combined.size <= MAX_CHUNK_PLAINTEXT_BYTES) { if (newest == null || combined.size <= MAX_CHUNK_PLAINTEXT_BYTES) {
writeChunk(newest ?: nextChunkFile(chunks), combined) writeChunk(newest ?: nextChunkFile(chunks), current + entry, combined)
} else { } else {
writeChunk(nextChunkFile(chunks), NotificationLogEntryJson.encode(listOf(entry))) writeChunk(nextChunkFile(chunks), listOf(entry))
} }
enforceLimits() enforceLimits()
} }
@@ -99,7 +167,7 @@ class EncryptedNotificationLogStore(context: Context) {
val entries = readChunk(chunk) val entries = readChunk(chunk)
val entry = entries.firstOrNull { it.id == id } ?: return@forEach val entry = entries.firstOrNull { it.id == id } ?: return@forEach
val retained = entries.filterNot { it.id == id } val retained = entries.filterNot { it.id == id }
if (retained.isEmpty()) chunk.delete() else writeChunk(chunk, NotificationLogEntryJson.encode(retained)) if (retained.isEmpty()) deleteChunk(chunk) else writeChunk(chunk, retained)
entry.imageId?.let(::deleteImage) entry.imageId?.let(::deleteImage)
return true return true
} }
@@ -124,27 +192,27 @@ class EncryptedNotificationLogStore(context: Context) {
private fun trimLogBytes(limit: Long) { private fun trimLogBytes(limit: Long) {
var chunks = chunkFiles() var chunks = chunkFiles()
var totalBytes = chunks.sumOf(File::length) var totalBytes = logBytes()
while (totalBytes > limit && chunks.isNotEmpty()) { while (totalBytes > limit && chunks.isNotEmpty()) {
val oldest = chunks.first() val oldest = chunks.first()
val entries = readChunk(oldest) val entries = readChunk(oldest)
if (entries.isEmpty()) { if (entries.isEmpty()) {
oldest.delete() deleteChunk(oldest)
} else if (chunks.size > 1) { } else if (chunks.size > 1) {
entries.mapNotNull { it.imageId }.forEach(::deleteImage) entries.mapNotNull { it.imageId }.forEach(::deleteImage)
oldest.delete() deleteChunk(oldest)
} else { } else {
var retained = entries var retained = entries
while (totalBytes > limit && retained.isNotEmpty()) { while (totalBytes > limit && retained.isNotEmpty()) {
retained.first().imageId?.let(::deleteImage) retained.first().imageId?.let(::deleteImage)
retained = retained.drop(1) retained = retained.drop(1)
if (retained.isEmpty()) oldest.delete() if (retained.isEmpty()) deleteChunk(oldest)
else writeChunk(oldest, NotificationLogEntryJson.encode(retained)) else writeChunk(oldest, retained)
totalBytes = chunkFiles().sumOf(File::length) totalBytes = logBytes()
} }
} }
chunks = chunkFiles() chunks = chunkFiles()
totalBytes = chunks.sumOf(File::length) totalBytes = logBytes()
} }
} }
@@ -205,7 +273,8 @@ class EncryptedNotificationLogStore(context: Context) {
} }
} }
private fun writeChunk(file: File, encoded: ByteArray) { private fun writeChunk(file: File, entries: List<NotificationLogEntry>,
encoded: ByteArray = NotificationLogEntryJson.encode(entries)) {
val payload = cipher.encrypt(encoded) val payload = cipher.encrypt(encoded)
val atomicFile = AtomicFile(file) val atomicFile = AtomicFile(file)
val output = atomicFile.startWrite() val output = atomicFile.startWrite()
@@ -222,8 +291,43 @@ class EncryptedNotificationLogStore(context: Context) {
atomicFile.failWrite(output) atomicFile.failWrite(output)
throw error throw error
} }
writeIndex(file, entries, payload.initializationVector)
} }
private fun indexFile(file: File) = EncryptedAtomicFile(appContext, "$DIRECTORY_NAME/${file.name}$INDEX_SUFFIX", cipher)
private fun readIndex(file: File): Pair<JSONObject, Boolean> {
val iv = DataInputStream(BufferedInputStream(AtomicFile(file).openRead())).use { input ->
require(input.readInt() == FILE_VERSION) { "Unsupported encrypted notification-log chunk." }
readByteArray(input, MAX_IV_BYTES)
}
val revision = Base64.encodeToString(iv, Base64.NO_WRAP)
val saved = indexFile(file).read()?.let { JSONObject(it.decodeToString()) }
if (saved?.getString("revision") == revision) return saved to false
// The IV changes on every write: a missing/interrupted index update cannot look current.
return writeIndex(file, readChunk(file), iv) to true
}
private fun writeIndex(file: File, entries: List<NotificationLogEntry>, iv: ByteArray): JSONObject {
val groups = JSONArray()
entries.indices.groupBy { entries[it].packageName to entries[it].action }.forEach { (key, positions) ->
groups.put(JSONArray().put(key.first).put(entries[positions.last()].appName)
.put(key.second.name).put(positions.size).put(JSONArray(positions)))
}
return JSONObject().put("revision", Base64.encodeToString(iv, Base64.NO_WRAP))
.put("records", entries.size).put("first", entries.first().id).put("last", entries.last().id)
.put("groups", groups)
.put("fields", Base64.encodeToString(ByteArray(entries.size) { entries[it].fieldsMask().toByte() }, Base64.NO_WRAP))
.also { indexFile(file).write(it.toString().encodeToByteArray()) }
}
private fun deleteChunk(file: File) {
AtomicFile(file).delete()
AtomicFile(File(directory, "${file.name}$INDEX_SUFFIX")).delete()
}
private fun logBytes() = directory.listFiles()?.sumOf(File::length) ?: 0L
private fun readByteArray(stream: DataInputStream, maximumLength: Int): ByteArray { private fun readByteArray(stream: DataInputStream, maximumLength: Int): ByteArray {
val length = stream.readInt() val length = stream.readInt()
require(length in 1..maximumLength) { "Invalid encrypted notification-log field length." } require(length in 1..maximumLength) { "Invalid encrypted notification-log field length." }
@@ -231,7 +335,7 @@ class EncryptedNotificationLogStore(context: Context) {
} }
private fun clearChunksOnly() { private fun clearChunksOnly() {
chunkFiles().forEach(File::delete) directory.listFiles()?.forEach { AtomicFile(it).delete() }
} }
private fun deleteImage(id: String) { private fun deleteImage(id: String) {
@@ -244,6 +348,7 @@ class EncryptedNotificationLogStore(context: Context) {
const val IMAGE_DIRECTORY_NAME = "notification-images" const val IMAGE_DIRECTORY_NAME = "notification-images"
const val CHUNK_PREFIX = "chunk-" const val CHUNK_PREFIX = "chunk-"
const val CHUNK_SUFFIX = ".bin" const val CHUNK_SUFFIX = ".bin"
const val INDEX_SUFFIX = ".index"
const val CHUNK_NAME_WIDTH = 12 const val CHUNK_NAME_WIDTH = 12
const val FILE_VERSION = 1 const val FILE_VERSION = 1
const val MAX_IV_BYTES = 32 const val MAX_IV_BYTES = 32
@@ -0,0 +1,46 @@
package se.ajpanton.notificationsmaster.data
import se.ajpanton.notificationsmaster.model.NotificationLogEntry
/** Decrypted content is bounded independently of the user's on-disk history limit. */
internal class LogWindowCache(
private val maxEntries: Int = 480,
private val maxBytes: Long = 4 * 1024 * 1024,
) {
private val entries = sortedMapOf<Int, NotificationLogEntry>()
val size get() = entries.size
val bytes get() = entries.values.sumOf(::entryBytes)
operator fun get(position: Int) = entries[position]
fun values(): Collection<NotificationLogEntry> = entries.values
fun clear() = entries.clear()
fun put(start: Int, rows: List<NotificationLogEntry>) {
rows.forEachIndexed { offset, entry -> entries[start + offset] = entry }
}
/** Visible rows are never evicted; the byte limit can be exceeded by that working set alone. */
fun retain(first: Int, last: Int, margin: Int): Map<Int, String> {
val removed = mutableMapOf<Int, String>()
entries.keys.filter { it < first - margin || it > last + margin }.forEach {
removed[it] = entries.remove(it)!!.id
}
var retainedBytes = bytes
if (size <= maxEntries && retainedBytes <= maxBytes) return removed
entries.keys.filter { it !in first..last }
.sortedByDescending { if (it < first) first - it else it - last }
.forEach { position ->
if (size > maxEntries || retainedBytes > maxBytes) {
val entry = entries.remove(position)!!
retainedBytes -= entryBytes(entry)
removed[position] = entry.id
}
}
return removed
}
private fun entryBytes(entry: NotificationLogEntry): Long = 256L + 2L * (
entry.id.length + entry.packageName.length + entry.appName.length +
(entry.contents?.length ?: 0) + (entry.previousContents?.length ?: 0) +
(entry.eventTimeZoneId?.length ?: 0) + (entry.imageId?.length ?: 0)
)
}
@@ -0,0 +1,65 @@
package se.ajpanton.notificationsmaster.data
import se.ajpanton.notificationsmaster.model.NotificationAction
import se.ajpanton.notificationsmaster.model.NotificationLogEntry
import se.ajpanton.notificationsmaster.settings.LogField
import se.ajpanton.notificationsmaster.search.LogContentSearch
internal fun NotificationLogEntry.fieldsMask(): Int =
(1 shl LogField.TIMESTAMP.ordinal) or (1 shl LogField.ACTION.ordinal) or
(if (appName.isNotBlank()) 1 shl LogField.APP_NAME.ordinal else 0) or
(if (packageName.isNotBlank()) 1 shl LogField.PACKAGE_NAME.ordinal else 0) or
(if (!contents.isNullOrEmpty()) 1 shl LogField.CONTENTS.ordinal else 0)
data class LogQuery(
val packages: Set<String> = emptySet(),
val actions: Set<NotificationAction> = NotificationAction.entries.toSet(),
val search: LogContentSearch? = null,
) {
fun matches(entry: NotificationLogEntry) = matches(entry.packageName, entry.action) &&
(search?.matches(listOf(entry.contents.orEmpty(), entry.previousContents.orEmpty())) != false)
fun matches(packageName: String, action: NotificationAction) =
action in actions && (packages.isEmpty() || packageName in packages)
}
/** A filtered snapshot of chunk boundaries, app names and one-byte field flags; no contents. */
class NotificationLogIndex internal constructor(
internal val chunks: List<Chunk>,
val query: LogQuery,
val apps: Map<String, String>,
val actions: Set<NotificationAction>,
) {
val count: Int = chunks.lastOrNull()?.end ?: 0
fun fieldsAt(position: Int): Int {
require(position in 0 until count)
val chunk = chunks[chunkAt(position)]
return chunk.fields[position - chunk.start].toInt()
}
internal fun chunkAt(position: Int): Int {
var low = 0
var high = chunks.lastIndex
while (low <= high) {
val middle = (low + high).ushr(1)
val chunk = chunks[middle]
when {
position < chunk.start -> high = middle - 1
position >= chunk.end -> low = middle + 1
else -> return middle
}
}
return chunks.size
}
internal data class Chunk(
val name: String,
val records: Int,
val firstId: String,
val lastId: String,
val start: Int,
val end: Int,
val fields: ByteArray,
val positions: IntArray? = null,
)
}
@@ -0,0 +1,177 @@
package se.ajpanton.notificationsmaster.search
import com.google.re2j.Pattern
enum class SearchMode(val label: String) {
REGEX("Regex"), LITERAL("Literal text"), AND("All terms (AND)"), OR("Any term (OR)");
val usesTerms get() = this == AND || this == OR
}
data class LogSearchSettings(
val text: String = "",
val mode: SearchMode = SearchMode.LITERAL,
val caseSensitive: Boolean = false,
val wholeWords: Boolean = false,
) {
val active get() = if (mode.usesTerms) text.isNotBlank() else text.isNotEmpty()
}
data class SearchSpan(val field: Int, val start: Int, val end: Int)
class SearchTooComplexException : Exception("Search is too complex. Try fewer terms or whole words.")
/** Offsets use UTF-16, like Android text spans. No text crosses a field boundary. */
class LogContentSearch(val settings: LogSearchSettings) {
private val pattern = if (settings.mode == SearchMode.REGEX && settings.active)
Pattern.compile(settings.text, if (settings.caseSensitive) 0 else Pattern.CASE_INSENSITIVE) else null
private val terms = if (settings.mode.usesTerms) settings.text.trim().split(Regex("\\s+")) else emptyList()
fun matches(fields: List<String>): Boolean {
checkInterrupted()
if (!settings.active) return true
return when (settings.mode) {
SearchMode.REGEX -> fields.any { it.isNotEmpty() && pattern!!.matcher(it).find() }
SearchMode.LITERAL -> fields.any { it.contains(settings.text, !settings.caseSensitive) }
SearchMode.OR -> terms.any { term -> fields.any { occurrences(it, term).hasNext() } }
SearchMode.AND -> termSpans(fields).size == terms.size
}
}
fun spans(fields: List<String>): List<SearchSpan> {
checkInterrupted()
if (!settings.active) return emptyList()
if (settings.mode.usesTerms) return termSpans(fields)
return buildList {
fields.forEachIndexed { field, text ->
if (pattern != null) {
val matcher = pattern.matcher(text)
while (text.isNotEmpty() && matcher.find()) {
checkInterrupted()
if (matcher.end() > matcher.start()) add(SearchSpan(field, matcher.start(), matcher.end()))
}
} else {
var cursor = 0
while (cursor <= text.length - settings.text.length) {
checkInterrupted()
val start = text.indexOf(settings.text, cursor, !settings.caseSensitive)
if (start < 0) break
cursor = start + settings.text.length
add(SearchSpan(field, start, cursor))
}
}
}
}
}
private fun occurrences(text: String, term: String): Iterator<Int> = sequence {
var cursor = 0
while (cursor <= text.length - term.length) {
checkInterrupted()
val start = text.indexOf(term, cursor, !settings.caseSensitive)
if (start < 0) break
val end = start + term.length
if (!settings.wholeWords ||
((start == 0 || !wordCharacter(text.codePointBefore(start))) &&
(end == text.length || !wordCharacter(text.codePointAt(end))))) yield(start)
cursor = start + 1 // Include overlapping candidates; the solver chooses compatible ones.
}
}.iterator()
private fun termSpans(fields: List<String>): List<SearchSpan> {
val groups = terms.groupingBy { it }.eachCount().entries.toList()
if (groups.size > 128 || terms.size > 4096) throw SearchTooComplexException()
if (groups.size == 1) {
val term = groups.single().key
val quota = groups.single().value
val selected = mutableListOf<SearchSpan>()
fields.forEachIndexed { field, text ->
var cursor = 0
val matches = occurrences(text, term)
while (selected.size < quota && matches.hasNext()) {
val start = matches.next()
if (start >= cursor) {
cursor = start + term.length
selected += SearchSpan(field, start, cursor)
}
}
}
return if (settings.mode == SearchMode.AND && selected.size < quota) emptyList() else selected
}
var candidateCount = 0
val candidates = groups.map { (term, _) -> buildList {
var offset = 0
fields.forEachIndexed { field, text ->
occurrences(text, term).forEach { start ->
if (++candidateCount > MAX_CANDIDATES) throw SearchTooComplexException()
add(Candidate(offset + start, offset + start + term.length, SearchSpan(field, start, start + term.length)))
}
offset = Math.addExact(offset, text.length + 1)
}
} }
val quotas = groups.map { it.value }
if (settings.mode == SearchMode.AND && candidates.indices.any { candidates[it].size < quotas[it] }) return emptyList()
val target = candidates.indices.sumOf { minOf(quotas[it], candidates[it].size) }
if (target == 0) return emptyList()
if (settings.wholeWords) {
val ordered = candidates.flatMapIndexed { group, matches -> matches.map { group to it } }.sortedBy { it.second.start }
// Ordinary whole-word candidates do not overlap. Punctuation-containing
// terms can still overlap, so those use the exact solver below.
if (ordered.zipWithNext().all { (left, right) -> left.second.end <= right.second.start }) {
val remaining = quotas.toMutableList()
val selected = ordered.mapNotNull { (group, match) ->
if (remaining[group] > 0) { remaining[group]--; match.span } else null
}
return if (settings.mode == SearchMode.AND && selected.size < terms.size) emptyList() else selected
}
}
val pending = ArrayDeque<Node>()
pending.add(Node(0, quotas, 0, null, null))
val earliest = mutableMapOf<List<Int>, Int>()
var best = pending.last()
var visited = 0
while (pending.isNotEmpty()) {
checkInterrupted()
if (++visited > MAX_STATES) throw SearchTooComplexException()
val node = pending.removeLast()
if (node.count > best.count) best = node
if (best.count == target) break
if (node.count + node.remaining.sum() <= best.count ||
earliest[node.remaining]?.let { it <= node.cursor } == true) continue
earliest[node.remaining] = node.cursor
// For a given term, its earliest compatible occurrence dominates later
// ones. Exploring term order still finds alternatives to greedy overlaps.
val next = candidates.indices.mapNotNull { group ->
if (node.remaining[group] == 0) return@mapNotNull null
val list = candidates[group]
var low = 0
var high = list.size
while (low < high) {
val middle = (low + high).ushr(1)
if (list[middle].start < node.cursor) low = middle + 1 else high = middle
}
list.getOrNull(low)?.let { group to it }
}.sortedByDescending { it.second.end }
next.forEach { (group, candidate) ->
if (pending.size >= MAX_STATES) throw SearchTooComplexException()
val remaining = node.remaining.toMutableList().apply { this[group]-- }
pending.add(Node(candidate.end, remaining, node.count + 1, node, candidate.span))
}
}
if (settings.mode == SearchMode.AND && best.count < terms.size) return emptyList()
return generateSequence(best) { it.parent }.mapNotNull { it.span }.toList().asReversed()
}
private data class Candidate(val start: Int, val end: Int, val span: SearchSpan)
private data class Node(val cursor: Int, val remaining: List<Int>, val count: Int, val parent: Node?, val span: SearchSpan?)
private companion object {
const val MAX_CANDIDATES = 100_000
const val MAX_STATES = 10_000
fun checkInterrupted() {
if (Thread.currentThread().isInterrupted) throw InterruptedException("Content search cancelled")
}
fun wordCharacter(codePoint: Int): Boolean = Character.isLetterOrDigit(codePoint) || codePoint == '_'.code ||
Character.getType(codePoint) == Character.NON_SPACING_MARK.toInt() ||
Character.getType(codePoint) == Character.COMBINING_SPACING_MARK.toInt()
}
}
@@ -0,0 +1,37 @@
package se.ajpanton.notificationsmaster.settings
import android.content.Context
import org.json.JSONObject
import se.ajpanton.notificationsmaster.data.EncryptedAtomicFile
import se.ajpanton.notificationsmaster.search.LogSearchSettings
import se.ajpanton.notificationsmaster.search.SearchMode
/** Queries can contain private message text, so persist them encrypted too. */
class LogSearchStore(context: Context) {
private val file = EncryptedAtomicFile(context.applicationContext, "log-search.bin")
fun load(): LogSearchSettings = synchronized(lock) {
if (!file.exists) cached = null
cached ?: file.read()?.let { bytes ->
val json = JSONObject(bytes.decodeToString())
LogSearchSettings(json.getString("text"), SearchMode.valueOf(json.getString("mode")),
json.getBoolean("caseSensitive"), json.getBoolean("wholeWords"))
}?.also { cached = it } ?: LogSearchSettings()
}
fun save(settings: LogSearchSettings) = synchronized(lock) {
val bytes = JSONObject().put("text", settings.text).put("mode", settings.mode.name)
.put("caseSensitive", settings.caseSensitive).put("wholeWords", settings.wholeWords)
.toString().encodeToByteArray()
require(bytes.size <= 64 * 1024) { "Search text is too long." }
file.write(bytes)
cached = settings
}
fun clear() = save(load().copy(text = ""))
private companion object {
val lock = Any()
var cached: LogSearchSettings? = null
}
}
@@ -0,0 +1,3 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24">
<path android:fillColor="#FFFFFFFF" android:pathData="M3,5h18l-7,8v5l-4,2v-7zM16.4,14l2.6,2.6 2.6,-2.6 1.4,1.4 -2.6,2.6 2.6,2.6 -1.4,1.4 -2.6,-2.6 -2.6,2.6 -1.4,-1.4 2.6,-2.6 -2.6,-2.6z" />
</vector>
@@ -0,0 +1,3 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24">
<path android:fillColor="#FFFFFFFF" android:pathData="M9.5,3a6.5,6.5 0,1 0,4.6 11.1L20,20l1.4,-1.4 -5.9,-5.9A6.5,6.5 0,0 0,9.5 3zM9.5,5a4.5,4.5 0,1 1,0 9a4.5,4.5 0,0 1,0 -9z" />
</vector>
@@ -0,0 +1,3 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24">
<path android:fillColor="#FFFFFFFF" android:pathData="M9.5,3a6.5,6.5 0,1 0,4.6 11.1L20,20l1.4,-1.4 -5.9,-5.9A6.5,6.5 0,0 0,9.5 3zM9.5,5a4.5,4.5 0,1 1,0 9a4.5,4.5 0,0 1,0 -9zM19,2a3,3 0,1 0,0 6a3,3 0,0 0,0 -6z" />
</vector>
@@ -26,6 +26,33 @@
android:layout_margin="24dp" android:layout_margin="24dp"
android:text="No logs yet." /> android:text="No logs yet." />
<LinearLayout
android:id="@+id/loading_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="16dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="8dp"
android:background="?attr/colorSurface"
android:elevation="4dp"
android:visibility="gone">
<ProgressBar
android:id="@+id/loading_spinner"
style="?android:attr/progressBarStyleSmall"
android:layout_width="24dp"
android:layout_height="24dp"
android:visibility="gone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Loading logs…" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton <com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/scroll_to_top" android:id="@+id/scroll_to_top"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@@ -0,0 +1,3 @@
<resources>
<color name="log_search_highlight">#69521C</color>
</resources>
+7
View File
@@ -0,0 +1,7 @@
<resources>
<item type="id" name="log_search_text" />
<item type="id" name="log_search_mode" />
<item type="id" name="log_search_words" />
<item type="id" name="log_search_case" />
<color name="log_search_highlight">#FFE39A</color>
</resources>
@@ -0,0 +1,44 @@
package se.ajpanton.notificationsmaster.data
import org.junit.Assert.*
import org.junit.Test
import se.ajpanton.notificationsmaster.model.NotificationAction
import se.ajpanton.notificationsmaster.model.NotificationLogEntry
class LogWindowCacheTest {
private fun row(position: Int, length: Int = 20) = NotificationLogEntry(
id = "$position", recordedAtEpochMillis = position.toLong(), packageName = "app",
appName = "App", action = NotificationAction.APPEARED, contents = "x".repeat(length),
)
@Test fun bothDirectionsKeepNearbyRowsAndEvictDistantOnes() {
val cache = LogWindowCache(maxEntries = 80)
cache.put(0, List(240) { row(it) })
val removed = cache.retain(100, 110, 80)
assertTrue(cache.size <= 80)
(100..110).forEach { assertNotNull(cache[it]) }
assertNull(cache[0])
assertNull(cache[239])
assertEquals("0", removed[0])
cache.put(0, List(40) { row(it) })
cache.retain(0, 10, 80)
assertNotNull(cache[0])
assertNull(cache[110])
}
@Test fun byteLimitAlsoBoundsLongMessagesButNeverDropsVisibleRows() {
val cache = LogWindowCache(maxEntries = 100, maxBytes = 16_000)
cache.put(0, List(100) { row(it, length = 2_000) })
cache.retain(40, 41, 100)
assertTrue(cache.bytes <= 16_000)
assertNotNull(cache[40])
assertNotNull(cache[41])
cache.put(40, listOf(row(40, length = 100_000)))
cache.retain(40, 41, 100)
assertEquals(2, cache.size)
assertTrue(cache.bytes > 16_000)
cache.clear()
assertEquals(0, cache.size)
assertEquals(0L, cache.bytes)
}
}
@@ -0,0 +1,101 @@
package se.ajpanton.notificationsmaster.search
import com.google.re2j.PatternSyntaxException
import org.junit.Assert.*
import org.junit.Test
import kotlin.random.Random
class LogContentSearchTest {
private val text = "there is some text and here is some more"
private fun search(query: String, mode: SearchMode = SearchMode.AND, words: Boolean = false, case: Boolean = false) =
LogContentSearch(LogSearchSettings(query, mode, case, words))
@Test fun duplicateTermsNeedDistinctOccurrences() {
assertEquals(2, search("some some").spans(listOf(text)).size)
assertTrue(search("some some").matches(listOf(text)))
assertFalse(search("some some some").matches(listOf(text)))
assertEquals(2, search("some some some", SearchMode.OR).spans(listOf(text)).size)
assertEquals(2, search("aa aa").spans(listOf("aaaa")).size)
assertFalse(search("aa aa").matches(listOf("aaa")))
}
@Test fun overlapsAreNotGreedyAndOrMaximizesTerms() {
assertFalse(search("mor ore").matches(listOf(text)))
assertEquals(listOf("there", "here"), search("here there").spans(listOf(text)).map { text.substring(it.start, it.end) })
assertEquals(1, search("mor ore", SearchMode.OR).spans(listOf(text)).size)
assertEquals(2, search("abcd ab cd", SearchMode.OR).spans(listOf("abcd")).size)
assertEquals(2, search("here there", SearchMode.OR).spans(listOf(text)).size)
}
@Test fun literalSpacesRegexCaseAndWholeWords() {
assertTrue(search("some text", SearchMode.LITERAL).matches(listOf(text)))
assertFalse(search("some text", SearchMode.LITERAL).matches(listOf(text)))
assertEquals(2, search("some", SearchMode.LITERAL).spans(listOf(text)).size)
assertTrue(search("SOME", SearchMode.LITERAL).matches(listOf(text)))
assertFalse(search("SOME", SearchMode.LITERAL, case = true).matches(listOf(text)))
assertEquals(2, search("s.me", SearchMode.REGEX).spans(listOf(text)).size)
assertTrue(search("^there.*more$", SearchMode.REGEX).matches(listOf(text)))
assertFalse(search("ore", words = true).matches(listOf(text)))
assertTrue(search("some some", words = true).matches(listOf(text)))
assertFalse(search("cat", words = true).matches(listOf("cats cat_name caté cat\u0301")))
assertTrue(search("cat", words = true).matches(listOf("(cat)!")))
assertFalse(search("é", words = true).matches(listOf("café")))
assertFalse(search("cat cat!", words = true).matches(listOf("cat!")))
assertEquals(1, search("cat cat!", SearchMode.OR, words = true).spans(listOf("cat!")).size)
}
@Test fun fieldsStaySeparateAndZeroWidthRegexDoesNotMatchMissingContents() {
val fields = listOf("first message", "previous message")
assertEquals(setOf(0, 1), search("message message").spans(fields).map { it.field }.toSet())
assertFalse(search("message previous", SearchMode.LITERAL).matches(fields))
assertTrue(search("^", SearchMode.REGEX).matches(fields))
assertTrue(search("^", SearchMode.REGEX).spans(fields).isEmpty())
assertFalse(search("^", SearchMode.REGEX).matches(listOf("", "")))
assertTrue(search(" ").matches(fields)) // Inactive term query.
}
@Test(expected = PatternSyntaxException::class) fun invalidRegexFailsClearly() { search("[", SearchMode.REGEX) }
@Test(expected = SearchTooComplexException::class) fun excessiveSearchFailsInsteadOfGuessingMatches() {
search((0..128).joinToString(" ") { "term$it" }).matches(listOf(text))
}
@Test fun longDuplicateQueriesAndCancellationDoNotOverflowTheStack() {
val query = List(2000) { "a" }.joinToString(" ")
assertEquals(2000, search(query).spans(listOf("a".repeat(4000))).size)
Thread.currentThread().interrupt()
try {
search("some some").matches(listOf(text))
fail("Cancelled searches must stop")
} catch (_: InterruptedException) {
// Do not leak the interrupt into the remaining test suite.
} finally {
Thread.interrupted()
}
}
@Test fun solverAgreesWithExhaustiveSmallCases() {
val random = Random(53)
repeat(300) {
val haystack = (0 until 8).map { if (random.nextBoolean()) 'a' else 'b' }.joinToString("")
val terms = List(4) { List(random.nextInt(1, 4)) { if (random.nextBoolean()) 'a' else 'b' }.joinToString("") }
fun exhaustive(term: Int, occupied: Set<Int>): Int {
if (term == terms.size) return 0
var best = exhaustive(term + 1, occupied)
for (start in 0..haystack.length - terms[term].length) {
val positions = (start until start + terms[term].length).toSet()
if (haystack.startsWith(terms[term], start) && positions.none { it in occupied }) {
best = maxOf(best, 1 + exhaustive(term + 1, occupied + positions))
}
}
return best
}
val maximum = exhaustive(0, emptySet())
val query = terms.joinToString(" ")
val spans = search(query, SearchMode.OR).spans(listOf(haystack))
assertEquals("$haystack / $query", maximum, spans.size)
assertEquals(spans.sumOf { it.end - it.start }, spans.flatMap { it.start until it.end }.toSet().size)
assertEquals(maximum == terms.size, search(query).matches(listOf(haystack)))
}
}
}