This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #493: Use WorkManager in fretboard
Closes #493: Use WorkManager in fretboard
- Loading branch information
1 parent
20f5ba1
commit e9df3eb
Showing
4 changed files
with
68 additions
and
0 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
23 changes: 23 additions & 0 deletions
23
...rd/src/main/java/mozilla/components/service/fretboard/scheduler/workmanager/SyncWorker.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,23 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.service.fretboard.scheduler.workmanager | ||
|
||
import androidx.work.Worker | ||
import mozilla.components.service.fretboard.Fretboard | ||
|
||
abstract class SyncWorker : Worker() { | ||
override fun doWork(): Result { | ||
getFretboard().updateExperiments() | ||
return Result.SUCCESS | ||
} | ||
|
||
/** | ||
* Used to provide the instance of Fretboard | ||
* the app is using | ||
* | ||
* @return current Fretboard instance | ||
*/ | ||
abstract fun getFretboard(): Fretboard | ||
} |
43 changes: 43 additions & 0 deletions
43
...va/mozilla/components/service/fretboard/scheduler/workmanager/WorkManagerSyncScheduler.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,43 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.service.fretboard.scheduler.workmanager | ||
|
||
import androidx.work.Constraints | ||
import androidx.work.NetworkType | ||
import androidx.work.PeriodicWorkRequest | ||
import androidx.work.WorkManager | ||
import androidx.work.WorkRequest | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* Class used to schedule sync of experiment | ||
* configuration from the server using WorkManager | ||
*/ | ||
class WorkManagerSyncScheduler { | ||
/** | ||
* Schedule sync with the request specified | ||
* | ||
* @param request object with the sync request configuration | ||
*/ | ||
fun schedule(request: WorkRequest) { | ||
WorkManager.getInstance().enqueue(request) | ||
} | ||
|
||
/** | ||
* Schedule sync with the default constraints | ||
* (once a day and charging) | ||
* | ||
* @param worker worker class | ||
*/ | ||
fun schedule(worker: Class<out SyncWorker>) { | ||
val constraints = Constraints.Builder() | ||
.setRequiredNetworkType(NetworkType.CONNECTED) | ||
.build() | ||
val syncWork = PeriodicWorkRequest.Builder(SyncWorker::class.java, 1, TimeUnit.DAYS) | ||
.setConstraints(constraints) | ||
.build() | ||
schedule(syncWork) | ||
} | ||
} |