Request listener rebind on startup
This commit is contained in:
@@ -2,6 +2,8 @@ package se.ajpanton.notificationlog
|
|||||||
|
|
||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.content.ComponentName
|
||||||
|
import android.app.NotificationManager
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
@@ -18,6 +20,7 @@ import se.ajpanton.notificationlog.settings.LogViewSettings
|
|||||||
import se.ajpanton.notificationlog.settings.LogViewSettingsStore
|
import se.ajpanton.notificationlog.settings.LogViewSettingsStore
|
||||||
import se.ajpanton.notificationlog.settings.TimestampZone
|
import se.ajpanton.notificationlog.settings.TimestampZone
|
||||||
import se.ajpanton.notificationlog.settings.DisplayEvent
|
import se.ajpanton.notificationlog.settings.DisplayEvent
|
||||||
|
import se.ajpanton.notificationlog.capture.NotificationCaptureService
|
||||||
import java.text.DateFormat
|
import java.text.DateFormat
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
@@ -91,6 +94,11 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
|
|||||||
private fun render(view: FragmentViewLogsBinding, addedRows: List<LogRow>, reset: Boolean) {
|
private fun render(view: FragmentViewLogsBinding, addedRows: List<LogRow>, reset: Boolean) {
|
||||||
if (reset) view.logRows.removeAllViews()
|
if (reset) view.logRows.removeAllViews()
|
||||||
view.emptyView.visibility = if (rows.isEmpty()) View.VISIBLE else View.GONE
|
view.emptyView.visibility = if (rows.isEmpty()) View.VISIBLE else View.GONE
|
||||||
|
view.emptyView.text = if (hasNotificationAccess()) {
|
||||||
|
"No logs yet."
|
||||||
|
} else {
|
||||||
|
"Notification access is disabled. Enable it in Settings."
|
||||||
|
}
|
||||||
val settings = LogViewSettingsStore(requireContext()).load()
|
val settings = LogViewSettingsStore(requireContext()).load()
|
||||||
addedRows.filter { eventFor(it.entry.action) in settings.visibleEvents }
|
addedRows.filter { eventFor(it.entry.action) in settings.visibleEvents }
|
||||||
.forEach { row -> view.logRows.addView(logRowView(row, settings)) }
|
.forEach { row -> view.logRows.addView(logRowView(row, settings)) }
|
||||||
@@ -264,6 +272,10 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
|
|||||||
else -> DisplayEvent.DISAPPEARING
|
else -> DisplayEvent.DISAPPEARING
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun hasNotificationAccess(): Boolean = requireContext()
|
||||||
|
.getSystemService(NotificationManager::class.java)
|
||||||
|
.isNotificationListenerAccessGranted(ComponentName(requireContext(), NotificationCaptureService::class.java))
|
||||||
|
|
||||||
private fun timestamp(value: Long, zone: TimestampZone, eventTimeZoneId: String?): String = DateFormat.getDateTimeInstance().apply {
|
private fun timestamp(value: Long, zone: TimestampZone, eventTimeZoneId: String?): String = DateFormat.getDateTimeInstance().apply {
|
||||||
timeZone = when (zone) {
|
timeZone = when (zone) {
|
||||||
TimestampZone.UTC -> java.util.TimeZone.getTimeZone("UTC")
|
TimestampZone.UTC -> java.util.TimeZone.getTimeZone("UTC")
|
||||||
|
|||||||
+5
-1
@@ -22,10 +22,14 @@ internal object NotificationListenerComponentController {
|
|||||||
} else {
|
} else {
|
||||||
PackageManager.COMPONENT_ENABLED_STATE_DISABLED
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED
|
||||||
}
|
}
|
||||||
if (packageManager.getComponentEnabledSetting(component) == desired) return
|
if (packageManager.getComponentEnabledSetting(component) != desired) {
|
||||||
packageManager.setComponentEnabledSetting(component, desired, PackageManager.DONT_KILL_APP)
|
packageManager.setComponentEnabledSetting(component, desired, PackageManager.DONT_KILL_APP)
|
||||||
|
}
|
||||||
if (desired == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
|
if (desired == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
|
||||||
try {
|
try {
|
||||||
|
// Data clearing and OEM listener management can leave an enabled
|
||||||
|
// component without a live binding. Ask Android to reconnect on
|
||||||
|
// every enabled-rule synchronization, not just a state change.
|
||||||
NotificationListenerService.requestRebind(component)
|
NotificationListenerService.requestRebind(component)
|
||||||
} catch (error: SecurityException) {
|
} catch (error: SecurityException) {
|
||||||
Log.w(TAG, "Notification access is not granted yet; Android will bind after the user grants it", error)
|
Log.w(TAG, "Notification access is not granted yet; Android will bind after the user grants it", error)
|
||||||
|
|||||||
Reference in New Issue
Block a user