Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/ScreenOrientationListener.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/ScreenOrientationListener.java b/content/public/android/java/src/org/chromium/content/browser/ScreenOrientationListener.java |
| index 8529da24ed632bfc3548edc6d369e178f777565d..17604b2d7dc4882b8fa84bd9d903d3462c9ee2f4 100644 |
| --- a/content/public/android/java/src/org/chromium/content/browser/ScreenOrientationListener.java |
| +++ b/content/public/android/java/src/org/chromium/content/browser/ScreenOrientationListener.java |
| @@ -18,7 +18,10 @@ import org.chromium.base.Log; |
| import org.chromium.base.ObserverList; |
| import org.chromium.base.ThreadUtils; |
| import org.chromium.base.VisibleForTesting; |
| -import org.chromium.ui.gfx.DeviceDisplayInfo; |
| + |
| +import java.util.Collections; |
| +import java.util.Set; |
| +import java.util.WeakHashMap; |
| /** |
| * ScreenOrientationListener is a class that informs its observers when the |
| @@ -59,19 +62,6 @@ public class ScreenOrientationListener { |
| * when the last observer is removed. |
| */ |
| void stopListening(); |
| - |
| - /** |
| - * Toggle the accurate mode if it wasn't already doing so. The backend |
| - * will keep track of the number of times this has been called. |
| - */ |
| - void startAccurateListening(); |
| - |
| - /** |
| - * Request to stop the accurate mode. It will effectively be stopped |
| - * only if this method is called as many times as |
| - * startAccurateListening(). |
| - */ |
| - void stopAccurateListening(); |
| } |
| /** |
| @@ -89,46 +79,16 @@ public class ScreenOrientationListener { |
| private static final long POLLING_DELAY = 500; |
| - private int mAccurateCount = 0; |
| - |
| // ScreenOrientationListenerBackend implementation: |
| @Override |
| public void startListening() { |
| - mAppContext.registerComponentCallbacks(this); |
| + mContext.registerComponentCallbacks(this); |
| } |
| @Override |
| public void stopListening() { |
| - mAppContext.unregisterComponentCallbacks(this); |
| - } |
| - |
| - @Override |
| - public void startAccurateListening() { |
| - ++mAccurateCount; |
| - |
| - if (mAccurateCount > 1) return; |
| - |
| - // Start polling if we went from 0 to 1. The polling will |
| - // automatically stop when mAccurateCount reaches 0. |
| - final ScreenOrientationConfigurationListener self = this; |
| - ThreadUtils.postOnUiThreadDelayed(new Runnable() { |
| - @Override |
| - public void run() { |
| - self.onConfigurationChanged(null); |
| - |
| - if (self.mAccurateCount < 1) return; |
| - |
| - ThreadUtils.postOnUiThreadDelayed(this, |
| - ScreenOrientationConfigurationListener.POLLING_DELAY); |
| - } |
| - }, POLLING_DELAY); |
| - } |
| - |
| - @Override |
| - public void stopAccurateListening() { |
| - --mAccurateCount; |
| - assert mAccurateCount >= 0; |
| + mContext.unregisterComponentCallbacks(this); |
| } |
| // ComponentCallbacks implementation: |
| @@ -158,27 +118,17 @@ public class ScreenOrientationListener { |
| @Override |
| public void startListening() { |
| DisplayManager displayManager = |
| - (DisplayManager) mAppContext.getSystemService(Context.DISPLAY_SERVICE); |
| + (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE); |
| displayManager.registerDisplayListener(this, null); |
| } |
| @Override |
| public void stopListening() { |
| DisplayManager displayManager = |
| - (DisplayManager) mAppContext.getSystemService(Context.DISPLAY_SERVICE); |
| + (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE); |
| displayManager.unregisterDisplayListener(this); |
| } |
| - @Override |
| - public void startAccurateListening() { |
| - // Always accurate. Do nothing. |
| - } |
| - |
| - @Override |
| - public void stopAccurateListening() { |
| - // Always accurate. Do nothing. |
| - } |
| - |
| // DisplayListener implementation: |
| @Override |
| @@ -207,31 +157,22 @@ public class ScreenOrientationListener { |
| // starting to listen again. |
| private int mOrientation; |
| - // Current application context derived from the first context being received. |
| - private Context mAppContext; |
| + private final Context mContext; |
| private ScreenOrientationListenerBackend mBackend; |
| - private static ScreenOrientationListener sInstance; |
| - |
| - /** |
| - * Returns a ScreenOrientationListener implementation based on the device's |
| - * supported API level. |
| - */ |
| - public static ScreenOrientationListener getInstance() { |
| - ThreadUtils.assertOnUiThread(); |
| + private static Set<ScreenOrientationListener> sListeners = |
| + Collections.newSetFromMap(new WeakHashMap<ScreenOrientationListener, Boolean>()); |
|
mlamouri (slow - plz ping)
2015/07/20 13:18:03
nit: could you rename + add comment. These are not
|
| + private static int sAccurateCount = 0; |
| - if (sInstance == null) { |
| - sInstance = new ScreenOrientationListener(); |
| - } |
| - |
| - return sInstance; |
| - } |
| - |
| - private ScreenOrientationListener() { |
| + public ScreenOrientationListener(Context context) { |
| + mContext = context; |
| mBackend = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 |
| ? new ScreenOrientationDisplayListener() |
| : new ScreenOrientationConfigurationListener(); |
| + |
| + // Only add a listener for polling if it is not already accurate enough |
| + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) sListeners.add(this); |
| } |
| /** |
| @@ -240,16 +181,8 @@ public class ScreenOrientationListener { |
| * orientation value. |
| * |
| * @param observer The observer that will get notified. |
| - * @param context The context associated with this observer. |
| */ |
| - public void addObserver(ScreenOrientationObserver observer, Context context) { |
| - if (mAppContext == null) { |
| - mAppContext = context.getApplicationContext(); |
| - } |
| - |
| - assert mAppContext == context.getApplicationContext(); |
| - assert mAppContext != null; |
| - |
| + public void addObserver(ScreenOrientationObserver observer) { |
| if (!mObservers.addObserver(observer)) { |
| Log.w(TAG, "Adding an observer that is already present!"); |
| return; |
| @@ -291,22 +224,6 @@ public class ScreenOrientationListener { |
| } |
| /** |
| - * Toggle the accurate mode if it wasn't already doing so. The backend will |
| - * keep track of the number of times this has been called. |
| - */ |
| - public void startAccurateListening() { |
| - mBackend.startAccurateListening(); |
| - } |
| - |
| - /** |
| - * Request to stop the accurate mode. It will effectively be stopped only if |
| - * this method is called as many times as startAccurateListening(). |
| - */ |
| - public void stopAccurateListening() { |
| - mBackend.stopAccurateListening(); |
| - } |
| - |
| - /** |
| * This should be called by classes extending ScreenOrientationListener when |
| * it is possible that there is a screen orientation change. If there is an |
| * actual change, the observers will get notified. |
| @@ -319,8 +236,6 @@ public class ScreenOrientationListener { |
| return; |
| } |
| - DeviceDisplayInfo.create(mAppContext).updateNativeSharedDisplayInfo(); |
| - |
| for (ScreenOrientationObserver observer : mObservers) { |
| observer.onScreenOrientationChanged(mOrientation); |
| } |
| @@ -331,7 +246,7 @@ public class ScreenOrientationListener { |
| */ |
| private void updateOrientation() { |
| WindowManager windowManager = |
| - (WindowManager) mAppContext.getSystemService(Context.WINDOW_SERVICE); |
| + (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); |
| switch (windowManager.getDefaultDisplay().getRotation()) { |
| case Surface.ROTATION_0: |
| @@ -351,4 +266,29 @@ public class ScreenOrientationListener { |
| "Display.getRotation() shouldn't return that value"); |
| } |
| } |
| + |
| + public static void startAccurateListening() { |
| + sAccurateCount++; |
| + if (sAccurateCount > 1) return; |
| + |
| + ThreadUtils.postOnUiThreadDelayed(new Runnable() { |
| + @Override |
| + public void run() { |
| + for (ScreenOrientationListener listener : sListeners) { |
| + if (listener != null) listener.notifyObservers(); |
| + } |
| + |
| + if (sAccurateCount < 1) return; |
| + |
| + ThreadUtils.postOnUiThreadDelayed( |
| + this, ScreenOrientationConfigurationListener.POLLING_DELAY); |
| + } |
| + }, ScreenOrientationConfigurationListener.POLLING_DELAY); |
| + } |
| + |
| + public static void stopAccurateListening() { |
| + sAccurateCount--; |
| + if (sAccurateCount > 0) return; |
| + assert sAccurateCount == 0; |
| + } |
| } |