-
-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Last Commit Merged: https://github.com/tachiyomiorg/tachiyomi/commit/cdc160afc2e3bb615fe35c8d7261a3bc16f61996
- Loading branch information
1 parent
28bb39c
commit 12ddefc
Showing
23 changed files
with
320 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreJob.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package eu.kanade.tachiyomi.data.backup | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import androidx.core.net.toUri | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.ExistingWorkPolicy | ||
import androidx.work.ForegroundInfo | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.WorkInfo | ||
import androidx.work.WorkManager | ||
import androidx.work.WorkerParameters | ||
import androidx.work.workDataOf | ||
import eu.kanade.tachiyomi.R | ||
import eu.kanade.tachiyomi.data.notification.Notifications | ||
import eu.kanade.tachiyomi.util.system.notificationManager | ||
import kotlinx.coroutines.CancellationException | ||
import logcat.LogPriority | ||
import tachiyomi.core.util.system.logcat | ||
|
||
class BackupRestoreJob(private val context: Context, workerParams: WorkerParameters) : | ||
CoroutineWorker(context, workerParams) { | ||
|
||
private val notifier = BackupNotifier(context) | ||
|
||
override suspend fun doWork(): Result { | ||
val uri = inputData.getString(LOCATION_URI_KEY)?.toUri() | ||
?: return Result.failure() | ||
|
||
try { | ||
setForeground(getForegroundInfo()) | ||
} catch (e: IllegalStateException) { | ||
logcat(LogPriority.ERROR, e) { "Not allowed to run on foreground service" } | ||
} | ||
|
||
return try { | ||
val restorer = BackupRestorer(context, notifier) | ||
restorer.restoreBackup(uri) | ||
Result.success() | ||
} catch (e: Exception) { | ||
if (e is CancellationException) { | ||
notifier.showRestoreError(context.getString(R.string.restoring_backup_canceled)) | ||
Result.success() | ||
} else { | ||
logcat(LogPriority.ERROR, e) | ||
notifier.showRestoreError(e.message) | ||
Result.failure() | ||
} | ||
} finally { | ||
context.notificationManager.cancel(Notifications.ID_RESTORE_PROGRESS) | ||
} | ||
} | ||
|
||
override suspend fun getForegroundInfo(): ForegroundInfo { | ||
return ForegroundInfo( | ||
Notifications.ID_RESTORE_PROGRESS, | ||
notifier.showRestoreProgress().build(), | ||
) | ||
} | ||
|
||
companion object { | ||
fun isRunning(context: Context): Boolean { | ||
val list = WorkManager.getInstance(context).getWorkInfosByTag(TAG).get() | ||
return list.find { it.state == WorkInfo.State.RUNNING } != null | ||
} | ||
|
||
fun start(context: Context, uri: Uri) { | ||
val inputData = workDataOf( | ||
LOCATION_URI_KEY to uri.toString(), | ||
) | ||
val request = OneTimeWorkRequestBuilder<BackupRestoreJob>() | ||
.addTag(TAG) | ||
.setInputData(inputData) | ||
.build() | ||
WorkManager.getInstance(context) | ||
.enqueueUniqueWork(TAG, ExistingWorkPolicy.KEEP, request) | ||
} | ||
|
||
fun stop(context: Context) { | ||
WorkManager.getInstance(context).cancelUniqueWork(TAG) | ||
} | ||
} | ||
} | ||
|
||
private const val TAG = "BackupRestore" | ||
|
||
private const val LOCATION_URI_KEY = "location_uri" // String |
Oops, something went wrong.