0 问题
发现下拉栏的auto-rotation打开后,app无法实现自动转屏。
1 平台
sdm660 Android12
sensor hal1.0
2 代码调用
重点看native-hal以及java部分
(因为我native到java也没理清,jni就只看到一个文件)
JAVA
android\frameworks\base\services\core\java\com\android\server\wm\DisplayRotation.java
WindowOrientationListener.java
JNI
android\frameworks\base\services\core\jni\com_android_server_sensor_SensorService.cpp
Native
android\frameworks\native\services\sensorservice\SensorService.cpp
Hardware
android\hardware\interfaces\sensors\1.0\default\Sensors.cpp
HAL service
android\hardware\interfaces\sensors\1.0\default\android.hardware.sensors@1.0-service.rc
HAL
android\vendor\qcom\proprietary\sensors\dsps\libhalsensors\src\sensors_hal.cpp
2.1 hal-native
这里hal调用其实也还有点不确定,如果写的有误,欢迎指正
Sensor事件上报
Native
线程池循环执行poll
传入sensors_event_t的buffer指针,以及事件个数
Hal
2.2 java
android\frameworks\base\services\core\java\com\android\server\wm\DisplayRotation.java
OrientationListener继承自WindowOrientationListener
可以参考下文的类继承关系。
最终在DisplayRotation.updateRotationUnchecked中调用了rotationForOrientation函数
rotationForOrientation获取了sensor的角度
int sensorRotation = mOrientationListener != null
? mOrientationListener.getProposedRotation() // may be -1
: -1;
此外还会判断xml中的属性值。
if (mAllowAllRotations == ALLOW_ALL_ROTATIONS_UNDEFINED) {
// Can't read this during init() because the context doesn't have display metrics at
// that time so we cannot determine tablet vs. phone then.
mAllowAllRotations = mContext.getResources().getBoolean(
R.bool.config_allowAllRotations)
? ALLOW_ALL_ROTATIONS_ENABLED
: ALLOW_ALL_ROTATIONS_DISABLED;
}
属性路径:
android\frameworks\base\core\res\res\values\config.xml
3 常用类关系
3.1 C++
3.2 java
package android.view;
android\frameworks\base\services\core\java\com\android\server\wm\WindowOrientationListener.java
package com.android.server.wm;
OrientationJudge(implements)继承了SensorEventListener的两个接口,额外定义几个接口。
AccelSensorJudge和OrientationSensorJudge对接口进行实现。
OrientationEventListenerInternal对OrientationEventListener进行实例化,实现了其接口
4 小结
分析主要是从hal开始,查看hal上报数据是否正常。
hal正常后再分析native层的服务接收情况。
之后就是分析java层的windowmanager接收到sensorevent后的处理。
存疑点:
native层的SensorService.cpp到java层的windowmanager之间如何实现的,没细看,暂时不纠结这个了。