Fixed Downloader Notifaction + added placeholder download

This commit is contained in:
Jay 2020-02-06 16:57:27 -08:00
parent 0f228a6967
commit 887c5fefae
5 changed files with 86 additions and 74 deletions

View File

@ -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)

View File

@ -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,
addAction(
R.drawable.ic_av_play_arrow_grey_img,
context.getString(R.string.action_resume),
NotificationReceiver.resumeDownloadsPendingBroadcast(context))
NotificationReceiver.resumeDownloadsPendingBroadcast(context)
)
//Clear action
addAction(R.drawable.ic_clear_grey_24dp_img,
addAction(
R.drawable.ic_clear_grey_24dp_img,
context.getString(R.string.action_cancel_all),
NotificationReceiver.clearDownloadsPendingBroadcast(context))
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
}
/**

View File

@ -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()

View File

@ -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,11 +126,13 @@ class Downloader(
notifier.onWarning(reason)
} else {
if (notifier.paused) {
if (queue.isEmpty()) {
notifier.dismiss()
}
else {
notifier.paused = false
if (queue.isEmpty()) notifier.dismiss()
else notifier.onDownloadPaused()
} else if (notifier.isSingleChapter && !notifier.errorThrown) {
notifier.isSingleChapter = 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<Download> = 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.
*/

View File

@ -85,7 +85,7 @@ open class MainActivity : BaseActivity() {
this.snackBar = snackBar
canDismissSnackBar = false
launchUI {
delay(1000)
delay(5000)
canDismissSnackBar = true
}
extraViewForUndo = extraViewToCheck