Index: base/android/java/src/org/chromium/base/ApplicationStatus.java |
diff --git a/base/android/java/src/org/chromium/base/ApplicationStatus.java b/base/android/java/src/org/chromium/base/ApplicationStatus.java |
index 5035b9cabfb0f182d869d154ced60f290bdcccdc..c6ff033593e9cb875bda4d80960cfaa24c81e11f 100644 |
--- a/base/android/java/src/org/chromium/base/ApplicationStatus.java |
+++ b/base/android/java/src/org/chromium/base/ApplicationStatus.java |
@@ -64,6 +64,13 @@ public class ApplicationStatus { |
private static ApplicationStateListener sNativeApplicationStateListener; |
/** |
+ * If true means we are running in 'webview' mode, see {@link #initializeForWebView} for |
+ * more details. |
+ */ |
+ private static boolean sWebViewMode = false; |
boliu
2015/10/01 16:59:02
+1 to a more contained name, instead of "isWebView
timvolodine
2015/10/02 14:55:09
went for sActivityLifecycleIndependentMode
|
+ private static Context sWrappedApplicationContext; |
boliu
2015/10/01 16:59:02
"Wrapped" is meaningless in base, so just sApplica
timvolodine
2015/10/02 14:55:09
Done.
|
+ |
+ /** |
* A map of which observers listen to state changes from which {@link Activity}. |
*/ |
private static final Map<Activity, ActivityInfo> sActivityInfo = |
@@ -109,28 +116,13 @@ public class ApplicationStatus { |
/** |
* Initializes the activity status for a specified application. |
+ * This method only registers for ActivityLifecycle changes. |
* |
* @param application The application whose status you wish to monitor. |
*/ |
- public static void initialize(BaseChromiumApplication application) { |
+ private static void initializeLifecycleCallbacks(Application application) { |
sApplication = application; |
- application.registerWindowFocusChangedListener( |
- new BaseChromiumApplication.WindowFocusChangedListener() { |
- @Override |
- public void onWindowFocusChanged(Activity activity, boolean hasFocus) { |
- if (!hasFocus || activity == sActivity) return; |
- |
- int state = getStateForActivity(activity); |
- |
- if (state != ActivityState.DESTROYED && state != ActivityState.STOPPED) { |
- sActivity = activity; |
- } |
- |
- // TODO(dtrainor): Notify of active activity change? |
- } |
- }); |
- |
application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { |
@Override |
public void onActivityCreated(final Activity activity, Bundle savedInstanceState) { |
@@ -168,6 +160,46 @@ public class ApplicationStatus { |
} |
/** |
+ * Initializes the activity status for a specified application. |
+ * |
+ * @param application The application whose status you wish to monitor. |
+ */ |
+ public static void initialize(BaseChromiumApplication application) { |
+ initializeLifecycleCallbacks(application); |
+ |
+ application.registerWindowFocusChangedListener( |
+ new BaseChromiumApplication.WindowFocusChangedListener() { |
+ @Override |
+ public void onWindowFocusChanged(Activity activity, boolean hasFocus) { |
+ if (!hasFocus || activity == sActivity) return; |
+ |
+ int state = getStateForActivity(activity); |
+ |
+ if (state != ActivityState.DESTROYED && state != ActivityState.STOPPED) { |
+ sActivity = activity; |
+ } |
+ |
+ // TODO(dtrainor): Notify of active activity change? |
+ } |
+ }); |
+ } |
+ |
+ /** |
+ * Initializes ApplicationStatus in WebView mode, which is different from the regular |
+ * initialization (@see initialize) in the following ways: |
+ * 1. We currently don't listen to focus changes (hence sActivity is always null). |
+ * 2. getApplicationContext returns wrapped application context. |
+ * 3. sActivityInfo does not have a particular update ordering because WebView can |
+ * be initialized at different times during activity lifecycle. |
+ */ |
+ public static void initializeForWebView(Application application, |
+ Context wrappedApplicationContext) { |
+ sWebViewMode = true; |
+ sWrappedApplicationContext = wrappedApplicationContext; |
+ initializeLifecycleCallbacks(application); |
+ } |
+ |
+ /** |
* Must be called by the main activity when it changes state. |
* |
* @param activity Current activity. |
@@ -176,18 +208,25 @@ public class ApplicationStatus { |
private static void onStateChange(Activity activity, int newState) { |
if (activity == null) throw new IllegalArgumentException("null activity is not supported"); |
- if (sActivity == null |
- || newState == ActivityState.CREATED |
- || newState == ActivityState.RESUMED |
- || newState == ActivityState.STARTED) { |
- sActivity = activity; |
- } |
- |
int oldApplicationState = getStateForApplication(); |
- if (newState == ActivityState.CREATED) { |
- assert !sActivityInfo.containsKey(activity); |
- sActivityInfo.put(activity, new ActivityInfo()); |
+ if (sWebViewMode) { |
+ assert sActivity == null; |
+ if (!sActivityInfo.containsKey(activity)) { |
+ sActivityInfo.put(activity, new ActivityInfo()); |
+ } |
+ } else { |
+ if (sActivity == null |
+ || newState == ActivityState.CREATED |
+ || newState == ActivityState.RESUMED |
+ || newState == ActivityState.STARTED) { |
+ sActivity = activity; |
+ } |
+ |
+ if (newState == ActivityState.CREATED) { |
+ assert !sActivityInfo.containsKey(activity); |
+ sActivityInfo.put(activity, new ActivityInfo()); |
+ } |
} |
// Invalidate the cached application state. |
@@ -233,8 +272,11 @@ public class ApplicationStatus { |
/** |
* @return The most recent focused {@link Activity} tracked by this class. Being focused means |
* out of all the activities tracked here, it has most recently gained window focus. |
+ * Note that in 'webview mode' the returned Activity is currently always null |
+ * (see crbug.com/538175). |
*/ |
public static Activity getLastTrackedFocusedActivity() { |
+ assert !sWebViewMode || sActivity == null; // sWebViewMode implies sActivity == null |
return sActivity; |
} |
@@ -253,7 +295,8 @@ public class ApplicationStatus { |
* @return The {@link Context} for the {@link Application}. |
*/ |
public static Context getApplicationContext() { |
- return sApplication != null ? sApplication.getApplicationContext() : null; |
+ if (sApplication == null) return null; |
+ return sWebViewMode ? sWrappedApplicationContext : sApplication.getApplicationContext(); |
boliu
2015/10/01 16:59:02
See, webview mode is already creeping in.
Chrome
timvolodine
2015/10/02 14:55:09
right, done.
|
} |
/** |