Display encrypted notification logs
This commit is contained in:
@@ -95,8 +95,11 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
|
||||
supportFragmentManager.commit {
|
||||
replace(
|
||||
R.id.content_frame,
|
||||
page.loggingType?.let { EventSettingsFragment.newInstance(page.titleRes, it) }
|
||||
?: PageFragment.newInstance(page.titleRes),
|
||||
when {
|
||||
page == Page.VIEW_LOGS -> ViewLogsFragment()
|
||||
page.loggingType != null -> EventSettingsFragment.newInstance(page.titleRes, page.loggingType)
|
||||
else -> PageFragment.newInstance(page.titleRes)
|
||||
},
|
||||
)
|
||||
runOnCommit(::applyPageTitleVisibility)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user