Move restore progress dialog to notification

This commit is contained in:
arkon 2020-04-21 21:53:15 -04:00
parent fc2c465bac
commit f69f78db34
6 changed files with 68 additions and 69 deletions

View file

@ -58,20 +58,10 @@ import uy.kohesive.injekt.injectLazy
class BackupManager(val context: Context, version: Int = CURRENT_VERSION) { class BackupManager(val context: Context, version: Int = CURRENT_VERSION) {
/**
* Database.
*/
internal val databaseHelper: DatabaseHelper by injectLazy() internal val databaseHelper: DatabaseHelper by injectLazy()
/**
* Source manager.
*/
internal val sourceManager: SourceManager by injectLazy() internal val sourceManager: SourceManager by injectLazy()
/**
* Tracking manager
*/
internal val trackManager: TrackManager by injectLazy() internal val trackManager: TrackManager by injectLazy()
private val preferences: PreferencesHelper by injectLazy()
/** /**
* Version of parser * Version of parser
@ -84,11 +74,6 @@ class BackupManager(val context: Context, version: Int = CURRENT_VERSION) {
*/ */
var parser: Gson = initParser() var parser: Gson = initParser()
/**
* Preferences
*/
private val preferences: PreferencesHelper by injectLazy()
/** /**
* Set version of parser * Set version of parser
* *

View file

@ -81,6 +81,12 @@ 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 {
putExtra(BackupConst.ACTION, BackupConst.ACTION_RESTORE_ERROR)
putExtra(BackupConst.EXTRA_ERROR_MESSAGE, context.getString(R.string.restoring_backup_canceled))
}
context.sendLocalBroadcast(errorIntent)
} }
} }
@ -456,7 +462,7 @@ class BackupRestoreService : Service() {
amount: Int, amount: Int,
title: String, title: String,
errors: Int, errors: Int,
content: String = getString(R.string.dialog_restoring_backup, title.chop(15)) content: String = title.chop(30)
) { ) {
val intent = Intent(BackupConst.INTENT_FILTER).apply { val intent = Intent(BackupConst.INTENT_FILTER).apply {
putExtra(BackupConst.EXTRA_PROGRESS, progress) putExtra(BackupConst.EXTRA_PROGRESS, progress)

View file

@ -9,6 +9,7 @@ import android.os.Build
import android.os.Handler import android.os.Handler
import eu.kanade.tachiyomi.BuildConfig.APPLICATION_ID as ID import eu.kanade.tachiyomi.BuildConfig.APPLICATION_ID as ID
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.backup.BackupRestoreService
import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
@ -61,6 +62,8 @@ class NotificationReceiver : BroadcastReceiver() {
// Share backup file // Share backup file
ACTION_SHARE_BACKUP -> shareBackup(context, intent.getParcelableExtra(EXTRA_URI), ACTION_SHARE_BACKUP -> shareBackup(context, intent.getParcelableExtra(EXTRA_URI),
intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1)) intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1))
ACTION_CANCEL_RESTORE -> cancelRestore(context,
intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1))
// Cancel library update and dismiss notification // Cancel library update and dismiss notification
ACTION_CANCEL_LIBRARY_UPDATE -> cancelLibraryUpdate(context, Notifications.ID_LIBRARY_PROGRESS) ACTION_CANCEL_LIBRARY_UPDATE -> cancelLibraryUpdate(context, Notifications.ID_LIBRARY_PROGRESS)
// Open reader activity // Open reader activity
@ -170,6 +173,17 @@ class NotificationReceiver : BroadcastReceiver() {
DiskUtil.scanMedia(context, file) DiskUtil.scanMedia(context, file)
} }
/**
* Method called when user wants to stop a backup restore job.
*
* @param context context of application
* @param notificationId id of notification
*/
private fun cancelRestore(context: Context, notificationId: Int) {
BackupRestoreService.stop(context)
Handler().post { dismissNotification(context, notificationId) }
}
/** /**
* Method called when user wants to stop a library update * Method called when user wants to stop a library update
* *
@ -222,6 +236,9 @@ class NotificationReceiver : BroadcastReceiver() {
// Called to launch send intent. // Called to launch send intent.
private const val ACTION_SHARE_BACKUP = "$ID.$NAME.SEND_BACKUP" private const val ACTION_SHARE_BACKUP = "$ID.$NAME.SEND_BACKUP"
// Called to cancel backup restore job.
private const val ACTION_CANCEL_RESTORE = "$ID.$NAME.CANCEL_RESTORE"
// Called to cancel library update. // Called to cancel library update.
private const val ACTION_CANCEL_LIBRARY_UPDATE = "$ID.$NAME.CANCEL_LIBRARY_UPDATE" private const val ACTION_CANCEL_LIBRARY_UPDATE = "$ID.$NAME.CANCEL_LIBRARY_UPDATE"
@ -475,7 +492,7 @@ class NotificationReceiver : BroadcastReceiver() {
* @param notificationId id of notification * @param notificationId id of notification
* @return [PendingIntent] * @return [PendingIntent]
*/ */
internal fun shareBackup(context: Context, uri: Uri, notificationId: Int): PendingIntent { internal fun shareBackupPendingBroadcast(context: Context, uri: Uri, notificationId: Int): PendingIntent {
val intent = Intent(context, NotificationReceiver::class.java).apply { val intent = Intent(context, NotificationReceiver::class.java).apply {
action = ACTION_SHARE_BACKUP action = ACTION_SHARE_BACKUP
putExtra(EXTRA_URI, uri) putExtra(EXTRA_URI, uri)
@ -491,7 +508,7 @@ class NotificationReceiver : BroadcastReceiver() {
* @param uri uri of error log file * @param uri uri of error log file
* @return [PendingIntent] * @return [PendingIntent]
*/ */
internal fun openErrorLog(context: Context, uri: Uri): PendingIntent { internal fun openErrorLogPendingActivity(context: Context, uri: Uri): PendingIntent {
val intent = Intent().apply { val intent = Intent().apply {
action = Intent.ACTION_VIEW action = Intent.ACTION_VIEW
setDataAndType(uri, "text/plain") setDataAndType(uri, "text/plain")
@ -499,5 +516,20 @@ class NotificationReceiver : BroadcastReceiver() {
} }
return PendingIntent.getActivity(context, 0, intent, 0) return PendingIntent.getActivity(context, 0, intent, 0)
} }
/**
* Returns [PendingIntent] that cancels a backup restore job.
*
* @param context context of application
* @param notificationId id of notification
* @return [PendingIntent]
*/
internal fun cancelRestorePendingBroadcast(context: Context, notificationId: Int): PendingIntent {
val intent = Intent(context, NotificationReceiver::class.java).apply {
action = ACTION_CANCEL_RESTORE
putExtra(EXTRA_NOTIFICATION_ID, notificationId)
}
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
} }
} }

View file

@ -23,7 +23,6 @@ import eu.kanade.tachiyomi.data.backup.models.Backup
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.ui.base.controller.popControllerWithTag
import eu.kanade.tachiyomi.ui.base.controller.requestPermissionsSafe import eu.kanade.tachiyomi.ui.base.controller.requestPermissionsSafe
import eu.kanade.tachiyomi.ui.setting.backup.BackupNotifier import eu.kanade.tachiyomi.ui.setting.backup.BackupNotifier
import eu.kanade.tachiyomi.util.preference.defaultValue import eu.kanade.tachiyomi.util.preference.defaultValue
@ -197,7 +196,9 @@ class SettingsBackupController : SettingsController() {
CODE_BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) { CODE_BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) {
val uri = data.data val uri = data.data
if (uri != null) { if (uri != null) {
RestoreBackupDialog(uri).showDialog(router) val ctrl = RestoreBackupDialog(uri)
ctrl.targetController = this@SettingsBackupController
ctrl.showDialog(router)
} }
} }
} }
@ -268,9 +269,8 @@ class SettingsBackupController : SettingsController() {
.onPositive { _, _ -> .onPositive { _, _ ->
val context = applicationContext val context = applicationContext
if (context != null) { if (context != null) {
RestoringBackupDialog().showDialog(router, TAG_RESTORING_BACKUP_DIALOG) (targetController as SettingsBackupController).notifier.showRestoreProgress()
BackupRestoreService.start(context, args.getParcelable(KEY_URI)!!) BackupRestoreService.start(context, args.getParcelable(KEY_URI)!!)
isRestoreStarted = true isRestoreStarted = true
} }
} }
@ -282,41 +282,6 @@ class SettingsBackupController : SettingsController() {
} }
} }
class RestoringBackupDialog : DialogController() {
private var materialDialog: MaterialDialog? = null
override fun onCreateDialog(savedViewState: Bundle?): Dialog {
return MaterialDialog.Builder(activity!!)
.title(R.string.backup)
.content(R.string.restoring_backup)
.progress(false, 100, true)
.cancelable(false)
.negativeText(R.string.action_stop)
.onNegative { _, _ ->
applicationContext?.let { BackupRestoreService.stop(it) }
}
.build()
.also { materialDialog = it }
}
override fun onDestroyView(view: View) {
super.onDestroyView(view)
materialDialog = null
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
router.popController(this)
}
fun updateProgress(content: String?, progress: Int, amount: Int) {
val dialog = materialDialog ?: return
dialog.setContent(content)
dialog.setProgress(progress)
dialog.maxProgress = amount
}
}
inner class BackupBroadcastReceiver : BroadcastReceiver() { inner class BackupBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
when (intent.getStringExtra(BackupConst.ACTION)) { when (intent.getStringExtra(BackupConst.ACTION)) {
@ -336,13 +301,11 @@ class SettingsBackupController : SettingsController() {
val progress = intent.getIntExtra(BackupConst.EXTRA_PROGRESS, 0) val progress = intent.getIntExtra(BackupConst.EXTRA_PROGRESS, 0)
val amount = intent.getIntExtra(BackupConst.EXTRA_AMOUNT, 0) val amount = intent.getIntExtra(BackupConst.EXTRA_AMOUNT, 0)
val content = intent.getStringExtra(BackupConst.EXTRA_CONTENT) val content = intent.getStringExtra(BackupConst.EXTRA_CONTENT)
(router.getControllerWithTag(TAG_RESTORING_BACKUP_DIALOG) notifier.showRestoreProgress(content, progress, amount)
as? RestoringBackupDialog)?.updateProgress(content, progress, amount)
} }
BackupConst.ACTION_RESTORE_COMPLETED -> { BackupConst.ACTION_RESTORE_COMPLETED -> {
isRestoreStarted = false isRestoreStarted = false
router.popControllerWithTag(TAG_RESTORING_BACKUP_DIALOG)
val time = intent.getLongExtra(BackupConst.EXTRA_TIME, 0) val time = intent.getLongExtra(BackupConst.EXTRA_TIME, 0)
val errorCount = intent.getIntExtra(BackupConst.EXTRA_ERRORS, 0) val errorCount = intent.getIntExtra(BackupConst.EXTRA_ERRORS, 0)
val path = intent.getStringExtra(BackupConst.EXTRA_ERROR_FILE_PATH) val path = intent.getStringExtra(BackupConst.EXTRA_ERROR_FILE_PATH)
@ -352,7 +315,6 @@ class SettingsBackupController : SettingsController() {
BackupConst.ACTION_RESTORE_ERROR -> { BackupConst.ACTION_RESTORE_ERROR -> {
isRestoreStarted = false isRestoreStarted = false
router.popControllerWithTag(TAG_RESTORING_BACKUP_DIALOG)
notifier.showRestoreError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE)) notifier.showRestoreError(intent.getStringExtra(BackupConst.EXTRA_ERROR_MESSAGE))
} }
} }
@ -364,8 +326,6 @@ class SettingsBackupController : SettingsController() {
const val CODE_BACKUP_RESTORE = 502 const val CODE_BACKUP_RESTORE = 502
const val CODE_BACKUP_DIR = 503 const val CODE_BACKUP_DIR = 503
const val TAG_RESTORING_BACKUP_DIALOG = "RestoringBackupDialog"
var isBackupStarted = false var isBackupStarted = false
var isRestoreStarted = false var isRestoreStarted = false
} }

