From d400ac2a49001b207741ab30e328259a1e77de4c Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 18 Feb 2023 17:10:45 -0500 Subject: [PATCH] Remove unnecessary usages of NotificationManagerCompat to actually create notifications --- app/src/main/java/eu/kanade/tachiyomi/App.kt | 3 +- .../data/library/LibraryUpdateNotifier.kt | 69 +++++++++---------- .../extension/api/ExtensionUpdateNotifier.kt | 47 +++++-------- 3 files changed, 53 insertions(+), 66 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/App.kt b/app/src/main/java/eu/kanade/tachiyomi/App.kt index ae018c2ec..2ea40a2ab 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/App.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/App.kt @@ -11,7 +11,6 @@ import android.content.IntentFilter import android.os.Build import android.os.Looper import android.webkit.WebView -import androidx.core.app.NotificationManagerCompat import androidx.core.content.getSystemService import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner @@ -43,6 +42,7 @@ import eu.kanade.tachiyomi.util.system.animatorDurationScale import eu.kanade.tachiyomi.util.system.isPreviewBuildType import eu.kanade.tachiyomi.util.system.isReleaseBuildType import eu.kanade.tachiyomi.util.system.notification +import eu.kanade.tachiyomi.util.system.notificationManager import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach @@ -96,7 +96,6 @@ class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory { // Show notification to disable Incognito Mode when it's enabled basePreferences.incognitoMode().changes() .onEach { enabled -> - val notificationManager = NotificationManagerCompat.from(this) if (enabled) { disableIncognitoReceiver.register() val notification = notification(Notifications.CHANNEL_INCOGNITO_MODE) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt index 9a989fdb6..f848b1aba 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt @@ -9,7 +9,6 @@ import android.graphics.BitmapFactory import android.graphics.drawable.BitmapDrawable import android.net.Uri import androidx.core.app.NotificationCompat -import androidx.core.app.NotificationManagerCompat import coil.imageLoader import coil.request.ImageRequest import coil.transform.CircleCropTransformation @@ -158,47 +157,45 @@ class LibraryUpdateNotifier(private val context: Context) { * @param updates a list of manga with new updates. */ fun showUpdateNotifications(updates: List>>) { - NotificationManagerCompat.from(context).apply { - // Parent group notification - notify( - Notifications.ID_NEW_CHAPTERS, - context.notification(Notifications.CHANNEL_NEW_CHAPTERS) { - setContentTitle(context.getString(R.string.notification_new_chapters)) - if (updates.size == 1 && !preferences.hideNotificationContent().get()) { - setContentText(updates.first().first.title.chop(NOTIF_TITLE_MAX_LEN)) - } else { - setContentText(context.resources.getQuantityString(R.plurals.notification_new_chapters_summary, updates.size, updates.size)) + // Parent group notification + context.notificationManager.notify( + Notifications.ID_NEW_CHAPTERS, + context.notification(Notifications.CHANNEL_NEW_CHAPTERS) { + setContentTitle(context.getString(R.string.notification_new_chapters)) + if (updates.size == 1 && !preferences.hideNotificationContent().get()) { + setContentText(updates.first().first.title.chop(NOTIF_TITLE_MAX_LEN)) + } else { + setContentText(context.resources.getQuantityString(R.plurals.notification_new_chapters_summary, updates.size, updates.size)) - if (!preferences.hideNotificationContent().get()) { - setStyle( - NotificationCompat.BigTextStyle().bigText( - updates.joinToString("\n") { - it.first.title.chop(NOTIF_TITLE_MAX_LEN) - }, - ), - ) - } + if (!preferences.hideNotificationContent().get()) { + setStyle( + NotificationCompat.BigTextStyle().bigText( + updates.joinToString("\n") { + it.first.title.chop(NOTIF_TITLE_MAX_LEN) + }, + ), + ) } + } - setSmallIcon(R.drawable.ic_tachi) - setLargeIcon(notificationBitmap) + setSmallIcon(R.drawable.ic_tachi) + setLargeIcon(notificationBitmap) - setGroup(Notifications.GROUP_NEW_CHAPTERS) - setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY) - setGroupSummary(true) - priority = NotificationCompat.PRIORITY_HIGH + setGroup(Notifications.GROUP_NEW_CHAPTERS) + setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY) + setGroupSummary(true) + priority = NotificationCompat.PRIORITY_HIGH - setContentIntent(getNotificationIntent()) - setAutoCancel(true) - }, - ) + setContentIntent(getNotificationIntent()) + setAutoCancel(true) + }, + ) - // Per-manga notification - if (!preferences.hideNotificationContent().get()) { - launchUI { - updates.forEach { (manga, chapters) -> - notify(manga.id.hashCode(), createNewChaptersNotification(manga, chapters)) - } + // Per-manga notification + if (!preferences.hideNotificationContent().get()) { + launchUI { + updates.forEach { (manga, chapters) -> + context.notificationManager.notify(manga.id.hashCode(), createNewChaptersNotification(manga, chapters)) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionUpdateNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionUpdateNotifier.kt index e63b72832..ad8a74761 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionUpdateNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionUpdateNotifier.kt @@ -1,42 +1,33 @@ package eu.kanade.tachiyomi.extension.api -import android.Manifest import android.content.Context -import android.content.pm.PackageManager -import androidx.core.app.ActivityCompat import androidx.core.app.NotificationCompat -import androidx.core.app.NotificationManagerCompat import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.util.system.notification +import eu.kanade.tachiyomi.util.system.notificationManager class ExtensionUpdateNotifier(private val context: Context) { fun promptUpdates(names: List) { - if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { - return - } - - NotificationManagerCompat.from(context).apply { - notify( - Notifications.ID_UPDATES_TO_EXTS, - context.notification(Notifications.CHANNEL_EXTENSIONS_UPDATE) { - setContentTitle( - context.resources.getQuantityString( - R.plurals.update_check_notification_ext_updates, - names.size, - names.size, - ), - ) - val extNames = names.joinToString(", ") - setContentText(extNames) - setStyle(NotificationCompat.BigTextStyle().bigText(extNames)) - setSmallIcon(R.drawable.ic_extension_24dp) - setContentIntent(NotificationReceiver.openExtensionsPendingActivity(context)) - setAutoCancel(true) - }, - ) - } + context.notificationManager.notify( + Notifications.ID_UPDATES_TO_EXTS, + context.notification(Notifications.CHANNEL_EXTENSIONS_UPDATE) { + setContentTitle( + context.resources.getQuantityString( + R.plurals.update_check_notification_ext_updates, + names.size, + names.size, + ), + ) + val extNames = names.joinToString(", ") + setContentText(extNames) + setStyle(NotificationCompat.BigTextStyle().bigText(extNames)) + setSmallIcon(R.drawable.ic_extension_24dp) + setContentIntent(NotificationReceiver.openExtensionsPendingActivity(context)) + setAutoCancel(true) + }, + ) } }