private static void scheduleLaunchTask(
     Context context, GcmNetworkManager scheduler, long minDelayMs) {
   // Google Play Services may not be up to date, if the application was not installed through
   // the Play Store. In this case, scheduling the task will fail silently.
   final long minDelaySecs = minDelayMs / 1000;
   OneoffTask oneoff =
       new OneoffTask.Builder()
           .setService(BackgroundSyncLauncherService.class)
           .setTag("BackgroundSync Event")
           // We have to set a non-zero execution window here
           .setExecutionWindow(minDelaySecs, minDelaySecs + 1)
           .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
           .setPersisted(true)
           .setUpdateCurrent(true)
           .build();
   try {
     scheduler.schedule(oneoff);
     recordBooleanHistogram("BackgroundSync.LaunchTask.ScheduleSuccess", true);
   } catch (IllegalArgumentException e) {
     // Log the occurrence so that we can keep track of how often this is happening, and
     // disable GCM for the remainder of this session.
     setGCMEnabled(false);
     recordBooleanHistogram("BackgroundSync.LaunchTask.ScheduleSuccess", false);
   }
 }
 private void addPeriodic(
     long periodSecs, long flexSecs, int connectivity, boolean charging, boolean persistence) {
   if (flexSecs > periodSecs) {
     Toast.makeText(getActivity(), getString(R.string.scheduler_error_flex), Toast.LENGTH_SHORT)
         .show();
     return;
   }
   String tag = Long.toString(SystemClock.currentThreadTimeMillis());
   final TaskTracker taskTracker = TaskTracker.createPeriodic(tag, periodSecs, flexSecs);
   PeriodicTask periodic =
       new PeriodicTask.Builder()
           .setService(TaskSchedulerService.class)
           .setPeriod(periodSecs)
           .setFlex(flexSecs)
           .setTag(tag)
           .setRequiredNetwork(connectivity)
           .setRequiresCharging(charging)
           .setPersisted(persistence)
           .build();
   mScheduler.schedule(periodic);
   mTasks.updateTask(taskTracker);
 }
 private void addOneOff(long winStartSecs, long winEndSecs, int connectivity, boolean charging) {
   if (winStartSecs > winEndSecs) {
     Toast.makeText(getActivity(), getString(R.string.scheduler_error_window), Toast.LENGTH_SHORT)
         .show();
     return;
   }
   String tag = Long.toString(SystemClock.currentThreadTimeMillis());
   final long elapsedNowSeconds = SystemClock.elapsedRealtime() / 1000;
   final TaskTracker taskTracker =
       TaskTracker.createOneoff(
           tag, elapsedNowSeconds + winStartSecs, elapsedNowSeconds + winEndSecs);
   OneoffTask oneOff =
       new OneoffTask.Builder()
           .setService(TaskSchedulerService.class)
           .setTag(tag)
           .setExecutionWindow(winStartSecs, winEndSecs)
           .setRequiredNetwork(connectivity)
           // Persistence not yet support for Oneoffs.
           .setRequiresCharging(charging)
           .build();
   mScheduler.schedule(oneOff);
   mTasks.updateTask(taskTracker);
 }