| 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.chrome.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; | |
| 11 | 10 |
| 12 import com.google.android.gms.gcm.GcmNetworkManager; | 11 import com.google.android.gms.gcm.GcmNetworkManager; |
| 13 import com.google.android.gms.gcm.OneoffTask; | 12 import com.google.android.gms.gcm.OneoffTask; |
| 14 import com.google.android.gms.gcm.Task; | 13 import com.google.android.gms.gcm.Task; |
| 15 | 14 |
| 15 import org.chromium.base.ContextUtils; |
| 16 import org.chromium.base.Log; | 16 import org.chromium.base.Log; |
| 17 import org.chromium.base.VisibleForTesting; | 17 import org.chromium.base.VisibleForTesting; |
| 18 import org.chromium.base.annotations.CalledByNative; | 18 import org.chromium.base.annotations.CalledByNative; |
| 19 import org.chromium.base.metrics.RecordHistogram; | 19 import org.chromium.base.metrics.RecordHistogram; |
| 20 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; | 20 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; |
| 21 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; | 21 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * The {@link BackgroundSyncLauncher} singleton is created and owned by the C++
browser. It | 24 * The {@link BackgroundSyncLauncher} singleton is created and owned by the C++
browser. It |
| 25 * registers interest in waking up the browser the next time the device goes onl
ine after the | 25 * registers interest in waking up the browser the next time the device goes onl
ine after the |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 * created (the native browser is started). This call is asynchronous and wi
ll run the callback | 86 * created (the native browser is started). This call is asynchronous and wi
ll run the callback |
| 87 * on the UI thread when complete. | 87 * on the UI thread when complete. |
| 88 * @param context The application context. | 88 * @param context The application context. |
| 89 * @param sharedPreferences The shared preferences. | 89 * @param sharedPreferences The shared preferences. |
| 90 */ | 90 */ |
| 91 protected static void shouldLaunchBrowserIfStopped( | 91 protected static void shouldLaunchBrowserIfStopped( |
| 92 final Context context, final ShouldLaunchCallback callback) { | 92 final Context context, final ShouldLaunchCallback callback) { |
| 93 new AsyncTask<Void, Void, Boolean>() { | 93 new AsyncTask<Void, Void, Boolean>() { |
| 94 @Override | 94 @Override |
| 95 protected Boolean doInBackground(Void... params) { | 95 protected Boolean doInBackground(Void... params) { |
| 96 SharedPreferences prefs = PreferenceManager.getDefaultSharedPref
erences(context); | 96 SharedPreferences prefs = ContextUtils.getAppSharedPreferences()
; |
| 97 return prefs.getBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE,
false); | 97 return prefs.getBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE,
false); |
| 98 } | 98 } |
| 99 @Override | 99 @Override |
| 100 protected void onPostExecute(Boolean shouldLaunch) { | 100 protected void onPostExecute(Boolean shouldLaunch) { |
| 101 callback.run(shouldLaunch); | 101 callback.run(shouldLaunch); |
| 102 } | 102 } |
| 103 }.execute(); | 103 }.execute(); |
| 104 } | 104 } |
| 105 | 105 |
| 106 /** | 106 /** |
| 107 * Manages the scheduled tasks which re-launch the browser when the device n
ext goes online | 107 * Manages the scheduled tasks which re-launch the browser when the device n
ext goes online |
| 108 * after at least {@code minDelayMs} milliseconds. | 108 * after at least {@code minDelayMs} milliseconds. |
| 109 * This method is called by C++ as background sync registrations are added a
nd removed. When the | 109 * This method is called by C++ as background sync registrations are added a
nd removed. When the |
| 110 * {@link BackgroundSyncLauncher} singleton is created (on browser start), t
his is called to | 110 * {@link BackgroundSyncLauncher} singleton is created (on browser start), t
his is called to |
| 111 * remove any pre-existing scheduled tasks. | 111 * remove any pre-existing scheduled tasks. |
| 112 * @param context The application context. | 112 * @param context The application context. |
| 113 * @param shouldLaunch Whether or not to launch the browser in the backgroun
d. | 113 * @param shouldLaunch Whether or not to launch the browser in the backgroun
d. |
| 114 * @param minDelayMs The minimum time to wait before checking on the browser
process. | 114 * @param minDelayMs The minimum time to wait before checking on the browser
process. |
| 115 */ | 115 */ |
| 116 @VisibleForTesting | 116 @VisibleForTesting |
| 117 @CalledByNative | 117 @CalledByNative |
| 118 protected void launchBrowserIfStopped( | 118 protected void launchBrowserIfStopped( |
| 119 final Context context, final boolean shouldLaunch, final long minDel
ayMs) { | 119 final Context context, final boolean shouldLaunch, final long minDel
ayMs) { |
| 120 new AsyncTask<Void, Void, Void>() { | 120 new AsyncTask<Void, Void, Void>() { |
| 121 @Override | 121 @Override |
| 122 protected Void doInBackground(Void... params) { | 122 protected Void doInBackground(Void... params) { |
| 123 SharedPreferences prefs = PreferenceManager.getDefaultSharedPref
erences(context); | 123 SharedPreferences prefs = ContextUtils.getAppSharedPreferences()
; |
| 124 prefs.edit() | 124 prefs.edit() |
| 125 .putBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, sho
uldLaunch) | 125 .putBoolean(PREF_BACKGROUND_SYNC_LAUNCH_NEXT_ONLINE, sho
uldLaunch) |
| 126 .apply(); | 126 .apply(); |
| 127 return null; | 127 return null; |
| 128 } | 128 } |
| 129 @Override | 129 @Override |
| 130 protected void onPostExecute(Void params) { | 130 protected void onPostExecute(Void params) { |
| 131 if (sGCMEnabled) { | 131 if (sGCMEnabled) { |
| 132 if (shouldLaunch) { | 132 if (shouldLaunch) { |
| 133 RecordHistogram.recordBooleanHistogram( | 133 RecordHistogram.recordBooleanHistogram( |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 } | 252 } |
| 253 }; | 253 }; |
| 254 BackgroundSyncLauncher.shouldLaunchBrowserIfStopped(context, callback); | 254 BackgroundSyncLauncher.shouldLaunchBrowserIfStopped(context, callback); |
| 255 } | 255 } |
| 256 | 256 |
| 257 @VisibleForTesting | 257 @VisibleForTesting |
| 258 static void setGCMEnabled(boolean enabled) { | 258 static void setGCMEnabled(boolean enabled) { |
| 259 sGCMEnabled = enabled; | 259 sGCMEnabled = enabled; |
| 260 } | 260 } |
| 261 } | 261 } |
| OLD | NEW |