fix: Backup failed trying to get foreground info

This commit is contained in:
Ahmad Ansori Palembani 2024-09-23 07:12:37 +07:00
parent 7752f64efb
commit 186d95fcf9
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
2 changed files with 18 additions and 2 deletions

View file

@ -38,7 +38,7 @@ class BackupNotifier(private val context: Context) {
context.notificationManager.notify(id, build())
}
fun showBackupProgress() {
fun showBackupProgress(): NotificationCompat.Builder {
val builder = with(progressNotificationBuilder) {
setContentTitle(context.getString(MR.strings.creating_backup))
@ -47,6 +47,8 @@ class BackupNotifier(private val context: Context) {
}
builder.show(Notifications.ID_BACKUP_PROGRESS)
return builder
}
fun showBackupError(error: String?) {

View file

@ -1,11 +1,14 @@
package eu.kanade.tachiyomi.data.backup.create
import android.content.Context
import android.content.pm.ServiceInfo
import android.net.Uri
import android.os.Build
import androidx.core.net.toUri
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.ExistingWorkPolicy
import androidx.work.ForegroundInfo
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkInfo
@ -46,7 +49,6 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet
val options = inputData.getBooleanArray(BACKUP_FLAGS_KEY)?.let { BackupOptions.fromBooleanArray(it) }
?: BackupOptions()
notifier.showBackupProgress()
return try {
val location = BackupCreator(context).createBackup(uri, options, isAutoBackup)
if (!isAutoBackup) notifier.showBackupComplete(UniFile.fromUri(context, location.toUri())!!)
@ -65,6 +67,18 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet
return storageManager.getAutomaticBackupsDirectory()?.uri
}
override suspend fun getForegroundInfo(): ForegroundInfo {
return ForegroundInfo(
Notifications.ID_BACKUP_PROGRESS,
notifier.showBackupProgress().build(),
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
} else {
0
},
)
}
companion object {
fun isManualJobRunning(context: Context): Boolean {
val list = WorkManager.getInstance(context).getWorkInfosByTag(TAG_MANUAL).get()