android light sensor port
基于android 2.2 添加Light Sensor支持:
1) UI部分
2) framework部分
3) HAL 部分
4) Driver 部分
下面分别讨论:
1) UI部分
a) 打开亮度设置菜单的自动调整亮度checkbox
frameworks/base/core/res/res/values/config.xml :
- <bool name="config_automatic_brightness_available">false</bool>
+ <bool name="config_automatic_brightness_available">true</bool>
b) 同时设置Lux级,亮度级,以5级亮度为例
<integer-array name="config_autoBrightnessLevels">
+ <item>1000</item>
+ <item>2000</item>
+ <item>3000</item>
+ <item>4000</item>
</integer-array>
<integer-array name="config_autoBrightnessLcdBacklightValues">
+ <item>20</item>
+ <item>80</item>
+ <item>120</item>
+ <item>160</item>
+ <item>250</item>
</integer-array>
autoBrightnessLevels 随便设,HAL层传上来的数据落在这几个值之间以区分级别
LcdBacklightValues 是5级亮度的实际值
2) framework部分
a) framework层的相关文件
./core/java/android/hardware/SensorEvent.java
./core/java/android/hardware/SensorListener.java
./core/java/android/hardware/ISensorService.aidl
./core/java/android/hardware/Sensor.java
./core/java/android/hardware/SensorEventListener.java
./core/jni/android_hardware_SensorManager.cpp
//SensorManager JNI
./core/java/android/hardware/SensorManager.java //SensorManager
./services/jni/com_android_server_SensorService.cpp //SensorSersvice JNI
./services/java/com/android/server/SensorService.java //SensorService
./services/java/com/android/server/PowerManagerService.java
Sensor系统相对比较简单,我们需要修改的更少:
添加Light Sensor只需要修改SensorManager.java, PowerManagerService.java两个文件
b) SensorManager.java:
- @@ -582,6 +582,8 @@ public class SensorManager
- return SENSOR_ORIENTATION_RAW;
- case Sensor.TYPE_TEMPERATURE:
- return SENSOR_TEMPERATURE;
- + case Sensor.TYPE_LIGHT:
- + return SENSOR_LIGHT;
- }
- return 0;
- }
- @@ -606,6 +608,9 @@ public class SensorManager
- result |= SensorManager.SENSOR_ORIENTATION |
- SensorManager.SENSOR_ORIENTATION_RAW;
- break;
- + case Sensor.TYPE_LIGHT:
- + result |= SensorManager.SENSOR_LIGHT;
- + break;
- }
- }
- return result;
- @@ -707,6 +712,8 @@ public class SensorManager
- listener, sensors, rate) || result;
- result = registerLegacyListener(SENSOR_TEMPERATURE, Sensor.TYPE_TEMPERATURE,
- listener, sensors, rate) || result;
- + result = registerLegacyListener(SENSOR_LIGHT, Sensor.TYPE_LIGHT,
- + listener, sensors, rate) || result;
- return result;
- }
- @@ -766,6 +773,8 @@ public class SensorManager
- listener, sensors);
- unregisterLegacyListener(SENSOR_TEMPERATURE, Sensor.TYPE_TEMPERATURE,
- listener, sensors);
- + unregisterLegacyListener(SENSOR_LIGHT, Sensor.TYPE_LIGHT,
- + listener, sensors);
- }
c) PowerManagerService.java:
这里似乎一个系统bug
- int lcdValue = getAutoBrightnessValue(
- - (mIsDocked ? value : mHighestLightSensorValue),
- - mLcdBacklightValues);
- + (true ? value : mHighestLightSensorValue),
- + mLcdBacklightValues);
3) HAL 部分
实现hardware/libhardware/include/hardware/sensor.h接口,写一个HAL层,
这部分请参考我上一篇文章: android sensor HAL范例,那个例子写的好级啦
需要说明的一点是,android系统的所有sensor公用一个HAL层文件,所以如果你的系统已有其他sensor那么需要在其他
sensor的HAL实现中添加light sensor
HAL层最终生成 /system/lib/hw/sensors.xxx.so
4) Driver 部分
不管啥接口的light sensor,请把它写成一个input设备,
通过/dev/input/eventX来提供数据
这样在HAL层polling的时候就可以
nread = read(ls_fd, &event, sizeof(event));
- if (event->type == EV_ABS) {
- LOGV("light-level type: %d code: %d value: %-5d time: %ds",
- event->type, event->code, event->value,
- (int)event->time.tv_sec);
- if (event->code == EVENT_TYPE_LIGHT) {
- struct input_absinfo absinfo;
- int index;
- if (!ioctl(fd, EVIOCGABS(ABS_DISTANCE), &absinfo)) {
- index = event->value;
- if (index >= 0) {
- new_sensors |= SENSORS_LIGHT;
- if (index >= ARRAY_SIZE(sLuxValues)) {
- index = ARRAY_SIZE(sLuxValues) - 1;
- }
- dev->sensors[ID_L].light = sLuxValues[index];
- }
- }
- }
- }
.light 最终传给 PowerManager, PowerManager根据config_autoBrightnessLevels数组
得到config_autoBrightnessLcdBacklightValues数据具体亮度值,然后调整亮度
比如 .light = 2500.0
3000.0 < 2500.0 < 4000.0 对应的LcdBacklightValues 就是160
-- THE END --