From 952a98c1804b2134e59fcb471c54cf7c4e1f415e Mon Sep 17 00:00:00 2001 From: Catting <5874051+mm12@users.noreply.github.com> Date: Mon, 26 Aug 2024 08:31:02 -0500 Subject: [PATCH] Add "show entry" action to download notifications (#1159) * Add 'show entry' to download notifications Signed-off-by: Catting <5874051+mm12@users.noreply.github.com> * fixup! Add 'show entry' to download notifications Signed-off-by: Catting <5874051+mm12@users.noreply.github.com> * fixup! Add 'show entry' to download notifications Signed-off-by: Catting <5874051+mm12@users.noreply.github.com> * spotless! Add 'show entry' to download notifications Signed-off-by: Catting <5874051+mm12@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com> * fixup! spotless- Apply suggestions from code review Signed-off-by: Catting <5874051+mm12@users.noreply.github.com> --------- Signed-off-by: Catting <5874051+mm12@users.noreply.github.com> Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com> --- .../data/download/DownloadNotifier.kt | 25 +++++++++++++++++-- .../tachiyomi/data/download/Downloader.kt | 5 ++-- .../data/notification/NotificationReceiver.kt | 22 ++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt index 4acd8322e..ee3a5fe0f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt @@ -83,6 +83,11 @@ internal class DownloadNotifier(private val context: Context) { context.stringResource(MR.strings.action_pause), NotificationReceiver.pauseDownloadsPendingBroadcast(context), ) + addAction( + R.drawable.ic_book_24dp, + context.stringResource(MR.strings.action_show_manga), + NotificationReceiver.openEntryPendingActivity(context, download.manga.id), + ) } val downloadingProgressText = context.stringResource( @@ -160,9 +165,10 @@ internal class DownloadNotifier(private val context: Context) { * * @param reason the text to show. * @param timeout duration after which to automatically dismiss the notification. + * @param mangaId the id of the entry being warned about * Only works on Android 8+. */ - fun onWarning(reason: String, timeout: Long? = null, contentIntent: PendingIntent? = null) { + fun onWarning(reason: String, timeout: Long? = null, contentIntent: PendingIntent? = null, mangaId: Long? = null) { with(errorNotificationBuilder) { setContentTitle(context.stringResource(MR.strings.download_notifier_downloader_title)) setStyle(NotificationCompat.BigTextStyle().bigText(reason)) @@ -170,6 +176,13 @@ internal class DownloadNotifier(private val context: Context) { setAutoCancel(true) clearActions() setContentIntent(NotificationHandler.openDownloadManagerPendingActivity(context)) + if (mangaId != null) { + addAction( + R.drawable.ic_book_24dp, + context.stringResource(MR.strings.action_show_manga), + NotificationReceiver.openEntryPendingActivity(context, mangaId), + ) + } setProgress(0, 0, false) timeout?.let { setTimeoutAfter(it) } contentIntent?.let { setContentIntent(it) } @@ -187,8 +200,9 @@ internal class DownloadNotifier(private val context: Context) { * * @param error string containing error information. * @param chapter string containing chapter title. + * @param mangaId the id of the entry that the error occurred on */ - fun onError(error: String? = null, chapter: String? = null, mangaTitle: String? = null) { + fun onError(error: String? = null, chapter: String? = null, mangaTitle: String? = null, mangaId: Long? = null) { // Create notification with(errorNotificationBuilder) { setContentTitle( @@ -198,6 +212,13 @@ internal class DownloadNotifier(private val context: Context) { setSmallIcon(R.drawable.ic_warning_white_24dp) clearActions() setContentIntent(NotificationHandler.openDownloadManagerPendingActivity(context)) + if (mangaId != null) { + addAction( + R.drawable.ic_book_24dp, + context.stringResource(MR.strings.action_show_manga), + NotificationReceiver.openEntryPendingActivity(context, mangaId), + ) + } setProgress(0, 0, false) show(Notifications.ID_DOWNLOAD_CHAPTER_ERROR) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index bf4940e2a..a8fd85f43 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -324,6 +324,7 @@ class Downloader( context.stringResource(MR.strings.download_insufficient_space), download.chapter.name, download.manga.title, + download.manga.id, ) return } @@ -407,7 +408,7 @@ class Downloader( // If the page list threw, it will resume here logcat(LogPriority.ERROR, error) download.status = Download.State.ERROR - notifier.onError(error.message, download.chapter.name, download.manga.title) + notifier.onError(error.message, download.chapter.name, download.manga.title, download.manga.id) } } @@ -457,7 +458,7 @@ class Downloader( // Mark this page as error and allow to download the remaining page.progress = 0 page.status = Page.State.ERROR - notifier.onError(e.message, download.chapter.name, download.manga.title) + notifier.onError(e.message, download.chapter.name, download.manga.title, download.manga.id) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt index de9e55803..01dada5c8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt @@ -248,6 +248,8 @@ class NotificationReceiver : BroadcastReceiver() { private const val ACTION_OPEN_CHAPTER = "$ID.$NAME.ACTION_OPEN_CHAPTER" private const val ACTION_DOWNLOAD_CHAPTER = "$ID.$NAME.ACTION_DOWNLOAD_CHAPTER" + private const val ACTION_OPEN_ENTRY = "$ID.$NAME.ACTION_OPEN_ENTRY" + private const val ACTION_RESUME_DOWNLOADS = "$ID.$NAME.ACTION_RESUME_DOWNLOADS" private const val ACTION_PAUSE_DOWNLOADS = "$ID.$NAME.ACTION_PAUSE_DOWNLOADS" private const val ACTION_CLEAR_DOWNLOADS = "$ID.$NAME.ACTION_CLEAR_DOWNLOADS" @@ -484,6 +486,26 @@ class NotificationReceiver : BroadcastReceiver() { ) } + /** + * Returns [PendingIntent] that opens the manga info controller + * + * @param context context of application + * @param mangaId id of the entry to open + */ + internal fun openEntryPendingActivity(context: Context, mangaId: Long): PendingIntent { + val newIntent = Intent(context, MainActivity::class.java).setAction(Constants.SHORTCUT_MANGA) + .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + .putExtra(Constants.MANGA_EXTRA, mangaId) + .putExtra("notificationId", mangaId.hashCode()) + + return PendingIntent.getActivity( + context, + mangaId.hashCode(), + newIntent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, + ) + } + /** * Returns [PendingIntent] that starts a service which stops the library update *