androidx.work.ExistingPeriodicWorkPolicy Java Examples

The following examples show how to use androidx.work.ExistingPeriodicWorkPolicy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WorkerWatchdog.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
static void init(Context context) {
    try {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        boolean watchdog = prefs.getBoolean("watchdog", true);
        if (watchdog) {
            Log.i("Queuing " + getName() + " every " + WATCHDOG_INTERVAL + " minutes");

            PeriodicWorkRequest workRequest =
                    new PeriodicWorkRequest.Builder(WorkerWatchdog.class, WATCHDOG_INTERVAL, TimeUnit.MINUTES)
                            .build();
            WorkManager.getInstance(context)
                    .enqueueUniquePeriodicWork(getName(), ExistingPeriodicWorkPolicy.REPLACE, workRequest);

            Log.i("Queued " + getName());
        } else {
            Log.i("Cancelling " + getName());
            WorkManager.getInstance(context).cancelUniqueWork(getName());
            Log.i("Cancelled " + getName());
        }
    } catch (IllegalStateException ex) {
        // https://issuetracker.google.com/issues/138465476
        Log.w(ex);
    }
}
 
Example #2
Source File: WorkerCleanup.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
static void queue(Context context) {
    try {
        Log.i("Queuing " + getName() + " every " + CLEANUP_INTERVAL + " hours");

        PeriodicWorkRequest workRequest =
                new PeriodicWorkRequest.Builder(WorkerCleanup.class, CLEANUP_INTERVAL, TimeUnit.HOURS)
                        .setInitialDelay(CLEANUP_INTERVAL, TimeUnit.HOURS)
                        .build();
        WorkManager.getInstance(context)
                .enqueueUniquePeriodicWork(getName(), ExistingPeriodicWorkPolicy.KEEP, workRequest);

        Log.i("Queued " + getName());
    } catch (IllegalStateException ex) {
        // https://issuetracker.google.com/issues/138465476
        Log.w(ex);
    }
}
 
Example #3
Source File: ZephyrWorkManager.java    From zephyr with MIT License 6 votes vote down vote up
private void setupAppSyncWork() {
    mLogger.log(LogLevel.DEBUG, LOG_TAG, "Setting up app sync work...");

    PeriodicWorkRequest.Builder appSyncWorkBuilder = new PeriodicWorkRequest.Builder(AppSyncWorker.class, 6, TimeUnit.HOURS);

    Constraints constraints = new Constraints.Builder()
            .setRequiresBatteryNotLow(true)
            .build();

    appSyncWorkBuilder.setConstraints(constraints)
            .addTag(SYNC_WORK_TAG);

    PeriodicWorkRequest appSyncWork = appSyncWorkBuilder.build();
    WorkManager.getInstance().enqueueUniquePeriodicWork(SYNC_WORK_TAG, ExistingPeriodicWorkPolicy.REPLACE, appSyncWork);
    mLogger.log(LogLevel.DEBUG, LOG_TAG, "Done setting up app sync work.");
}
 
Example #4
Source File: WorkerHelper.java    From GeometricWeather with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void setNormalPollingWork(Context context, float pollingRate) {
    PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(
            NormalUpdateWorker.class,
            (long) (pollingRate * MINUTES_PER_HOUR),
            TimeUnit.MINUTES
    ).setBackoffCriteria(
            BackoffPolicy.LINEAR,
            BACKOFF_DELAY_MINUTES,
            TimeUnit.MINUTES
    ).setConstraints(
            new Constraints.Builder()
                    .setRequiredNetworkType(NetworkType.CONNECTED)
                    .build()
    ).build();

    WorkManager.getInstance(context).enqueueUniquePeriodicWork(
            WORK_NAME_NORMAL_VIEW,
            ExistingPeriodicWorkPolicy.KEEP,
            request
    );
}
 
Example #5
Source File: SyncWorker.java    From nextcloud-notes with GNU General Public License v3.0 6 votes vote down vote up
public static void update(@NonNull Context context, @NonNull String preferenceValue) {
    deregister(context);
    if (!context.getString(R.string.pref_value_sync_off).equals(preferenceValue)) {
        int repeatInterval = 15;
        TimeUnit unit = TimeUnit.MINUTES;
        if (context.getString(R.string.pref_value_sync_1_hour).equals(preferenceValue)) {
            repeatInterval = 1;
            unit = TimeUnit.HOURS;
        } else if (context.getString(R.string.pref_value_sync_6_hours).equals(preferenceValue)) {
            repeatInterval = 6;
            unit = TimeUnit.HOURS;
        }
        PeriodicWorkRequest work = new PeriodicWorkRequest.Builder(SyncWorker.class, repeatInterval, unit)
                .setConstraints(constraints).build();
        WorkManager.getInstance(context.getApplicationContext()).enqueueUniquePeriodicWork(WORKER_TAG, ExistingPeriodicWorkPolicy.REPLACE, work);
        Log.i(TAG, "Registering worker running each " + repeatInterval + " " + unit);
    }
}
 
Example #6
Source File: NotiWorker.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
public static void scheduleOrCancelWork() {
    if (HiSettingsHelper.getInstance().isNotiTaskEnabled()) {
        Constraints constraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build();

        PeriodicWorkRequest request
                = new PeriodicWorkRequest
                .Builder(NotiWorker.class, NOTI_REPEAT_MINUTTE, TimeUnit.MINUTES)
                .setConstraints(constraints)
                .build();
        WorkManager.getInstance()
                .enqueueUniquePeriodicWork(WORK_NAME, ExistingPeriodicWorkPolicy.KEEP, request);
    } else {
        WorkManager.getInstance().cancelUniqueWork(WORK_NAME);
    }
}
 
