mirror of
https://github.com/null2264/yokai.git
synced 2025-07-17 06:16:54 +00:00
refactor(backup/restore): Return try catch
This commit is contained in:
parent
81a034db1a
commit
b2478844d7
1 changed files with 10 additions and 10 deletions
|
@ -26,7 +26,6 @@ import yokai.util.lang.getString
|
||||||
class BackupRestoreJob(val context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) {
|
class BackupRestoreJob(val context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) {
|
||||||
|
|
||||||
private val notifier = BackupNotifier(context.localeContext)
|
private val notifier = BackupNotifier(context.localeContext)
|
||||||
private val restorer = BackupRestorer(context, notifier)
|
|
||||||
|
|
||||||
override suspend fun getForegroundInfo(): ForegroundInfo {
|
override suspend fun getForegroundInfo(): ForegroundInfo {
|
||||||
val notification = notifier.showRestoreProgress(progress = -1).build()
|
val notification = notifier.showRestoreProgress(progress = -1).build()
|
||||||
|
@ -41,27 +40,28 @@ class BackupRestoreJob(val context: Context, workerParams: WorkerParameters) : C
|
||||||
override suspend fun doWork(): Result {
|
override suspend fun doWork(): Result {
|
||||||
if (isRunning(context)) return Result.failure()
|
if (isRunning(context)) return Result.failure()
|
||||||
|
|
||||||
tryToSetForeground()
|
|
||||||
|
|
||||||
val uriPath = inputData.getString(BackupConst.EXTRA_URI) ?: return Result.failure()
|
val uriPath = inputData.getString(BackupConst.EXTRA_URI) ?: return Result.failure()
|
||||||
|
|
||||||
val uri = Uri.parse(uriPath) ?: return Result.failure()
|
val uri = Uri.parse(uriPath) ?: return Result.failure()
|
||||||
|
|
||||||
withIOContext {
|
tryToSetForeground()
|
||||||
|
|
||||||
|
return withIOContext {
|
||||||
try {
|
try {
|
||||||
if (!restorer.restoreBackup(uri)) {
|
if (!BackupRestorer(context, notifier).restoreBackup(uri))
|
||||||
notifier.showRestoreError(context.getString(MR.strings.restoring_backup_canceled))
|
notifier.showRestoreError(context.getString(MR.strings.restoring_backup_canceled))
|
||||||
}
|
Result.success()
|
||||||
} catch (exception: Exception) {
|
} catch (e: Exception) {
|
||||||
if (exception is CancellationException) {
|
if (e is CancellationException) {
|
||||||
notifier.showRestoreError(context.getString(MR.strings.restoring_backup_canceled))
|
notifier.showRestoreError(context.getString(MR.strings.restoring_backup_canceled))
|
||||||
|
Result.success()
|
||||||
} else {
|
} else {
|
||||||
restorer.writeErrorLog()
|
restorer.writeErrorLog()
|
||||||
notifier.showRestoreError(exception.message)
|
notifier.showRestoreError(e.message)
|
||||||
|
Result.failure()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Result.success()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue