Initial commit

This commit is contained in:
MM20
2021-09-18 23:37:52 +02:00
commit 749e4e3073
938 changed files with 50475 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
/build
+50
View File
@@ -0,0 +1,50 @@
plugins {
id("com.android.library")
id("kotlin-android")
id("kotlin-android-extensions")
}
android {
compileSdk = sdk.versions.compileSdk.get().toInt()
defaultConfig {
minSdk = sdk.versions.minSdk.get().toInt()
targetSdk = sdk.versions.targetSdk.get().toInt()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation(libs.bundles.kotlin)
implementation(libs.androidx.core)
implementation(libs.androidx.media2)
implementation(libs.bundles.androidx.lifecycle)
implementation(project(":music"))
implementation(project(":preferences"))
implementation(project(":badges"))
}
View File
+21
View File
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.kts.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.mm20.launcher2.notifications">
<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,116 @@
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 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.preferences.LauncherPreferences
import java.lang.ref.WeakReference
class NotificationService : NotificationListenerService() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return Service.START_STICKY
}
override fun onListenerConnected() {
super.onListenerConnected()
instance = WeakReference(this)
val notifications = getNotifications().sortedByDescending { 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.getInstance(this).setMediaSession(MediaSessionCompat.Token.fromToken(token))
}
if (LauncherPreferences.instance.notificationBadges) {
generateBadges()
}
}
fun getNotifications(): Array<StatusBarNotification> {
return try {
activeNotifications
} catch (e: SecurityException) {
emptyArray()
}
}
fun generateBadges() {
BadgeProvider.getInstance(this).removeNotificationBadges()
getNotifications().forEach {
val pkg = it.packageName
val badge = BadgeProvider.getInstance(this).getBadge("app://$pkg") ?: Badge()
badge.number = activeNotifications.filter {
it.packageName == pkg
}.sumBy {
it.notification.number
}
BadgeProvider.getInstance(this).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.getInstance(this).setMediaSession(MediaSessionCompat.Token.fromToken(token))
}
if (LauncherPreferences.instance.notificationBadges) {
val pkg = sbn.packageName
val badge = BadgeProvider.getInstance(this).getBadge("app://$pkg") ?: Badge()
badge.number = activeNotifications.filter { it.packageName == pkg }.sumBy {
it.notification.number
}
BadgeProvider.getInstance(this).setBadge("app://$pkg", badge)
}
}
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.getInstance(this).getBadge("app://$pkg") ?: Badge()
badge.number = activeNotifications.filter { it.packageName == pkg }.sumBy {
it.notification.number
}
BadgeProvider.getInstance(this).setBadge("app://$pkg", badge)
} else {
BadgeProvider.getInstance(this).removeBadge("app://$pkg")
}
}
}
override fun onListenerDisconnected() {
super.onListenerDisconnected()
BadgeProvider.getInstance(this).removeNotificationBadges()
}
companion object {
private var instance: WeakReference<NotificationService>? = null
fun getInstance(): NotificationService? {
return instance?.get()
}
}
}