Migrate WorkManagerScheduler to non-deprecated WorkManager.getInstance

PiperOrigin-RevId: 322143769
This commit is contained in:
olly 2020-07-20 15:53:26 +01:00 committed by Oliver Woodman
parent 0cd15d9158
commit df1536ab24

View File

@ -46,16 +46,27 @@ public final class WorkManagerScheduler implements Scheduler {
| Requirements.DEVICE_CHARGING | Requirements.DEVICE_CHARGING
| Requirements.DEVICE_STORAGE_NOT_LOW; | Requirements.DEVICE_STORAGE_NOT_LOW;
private final WorkManager workManager;
private final String workName; private final String workName;
/** @deprecated Call {@link #WorkManagerScheduler(Context, String)} instead. */
@Deprecated
@SuppressWarnings("deprecation")
public WorkManagerScheduler(String workName) {
this.workName = workName;
workManager = WorkManager.getInstance();
}
/** /**
* @param context A context.
* @param workName A name for work scheduled by this instance. If the same name was used by a * @param workName A name for work scheduled by this instance. If the same name was used by a
* previous instance, anything scheduled by the previous instance will be canceled by this * previous instance, anything scheduled by the previous instance will be canceled by this
* instance if {@link #schedule(Requirements, String, String)} or {@link #cancel()} are * instance if {@link #schedule(Requirements, String, String)} or {@link #cancel()} are
* called. * called.
*/ */
public WorkManagerScheduler(String workName) { public WorkManagerScheduler(Context context, String workName) {
this.workName = workName; this.workName = workName;
workManager = WorkManager.getInstance(context.getApplicationContext());
} }
@Override @Override
@ -63,13 +74,13 @@ public final class WorkManagerScheduler implements Scheduler {
Constraints constraints = buildConstraints(requirements); Constraints constraints = buildConstraints(requirements);
Data inputData = buildInputData(requirements, servicePackage, serviceAction); Data inputData = buildInputData(requirements, servicePackage, serviceAction);
OneTimeWorkRequest workRequest = buildWorkRequest(constraints, inputData); OneTimeWorkRequest workRequest = buildWorkRequest(constraints, inputData);
WorkManager.getInstance().enqueueUniqueWork(workName, ExistingWorkPolicy.REPLACE, workRequest); workManager.enqueueUniqueWork(workName, ExistingWorkPolicy.REPLACE, workRequest);
return true; return true;
} }
@Override @Override
public boolean cancel() { public boolean cancel() {
WorkManager.getInstance().cancelUniqueWork(workName); workManager.cancelUniqueWork(workName);
return true; return true;
} }