com.google.android.gms.gcm.OneoffTask Java Examples

The following examples show how to use com.google.android.gms.gcm.OneoffTask. 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: DownloadResumptionScheduler.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Schedules a future task to start download resumption.
 * @param allowMeteredConnection Whether download resumption can start if connection is metered.
 */
public void schedule(boolean allowMeteredConnection) {
    GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(mContext);
    int networkType = allowMeteredConnection
            ? Task.NETWORK_STATE_CONNECTED : Task.NETWORK_STATE_UNMETERED;
    OneoffTask task = new OneoffTask.Builder()
            .setService(ChromeBackgroundService.class)
            .setExecutionWindow(0, ONE_DAY_IN_SECONDS)
            .setTag(TASK_TAG)
            .setUpdateCurrent(true)
            .setRequiredNetwork(networkType)
            .setRequiresCharging(false)
            .build();
    try {
        gcmNetworkManager.schedule(task);
    } catch (IllegalArgumentException e) {
        Log.e(TAG, "unable to schedule resumption task.", e);
    }
}
 
Example #2
Source File: NetworkGCMTaskService.java    From q-municate-android with Apache License 2.0 6 votes vote down vote up
public static void scheduleOneOff(Context context, String what) {
        Bundle extras = new Bundle();
        extras.putString(EXTRA_KEY, what);
        OneoffTask task = new OneoffTask.Builder()
                .setService(NetworkGCMTaskService.class)
                .setTag(TASK_NETWORK_TAG)
                .setExtras(extras)
//                Execution window: The time period in which the task will execute.
//                First param is the lower bound and the second is the upper bound (both are in seconds).
                .setExecutionWindow(0L, 30L)
                .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
                .setUpdateCurrent(true)
                .build();

        GcmNetworkManager.getInstance(context).schedule(task);
    }
 
Example #3
Source File: BackgroundSyncLauncher.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private static boolean scheduleLaunchTask(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(ChromeBackgroundService.class)
                                .setTag(TASK_TAG)
                                // 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);
    } catch (IllegalArgumentException e) {
        // Disable GCM for the remainder of this session.
        setGCMEnabled(false);
        // Return false so that the failure will be logged.
        return false;
    }
    return true;
}
 
Example #4
Source File: PrecacheController.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Schedules a one-off task to finish precaching the resources that were
 * still outstanding when the last task was interrupted. Interrupting such
 * a one-off task will result in scheduling a new one.
 * @param context The application context.
 */
private static void schedulePrecacheCompletionTask(Context context) {
    Log.v(TAG, "scheduling a precache completion task");
    OneoffTask task = new OneoffTask.Builder()
                              .setExecutionWindow(COMPLETION_TASK_MIN_DELAY_SECONDS,
                                      COMPLETION_TASK_MAX_DELAY_SECONDS)
                              .setPersisted(true)
                              .setRequiredNetwork(OneoffTask.NETWORK_STATE_UNMETERED)
                              .setRequiresCharging(ChromeVersionInfo.isStableBuild())
                              .setService(ChromeBackgroundService.class)
                              .setTag(CONTINUATION_TASK_TAG)
                              .setUpdateCurrent(true)
                              .build();
    if (sTaskScheduler.scheduleTask(context, task)) {
        PrecacheUMA.record(PrecacheUMA.Event.ONEOFF_TASK_SCHEDULE);
    } else {
        PrecacheUMA.record(PrecacheUMA.Event.ONEOFF_TASK_SCHEDULE_FAIL);
    }
}
 
Example #5
Source File: DownloadResumptionScheduler.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Schedules a future task to start download resumption.
 * @param allowMeteredConnection Whether download resumption can start if connection is metered.
 */
public void schedule(boolean allowMeteredConnection) {
    GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(mContext);
    int networkType = allowMeteredConnection
            ? Task.NETWORK_STATE_CONNECTED : Task.NETWORK_STATE_UNMETERED;
    OneoffTask task = new OneoffTask.Builder()
                              .setService(ChromeBackgroundService.class)
                              .setExecutionWindow(0, ONE_DAY_IN_SECONDS)
                              .setTag(TASK_TAG)
                              .setUpdateCurrent(true)
                              .setRequiredNetwork(networkType)
                              .setRequiresCharging(false)
                              .build();
    try {
        gcmNetworkManager.schedule(task);
    } catch (IllegalArgumentException e) {
        Log.e(TAG, "unable to schedule resumption task.", e);
    }
}
 
