Guide users to notification listener access

This commit is contained in:
ajp_anton
2026-07-23 09:34:28 +00:00
parent 05a000c5d6
commit 23aa3282dd
2 changed files with 24 additions and 1 deletions
@@ -1,6 +1,9 @@
package se.ajpanton.notificationlog
import android.os.Bundle
import android.content.ComponentName
import android.content.Intent
import android.provider.Settings
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
@@ -11,6 +14,7 @@ import se.ajpanton.notificationlog.model.NotificationLogEntry
import se.ajpanton.notificationlog.settings.LogField
import se.ajpanton.notificationlog.settings.LogViewSettingsStore
import se.ajpanton.notificationlog.settings.TimestampZone
import se.ajpanton.notificationlog.capture.NotificationCaptureService
import java.text.DateFormat
import java.util.Date
@@ -20,6 +24,9 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentViewLogsBinding.bind(view)
binding!!.notificationAccess.setOnClickListener {
startActivity(Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS))
}
binding!!.clearLogs.setOnClickListener {
MaterialAlertDialogBuilder(requireContext())
.setTitle("Clear logs?")
@@ -34,6 +41,15 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
loadLogs()
}
override fun onResume() {
super.onResume()
binding?.notificationAccess?.text = if (isListenerEnabled()) {
"Notification access enabled"
} else {
"Enable notification access"
}
}
override fun onDestroyView() { binding = null; super.onDestroyView() }
private fun loadLogs() {
@@ -67,4 +83,11 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
if (zone == TimestampZone.UTC) format.timeZone = java.util.TimeZone.getTimeZone("UTC")
return format.format(Date(value))
}
private fun isListenerEnabled(): Boolean {
val manager = requireContext().getSystemService(android.app.NotificationManager::class.java)
return manager.isNotificationListenerAccessGranted(
ComponentName(requireContext(), NotificationCaptureService::class.java),
)
}
}