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 dfc0d64583..7df77e3518 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 @@ -427,14 +427,14 @@ class DownloadManager(private val context: Context, private val sourceManager: S return !pending.isEmpty() } - fun stopDownloads(error: String = "") { + fun stopDownloads(errorMessage: String? = null) { destroySubscriptions() for (download in queue) { if (download.status == Download.DOWNLOADING) { download.status = Download.ERROR } } - downloadNotifier.onError(error) + errorMessage?.let { downloadNotifier.onError(it) } } fun clearQueue() { 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 ee37f7df49..8b90b0871d 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 @@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.util.notificationManager /** * DownloadNotifier is used to show notifications when downloading one or multiple chapters. + * * @param context context of application */ class DownloadNotifier(private val context: Context) { @@ -21,7 +22,8 @@ class DownloadNotifier(private val context: Context) { /** * Id of the notification. */ - private val notificationId = Constants.NOTIFICATION_DOWNLOAD_CHAPTER_ID + private val notificationId: Int + get() = Constants.NOTIFICATION_DOWNLOAD_CHAPTER_ID /** * Status of download. Used for correct notification icon. @@ -41,33 +43,31 @@ class DownloadNotifier(private val context: Context) { /** * Called when download progress changes. * Note: Only accepted when multi download active. + * * @param queue the queue containing downloads. */ internal fun onProgressChange(queue: DownloadQueue) { - // If single download mode return. - if (!multipleDownloadThreads) - return - // Update progress. - doOnProgressChange(null, queue) + if (multipleDownloadThreads) { + doOnProgressChange(null, queue) + } } /** * Called when download progress changes * Note: Only accepted when single download active + * * @param download download object containing download information * @param queue the queue containing downloads */ internal fun onProgressChange(download: Download, queue: DownloadQueue) { - // If multi download mode return. - if (multipleDownloadThreads) - return - // Update progress. - doOnProgressChange(download, queue) + if (!multipleDownloadThreads) { + doOnProgressChange(download, queue) + } } - /** * Show notification progress of chapter + * * @param download download object containing download information * @param queue the queue containing downloads */ @@ -86,8 +86,7 @@ class DownloadNotifier(private val context: Context) { } // Create notification - with (notificationBuilder) - { + with (notificationBuilder) { // Check if icon needs refresh if (!isDownloading) { setSmallIcon(android.R.drawable.stat_sys_download) @@ -95,10 +94,10 @@ class DownloadNotifier(private val context: Context) { } if (multipleDownloadThreads) { - setContentTitle(context.getString(R.string.app_name)) + setContentTitle(context.getString(R.string.app_name)) - setContentText(context.getString(R.string.chapter_downloading_progress) - .format(initialQueueSize - queue.size, initialQueueSize)) + setContentText(context.getString(R.string.chapter_downloading_progress) + .format(initialQueueSize - queue.size, initialQueueSize)) setProgress(initialQueueSize, initialQueueSize - queue.size, false) } else { download?.let { @@ -120,18 +119,13 @@ class DownloadNotifier(private val context: Context) { /** * Called when chapter is downloaded + * * @param download download object containing download information */ private fun onComplete(download: Download?) { - //Create notification. + // Create notification. with(notificationBuilder) { - // Set notification title - if (download != null) - setContentTitle(download.chapter?.name) - else - setContentTitle(context.getString(R.string.app_name)) - - // Set content information and progress. + setContentTitle(download?.chapter?.name ?: context.getString(R.string.app_name)) setContentText(context.getString(R.string.update_check_notification_download_complete)) setSmallIcon(android.R.drawable.stat_sys_download_done) setProgress(0, 0, false) @@ -154,23 +148,15 @@ class DownloadNotifier(private val context: Context) { /** * Called on error while downloading chapter + * * @param error string containing error information * @param chapter string containing chapter title */ - internal fun onError(error: String? = "", chapter: String = "") { + internal fun onError(error: String? = null, chapter: String? = null) { // Create notification with(notificationBuilder) { - if (chapter.isNullOrEmpty()) { - setContentTitle(context.getString(R.string.download_notifier_title_error)) - } else { - setContentTitle(chapter) - } - - if (error.isNullOrEmpty()) - setContentText(context.getString(R.string.download_notifier_unkown_error)) - else - setContentText(error) - + setContentTitle(chapter ?: context.getString(R.string.download_notifier_title_error)) + setContentText(error ?: context.getString(R.string.download_notifier_unkown_error)) setSmallIcon(android.R.drawable.stat_sys_warning) setProgress(0, 0, false) } 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 8407be3daf..aa85b6c6a3 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 @@ -82,12 +82,12 @@ class DownloadService : Service() { stopSelf() } } else if (isRunning) { - downloadManager.stopDownloads(baseContext.getString(R.string.download_notifier_text_only_wifi)) + downloadManager.stopDownloads(getString(R.string.download_notifier_text_only_wifi)) } } else -> { if (isRunning) { - downloadManager.stopDownloads(baseContext.getString(R.string.download_notifier_text_only_wifi)) + downloadManager.stopDownloads(getString(R.string.download_notifier_text_only_wifi)) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/model/DownloadQueue.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/model/DownloadQueue.kt index 27b9f12190..e55e66e75d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/model/DownloadQueue.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/model/DownloadQueue.kt @@ -22,12 +22,7 @@ class DownloadQueue : CopyOnWriteArrayList() { } fun del(chapter: Chapter) { - for (download in this) { - if (download.chapter.id == chapter.id) { - del(download) - break - } - } + find { it.chapter.id == chapter.id }?.let { del(it) } } fun getActiveDownloads() = diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index 995718cf0d..bda286be4a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -63,13 +63,14 @@ class LibraryUpdateService : Service() { */ private var subscription: Subscription? = null + /** + * Id of the library update notification. + */ + private val notificationId: Int + get() = Constants.NOTIFICATION_LIBRARY_ID + companion object { - /** - * Id of the library update notification. - */ - const val UPDATE_NOTIFICATION_ID = Constants.NOTIFICATION_LIBRARY_ID - /** * Key for manual library update. */ @@ -350,7 +351,7 @@ class LibraryUpdateService : Service() { * @param body the body of the notification. */ private fun showNotification(title: String, body: String) { - notificationManager.notify(UPDATE_NOTIFICATION_ID, notification() { + notificationManager.notify(notificationId, notification() { setSmallIcon(R.drawable.ic_refresh_white_24dp_img) setContentTitle(title) setContentText(body) @@ -365,7 +366,7 @@ class LibraryUpdateService : Service() { * @param total the total progress. */ private fun showProgressNotification(manga: Manga, current: Int, total: Int, cancelIntent: PendingIntent) { - notificationManager.notify(UPDATE_NOTIFICATION_ID, notification() { + notificationManager.notify(notificationId, notification() { setSmallIcon(R.drawable.ic_refresh_white_24dp_img) setContentTitle(manga.title) setProgress(total, current, false) @@ -385,7 +386,7 @@ class LibraryUpdateService : Service() { val title = getString(R.string.notification_update_completed) val body = getUpdatedMangasBody(updates, failed) - notificationManager.notify(UPDATE_NOTIFICATION_ID, notification() { + notificationManager.notify(notificationId, notification() { setSmallIcon(R.drawable.ic_refresh_white_24dp_img) setContentTitle(title) setStyle(NotificationCompat.BigTextStyle().bigText(body)) @@ -398,7 +399,7 @@ class LibraryUpdateService : Service() { * Cancels the notification. */ private fun cancelNotification() { - notificationManager.cancel(UPDATE_NOTIFICATION_ID) + notificationManager.cancel(notificationId) } /** @@ -457,7 +458,7 @@ class LibraryUpdateService : Service() { */ override fun onReceive(context: Context, intent: Intent) { LibraryUpdateService.stop(context) - context.notificationManager.cancel(UPDATE_NOTIFICATION_ID) + context.notificationManager.cancel(Constants.NOTIFICATION_LIBRARY_ID) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateDownloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateDownloader.kt index 517bb5bfcc..7c23e496ff 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateDownloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateDownloader.kt @@ -44,13 +44,19 @@ class UpdateDownloader(private val context: Context) : /** * Default download dir */ - val apkFile = File(context.externalCacheDir, "update.apk") + private val apkFile = File(context.externalCacheDir, "update.apk") /** * Notification builder */ - val notificationBuilder = NotificationCompat.Builder(context) + private val notificationBuilder = NotificationCompat.Builder(context) + + /** + * Id of the notification + */ + private val notificationId: Int + get() = Constants.NOTIFICATION_UPDATER_ID init { App.get(context).component.inject(this) @@ -117,7 +123,7 @@ class UpdateDownloader(private val context: Context) : values.getOrNull(0)?.let { notificationBuilder.setProgress(100, it, false) // Displays the progress bar on notification - context.notificationManager.notify(InstallOnReceived.notificationId, notificationBuilder.build()) + context.notificationManager.notify(notificationId, notificationBuilder.build()) } } @@ -146,7 +152,7 @@ class UpdateDownloader(private val context: Context) : } val notification = notificationBuilder.build() notification.flags = Notification.FLAG_NO_CLEAR - context.notificationManager.notify(InstallOnReceived.notificationId, notification) + context.notificationManager.notify(notificationId, notification) } /** @@ -170,29 +176,26 @@ class UpdateDownloader(private val context: Context) : class InstallOnReceived : BroadcastReceiver() { companion object { // Install apk action - val INSTALL_APK = "eu.kanade.INSTALL_APK" + const val INSTALL_APK = "eu.kanade.INSTALL_APK" // Retry download action - val RETRY_DOWNLOAD = "eu.kanade.RETRY_DOWNLOAD" + const val RETRY_DOWNLOAD = "eu.kanade.RETRY_DOWNLOAD" // Retry download action - val CANCEL_NOTIFICATION = "eu.kanade.CANCEL_NOTIFICATION" + const val CANCEL_NOTIFICATION = "eu.kanade.CANCEL_NOTIFICATION" // Absolute path of file || URL of file - val FILE_LOCATION = "file_location" - - // Id of the notification - val notificationId = Constants.NOTIFICATION_UPDATER_ID + const val FILE_LOCATION = "file_location" } override fun onReceive(context: Context, intent: Intent) { when (intent.action) { - // Install apk. + // Install apk. INSTALL_APK -> UpdateDownloader.installAPK(context, File(intent.getStringExtra(FILE_LOCATION))) - // Retry download. + // Retry download. RETRY_DOWNLOAD -> UpdateDownloader(context).execute(intent.getStringExtra(FILE_LOCATION)) - CANCEL_NOTIFICATION -> context.notificationManager.cancel(notificationId) + CANCEL_NOTIFICATION -> context.notificationManager.cancel(Constants.NOTIFICATION_UPDATER_ID) } }