package se.ajpanton.notificationlog import android.app.Application import android.content.pm.PackageManager import se.ajpanton.notificationlog.data.EncryptedNotificationLogStore import se.ajpanton.notificationlog.settings.AppFilterSettingsStore import se.ajpanton.notificationlog.settings.LoggingRuleStore import se.ajpanton.notificationlog.settings.LoggingType import se.ajpanton.notificationlog.settings.NotificationListenerComponentController import se.ajpanton.notificationlog.settings.PerAppEventSettingsStore class NotificationLogApplication : Application() { override fun onCreate() { super.onCreate() synchronizeListenerComponent() Thread(::removeStaleAppStorage, "notification-log-storage-cleanup").start() } /** * Clearing app data resets rule preferences but deliberately does not reset * PackageManager's enabled/disabled state for the listener component. * Reconcile it before the UI starts so fresh default rules can bind again. */ private fun synchronizeListenerComponent() { val rules = LoggingRuleStore(this) NotificationListenerComponentController.update( this, LoggingType.entries.map(rules::ruleFor), PerAppEventSettingsStore(this).hasEnabledEventOverride(), ) } private fun removeStaleAppStorage() { val installedPackages = packageManager.getInstalledApplications( PackageManager.ApplicationInfoFlags.of(0), ).mapTo(mutableSetOf()) { it.packageName } AppFilterSettingsStore(this).removeUninstalledPackages(installedPackages) PerAppEventSettingsStore(this).removeUninstalledPackages(installedPackages) EncryptedNotificationLogStore(this).removeOrphanedImages() } }