Move some restore notification logic into service

This commit is contained in:
arkon 2020-04-25 18:59:12 -04:00
parent 96c55db7ca
commit 9d22a9e664
5 changed files with 25 additions and 50 deletions

View file

@ -7,17 +7,7 @@ object BackupConst {
const val INTENT_FILTER = "SettingsBackupFragment"
const val ACTION_BACKUP_COMPLETED = "$ID.$INTENT_FILTER.ACTION_BACKUP_COMPLETED"
const val ACTION_BACKUP_ERROR = "$ID.$INTENT_FILTER.ACTION_BACKUP_ERROR"
const val ACTION_RESTORE_PROGRESS = "$ID.$INTENT_FILTER.ACTION_RESTORE_PROGRESS"
const val ACTION_RESTORE_COMPLETED = "$ID.$INTENT_FILTER.ACTION_RESTORE_COMPLETED"
const val ACTION_RESTORE_ERROR = "$ID.$INTENT_FILTER.ACTION_RESTORE_ERROR"
const val ACTION = "$ID.$INTENT_FILTER.ACTION"
const val EXTRA_PROGRESS = "$ID.$INTENT_FILTER.EXTRA_PROGRESS"
const val EXTRA_AMOUNT = "$ID.$INTENT_FILTER.EXTRA_AMOUNT"
const val EXTRA_ERRORS = "$ID.$INTENT_FILTER.EXTRA_ERRORS"
const val EXTRA_CONTENT = "$ID.$INTENT_FILTER.EXTRA_CONTENT"
const val EXTRA_ERROR_MESSAGE = "$ID.$INTENT_FILTER.EXTRA_ERROR_MESSAGE"
const val EXTRA_URI = "$ID.$INTENT_FILTER.EXTRA_URI"
const val EXTRA_TIME = "$ID.$INTENT_FILTER.EXTRA_TIME"
const val EXTRA_ERROR_FILE_PATH = "$ID.$INTENT_FILTER.EXTRA_ERROR_FILE_PATH"
const val EXTRA_ERROR_FILE = "$ID.$INTENT_FILTER.EXTRA_ERROR_FILE"
}

View file

