+ - 20
+ - 80
+ - 120
+ - 160
+ - 250
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);
- }
@@ -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);
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];
- }
- }
- }
- }
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 --