螢幕亮度自動調節:
主要是從Sensor分析之中分離出來分析LIGHT 光線感應器,因此就分析一下自動調節螢幕亮度(手機隨著光線的強度自我調節,也就是在亮的光線下螢幕自動調亮一些,暗的時候就自動調暗一些,省得光線對眼睛有刺激).....
(本人從曆經挫折才大概瞭解流程),現在就開始講一下流程,如果有不對地方希望牛人指出來。。。
先從Sttings入手吧,在diaplay中有螢幕亮度調節,有一個進度調,上面有一個checkbox(自動調節螢幕亮度的),
那麼我們當然去找settings下的檔案開始看了,先找到
DiaplaySettings.java檔案以及display_setting.xml檔案,display_setting.xml裡面能找到相應的組件,但是 在DiaplaySettings.java檔案中卻不能找到調用相應組件的代碼,
那麼我們再到settings中去找,能找到BrightnessPreference.java檔案,沒錯這個檔案就是自動調節亮度的檔案,我們先看看代碼:
建構函式中有mAutomaticAvailable = context.getResources().getBoolean(com.android.internal.R.bool.config_automatic_brightness_available);
開始以為mAutomaticAvailabl的值(為true,在config.xml中)是判斷是否自動調節亮度,因為在我跟進到PowerManagerService.java中,在initInThread() 中
mUseSoftwareAutoBrightness = resources.getBoolean(
com.android.internal.R.bool.config_automatic_brightness_available);
if (mUseSoftwareAutoBrightness) {
mAutoBrightnessLevels = resources.getIntArray(
com.android.internal.R.array.config_autoBrightnessLevels);//得到自動化調節的值調用 getAutoBrightnessValue(int sensorValue, int[] values) 方法
mLcdBacklightValues = resources.getIntArray(
com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
mButtonBacklightValues = resources.getIntArray(
com.android.internal.R.array.config_autoBrightnessButtonBacklightValues);
mKeyboardBacklightValues = resources.getIntArray(。。。。。
。。。。。。這裡看起來好像是當mUseSoftwareAutoBrightness為true時就取得config.xml檔案config_autoBrightnessLevels的值,再去根據光線自動調節,但是mAutomaticAvailabl的值只是說明有沒有自動調節螢幕的功能(比如說你將config.xml檔案的config_automatic_brightness_available的值改為false,編譯替換framework-res.apk那麼在螢幕亮度調節中將沒有自動調節的checkbox),這也能解釋建構函式一開始就初始化mAutomaticAvailabl的值。
接下來在BrightnessPreference.java檔案中有onProgressChanged,onStartTrackingTouch(SeekBar seekBar),onStopTrackingTouch(SeekBar seekBar)方法,這些一看也能懂就不介紹了,在onBindDialogView(View view)方法中有
mCheckBox = (CheckBox)view.findViewById(R.id.automatic_mode);
if (mAutomaticAvailable) {//當有自動調節功能
mCheckBox.setOnCheckedChangeListener(this);//為checkbox添加監聽事件
try {
mOldAutomatic = Settings.System.getInt(getContext().getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE);
。。。。。。
主要看public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setMode(isChecked ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
: Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
if (!isChecked) {
setBrightness(mSeekBar.getProgress() + MINIMUM_BACKLIGHT);
}
}
這裡才是你選擇自動調節亮度的checkbox,如果選擇則setMode(Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);並將資料存放到資料庫中。
這裡是重點:這裡有個資料庫,當監測到資料庫中的值發生改變時也就是變為Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC時,就會跳到
powerManagerService.java的initInThread()中ContentResolver resolver = mContext.getContentResolver();
Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null,
"(" + Settings.System.NAME + "=?) or ("
+ Settings.System.NAME + "=?) or ("
+ Settings.System.NAME + "=?) or ("
+ Settings.System.NAME + "=?) or ("
+ Settings.System.NAME + "=?) or ("
+ Settings.System.NAME + "=?) or ("
+ Settings.System.NAME + "=?)",
new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN, XEC_DLS_CONTROL,
SCREEN_BRIGHTNESS_MODE, WINDOW_ANIMATION_SCALE, TRANSITION_ANIMATION_SCALE},
null);
mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler);
SettingsObserver settingsObserver = new SettingsObserver();
mSettings.addObserver(settingsObserver);
資料庫中資料發生改變時觸發事件的發生。
在 powerManagerService.java的systemReady()(只在開機調用一次就不再調用)中
if (mUseSoftwareAutoBrightness) {
Log.i("frist","frist="+"aaaa");
mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
enableLightSensor(true);
}
而mUseSoftwareAutoBrightness是在initInThread()線程中取得了true值,那麼就到enableLightSensor(true);方法去看
if (enable) {
mSensorManager.registerListener(mLightListener, mLightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
如果傳遞來的值為true也就是enable=true就監聽而且用匹配螢幕方向變化的感應SENSOR_DELAY_NORMAL。
再看mLightListener事件:SensorEventListener mLightListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
synchronized (mLocks) {
int value = (int)event.values[0];//從event裡面擷取當前資料
Log.i("value","value="+value);
long milliseconds = SystemClock.elapsedRealtime();
if (mDebugLightSensor) {
Slog.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;
}
}
}
當前螢幕亮度肯定不等與-1,所以做else中的mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY),那麼我們再到mAutoBrightnessTask
線程看:private Runnable mAutoBrightnessTask = new Runnable() {
public void run() {
synchronized (mLocks) {
int value = (int)mLightSensorPendingValue;
Log.i("mLightSensorPendingValue","mLightSensorPendingValue"+mLightSensorPendingValue);
if (value >= 0) {
mLightSensorPendingValue = -1;
lightSensorChangedLocked(value);
}
}
}
};就是不斷的去調用lightSensorChangedLocked(value)
private void lightSensorChangedLocked(int value) {
。。。。。。。
if (mLightSensorValue != value) {
mLightSensorValue = value;
if ((mPowerState & BATTERY_LOW_BIT) == 0) {
// Use light sensor value no matter it is in a dock or not.
int lcdValue = getAutoBrightnessValue(
value,
mLcdBacklightValues);// 取得mLcdBacklightValues裡面的值
int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues);
int keyboardValue;
if (mKeyboardVisible) {
keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues);
} else {
keyboardValue = 0;
}
。。。
if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) {//設定螢幕亮度
mScreenBrightness.setTargetLocked(lcdValue, AUTOBRIGHTNESS_ANIM_STEPS,
INITIAL_SCREEN_BRIGHTNESS, (int)mScreenBrightness.curValue);
}
。。。。。
}