一)Setting相关处理
Settings->display->auto rotate(自动旋转屏幕)
/packages/apps/Settings/res/values/strings.xml
<string name="accelerometer_title">Auto-rotate screen</string>
packages/apps/Settings/res/xml/display_settings.xml
<CheckBoxPreference
android:key="accelerometer"
android:title="@string/accelerometer_title"/>
packages/apps/Settings/src/com/android/settings/DisplaySettings.java
mAccelerometer.setChecked(Settings.System.getInt( if (preference == mAccelerometer) {
getContentResolver(), Settings.System.putInt(getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION, 0) != 0); Settings.System.ACCELEROMETER_ROTATION,
mAccelerometer.isChecked() ? 1 : 0);
Settings.System.ACCELEROMETER_ROTATION表示重力感应,横竖屏切换 }
二)自动转屏的实现
frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java
void observe() {
ContentResolver resolver = mContext.getContentResolver();
resolver.registerContentObserver(Settings.System.getUriFor(
Settings.System.END_BUTTON_BEHAVIOR), false, this);
resolver.registerContentObserver(Settings.Secure.getUriFor(
Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR), false, this);
resolver.registerContentObserver(Settings.System.getUriFor(
Settings.System.ACCELEROMETER_ROTATION), false, this);
int accelerometerDefault = Settings.System.getInt(resolver,
Settings.System.ACCELEROMETER_ROTATION, DEFAULT_ACCELEROMETER_ROTATION);
if (mAccelerometerDefault != accelerometerDefault) {
mAccelerometerDefault = accelerometerDefault;
updateOrientationListenerLp();
}
MyOrientationListener mOrientationListener;
packages/apps/Phone/src/com/android/phone/AccelerometerListener.java
private void onSensorEvent(double x, double y, double z) {
。。。
setOrientation(orientation);
}
2)加速度(accelerometer)是否就是重力感应
自动调整亮度说明
自动调整亮度,即:当阳光很强时,由于外界亮度大,需要加亮屏幕,才能看清,否则觉得屏幕昏暗;当外界光线很弱时,屏幕只需要低一点的亮度就够看清了,否则觉得刺眼。
要启动自动调整亮度,
1)首先,必须配置config.xml
在frameworks/base/core/res/res/values/config.xml里面的配置项config_automatic_brightness_available设置为true,缺省是false
2)在第一步设置之后,Settings->display->亮度设置,界面上会多出一个“自动调整亮度”选择框,如果选择框打勾,则表示真正启动了自动亮度调整
以下是自动亮度调整的代码跟踪
PowerManagerService.java
systemReady方法里面代码片段是
if (mUseSoftwareAutoBrightness) {
if (mDebugLightSensor)
Log.d(TAG, "mUseSoftwareAutoBrightness=true");
mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
enableLightSensor(true);
}
(mUseSoftwareAutoBrightness从frameworks/base/core/res/res/values/config.xml里面的配置项config_automatic_brightness_available获取,
如果为true,则调用enableLightSensor(true);
enableLightSensor函数会调用
mSensorManager.registerListener(mLightListener, mLightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
其中mSensorManager是PowerManagerService中的SensorManager对象成员变量,
mLightListener是注册的监听器类SensorEventListener对象,如果亮度变化,会触发其方法的调用,
mLightSensor是PowerManagerService中的LightSensor对象成员变量,代表光传感器。
SensorEventListener类定义如下
SensorEventListener mLightListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
synchronized (mLocks) {
// ignore light sensor while screen is turning off
if (isScreenTurningOffLocked()) {
Log.d(TAG, "ScreenTurningOffLocked, just return.");
return;
}
int value = (int)event.values[0]; //从event里面获取当前数据,亮度大概在0到二十几
long milliseconds = SystemClock.elapsedRealtime();
if (mDebugLightSensor) {
Log.d(TAG, "onSensorChanged: light value: " + value);
}
mHandler.removeCallbacks(mAutoBrightnessTask);
if (mLightSensorValue != value) { //mLightSensorValue是当前的屏幕亮度
if (mLightSensorValue == -1 ||
milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
// process the value immediately if screen has just turned on
lightSensorChangedLocked(value); //屏幕刚刚打开
} else {
// delay processing to debounce the sensor
mLightSensorPendingValue = value; //mLightSensorPendingValue是准备要设置的亮度
mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY); //在mAutoBrightnessTask任务里面延后处理
}
} else {
mLightSensorPendingValue = -1;
}
}
}
。。。
};
mAutoBrightnessTask这个AutoBrightnessTask线程的run方法里面调用
private void lightSensorChangedLocked(int value) //此value的就是即将要设置的亮度值
mScreenBrightness.setTargetLocked(lcdValue,
AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_SCREEN_BRIGHTNESS,
(int)mScreenBrightness.curValue)
然后启动mLightAnimator
说明,除了设置config_automatic_brightness_available为true之外,还必须设置其他一些数据项,否则运行时报异常
2124行getAutoBrightnessValue的return values[i];报错
参考$ANDROID_ROOT/device/htc/passion-common/overlay/frameworks/base/core/res/res/values/config.xml的内容,修改$ANDROID_ROOT/frameworks/base/core/res/res/values/config.xml
一共有以下几个配置项数据需要配置
config_autoBrightnessLevels -保存在PowerManagerService的mAutoBrightnessLevels变量中
config_autoBrightnessLcdBacklightValues --mLcdBacklightValues
config_autoBrightnessButtonBacklightValues <integer-array name="config_autoBrightnessLevels">
config_autoBrightnessKeyboardBacklightValues <item>200</item>
以上每个项里面有多个子项,用于提供给数组的数据。 <item>400</item>
设置完之后,运行时不报异常,打印日志如下: <item>1000</item>
<item>3000</item>
01-08 02:14:48.319: DEBUG/PowerManagerService(131): onSensorChanged: light value: 7 </integer-array>
01-08 02:14:50.322: DEBUG/PowerManagerService(131): lightSensorChangedLocked 7
01-08 02:14:50.322: DEBUG/PowerManagerService(131): lcdValue 25 <!-- Array of output values for LCD backlight corresponding to the LUX values
01-08 02:14:50.322: DEBUG/PowerManagerService(131): buttonValue 255 in the config_autoBrightnessLevels array. This array should have size one greater
01-08 02:14:50.322: DEBUG/PowerManagerService(131): keyboardValue 0 than the size of the config_autoBrightnessLevels array.
但看上去并没有根据外界亮度改变屏幕和键盘等亮度,所以代码还待分析。 This must be overridden in platform specific overlays -->
<integer-array name="config_autoBrightnessLcdBacklightValues">
重新分析LightSensorChangedLocked方法里面的代码片段 <item>25</item>
int lcdValue = getAutoBrightnessValue( <item>55</item>
(mIsDocked ? value : mHighestLightSensorValue), //第一个参数取二者的最大值 <item>70</item>
mLcdBacklightValues); <item>70</item>
<item>250</item>
</integer-array>
private int getAutoBrightnessValue(int sensorValue, int[] values) {
try {
int i;
for (i = 0; i < mAutoBrightnessLevels.length; i++) {
if (sensorValue < mAutoBrightnessLevels[i]) {
break;
}
}
return values[i];
} catch (Exception e) {
// guard against null pointer or index out of bounds errors
Slog.e(TAG, "getAutoBrightnessValue", e);
return 255;
}
}
此段代码的意思是,如果传入的sensorValue比config_autoBrightnessLevels的第i个值要小,则获得值是config_autoBrightnessLcdBacklightValues的第i个值
目前传入的sensorValue总是比config_autoBrightnessLevels的第一个值要小,所以每次lcdValue得到的值都是config_autoBrightnessLcdBacklightValues的第一个值
所以亮度一直都没实际变动。
前面根据日志,onSensorChanged方法里面每次获取当前亮度在0到20几之间,似乎和config_autoBrightnessLevels 的配置不协调,显得太小了。