【Android-sensor】auto-rotation打开后应用无法自动转屏

本文分析了Android设备上自动转屏功能失效的问题,从HAL层到Java层逐步排查,涉及SensorService、DisplayRotation等关键组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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++

ASensorManager
SensorManager
ISensorManager
IInterface
ISensorServer
ThresholdSensor
ProximitySensor
ProximitySensorImpl
BinderService<SensorService>
SensorService
BnInterface<ISensorServer>
BnSensorServer
Thread

3.2 java

package android.view;

SensorEventListener
onSensorChanged()
onAccuracyChanged()
SensorEventListenerImpl
onSensorChanged()
onAccuracyChanged()
OrientationEventListener
SensorEventListenerImpl
OrientationEventListenerInternal
onOrientationChanged()
OrientationListener
OrientationEventListenerInternal
SensorListener

android\frameworks\base\services\core\java\com\android\server\wm\WindowOrientationListener.java
package com.android.server.wm;

WindowOrientationListener
OrientationJudge
AccelSensorJudge
OrientationSensorJudge
SensorEventListener
onSensorChanged()
onAccuracyChanged()
SensorEventListenerImpl
实现
onSensorChanged()
onAccuracyChanged()
OrientationJudge
AccelSensorJudge
OrientationSensorJudge
DisplayArea
getOrientation()
Dimmable
RootDisplayArea
DisplayContent
OrientationListener
UpdateRunnable
onProposedRotationChanged()
DisplayRotation
OrientationListener

OrientationJudge(implements)继承了SensorEventListener的两个接口,额外定义几个接口。
AccelSensorJudge和OrientationSensorJudge对接口进行实现。

OrientationEventListenerInternal对OrientationEventListener进行实例化,实现了其接口

WindowContainer
onConfigurationChanged()
DisplayRotation
rotationForOrientation()
WindowOrientationListener
onProposedRotationChanged()

4 小结

分析主要是从hal开始,查看hal上报数据是否正常。
hal正常后再分析native层的服务接收情况。
之后就是分析java层的windowmanager接收到sensorevent后的处理。

存疑点:
native层的SensorService.cpp到java层的windowmanager之间如何实现的,没细看,暂时不纠结这个了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值