Refactor badges and notifications, migrate badge settings

This commit is contained in:
MM20
2022-01-15 14:31:13 +01:00
parent dc64fdebdd
commit 6ca440f553
40 changed files with 937 additions and 303 deletions
-2
View File
@@ -44,9 +44,7 @@ dependencies {
implementation(libs.koin.android)
implementation(project(":music"))
implementation(project(":preferences"))
implementation(project(":badges"))
implementation(project(":permissions"))
}
@@ -0,0 +1,7 @@
package de.mm20.launcher2.notifications
import org.koin.dsl.module
val notificationsModule = module {
single<NotificationRepository> { NotificationRepositoryImpl() }
}
@@ -0,0 +1,63 @@
package de.mm20.launcher2.notifications
import android.service.notification.StatusBarNotification
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
interface NotificationRepository {
val notifications: Flow<List<StatusBarNotification>>
/**
* Internal use only. Used by NotificationService.
*/
fun setNotifications(notifications: List<StatusBarNotification>)
/**
* Internal use only. Used by NotificationService.
*/
fun postNotification(notification: StatusBarNotification)
/**
* Internal use only. Used by NotificationService.
*/
fun removeNotification(notification: StatusBarNotification)
/**
* Cancel a notification
*/
fun cancelNotification(notification: StatusBarNotification)
}
internal class NotificationRepositoryImpl() : NotificationRepository {
private val scope = CoroutineScope(Job() + Dispatchers.Main)
override val notifications: MutableStateFlow<List<StatusBarNotification>> = MutableStateFlow(
emptyList()
)
override fun setNotifications(notifications: List<StatusBarNotification>) {
this.notifications.value = notifications
}
override fun postNotification(notification: StatusBarNotification) {
notifications.value = notifications.value.filter { !isEqual(it, notification) } + notification
}
override fun removeNotification(notification: StatusBarNotification) {
notifications.value = notifications.value.filter { !isEqual(it, notification) }
}
private fun isEqual(
notification1: StatusBarNotification,
notification2: StatusBarNotification
): Boolean {
return notification1.key == notification2.key
}
override fun cancelNotification(notification: StatusBarNotification) {
NotificationService.getInstance()?.cancelNotification(notification.key)
}
}
@@ -1,28 +1,18 @@
package de.mm20.launcher2.notifications
import android.app.Notification
import android.app.Service
import android.content.Intent
import android.graphics.drawable.Drawable
import android.media.session.MediaSession
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import android.support.v4.media.session.MediaSessionCompat
import android.util.Log
import androidx.core.app.NotificationCompat
import de.mm20.launcher2.badges.Badge
import de.mm20.launcher2.badges.BadgeProvider
import de.mm20.launcher2.music.MusicRepository
import de.mm20.launcher2.permissions.PermissionsManager
import de.mm20.launcher2.preferences.LauncherPreferences
import org.koin.android.ext.android.inject
import java.lang.ref.WeakReference
class NotificationService : NotificationListenerService() {
private val musicRepository: MusicRepository by inject()
private val notificationRepository: NotificationRepository by inject()
private val badgeProvider: BadgeProvider by inject()
private val permissionsManager: PermissionsManager by inject()
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@@ -35,19 +25,10 @@ class NotificationService : NotificationListenerService() {
permissionsManager.reportNotificationListenerState(true)
instance = WeakReference(this)
val notifications = getNotifications().sortedBy { it.postTime }
for (n in notifications) {
/*val intent = Intent(Intent.ACTION_MAIN).apply { addCategory(Intent.CATEGORY_APP_MUSIC) }
if (packageManager.queryIntentActivities(intent, 0).none { it.activityInfo.packageName == n.packageName }) continue*/
val token = n.notification.extras[NotificationCompat.EXTRA_MEDIA_SESSION] as? MediaSession.Token
?: continue
musicRepository.setMediaSession(MediaSessionCompat.Token.fromToken(token), n.packageName)
}
if (LauncherPreferences.instance.notificationBadges) {
generateBadges()
}
notificationRepository.setNotifications(notifications)
}
fun getNotifications(): Array<StatusBarNotification> {
private fun getNotifications(): Array<StatusBarNotification> {
return try {
activeNotifications
} catch (e: SecurityException) {
@@ -55,73 +36,26 @@ class NotificationService : NotificationListenerService() {
}
}
fun generateBadges() {
badgeProvider.removeNotificationBadges()
getNotifications().forEach {
val pkg = it.packageName
val badge = badgeProvider.getBadge("app://$pkg") ?: Badge()
badge.number = activeNotifications.filter {
it.packageName == pkg
}.sumBy {
it.notification.number
}
badgeProvider.setBadge("app://$pkg", badge)
}
}
fun getNotifications(packageName: String): List<StatusBarNotification> {
return getNotifications().filter { it.packageName == packageName }
}
private fun getLargeIcon(notification: Notification): Drawable? {
return notification.getLargeIcon()?.loadDrawable(this)
}
override fun onNotificationPosted(sbn: StatusBarNotification) {
if (sbn.notification.category == Notification.CATEGORY_TRANSPORT || sbn.notification.category == Notification.CATEGORY_SERVICE) {
/*val intent = Intent(Intent.ACTION_MAIN).apply { addCategory(Intent.CATEGORY_APP_MUSIC) }
if (packageManager.queryIntentActivities(intent, 0).none { it.activityInfo.packageName == sbn.packageName }) return*/
val token = sbn.notification.extras[NotificationCompat.EXTRA_MEDIA_SESSION] as? MediaSession.Token
?: return
musicRepository.setMediaSession(MediaSessionCompat.Token.fromToken(token), sbn.packageName)
}
if (LauncherPreferences.instance.notificationBadges) {
val pkg = sbn.packageName
val badge = badgeProvider.getBadge("app://$pkg") ?: Badge()
badge.number = activeNotifications.filter { it.packageName == pkg }.sumBy {
it.notification.number
}
badgeProvider.setBadge("app://$pkg", badge)
}
notificationRepository.postNotification(sbn)
}
override fun onNotificationRemoved(sbn: StatusBarNotification) {
super.onNotificationRemoved(sbn)
if (LauncherPreferences.instance.notificationBadges) {
val pkg = sbn.packageName
if (getNotifications().any { it.packageName == pkg && it.id != sbn.id }) {
val badge = badgeProvider.getBadge("app://$pkg") ?: Badge()
badge.number = activeNotifications.filter { it.packageName == pkg }.sumBy {
it.notification.number
}
badgeProvider.setBadge("app://$pkg", badge)
} else {
badgeProvider.removeBadge("app://$pkg")
}
}
notificationRepository.removeNotification(sbn)
}
override fun onListenerDisconnected() {
super.onListenerDisconnected()
badgeProvider.removeNotificationBadges()
permissionsManager.reportNotificationListenerState(false)
notificationRepository.setNotifications(emptyList())
Log.d("MM20", "Notification listener disconnected")
}
companion object {
private var instance: WeakReference<NotificationService>? = null
fun getInstance(): NotificationService? {
internal fun getInstance(): NotificationService? {
return instance?.get()
}
}