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 cancelTask(String tag) {
   mScheduler.cancelTask(tag, TaskSchedulerService.class);
   TaskTracker task = mTasks.getTask(tag);
   if (task != null) {
     task.cancel();
     mTasks.updateTask(task);
   }
   refresh();
 }
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
    View view = inflater.inflate(R.layout.fragment_network_scheduler, container, false);
    view.findViewById(R.id.scheduler_about_apis).setOnClickListener(this);
    view.findViewById(R.id.scheduler_add_oneoff).setOnClickListener(this);
    view.findViewById(R.id.scheduler_add_periodic).setOnClickListener(this);

    mScheduler = GcmNetworkManager.getInstance(getActivity());

    mTasks = TaskCollection.getInstance(getActivity());

    return view;
  }
 private static void removeScheduledTasks(GcmNetworkManager scheduler) {
   try {
     scheduler.cancelAllTasks(BackgroundSyncLauncherService.class);
     recordBooleanHistogram("BackgroundSync.LaunchTask.CancelSuccess", true);
   } catch (IllegalArgumentException e) {
     // This occurs when BackgroundSyncLauncherService is not found in the application
     // manifest. This should not happen in code that reaches here, but has been seen in
     // the past. See https://crbug.com/548314
     // 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.CancelSuccess", false);
   }
 }
 /**
  * Reschedule any required background sync tasks, if they have been removed due to an application
  * upgrade.
  *
  * <p>This method checks the saved preferences, and reschedules the sync tasks as appropriate to
  * match the preferences. This method is static so that it can be run without actually
  * instantiating a BackgroundSyncLauncher.
  */
 protected static void rescheduleTasksOnUpgrade(final Context context) {
   final GcmNetworkManager scheduler = GcmNetworkManager.getInstance(context);
   BackgroundSyncLauncher.ShouldLaunchCallback callback =
       new BackgroundSyncLauncher.ShouldLaunchCallback() {
         @Override
         public void run(Boolean shouldLaunch) {
           if (shouldLaunch) {
             // It's unclear what time the sync event was supposed to fire, so fire
             // without delay and let the browser reschedule if necessary.
             scheduleLaunchTask(context, scheduler, 0);
           }
         }
       };
   BackgroundSyncLauncher.shouldLaunchBrowserIfStopped(context, callback);
 }
 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);
 }
 protected BackgroundSyncLauncher(Context context) {
   mScheduler = GcmNetworkManager.getInstance(context);
   launchBrowserIfStopped(context, false, 0);
 }