Skip to content

Commit

Permalink
merge36
Browse files Browse the repository at this point in the history
  • Loading branch information
LuftVerbot committed Jul 21, 2023
1 parent 28bb39c commit 12ddefc
Show file tree
Hide file tree
Showing 23 changed files with 320 additions and 412 deletions.
4 changes: 0 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,6 @@
android:name=".data.updater.AppUpdateService"
android:exported="false" />

<service
android:name=".data.backup.BackupRestoreService"
android:exported="false" />

<service android:name=".extension.manga.util.MangaExtensionInstallService"
android:exported="false" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ import eu.kanade.presentation.more.settings.Preference
import eu.kanade.presentation.util.collectAsState
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.backup.BackupConst
import eu.kanade.tachiyomi.data.backup.BackupCreatorJob
import eu.kanade.tachiyomi.data.backup.BackupCreateJob
import eu.kanade.tachiyomi.data.backup.BackupFileValidator
import eu.kanade.tachiyomi.data.backup.BackupRestoreService
import eu.kanade.tachiyomi.data.backup.BackupRestoreJob
import eu.kanade.tachiyomi.data.backup.models.Backup
import eu.kanade.tachiyomi.data.preference.FLAG_CATEGORIES
import eu.kanade.tachiyomi.data.preference.FLAG_CHAPTERS
Expand Down Expand Up @@ -100,7 +100,7 @@ object SettingsBackupScreen : SearchableSettings {
Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION,
)
BackupCreatorJob.startNow(context, it, flag)
BackupCreateJob.startNow(context, it, flag)
}
flag = 0
}
Expand All @@ -126,7 +126,7 @@ object SettingsBackupScreen : SearchableSettings {
subtitle = stringResource(R.string.pref_create_backup_summ),
onClick = {
scope.launch {
if (!BackupCreatorJob.isManualJobRunning(context)) {
if (!BackupCreateJob.isManualJobRunning(context)) {
if (DeviceUtil.isMiui && DeviceUtil.isMiuiOptimizationDisabled()) {
context.toast(R.string.restore_miui_warning, Toast.LENGTH_LONG)
}
Expand Down Expand Up @@ -288,7 +288,7 @@ object SettingsBackupScreen : SearchableSettings {
confirmButton = {
TextButton(
onClick = {
BackupRestoreService.start(context, err.uri)
BackupRestoreJob.start(context, err.uri)
onDismissRequest()
},
) {
Expand Down Expand Up @@ -318,7 +318,7 @@ object SettingsBackupScreen : SearchableSettings {
}

if (results.missingSources.isEmpty() && results.missingTrackers.isEmpty()) {
BackupRestoreService.start(context, it)
BackupRestoreJob.start(context, it)
return@rememberLauncherForActivityResult
}

Expand All @@ -330,7 +330,7 @@ object SettingsBackupScreen : SearchableSettings {
title = stringResource(R.string.pref_restore_backup),
subtitle = stringResource(R.string.pref_restore_backup_summ),
onClick = {
if (!BackupRestoreService.isRunning(context)) {
if (!BackupRestoreJob.isRunning(context)) {
if (DeviceUtil.isMiui && DeviceUtil.isMiuiOptimizationDisabled()) {
context.toast(R.string.restore_miui_warning, Toast.LENGTH_LONG)
}
Expand Down Expand Up @@ -381,7 +381,7 @@ object SettingsBackupScreen : SearchableSettings {
168 to stringResource(R.string.update_weekly),
),
onValueChanged = {
BackupCreatorJob.setupTask(context, it)
BackupCreateJob.setupTask(context, it)
true
},
),
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/eu/kanade/presentation/util/Resources.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap

/**
* Create a BitmapPainter from an drawable resource.
*
* > Only use this if [androidx.compose.ui.res.painterResource] doesn't work.
* Create a BitmapPainter from a drawable resource.
* Use this only if [androidx.compose.ui.res.painterResource] doesn't work.
*
* @param id the resource identifier
*
* @return the bitmap associated with the resource
*/
@Composable
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/eu/kanade/tachiyomi/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import eu.kanade.tachiyomi.network.NetworkPreferences
import eu.kanade.tachiyomi.ui.base.delegate.SecureActivityDelegate
import eu.kanade.tachiyomi.util.system.WebViewUtil
import eu.kanade.tachiyomi.util.system.animatorDurationScale
import eu.kanade.tachiyomi.util.system.notification
import eu.kanade.tachiyomi.util.system.notificationManager
import eu.kanade.tachiyomi.util.system.notify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
Expand Down Expand Up @@ -96,7 +96,10 @@ class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory {
.onEach { enabled ->
if (enabled) {
disableIncognitoReceiver.register()
val notification = notification(Notifications.CHANNEL_INCOGNITO_MODE) {
notify(
Notifications.ID_INCOGNITO_MODE,
Notifications.CHANNEL_INCOGNITO_MODE,
) {
setContentTitle(getString(R.string.pref_incognito_mode))
setContentText(getString(R.string.notification_incognito_text))
setSmallIcon(R.drawable.ic_glasses_24dp)
Expand All @@ -110,7 +113,6 @@ class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory {
)
setContentIntent(pendingIntent)
}
notificationManager.notify(Notifications.ID_INCOGNITO_MODE, notification)
} else {
disableIncognitoReceiver.unregister()
notificationManager.cancel(Notifications.ID_INCOGNITO_MODE)
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/eu/kanade/tachiyomi/Migrations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import eu.kanade.domain.base.BasePreferences
import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.domain.ui.UiPreferences
import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.data.backup.BackupCreatorJob
import eu.kanade.tachiyomi.data.backup.BackupCreateJob
import eu.kanade.tachiyomi.data.library.anime.AnimeLibraryUpdateJob
import eu.kanade.tachiyomi.data.library.manga.MangaLibraryUpdateJob
import eu.kanade.tachiyomi.data.preference.PreferenceValues
Expand Down Expand Up @@ -61,7 +61,7 @@ object Migrations {
// Always set up background tasks to ensure they're running
MangaLibraryUpdateJob.setupTask(context)
AnimeLibraryUpdateJob.setupTask(context)
BackupCreatorJob.setupTask(context)
BackupCreateJob.setupTask(context)

// Fresh install
if (oldVersion == 0) {
Expand Down Expand Up @@ -103,7 +103,7 @@ object Migrations {
if (oldVersion < 43) {
// Restore jobs after migrating from Evernote's job scheduler to WorkManager.
MangaLibraryUpdateJob.setupTask(context)
BackupCreatorJob.setupTask(context)
BackupCreateJob.setupTask(context)
}
if (oldVersion < 44) {
// Reset sorting preference if using removed sort by source
Expand Down Expand Up @@ -287,7 +287,7 @@ object Migrations {
}
}
if (oldVersion < 76) {
BackupCreatorJob.setupTask(context)
BackupCreateJob.setupTask(context)
}
if (oldVersion < 77) {
val oldReaderTap = prefs.getBoolean("reader_tap", false)
Expand Down Expand Up @@ -331,7 +331,7 @@ object Migrations {
}
if (backupPreferences.backupInterval().get() == 0) {
backupPreferences.backupInterval().set(12)
BackupCreatorJob.setupTask(context)
BackupCreateJob.setupTask(context)
}
}
if (oldVersion < 85) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.concurrent.TimeUnit

class BackupCreatorJob(private val context: Context, workerParams: WorkerParameters) :
class BackupCreateJob(private val context: Context, workerParams: WorkerParameters) :
CoroutineWorker(context, workerParams) {

private val notifier = BackupNotifier(context)
Expand All @@ -41,7 +41,6 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet
logcat(LogPriority.ERROR, e) { "Not allowed to run on foreground service" }
}

context.notificationManager.notify(Notifications.ID_BACKUP_PROGRESS, notifier.showBackupProgress().build())
return try {
val location = BackupManager(context).createBackup(uri, flags, isAutoBackup)
if (!isAutoBackup) notifier.showBackupComplete(UniFile.fromUri(context, location.toUri()))
Expand Down Expand Up @@ -73,7 +72,7 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet
}
val workManager = WorkManager.getInstance(context)
if (interval > 0) {
val request = PeriodicWorkRequestBuilder<BackupCreatorJob>(
val request = PeriodicWorkRequestBuilder<BackupCreateJob>(
interval.toLong(),
TimeUnit.HOURS,
10,
Expand All @@ -100,7 +99,7 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet
LOCATION_URI_KEY to uri.toString(),
BACKUP_FLAGS_KEY to flags,
)
val request = OneTimeWorkRequestBuilder<BackupCreatorJob>()
val request = OneTimeWorkRequestBuilder<BackupCreateJob>()
.addTag(TAG_MANUAL)
.setInputData(inputData)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ class BackupNotifier(private val context: Context) {
setContentTitle(context.getString(R.string.backup_created))
setContentText(unifile.filePath ?: unifile.name)

// Clear old actions if they exist
clearActions()

addAction(
R.drawable.ic_share_24dp,
context.getString(R.string.action_share),
Expand All @@ -91,12 +89,10 @@ class BackupNotifier(private val context: Context) {
setProgress(maxAmount, progress, false)
setOnlyAlertOnce(true)

// Clear old actions if they exist
clearActions()

addAction(
R.drawable.ic_close_24dp,
context.getString(R.string.action_stop),
context.getString(R.string.action_cancel),
NotificationReceiver.cancelRestorePendingBroadcast(context, Notifications.ID_RESTORE_PROGRESS),
)
}
Expand Down Expand Up @@ -132,9 +128,7 @@ class BackupNotifier(private val context: Context) {
setContentTitle(context.getString(R.string.restore_completed))
setContentText(context.resources.getQuantityString(R.plurals.restore_completed_message, errorCount, timeString, errorCount))

// Clear old actions if they exist
clearActions()

if (errorCount > 0 && !path.isNullOrEmpty() && !file.isNullOrEmpty()) {
val destFile = File(path, file)
val uri = destFile.getUriCompat(context)
Expand Down
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
Loading

0 comments on commit 12ddefc

Please sign in to comment.