View file

@ -66,14 +66,30 @@ internal class BackupNotifier(private val context: Context) {
addAction(R.drawable.ic_share_24dp, addAction(R.drawable.ic_share_24dp,
context.getString(R.string.action_share), context.getString(R.string.action_share),
NotificationReceiver.shareBackup(context, unifile.uri, Notifications.ID_BACKUP)) NotificationReceiver.shareBackupPendingBroadcast(context, unifile.uri, Notifications.ID_BACKUP))
} }
notificationBuilder.show(Notifications.ID_BACKUP) notificationBuilder.show(Notifications.ID_BACKUP)
} }
fun showRestoreProgress() { fun showRestoreProgress(content: String = "", progress: Int = 0, maxAmount: Int = 100) {
// TODO with(notificationBuilder) {
setContentTitle(context.getString(R.string.restoring_backup))
setContentText(content)
setProgress(maxAmount, progress, false)
// Clear old actions if they exist
if (mActions.isNotEmpty()) {
mActions.clear()
}
addAction(R.drawable.ic_close_24dp,
context.getString(R.string.action_stop),
NotificationReceiver.cancelRestorePendingBroadcast(context, Notifications.ID_RESTORE))
}
notificationBuilder.show(Notifications.ID_RESTORE)
} }
fun showRestoreError(error: String?) { fun showRestoreError(error: String?) {
@ -116,7 +132,7 @@ internal class BackupNotifier(private val context: Context) {
addAction(R.drawable.nnf_ic_file_folder, addAction(R.drawable.nnf_ic_file_folder,
context.getString(R.string.action_open_log), context.getString(R.string.action_open_log),
NotificationReceiver.openErrorLog(context, uri)) NotificationReceiver.openErrorLogPendingActivity(context, uri))
} }
} }

