Catch SecurityException in NotificationListener

This commit is contained in:
Jonas L 2019-01-18 07:37:50 +01:00
parent a8bff6149d
commit a833037136
3 changed files with 18 additions and 2 deletions

View file

@ -57,15 +57,29 @@ class NotificationListener: NotificationListenerService() {
runAsync {
if (shouldRemoveNotification(sbn) == NotificationHandling.Replace) {
val success = try {
cancelNotification(sbn.key)
true
} catch (ex: SecurityException) {
// this occurs when the notification access is revoked
// while this function is running
false
}
notificationManager.notify(
sbn.packageName,
NotificationIds.NOTIFICATION_BLOCKED,
NotificationCompat.Builder(this@NotificationListener, NotificationChannels.BLOCKED_NOTIFICATIONS_NOTIFICATION)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_stat_block)
.setContentTitle(getString(R.string.notification_filter_not_blocked_title))
.setContentTitle(
if (success)
getString(R.string.notification_filter_not_blocked_title)
else
getString(R.string.notification_filter_blocking_failed_title)
)
.setContentText(queryAppTitleCache.query(sbn.packageName))
.setLocalOnly(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)

View file

@ -16,4 +16,5 @@
-->
<resources>
<string name="notification_filter_not_blocked_title">TimeLimit hat eine Benachrichtigung blockiert</string>
<string name="notification_filter_blocking_failed_title">TimeLimit konnte eine Benachrichtigung nicht blockieren</string>
</resources>

View file

@ -16,4 +16,5 @@
-->
<resources>
<string name="notification_filter_not_blocked_title">TimeLimit has blocked a notification</string>
<string name="notification_filter_blocking_failed_title">TimeLimit could not block a notification</string>
</resources>