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.chrome.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("chrome") |
jkarlin
2015/10/14 18:30:28
chrome code exists in the top level namespace
iclelland
2015/10/14 19:31:24
Done.
| |
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 | |
27 // The instance of BackgroundSyncLauncher currently owned by a C++ | 30 // The instance of BackgroundSyncLauncher currently owned by a C++ |
28 // BackgroundSyncLauncherAndroid, if any. If it is non-null then the browser is running. | 31 // BackgroundSyncLauncherAndroid, if any. If it is non-null then the browser is running. |
29 private static BackgroundSyncLauncher sInstance; | 32 private static BackgroundSyncLauncher sInstance; |
30 | 33 |
34 private GcmNetworkManager mScheduler; | |
35 | |
31 /** | 36 /** |
32 * Create a BackgroundSyncLauncher object, which is owned by C++. | 37 * Create a BackgroundSyncLauncher object, which is owned by C++. |
33 * @param context The app context. | 38 * @param context The app context. |
34 */ | 39 */ |
35 @VisibleForTesting | 40 @VisibleForTesting |
36 @CalledByNative | 41 @CalledByNative |
37 protected static BackgroundSyncLauncher create(Context context) { | 42 protected static BackgroundSyncLauncher create(Context context) { |
38 if (sInstance != null) { | 43 if (sInstance != null) { |
39 throw new IllegalStateException("Already instantiated"); | 44 throw new IllegalStateException("Already instantiated"); |
40 } | 45 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 return prefs.getBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, false); | 80 return prefs.getBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, false); |
76 } | 81 } |
77 @Override | 82 @Override |
78 protected void onPostExecute(Boolean shouldLaunch) { | 83 protected void onPostExecute(Boolean shouldLaunch) { |
79 callback.run(shouldLaunch); | 84 callback.run(shouldLaunch); |
80 } | 85 } |
81 }.execute(); | 86 }.execute(); |
82 } | 87 } |
83 | 88 |
84 /** | 89 /** |
85 * Set interest (or disinterest) in launching the browser the next time the device goes online | 90 * Manages the scheduled tasks which re-launch the browser when the device n ext goes online. |
86 * after the browser closes. On creation of the {@link BackgroundSyncLaunche r} class (on browser | 91 * This method is called by C++ as background sync registrations are added a nd removed. When the |
87 * start) this value is reset to false. This is set by C++ and reset to fals e each time | 92 * {@link BackgroundSyncLauncher} singleton is created (on browser start), t his is called to |
88 * {@link BackgroundSyncLauncher}'s singleton is created (the native browser is started). This | 93 * remove any pre-existing scheduled tasks. |
89 * call is asynchronous. | |
90 */ | 94 */ |
91 @VisibleForTesting | 95 @VisibleForTesting |
92 @CalledByNative | 96 @CalledByNative |
93 protected void setLaunchWhenNextOnline(final Context context, final boolean shouldLaunch) { | 97 protected void launchBrowserWhenNextOnlineIfStopped( |
98 final Context context, final boolean shouldLaunch) { | |
94 new AsyncTask<Void, Void, Void>() { | 99 new AsyncTask<Void, Void, Void>() { |
95 @Override | 100 @Override |
96 protected Void doInBackground(Void... params) { | 101 protected Void doInBackground(Void... params) { |
97 SharedPreferences prefs = PreferenceManager.getDefaultSharedPref erences(context); | 102 SharedPreferences prefs = PreferenceManager.getDefaultSharedPref erences(context); |
98 prefs.edit() | 103 prefs.edit() |
99 .putBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, sho uldLaunch) | 104 .putBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, sho uldLaunch) |
100 .apply(); | 105 .apply(); |
101 return null; | 106 return null; |
102 } | 107 } |
103 }.execute(); | 108 }.execute(); |
109 if (shouldLaunch) { | |
110 scheduleLaunchTask(mScheduler); | |
111 } else { | |
112 removeScheduledTasks(mScheduler); | |
113 } | |
104 } | 114 } |
105 | 115 |
106 /** | 116 /** |
107 * True if the native browser has started and created an instance of {@link | 117 * True if the native browser has started and created an instance of {@link |
108 * BackgroundSyncLauncher}. | 118 * BackgroundSyncLauncher}. |
109 */ | 119 */ |
110 protected static boolean hasInstance() { | 120 protected static boolean hasInstance() { |
111 return sInstance != null; | 121 return sInstance != null; |
112 } | 122 } |
113 | 123 |
114 private BackgroundSyncLauncher(Context context) { | 124 private BackgroundSyncLauncher(Context context) { |
115 setLaunchWhenNextOnline(context, false); | 125 mScheduler = GcmNetworkManager.getInstance(context); |
126 launchBrowserWhenNextOnlineIfStopped(context, false); | |
116 } | 127 } |
117 } | 128 |
129 private static void scheduleLaunchTask(GcmNetworkManager scheduler) { | |
130 // Google Play Services may not be up to date, if the application was no t installed through | |
131 // the Play Store. In this case, scheduling the task will fail silently. | |
132 // TODO(iclelland): Check whether the Play Services client library match es the requirements | |
133 // in the manifest, and respond appropriately if it does not. | |
134 OneoffTask oneoff = new OneoffTask.Builder() | |
135 .setService(BackgroundSyncLauncherService.cl ass) | |
136 .setTag("BackgroundSync Event") | |
137 .setExecutionWindow(0, 1) | |
138 .setRequiredNetwork(Task.NETWORK_STATE_CONNE CTED) | |
139 .setPersisted(true) | |
140 .setUpdateCurrent(true) | |
141 .build(); | |
142 scheduler.schedule(oneoff); | |
143 } | |
144 | |
145 private static void removeScheduledTasks(GcmNetworkManager scheduler) { | |
146 scheduler.cancelAllTasks(BackgroundSyncLauncherService.class); | |
147 } | |
148 | |
149 /** | |
150 * Reschedule any required background sync tasks, if they have been removed due to an | |
151 * application upgrade. | |
152 * | |
153 * This method checks the saved preferences, and reschedules the sync tasks as appropriate | |
154 * to match the preferences. | |
155 * This method is static so that it can be run without actually instantiatin g a | |
156 * BackgroundSyncLauncher. | |
157 */ | |
158 protected static void rescheduleTasksOnUpgrade(final Context context) { | |
159 final GcmNetworkManager scheduler = GcmNetworkManager.getInstance(contex t); | |
160 BackgroundSyncLauncher.ShouldLaunchCallback callback = | |
161 new BackgroundSyncLauncher.ShouldLaunchCallback() { | |
162 @Override | |
163 public void run(Boolean shouldLaunch) { | |
164 if (shouldLaunch) { | |
165 scheduleLaunchTask(scheduler); | |
166 } | |
167 } | |
168 }; | |
169 BackgroundSyncLauncher.shouldLaunchWhenNextOnline(context, callback); | |
170 } | |
171 } | |
OLD | NEW |