WindowOrientationListenerpublic 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. |
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.
this(context, handler, SensorManager.SENSOR_DELAY_UI);
| private WindowOrientationListener(android.content.Context context, android.os.Handler handler, int rate)Creates a new WindowOrientationListener.
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 boolean | canDetectOrientation()Returns true if sensor is enabled and false otherwise
synchronized (mLock) {
return mSensor != null;
}
| public void | disable()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 void | dump(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 void | enable()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 int | getProposedRotation()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.
synchronized (mLock) {
if (mEnabled) {
return mSensorEventListener.getProposedRotationLocked();
}
return -1;
}
| public abstract void | onProposedRotationChanged(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.
| public void | setCurrentRotation(int rotation)Sets the current rotation.
synchronized (mLock) {
mCurrentRotation = rotation;
}
|
|