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 5f44ebe01..43d4a23e0 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 @@ -514,10 +514,9 @@ class Downloader( companion object { const val TMP_DIR_SUFFIX = "_tmp" + const val CHAPTERS_PER_SOURCE_QUEUE_WARNING_THRESHOLD = 15 } } -private const val CHAPTERS_PER_SOURCE_QUEUE_WARNING_THRESHOLD = 15 - // Arbitrary minimum required space to start a download: 50 MB private const val MIN_DISK_SPACE = 50 * 1024 * 1024 diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt index 991ae3011..3a1fa00b3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt @@ -16,6 +16,7 @@ import coil.transform.CircleCropTransformation import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.download.Downloader import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.preference.PreferencesHelper @@ -212,6 +213,20 @@ class LibraryUpdateNotifier(private val context: Context) { Notifications.ID_NEW_CHAPTERS ) ) + // Download chapters action + // Only add the action when chapters is within threshold + if (chapters.size <= Downloader.CHAPTERS_PER_SOURCE_QUEUE_WARNING_THRESHOLD) { + addAction( + R.drawable.ic_download_24dp, + context.getString(R.string.action_download), + NotificationReceiver.downloadChaptersPendingBroadcast( + context, + manga, + chapters, + Notifications.ID_NEW_CHAPTERS + ) + ) + } } } 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 f8315e7f9..9ffcfdafe 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 @@ -102,6 +102,18 @@ class NotificationReceiver : BroadcastReceiver() { markAsRead(urls, mangaId) } } + // Download manga chapters + ACTION_DOWNLOAD_CHAPTER -> { + val notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1) + if (notificationId > -1) { + dismissNotification(context, notificationId, intent.getIntExtra(EXTRA_GROUP_ID, 0)) + } + val urls = intent.getStringArrayExtra(EXTRA_CHAPTER_URL) ?: return + val mangaId = intent.getLongExtra(EXTRA_MANGA_ID, -1) + if (mangaId > -1) { + downloadChapters(urls, mangaId) + } + } // Share crash dump file ACTION_SHARE_CRASH_LOG -> shareFile( @@ -235,6 +247,24 @@ class NotificationReceiver : BroadcastReceiver() { } } + /** + * Method called when user wants to download chapters + * + * @param chapterUrls URLs of chapter to download + * @param mangaId id of manga + */ + private fun downloadChapters(chapterUrls: Array, mangaId: Long) { + val db: DatabaseHelper = Injekt.get() + + launchIO { + val chapters = chapterUrls.mapNotNull { db.getChapter(it, mangaId).executeAsBlocking() } + val manga = db.getManga(mangaId).executeAsBlocking() + if (chapters.isNotEmpty() && manga != null) { + downloadManager.downloadChapters(manga, chapters) + } + } + } + companion object { private const val NAME = "NotificationReceiver" @@ -251,6 +281,7 @@ class NotificationReceiver : BroadcastReceiver() { private const val ACTION_MARK_AS_READ = "$ID.$NAME.MARK_AS_READ" 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_RESUME_DOWNLOADS = "$ID.$NAME.ACTION_RESUME_DOWNLOADS" private const val ACTION_PAUSE_DOWNLOADS = "$ID.$NAME.ACTION_PAUSE_DOWNLOADS" @@ -442,6 +473,28 @@ class NotificationReceiver : BroadcastReceiver() { return PendingIntent.getBroadcast(context, manga.id.hashCode(), newIntent, PendingIntent.FLAG_UPDATE_CURRENT) } + /** + * Returns [PendingIntent] that downloads chapters + * + * @param context context of application + * @param manga manga of chapter + */ + internal fun downloadChaptersPendingBroadcast( + context: Context, + manga: Manga, + chapters: Array, + groupId: Int + ): PendingIntent { + val newIntent = Intent(context, NotificationReceiver::class.java).apply { + action = ACTION_DOWNLOAD_CHAPTER + putExtra(EXTRA_CHAPTER_URL, chapters.map { it.url }.toTypedArray()) + putExtra(EXTRA_MANGA_ID, manga.id) + putExtra(EXTRA_NOTIFICATION_ID, manga.id.hashCode()) + putExtra(EXTRA_GROUP_ID, groupId) + } + return PendingIntent.getBroadcast(context, manga.id.hashCode(), newIntent, PendingIntent.FLAG_UPDATE_CURRENT) + } + /** * Returns [PendingIntent] that starts a service which stops the library update * diff --git a/app/src/main/res/drawable/ic_download_24dp.xml b/app/src/main/res/drawable/ic_download_24dp.xml new file mode 100644 index 000000000..9365670b9 --- /dev/null +++ b/app/src/main/res/drawable/ic_download_24dp.xml @@ -0,0 +1,9 @@ + + +