| Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsLauncher.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsLauncher.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsLauncher.java
|
| index f8dd2c5d59dd7c3637434a21572e726d4ecd46d3..aa70a58fa5efea03801a8ac39959f1df5b77ab31 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsLauncher.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsLauncher.java
|
| @@ -7,6 +7,7 @@ package org.chromium.chrome.browser.ntp.snippets;
|
| import android.content.Context;
|
|
|
| import com.google.android.gms.gcm.GcmNetworkManager;
|
| +import com.google.android.gms.gcm.OneoffTask;
|
| import com.google.android.gms.gcm.PeriodicTask;
|
| import com.google.android.gms.gcm.Task;
|
|
|
| @@ -26,10 +27,15 @@ import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler;
|
| public class SnippetsLauncher {
|
| private static final String TAG = "SnippetsLauncher";
|
|
|
| + // Task tags for fetching snippets.
|
| public static final String TASK_TAG_WIFI_CHARGING = "FetchSnippetsWifiCharging";
|
| public static final String TASK_TAG_WIFI = "FetchSnippetsWifi";
|
| public static final String TASK_TAG_FALLBACK = "FetchSnippetsFallback";
|
|
|
| + // Task tag for re-scheduling the snippet fetching. This is used to support different fetching
|
| + // intervals during different times of day.
|
| + public static final String TASK_TAG_RESCHEDULE = "RescheduleSnippets";
|
| +
|
| // The instance of SnippetsLauncher currently owned by a C++ SnippetsLauncherAndroid, if any.
|
| // If it is non-null then the browser is running.
|
| private static SnippetsLauncher sInstance;
|
| @@ -90,7 +96,7 @@ public class SnippetsLauncher {
|
| }
|
| }
|
|
|
| - private static PeriodicTask buildTask(
|
| + private static PeriodicTask buildFetchTask(
|
| String tag, long periodSeconds, int requiredNetwork, boolean requiresCharging) {
|
| return new PeriodicTask.Builder()
|
| .setService(ChromeBackgroundService.class)
|
| @@ -103,21 +109,48 @@ public class SnippetsLauncher {
|
| .build();
|
| }
|
|
|
| + private static OneoffTask buildRescheduleTask(long delaySeconds) {
|
| + final int 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();
|
| + }
|
| +
|
| + private void scheduleOrCancelFetchTask(
|
| + String taskTag, long period, int requiredNetwork, boolean requiresCharging) {
|
| + if (period > 0) {
|
| + mScheduler.schedule(buildFetchTask(taskTag, period, requiredNetwork, requiresCharging));
|
| + } else {
|
| + mScheduler.cancelTask(taskTag, ChromeBackgroundService.class);
|
| + }
|
| + }
|
| +
|
| @CalledByNative
|
| - private boolean schedule(
|
| - long periodWifiChargingSeconds, long periodWifiSeconds, long periodFallbackSeconds) {
|
| + private boolean schedule(long periodWifiChargingSeconds, long periodWifiSeconds,
|
| + long periodFallbackSeconds, long rescheduleDelaySeconds) {
|
| if (!mGCMEnabled) return false;
|
| Log.d(TAG, "Scheduling: " + periodWifiChargingSeconds + " " + periodWifiSeconds + " "
|
| + periodFallbackSeconds);
|
| // 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.
|
| try {
|
| - mScheduler.schedule(buildTask(TASK_TAG_WIFI_CHARGING, periodWifiChargingSeconds,
|
| - Task.NETWORK_STATE_UNMETERED, true));
|
| - mScheduler.schedule(buildTask(
|
| - TASK_TAG_WIFI, periodWifiSeconds, Task.NETWORK_STATE_UNMETERED, false));
|
| - mScheduler.schedule(buildTask(
|
| - TASK_TAG_FALLBACK, periodFallbackSeconds, Task.NETWORK_STATE_CONNECTED, false));
|
| + scheduleOrCancelFetchTask(TASK_TAG_WIFI_CHARGING, periodWifiChargingSeconds,
|
| + Task.NETWORK_STATE_UNMETERED, true);
|
| + scheduleOrCancelFetchTask(
|
| + TASK_TAG_WIFI, periodWifiSeconds, Task.NETWORK_STATE_UNMETERED, false);
|
| + scheduleOrCancelFetchTask(
|
| + TASK_TAG_FALLBACK, periodFallbackSeconds, Task.NETWORK_STATE_CONNECTED, false);
|
| + if (rescheduleDelaySeconds > 0) {
|
| + mScheduler.schedule(buildRescheduleTask(rescheduleDelaySeconds));
|
| + } else {
|
| + mScheduler.cancelTask(TASK_TAG_RESCHEDULE, ChromeBackgroundService.class);
|
| + }
|
| } catch (IllegalArgumentException e) {
|
| // Disable GCM for the remainder of this session.
|
| mGCMEnabled = false;
|
| @@ -131,18 +164,7 @@ public class SnippetsLauncher {
|
| private boolean unschedule() {
|
| if (!mGCMEnabled) return false;
|
| Log.i(TAG, "Unscheduling");
|
| - try {
|
| - mScheduler.cancelTask(TASK_TAG_WIFI_CHARGING, ChromeBackgroundService.class);
|
| - mScheduler.cancelTask(TASK_TAG_WIFI, ChromeBackgroundService.class);
|
| - mScheduler.cancelTask(TASK_TAG_FALLBACK, ChromeBackgroundService.class);
|
| - } catch (IllegalArgumentException e) {
|
| - // This occurs when SnippetsLauncherService is not found in the application
|
| - // manifest. Disable GCM for the remainder of this session.
|
| - mGCMEnabled = false;
|
| - // Return false so that the failure will be logged.
|
| - return false;
|
| - }
|
| - return true;
|
| + return schedule(0, 0, 0, 0);
|
| }
|
| }
|
|
|
|
|