This source file includes following definitions.
- onScreenOrientationChanged
- startListening
- stopListening
- startListening
- stopListening
- onConfigurationChanged
- onLowMemory
- SuppressLint
- startListening
- stopListening
- onDisplayAdded
- onDisplayRemoved
- onDisplayChanged
- getInstance
- injectConfigurationListenerBackendForTest
- addObserver
- removeObserver
- notifyObservers
- updateOrientation
package org.chromium.content.browser;
import android.annotation.SuppressLint;
import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManager.DisplayListener;
import android.os.Build;
import android.util.Log;
import android.view.Surface;
import android.view.WindowManager;
import com.google.common.annotations.VisibleForTesting;
import org.chromium.base.ObserverList;
import org.chromium.base.ThreadUtils;
import org.chromium.ui.gfx.DeviceDisplayInfo;
@VisibleForTesting
public class ScreenOrientationListener {
public interface ScreenOrientationObserver {
void onScreenOrientationChanged(int orientation);
}
private interface ScreenOrientationListenerBackend {
void startListening();
void stopListening();
}
private class ScreenOrientationConfigurationListener
implements ScreenOrientationListenerBackend, ComponentCallbacks {
@Override
public void startListening() {
mAppContext.registerComponentCallbacks(this);
}
@Override
public void stopListening() {
mAppContext.unregisterComponentCallbacks(this);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
notifyObservers();
}
@Override
public void onLowMemory() {
}
}
@SuppressLint("NewApi")
private class ScreenOrientationDisplayListener
implements ScreenOrientationListenerBackend, DisplayListener {
@Override
public void startListening() {
DisplayManager displayManager =
(DisplayManager) mAppContext.getSystemService(Context.DISPLAY_SERVICE);
displayManager.registerDisplayListener(this, null);
}
@Override
public void stopListening() {
DisplayManager displayManager =
(DisplayManager) mAppContext.getSystemService(Context.DISPLAY_SERVICE);
displayManager.unregisterDisplayListener(this);
}
@Override
public void onDisplayAdded(int displayId) {
}
@Override
public void onDisplayRemoved(int displayId) {
}
@Override
public void onDisplayChanged(int displayId) {
notifyObservers();
}
}
private static final String TAG = "ScreenOrientationListener";
private final ObserverList<ScreenOrientationObserver> mObservers =
new ObserverList<ScreenOrientationObserver>();
private int mOrientation;
private Context mAppContext;
private ScreenOrientationListenerBackend mBackend;
private static ScreenOrientationListener sInstance;
public static ScreenOrientationListener getInstance() {
ThreadUtils.assertOnUiThread();
if (sInstance == null) {
sInstance = new ScreenOrientationListener();
}
return sInstance;
}
private ScreenOrientationListener() {
mBackend = Build.VERSION.SDK_INT >= 17 ?
new ScreenOrientationDisplayListener() :
new ScreenOrientationConfigurationListener();
}
@VisibleForTesting
void injectConfigurationListenerBackendForTest() {
mBackend = new ScreenOrientationConfigurationListener();
}
public void addObserver(ScreenOrientationObserver observer, Context context) {
if (mAppContext == null) {
mAppContext = context.getApplicationContext();
}
assert mAppContext == context.getApplicationContext();
assert mAppContext != null;
if (!mObservers.addObserver(observer)) {
Log.w(TAG, "Adding an observer that is already present!");
return;
}
if (mObservers.size() == 1) {
updateOrientation();
mBackend.startListening();
}
final ScreenOrientationObserver obs = observer;
ThreadUtils.assertOnUiThread();
ThreadUtils.postOnUiThread(new Runnable() {
@Override
public void run() {
obs.onScreenOrientationChanged(mOrientation);
}
});
}
public void removeObserver(ScreenOrientationObserver observer) {
if (!mObservers.removeObserver(observer)) {
Log.w(TAG, "Removing an inexistent observer!");
return;
}
if (mObservers.isEmpty()) {
mBackend.stopListening();
}
}
private void notifyObservers() {
int previousOrientation = mOrientation;
updateOrientation();
DeviceDisplayInfo.create(mAppContext).updateNativeSharedDisplayInfo();
if (mOrientation == previousOrientation) {
return;
}
for (ScreenOrientationObserver observer : mObservers) {
observer.onScreenOrientationChanged(mOrientation);
}
}
private void updateOrientation() {
WindowManager windowManager =
(WindowManager) mAppContext.getSystemService(Context.WINDOW_SERVICE);
switch (windowManager.getDefaultDisplay().getRotation()) {
case Surface.ROTATION_0:
mOrientation = 0;
break;
case Surface.ROTATION_90:
mOrientation = 90;
break;
case Surface.ROTATION_180:
mOrientation = 180;
break;
case Surface.ROTATION_270:
mOrientation = -90;
break;
default:
throw new IllegalStateException(
"Display.getRotation() shouldn't return that value");
}
}
}