Example #6
Source File: NetworkSchedulerFragment.java    From gcm with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #7
Source File: MainActivity.java    From android-gcmnetworkmanager with Apache License 2.0 6 votes vote down vote up
public void startWifiTask() {
    Log.d(TAG, "startWiFiTask");

    // Disable WiFi so the task does not start immediately
    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifi.setWifiEnabled(false);

    // [START start_one_off_task]
    OneoffTask task = new OneoffTask.Builder()
            .setService(MyTaskService.class)
            .setTag(TASK_TAG_WIFI)
            .setExecutionWindow(0L, 3600L)
            .setRequiredNetwork(Task.NETWORK_STATE_UNMETERED)
            .build();

    mGcmNetworkManager.schedule(task);
    // [END start_one_off_task]
}
 
Example #8
Source File: BackgroundSyncLauncher.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
private static boolean 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(ChromeBackgroundService.class)
                                .setTag(TASK_TAG)
                                // 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);
    } catch (IllegalArgumentException e) {
        // Disable GCM for the remainder of this session.
        setGCMEnabled(false);
        // Return false so that the failure will be logged.
        return false;
    }
    return true;
}
 
Example #9
Source File: PrecacheController.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * Schedules a one-off task to finish precaching the resources that were
 * still outstanding when the last task was interrupted. Interrupting such
 * a one-off task will result in scheduling a new one.
 * @param context The application context.
 */
private static void schedulePrecacheCompletionTask(Context context) {
    Log.v(TAG, "scheduling a precache completion task");
    OneoffTask task = new OneoffTask.Builder()
                              .setExecutionWindow(COMPLETION_TASK_MIN_DELAY_SECONDS,
                                      COMPLETION_TASK_MAX_DELAY_SECONDS)
                              .setPersisted(true)
                              .setRequiredNetwork(OneoffTask.NETWORK_STATE_UNMETERED)
                              .setRequiresCharging(ChromeVersionInfo.isStableBuild())
                              .setService(ChromeBackgroundService.class)
                              .setTag(CONTINUATION_TASK_TAG)
                              .setUpdateCurrent(true)
                              .build();
    if (sTaskScheduler.scheduleTask(context, task)) {
        PrecacheUMA.record(PrecacheUMA.Event.ONEOFF_TASK_SCHEDULE);
    } else {
        PrecacheUMA.record(PrecacheUMA.Event.ONEOFF_TASK_SCHEDULE_FAIL);
    }
}
 
Example #10
Source File: DownloadResumptionScheduler.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * Schedules a future task to start download resumption.
 * @param allowMeteredConnection Whether download resumption can start if connection is metered.
 */
public void schedule(boolean allowMeteredConnection) {
    GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(mContext);
    int networkType = allowMeteredConnection
            ? Task.NETWORK_STATE_CONNECTED : Task.NETWORK_STATE_UNMETERED;
    OneoffTask task = new OneoffTask.Builder()
            .setService(ChromeBackgroundService.class)
            .setExecutionWindow(0, ONE_DAY_IN_SECONDS)
            .setTag(TASK_TAG)
            .setUpdateCurrent(true)
            .setRequiredNetwork(networkType)
            .setRequiresCharging(false)
            .build();
    try {
        gcmNetworkManager.schedule(task);
    } catch (IllegalArgumentException e) {
        Log.e(TAG, "unable to schedule resumption task.", e);
    }
}
 
Example #11
Source File: BackgroundScheduler.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * For the given Triggering conditions, start a new GCM Network Manager request allowed
 * to run after {@code delayStartSecs} seconds.
 */
