From 32db1e3045a3c6c787668fdca8a9b480497e0864 Mon Sep 17 00:00:00 2001 From: inorichi Date: Sat, 28 Apr 2018 10:54:27 +0200 Subject: [PATCH] Run downloader in foreground service --- app/.gitignore | 3 ++- .../tachiyomi/data/download/DownloadService.kt | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/.gitignore b/app/.gitignore index 90de2b9c81..4f8f315a17 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -1,4 +1,5 @@ /build *iml *.iml -custom.gradle \ No newline at end of file +custom.gradle +google-services.json 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 ae2cf2111d..8fdb4f6267 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 @@ -1,16 +1,20 @@ package eu.kanade.tachiyomi.data.download +import android.app.Notification import android.app.Service import android.content.Context import android.content.Intent import android.net.NetworkInfo.State.CONNECTED import android.net.NetworkInfo.State.DISCONNECTED +import android.os.Build import android.os.IBinder import android.os.PowerManager +import android.support.v4.app.NotificationCompat import com.github.pwittchen.reactivenetwork.library.Connectivity import com.github.pwittchen.reactivenetwork.library.ReactiveNetwork import com.jakewharton.rxrelay.BehaviorRelay import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.connectivityManager import eu.kanade.tachiyomi.util.plusAssign @@ -41,7 +45,12 @@ class DownloadService : Service() { * @param context the application context. */ fun start(context: Context) { - context.startService(Intent(context, DownloadService::class.java)) + val intent = Intent(context, DownloadService::class.java) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + context.startService(intent) + } else { + context.startForegroundService(intent) + } } /** @@ -81,6 +90,7 @@ class DownloadService : Service() { */ override fun onCreate() { super.onCreate() + startForeground(Notifications.ID_DOWNLOAD_CHAPTER, getPlaceholderNotification()) runningRelay.call(true) subscriptions = CompositeSubscription() listenDownloaderState() @@ -176,4 +186,10 @@ class DownloadService : Service() { if (!isHeld) acquire() } + private fun getPlaceholderNotification(): Notification { + return NotificationCompat.Builder(this, Notifications.CHANNEL_DOWNLOADER) + .setContentTitle(getString(R.string.download_notifier_downloader_title)) + .build() + } + }