@ -34,7 +34,6 @@ 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.system.isServiceRunning
import eu.kanade.tachiyomi.util.system.sendLocalBroadcast
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
@ -90,11 +89,7 @@ class BackupRestoreService : Service() {
fun stop(context: Context) {
context.stopService(Intent(context, BackupRestoreService::class.java))
val errorIntent = Intent(BackupConst.INTENT_FILTER).apply {
putExtra(BackupConst.ACTION, BackupConst.ACTION_RESTORE_ERROR)
putExtra(BackupConst.EXTRA_ERROR_MESSAGE, context.getString(R.string.restoring_backup_canceled))
}
context.sendLocalBroadcast(errorIntent)
BackupNotifier(context).showRestoreError(context.getString(R.string.restoring_backup_canceled))
}
}
@ -135,7 +130,7 @@ class BackupRestoreService : Service() {
super.onCreate()
notifier = BackupNotifier(this)
startForeground(Notifications.ID_RESTORE, notifier.showRestoreProgress().build())
startForeground(Notifications.ID_RESTORE_PROGRESS, notifier.showRestoreProgress().build())
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "BackupRestoreService:WakeLock"
@ -182,11 +177,7 @@ class BackupRestoreService : Service() {
Timber.e(exception)
writeErrorLog()
val errorIntent = Intent(BackupConst.INTENT_FILTER).apply {
putExtra(BackupConst.ACTION, BackupConst.ACTION_RESTORE_ERROR)
putExtra(BackupConst.EXTRA_ERROR_MESSAGE, exception.message)
}
sendLocalBroadcast(errorIntent)
notifier.showRestoreError(exception.message)
stopSelf(startId)
}
@ -235,14 +226,8 @@ class BackupRestoreService : Service() {
val time = endTime - startTime
val logFile = writeErrorLog()
val completeIntent = Intent(BackupConst.INTENT_FILTER).apply {
putExtra(BackupConst.EXTRA_TIME, time)
putExtra(BackupConst.EXTRA_ERRORS, errors.size)
putExtra(BackupConst.EXTRA_ERROR_FILE_PATH, logFile.parent)
putExtra(BackupConst.EXTRA_ERROR_FILE, logFile.name)
putExtra(BackupConst.ACTION, BackupConst.ACTION_RESTORE_COMPLETED)
}
sendLocalBroadcast(completeIntent)
notifier.showRestoreComplete(time, errors.size, logFile.parent, logFile.name)
}
private fun restoreCategories(categoriesJson: JsonElement) {

View file

@ -49,8 +49,10 @@ object Notifications {
* Notification channel and ids used by the backup/restore system.
*/
const val CHANNEL_BACKUP_RESTORE = "backup_restore_channel"
const val ID_BACKUP = -501
const val ID_RESTORE = -502
const val ID_BACKUP_PROGRESS = -501
const val ID_BACKUP_COMPLETE = -502
const val ID_RESTORE_PROGRESS = -503
const val ID_RESTORE_COMPLETE = -504
/**
* Creates the notification channels introduced in Android Oreo.

View file

@ -301,16 +301,6 @@ class SettingsBackupController : SettingsController() {
notifier.showBackupError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE))
}
BackupConst.ACTION_RESTORE_COMPLETED -> {
val time = intent.getLongExtra(BackupConst.EXTRA_TIME, 0)
val errorCount = intent.getIntExtra(BackupConst.EXTRA_ERRORS, 0)
val path = intent.getStringExtra(BackupConst.EXTRA_ERROR_FILE_PATH)
val file = intent.getStringExtra(BackupConst.EXTRA_ERROR_FILE)
notifier.showRestoreComplete(time, errorCount, path, file)
}
BackupConst.ACTION_RESTORE_ERROR -> {
notifier.showRestoreError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE))
}
}
}
}

View file

@ -34,10 +34,12 @@ internal class BackupNotifier(private val context: Context) {
setOngoing(true)
}
notificationBuilder.show(Notifications.ID_BACKUP)
notificationBuilder.show(Notifications.ID_BACKUP_PROGRESS)
}
fun showBackupError(error: String?) {
context.notificationManager.cancel(Notifications.ID_BACKUP_PROGRESS)
with(notificationBuilder) {
setContentTitle(context.getString(R.string.creating_backup_error))
setContentText(error)
@ -47,10 +49,12 @@ internal class BackupNotifier(private val context: Context) {
setOngoing(false)
}
notificationBuilder.show(Notifications.ID_BACKUP)
notificationBuilder.show(Notifications.ID_BACKUP_COMPLETE)
}
fun showBackupComplete(unifile: UniFile) {
context.notificationManager.cancel(Notifications.ID_BACKUP_PROGRESS)
with(notificationBuilder) {
setContentTitle(context.getString(R.string.backup_created))
@ -70,11 +74,11 @@ internal class BackupNotifier(private val context: Context) {
addAction(
R.drawable.ic_share_24dp,
context.getString(R.string.action_share),
NotificationReceiver.shareBackupPendingBroadcast(context, unifile.uri, Notifications.ID_BACKUP)
NotificationReceiver.shareBackupPendingBroadcast(context, unifile.uri, Notifications.ID_BACKUP_COMPLETE)
)
}
notificationBuilder.show(Notifications.ID_BACKUP)
notificationBuilder.show(Notifications.ID_BACKUP_COMPLETE)
}
fun showRestoreProgress(content: String = "", progress: Int = 0, maxAmount: Int = 100): NotificationCompat.Builder {
@ -93,16 +97,18 @@ internal class BackupNotifier(private val context: Context) {
addAction(
R.drawable.ic_close_24dp,
context.getString(R.string.action_stop),
NotificationReceiver.cancelRestorePendingBroadcast(context, Notifications.ID_RESTORE)
NotificationReceiver.cancelRestorePendingBroadcast(context, Notifications.ID_RESTORE_PROGRESS)
)
}
builder.show(Notifications.ID_RESTORE)
builder.show(Notifications.ID_RESTORE_PROGRESS)
return builder
}
fun showRestoreError(error: String?) {
context.notificationManager.cancel(Notifications.ID_RESTORE_PROGRESS)
with(notificationBuilder) {
setContentTitle(context.getString(R.string.restoring_backup_error))
setContentText(error)
@ -112,10 +118,12 @@ internal class BackupNotifier(private val context: Context) {
setOngoing(false)
}
notificationBuilder.show(Notifications.ID_RESTORE)
notificationBuilder.show(Notifications.ID_RESTORE_COMPLETE)
}
fun showRestoreComplete(time: Long, errorCount: Int, path: String?, file: String?) {
context.notificationManager.cancel(Notifications.ID_RESTORE_PROGRESS)
val timeString = context.getString(
R.string.restore_duration,
TimeUnit.MILLISECONDS.toMinutes(time),
@ -152,6 +160,6 @@ internal class BackupNotifier(private val context: Context) {
}
}
notificationBuilder.show(Notifications.ID_RESTORE)
notificationBuilder.show(Notifications.ID_RESTORE_COMPLETE)
}
}