private static void schedule(Context context, TriggerConditions triggerConditions,
        long delayStartSecs, boolean overwrite) {
    // Get the GCM Network Scheduler.
    GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(context);

    Bundle taskExtras = new Bundle();
    TaskExtrasPacker.packTimeInBundle(taskExtras);
    TaskExtrasPacker.packTriggerConditionsInBundle(taskExtras, triggerConditions);

    Task task = new OneoffTask.Builder()
                        .setService(ChromeBackgroundService.class)
                        .setExecutionWindow(delayStartSecs, ONE_WEEK_IN_SECONDS)
                        .setTag(OfflinePageUtils.TASK_TAG)
                        .setUpdateCurrent(overwrite)
                        .setRequiredNetwork(triggerConditions.requireUnmeteredNetwork()
                                        ? Task.NETWORK_STATE_UNMETERED
                                        : Task.NETWORK_STATE_CONNECTED)
                        .setRequiresCharging(triggerConditions.requirePowerConnected())
                        .setExtras(taskExtras)
                        .build();

    gcmNetworkManager.schedule(task);
}
 
Example #12
Source File: BackgroundSyncLauncher.java    From delion with Apache License 2.0 6 votes vote down vote up
private static boolean 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(ChromeBackgroundService.class)
                                .setTag(TASK_TAG)
                                // 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);
    } catch (IllegalArgumentException e) {
        // Disable GCM for the remainder of this session.
        setGCMEnabled(false);
        // Return false so that the failure will be logged.
        return false;
    }
    return true;
}
 
Example #13
Source File: PrecacheController.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Schedules a one-off task to finish precaching the resources that were
 * still outstanding when the last task was interrupted. Interrupting such
 * a one-off task will result in scheduling a new one.
 * @param context The application context.
 */
private static void schedulePrecacheCompletionTask(Context context) {
    Log.v(TAG, "scheduling a precache completion task");
    OneoffTask task = new OneoffTask.Builder()
            .setExecutionWindow(COMPLETION_TASK_MIN_DELAY_SECONDS,
                    COMPLETION_TASK_MAX_DELAY_SECONDS)
            .setPersisted(true)
            .setRequiredNetwork(OneoffTask.NETWORK_STATE_UNMETERED)
            .setRequiresCharging(true)
            .setService(ChromeBackgroundService.class)
            .setTag(CONTINUATION_TASK_TAG)
            .setUpdateCurrent(true)
            .build();
    if (sTaskScheduler.scheduleTask(context, task)) {
        PrecacheUMA.record(PrecacheUMA.Event.ONEOFF_TASK_SCHEDULE);
    } else {
        PrecacheUMA.record(PrecacheUMA.Event.ONEOFF_TASK_SCHEDULE_FAIL);
    }
}
 
Example #14
Source File: BackgroundScheduler.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * For the given Triggering conditions, start a new GCM Network Manager request allowed
 * to run after {@code delayStartSecs} seconds.
 */
private static void schedule(Context context, TriggerConditions triggerConditions,
        long delayStartSecs, boolean overwrite) {
    // Get the GCM Network Scheduler.
    GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(context);

    Bundle taskExtras = new Bundle();
    TaskExtrasPacker.packTimeInBundle(taskExtras);
    TaskExtrasPacker.packTriggerConditionsInBundle(taskExtras, triggerConditions);

    Task task = new OneoffTask.Builder()
                        .setService(ChromeBackgroundService.class)
                        .setExecutionWindow(delayStartSecs, ONE_WEEK_IN_SECONDS)
                        .setTag(OfflinePageUtils.TASK_TAG)
                        .setUpdateCurrent(overwrite)
                        .setRequiredNetwork(triggerConditions.requireUnmeteredNetwork()
                                        ? Task.NETWORK_STATE_UNMETERED
                                        : Task.NETWORK_STATE_CONNECTED)
                        .setRequiresCharging(triggerConditions.requirePowerConnected())
                        .setExtras(taskExtras)
                        .build();

    gcmNetworkManager.schedule(task);
}
 
Example #15
Source File: MainActivity.java    From android-gcmnetworkmanager with Apache License 2.0 5 votes vote down vote up
public void startChargingTask() {
    Log.d(TAG, "startChargingTask");

    OneoffTask task = new OneoffTask.Builder()
            .setService(MyTaskService.class)
            .setTag(TASK_TAG_CHARGING)
            .setExecutionWindow(0L, 3600L)
            .setRequiresCharging(true)
            .build();

    mGcmNetworkManager.schedule(task);
}
 
