Add uses of NotificationManagerCompat and related classes

pull/440/head
Isira Seneviratne 3 years ago committed by Koitharu
parent 2342594885
commit 01e27ba91f

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

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

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

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

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

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

Loading…
Cancel
Save