Reorganize and group modules

This commit is contained in:
MM20
2022-12-13 17:37:26 +01:00
parent bac24baad2
commit 3f8880a90a
995 changed files with 501 additions and 298 deletions
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<service
android:name=".NotificationService"
android:exported="true"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
</application>
</manifest>
@@ -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.Default)
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)
}
}
@@ -0,0 +1,62 @@
package de.mm20.launcher2.notifications
import android.app.Service
import android.content.Intent
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import android.util.Log
import de.mm20.launcher2.permissions.PermissionsManager
import org.koin.android.ext.android.inject
import java.lang.ref.WeakReference
class NotificationService : NotificationListenerService() {
private val notificationRepository: NotificationRepository by inject()
private val permissionsManager: PermissionsManager by inject()
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return Service.START_STICKY
}
override fun onListenerConnected() {
super.onListenerConnected()
Log.d("MM20", "Notification listener connected")
permissionsManager.reportNotificationListenerState(true)
instance = WeakReference(this)
val notifications = getNotifications().sortedBy { it.postTime }
notificationRepository.setNotifications(notifications)
}
private fun getNotifications(): Array<StatusBarNotification> {
return try {
activeNotifications
} catch (e: SecurityException) {
emptyArray()
}
}
override fun onNotificationPosted(sbn: StatusBarNotification) {
notificationRepository.postNotification(sbn)
}
override fun onNotificationRemoved(sbn: StatusBarNotification) {
super.onNotificationRemoved(sbn)
notificationRepository.removeNotification(sbn)
}
override fun onListenerDisconnected() {
super.onListenerDisconnected()
permissionsManager.reportNotificationListenerState(false)
notificationRepository.setNotifications(emptyList())
Log.d("MM20", "Notification listener disconnected")
}
companion object {
private var instance: WeakReference<NotificationService>? = null
internal fun getInstance(): NotificationService? {
return instance?.get()
}
}
}