Commit df1536ab by olly Committed by Oliver Woodman

Migrate WorkManagerScheduler to non-deprecated WorkManager.getInstance

PiperOrigin-RevId: 322143769
parent 0cd15d91
...@@ -46,16 +46,27 @@ public final class WorkManagerScheduler implements Scheduler { ...@@ -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 { ...@@ -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;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment