From 6de00b1f21535e536c4c09faad1c6444b710a70d Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 22 Apr 2020 23:09:24 -0400 Subject: [PATCH] Make restore a foreground service --- .../data/backup/BackupRestoreService.kt | 46 +++++++++---------- .../data/notification/Notifications.kt | 2 +- .../ui/setting/SettingsBackupController.kt | 7 --- .../ui/setting/backup/BackupNotifier.kt | 8 ++-- 4 files changed, 27 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt index 3bd2998bbc..7fb4487fee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt @@ -4,6 +4,7 @@ import android.app.Service import android.content.Context import android.content.Intent import android.net.Uri +import android.os.Build import android.os.IBinder import android.os.PowerManager import com.github.salomonbrys.kotson.fromJson @@ -26,8 +27,10 @@ import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.MangaImpl import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.database.models.TrackImpl +import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.source.Source +import eu.kanade.tachiyomi.ui.setting.backup.BackupNotifier import eu.kanade.tachiyomi.util.lang.chop import eu.kanade.tachiyomi.util.system.isServiceRunning import eu.kanade.tachiyomi.util.system.sendLocalBroadcast @@ -70,7 +73,11 @@ class BackupRestoreService : Service() { val intent = Intent(context, BackupRestoreService::class.java).apply { putExtra(BackupConst.EXTRA_URI, uri) } - context.startService(intent) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + context.startService(intent) + } else { + context.startForegroundService(intent) + } } } @@ -115,20 +122,13 @@ class BackupRestoreService : Service() { */ private val errors = mutableListOf>() - /** - * Backup manager - */ private lateinit var backupManager: BackupManager - /** - * Database - */ private val db: DatabaseHelper by injectLazy() - /** - * Tracking manager - */ - internal val trackManager: TrackManager by injectLazy() + private val trackManager: TrackManager by injectLazy() + + private lateinit var notifier: BackupNotifier private lateinit var executor: ExecutorService @@ -137,10 +137,14 @@ class BackupRestoreService : Service() { */ override fun onCreate() { super.onCreate() + notifier = BackupNotifier(this) + executor = Executors.newSingleThreadExecutor() + + startForeground(Notifications.ID_RESTORE, notifier.showRestoreProgress().build()) + wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock( PowerManager.PARTIAL_WAKE_LOCK, "BackupRestoreService:WakeLock") wakeLock.acquire() - executor = Executors.newSingleThreadExecutor() } /** @@ -218,7 +222,7 @@ class BackupRestoreService : Service() { json.get(CATEGORIES)?.let { backupManager.restoreCategories(it.asJsonArray) restoreProgress += 1 - showRestoreProgress(restoreProgress, restoreAmount, "Categories added", errors.size) + showRestoreProgress(restoreProgress, restoreAmount, "Categories added") } mangasJson @@ -243,7 +247,7 @@ class BackupRestoreService : Service() { errors.add(Date() to "${manga.title} - ${getString(R.string.source_not_found)}") restoreProgress += 1 val content = getString(R.string.dialog_restoring_source_not_found, manga.title.chop(15)) - showRestoreProgress(restoreProgress, restoreAmount, manga.title, errors.size, content) + showRestoreProgress(restoreProgress, restoreAmount, manga.title, content) Observable.just(manga) } } @@ -363,7 +367,7 @@ class BackupRestoreService : Service() { } .doOnCompleted { restoreProgress += 1 - showRestoreProgress(restoreProgress, restoreAmount, manga.title, errors.size) + showRestoreProgress(restoreProgress, restoreAmount, manga.title) } } @@ -395,7 +399,7 @@ class BackupRestoreService : Service() { } .doOnCompleted { restoreProgress += 1 - showRestoreProgress(restoreProgress, restoreAmount, backupManga.title, errors.size) + showRestoreProgress(restoreProgress, restoreAmount, backupManga.title) } } @@ -461,16 +465,8 @@ class BackupRestoreService : Service() { progress: Int, amount: Int, title: String, - errors: Int, content: String = title.chop(30) ) { - val intent = Intent(BackupConst.INTENT_FILTER).apply { - putExtra(BackupConst.EXTRA_PROGRESS, progress) - putExtra(BackupConst.EXTRA_AMOUNT, amount) - putExtra(BackupConst.EXTRA_CONTENT, content) - putExtra(BackupConst.EXTRA_ERRORS, errors) - putExtra(BackupConst.ACTION, BackupConst.ACTION_RESTORE_PROGRESS) - } - sendLocalBroadcast(intent) + notifier.showRestoreProgress(content, progress, amount) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/notification/Notifications.kt b/app/src/main/java/eu/kanade/tachiyomi/data/notification/Notifications.kt index 9bf6cf1f3f..fa45ac4bcb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/notification/Notifications.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/notification/Notifications.kt @@ -76,7 +76,7 @@ object Notifications { NotificationChannel(CHANNEL_UPDATES_TO_EXTS, context.getString(R.string.channel_ext_updates), NotificationManager.IMPORTANCE_DEFAULT), NotificationChannel(CHANNEL_BACKUP_RESTORE, context.getString(R.string.channel_backup_restore), - NotificationManager.IMPORTANCE_LOW).apply { + NotificationManager.IMPORTANCE_HIGH).apply { setShowBadge(false) } ) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt index c74b2139a6..93e4ff0f62 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt @@ -269,7 +269,6 @@ class SettingsBackupController : SettingsController() { .positiveButton(R.string.action_restore) { val context = applicationContext if (context != null) { - (targetController as SettingsBackupController).notifier.showRestoreProgress() BackupRestoreService.start(context, args.getParcelable(KEY_URI)!!) isRestoreStarted = true } @@ -296,12 +295,6 @@ class SettingsBackupController : SettingsController() { notifier.showBackupError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE)) } - BackupConst.ACTION_RESTORE_PROGRESS -> { - val progress = intent.getIntExtra(BackupConst.EXTRA_PROGRESS, 0) - val amount = intent.getIntExtra(BackupConst.EXTRA_AMOUNT, 0) - val content = intent.getStringExtra(BackupConst.EXTRA_CONTENT) - notifier.showRestoreProgress(content, progress, amount) - } BackupConst.ACTION_RESTORE_COMPLETED -> { isRestoreStarted = false diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/backup/BackupNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/backup/BackupNotifier.kt index acb4daef9f..449ca2561f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/backup/BackupNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/backup/BackupNotifier.kt @@ -72,8 +72,8 @@ internal class BackupNotifier(private val context: Context) { notificationBuilder.show(Notifications.ID_BACKUP) } - fun showRestoreProgress(content: String = "", progress: Int = 0, maxAmount: Int = 100) { - with(notificationBuilder) { + fun showRestoreProgress(content: String = "", progress: Int = 0, maxAmount: Int = 100): NotificationCompat.Builder { + val builder = with(notificationBuilder) { setContentTitle(context.getString(R.string.restoring_backup)) setContentText(content) @@ -89,7 +89,9 @@ internal class BackupNotifier(private val context: Context) { NotificationReceiver.cancelRestorePendingBroadcast(context, Notifications.ID_RESTORE)) } - notificationBuilder.show(Notifications.ID_RESTORE) + builder.show(Notifications.ID_RESTORE) + + return builder } fun showRestoreError(error: String?) {