一 概述
frameworks\base\core\java\android\view\WindowOrientationListener.java |
监听sensor是否有数据变化 |
首先看看芯片方向(x,y,z):
补充流程:
PhoneWindowManager调用updateOrientationListenerLp去SensorManager里面去注册一个Listener,
当Sensor发生变化的时候,PhoneWindowManager就可以监听到并且调用回调onProposeRotationChanged。
Callback 如下:
1 MyOrientationListener.onProposeRotationChange PhoneWindowManager.java
-------------》
class MyOrientationListener extends WindowOrientationListener {
@Override
public void onProposedRotationChanged(introtation) {
if (localLOGV) Log.v(TAG,"onProposedRotationChanged, rotation=" + rotation);
updateRotation(false);
}
}
--------》
2. PhoneWindowManager.updateRotation(booleanalwaysSendConfiguration)
void updateRotation(booleanalwaysSendConfiguration) {
try {
//set orientation on WindowManager
mWindowManager.updateRotation(alwaysSendConfiguration, false);、、WindowManagerService的对象
} catch (RemoteException e) {
// Ignore
}
}
3. WindowManagerService.updateRotation(booleanalwaysSendConfiguration, boolean forceRelayout)
二代码分析
public void onSensorChanged(SensorEvent event) {
// The vector given in theSensorEvent points straight up (towards the sky) under ideal
// conditions (the phone is notaccelerating). I'll call this up vectorelsewhere.
float x =event.values[ACCELEROMETER_DATA_X];
float y =event.values[ACCELEROMETER_DATA_Y];
float z =event.values[ACCELEROMETER_DATA_Z];
if (LOG) {
Slog.v(TAG, "Rawacceleration vector: "
+ "x=" + x +", y=" + y + ", z=" + z
+ ",magnitude=" + FloatMath.sqrt(x * x + y * y + z * z));
}
// Apply a low-pass filter to theacceleration up vector in cartesian space.
// Reset the orientation listenerstate if the samples are too far apart in time
// or when we see values of (0, 0,0) which indicates that we polled the
// accelerometer too soon afterturning it on and we don't have any data yet.
final long now = event.timestamp;
final long then =mLastFilteredTimestampNanos;
final float timeDeltaMS = (now -then) * 0.000001f;
final boolean skipSample;
if (now < then
|| now > then +MAX_FILTER_DELTA_TIME_NANOS
|| (x == 0 && y ==0 && z == 0)) {
if (LOG) {
Slog.v(TAG, "Resettingorientation listener.");
}
reset();
skipSample = true;
} else {
final float alpha = timeDeltaMS/ (FILTER_TIME_CONSTANT_MS + timeDeltaMS);
x = alpha * (x -mLastFilteredX) + mLastFilte