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; | |
9 | |
10 import com.google.android.gms.gcm.GcmNetworkManager; | |
11 import com.google.android.gms.gcm.OneoffTask; | |
12 import com.google.android.gms.gcm.Task; | |
13 | |
14 import org.chromium.chrome.browser.ChromeBackgroundService; | |
15 | 8 |
16 /** | 9 /** |
17 * The background scheduler class is for setting GCM Network Manager tasks. | 10 * The background scheduler class is for setting GCM Network Manager tasks. |
18 */ | 11 */ |
19 public class BackgroundScheduler { | 12 public abstract class BackgroundScheduler { |
20 private static final long ONE_WEEK_IN_SECONDS = 60 * 60 * 24 * 7; | 13 private static final long ONE_WEEK_IN_SECONDS = 60 * 60 * 24 * 7; |
nyquist
2017/02/16 23:49:32
Nit: TimeUnit.DAYS.toSeconds(7) ?
fgorski
2017/02/17 22:31:15
Done.
| |
21 private static final long NO_DELAY = 0; | 14 private static final long NO_DELAY = 0; |
22 private static final boolean OVERWRITE = true; | 15 private static final boolean OVERWRITE = true; |
23 | 16 |
24 /** | 17 /** |
25 * For the given Triggering conditions, start a new GCM Network Manager requ est. | 18 * Context used by the scheduler to access services. Extracted to a field, t o clean up method |
19 * signatures. | |
26 */ | 20 */ |
27 public static void schedule(Context context, TriggerConditions triggerCondit ions) { | 21 private Context mContext; |
28 schedule(context, triggerConditions, NO_DELAY, OVERWRITE); | 22 |
23 /** | |
24 * Provides an instance of BackgroundScheduler for given context and current API level. | |
25 * <p> | |
26 * Warning: Don't cache the returned value, as it is bound to {@code context }. Consumers should | |
27 * simply get an instance every time. | |
28 * @return An instance of BackgroundScheduler. | |
29 */ | |
30 public static BackgroundScheduler getInstance(Context context) { | |
31 // TODO(fgorski): Enable JobScheduler for >= N_MR1 once service implemen ted. | |
nyquist
2017/02/16 23:49:32
If a user upgrades the OS, do we need to force-can
fgorski
2017/02/17 22:31:15
We thought about that:
The first job that comes w
nyquist
2017/02/21 18:40:13
Yeah, I think that should work. And yeah; I do thi
| |
32 return new BackgroundGcmScheduler(context); | |
33 } | |
34 | |
35 protected BackgroundScheduler(Context context) { | |
36 mContext = context; | |
37 } | |
38 | |
39 /** Schedules a GCM Network Manager task for provided triggering conditions. */ | |
40 public void schedule(TriggerConditions triggerConditions) { | |
41 scheduleImpl(triggerConditions, NO_DELAY, ONE_WEEK_IN_SECONDS, OVERWRITE ); | |
29 } | 42 } |
30 | 43 |
31 /** | 44 /** |
32 * If there is no currently scheduled task, then start a GCM Network Manager request | 45 * If there is no currently scheduled task, then start a GCM Network Manager request |
33 * for the given Triggering conditions but delayed to run after {@code delay StartSecs}. | 46 * for the given Triggering conditions but delayed to run after {@code delay StartSeconds}. |
34 * Typically, the Request Coordinator will overwrite this task after task pr ocessing | 47 * Typically, the Request Coordinator will overwrite this task after task pr ocessing |
35 * and/or queue updates. This is a backup task in case processing is killed by the | 48 * and/or queue updates. This is a backup task in case processing is killed by the |
36 * system. | 49 * system. |
37 */ | 50 */ |
38 public static void backupSchedule( | 51 public void scheduleBackup(TriggerConditions triggerConditions, long delaySt artSeconds) { |
39 Context context, TriggerConditions triggerConditions, long delayStar tSecs) { | 52 scheduleImpl(triggerConditions, delayStartSeconds, ONE_WEEK_IN_SECONDS, !OVERWRITE); |
40 schedule(context, triggerConditions, delayStartSecs, !OVERWRITE); | |
41 } | 53 } |
42 | 54 |
43 /** | 55 /** Cancel any outstanding GCM Network Manager requests. */ |
44 * Cancel any outstanding GCM Network Manager requests. | 56 public abstract void cancel(); |
45 */ | |
46 public static void unschedule(Context context) { | |
47 // Get the GCM Network Scheduler. | |
48 GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(cont ext); | |
49 gcmNetworkManager.cancelTask(OfflinePageUtils.TASK_TAG, ChromeBackground Service.class); | |
50 } | |
51 | 57 |
52 /** | 58 /** |
53 * For the given Triggering conditions, start a new GCM Network Manager requ est allowed | 59 * For the given Triggering conditions, start a new GCM Network Manager requ est allowed |
54 * to run after {@code delayStartSecs} seconds. | 60 * to run after {@code delayStartSecs} seconds. |
55 */ | 61 */ |
56 private static void schedule(Context context, TriggerConditions triggerCondi tions, | 62 protected abstract void scheduleImpl(TriggerConditions triggerConditions, |
57 long delayStartSecs, boolean overwrite) { | 63 long delayStartSeconds, long executionDeadlineSeconds, boolean overw rite); |
58 // Get the GCM Network Scheduler. | |
59 GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(cont ext); | |
60 | 64 |
61 Bundle taskExtras = new Bundle(); | 65 /** @return Context used to access OS services. */ |
62 TaskExtrasPacker.packTimeInBundle(taskExtras); | 66 protected Context getContext() { |
63 TaskExtrasPacker.packTriggerConditionsInBundle(taskExtras, triggerCondit ions); | 67 return mContext; |
64 | |
65 Task task = new OneoffTask.Builder() | |
66 .setService(ChromeBackgroundService.class) | |
67 .setExecutionWindow(delayStartSecs, ONE_WEEK_IN_SECO NDS) | |
68 .setTag(OfflinePageUtils.TASK_TAG) | |
69 .setUpdateCurrent(overwrite) | |
70 .setRequiredNetwork(triggerConditions.requireUnmeter edNetwork() | |
71 ? Task.NETWORK_STATE_UNMETERED | |
72 : Task.NETWORK_STATE_CONNECTED) | |
73 .setRequiresCharging(triggerConditions.requirePowerC onnected()) | |
74 .setExtras(taskExtras) | |
75 .build(); | |
76 | |
77 gcmNetworkManager.schedule(task); | |
78 } | 68 } |
79 | 69 |
80 /** | 70 /** |
81 * Get the latest power conditions from the android APIs. | 71 * Get the latest power conditions from the android APIs. |
82 */ | 72 */ |
83 public static boolean getPowerConditions(Context context) { | 73 public static boolean getPowerConditions(Context context) { |
84 return OfflinePageUtils.getPowerConditions(context); | 74 return OfflinePageUtils.getPowerConditions(context); |
85 } | 75 } |
86 | 76 |
87 /** | 77 /** |
88 * Get the latest battery conditions from the android APIs. | 78 * Get the latest battery conditions from the android APIs. |
89 */ | 79 */ |
90 public static int getBatteryConditions(Context context) { | 80 public static int getBatteryConditions(Context context) { |
91 return OfflinePageUtils.getBatteryConditions(context); | 81 return OfflinePageUtils.getBatteryConditions(context); |
92 } | 82 } |
93 | 83 |
94 /** | 84 /** |
95 * Get the latest network conditions from the android APIs. | 85 * Get the latest network conditions from the android APIs. |
96 */ | 86 */ |
97 public static int getNetworkConditions(Context context) { | 87 public static int getNetworkConditions(Context context) { |
98 return OfflinePageUtils.getNetworkConditions(context); | 88 return OfflinePageUtils.getNetworkConditions(context); |
99 } | 89 } |
100 } | 90 } |
OLD | NEW |