OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.SharedPreferences; | 8 import android.content.SharedPreferences; |
9 import android.os.AsyncTask; | 9 import android.os.AsyncTask; |
10 import android.preference.PreferenceManager; | 10 import android.preference.PreferenceManager; |
11 | 11 |
12 import com.google.android.gms.gcm.GcmNetworkManager; | |
13 import com.google.android.gms.gcm.OneoffTask; | |
14 import com.google.android.gms.gcm.Task; | |
15 | |
12 import org.chromium.base.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
13 import org.chromium.base.annotations.CalledByNative; | 17 import org.chromium.base.annotations.CalledByNative; |
14 import org.chromium.base.annotations.JNINamespace; | 18 import org.chromium.base.annotations.JNINamespace; |
15 | 19 |
16 /** | 20 /** |
17 * The {@link BackgroundSyncLauncher} singleton is created and owned by the C++ browser. It | 21 * The {@link BackgroundSyncLauncher} singleton is created and owned by the C++ browser. It |
18 * registers interest in waking up the browser the next time the device goes onl ine after the | 22 * registers interest in waking up the browser the next time the device goes onl ine after the |
19 *browser closes via the {@link #setLaunchWhenNextOnline} method. | 23 *browser closes via the {@link #setLaunchWhenNextOnline} method. |
20 * | 24 * |
21 * Thread model: This class is to be run on the UI thread only. | 25 * Thread model: This class is to be run on the UI thread only. |
22 */ | 26 */ |
23 @JNINamespace("content") | 27 @JNINamespace("content") |
24 public class BackgroundSyncLauncher { | 28 public class BackgroundSyncLauncher { |
25 static final String PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE = "bgsync_launch _next_online"; | 29 static final String PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE = "bgsync_launch _next_online"; |
26 | 30 |
27 // The instance of BackgroundSyncLauncher currently owned by a C++ | 31 // The instance of BackgroundSyncLauncher currently owned by a C++ |
28 // BackgroundSyncLauncherAndroid, if any. If it is non-null then the browser is running. | 32 // BackgroundSyncLauncherAndroid, if any. If it is non-null then the browser is running. |
29 private static BackgroundSyncLauncher sInstance; | 33 private static BackgroundSyncLauncher sInstance; |
30 | 34 |
35 private GcmNetworkManager mScheduler; | |
36 | |
31 /** | 37 /** |
32 * Create a BackgroundSyncLauncher object, which is owned by C++. | 38 * Create a BackgroundSyncLauncher object, which is owned by C++. |
33 * @param context The app context. | 39 * @param context The app context. |
34 */ | 40 */ |
35 @VisibleForTesting | 41 @VisibleForTesting |
36 @CalledByNative | 42 @CalledByNative |
37 protected static BackgroundSyncLauncher create(Context context) { | 43 protected static BackgroundSyncLauncher create(Context context) { |
38 if (sInstance != null) { | 44 if (sInstance != null) { |
39 throw new IllegalStateException("Already instantiated"); | 45 throw new IllegalStateException("Already instantiated"); |
40 } | 46 } |
(...skipping 10 matching lines...) Expand all Loading... | |
51 protected void destroy() { | 57 protected void destroy() { |
52 assert sInstance == this; | 58 assert sInstance == this; |
53 sInstance = null; | 59 sInstance = null; |
54 } | 60 } |
55 | 61 |
56 /** | 62 /** |
57 * Callback for {@link #shouldLaunchWhenNextOnline}. The run method is invok ed on the UI thread. | 63 * Callback for {@link #shouldLaunchWhenNextOnline}. The run method is invok ed on the UI thread. |
58 */ | 64 */ |
59 public static interface ShouldLaunchCallback { public void run(Boolean shoul dLaunch); } | 65 public static interface ShouldLaunchCallback { public void run(Boolean shoul dLaunch); } |
60 | 66 |
67 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.
| |
68 // Google Play Services may not be up to date, if the application was no t installed through | |
69 // the Play Store. In this case, scheduling the task will fail silently. | |
70 // TODO(iclelland): Check whether the Play Services client library match es the requirements | |
71 // in the manifest, and respond appropriately if it does not. | |
72 OneoffTask oneoff = new OneoffTask.Builder() | |
73 .setService(BackgroundSyncLauncherService.class) | |
74 .setTag("BackgroundSync Event") | |
75 .setExecutionWindow(0, 0) | |
76 .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED) | |
77 .setPersisted(true) | |
78 .setUpdateCurrent(true) | |
79 .build(); | |
80 mScheduler.schedule(oneoff); | |
81 } | |
82 | |
83 private void removeScheduledTasks() { | |
84 mScheduler.cancelAllTasks(BackgroundSyncLauncherService.class); | |
85 } | |
86 | |
61 /** | 87 /** |
62 * Returns whether the browser should be launched when the device next goes online. | 88 * Returns whether the browser should be launched when the device next goes online. |
63 * This is set by C++ and reset to false each time {@link BackgroundSyncLaun cher}'s singleton is | 89 * This is set by C++ and reset to false each time {@link BackgroundSyncLaun cher}'s singleton is |
64 * created (the native browser is started). This call is asynchronous and wi ll run the callback | 90 * created (the native browser is started). This call is asynchronous and wi ll run the callback |
65 * on the UI thread when complete. | 91 * on the UI thread when complete. |
66 * @param context The application context. | 92 * @param context The application context. |
67 * @param sharedPreferences The shared preferences. | 93 * @param sharedPreferences The shared preferences. |
68 */ | 94 */ |
69 protected static void shouldLaunchWhenNextOnline( | 95 protected static void shouldLaunchWhenNextOnline( |
70 final Context context, final ShouldLaunchCallback callback) { | 96 final Context context, final ShouldLaunchCallback callback) { |
(...skipping 18 matching lines...) Expand all Loading... | |
89 * call is asynchronous. | 115 * call is asynchronous. |
90 */ | 116 */ |
91 @VisibleForTesting | 117 @VisibleForTesting |
92 @CalledByNative | 118 @CalledByNative |
93 protected void setLaunchWhenNextOnline(final Context context, final boolean shouldLaunch) { | 119 protected void setLaunchWhenNextOnline(final Context context, final boolean shouldLaunch) { |
94 new AsyncTask<Void, Void, Void>() { | 120 new AsyncTask<Void, Void, Void>() { |
95 @Override | 121 @Override |
96 protected Void doInBackground(Void... params) { | 122 protected Void doInBackground(Void... params) { |
97 SharedPreferences prefs = PreferenceManager.getDefaultSharedPref erences(context); | 123 SharedPreferences prefs = PreferenceManager.getDefaultSharedPref erences(context); |
98 prefs.edit() | 124 prefs.edit() |
99 .putBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, sho uldLaunch) | 125 .putBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, sho uldLaunch) |
jkarlin
2015/09/02 18:16:36
Do we still need this preference?
iclelland
2015/09/02 20:04:48
I wasn't certain. I was still tracing through the
jkarlin
2015/09/03 11:11:57
Nope, this isn't checked by the C++ code, only by
iclelland
2015/09/03 15:52:06
Done.
| |
100 .apply(); | 126 .apply(); |
101 return null; | 127 return null; |
102 } | 128 } |
103 }.execute(); | 129 }.execute(); |
130 if (shouldLaunch) { | |
131 scheduleLaunchTask(); | |
132 } else { | |
133 removeScheduledTasks(); | |
134 } | |
104 } | 135 } |
105 | 136 |
106 /** | 137 /** |
107 * True if the native browser has started and created an instance of {@link | 138 * True if the native browser has started and created an instance of {@link |
108 * BackgroundSyncLauncher}. | 139 * BackgroundSyncLauncher}. |
109 */ | 140 */ |
110 protected static boolean hasInstance() { | 141 protected static boolean hasInstance() { |
111 return sInstance != null; | 142 return sInstance != null; |
112 } | 143 } |
113 | 144 |
114 private BackgroundSyncLauncher(Context context) { | 145 private BackgroundSyncLauncher(Context context) { |
146 mScheduler = GcmNetworkManager.getInstance(context); | |
115 setLaunchWhenNextOnline(context, false); | 147 setLaunchWhenNextOnline(context, false); |
116 } | 148 } |
117 } | 149 } |
OLD | NEW |