forked from seedvault-app/seedvault
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
195 additions
and
61 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
57 changes: 0 additions & 57 deletions
57
app/src/main/java/com/stevesoltys/seedvault/BackupWorker.kt
This file was deleted.
Oops, something went wrong.
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
63 changes: 63 additions & 0 deletions
63
app/src/main/java/com/stevesoltys/seedvault/worker/ApkBackupWorker.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,63 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 The Calyx Institute | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.stevesoltys.seedvault.worker | ||
|
||
import android.content.Context | ||
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC | ||
import android.util.Log | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.ForegroundInfo | ||
import androidx.work.WorkerParameters | ||
import com.stevesoltys.seedvault.ui.notification.BackupNotificationManager | ||
import com.stevesoltys.seedvault.ui.notification.NOTIFICATION_ID_APK | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
|
||
class ApkBackupWorker( | ||
appContext: Context, | ||
workerParams: WorkerParameters, | ||
) : CoroutineWorker(appContext, workerParams), KoinComponent { | ||
|
||
companion object { | ||
private const val UNIQUE_WORK_NAME = "APK_BACKUP" | ||
|
||
fun schedule(workScheduler: WorkScheduler) { | ||
workScheduler.schedule<ApkBackupWorker>(UNIQUE_WORK_NAME) | ||
} | ||
|
||
fun scheduleNow(workScheduler: WorkScheduler) { | ||
workScheduler.scheduleNow<ApkBackupWorker>(UNIQUE_WORK_NAME) | ||
} | ||
|
||
fun unschedule(workScheduler: WorkScheduler) { | ||
workScheduler.unschedule(UNIQUE_WORK_NAME) | ||
} | ||
} | ||
|
||
private val apkBackupManager by inject<ApkBackupManager>() | ||
private val nm by inject<BackupNotificationManager>() | ||
|
||
override suspend fun doWork(): Result { | ||
try { | ||
setForeground(createForegroundInfo()) | ||
} catch (e: Exception) { | ||
Log.e("ApkBackupWorker", "Error while running setForeground: ", e) | ||
} | ||
return try { | ||
apkBackupManager.backup() | ||
Result.success() | ||
} catch (e: Exception) { | ||
Log.e(ApkBackupWorker::class.simpleName, "Error backing up APKs: ", e) | ||
Result.retry() | ||
} | ||
} | ||
|
||
private fun createForegroundInfo() = ForegroundInfo( | ||
NOTIFICATION_ID_APK, | ||
nm.getApkBackupNotification(null), | ||
FOREGROUND_SERVICE_TYPE_DATA_SYNC, | ||
) | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/stevesoltys/seedvault/worker/AppDataBackupWorker.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,36 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 The Calyx Institute | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.stevesoltys.seedvault.worker | ||
|
||
import android.content.Context | ||
import androidx.work.Worker | ||
import androidx.work.WorkerParameters | ||
import com.stevesoltys.seedvault.transport.requestBackup | ||
|
||
class AppDataBackupWorker( | ||
appContext: Context, | ||
workerParams: WorkerParameters, | ||
) : Worker(appContext, workerParams) { | ||
|
||
companion object { | ||
private const val UNIQUE_WORK_NAME = "APP_BACKUP" | ||
|
||
fun schedule(workScheduler: WorkScheduler) { | ||
workScheduler.schedule<AppDataBackupWorker>(UNIQUE_WORK_NAME) | ||
} | ||
|
||
fun unschedule(workScheduler: WorkScheduler) { | ||
workScheduler.unschedule(UNIQUE_WORK_NAME) | ||
} | ||
} | ||
|
||
override fun doWork(): Result { | ||
// TODO once we make this the default, we should do storage backup here as well | ||
// or have two workers and ensure they never run at the same time | ||
return if (requestBackup(applicationContext)) Result.success() | ||
else Result.retry() | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
app/src/main/java/com/stevesoltys/seedvault/worker/WorkScheduler.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,57 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 The Calyx Institute | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.stevesoltys.seedvault.worker | ||
|
||
import android.content.Context | ||
import androidx.work.BackoffPolicy | ||
import androidx.work.Constraints | ||
import androidx.work.ExistingPeriodicWorkPolicy.UPDATE | ||
import androidx.work.ExistingWorkPolicy.REPLACE | ||
import androidx.work.ListenableWorker | ||
import androidx.work.NetworkType | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST | ||
import androidx.work.PeriodicWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import java.util.concurrent.TimeUnit | ||
|
||
class WorkScheduler(appContext: Context) { | ||
|
||
val workManager = WorkManager.getInstance(appContext) | ||
|
||
inline fun <reified W : ListenableWorker> schedule( | ||
uniqueWorkName: String, | ||
) { | ||
val constraints = Constraints.Builder() | ||
.setRequiredNetworkType(NetworkType.UNMETERED) | ||
.setRequiresCharging(true) | ||
.build() | ||
val workRequest = PeriodicWorkRequestBuilder<W>( | ||
repeatInterval = 24, | ||
repeatIntervalTimeUnit = TimeUnit.HOURS, | ||
flexTimeInterval = 2, | ||
flexTimeIntervalUnit = TimeUnit.HOURS, | ||
).setConstraints(constraints) | ||
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.HOURS) | ||
.build() | ||
workManager.enqueueUniquePeriodicWork(uniqueWorkName, UPDATE, workRequest) | ||
} | ||
|
||
inline fun <reified W : ListenableWorker> scheduleNow( | ||
uniqueWorkName: String, | ||
) { | ||
val workRequest = OneTimeWorkRequestBuilder<W>() | ||
.setExpedited(RUN_AS_NON_EXPEDITED_WORK_REQUEST) | ||
.build() | ||
// TODO still replaces scheduled request | ||
workManager.enqueueUniqueWork(uniqueWorkName, REPLACE, workRequest) | ||
} | ||
|
||
fun unschedule(uniqueWorkName: String) { | ||
workManager.cancelUniqueWork(uniqueWorkName) | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/stevesoltys/seedvault/worker/WorkerModule.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,14 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 The Calyx Institute | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.stevesoltys.seedvault.worker | ||
|
||
import org.koin.android.ext.koin.androidContext | ||
import org.koin.dsl.module | ||
|
||
val workerModule = module { | ||
single { WorkScheduler(androidContext()) } | ||
single { ApkBackupManager(androidContext(), get(), get(), get(), get(), get(), get()) } | ||
} |