Add showing blocking reason at blocked notifications

This commit is contained in:
Jonas L 2019-01-22 13:24:17 +01:00
parent 03977e7f68
commit 9163fd4353
3 changed files with 31 additions and 12 deletions

View file

@ -56,7 +56,9 @@ class NotificationListener: NotificationListenerService() {
} }
runAsync { runAsync {
if (shouldRemoveNotification(sbn) == NotificationHandling.Replace) { val reason = shouldRemoveNotification(sbn)
if (reason != BlockingReason.None) {
val success = try { val success = try {
cancelNotification(sbn.key) cancelNotification(sbn.key)
@ -80,7 +82,18 @@ class NotificationListener: NotificationListenerService() {
else else
getString(R.string.notification_filter_blocking_failed_title) getString(R.string.notification_filter_blocking_failed_title)
) )
.setContentText(queryAppTitleCache.query(sbn.packageName)) .setContentText(
queryAppTitleCache.query(sbn.packageName) +
" - " +
when (reason) {
BlockingReason.NotPartOfAnCategory -> getString(R.string.lock_reason_short_no_category)
BlockingReason.TemporarilyBlocked -> getString(R.string.lock_reason_short_temporarily_blocked)
BlockingReason.TimeOver -> getString(R.string.lock_reason_short_time_over)
BlockingReason.TimeOverExtraTimeCanBeUsedLater -> getString(R.string.lock_reason_short_time_over)
BlockingReason.BlockedAtThisTime -> getString(R.string.lock_reason_short_blocked_time_area)
BlockingReason.None -> throw IllegalStateException()
}
)
.setLocalOnly(true) .setLocalOnly(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT) .setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build() .build()
@ -95,26 +108,26 @@ class NotificationListener: NotificationListenerService() {
// not interesting but required for old android versions // not interesting but required for old android versions
} }
private suspend fun shouldRemoveNotification(sbn: StatusBarNotification): NotificationHandling { private suspend fun shouldRemoveNotification(sbn: StatusBarNotification): BlockingReason {
if (sbn.packageName == packageName || sbn.isOngoing) { if (sbn.packageName == packageName || sbn.isOngoing) {
return NotificationHandling.Keep return BlockingReason.None
} }
val blockingReason = blockingReasonUtil.getBlockingReason(sbn.packageName).waitForNonNullValue() val blockingReason = blockingReasonUtil.getBlockingReason(sbn.packageName).waitForNonNullValue()
if (blockingReason == BlockingReason.None) { if (blockingReason == BlockingReason.None) {
return NotificationHandling.Keep return BlockingReason.None
} }
if (isSystemApp(sbn.packageName) && blockingReason == BlockingReason.NotPartOfAnCategory) { if (isSystemApp(sbn.packageName) && blockingReason == BlockingReason.NotPartOfAnCategory) {
return NotificationHandling.Keep return BlockingReason.None
} }
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "blocking notification of ${sbn.packageName} because $blockingReason") Log.d(LOG_TAG, "blocking notification of ${sbn.packageName} because $blockingReason")
} }
return NotificationHandling.Replace return blockingReason
} }
private fun isSystemApp(packageName: String): Boolean { private fun isSystemApp(packageName: String): Boolean {
@ -126,8 +139,4 @@ class NotificationListener: NotificationListenerService() {
return false return false
} }
} }
} }
enum class NotificationHandling {
Replace, Keep
}

View file

@ -55,4 +55,9 @@
Diese App ist in einer Kategorie, deren maximale Nutzungszeit erreicht wurde. Diese App ist in einer Kategorie, deren maximale Nutzungszeit erreicht wurde.
Es gab noch Extra-Zeit, aber die maximale Nutzung der Extra-Zeit wurde erreicht. Es gab noch Extra-Zeit, aber die maximale Nutzung der Extra-Zeit wurde erreicht.
</string> </string>
<string name="lock_reason_short_no_category">keine Kategorie</string>
<string name="lock_reason_short_temporarily_blocked">vorübergehend gesperrt</string>
<string name="lock_reason_short_time_over">Zeit verbraucht</string>
<string name="lock_reason_short_blocked_time_area">Sperrzeit</string>
</resources> </resources>

View file

@ -59,4 +59,9 @@
This App is part of a category whose time limit was reached. This App is part of a category whose time limit was reached.
There is still extra time, but the usage limit for the extra time was reached, too. There is still extra time, but the usage limit for the extra time was reached, too.
</string> </string>
<string name="lock_reason_short_no_category">no category</string>
<string name="lock_reason_short_temporarily_blocked">temporarily blocked</string>
<string name="lock_reason_short_time_over">time over</string>
<string name="lock_reason_short_blocked_time_area">blocked time area</string>
</resources> </resources>