View file

@ -315,7 +315,6 @@
<string name="pref_backup_service_category">Service</string> <string name="pref_backup_service_category">Service</string>
<string name="pref_backup_interval">Backup frequency</string> <string name="pref_backup_interval">Backup frequency</string>
<string name="pref_backup_slots">Max automatic backups</string> <string name="pref_backup_slots">Max automatic backups</string>
<string name="dialog_restoring_backup">Restoring backup\n%1$s added to library</string>
<string name="source_not_found">Source not found</string> <string name="source_not_found">Source not found</string>
<string name="dialog_restoring_source_not_found">Restoring backup\n%1$s source not found</string> <string name="dialog_restoring_source_not_found">Restoring backup\n%1$s source not found</string>
<string name="backup_created">Backup created</string> <string name="backup_created">Backup created</string>
@ -330,6 +329,7 @@
<string name="restore_in_progress">Restore is already in progress</string> <string name="restore_in_progress">Restore is already in progress</string>
<string name="restoring_backup">Restoring backup</string> <string name="restoring_backup">Restoring backup</string>
<string name="restoring_backup_error">Restoring backup failed</string> <string name="restoring_backup_error">Restoring backup failed</string>
<string name="restoring_backup_canceled">Canceled restore</string>
<!-- Advanced section --> <!-- Advanced section -->
<string name="pref_clear_chapter_cache">Clear chapter cache</string> <string name="pref_clear_chapter_cache">Clear chapter cache</string>