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 |
9 import android.os.AsyncTask; | 9 import com.google.android.gms.gcm.GcmNetworkManager; |
10 import android.preference.PreferenceManager; | 10 import com.google.android.gms.gcm.OneoffTask; |
11 import com.google.android.gms.gcm.Task; | |
11 | 12 |
12 import org.chromium.base.VisibleForTesting; | 13 import org.chromium.base.VisibleForTesting; |
13 import org.chromium.base.annotations.CalledByNative; | 14 import org.chromium.base.annotations.CalledByNative; |
14 import org.chromium.base.annotations.JNINamespace; | 15 import org.chromium.base.annotations.JNINamespace; |
15 | 16 |
16 /** | 17 /** |
17 * The {@link BackgroundSyncLauncher} singleton is created and owned by the C++ browser. It | 18 * 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 | 19 * 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. | 20 *browser closes via the {@link #setLaunchWhenNextOnline} method. |
jkarlin
2015/09/04 13:21:35
while you're here, can you add a space after the a
| |
20 * | 21 * |
21 * Thread model: This class is to be run on the UI thread only. | 22 * Thread model: This class is to be run on the UI thread only. |
22 */ | 23 */ |
23 @JNINamespace("content") | 24 @JNINamespace("content") |
24 public class BackgroundSyncLauncher { | 25 public class BackgroundSyncLauncher { |
25 static final String PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE = "bgsync_launch _next_online"; | |
26 | |
27 // The instance of BackgroundSyncLauncher currently owned by a C++ | 26 // The instance of BackgroundSyncLauncher currently owned by a C++ |
28 // BackgroundSyncLauncherAndroid, if any. If it is non-null then the browser is running. | 27 // BackgroundSyncLauncherAndroid, if any. If it is non-null then the browser is running. |
29 private static BackgroundSyncLauncher sInstance; | 28 private static BackgroundSyncLauncher sInstance; |
30 | 29 |
30 private GcmNetworkManager mScheduler; | |
31 | |
31 /** | 32 /** |
32 * Create a BackgroundSyncLauncher object, which is owned by C++. | 33 * Create a BackgroundSyncLauncher object, which is owned by C++. |
33 * @param context The app context. | 34 * @param context The app context. |
34 */ | 35 */ |
35 @VisibleForTesting | 36 @VisibleForTesting |
36 @CalledByNative | 37 @CalledByNative |
37 protected static BackgroundSyncLauncher create(Context context) { | 38 protected static BackgroundSyncLauncher create(Context context) { |
38 if (sInstance != null) { | 39 if (sInstance != null) { |
39 throw new IllegalStateException("Already instantiated"); | 40 throw new IllegalStateException("Already instantiated"); |
40 } | 41 } |
41 | 42 |
42 sInstance = new BackgroundSyncLauncher(context); | 43 sInstance = new BackgroundSyncLauncher(context); |
43 return sInstance; | 44 return sInstance; |
44 } | 45 } |
45 | 46 |
46 /** | 47 /** |
47 * Called when the C++ counterpart is deleted. | 48 * Called when the C++ counterpart is deleted. |
48 */ | 49 */ |
49 @VisibleForTesting | 50 @VisibleForTesting |
50 @CalledByNative | 51 @CalledByNative |
51 protected void destroy() { | 52 protected void destroy() { |
52 assert sInstance == this; | 53 assert sInstance == this; |
53 sInstance = null; | 54 sInstance = null; |
54 } | 55 } |
55 | 56 |
56 /** | 57 /** |
57 * Callback for {@link #shouldLaunchWhenNextOnline}. The run method is invok ed on the UI thread. | |
58 */ | |
59 public static interface ShouldLaunchCallback { public void run(Boolean shoul dLaunch); } | |
60 | |
61 /** | |
62 * 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 | |
64 * created (the native browser is started). This call is asynchronous and wi ll run the callback | |
65 * on the UI thread when complete. | |
66 * @param context The application context. | |
67 * @param sharedPreferences The shared preferences. | |
68 */ | |
69 protected static void shouldLaunchWhenNextOnline( | |
70 final Context context, final ShouldLaunchCallback callback) { | |
71 new AsyncTask<Void, Void, Boolean>() { | |
72 @Override | |
73 protected Boolean doInBackground(Void... params) { | |
74 SharedPreferences prefs = PreferenceManager.getDefaultSharedPref erences(context); | |
75 return prefs.getBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, false); | |
76 } | |
77 @Override | |
78 protected void onPostExecute(Boolean shouldLaunch) { | |
79 callback.run(shouldLaunch); | |
80 } | |
81 }.execute(); | |
82 } | |
83 | |
84 /** | |
85 * Set interest (or disinterest) in launching the browser the next time the device goes online | 58 * Set interest (or disinterest) in launching the browser the next time the device goes online |
86 * after the browser closes. On creation of the {@link BackgroundSyncLaunche r} class (on browser | 59 * after the browser closes, by scheduling a one-shot task to launch the bro wser. On creation |
87 * start) this value is reset to false. This is set by C++ and reset to fals e each time | 60 * of the {@link BackgroundSyncLauncher} class (on browser start) this is re set to false |
88 * {@link BackgroundSyncLauncher}'s singleton is created (the native browser is started). This | 61 * (removing any scheduled tasks). This is set by C++ and reset to false eac h time |
89 * call is asynchronous. | 62 * {@link BackgroundSyncLauncher}'s singleton is created (the native browser is started). |
jkarlin
2015/09/04 13:21:35
Any chance you could rephrase the entire above com
iclelland
2015/09/10 20:40:46
Rewritten -- hopefully clearer now.
| |
90 */ | 63 */ |
91 @VisibleForTesting | 64 @VisibleForTesting |
92 @CalledByNative | 65 @CalledByNative |
93 protected void setLaunchWhenNextOnline(final Context context, final boolean shouldLaunch) { | 66 protected void setLaunchWhenNextOnline(final boolean shouldLaunch) { |
jkarlin
2015/09/04 13:21:35
The name of this function could be clearer, perhap
iclelland
2015/09/10 20:40:46
Done.
| |
94 new AsyncTask<Void, Void, Void>() { | 67 if (shouldLaunch) { |
95 @Override | 68 scheduleLaunchTask(); |
96 protected Void doInBackground(Void... params) { | 69 } else { |
97 SharedPreferences prefs = PreferenceManager.getDefaultSharedPref erences(context); | 70 removeScheduledTasks(); |
98 prefs.edit() | 71 } |
99 .putBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, sho uldLaunch) | |
100 .apply(); | |
101 return null; | |
102 } | |
103 }.execute(); | |
104 } | 72 } |
105 | 73 |
106 /** | 74 /** |
107 * True if the native browser has started and created an instance of {@link | 75 * True if the native browser has started and created an instance of {@link |
108 * BackgroundSyncLauncher}. | 76 * BackgroundSyncLauncher}. |
109 */ | 77 */ |
110 protected static boolean hasInstance() { | 78 protected static boolean hasInstance() { |
111 return sInstance != null; | 79 return sInstance != null; |
112 } | 80 } |
113 | 81 |
114 private BackgroundSyncLauncher(Context context) { | 82 private BackgroundSyncLauncher(Context context) { |
115 setLaunchWhenNextOnline(context, false); | 83 mScheduler = GcmNetworkManager.getInstance(context); |
84 setLaunchWhenNextOnline(false); | |
116 } | 85 } |
117 } | 86 |
87 private void scheduleLaunchTask() { | |
88 // Google Play Services may not be up to date, if the application was no t installed through | |
89 // the Play Store. In this case, scheduling the task will fail silently. | |
90 // TODO(iclelland): Check whether the Play Services client library match es the requirements | |
91 // in the manifest, and respond appropriately if it does not. | |
92 OneoffTask oneoff = new OneoffTask.Builder() | |
93 .setService(BackgroundSyncLauncherService.class) | |
94 .setTag("BackgroundSync Event") | |
95 .setExecutionWindow(0, 1) | |
96 .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED) | |
97 .setPersisted(true) | |
98 .setUpdateCurrent(true) | |
99 .build(); | |
100 mScheduler.schedule(oneoff); | |
101 } | |
102 | |
103 private void removeScheduledTasks() { | |
104 mScheduler.cancelAllTasks(BackgroundSyncLauncherService.class); | |
105 } | |
jkarlin
2015/09/04 13:21:35
I just saw this in the docs:
Important: When Goog
iclelland
2015/09/10 20:40:46
I'll add a TODO to handle the update case better.
jkarlin
2015/09/11 18:57:55
We'll also need it for retries as retry has an exp
iclelland
2015/09/24 13:40:53
Done.
| |
106 } | |
OLD | NEW |