Search in sources :

Example 1 with WorkManager

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");
}
Also used : WorkRequest(androidx.work.WorkRequest) PeriodicWorkRequest(androidx.work.PeriodicWorkRequest) WorkManager(androidx.work.WorkManager) TaskStackBuilder(androidx.core.app.TaskStackBuilder) PeriodicWorkRequest(androidx.work.PeriodicWorkRequest)

Example 2 with WorkManager

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;
}
Also used : WorkManager(androidx.work.WorkManager)

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

Example 4 with WorkManager

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);
}
Also used : WorkManager(androidx.work.WorkManager)

Example 5 with WorkManager

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

Aggregations

WorkManager (androidx.work.WorkManager)9 PeriodicWorkRequest (androidx.work.PeriodicWorkRequest)4 Constraints (androidx.work.Constraints)2 OneTimeWorkRequest (androidx.work.OneTimeWorkRequest)2 JobProxyIllegalStateException (com.evernote.android.job.JobProxyIllegalStateException)2 Account (android.accounts.Account)1 Context (android.content.Context)1 DataSetObserver (android.database.DataSetObserver)1 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 DateUtils (android.text.format.DateUtils)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 ViewGroup (android.view.ViewGroup)1 BaseExpandableListAdapter (android.widget.BaseExpandableListAdapter)1 ExpandableListView (android.widget.ExpandableListView)1 ImageButton (android.widget.ImageButton)1 ImageView (android.widget.ImageView)1 ProgressBar (android.widget.ProgressBar)1