Move some restore notification logic into service

pull/2961/head
arkon 4 years ago
parent 96c55db7ca
commit 9d22a9e664

@ -7,17 +7,7 @@ object BackupConst {
const val INTENT_FILTER = "SettingsBackupFragment" const val INTENT_FILTER = "SettingsBackupFragment"
const val ACTION_BACKUP_COMPLETED = "$ID.$INTENT_FILTER.ACTION_BACKUP_COMPLETED" 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_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 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_ERROR_MESSAGE = "$ID.$INTENT_FILTER.EXTRA_ERROR_MESSAGE"
const val EXTRA_URI = "$ID.$INTENT_FILTER.EXTRA_URI" 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"
} }

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

@ -49,8 +49,10 @@ object Notifications {
* Notification channel and ids used by the backup/restore system. * Notification channel and ids used by the backup/restore system.
*/ */
const val CHANNEL_BACKUP_RESTORE = "backup_restore_channel" const val CHANNEL_BACKUP_RESTORE = "backup_restore_channel"
const val ID_BACKUP = -501 const val ID_BACKUP_PROGRESS = -501
const val ID_RESTORE = -502 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. * Creates the notification channels introduced in Android Oreo.

@ -301,16 +301,6 @@ class SettingsBackupController : SettingsController() {
notifier.showBackupError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE)) 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))
}
} }
} }
} }

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

Loading…
Cancel
Save