Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java b/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java |
| index bad267185182d208dcaf14efc6b12dae6704278a..510a5b461a899cfeaff545ec63b27982e6520df2 100644 |
| --- a/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java |
| +++ b/content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncher.java |
| @@ -9,6 +9,10 @@ import android.content.SharedPreferences; |
| import android.os.AsyncTask; |
| import android.preference.PreferenceManager; |
| +import com.google.android.gms.gcm.GcmNetworkManager; |
| +import com.google.android.gms.gcm.OneoffTask; |
| +import com.google.android.gms.gcm.Task; |
| + |
| import org.chromium.base.VisibleForTesting; |
| import org.chromium.base.annotations.CalledByNative; |
| import org.chromium.base.annotations.JNINamespace; |
| @@ -28,6 +32,8 @@ public class BackgroundSyncLauncher { |
| // BackgroundSyncLauncherAndroid, if any. If it is non-null then the browser is running. |
| private static BackgroundSyncLauncher sInstance; |
| + private GcmNetworkManager mScheduler; |
| + |
| /** |
| * Create a BackgroundSyncLauncher object, which is owned by C++. |
| * @param context The app context. |
| @@ -58,6 +64,26 @@ public class BackgroundSyncLauncher { |
| */ |
| public static interface ShouldLaunchCallback { public void run(Boolean shouldLaunch); } |
| + private void scheduleLaunchTask() { |
|
jkarlin
2015/09/02 18:16:36
It appears that this can be called multiple times,
iclelland
2015/09/02 20:04:48
If it is called when there is already a task sched
jkarlin
2015/09/03 11:11:57
Acknowledged.
|
| + // 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. |
| + // TODO(iclelland): Check whether the Play Services client library matches the requirements |
| + // in the manifest, and respond appropriately if it does not. |
| + OneoffTask oneoff = new OneoffTask.Builder() |
| + .setService(BackgroundSyncLauncherService.class) |
| + .setTag("BackgroundSync Event") |
| + .setExecutionWindow(0, 0) |
| + .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED) |
| + .setPersisted(true) |
| + .setUpdateCurrent(true) |
| + .build(); |
| + mScheduler.schedule(oneoff); |
| + } |
| + |
| + private void removeScheduledTasks() { |
| + mScheduler.cancelAllTasks(BackgroundSyncLauncherService.class); |
| + } |
| + |
| /** |
| * Returns whether the browser should be launched when the device next goes online. |
| * This is set by C++ and reset to false each time {@link BackgroundSyncLauncher}'s singleton is |
| @@ -101,6 +127,11 @@ public class BackgroundSyncLauncher { |
| return null; |
| } |
| }.execute(); |
| + if (shouldLaunch) { |
| + scheduleLaunchTask(); |
| + } else { |
| + removeScheduledTasks(); |
| + } |
| } |
| /** |
| @@ -112,6 +143,7 @@ public class BackgroundSyncLauncher { |
| } |
| private BackgroundSyncLauncher(Context context) { |
| + mScheduler = GcmNetworkManager.getInstance(context); |
| setLaunchWhenNextOnline(context, false); |
| } |
| -} |
| +} |