Example #16
Source File: BackgroundGcmScheduler.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
protected void scheduleImpl(TriggerConditions triggerConditions, long delayStartSeconds,
        long executionDeadlineSeconds, boolean overwrite) {
    GcmNetworkManager gcmNetworkManager = getGcmNetworkManager();
    if (gcmNetworkManager == null) return;

    Bundle taskExtras = new Bundle();
    TaskExtrasPacker.packTimeInBundle(taskExtras);
    TaskExtrasPacker.packHoldWakelock(taskExtras);
    TaskExtrasPacker.packTriggerConditionsInBundle(taskExtras, triggerConditions);

    Task task = new OneoffTask.Builder()
                        .setService(ChromeBackgroundService.class)
                        .setExecutionWindow(delayStartSeconds, executionDeadlineSeconds)
                        .setTag(OfflinePageUtils.TASK_TAG)
                        .setUpdateCurrent(overwrite)
                        .setRequiredNetwork(triggerConditions.requireUnmeteredNetwork()
                                        ? Task.NETWORK_STATE_UNMETERED
                                        : Task.NETWORK_STATE_CONNECTED)
                        .setRequiresCharging(triggerConditions.requirePowerConnected())
                        .setPersisted(true)
                        .setExtras(taskExtras)
                        .build();

    // Schedule a task using GCM network manager.
    gcmNetworkManager.schedule(task);
}
 
Example #17
Source File: SnippetsLauncher.java    From delion with Apache License 2.0 5 votes vote down vote up
private static OneoffTask buildRescheduleTask(Date date) {
    Date now = new Date();
    // Convert from milliseconds to seconds, rounding up.
    long delaySeconds = (date.getTime() - now.getTime() + 999) / 1000;
    final long intervalSeconds = 15 * 60;
    return new OneoffTask.Builder()
            .setService(ChromeBackgroundService.class)
            .setTag(TASK_TAG_RESCHEDULE)
            .setExecutionWindow(delaySeconds, delaySeconds + intervalSeconds)
            .setRequiredNetwork(Task.NETWORK_STATE_ANY)
            .setRequiresCharging(false)
            .setPersisted(true)
            .setUpdateCurrent(true)
            .build();
}
 
Example #18
Source File: BackgroundTaskSchedulerGcmNetworkManager.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private static Task.Builder getOneOffTaskBuilder(TaskInfo.OneOffInfo oneOffInfo) {
    OneoffTask.Builder builder = new OneoffTask.Builder();
    long windowStartSeconds = oneOffInfo.hasWindowStartTimeConstraint()
            ? TimeUnit.MILLISECONDS.toSeconds(oneOffInfo.getWindowStartTimeMs())
            : 0;
    builder.setExecutionWindow(windowStartSeconds,
            TimeUnit.MILLISECONDS.toSeconds(oneOffInfo.getWindowEndTimeMs()));
    return builder;
}
 
Example #19
Source File: AndroidGcmController.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Clears the current registration token and schedules a {@link OneoffTask} to start the
 * GcmRegistrationTaskService if Google Play Services is available.
 *
 * <p>Declared public to be used by the client to update the token if they define an
 * implementation of InstanceIDListenerService.
 */
public void fetchToken() {
  // Clear the current token. If the call to InstanceID#getToken fails a new token will be fetched
  // on the next call to {@code initializeGcm}.
  AndroidChannelPreferences.setRegistrationToken(context, "");

  // The GMS client library requires the corresponding version of Google Play Services APK to be
  // installed on the device.
  if (CommonUtils.getPackageVersion(context, GOOGLE_PLAY_SERVICES_PACKAGE)
      < QUESO_PLAY_SERVICES_VERSION) {
    logger.warning("Google Play Services unavailable. Initialization failed.");
    return;
  }

  OneoffTask registrationTask =
      new OneoffTask.Builder()
          .setExecutionWindow(0, 1)
          .setTag(AndroidChannelConstants.GCM_REGISTRATION_TASK_SERVICE_TAG)
          .setService(GcmRegistrationTaskService.class)
          .build();

  try {
    gcmNetworkManager.schedule(registrationTask);
  } catch (IllegalArgumentException exception) {
    // Scheduling the service can throw an exception due to a framework error on Android when
    // the the look up for the GCMTaskService being scheduled to be run fails.
    // See crbug/548314.
    logger.warning("Failed to schedule GCM registration task. Exception: %s", exception);
  }
}