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) { val builder = with(progressNotificationBuilder) {
setContentTitle(context.getString(R.string.restoring_backup)) setContentTitle(context.getString(R.string.restoring_backup))
// if (!preferences.hideNotificationContent()) { if (!preferences.hideNotificationContent()) {
setContentText(content) setContentText(content)
// } }
setProgress(maxAmount, progress, false) setProgress(maxAmount, progress, false)
setOnlyAlertOnce(true) 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.NotificationHandler
import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications 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.lang.chop
import eu.kanade.tachiyomi.util.system.notificationManager import eu.kanade.tachiyomi.util.system.notificationManager
import uy.kohesive.injekt.injectLazy
import java.util.regex.Pattern import java.util.regex.Pattern
/** /**
@ -21,6 +23,9 @@ import java.util.regex.Pattern
* @param context context of application * @param context context of application
*/ */
internal class DownloadNotifier(private val context: Context) { internal class DownloadNotifier(private val context: Context) {
private val preferences: PreferencesHelper by injectLazy()
/** /**
* Notification builder. * Notification builder.
*/ */
@ -53,15 +58,6 @@ internal class DownloadNotifier(private val context: Context) {
context.notificationManager.notify(id, build()) 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 * Dismiss the downloader's notification. Downloader error notifications use a different id, so
* those can only be dismissed by the user. * 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 title = download.manga.title.chop(15)
val quotedTitle = Pattern.quote(title) val quotedTitle = Pattern.quote(title)
val chapter = download.chapter.name.replaceFirst( 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 downloadingProgressText =
val quotedTitle = Pattern.quote(title)
val chapter = download.chapter.name.replaceFirst(
"$quotedTitle[\\s]*[-]*[\\s]*".toRegex(RegexOption.IGNORE_CASE),
""
)
setContentTitle("$title - $chapter".chop(30))
setContentText(
context.getString(R.string.downloading_progress) context.getString(R.string.downloading_progress)
.format(download.downloadedImages, download.pages!!.size) .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) setStyle(null)
setProgress(download.pages!!.size, download.downloadedImages, false) setProgress(download.pages!!.size, download.downloadedImages, false)
} }

View File

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

View File

@ -185,6 +185,8 @@ object PreferenceKeys {
const val secureScreen = "secure_screen" const val secureScreen = "secure_screen"
const val hideNotificationContent = "hide_notification_content"
const val removeArticles = "remove_articles" const val removeArticles = "remove_articles"
const val skipPreMigration = "skip_pre_migration" 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 secureScreen() = rxPrefs.getBoolean(Keys.secureScreen, false)
fun hideNotificationContent() = prefs.getBoolean(Keys.hideNotificationContent, false)
fun removeArticles() = rxPrefs.getBoolean(Keys.removeArticles, false) fun removeArticles() = rxPrefs.getBoolean(Keys.removeArticles, false)
fun migrateFlags() = rxPrefs.getInteger("migrate_flags", Int.MAX_VALUE) fun migrateFlags() = rxPrefs.getInteger("migrate_flags", Int.MAX_VALUE)

View File

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