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