Add uses of NotificationManagerCompat and related classes

This commit is contained in:
Isira Seneviratne
2023-07-26 06:24:37 +05:30
committed by Koitharu
parent 2342594885
commit 01e27ba91f
6 changed files with 88 additions and 131 deletions

View File

@@ -1,13 +1,11 @@
package org.koitharu.kotatsu.download.ui.worker
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.text.format.DateUtils
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.PendingIntentCompat
@@ -247,20 +245,14 @@ class DownloadNotificationFactory @AssistedInject constructor(
}
private fun createChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val manager = NotificationManagerCompat.from(context)
if (manager.getNotificationChannel(CHANNEL_ID) == null) {
val channel = NotificationChannel(
CHANNEL_ID,
context.getString(R.string.downloads),
NotificationManager.IMPORTANCE_LOW,
)
channel.enableVibration(false)
channel.enableLights(false)
channel.setSound(null, null)
manager.createNotificationChannel(channel)
}
}
val manager = NotificationManagerCompat.from(context)
val channel = NotificationChannelCompat.Builder(CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_LOW)
.setName(context.getString(R.string.downloads))
.setVibrationEnabled(false)
.setLightsEnabled(false)
.setSound(null, null)
.build()
manager.createNotificationChannel(channel)
}
@AssistedFactory

View File

