use of androidx.work.WorkManager in project Tusky by Vavassor.
the class NotificationHelper method enablePullNotifications.
public static void enablePullNotifications(Context context) {
WorkManager workManager = WorkManager.getInstance(context);
workManager.cancelAllWorkByTag(NOTIFICATION_PULL_TAG);
WorkRequest workRequest = new PeriodicWorkRequest.Builder(NotificationWorker.class, PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS, PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS, TimeUnit.MILLISECONDS).addTag(NOTIFICATION_PULL_TAG).setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()).build();
workManager.enqueue(workRequest);
Log.d(TAG, "enabled notification checks with " + PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS + "ms interval");
}
use of androidx.work.WorkManager in project android-job by evernote.
the class JobProxyWorkManager method getWorkManager.
private WorkManager getWorkManager() {
// don't cache the instance, it could change under the hood, e.g. during tests
WorkManager workManager;
try {
workManager = WorkManager.getInstance(mContext);
} catch (Throwable t) {
workManager = null;
}
if (workManager == null) {
try {
WorkManager.initialize(mContext, new Configuration.Builder().build());
workManager = WorkManager.getInstance(mContext);
} catch (Throwable ignored) {
}
CAT.w("WorkManager getInstance() returned null, now: %s", workManager);
}
return workManager;
}
use of androidx.work.WorkManager in project android-job by evernote.
the class JobProxyWorkManager method plantPeriodic.
@Override
public void plantPeriodic(JobRequest request) {
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(PlatformWorker.class, request.getIntervalMs(), TimeUnit.MILLISECONDS, request.getFlexMs(), TimeUnit.MILLISECONDS).setConstraints(buildConstraints(request)).addTag(createTag(request.getJobId())).build();
WorkManager workManager = getWorkManager();
if (workManager == null) {
throw new JobProxyIllegalStateException("WorkManager is null");
}
workManager.enqueue(workRequest);
}
use of androidx.work.WorkManager in project android-job by evernote.
the class JobProxyWorkManager method cancel.
@Override
public void cancel(int jobId) {
WorkManager workManager = getWorkManager();
if (workManager == null) {
return;
}
workManager.cancelAllWorkByTag(createTag(jobId));
TransientBundleHolder.cleanUpBundle(jobId);
}
use of androidx.work.WorkManager in project fdroidclient by f-droid.
the class FDroidMetricsWorker method schedule.
/**
* Schedule or cancel a work request to update the app index, according to the
* current preferences. It is meant to run weekly, so it will schedule one week
* from the last run. If it has never been run, it will run as soon as possible.
* <p>
* Although {@link Constraints.Builder#setRequiresDeviceIdle(boolean)} is available
* down to {@link Build.VERSION_CODES#M}, it will cause {@code UpdateService} to
* rarely run, if ever on some devices. So {@link Constraints.Builder#setRequiresDeviceIdle(boolean)}
* should only be used in conjunction with
* {@link Constraints.Builder#setTriggerContentMaxDelay(long, TimeUnit)} to ensure
* that updates actually happen regularly.
*/
public static void schedule(final Context context) {
final WorkManager workManager = WorkManager.getInstance(context);
long interval = TimeUnit.DAYS.toMillis(7);
final Constraints.Builder constraintsBuilder = new Constraints.Builder().setRequiresCharging(true).setRequiresBatteryNotLow(true);
// TODO use the Data/WiFi preferences here
if (Build.VERSION.SDK_INT >= 24) {
constraintsBuilder.setTriggerContentMaxDelay(interval, TimeUnit.MILLISECONDS);
constraintsBuilder.setRequiresDeviceIdle(true);
}
final PeriodicWorkRequest cleanCache = new PeriodicWorkRequest.Builder(FDroidMetricsWorker.class, interval, TimeUnit.MILLISECONDS).setConstraints(constraintsBuilder.build()).build();
workManager.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, cleanCache);
Utils.debugLog(TAG, "Scheduled periodic work");
}
Aggregations