Chromium Code Reviews| 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.Build; | |
| 8 import android.os.Bundle; | 9 import android.os.Bundle; |
| 9 | 10 |
| 10 import com.google.android.gms.gcm.GcmNetworkManager; | 11 import com.google.android.gms.gcm.GcmNetworkManager; |
| 11 import com.google.android.gms.gcm.OneoffTask; | 12 import com.google.android.gms.gcm.OneoffTask; |
| 12 import com.google.android.gms.gcm.Task; | 13 import com.google.android.gms.gcm.Task; |
| 13 | 14 |
| 14 import org.chromium.chrome.browser.ChromeBackgroundService; | 15 import org.chromium.chrome.browser.ChromeBackgroundService; |
| 15 | 16 |
| 16 /** | 17 /** |
| 17 * The background scheduler class is for setting GCM Network Manager tasks. | 18 * The background scheduler class is for setting GCM Network Manager tasks. |
| 18 */ | 19 */ |
| 19 public class BackgroundScheduler { | 20 public class BackgroundScheduler { |
| 20 private static final long ONE_WEEK_IN_SECONDS = 60 * 60 * 24 * 7; | 21 private static final long ONE_WEEK_IN_SECONDS = 60 * 60 * 24 * 7; |
| 21 private static final long NO_DELAY = 0; | 22 private static final long NO_DELAY = 0; |
| 22 private static final boolean OVERWRITE = true; | 23 private static final boolean OVERWRITE = true; |
| 23 | 24 |
| 24 /** | 25 private Context mContext; |
|
Pete Williamson
2017/02/10 02:10:42
Why hold onto the context?
As I recall, we need a
fgorski
2017/02/10 19:26:00
It makes sense to add it for 2 reasons:
* All meth
Pete Williamson
2017/02/10 22:45:22
I'm OK with leaving the code as is, but I still th
| |
| 25 * For the given Triggering conditions, start a new GCM Network Manager requ est. | 26 |
| 26 */ | 27 /** @return An instance of BackgroundScheduler for given context, relevant t o the API level. */ |
|
Pete Williamson
2017/02/10 02:10:42
nit: relevant to the API level. -> for the current
fgorski
2017/02/10 19:26:00
Done.
| |
| 27 public static void schedule(Context context, TriggerConditions triggerCondit ions) { | 28 public static BackgroundScheduler getInstance(Context context) { |
| 28 schedule(context, triggerConditions, NO_DELAY, OVERWRITE); | 29 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) { |
| 30 return new BackgroundScheduler(context); | |
| 31 } | |
| 32 | |
| 33 return new BackgroundJobScheduler(context); | |
|
Pete Williamson
2017/02/10 02:10:42
I'm not sure we are ready to turn this on yet, a l
fgorski
2017/02/10 19:26:00
We are not. I added a TODO to leave it for next pa
| |
| 34 } | |
| 35 | |
| 36 protected BackgroundScheduler(Context context) { | |
| 37 mContext = context; | |
| 38 } | |
| 39 | |
| 40 protected Context getContext() { | |
| 41 return mContext; | |
| 42 } | |
| 43 | |
| 44 /** Schedules a GCM Network Manager task for provided triggering conditions. */ | |
| 45 public void schedule(TriggerConditions triggerConditions) { | |
| 46 scheduleImpl(getContext(), triggerConditions, NO_DELAY, ONE_WEEK_IN_SECO NDS, OVERWRITE); | |
|
Pete Williamson
2017/02/10 02:10:43
It might be cleaner to move the GCM impl into its
fgorski
2017/02/10 19:26:00
Done.
I thought about that earlier, but I was tryi
| |
| 29 } | 47 } |
| 30 | 48 |
| 31 /** | 49 /** |
| 32 * If there is no currently scheduled task, then start a GCM Network Manager request | 50 * 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}. | 51 * 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 | 52 * 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 | 53 * and/or queue updates. This is a backup task in case processing is killed by the |
| 36 * system. | 54 * system. |
| 37 */ | 55 */ |
| 38 public static void backupSchedule( | 56 public void backupSchedule(TriggerConditions triggerConditions, long delaySt artSeconds) { |
| 39 Context context, TriggerConditions triggerConditions, long delayStar tSecs) { | 57 scheduleImpl(getContext(), triggerConditions, delayStartSeconds, ONE_WEE K_IN_SECONDS, |
| 40 schedule(context, triggerConditions, delayStartSecs, !OVERWRITE); | 58 !OVERWRITE); |
| 41 } | 59 } |
| 42 | 60 |
| 43 /** | 61 /** |
| 44 * Cancel any outstanding GCM Network Manager requests. | 62 * Cancel any outstanding GCM Network Manager requests. |
| 45 */ | 63 */ |
| 46 public static void unschedule(Context context) { | 64 public void unschedule() { |
| 47 // Get the GCM Network Scheduler. | 65 GcmNetworkManager.getInstance(getContext()) |
| 48 GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(cont ext); | 66 .cancelTask(OfflinePageUtils.TASK_TAG, ChromeBackgroundService.c lass); |
| 49 gcmNetworkManager.cancelTask(OfflinePageUtils.TASK_TAG, ChromeBackground Service.class); | |
| 50 } | 67 } |
| 51 | 68 |
| 52 /** | 69 /** |
| 53 * For the given Triggering conditions, start a new GCM Network Manager requ est allowed | 70 * For the given Triggering conditions, start a new GCM Network Manager requ est allowed |
| 54 * to run after {@code delayStartSecs} seconds. | 71 * to run after {@code delayStartSecs} seconds. |
| 55 */ | 72 */ |
| 56 private static void schedule(Context context, TriggerConditions triggerCondi tions, | 73 protected void scheduleImpl(Context context, TriggerConditions triggerCondit ions, |
| 57 long delayStartSecs, boolean overwrite) { | 74 long delayStartSeconds, long executionDeadlineSeconds, boolean overw rite) { |
| 58 // Get the GCM Network Scheduler. | |
| 59 GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(cont ext); | |
| 60 | |
| 61 Bundle taskExtras = new Bundle(); | 75 Bundle taskExtras = new Bundle(); |
| 62 TaskExtrasPacker.packTimeInBundle(taskExtras); | 76 TaskExtrasPacker.packTimeInBundle(taskExtras); |
| 77 TaskExtrasPacker.packHoldWakelock(taskExtras); | |
| 63 TaskExtrasPacker.packTriggerConditionsInBundle(taskExtras, triggerCondit ions); | 78 TaskExtrasPacker.packTriggerConditionsInBundle(taskExtras, triggerCondit ions); |
| 64 | 79 |
| 65 Task task = new OneoffTask.Builder() | 80 Task task = new OneoffTask.Builder() |
| 66 .setService(ChromeBackgroundService.class) | 81 .setService(ChromeBackgroundService.class) |
| 67 .setExecutionWindow(delayStartSecs, ONE_WEEK_IN_SECO NDS) | 82 .setExecutionWindow(delayStartSeconds, executionDead lineSeconds) |
| 68 .setTag(OfflinePageUtils.TASK_TAG) | 83 .setTag(OfflinePageUtils.TASK_TAG) |
| 69 .setUpdateCurrent(overwrite) | 84 .setUpdateCurrent(overwrite) |
| 70 .setRequiredNetwork(triggerConditions.requireUnmeter edNetwork() | 85 .setRequiredNetwork(triggerConditions.requireUnmeter edNetwork() |
| 71 ? Task.NETWORK_STATE_UNMETERED | 86 ? Task.NETWORK_STATE_UNMETERED |
| 72 : Task.NETWORK_STATE_CONNECTED) | 87 : Task.NETWORK_STATE_CONNECTED) |
| 73 .setRequiresCharging(triggerConditions.requirePowerC onnected()) | 88 .setRequiresCharging(triggerConditions.requirePowerC onnected()) |
| 74 .setExtras(taskExtras) | 89 .setExtras(taskExtras) |
| 75 .build(); | 90 .build(); |
| 76 | 91 |
| 77 gcmNetworkManager.schedule(task); | 92 // Schedule a task using GCM network manager. |
| 93 GcmNetworkManager.getInstance(context).schedule(task); | |
| 78 } | 94 } |
| 79 | 95 |
| 80 /** | 96 /** |
| 81 * Get the latest power conditions from the android APIs. | 97 * Get the latest power conditions from the android APIs. |
| 82 */ | 98 */ |
| 83 public static boolean getPowerConditions(Context context) { | 99 public static boolean getPowerConditions(Context context) { |
| 84 return OfflinePageUtils.getPowerConditions(context); | 100 return OfflinePageUtils.getPowerConditions(context); |
| 85 } | 101 } |
| 86 | 102 |
| 87 /** | 103 /** |
| 88 * Get the latest battery conditions from the android APIs. | 104 * Get the latest battery conditions from the android APIs. |
| 89 */ | 105 */ |
| 90 public static int getBatteryConditions(Context context) { | 106 public static int getBatteryConditions(Context context) { |
| 91 return OfflinePageUtils.getBatteryConditions(context); | 107 return OfflinePageUtils.getBatteryConditions(context); |
| 92 } | 108 } |
| 93 | 109 |
| 94 /** | 110 /** |
| 95 * Get the latest network conditions from the android APIs. | 111 * Get the latest network conditions from the android APIs. |
| 96 */ | 112 */ |
| 97 public static int getNetworkConditions(Context context) { | 113 public static int getNetworkConditions(Context context) { |
| 98 return OfflinePageUtils.getNetworkConditions(context); | 114 return OfflinePageUtils.getNetworkConditions(context); |
| 99 } | 115 } |
| 100 } | 116 } |
| OLD | NEW |