Display encrypted notification logs

This commit is contained in:
ajp_anton
2026-07-23 09:25:53 +00:00
parent 3eba02d25b
commit 4fc03a43a7
3 changed files with 74 additions and 2 deletions
@@ -0,0 +1,59 @@
package se.ajpanton.notificationlog
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import se.ajpanton.notificationlog.data.EncryptedNotificationLogStore
import se.ajpanton.notificationlog.databinding.FragmentViewLogsBinding
import se.ajpanton.notificationlog.model.NotificationLogEntry
import java.text.DateFormat
import java.util.Date
class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
private var binding: FragmentViewLogsBinding? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentViewLogsBinding.bind(view)
binding!!.clearLogs.setOnClickListener {
MaterialAlertDialogBuilder(requireContext())
.setTitle("Clear logs?")
.setMessage("This permanently removes every stored notification log.")
.setNegativeButton("Cancel", null)
.setPositiveButton("Clear") { _, _ ->
EncryptedNotificationLogStore(requireContext()).clear()
loadLogs()
}
.show()
}
loadLogs()
}
override fun onDestroyView() { binding = null; super.onDestroyView() }
private fun loadLogs() {
val context = requireContext().applicationContext
Thread {
val entries = EncryptedNotificationLogStore(context).readAll().sortedByDescending { it.recordedAtEpochMillis }
activity?.runOnUiThread { binding?.let { render(it, entries) } }
}.start()
}
private fun render(view: FragmentViewLogsBinding, entries: List<NotificationLogEntry>) {
view.logRows.removeAllViews()
view.emptyView.visibility = if (entries.isEmpty()) View.VISIBLE else View.GONE
entries.forEach { entry ->
view.logRows.addView(TextView(requireContext()).apply {
text = listOfNotNull(
DateFormat.getDateTimeInstance().format(Date(entry.recordedAtEpochMillis)),
entry.appName,
entry.action.name.lowercase().replace('_', ' '),
entry.contents,
).joinToString(" · ")
setPadding(0, 12, 0, 12)
})
}
}
}