Search in sources :

Example 1 with PeriodicWorkRequest

use of androidx.work.PeriodicWorkRequest 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);
}
Also used : WorkManager(androidx.work.WorkManager) JobProxyIllegalStateException(com.evernote.android.job.JobProxyIllegalStateException) PeriodicWorkRequest(androidx.work.PeriodicWorkRequest)

Example 2 with PeriodicWorkRequest

use of androidx.work.PeriodicWorkRequest in project AntennaPod by AntennaPod.

the class AutoUpdateManager method restartUpdateIntervalAlarm.

/**
 * Sets the interval in which the feeds are refreshed automatically
 */
private static void restartUpdateIntervalAlarm(long intervalMillis, Context context) {
    Log.d(TAG, "Restarting update alarm.");
    PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(FeedUpdateWorker.class, intervalMillis, TimeUnit.MILLISECONDS).setConstraints(getConstraints()).build();
    WorkManager.getInstance(context).enqueueUniquePeriodicWork(WORK_ID_FEED_UPDATE, ExistingPeriodicWorkPolicy.REPLACE, workRequest);
}
Also used : PeriodicWorkRequest(androidx.work.PeriodicWorkRequest)

Example 3 with PeriodicWorkRequest

use of androidx.work.PeriodicWorkRequest 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");
}
Also used : Constraints(androidx.work.Constraints) WorkManager(androidx.work.WorkManager) PeriodicWorkRequest(androidx.work.PeriodicWorkRequest)

Example 4 with PeriodicWorkRequest

use of androidx.work.PeriodicWorkRequest in project fdroidclient by f-droid.

the class CleanCacheWorker method schedule.

/**
 * Schedule or cancel a work request to clean up caches, according to the
 * current preferences. Should be called a) at boot, b) if the preference
 * is changed, or c) on startup, in case we get upgraded.
 */
public static void schedule(@NonNull final Context context) {
    final WorkManager workManager = WorkManager.getInstance(context);
    final long keepTime = Preferences.get().getKeepCacheTime();
    long interval = TimeUnit.DAYS.toMillis(1);
    if (keepTime < interval) {
        interval = keepTime;
    }
    final Constraints.Builder constraintsBuilder = new Constraints.Builder().setRequiresCharging(true).setRequiresBatteryNotLow(true);
    if (Build.VERSION.SDK_INT >= 23) {
        constraintsBuilder.setRequiresDeviceIdle(true);
    }
    final PeriodicWorkRequest cleanCache = new PeriodicWorkRequest.Builder(CleanCacheWorker.class, interval, TimeUnit.MILLISECONDS).setConstraints(constraintsBuilder.build()).build();
    workManager.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, cleanCache);
    Utils.debugLog(TAG, "Scheduled periodic work for cleaning the cache.");
}
Also used : Constraints(androidx.work.Constraints) WorkManager(androidx.work.WorkManager) PeriodicWorkRequest(androidx.work.PeriodicWorkRequest)

Aggregations

PeriodicWorkRequest (androidx.work.PeriodicWorkRequest)4 WorkManager (androidx.work.WorkManager)3 Constraints (androidx.work.Constraints)2 JobProxyIllegalStateException (com.evernote.android.job.JobProxyIllegalStateException)1