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..8010eaac031cc4f2a7d9a7e998068b44ac9809a9 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; |
@@ -23,11 +27,12 @@ import org.chromium.base.annotations.JNINamespace; |
@JNINamespace("content") |
public class BackgroundSyncLauncher { |
static final String PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE = "bgsync_launch_next_online"; |
- |
// The instance of BackgroundSyncLauncher currently owned by a C++ |
// 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. |
@@ -54,43 +59,15 @@ public class BackgroundSyncLauncher { |
} |
/** |
- * Callback for {@link #shouldLaunchWhenNextOnline}. The run method is invoked on the UI thread. |
- */ |
- public static interface ShouldLaunchCallback { public void run(Boolean shouldLaunch); } |
- |
- /** |
- * 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 |
- * created (the native browser is started). This call is asynchronous and will run the callback |
- * on the UI thread when complete. |
- * @param context The application context. |
- * @param sharedPreferences The shared preferences. |
- */ |
- protected static void shouldLaunchWhenNextOnline( |
- final Context context, final ShouldLaunchCallback callback) { |
- new AsyncTask<Void, Void, Boolean>() { |
- @Override |
- protected Boolean doInBackground(Void... params) { |
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); |
- return prefs.getBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, false); |
- } |
- @Override |
- protected void onPostExecute(Boolean shouldLaunch) { |
- callback.run(shouldLaunch); |
- } |
- }.execute(); |
- } |
- |
- /** |
- * Set interest (or disinterest) in launching the browser the next time the device goes online |
- * after the browser closes. On creation of the {@link BackgroundSyncLauncher} class (on browser |
- * start) this value is reset to false. This is set by C++ and reset to false each time |
- * {@link BackgroundSyncLauncher}'s singleton is created (the native browser is started). This |
- * call is asynchronous. |
+ * Manages the scheduled tasks which re-launch the browser when the device next goes online. |
+ * This method is called by C++ as background sync registrations are added and removed. When the |
+ * {@link BackgroundSyncLauncher} singleton is created (on browser start), this is called to |
+ * remove any pre-existing scheduled tasks. |
*/ |
@VisibleForTesting |
@CalledByNative |
- protected void setLaunchWhenNextOnline(final Context context, final boolean shouldLaunch) { |
+ protected void launchBrowserWhenNextOnlineIfStopped( |
+ final Context context, final boolean shouldLaunch) { |
new AsyncTask<Void, Void, Void>() { |
@Override |
protected Void doInBackground(Void... params) { |
@@ -101,6 +78,11 @@ public class BackgroundSyncLauncher { |
return null; |
} |
}.execute(); |
+ if (shouldLaunch) { |
+ scheduleLaunchTask(mScheduler); |
+ } else { |
+ removeScheduledTasks(mScheduler); |
+ } |
} |
/** |
@@ -112,6 +94,53 @@ public class BackgroundSyncLauncher { |
} |
private BackgroundSyncLauncher(Context context) { |
- setLaunchWhenNextOnline(context, false); |
+ mScheduler = GcmNetworkManager.getInstance(context); |
+ launchBrowserWhenNextOnlineIfStopped(context, false); |
+ } |
+ |
+ private static void scheduleLaunchTask(GcmNetworkManager scheduler) { |
+ // Google Play Services may not be up to date, if the application was not installed through |
Yaron
2015/09/24 16:13:17
What version of Play Services do these new depende
iclelland
2015/09/24 17:11:50
Done.
This requires Google Play Services 7.5, whi
|
+ // 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, 1) |
+ .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED) |
+ .setPersisted(true) |
+ .setUpdateCurrent(true) |
+ .build(); |
+ scheduler.schedule(oneoff); |
+ } |
+ |
+ private static void removeScheduledTasks(GcmNetworkManager scheduler) { |
+ scheduler.cancelAllTasks(BackgroundSyncLauncherService.class); |
+ } |
+ |
+ /** |
+ * Reschedule any required background sync tasks, if they have been removed due to an |
+ * application upgrade. |
+ * |
+ * This method checks the saved preferences, and reschedules the sync tasks as appropriate |
+ * to match the preferences. |
+ * This method is static so that it can be run without actually instantiating a |
+ * BackgroundSyncLauncher. |
+ */ |
+ protected static void rescheduleTasksOnUpgrade(final Context context) { |
+ final GcmNetworkManager scheduler = GcmNetworkManager.getInstance(context); |
+ new AsyncTask<Void, Void, Boolean>() { |
jkarlin
2015/09/24 18:08:47
I'd prefer reusing the shouldLaunchWhenNextOnline
iclelland
2015/10/14 17:24:25
Done. I've reinstated that method, and am using it
|
+ @Override |
+ protected Boolean doInBackground(Void... params) { |
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); |
+ return prefs.getBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, false); |
+ } |
+ @Override |
+ protected void onPostExecute(Boolean shouldLaunch) { |
+ if (shouldLaunch) { |
+ scheduleLaunchTask(scheduler); |
+ } |
+ } |
+ }.execute(); |
} |
-} |
+} |