Make restore a foreground service

This commit is contained in:
arkon 2020-04-22 23:09:24 -04:00
parent 2b27b40142
commit 6de00b1f21
4 changed files with 27 additions and 36 deletions

View file

@ -4,6 +4,7 @@ import android.app.Service
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.IBinder import android.os.IBinder
import android.os.PowerManager import android.os.PowerManager
import com.github.salomonbrys.kotson.fromJson 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.MangaImpl
import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.database.models.TrackImpl 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.data.track.TrackManager
import eu.kanade.tachiyomi.source.Source 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.lang.chop
import eu.kanade.tachiyomi.util.system.isServiceRunning import eu.kanade.tachiyomi.util.system.isServiceRunning
import eu.kanade.tachiyomi.util.system.sendLocalBroadcast import eu.kanade.tachiyomi.util.system.sendLocalBroadcast
@ -70,7 +73,11 @@ class BackupRestoreService : Service() {
val intent = Intent(context, BackupRestoreService::class.java).apply { val intent = Intent(context, BackupRestoreService::class.java).apply {
putExtra(BackupConst.EXTRA_URI, uri) putExtra(BackupConst.EXTRA_URI, uri)
} }
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
context.startService(intent) context.startService(intent)
} else {
context.startForegroundService(intent)
}
} }
} }
@ -115,20 +122,13 @@ class BackupRestoreService : Service() {
*/ */
private val errors = mutableListOf<Pair<Date, String>>() private val errors = mutableListOf<Pair<Date, String>>()
/**
* Backup manager
*/
private lateinit var backupManager: BackupManager private lateinit var backupManager: BackupManager
/**
* Database
*/
private val db: DatabaseHelper by injectLazy() private val db: DatabaseHelper by injectLazy()
/** private val trackManager: TrackManager by injectLazy()
* Tracking manager
*/ private lateinit var notifier: BackupNotifier
internal val trackManager: TrackManager by injectLazy()
private lateinit var executor: ExecutorService private lateinit var executor: ExecutorService
@ -137,10 +137,14 @@ class BackupRestoreService : Service() {
*/ */
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
notifier = BackupNotifier(this)
executor = Executors.newSingleThreadExecutor()
startForeground(Notifications.ID_RESTORE, notifier.showRestoreProgress().build())
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock( wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "BackupRestoreService:WakeLock") PowerManager.PARTIAL_WAKE_LOCK, "BackupRestoreService:WakeLock")
wakeLock.acquire() wakeLock.acquire()
executor = Executors.newSingleThreadExecutor()
} }
/** /**
@ -218,7 +222,7 @@ class BackupRestoreService : Service() {
json.get(CATEGORIES)?.let { json.get(CATEGORIES)?.let {
backupManager.restoreCategories(it.asJsonArray) backupManager.restoreCategories(it.asJsonArray)
restoreProgress += 1 restoreProgress += 1
showRestoreProgress(restoreProgress, restoreAmount, "Categories added", errors.size) showRestoreProgress(restoreProgress, restoreAmount, "Categories added")
} }
mangasJson mangasJson
@ -243,7 +247,7 @@ class BackupRestoreService : Service() {
errors.add(Date() to "${manga.title} - ${getString(R.string.source_not_found)}") errors.add(Date() to "${manga.title} - ${getString(R.string.source_not_found)}")
restoreProgress += 1 restoreProgress += 1
val content = getString(R.string.dialog_restoring_source_not_found, manga.title.chop(15)) 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) Observable.just(manga)
} }
} }
@ -363,7 +367,7 @@ class BackupRestoreService : Service() {
} }
.doOnCompleted { .doOnCompleted {
restoreProgress += 1 restoreProgress += 1
showRestoreProgress(restoreProgress, restoreAmount, manga.title, errors.size) showRestoreProgress(restoreProgress, restoreAmount, manga.title)
} }
} }
@ -395,7 +399,7 @@ class BackupRestoreService : Service() {
} }
.doOnCompleted { .doOnCompleted {
restoreProgress += 1 restoreProgress += 1
showRestoreProgress(restoreProgress, restoreAmount, backupManga.title, errors.size) showRestoreProgress(restoreProgress, restoreAmount, backupManga.title)
} }
} }
@ -461,16 +465,8 @@ class BackupRestoreService : Service() {
progress: Int, progress: Int,
amount: Int, amount: Int,
title: String, title: String,
errors: Int,
content: String = title.chop(30) content: String = title.chop(30)
) { ) {
val intent = Intent(BackupConst.INTENT_FILTER).apply { notifier.showRestoreProgress(content, progress, amount)
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)
} }
} }

View file

@ -76,7 +76,7 @@ object Notifications {
NotificationChannel(CHANNEL_UPDATES_TO_EXTS, context.getString(R.string.channel_ext_updates), NotificationChannel(CHANNEL_UPDATES_TO_EXTS, context.getString(R.string.channel_ext_updates),
NotificationManager.IMPORTANCE_DEFAULT), NotificationManager.IMPORTANCE_DEFAULT),
NotificationChannel(CHANNEL_BACKUP_RESTORE, context.getString(R.string.channel_backup_restore), NotificationChannel(CHANNEL_BACKUP_RESTORE, context.getString(R.string.channel_backup_restore),
NotificationManager.IMPORTANCE_LOW).apply { NotificationManager.IMPORTANCE_HIGH).apply {
setShowBadge(false) setShowBadge(false)
} }
) )

View file

@ -269,7 +269,6 @@ class SettingsBackupController : SettingsController() {
.positiveButton(R.string.action_restore) { .positiveButton(R.string.action_restore) {
val context = applicationContext val context = applicationContext
if (context != null) { if (context != null) {
(targetController as SettingsBackupController).notifier.showRestoreProgress()
BackupRestoreService.start(context, args.getParcelable(KEY_URI)!!) BackupRestoreService.start(context, args.getParcelable(KEY_URI)!!)
isRestoreStarted = true isRestoreStarted = true
} }
@ -296,12 +295,6 @@ class SettingsBackupController : SettingsController() {
notifier.showBackupError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE)) 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 -> { BackupConst.ACTION_RESTORE_COMPLETED -> {
isRestoreStarted = false isRestoreStarted = false

View file

@ -72,8 +72,8 @@ internal class BackupNotifier(private val context: Context) {
notificationBuilder.show(Notifications.ID_BACKUP) notificationBuilder.show(Notifications.ID_BACKUP)
} }
fun showRestoreProgress(content: String = "", progress: Int = 0, maxAmount: Int = 100) { fun showRestoreProgress(content: String = "", progress: Int = 0, maxAmount: Int = 100): NotificationCompat.Builder {
with(notificationBuilder) { val builder = with(notificationBuilder) {
setContentTitle(context.getString(R.string.restoring_backup)) setContentTitle(context.getString(R.string.restoring_backup))
setContentText(content) setContentText(content)
@ -89,7 +89,9 @@ internal class BackupNotifier(private val context: Context) {
NotificationReceiver.cancelRestorePendingBroadcast(context, Notifications.ID_RESTORE)) NotificationReceiver.cancelRestorePendingBroadcast(context, Notifications.ID_RESTORE))
} }
notificationBuilder.show(Notifications.ID_RESTORE) builder.show(Notifications.ID_RESTORE)
return builder
} }
fun showRestoreError(error: String?) { fun showRestoreError(error: String?) {