-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add backup/restore feature with auto backup
- Loading branch information
1 parent
c256340
commit 7babddf
Showing
22 changed files
with
896 additions
and
26 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
98 changes: 98 additions & 0 deletions
98
app/src/main/java/me/timschneeberger/rootlessjamesdsp/backup/BackupCreatorJob.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,98 @@ | ||
package me.timschneeberger.rootlessjamesdsp.backup | ||
|
||
import android.app.NotificationManager | ||
import android.content.Context | ||
import android.net.Uri | ||
import androidx.core.net.toUri | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.ExistingPeriodicWorkPolicy | ||
import androidx.work.ExistingWorkPolicy | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.PeriodicWorkRequestBuilder | ||
import androidx.work.WorkInfo | ||
import androidx.work.WorkManager | ||
import androidx.work.WorkerParameters | ||
import androidx.work.workDataOf | ||
import com.hippo.unifile.UniFile | ||
import me.timschneeberger.rootlessjamesdsp.Notifications | ||
import me.timschneeberger.rootlessjamesdsp.R | ||
import me.timschneeberger.rootlessjamesdsp.utils.Preferences | ||
import me.timschneeberger.rootlessjamesdsp.utils.SystemServices | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
import timber.log.Timber | ||
import java.util.concurrent.TimeUnit | ||
|
||
class BackupCreatorJob(private val context: Context, workerParams: WorkerParameters) : | ||
CoroutineWorker(context, workerParams) { | ||
|
||
private val notificationManager = SystemServices.get<NotificationManager>(context) | ||
|
||
override suspend fun doWork(): Result { | ||
val notifier = BackupNotifier(context) | ||
val uri = inputData.getString(LOCATION_URI_KEY)?.toUri() | ||
?: preferences.get<String>(R.string.key_backup_location).toUri() | ||
val isAutoBackup = inputData.getBoolean(IS_AUTO_BACKUP_KEY, true) | ||
|
||
notificationManager.notify(Notifications.ID_BACKUP_PROGRESS, notifier.showBackupProgress().build()) | ||
return try { | ||
val location = BackupManager(context).createBackup(uri, isAutoBackup) | ||
if (!isAutoBackup) notifier.showBackupComplete(UniFile.fromUri(context, location.toUri())) | ||
Result.success() | ||
} catch (e: Exception) { | ||
Timber.e(e) | ||
if (!isAutoBackup) notifier.showBackupError(e.message) | ||
Result.failure() | ||
} finally { | ||
notificationManager.cancel(Notifications.ID_BACKUP_PROGRESS) | ||
} | ||
} | ||
|
||
companion object : KoinComponent { | ||
private val preferences: Preferences.App by inject() | ||
|
||
fun isManualJobRunning(context: Context): Boolean { | ||
val list = WorkManager.getInstance(context).getWorkInfosByTag(TAG_MANUAL).get() | ||
return list.find { it.state == WorkInfo.State.RUNNING } != null | ||
} | ||
|
||
fun setupTask(context: Context, prefInterval: Int? = null) { | ||
val interval = prefInterval ?: preferences.get<String>(R.string.key_backup_frequency).toInt() | ||
val workManager = WorkManager.getInstance(context) | ||
if (interval > 0) { | ||
|
||
val request = PeriodicWorkRequestBuilder<BackupCreatorJob>( | ||
interval.toLong(), | ||
TimeUnit.HOURS, | ||
10, | ||
TimeUnit.MINUTES, | ||
) | ||
.addTag(TAG_AUTO) | ||
.setInputData(workDataOf(IS_AUTO_BACKUP_KEY to true)) | ||
.build() | ||
|
||
workManager.enqueueUniquePeriodicWork(TAG_AUTO, ExistingPeriodicWorkPolicy.REPLACE, request) | ||
} else { | ||
workManager.cancelUniqueWork(TAG_AUTO) | ||
} | ||
} | ||
|
||
fun startNow(context: Context, uri: Uri) { | ||
val inputData = workDataOf( | ||
IS_AUTO_BACKUP_KEY to false, | ||
LOCATION_URI_KEY to uri.toString(), | ||
) | ||
val request = OneTimeWorkRequestBuilder<BackupCreatorJob>() | ||
.addTag(TAG_MANUAL) | ||
.setInputData(inputData) | ||
.build() | ||
WorkManager.getInstance(context).enqueueUniqueWork(TAG_MANUAL, ExistingWorkPolicy.KEEP, request) | ||
} | ||
} | ||
} | ||
|
||
private const val TAG_AUTO = "BackupCreator" | ||
private const val TAG_MANUAL = "$TAG_AUTO:manual" | ||
|
||
private const val IS_AUTO_BACKUP_KEY = "is_auto_backup" // Boolean | ||
private const val LOCATION_URI_KEY = "location_uri" // String |
Oops, something went wrong.