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),
)
}
}
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="@dimen/page_padding">
<Button android:id="@+id/clear_logs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear logs" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"><Button android:id="@+id/notification_access" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enable notification access"/><Button android:id="@+id/clear_logs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear logs" /></LinearLayout>
<ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
<TextView android:id="@+id/empty_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="No logs yet." />