@@ -1,13 +1,12 @@
package org.koitharu.kotatsu.local.ui
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.net.Uri
import android.os.Build
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.PendingIntentCompat
import androidx.hilt.work.HiltWorker
import androidx.work.Constraints
@@ -39,9 +38,7 @@ class ImportWorker @AssistedInject constructor(
private val coil: ImageLoader
) : CoroutineWorker(appContext, params) {
private val notificationManager by lazy {
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}
private val notificationManager by lazy { NotificationManagerCompat.from(appContext) }
override suspend fun doWork(): Result {
val uri = inputData.getString(DATA_URI)?.toUriOrNull() ?: return Result.failure()
@@ -56,14 +53,14 @@ class ImportWorker @AssistedInject constructor(
override suspend fun getForegroundInfo(): ForegroundInfo {
val title = applicationContext.getString(R.string.importing_manga)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(CHANNEL_ID, title, NotificationManager.IMPORTANCE_LOW)
channel.setShowBadge(false)
channel.enableVibration(false)
channel.setSound(null, null)
channel.enableLights(false)
notificationManager.createNotificationChannel(channel)
}
val channel = NotificationChannelCompat.Builder(CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_LOW)
.setName(title)
.setShowBadge(false)
.setVibrationEnabled(false)
.setSound(null, null)
.setLightsEnabled(false)
.build()
notificationManager.createNotificationChannel(channel)
val notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
.setContentTitle(title)

View File

@@ -1,11 +1,11 @@
package org.koitharu.kotatsu.local.ui
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat
import dagger.hilt.android.AndroidEntryPoint
@@ -67,15 +67,15 @@ class LocalChaptersRemoveService : CoroutineIntentService() {
private fun startForeground() {
val title = getString(R.string.local_manga_processing)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(CHANNEL_ID, title, NotificationManager.IMPORTANCE_LOW)
channel.setShowBadge(false)
channel.enableVibration(false)
channel.setSound(null, null)
channel.enableLights(false)
manager.createNotificationChannel(channel)
}
val manager = NotificationManagerCompat.from(this)
val channel = NotificationChannelCompat.Builder(CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_LOW)
.setName(title)
.setShowBadge(false)
.setVibrationEnabled(false)
.setSound(null, null)
.setLightsEnabled(false)
.build()
manager.createNotificationChannel(channel)
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(title)

View File

@@ -1,12 +1,11 @@
package org.koitharu.kotatsu.suggestions.ui
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.os.Build
import androidx.annotation.FloatRange
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.PendingIntentCompat
import androidx.core.text.HtmlCompat
import androidx.core.text.buildSpannedString
@@ -83,6 +82,8 @@ class SuggestionsWorker @AssistedInject constructor(
private val sourcesRepository: MangaSourcesRepository,
) : CoroutineWorker(appContext, params) {
private val notificationManager by lazy { NotificationManagerCompat.from(appContext) }
override suspend fun doWork(): Result {
trySetForeground()
if (!appSettings.isSuggestionsEnabled) {
@@ -95,20 +96,15 @@ class SuggestionsWorker @AssistedInject constructor(
}
override suspend fun getForegroundInfo(): ForegroundInfo {
val manager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val title = applicationContext.getString(R.string.suggestions_updating)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
WORKER_CHANNEL_ID,
title,
NotificationManager.IMPORTANCE_LOW,
)
channel.setShowBadge(false)
channel.enableVibration(false)
channel.setSound(null, null)
channel.enableLights(false)
manager.createNotificationChannel(channel)
}
val channel = NotificationChannelCompat.Builder(WORKER_CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_LOW)
.setName(title)
.setShowBadge(true)
.setVibrationEnabled(false)
.setSound(null, null)
.setLightsEnabled(true)
.build()
notificationManager.createNotificationChannel(channel)
val notification = NotificationCompat.Builder(applicationContext, WORKER_CHANNEL_ID)
.setContentTitle(title)
@@ -215,18 +211,14 @@ class SuggestionsWorker @AssistedInject constructor(
}.getOrDefault(emptyList())
private suspend fun showNotification(manga: Manga) {
val manager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
MANGA_CHANNEL_ID,
applicationContext.getString(R.string.suggestions),
NotificationManager.IMPORTANCE_DEFAULT,
)
channel.description = applicationContext.getString(R.string.suggestions_summary)
channel.enableLights(true)
channel.setShowBadge(true)
manager.createNotificationChannel(channel)
}
val channel = NotificationChannelCompat.Builder(MANGA_CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_DEFAULT)
.setName(applicationContext.getString(R.string.suggestions))
.setDescription(applicationContext.getString(R.string.suggestions_summary))
.setLightsEnabled(true)
.setShowBadge(true)
.build()
notificationManager.createNotificationChannel(channel)
val id = manga.url.hashCode()
val title = applicationContext.getString(R.string.suggestion_manga, manga.title)
val builder = NotificationCompat.Builder(applicationContext, MANGA_CHANNEL_ID)
@@ -305,7 +297,7 @@ class SuggestionsWorker @AssistedInject constructor(
),
)
}
manager.notify(TAG, id, builder.build())
notificationManager.notify(TAG, id, builder.build())
}
@FloatRange(from = 0.0, to = 1.0)

View File

@@ -1,13 +1,13 @@
package org.koitharu.kotatsu.tracker.work
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.VISIBILITY_PUBLIC
import androidx.core.app.NotificationCompat.VISIBILITY_SECRET
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.PendingIntentCompat
import androidx.core.content.ContextCompat
import androidx.hilt.work.HiltWorker
@@ -67,10 +67,7 @@ class TrackWorker @AssistedInject constructor(
private val tracker: Tracker,
@TrackerLogger private val logger: FileLogger,
) : CoroutineWorker(context, workerParams) {
private val notificationManager by lazy {
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}
private val notificationManager by lazy { NotificationManagerCompat.from(applicationContext) }
override suspend fun doWork(): Result {
trySetForeground()
@@ -209,18 +206,15 @@ class TrackWorker @AssistedInject constructor(
override suspend fun getForegroundInfo(): ForegroundInfo {
val title = applicationContext.getString(R.string.check_for_new_chapters)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
WORKER_CHANNEL_ID,
title,
NotificationManager.IMPORTANCE_LOW,
)
channel.setShowBadge(false)
channel.enableVibration(false)
channel.setSound(null, null)
channel.enableLights(false)
notificationManager.createNotificationChannel(channel)
}
val channel = NotificationChannelCompat.Builder(WORKER_CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_LOW)
.setName(title)
.setShowBadge(false)
.setVibrationEnabled(false)
.setSound(null, null)
.setLightsEnabled(false)
.build()
notificationManager.createNotificationChannel(channel)
val notification = NotificationCompat.Builder(applicationContext, WORKER_CHANNEL_ID)
.setContentTitle(title)
.setPriority(NotificationCompat.PRIORITY_MIN)

View File

@@ -1,11 +1,10 @@
package org.koitharu.kotatsu.tracker.work
import android.app.NotificationChannel
import android.app.NotificationChannelGroup
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationChannelGroupCompat
import androidx.core.app.NotificationManagerCompat
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
@@ -24,9 +23,6 @@ class TrackerNotificationChannels @Inject constructor(
get() = !manager.areNotificationsEnabled()
fun updateChannels(categories: Collection<FavouriteCategory>) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return
}
manager.deleteNotificationChannel(OLD_CHANNEL_ID)
val group = createGroup()
val existingChannels = group.channels.associateByTo(HashMap()) { it.id }
@@ -35,8 +31,10 @@ class TrackerNotificationChannels @Inject constructor(
if (existingChannels.remove(id)?.name == category.title) {
continue
}
val channel = NotificationChannel(id, category.title, NotificationManager.IMPORTANCE_DEFAULT)
channel.group = GROUP_ID
val channel = NotificationChannelCompat.Builder(id, NotificationManagerCompat.IMPORTANCE_DEFAULT)
.setName(category.title)
.setGroup(GROUP_ID)
.build()
manager.createNotificationChannel(channel)
}
existingChannels.remove(CHANNEL_ID_HISTORY)
@@ -47,23 +45,15 @@ class TrackerNotificationChannels @Inject constructor(
}
fun createChannel(category: FavouriteCategory) {
renameChannel(category.id, category.title)
}
fun renameChannel(categoryId: Long, name: String) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return
}
val id = getFavouritesChannelId(categoryId)
val channel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT)
channel.group = createGroup().id
val id = getFavouritesChannelId(category.id)
val channel = NotificationChannelCompat.Builder(id, NotificationManagerCompat.IMPORTANCE_DEFAULT)
.setName(category.title)
.setGroup(createGroup().id)
.build()
manager.createNotificationChannel(channel)
}
fun deleteChannel(categoryId: Long) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return
}
manager.deleteNotificationChannel(getFavouritesChannelId(categoryId))
}
@@ -97,11 +87,8 @@ class TrackerNotificationChannels @Inject constructor(
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return settings.isTrackerNotificationsEnabled
}
val group = manager.getNotificationChannelGroup(GROUP_ID) ?: return true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && group.isBlocked) {
return false
}
return group.channels.any { it.importance != NotificationManagerCompat.IMPORTANCE_NONE }
val group = manager.getNotificationChannelGroupCompat(GROUP_ID) ?: return true
return !group.isBlocked && group.channels.any { it.importance != NotificationManagerCompat.IMPORTANCE_NONE }
}
fun getFavouritesChannelId(categoryId: Long): String {
@@ -112,26 +99,21 @@ class TrackerNotificationChannels @Inject constructor(
return CHANNEL_ID_HISTORY
}
@RequiresApi(Build.VERSION_CODES.O)
private fun createGroup(): NotificationChannelGroup {
manager.getNotificationChannelGroup(GROUP_ID)?.let {
return it
private fun createGroup(): NotificationChannelGroupCompat {
return manager.getNotificationChannelGroupCompat(GROUP_ID) ?: run {
val group = NotificationChannelGroupCompat.Builder(GROUP_ID)
.setName(context.getString(R.string.new_chapters))
.build()
manager.createNotificationChannelGroup(group)
group
}
val group = NotificationChannelGroup(GROUP_ID, context.getString(R.string.new_chapters))
manager.createNotificationChannelGroup(group)
return group
}
private fun createHistoryChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return
}
val channel = NotificationChannel(
CHANNEL_ID_HISTORY,
context.getString(R.string.history),
NotificationManager.IMPORTANCE_DEFAULT,
)
channel.group = GROUP_ID
val channel = NotificationChannelCompat.Builder(CHANNEL_ID_HISTORY, NotificationManagerCompat.IMPORTANCE_DEFAULT)
.setName(context.getString(R.string.history))
.setGroup(GROUP_ID)
.build()
manager.createNotificationChannel(channel)
}