Add the app notifications filter

This commit is contained in:
Tommaso Berlose
2020-10-14 12:54:39 +02:00
parent 5259a81cfb
commit 1c1f55e20a
19 changed files with 376 additions and 41 deletions

View File

@ -1,15 +1,14 @@
package com.tommasoberlose.anotherwidget.helpers
import android.app.Notification
import android.app.NotificationManager
import android.content.ContentResolver
import android.content.Context
import android.service.notification.StatusBarNotification
import android.provider.Settings
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import com.chibatching.kotpref.Kotpref
import com.chibatching.kotpref.blockingBulk
import com.google.gson.Gson
import com.tommasoberlose.anotherwidget.global.Preferences
import java.lang.Exception
import com.tommasoberlose.anotherwidget.receivers.NotificationListener
object ActiveNotificationsHelper {
fun showLastNotification(): Boolean {
@ -25,4 +24,22 @@ object ActiveNotificationsHelper {
remove(Preferences::lastNotificationIcon)
}
}
fun checkNotificationAccess(context: Context): Boolean {
val contentResolver: ContentResolver = context.contentResolver
val enabledNotificationListeners =
Settings.Secure.getString(contentResolver, "enabled_notification_listeners")
val packageName: String = context.packageName
return NotificationManagerCompat.getEnabledListenerPackages(context).contains(packageName) && (enabledNotificationListeners != null && enabledNotificationListeners.contains(NotificationListener::class.java.name))
}
fun isAppAccepted(appPkg: String): Boolean = Preferences.appNotificationsFilter == "" || Preferences.appNotificationsFilter.contains(appPkg)
fun toggleAppFilter(appPkg: String) {
if (Preferences.appNotificationsFilter == "" || !Preferences.appNotificationsFilter.contains(appPkg)) {
Preferences.appNotificationsFilter = Preferences.appNotificationsFilter.split(",").union(listOf(appPkg)).joinToString(separator = ",")
} else {
Preferences.appNotificationsFilter = Preferences.appNotificationsFilter.split(",").filter { it != appPkg }.joinToString(separator = ",")
}
}
}