From 887c5fefaed1ffb5f815c665fd42681117b6da8f Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 6 Feb 2020 16:57:27 -0800 Subject: [PATCH] Fixed Downloader Notifaction + added placeholder download --- .../data/download/DownloadManager.kt | 6 +- .../data/download/DownloadNotifier.kt | 108 +++++++++--------- .../data/download/DownloadService.kt | 12 ++ .../tachiyomi/data/download/Downloader.kt | 32 +++--- .../kanade/tachiyomi/ui/main/MainActivity.kt | 2 +- 5 files changed, 86 insertions(+), 74 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index f4091918b0..5c89f19f5a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -77,6 +77,10 @@ class DownloadManager(val context: Context) { downloader.stop(reason) } + fun setPlaceholder() { + downloader.setPlaceholder() + } + /** * Tells the downloader to pause downloads. */ @@ -218,7 +222,7 @@ class DownloadManager(val context: Context) { if(!wasPaused && downloader.queue.isNotEmpty()){ downloader.start() } - else if (downloader.queue.isEmpty()) { + else if (downloader.queue.isEmpty() && DownloadService.isRunning(context)) { DownloadService.stop(context) } queue.remove(chapters) 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 d9f2f10e46..8a2ea643cd 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 @@ -33,27 +33,11 @@ internal class DownloadNotifier(private val context: Context) { */ private var isDownloading = false - /** - * The size of queue on start download. - */ - var initialQueueSize = 0 - set(value) { - if (value != 0) { - isSingleChapter = (value == 1) - } - field = value - } - /** * Updated when error is thrown */ var errorThrown = false - /** - * Updated when only single page is downloaded - */ - var isSingleChapter = false - /** * Updated when paused */ @@ -84,6 +68,48 @@ internal class DownloadNotifier(private val context: Context) { context.notificationManager.cancel(Notifications.ID_DOWNLOAD_CHAPTER) } + fun setPlaceholder(download: Download?) { + with(notification) { + // Check if first call. + if (!isDownloading) { + setSmallIcon(android.R.drawable.stat_sys_download) + setAutoCancel(false) + clearActions() + // Open download manager when clicked + setContentIntent(NotificationHandler.openDownloadManagerPendingActivity(context)) + isDownloading = true + // Pause action + addAction(R.drawable.ic_av_pause_grey_24dp_img, + context.getString(R.string.action_pause), + NotificationReceiver.pauseDownloadsPendingBroadcast(context)) + } + + if (download != null) { + val title = download.manga.currentTitle().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( + context.getString(R.string.chapter_downloading) + ) + } + else { + setContentTitle( + context.getString( + R.string.chapter_downloading + ) + ) + setContentText(null) + } + setProgress(0,0, true) + setStyle(null) + } + // Displays the progress bar on notification + notification.show() + + } + /** * Called when download progress changes. * @@ -133,13 +159,17 @@ internal class DownloadNotifier(private val context: Context) { // Open download manager when clicked setContentIntent(NotificationHandler.openDownloadManagerPendingActivity(context)) // Resume action - addAction(R.drawable.ic_av_play_arrow_grey_img, - context.getString(R.string.action_resume), - NotificationReceiver.resumeDownloadsPendingBroadcast(context)) + addAction( + R.drawable.ic_av_play_arrow_grey_img, + context.getString(R.string.action_resume), + NotificationReceiver.resumeDownloadsPendingBroadcast(context) + ) //Clear action - addAction(R.drawable.ic_clear_grey_24dp_img, - context.getString(R.string.action_cancel_all), - NotificationReceiver.clearDownloadsPendingBroadcast(context)) + addAction( + R.drawable.ic_clear_grey_24dp_img, + context.getString(R.string.action_cancel_all), + NotificationReceiver.clearDownloadsPendingBroadcast(context) + ) } // Show notification. @@ -147,40 +177,6 @@ internal class DownloadNotifier(private val context: Context) { // Reset initial values isDownloading = false - initialQueueSize = 0 - } - - /** - * Called when chapter is downloaded. - * - * @param download download object containing download information. - */ - fun onDownloadCompleted(download: Download, queue: DownloadQueue) { - // Check if last download - if (!queue.isEmpty()) { - return - } - // Create notification. - with(notification) { - val title = download.manga.currentTitle().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(context.getString(R.string.update_check_notification_download_complete)) - setSmallIcon(android.R.drawable.stat_sys_download_done) - setAutoCancel(true) - clearActions() - setContentIntent(NotificationReceiver.openChapterPendingActivity(context, download - .manga, download.chapter)) - setProgress(0, 0, false) - } - - // Show notification. - notification.show() - - // Reset initial values - isDownloading = false - initialQueueSize = 0 } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt index 04a53f88c0..0e287e0b5c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt @@ -18,6 +18,7 @@ import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.lang.plusAssign import eu.kanade.tachiyomi.util.system.connectivityManager +import eu.kanade.tachiyomi.util.system.isServiceRunning import eu.kanade.tachiyomi.util.system.powerManager import eu.kanade.tachiyomi.util.system.toast import rx.android.schedulers.AndroidSchedulers @@ -61,6 +62,16 @@ class DownloadService : Service() { fun stop(context: Context) { context.stopService(Intent(context, DownloadService::class.java)) } + + /** + * Returns the status of the service. + * + * @param context the application context. + * @return true if the service is running, false otherwise. + */ + fun isRunning(context: Context): Boolean { + return context.isServiceRunning(DownloadService::class.java) + } } /** @@ -91,6 +102,7 @@ class DownloadService : Service() { override fun onCreate() { super.onCreate() startForeground(Notifications.ID_DOWNLOAD_CHAPTER, getPlaceholderNotification()) + downloadManager.setPlaceholder() runningRelay.call(true) subscriptions = CompositeSubscription() listenDownloaderState() 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 2b3834184d..058ac5acb4 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 @@ -15,12 +15,12 @@ import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.source.online.fetchAllImageUrlsFromPageList import eu.kanade.tachiyomi.util.lang.RetryWithDelay -import eu.kanade.tachiyomi.util.system.launchNow -import eu.kanade.tachiyomi.util.system.launchUI import eu.kanade.tachiyomi.util.lang.plusAssign import eu.kanade.tachiyomi.util.storage.DiskUtil import eu.kanade.tachiyomi.util.storage.saveTo import eu.kanade.tachiyomi.util.system.ImageUtil +import eu.kanade.tachiyomi.util.system.launchNow +import eu.kanade.tachiyomi.util.system.launchUI import kotlinx.coroutines.async import okhttp3.Response import rx.Observable @@ -126,12 +126,14 @@ class Downloader( notifier.onWarning(reason) } else { if (notifier.paused) { - notifier.paused = false - if (queue.isEmpty()) notifier.dismiss() - else notifier.onDownloadPaused() - } else if (notifier.isSingleChapter && !notifier.errorThrown) { - notifier.isSingleChapter = false - } else { + if (queue.isEmpty()) { + notifier.dismiss() + } + else { + notifier.paused = false + notifier.onDownloadPaused() + } + }else { notifier.dismiss() } } @@ -185,7 +187,7 @@ class Downloader( } queue.remove(manga) if (queue.isEmpty()) { - DownloadService.stop(context) + if (DownloadService.isRunning(context)) DownloadService.stop(context) stop() } notifier.dismiss() @@ -253,9 +255,6 @@ class Downloader( if (chaptersToQueue.isNotEmpty()) { queue.addAll(chaptersToQueue) - // Initialize queue size. - notifier.initialQueueSize = queue.size - if (isRunning) { // Send the list of downloads to the downloader. downloadsRelay.call(chaptersToQueue) @@ -276,7 +275,7 @@ class Downloader( private fun downloadChapter(download: Download): Observable = Observable.defer { val chapterDirname = provider.getChapterDirName(download.chapter) val mangaDir = provider.getMangaDir(download.manga, download.source) - val tmpDir = mangaDir.createDirectory("${chapterDirname}_tmp") + val tmpDir = mangaDir.createDirectory(chapterDirname + TMP_DIR_SUFFIX) val pageListObservable = if (download.pages == null) { // Pull page list from network and add them to download object @@ -479,13 +478,14 @@ class Downloader( queue.remove(download) } if (areAllDownloadsFinished()) { - if (notifier.isSingleChapter && !notifier.errorThrown) { - notifier.onDownloadCompleted(download, queue) - } DownloadService.stop(context) } } + fun setPlaceholder() { + notifier.setPlaceholder(queue.firstOrNull()) + } + /** * Returns true if all the queued downloads are in DOWNLOADED or ERROR state. */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index db2e01047d..4ab90b1055 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -85,7 +85,7 @@ open class MainActivity : BaseActivity() { this.snackBar = snackBar canDismissSnackBar = false launchUI { - delay(1000) + delay(5000) canDismissSnackBar = true } extraViewForUndo = extraViewToCheck