Index: base/android/java/src/org/chromium/base/BaseChromiumApplication.java |
diff --git a/base/android/java/src/org/chromium/base/BaseChromiumApplication.java b/base/android/java/src/org/chromium/base/BaseChromiumApplication.java |
index 97b2ab49a803293c50911add5a6bc8891ea5a650..c921dd9c814ef0449b55c8a41cdbb6346c5e259c 100644 |
--- a/base/android/java/src/org/chromium/base/BaseChromiumApplication.java |
+++ b/base/android/java/src/org/chromium/base/BaseChromiumApplication.java |
@@ -4,17 +4,96 @@ |
package org.chromium.base; |
+import android.app.Activity; |
import android.app.Application; |
+import android.os.Bundle; |
+import android.view.Window; |
/** |
* Basic application functionality that should be shared among all browser applications. |
*/ |
public class BaseChromiumApplication extends Application { |
+ /** |
+ * Interface to be implemented by listeners for window focus events. |
+ */ |
+ public interface WindowFocusChangedListener { |
+ /** |
+ * Called when the window focus changes for {@code activity}. |
+ * @param activity The {@link Activity} that has a window focus changed event. |
+ * @param hasFocus Whether or not {@code activity} gained or lost focus. |
+ */ |
+ public void onWindowFocusChanged(Activity activity, boolean hasFocus); |
+ } |
+ |
+ private ObserverList<WindowFocusChangedListener> sWindowFocusListeners = |
+ new ObserverList<WindowFocusChangedListener>(); |
@Override |
public void onCreate() { |
super.onCreate(); |
- ActivityStatus.initialize(this); |
+ ApplicationStatus.initialize(this); |
+ |
+ registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { |
+ @Override |
+ public void onActivityCreated(final Activity activity, Bundle savedInstanceState) { |
+ Window.Callback callback = activity.getWindow().getCallback(); |
+ activity.getWindow().setCallback(new WindowCallbackWrapper(callback) { |
+ @Override |
+ public void onWindowFocusChanged(boolean hasFocus) { |
+ super.onWindowFocusChanged(hasFocus); |
+ |
+ for (WindowFocusChangedListener listener : sWindowFocusListeners) { |
+ listener.onWindowFocusChanged(activity, hasFocus); |
+ } |
+ } |
+ }); |
+ } |
+ |
+ @Override |
+ public void onActivityDestroyed(Activity activity) { |
+ assert activity.getWindow().getCallback() instanceof WindowCallbackWrapper; |
+ } |
+ |
+ @Override |
+ public void onActivityPaused(Activity activity) { |
+ assert activity.getWindow().getCallback() instanceof WindowCallbackWrapper; |
+ } |
+ |
+ @Override |
+ public void onActivityResumed(Activity activity) { |
+ assert activity.getWindow().getCallback() instanceof WindowCallbackWrapper; |
+ } |
+ |
+ @Override |
+ public void onActivitySaveInstanceState(Activity activity, Bundle outState) { |
+ assert activity.getWindow().getCallback() instanceof WindowCallbackWrapper; |
+ } |
+ |
+ @Override |
+ public void onActivityStarted(Activity activity) { |
+ assert activity.getWindow().getCallback() instanceof WindowCallbackWrapper; |
+ } |
+ |
+ @Override |
+ public void onActivityStopped(Activity activity) { |
+ assert activity.getWindow().getCallback() instanceof WindowCallbackWrapper; |
+ } |
+ }); |
} |
+ /** |
+ * Registers a listener to receive window focus updates on activities in this application. |
+ * @param listener Listener to receive window focus events. |
+ */ |
+ public void registerWindowFocusChangedListener(WindowFocusChangedListener listener) { |
+ sWindowFocusListeners.addObserver(listener); |
+ } |
+ |
+ /** |
+ * Unregisters a listener from receiving window focus updates on activities in this application. |
+ * @param listener Listener that doesn't want to receive window focus events. |
+ */ |
+ public void unregisterWindowFocusChangedListener(WindowFocusChangedListener listener) { |
+ sWindowFocusListeners.removeObserver(listener); |
+ } |
} |