Example #7
Source File: CommCareApplication.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
private void scheduleFormSubmissions() {
    Constraints constraints = new Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .setRequiresBatteryNotLow(true)
            .build();

    PeriodicWorkRequest formSubmissionRequest =
            new PeriodicWorkRequest.Builder(FormSubmissionWorker.class, PERIODICITY_FOR_FORM_SUBMISSION_IN_HOURS, TimeUnit.HOURS)
                    .addTag(getCurrentApp().getAppRecord().getApplicationId())
                    .setConstraints(constraints)
                    .setBackoffCriteria(
                            BackoffPolicy.EXPONENTIAL,
                            BACKOFF_DELAY_FOR_FORM_SUBMISSION_RETRY,
                            TimeUnit.MILLISECONDS)
                    .build();

    WorkManager.getInstance(this).enqueueUniquePeriodicWork(
            FormSubmissionHelper.getFormSubmissionRequestName(getCurrentApp().getUniqueId()),
            ExistingPeriodicWorkPolicy.KEEP,
            formSubmissionRequest
    );
}
 
Example #8
Source File: CommCareApplication.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
private void scheduleAppUpdate() {
    if (UpdateHelper.shouldAutoUpdate()) {
        Constraints constraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .setRequiresBatteryNotLow(true)
                .build();


        PeriodicWorkRequest updateRequest =
                new PeriodicWorkRequest.Builder(UpdateWorker.class, UpdateHelper.getAutoUpdatePeriodicity(), TimeUnit.HOURS)
                        .addTag(getCurrentApp().getAppRecord().getApplicationId())
                        .setConstraints(constraints)
                        .setBackoffCriteria(
                                BackoffPolicy.EXPONENTIAL,
                                BACKOFF_DELAY_FOR_UPDATE_RETRY,
                                TimeUnit.MILLISECONDS)
                        .build();

        WorkManager.getInstance(this).enqueueUniquePeriodicWork(
                UpdateHelper.getUpdateRequestName(),
                ExistingPeriodicWorkPolicy.KEEP,
                updateRequest
        );
    }
}
 
Example #9
Source File: SyncManagerPresenter.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void syncData(int seconds, String scheduleTag) {
    preferenceProvider.setValue(Constants.TIME_DATA, seconds);
    workManagerController.cancelUniqueWork(scheduleTag);
    WorkerItem workerItem = new WorkerItem(scheduleTag, WorkerType.DATA, (long) seconds, null, null, ExistingPeriodicWorkPolicy.REPLACE);
    workManagerController.enqueuePeriodicWork(workerItem);
    checkData();
}
 
Example #10
Source File: SyncManagerPresenter.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void syncMeta(int seconds, String scheduleTag) {
    preferenceProvider.setValue(Constants.TIME_META, seconds);
    workManagerController.cancelUniqueWork(scheduleTag);
    WorkerItem workerItem = new WorkerItem(scheduleTag, WorkerType.METADATA, (long) seconds, null, null, ExistingPeriodicWorkPolicy.REPLACE);
    workManagerController.enqueuePeriodicWork(workerItem);
    checkData();
}
 
Example #11
Source File: ApplicationContext.java    From deltachat-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCreate() {
  super.onCreate();

  // if (LeakCanary.isInAnalyzerProcess(this)) {
  //   // This process is dedicated to LeakCanary for heap analysis.
  //   // You should not init your app in this process.
  //   return;
  // }
  // LeakCanary.install(this);

  Log.i("DeltaChat", "++++++++++++++++++ ApplicationContext.onCreate() ++++++++++++++++++");

  System.loadLibrary("native-utils");
  dcContext = new ApplicationDcContext(this);

  new ForegroundDetector(ApplicationContext.getInstance(this));

  BroadcastReceiver networkStateReceiver = new NetworkStateReceiver();
  registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));

  KeepAliveService.maybeStartSelf(this);

  initializeRandomNumberFix();
  initializeLogging();
  initializeJobManager();
  ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
  InChatSounds.getInstance(this);

  dcLocationManager = new DcLocationManager(this);
  try {
    DynamicLanguage.setContextLocale(this, DynamicLanguage.getSelectedLocale(this));
  }
  catch (Exception e) {
    e.printStackTrace();
  }

  dcContext.setStockTranslations();

  IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
  registerReceiver(new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          dcContext.setStockTranslations();
      }
  }, filter);

  // MAYBE TODO: i think the ApplicationContext is also created
  // when the app is stated by FetchWorker timeouts.
  // in this case, the normal threads shall not be started.
  Constraints constraints = new Constraints.Builder()
          .setRequiredNetworkType(NetworkType.CONNECTED)
          .build();
  PeriodicWorkRequest fetchWorkRequest = new PeriodicWorkRequest.Builder(
          FetchWorker.class,
          PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, // usually 15 minutes
          TimeUnit.MILLISECONDS,
          PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS, // the start may be preferred by up to 5 minutes, so we run every 10-15 minutes
          TimeUnit.MILLISECONDS)
          .setConstraints(constraints)
          .build();
  WorkManager.getInstance(this).enqueueUniquePeriodicWork(
          "FetchWorker",
          ExistingPeriodicWorkPolicy.KEEP,
          fetchWorkRequest);
}
 
Example #12
Source File: CapabilitiesWorker.java    From nextcloud-notes with GNU General Public License v3.0 4 votes vote down vote up
public static void update(@NonNull Context context) {
    deregister(context);
    Log.i(TAG, "Registering capabilities worker running each 24 hours.");
    WorkManager.getInstance(context.getApplicationContext()).enqueueUniquePeriodicWork(WORKER_TAG, ExistingPeriodicWorkPolicy.REPLACE, work);
}