From a0f7761a37d1fc8af79f32ed0a3d83e2a835ceab Mon Sep 17 00:00:00 2001 From: len Date: Sat, 7 May 2016 23:50:05 +0200 Subject: [PATCH] Minor changes --- .../data/download/DownloadManager.kt | 4 +- .../data/download/DownloadNotifier.kt | 60 +++++++------------ .../data/download/DownloadService.kt | 4 +- .../data/download/model/DownloadQueue.kt | 7 +-- .../data/library/LibraryUpdateService.kt | 21 +++---- .../data/updater/UpdateDownloader.kt | 31 +++++----- 6 files changed, 56 insertions(+), 71 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 dfc0d6458..7df77e351 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 ee37f7df4..8b90b0871 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 8407be3da..aa85b6c6a 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 27b9f1219..e55e66e75 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 995718cf0..bda286be4 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 517bb5bfc..7c23e496f 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) } }