Hide notification content

Closes #843

Co-Authored-By: arkon <4098258+arkon@users.noreply.github.com>
This commit is contained in:
Jays2Kings 2021-07-04 13:58:58 -04:00
parent c4d9b66485
commit 3a6507101a
6 changed files with 113 additions and 91 deletions

View File

@ -84,9 +84,9 @@ class BackupNotifier(private val context: Context) {
val builder = with(progressNotificationBuilder) {
setContentTitle(context.getString(R.string.restoring_backup))
// if (!preferences.hideNotificationContent()) {
setContentText(content)
// }
if (!preferences.hideNotificationContent()) {
setContentText(content)
}
setProgress(maxAmount, progress, false)
setOnlyAlertOnce(true)

View File

@ -11,8 +11,10 @@ import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.data.notification.NotificationHandler
import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.lang.chop
import eu.kanade.tachiyomi.util.system.notificationManager
import uy.kohesive.injekt.injectLazy
import java.util.regex.Pattern
/**
@ -21,6 +23,9 @@ import java.util.regex.Pattern
* @param context context of application
*/
internal class DownloadNotifier(private val context: Context) {
private val preferences: PreferencesHelper by injectLazy()
/**
* Notification builder.
*/
@ -53,15 +58,6 @@ internal class DownloadNotifier(private val context: Context) {
context.notificationManager.notify(id, build())
}
/**
* Clear old actions if they exist.
*/
private fun clearActions() = with(notification) {
if (!mActions.isEmpty()) {
mActions.clear()
}
}
/**
* Dismiss the downloader's notification. Downloader error notifications use a different id, so
* those can only be dismissed by the user.
@ -89,7 +85,7 @@ internal class DownloadNotifier(private val context: Context) {
)
}
if (download != null) {
if (download != null && !preferences.hideNotificationContent()) {
val title = download.manga.title.chop(15)
val quotedTitle = Pattern.quote(title)
val chapter = download.chapter.name.replaceFirst(
@ -141,17 +137,22 @@ internal class DownloadNotifier(private val context: Context) {
)
}
val title = download.manga.title.chop(15)
val quotedTitle = Pattern.quote(title)
val chapter = download.chapter.name.replaceFirst(
"$quotedTitle[\\s]*[-]*[\\s]*".toRegex(RegexOption.IGNORE_CASE),
""
)
setContentTitle("$title - $chapter".chop(30))
setContentText(
val downloadingProgressText =
context.getString(R.string.downloading_progress)
.format(download.downloadedImages, download.pages!!.size)
)
if (preferences.hideNotificationContent()) {
setContentTitle(downloadingProgressText)
} else {
val title = download.manga.title.chop(15)
val quotedTitle = Pattern.quote(title)
val chapter = download.chapter.name.replaceFirst(
"$quotedTitle[\\s]*[-]*[\\s]*".toRegex(RegexOption.IGNORE_CASE),
""
)
setContentTitle("$title - $chapter".chop(30))
setContentText(downloadingProgressText)
}
setStyle(null)
setProgress(download.pages!!.size, download.downloadedImages, false)
}

View File

@ -75,7 +75,11 @@ class LibraryUpdateNotifier(private val context: Context) {
* @param total the total progress.
*/
fun showProgressNotification(manga: Manga, current: Int, total: Int) {
val title = manga.title
val title = if (preferences.hideNotificationContent()) {
context.getString(R.string.checking_for_new_chapters)
} else {
manga.title
}
context.notificationManager.notify(
Notifications.ID_LIBRARY_PROGRESS,
@ -129,72 +133,78 @@ class LibraryUpdateNotifier(private val context: Context) {
val updates = newUpdates.toImmutableMap()
GlobalScope.launch {
val notifications = ArrayList<Pair<Notification, Int>>()
updates.forEach {
val manga = it.key
val chapters = it.value
val chapterNames = chapters.map { chapter -> chapter.name }
notifications.add(
Pair(
context.notification(Notifications.CHANNEL_NEW_CHAPTERS) {
setSmallIcon(R.drawable.ic_tachi)
try {
val request = ImageRequest.Builder(context).data(manga)
.parameters(Parameters.Builder().set(MangaFetcher.onlyCache, true).build())
.networkCachePolicy(CachePolicy.READ_ONLY)
.transformations(CircleCropTransformation())
.size(width = ICON_SIZE, height = ICON_SIZE).build()
if (!preferences.hideNotificationContent()) {
updates.forEach {
val manga = it.key
val chapters = it.value
val chapterNames = chapters.map { chapter -> chapter.name }
notifications.add(
Pair(
context.notification(Notifications.CHANNEL_NEW_CHAPTERS) {
setSmallIcon(R.drawable.ic_tachi)
try {
val request = ImageRequest.Builder(context).data(manga)
.parameters(
Parameters.Builder().set(MangaFetcher.onlyCache, true)
.build()
)
.networkCachePolicy(CachePolicy.READ_ONLY)
.transformations(CircleCropTransformation())
.size(width = ICON_SIZE, height = ICON_SIZE).build()
Coil.imageLoader(context).execute(request).drawable?.let { drawable ->
setLargeIcon((drawable as BitmapDrawable).bitmap)
Coil.imageLoader(context)
.execute(request).drawable?.let { drawable ->
setLargeIcon((drawable as BitmapDrawable).bitmap)
}
} catch (e: Exception) {
}
} catch (e: Exception) {
}
setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
setContentTitle(manga.title)
color = ContextCompat.getColor(context, R.color.colorAccent)
val chaptersNames = if (chapterNames.size > MAX_CHAPTERS) {
"${chapterNames.take(MAX_CHAPTERS - 1).joinToString(", ")}, " +
context.resources.getQuantityString(
R.plurals.notification_and_n_more,
(chapterNames.size - (MAX_CHAPTERS - 1)),
(chapterNames.size - (MAX_CHAPTERS - 1))
setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
setContentTitle(manga.title)
color = ContextCompat.getColor(context, R.color.colorAccent)
val chaptersNames = if (chapterNames.size > MAX_CHAPTERS) {
"${chapterNames.take(MAX_CHAPTERS - 1).joinToString(", ")}, " +
context.resources.getQuantityString(
R.plurals.notification_and_n_more,
(chapterNames.size - (MAX_CHAPTERS - 1)),
(chapterNames.size - (MAX_CHAPTERS - 1))
)
} else chapterNames.joinToString(", ")
setContentText(chaptersNames)
setStyle(NotificationCompat.BigTextStyle().bigText(chaptersNames))
priority = NotificationCompat.PRIORITY_HIGH
setGroup(Notifications.GROUP_NEW_CHAPTERS)
setContentIntent(
NotificationReceiver.openChapterPendingActivity(
context,
manga,
chapters.first()
)
} else chapterNames.joinToString(", ")
setContentText(chaptersNames)
setStyle(NotificationCompat.BigTextStyle().bigText(chaptersNames))
priority = NotificationCompat.PRIORITY_HIGH
setGroup(Notifications.GROUP_NEW_CHAPTERS)
setContentIntent(
NotificationReceiver.openChapterPendingActivity(
context,
manga,
chapters.first()
)
)
addAction(
R.drawable.ic_eye_24dp,
context.getString(R.string.mark_as_read),
NotificationReceiver.markAsReadPendingBroadcast(
context,
manga,
chapters,
Notifications.ID_NEW_CHAPTERS
addAction(
R.drawable.ic_eye_24dp,
context.getString(R.string.mark_as_read),
NotificationReceiver.markAsReadPendingBroadcast(
context,
manga,
chapters,
Notifications.ID_NEW_CHAPTERS
)
)
)
addAction(
R.drawable.ic_book_24dp,
context.getString(R.string.view_chapters),
NotificationReceiver.openChapterPendingActivity(
context,
manga,
Notifications.ID_NEW_CHAPTERS
addAction(
R.drawable.ic_book_24dp,
context.getString(R.string.view_chapters),
NotificationReceiver.openChapterPendingActivity(
context,
manga,
Notifications.ID_NEW_CHAPTERS
)
)
)
setAutoCancel(true)
},
manga.id.hashCode()
setAutoCancel(true)
},
manga.id.hashCode()
)
)
)
}
}
NotificationManagerCompat.from(context).apply {
@ -213,15 +223,17 @@ class LibraryUpdateNotifier(private val context: Context) {
updates.size
)
)
setStyle(
NotificationCompat.BigTextStyle()
.bigText(
updates.keys.joinToString("\n") {
it.title.chop(45)
}
)
)
} else {
if (!preferences.hideNotificationContent()) {
setStyle(
NotificationCompat.BigTextStyle()
.bigText(
updates.keys.joinToString("\n") {
it.title.chop(45)
}
)
)
}
} else if (!preferences.hideNotificationContent()) {
setContentText(updates.keys.first().title.chop(45))
}
priority = NotificationCompat.PRIORITY_HIGH

View File

@ -185,6 +185,8 @@ object PreferenceKeys {
const val secureScreen = "secure_screen"
const val hideNotificationContent = "hide_notification_content"
const val removeArticles = "remove_articles"
const val skipPreMigration = "skip_pre_migration"

View File

@ -320,6 +320,8 @@ class PreferencesHelper(val context: Context) {
fun secureScreen() = rxPrefs.getBoolean(Keys.secureScreen, false)
fun hideNotificationContent() = prefs.getBoolean(Keys.hideNotificationContent, false)
fun removeArticles() = rxPrefs.getBoolean(Keys.removeArticles, false)
fun migrateFlags() = rxPrefs.getInteger("migrate_flags", Int.MAX_VALUE)

View File

@ -58,5 +58,10 @@ class SettingsSecurityController : SettingsController() {
true
}
}
switchPreference {
key = PreferenceKeys.hideNotificationContent
titleRes = R.string.hide_notification_content
defaultValue = false
}
}
}