FileDocCategorySizeDatePackage
WindowOrientationListener.javaAPI DocAndroid 5.1 API37687Thu Mar 12 22:22:42 GMT 2015com.android.internal.policy.impl

WindowOrientationListener

public abstract class WindowOrientationListener extends Object
A special helper class used by the WindowManager for receiving notifications from the SensorManager when the orientation of the device has changed. NOTE: If changing anything here, please run the API demo "App/Activity/Screen Orientation" to ensure that all orientation modes still work correctly. You can also visualize the behavior of the WindowOrientationListener. Refer to frameworks/base/tools/orientationplot/README.txt for details.
hide

Fields Summary
private static final String
TAG
private static final boolean
LOG
private static final boolean
USE_GRAVITY_SENSOR
private android.os.Handler
mHandler
private android.hardware.SensorManager
mSensorManager
private boolean
mEnabled
private int
mRate
private android.hardware.Sensor
mSensor
private SensorEventListenerImpl
mSensorEventListener
private int
mCurrentRotation
private final Object
mLock
Constructors Summary
public WindowOrientationListener(android.content.Context context, android.os.Handler handler)
Creates a new WindowOrientationListener.

param
context for the WindowOrientationListener.
param
handler Provides the Looper for receiving sensor updates.


                            
         
        this(context, handler, SensorManager.SENSOR_DELAY_UI);
    
private WindowOrientationListener(android.content.Context context, android.os.Handler handler, int rate)
Creates a new WindowOrientationListener.

param
context for the WindowOrientationListener.
param
handler Provides the Looper for receiving sensor updates.
param
rate at which sensor events are processed (see also {@link android.hardware.SensorManager SensorManager}). Use the default value of {@link android.hardware.SensorManager#SENSOR_DELAY_NORMAL SENSOR_DELAY_NORMAL} for simple screen orientation change detection. This constructor is private since no one uses it.

        mHandler = handler;
        mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        mRate = rate;
        mSensor = mSensorManager.getDefaultSensor(USE_GRAVITY_SENSOR
                ? Sensor.TYPE_GRAVITY : Sensor.TYPE_ACCELEROMETER);
        if (mSensor != null) {
            // Create listener only if sensors do exist
            mSensorEventListener = new SensorEventListenerImpl();
        }
    
Methods Summary
public booleancanDetectOrientation()
Returns true if sensor is enabled and false otherwise

        synchronized (mLock) {
            return mSensor != null;
        }
    
public voiddisable()
Disables the WindowOrientationListener.

        synchronized (mLock) {
            if (mSensor == null) {
                Log.w(TAG, "Cannot detect sensors. Invalid disable");
                return;
            }
            if (mEnabled == true) {
                if (LOG) {
                    Log.d(TAG, "WindowOrientationListener disabled");
                }
                mSensorManager.unregisterListener(mSensorEventListener);
                mEnabled = false;
            }
        }
    
public voiddump(java.io.PrintWriter pw, java.lang.String prefix)

        synchronized (mLock) {
            pw.println(prefix + TAG);
            prefix += "  ";
            pw.println(prefix + "mEnabled=" + mEnabled);
            pw.println(prefix + "mCurrentRotation=" + mCurrentRotation);
            pw.println(prefix + "mSensor=" + mSensor);
            pw.println(prefix + "mRate=" + mRate);

            if (mSensorEventListener != null) {
                mSensorEventListener.dumpLocked(pw, prefix);
            }
        }
    
public voidenable()
Enables the WindowOrientationListener so it will monitor the sensor and call {@link #onProposedRotationChanged(int)} when the device orientation changes.

        synchronized (mLock) {
            if (mSensor == null) {
                Log.w(TAG, "Cannot detect sensors. Not enabled");
                return;
            }
            if (mEnabled == false) {
                if (LOG) {
                    Log.d(TAG, "WindowOrientationListener enabled");
                }
                mSensorEventListener.resetLocked();
                mSensorManager.registerListener(mSensorEventListener, mSensor, mRate, mHandler);
                mEnabled = true;
            }
        }
    
public intgetProposedRotation()
Gets the proposed rotation. This method only returns a rotation if the orientation listener is certain of its proposal. If the rotation is indeterminate, returns -1.

return
The proposed rotation, or -1 if unknown.

        synchronized (mLock) {
            if (mEnabled) {
                return mSensorEventListener.getProposedRotationLocked();
            }
            return -1;
        }
    
public abstract voidonProposedRotationChanged(int rotation)
Called when the rotation view of the device has changed. This method is called whenever the orientation becomes certain of an orientation. It is called each time the orientation determination transitions from being uncertain to being certain again, even if it is the same orientation as before.

param
rotation The new orientation of the device, one of the Surface.ROTATION_* constants.
see
android.view.Surface

public voidsetCurrentRotation(int rotation)
Sets the current rotation.

param
rotation The current rotation.

        synchronized (mLock) {
            mCurrentRotation = rotation;
        }