| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.offlinepages; | 5 package org.chromium.chrome.browser.offlinepages; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Bundle; | 8 import android.os.Bundle; |
| 9 | 9 |
| 10 import com.google.android.gms.gcm.GcmNetworkManager; | 10 import com.google.android.gms.gcm.GcmNetworkManager; |
| 11 import com.google.android.gms.gcm.OneoffTask; | 11 import com.google.android.gms.gcm.OneoffTask; |
| 12 import com.google.android.gms.gcm.Task; | 12 import com.google.android.gms.gcm.Task; |
| 13 | 13 |
| 14 import org.chromium.chrome.browser.ChromeBackgroundService; | 14 import org.chromium.chrome.browser.ChromeBackgroundService; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * The background scheduler class is for setting GCM Network Manager tasks. | 17 * The background scheduler class is for setting GCM Network Manager tasks. |
| 18 */ | 18 */ |
| 19 public class BackgroundScheduler { | 19 public class BackgroundScheduler { |
| 20 /** Bundle key for the timestamp in milliseconds when the request started. *
/ | |
| 21 public static final String DATE_TAG = "Date"; | |
| 22 private static final long ONE_WEEK_IN_SECONDS = 60 * 60 * 24 * 7; | 20 private static final long ONE_WEEK_IN_SECONDS = 60 * 60 * 24 * 7; |
| 23 | 21 |
| 24 /** | 22 /** |
| 25 * For the given Triggering conditions, start a new GCM Network Manager requ
est. | 23 * For the given Triggering conditions, start a new GCM Network Manager requ
est. |
| 26 */ | 24 */ |
| 27 public static void schedule(Context context) { | 25 public static void schedule(Context context) { |
| 28 // Get the GCM Network Scheduler. | 26 // Get the GCM Network Scheduler. |
| 29 GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(cont
ext); | 27 GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(cont
ext); |
| 30 | 28 |
| 31 // TODO(petewil): Move the bundle packing and unpacking into a new Sched
ulerBridge | |
| 32 // class which can be mocked, and calls the static BackgroundSchdeulerBr
idge. | |
| 33 // TODO(petewil): Add the triggering conditions into the argument bundle
. | 29 // TODO(petewil): Add the triggering conditions into the argument bundle
. |
| 34 // Triggering conditions will include network state and charging require
ments, maybe | 30 // Triggering conditions will include network state and charging require
ments, maybe |
| 35 // also battery percentage. | 31 // also battery percentage. |
| 36 Bundle taskExtras = new Bundle(); | 32 Bundle taskExtras = new Bundle(); |
| 37 taskExtras.putLong(DATE_TAG, System.currentTimeMillis()); | 33 TaskExtrasPacker.packTimeInBundle(taskExtras); |
| 38 | 34 |
| 39 Task task = new OneoffTask.Builder() | 35 Task task = new OneoffTask.Builder() |
| 40 .setService(ChromeBackgroundService.class) | 36 .setService(ChromeBackgroundService.class) |
| 41 .setExecutionWindow(0, ONE_WEEK_IN_SECONDS) | 37 .setExecutionWindow(0, ONE_WEEK_IN_SECONDS) |
| 42 .setTag(OfflinePageUtils.TASK_TAG) | 38 .setTag(OfflinePageUtils.TASK_TAG) |
| 43 .setUpdateCurrent(true) | 39 .setUpdateCurrent(true) |
| 44 .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED) | 40 .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED) |
| 45 .setRequiresCharging(false) | 41 .setRequiresCharging(false) |
| 46 .setExtras(taskExtras) | 42 .setExtras(taskExtras) |
| 47 .setPersisted(true) | |
| 48 .build(); | 43 .build(); |
| 49 | 44 |
| 50 gcmNetworkManager.schedule(task); | 45 gcmNetworkManager.schedule(task); |
| 51 } | 46 } |
| 52 | 47 |
| 53 /** | 48 /** |
| 54 * Cancel any outstanding GCM Network Manager requests. | 49 * Cancel any outstanding GCM Network Manager requests. |
| 55 */ | 50 */ |
| 56 public static void unschedule(Context context) { | 51 public static void unschedule(Context context) { |
| 57 // TODO(petewil): Take our task off the task queue. | 52 // TODO(petewil): Take our task off the task queue. |
| 58 } | 53 } |
| 59 } | 54 } |
| OLD | NEW |