一、初始化信息
首先看看Android中的幾種電源狀態定義,這幾個值定義了不同的led開閉狀態以及lcd背光的不同亮度模式:
- // flags for setPowerState
- private static final int SCREEN_ON_BIT = 0x00000001;
- private static final int SCREEN_BRIGHT_BIT = 0x00000002;
- private static final int BUTTON_BRIGHT_BIT = 0x00000004;
- private static final int KEYBOARD_BRIGHT_BIT = 0x00000008;
- private static final int BATTERY_LOW_BIT = 0x00000010;
- // values for setPowerState
- // SCREEN_OFF == everything off
- private static final int SCREEN_OFF = 0x00000000;
- // SCREEN_DIM == screen on, screen backlight dim
- private static final int SCREEN_DIM = SCREEN_ON_BIT;
- // SCREEN_BRIGHT == screen on, screen backlight bright
- private static final int SCREEN_BRIGHT = SCREEN_ON_BIT | SCREEN_BRIGHT_BIT;
- // SCREEN_BUTTON_BRIGHT == screen on, screen and button backlights bright
- private static final int SCREEN_BUTTON_BRIGHT = SCREEN_BRIGHT | BUTTON_BRIGHT_BIT;
- // SCREEN_BUTTON_BRIGHT == screen on, screen, button and keyboard backlights bright
- private static final int ALL_BRIGHT = SCREEN_BUTTON_BRIGHT | KEYBOARD_BRIGHT_BIT;
- // used for noChangeLights in setPowerState()
- private static final int LIGHTS_MASK = SCREEN_BRIGHT_BIT | BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT;
// flags for setPowerState
private static final int SCREEN_ON_BIT = 0x00000001;
private static final int SCREEN_BRIGHT_BIT = 0x00000002;
private static final int BUTTON_BRIGHT_BIT = 0x00000004;
private static final int KEYBOARD_BRIGHT_BIT = 0x00000008;
private static final int BATTERY_LOW_BIT = 0x00000010;
// values for setPowerState
// SCREEN_OFF == everything off
private static final int SCREEN_OFF = 0x00000000;
// SCREEN_DIM == screen on, screen backlight dim
private static final int SCREEN_DIM = SCREEN_ON_BIT;
// SCREEN_BRIGHT == screen on, screen backlight bright
private static final int SCREEN_BRIGHT = SCREEN_ON_BIT | SCREEN_BRIGHT_BIT;
// SCREEN_BUTTON_BRIGHT == screen on, screen and button backlights bright
private static final int SCREEN_BUTTON_BRIGHT = SCREEN_BRIGHT | BUTTON_BRIGHT_BIT;
// SCREEN_BUTTON_BRIGHT == screen on, screen, button and keyboard backlights bright
private static final int ALL_BRIGHT = SCREEN_BUTTON_BRIGHT | KEYBOARD_BRIGHT_BIT;
// used for noChangeLights in setPowerState()
private static final int LIGHTS_MASK = SCREEN_BRIGHT_BIT | BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT;
在frameworks/base/core/res/res/values/config.xml中配置自動背光的總開關mUseSoftwareAutoBrightness以及自動背光模式下的背光數值,如果沒有lightsensor用於自動背光調整,則需要將mUseSoftwareAutoBrightness配置爲false,配置項如下:
- <bool name="config_automatic_brightness_available">true</bool>
- <!-- Array of light sensor LUX values to define our levels for auto backlight brightness support.
- The N entries of this array define N + 1 zones as follows:
- Zone 0: 0 <= LUX < array[0]
- Zone 1: array[0] <= LUX < array[1]
- ...
- Zone N: array[N - 1] <= LUX < array[N]
- Zone N + 1: array[N] <= LUX < infinity
- Must be overridden in platform specific overlays -->
- <integer-array name="config_autoBrightnessLevels">
- <item>3</item>
- <item>10</item>
- <item>40</item>
- <item>65</item>
- <item>145</item>
- <item>300</item>
- <item>550</item>
- <item>930</item>
- <item>1250</item>
- </integer-array>
- <!-- Array of output values for LCD backlight corresponding to the LUX values
- in the config_autoBrightnessLevels array. This array should have size one greater
- than the size of the config_autoBrightnessLevels array.
- This must be overridden in platform specific overlays -->
- <integer-array name="config_autoBrightnessLcdBacklightValues">
- <item>101</item>
- <item>117</item>
- <item>142</item>
- <item>183</item>
- <item>195</item>
- <item>216</item>
- <item>234</item>
- <item>245</item>
- <item>250</item>
- <item>250</item>
- </integer-array>
- <!-- Array of output values for button backlight corresponding to the LUX values
- in the config_autoBrightnessLevels array. This array should have size one greater
- than the size of the config_autoBrightnessLevels array.
- This must be overridden in platform specific overlays -->
- <integer-array name="config_autoBrightnessButtonBacklightValues">
- <item>255</item>
- <item>255</item>
- <item>255</item>
- <item>0</item>
- <item>0</item>
- <item>0</item>
- <item>0</item>
- <item>0</item>
- <item>0</item>
- <item>0</item>
- </integer-array>
- <!-- Array of output values for keyboard backlight corresponding to the LUX values
- in the config_autoBrightnessLevels array. This array should have size one greater
- than the size of the config_autoBrightnessLevels array.
- This must be overridden in platform specific overlays -->
- <integer-array name="config_autoBrightnessKeyboardBacklightValues">
- </integer-array>
<bool name="config_automatic_brightness_available">true</bool>
<!-- Array of light sensor LUX values to define our levels for auto backlight brightness support.
The N entries of this array define N + 1 zones as follows:
Zone 0: 0 <= LUX < array[0]
Zone 1: array[0] <= LUX < array[1]
...
Zone N: array[N - 1] <= LUX < array[N]
Zone N + 1: array[N] <= LUX < infinity
Must be overridden in platform specific overlays -->
<integer-array name="config_autoBrightnessLevels">
<item>3</item>
<item>10</item>
<item>40</item>
<item>65</item>
<item>145</item>
<item>300</item>
<item>550</item>
<item>930</item>
<item>1250</item>
</integer-array>
<!-- Array of output values for LCD backlight corresponding to the LUX values
in the config_autoBrightnessLevels array. This array should have size one greater
than the size of the config_autoBrightnessLevels array.
This must be overridden in platform specific overlays -->
<integer-array name="config_autoBrightnessLcdBacklightValues">
<item>101</item>
<item>117</item>
<item>142</item>
<item>183</item>
<item>195</item>
<item>216</item>
<item>234</item>
<item>245</item>
<item>250</item>
<item>250</item>
</integer-array>
<!-- Array of output values for button backlight corresponding to the LUX values
in the config_autoBrightnessLevels array. This array should have size one greater
than the size of the config_autoBrightnessLevels array.
This must be overridden in platform specific overlays -->
<integer-array name="config_autoBrightnessButtonBacklightValues">
<item>255</item>
<item>255</item>
<item>255</item>
<item>0</item>
<item>0</item>
<item>0</item>
<item>0</item>
<item>0</item>
<item>0</item>
<item>0</item>
</integer-array>
<!-- Array of output values for keyboard backlight corresponding to the LUX values
in the config_autoBrightnessLevels array. This array should have size one greater
than the size of the config_autoBrightnessLevels array.
This must be overridden in platform specific overlays -->
<integer-array name="config_autoBrightnessKeyboardBacklightValues">
</integer-array>
在初始化的時候會調用如下代碼獲取配置信息:
- // 獲取系統默認配置
- Resources resources = mContext.getResources();
- // 屏幕動畫是否開啓
- mAnimateScreenLights = resources.getBoolean(
- com.android.internal.R.bool.config_animateScreenLights);
- mUnplugTurnsOnScreen = resources.getBoolean(
- com.android.internal.R.bool.config_unplugTurnsOnScreen);
- // 軟件自動背光調整是否打開
- mUseSoftwareAutoBrightness = resources.getBoolean(
- com.android.internal.R.bool.config_automatic_brightness_available);
- // 讀取自動背光參考值
- if (mUseSoftwareAutoBrightness) {
- mAutoBrightnessLevels = resources.getIntArray(
- com.android.internal.R.array.config_autoBrightnessLevels);
- mLcdBacklightValues = resources.getIntArray( // lcd背光值
- com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
- mButtonBacklightValues = resources.getIntArray( // button背光值
- com.android.internal.R.array.config_autoBrightnessButtonBacklightValues);
- mKeyboardBacklightValues = resources.getIntArray( // keyboard背光值
- com.android.internal.R.array.config_autoBrightnessKeyboardBacklightValues);
- mLightSensorWarmupTime = resources.getInteger(
- com.android.internal.R.integer.config_lightSensorWarmupTime);
- }
// 獲取系統默認配置
Resources resources = mContext.getResources();
// 屏幕動畫是否開啓
mAnimateScreenLights = resources.getBoolean(
com.android.internal.R.bool.config_animateScreenLights);
mUnplugTurnsOnScreen = resources.getBoolean(
com.android.internal.R.bool.config_unplugTurnsOnScreen);
// 軟件自動背光調整是否打開
mUseSoftwareAutoBrightness = resources.getBoolean(
com.android.internal.R.bool.config_automatic_brightness_available);
// 讀取自動背光參考值
if (mUseSoftwareAutoBrightness) {
mAutoBrightnessLevels = resources.getIntArray(
com.android.internal.R.array.config_autoBrightnessLevels);
mLcdBacklightValues = resources.getIntArray( // lcd背光值
com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
mButtonBacklightValues = resources.getIntArray( // button背光值
com.android.internal.R.array.config_autoBrightnessButtonBacklightValues);
mKeyboardBacklightValues = resources.getIntArray( // keyboard背光值
com.android.internal.R.array.config_autoBrightnessKeyboardBacklightValues);
mLightSensorWarmupTime = resources.getInteger(
com.android.internal.R.integer.config_lightSensorWarmupTime);
}
二、代碼流程分析
在Android中,如果framework層接收到輸入事件就會調用PowerManagerService裏面的userActivity()去設置系統的下一個電源狀態,流程圖如下:
當調用userActivity()之後,將進行一些事件類型和狀態的判斷並先後調用setPowerState()設置新的狀態、setTimeoutLocked()設置新狀態超時後的狀態,代碼分析如下:
- private void userActivity(long time, long timeoutOverride, boolean noChangeLights,
- int eventType, boolean force) {
- // 檢測是否忽略CHEEK_EVENT事件
- if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)
- && (eventType == CHEEK_EVENT)) {
- if (false) {
- Slog.d(TAG, "dropping cheek event mPokey=0x" + Integer.toHexString(mPokey));
- }
- return;
- }
- // 檢測是否忽略CHEEK_EVENT與TOUCH事件
- if (((mPokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0)
- && (eventType == TOUCH_EVENT || eventType == TOUCH_UP_EVENT
- || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT)) {
- if (false) {
- Slog.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey));
- }
- return;
- }
- if (false) {
- if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)) {
- Slog.d(TAG, "userActivity !!!");//, new RuntimeException());
- } else {
- Slog.d(TAG, "mPokey=0x" + Integer.toHexString(mPokey));
- }
- }
- synchronized (mLocks) {
- if (mSpew) {
- Slog.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time
- + " mUserActivityAllowed=" + mUserActivityAllowed
- + " mUserState=0x" + Integer.toHexString(mUserState)
- + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState)
- + " mProximitySensorActive=" + mProximitySensorActive
- + " timeoutOverride=" + timeoutOverride
- + " force=" + force
- + " eventType=" + eventType);
- }
- // 如果當前屏幕爲關閉狀態則不處理
- if (isScreenTurningOffLocked()) {
- Slog.d(TAG, "ignoring user activity while turning off screen");
- return;
- }
- // 如果正在等待距離傳感器爲負值時用戶按下power鍵則關閉距離傳感器功能
- if (mProximitySensorActive && mProximityWakeLockCount == 0) {
- mProximitySensorActive = false;
- }
- if (mLastEventTime <= time || force) { // 判斷事件是否爲最新的
- mLastEventTime = time;
- if ((mUserActivityAllowed && !mProximitySensorActive) || force) {
- // 在用戶點擊觸摸屏或者虛擬按鍵並且不是自動背光狀態時設置下一個狀態爲全亮
- if ((eventType == BUTTON_EVENT || eventType == TOUCH_EVENT
- || eventType == TOUCH_UP_EVENT || eventType == LONG_TOUCH_EVENT)
- && (!mUseSoftwareAutoBrightness || !mAutoBrightessEnabled)) {
- mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
- } else {
- // 設置系統下一個狀態爲SCREEN_BRIGHT,並保留上次狀態
- mUserState |= SCREEN_BRIGHT;
- }
- int uid = Binder.getCallingUid();
- long ident = Binder.clearCallingIdentity();
- try {
- mBatteryStats.noteUserActivity(uid, eventType);
- } catch (RemoteException e) {
- // Ignore
- } finally {
- Binder.restoreCallingIdentity(ident);
- }
- // 獲取當前WakeLock狀態
- mWakeLockState = mLocks.reactivateScreenLocksLocked();
- // 設置下一個狀態
- setPowerState(mUserState | mWakeLockState, noChangeLights,
- WindowManagerPolicy.OFF_BECAUSE_OF_USER);
- // 設置超時後的狀態
- setTimeoutLocked(time, timeoutOverride, SCREEN_BRIGHT);
- }
- }
- }
- if (mPolicy != null) {
- mPolicy.userActivity();
- }
- }
private void userActivity(long time, long timeoutOverride, boolean noChangeLights,
int eventType, boolean force) {
// 檢測是否忽略CHEEK_EVENT事件
if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)
&& (eventType == CHEEK_EVENT)) {
if (false) {
Slog.d(TAG, "dropping cheek event mPokey=0x" + Integer.toHexString(mPokey));
}
return;
}
// 檢測是否忽略CHEEK_EVENT與TOUCH事件
if (((mPokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0)
&& (eventType == TOUCH_EVENT || eventType == TOUCH_UP_EVENT
|| eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT)) {
if (false) {
Slog.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey));
}
return;
}
if (false) {
if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)) {
Slog.d(TAG, "userActivity !!!");//, new RuntimeException());
} else {
Slog.d(TAG, "mPokey=0x" + Integer.toHexString(mPokey));
}
}
synchronized (mLocks) {
if (mSpew) {
Slog.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time
+ " mUserActivityAllowed=" + mUserActivityAllowed
+ " mUserState=0x" + Integer.toHexString(mUserState)
+ " mWakeLockState=0x" + Integer.toHexString(mWakeLockState)
+ " mProximitySensorActive=" + mProximitySensorActive
+ " timeoutOverride=" + timeoutOverride
+ " force=" + force
+ " eventType=" + eventType);
}
// 如果當前屏幕爲關閉狀態則不處理
if (isScreenTurningOffLocked()) {
Slog.d(TAG, "ignoring user activity while turning off screen");
return;
}
// 如果正在等待距離傳感器爲負值時用戶按下power鍵則關閉距離傳感器功能
if (mProximitySensorActive && mProximityWakeLockCount == 0) {
mProximitySensorActive = false;
}
if (mLastEventTime <= time || force) { // 判斷事件是否爲最新的
mLastEventTime = time;
if ((mUserActivityAllowed && !mProximitySensorActive) || force) {
// 在用戶點擊觸摸屏或者虛擬按鍵並且不是自動背光狀態時設置下一個狀態爲全亮
if ((eventType == BUTTON_EVENT || eventType == TOUCH_EVENT
|| eventType == TOUCH_UP_EVENT || eventType == LONG_TOUCH_EVENT)
&& (!mUseSoftwareAutoBrightness || !mAutoBrightessEnabled)) {
mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
} else {
// 設置系統下一個狀態爲SCREEN_BRIGHT,並保留上次狀態
mUserState |= SCREEN_BRIGHT;
}
int uid = Binder.getCallingUid();
long ident = Binder.clearCallingIdentity();
try {
mBatteryStats.noteUserActivity(uid, eventType);
} catch (RemoteException e) {
// Ignore
} finally {
Binder.restoreCallingIdentity(ident);
}
// 獲取當前WakeLock狀態
mWakeLockState = mLocks.reactivateScreenLocksLocked();
// 設置下一個狀態
setPowerState(mUserState | mWakeLockState, noChangeLights,
WindowManagerPolicy.OFF_BECAUSE_OF_USER);
// 設置超時後的狀態
setTimeoutLocked(time, timeoutOverride, SCREEN_BRIGHT);
}
}
}
if (mPolicy != null) {
mPolicy.userActivity();
}
}
- private void setTimeoutLocked(long now, final long originalTimeoutOverride, int nextState) {
- long timeoutOverride = originalTimeoutOverride; // 超時時間
- if (mBootCompleted) { // 判斷系統是否啓動完成
- synchronized (mLocks) {
- long when = 0;
- if (timeoutOverride <= 0) {
- switch (nextState)
- {
- case SCREEN_BRIGHT:
- when = now + mKeylightDelay; // 關閉按鍵燈的時間
- break;
- case SCREEN_DIM:
- if (mDimDelay >= 0) {
- when = now + mDimDelay; // 屏幕亮度降低的時間
- break;
- } else {
- Slog.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim");
- }
- case SCREEN_OFF:
- synchronized (mLocks) {
- when = now + mScreenOffDelay; // 關閉屏幕時間
- }
- break;
- default:
- when = now;
- break;
- }
- } else { // 如果指定了originalTimeoutOverride
- override: {
- if (timeoutOverride <= mScreenOffDelay) {
- when = now + timeoutOverride;
- nextState = SCREEN_OFF;
- break override;
- }
- timeoutOverride -= mScreenOffDelay;
- if (mDimDelay >= 0) {
- if (timeoutOverride <= mDimDelay) {
- when = now + timeoutOverride;
- nextState = SCREEN_DIM;
- break override;
- }
- timeoutOverride -= mDimDelay;
- }
- when = now + timeoutOverride;
- nextState = SCREEN_BRIGHT;
- }
- }
- if (mSpew) {
- Slog.d(TAG, "setTimeoutLocked now=" + now
- + " timeoutOverride=" + timeoutOverride
- + " nextState=" + nextState + " when=" + when);
- }
- mHandler.removeCallbacks(mTimeoutTask); // 清除超時任務
- mTimeoutTask.nextState = nextState; // 設置超時後的狀態
- mTimeoutTask.remainingTimeoutOverride = timeoutOverride > 0
- ? (originalTimeoutOverride - timeoutOverride)
- : -1;
- mHandler.postAtTime(mTimeoutTask, when); // 啓動超時任務
- mNextTimeout = when; // for debugging
- }
- }
- }
private void setTimeoutLocked(long now, final long originalTimeoutOverride, int nextState) {
long timeoutOverride = originalTimeoutOverride; // 超時時間
if (mBootCompleted) { // 判斷系統是否啓動完成
synchronized (mLocks) {
long when = 0;
if (timeoutOverride <= 0) {
switch (nextState)
{
case SCREEN_BRIGHT:
when = now + mKeylightDelay; // 關閉按鍵燈的時間
break;
case SCREEN_DIM:
if (mDimDelay >= 0) {
when = now + mDimDelay; // 屏幕亮度降低的時間
break;
} else {
Slog.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim");
}
case SCREEN_OFF:
synchronized (mLocks) {
when = now + mScreenOffDelay; // 關閉屏幕時間
}
break;
default:
when = now;
break;
}
} else { // 如果指定了originalTimeoutOverride
override: {
if (timeoutOverride <= mScreenOffDelay) {
when = now + timeoutOverride;
nextState = SCREEN_OFF;
break override;
}
timeoutOverride -= mScreenOffDelay;
if (mDimDelay >= 0) {
if (timeoutOverride <= mDimDelay) {
when = now + timeoutOverride;
nextState = SCREEN_DIM;
break override;
}
timeoutOverride -= mDimDelay;
}
when = now + timeoutOverride;
nextState = SCREEN_BRIGHT;
}
}
if (mSpew) {
Slog.d(TAG, "setTimeoutLocked now=" + now
+ " timeoutOverride=" + timeoutOverride
+ " nextState=" + nextState + " when=" + when);
}
mHandler.removeCallbacks(mTimeoutTask); // 清除超時任務
mTimeoutTask.nextState = nextState; // 設置超時後的狀態
mTimeoutTask.remainingTimeoutOverride = timeoutOverride > 0
? (originalTimeoutOverride - timeoutOverride)
: -1;
mHandler.postAtTime(mTimeoutTask, when); // 啓動超時任務
mNextTimeout = when; // for debugging
}
}
}
- private class TimeoutTask implements Runnable
- {
- int nextState; // access should be synchronized on mLocks
- long remainingTimeoutOverride;
- public void run()
- {
- synchronized (mLocks) {
- if (mSpew) {
- Slog.d(TAG, "user activity timeout timed out nextState=" + this.nextState);
- }
- if (nextState == -1) {
- return;
- }
- mUserState = this.nextState; // 設置全局變量
- setPowerState(this.nextState | mWakeLockState); // 設置電源狀態
- long now = SystemClock.uptimeMillis();
- switch (this.nextState) // 設置超時後的下一個狀態
- {
- case SCREEN_BRIGHT: // 如果當前屏幕全亮則下一狀態爲暗屏
- if (mDimDelay >= 0) {
- setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_DIM);
- break;
- }
- case SCREEN_DIM: // 如果當前屏幕爲暗屏則下一狀態爲熄屏
- setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_OFF);
- break;
- }
- }
- }
- }
private class TimeoutTask implements Runnable
{
int nextState; // access should be synchronized on mLocks
long remainingTimeoutOverride;
public void run()
{
synchronized (mLocks) {
if (mSpew) {
Slog.d(TAG, "user activity timeout timed out nextState=" + this.nextState);
}
if (nextState == -1) {
return;
}
mUserState = this.nextState; // 設置全局變量
setPowerState(this.nextState | mWakeLockState); // 設置電源狀態
long now = SystemClock.uptimeMillis();
switch (this.nextState) // 設置超時後的下一個狀態
{
case SCREEN_BRIGHT: // 如果當前屏幕全亮則下一狀態爲暗屏
if (mDimDelay >= 0) {
setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_DIM);
break;
}
case SCREEN_DIM: // 如果當前屏幕爲暗屏則下一狀態爲熄屏
setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_OFF);
break;
}
}
}
}
在setPowerState()中如果leds有變化則調用updateLightsLocked()更新leds的狀態,如果lcd新狀態和當前狀態不同,則可能需要調用setScreenStateLocked()處理系統的休眠和喚醒流程,代碼分析如下:
- private void setPowerState(int newState, boolean noChangeLights, int reason)
- {
- synchronized (mLocks) {
- int err;
- if (mSpew) {
- Slog.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState)
- + " newState=0x" + Integer.toHexString(newState)
- + " noChangeLights=" + noChangeLights
- + " reason=" + reason);
- }
- if (noChangeLights) { // 如果不改變light狀態,則保持上次light狀態
- newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK);
- }
- if (mProximitySensorActive) { // 如果距離傳感器鎖定則關閉lcd背光
- // don't turn on the screen when the proximity sensor lock is held
- newState = (newState & ~SCREEN_BRIGHT);
- }
- if (batteryIsLow()) { // 如果當前低電則添加低電狀態
- newState |= BATTERY_LOW_BIT;
- } else {
- newState &= ~BATTERY_LOW_BIT;
- }
- if (newState == mPowerState) { // 如果本次狀態和上次狀態一致則直接返回
- return;
- }
- // 如果正在啓動並且沒有設置自動背光則爲全亮
- if (!mBootCompleted && !mUseSoftwareAutoBrightness) {
- newState |= ALL_BRIGHT;
- }
- boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0; // 當前屏幕狀態
- boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0; // 下次屏幕狀態
- if (mSpew) {
- Slog.d(TAG, "setPowerState: mPowerState=" + mPowerState
- + " newState=" + newState + " noChangeLights=" + noChangeLights);
- Slog.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0)
- + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0));
- Slog.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0)
- + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0));
- Slog.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0)
- + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0));
- Slog.d(TAG, " oldScreenOn=" + oldScreenOn
- + " newScreenOn=" + newScreenOn);
- Slog.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0)
- + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0));
- }
- if (mPowerState != newState) { // 判斷狀態是否改變
- updateLightsLocked(newState, 0); // 更新按鍵燈狀態
- mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK);
- }
- if (oldScreenOn != newScreenOn) { // 判斷屏幕狀態是否不同
- if (newScreenOn) { // 如果要點亮屏幕
- // When the user presses the power button, we need to always send out the
- // notification that it's going to sleep so the keyguard goes on. But
- // we can't do that until the screen fades out, so we don't show the keyguard
- // too early.
- if (mStillNeedSleepNotification) {
- sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
- }
- // Turn on the screen UNLESS there was a prior
- // preventScreenOn(true) request. (Note that the lifetime
- // of a single preventScreenOn() request is limited to 5
- // seconds to prevent a buggy app from disabling the
- // screen forever; see forceReenableScreen().)
- boolean reallyTurnScreenOn = true;
- if (mSpew) {
- Slog.d(TAG, "- turning screen on... mPreventScreenOn = "
- + mPreventScreenOn);
- }
- if (mPreventScreenOn) { // 是否阻止屏幕點亮
- if (mSpew) {
- Slog.d(TAG, "- PREVENTING screen from really turning on!");
- }
- reallyTurnScreenOn = false;
- }
- if (reallyTurnScreenOn) {
- err = setScreenStateLocked(true); // 喚醒系統,點亮屏幕
- long identity = Binder.clearCallingIdentity();
- try {
- mBatteryStats.noteScreenBrightness(getPreferredBrightness());
- mBatteryStats.noteScreenOn();
- } catch (RemoteException e) {
- Slog.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e);
- } finally {
- Binder.restoreCallingIdentity(identity);
- }
- } else {
- setScreenStateLocked(false); // 休眠系統
- // But continue as if we really did turn the screen on...
- err = 0;
- }
- mLastTouchDown = 0;
- mTotalTouchDownTime = 0;
- mTouchCycles = 0;
- EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, reason,
- mTotalTouchDownTime, mTouchCycles);
- if (err == 0) {
- mPowerState |= SCREEN_ON_BIT;
- sendNotificationLocked(true, -1);
- }
- } else { // 如果要關閉屏幕
- // cancel light sensor task
- mHandler.removeCallbacks(mAutoBrightnessTask); // 取消背光自動調整任務
- mScreenOffTime = SystemClock.elapsedRealtime(); // 獲取關閉時間
- long identity = Binder.clearCallingIdentity();
- try {
- mBatteryStats.noteScreenOff();
- } catch (RemoteException e) {
- Slog.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e);
- } finally {
- Binder.restoreCallingIdentity(identity);
- }
- mPowerState &= ~SCREEN_ON_BIT; // 設置狀態位
- mScreenOffReason = reason;
- if (!mScreenBrightness.animating) { // 判斷是否打開關屏動畫
- err = screenOffFinishedAnimatingLocked(reason);
- } else {
- err = 0;
- mLastTouchDown = 0;
- }
- }
- }
- updateNativePowerStateLocked(); // 更新HAL層狀態
- }
- }
private void setPowerState(int newState, boolean noChangeLights, int reason)
{
synchronized (mLocks) {
int err;
if (mSpew) {
Slog.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState)
+ " newState=0x" + Integer.toHexString(newState)
+ " noChangeLights=" + noChangeLights
+ " reason=" + reason);
}
if (noChangeLights) { // 如果不改變light狀態,則保持上次light狀態
newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK);
}
if (mProximitySensorActive) { // 如果距離傳感器鎖定則關閉lcd背光
// don't turn on the screen when the proximity sensor lock is held
newState = (newState & ~SCREEN_BRIGHT);
}
if (batteryIsLow()) { // 如果當前低電則添加低電狀態
newState |= BATTERY_LOW_BIT;
} else {
newState &= ~BATTERY_LOW_BIT;
}
if (newState == mPowerState) { // 如果本次狀態和上次狀態一致則直接返回
return;
}
// 如果正在啓動並且沒有設置自動背光則爲全亮
if (!mBootCompleted && !mUseSoftwareAutoBrightness) {
newState |= ALL_BRIGHT;
}
boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0; // 當前屏幕狀態
boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0; // 下次屏幕狀態
if (mSpew) {
Slog.d(TAG, "setPowerState: mPowerState=" + mPowerState
+ " newState=" + newState + " noChangeLights=" + noChangeLights);
Slog.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0)
+ " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0));
Slog.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0)
+ " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0));
Slog.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0)
+ " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0));
Slog.d(TAG, " oldScreenOn=" + oldScreenOn
+ " newScreenOn=" + newScreenOn);
Slog.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0)
+ " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0));
}
if (mPowerState != newState) { // 判斷狀態是否改變
updateLightsLocked(newState, 0); // 更新按鍵燈狀態
mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK);
}
if (oldScreenOn != newScreenOn) { // 判斷屏幕狀態是否不同
if (newScreenOn) { // 如果要點亮屏幕
// When the user presses the power button, we need to always send out the
// notification that it's going to sleep so the keyguard goes on. But
// we can't do that until the screen fades out, so we don't show the keyguard
// too early.
if (mStillNeedSleepNotification) {
sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
}
// Turn on the screen UNLESS there was a prior
// preventScreenOn(true) request. (Note that the lifetime
// of a single preventScreenOn() request is limited to 5
// seconds to prevent a buggy app from disabling the
// screen forever; see forceReenableScreen().)
boolean reallyTurnScreenOn = true;
if (mSpew) {
Slog.d(TAG, "- turning screen on... mPreventScreenOn = "
+ mPreventScreenOn);
}
if (mPreventScreenOn) { // 是否阻止屏幕點亮
if (mSpew) {
Slog.d(TAG, "- PREVENTING screen from really turning on!");
}
reallyTurnScreenOn = false;
}
if (reallyTurnScreenOn) {
err = setScreenStateLocked(true); // 喚醒系統,點亮屏幕
long identity = Binder.clearCallingIdentity();
try {
mBatteryStats.noteScreenBrightness(getPreferredBrightness());
mBatteryStats.noteScreenOn();
} catch (RemoteException e) {
Slog.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e);
} finally {
Binder.restoreCallingIdentity(identity);
}
} else {
setScreenStateLocked(false); // 休眠系統
// But continue as if we really did turn the screen on...
err = 0;
}
mLastTouchDown = 0;
mTotalTouchDownTime = 0;
mTouchCycles = 0;
EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, reason,
mTotalTouchDownTime, mTouchCycles);
if (err == 0) {
mPowerState |= SCREEN_ON_BIT;
sendNotificationLocked(true, -1);
}
} else { // 如果要關閉屏幕
// cancel light sensor task
mHandler.removeCallbacks(mAutoBrightnessTask); // 取消背光自動調整任務
mScreenOffTime = SystemClock.elapsedRealtime(); // 獲取關閉時間
long identity = Binder.clearCallingIdentity();
try {
mBatteryStats.noteScreenOff();
} catch (RemoteException e) {
Slog.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e);
} finally {
Binder.restoreCallingIdentity(identity);
}
mPowerState &= ~SCREEN_ON_BIT; // 設置狀態位
mScreenOffReason = reason;
if (!mScreenBrightness.animating) { // 判斷是否打開關屏動畫
err = screenOffFinishedAnimatingLocked(reason);
} else {
err = 0;
mLastTouchDown = 0;
}
}
}
updateNativePowerStateLocked(); // 更新HAL層狀態
}
}
在updateLightsLocked()裏面將會進行一系列的狀態處理, 比如調用applyButtonState()獲取button led的相關數據和狀態,然後調用setLightBrightness()設置leds,代碼分析如下:- private void updateLightsLocked(int newState, int forceState) {
- final int oldState = mPowerState;
- if ((newState & SCREEN_ON_BIT) != 0) { // 如果屏幕打開則設置按鍵燈
- newState = applyButtonState(newState); // 請求button燈狀態
- newState = applyKeyboardState(newState); // 請求keyboard燈狀態
- }
- final int realDifference = (newState ^ oldState); // 判斷狀態是否相同,取得不同的位
- final int difference = realDifference | forceState; // 是否強制更新狀態
- if (difference == 0) {
- return;
- }
- int offMask = 0;
- int dimMask = 0;
- int onMask = 0;
- int preferredBrightness = getPreferredBrightness(); // 獲取參考亮度
- if ((difference & KEYBOARD_BRIGHT_BIT) != 0) { // 如果keyboard有變化
- if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
- offMask |= KEYBOARD_BRIGHT_BIT;
- } else {
- onMask |= KEYBOARD_BRIGHT_BIT;
- }
- }
- if ((difference & BUTTON_BRIGHT_BIT) != 0) { // 如果button有變化
- if ((newState & BUTTON_BRIGHT_BIT) == 0) {
- offMask |= BUTTON_BRIGHT_BIT;
- } else {
- onMask |= BUTTON_BRIGHT_BIT;
- }
- }
- // 如果狀態不同且forceState有效或者屏幕狀態變化
- if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
- int nominalCurrentValue = -1;
- // If there was an actual difference in the light state, then
- // figure out the "ideal" current value based on the previous
- // state. Otherwise, this is a change due to the brightness
- // override, so we want to animate from whatever the current
- // value is.
- // 如果屏幕狀態變化
- if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
- switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) { // 判斷當前屏幕狀態
- case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT: // 屏幕全亮狀態
- nominalCurrentValue = preferredBrightness;
- break;
- case SCREEN_ON_BIT: // 屏幕DIM狀態
- nominalCurrentValue = Power.BRIGHTNESS_DIM;
- break;
- case 0: // 屏幕關閉狀態
- nominalCurrentValue = Power.BRIGHTNESS_OFF;
- break;
- case SCREEN_BRIGHT_BIT:
- default:
- // not possible
- nominalCurrentValue = (int)mScreenBrightness.curValue;
- break;
- }
- }
- int brightness = preferredBrightness;
- int steps = ANIM_STEPS; // 動畫步數
- if ((newState & SCREEN_BRIGHT_BIT) == 0) { // 下一個狀態爲DIM或者OFF
- // dim or turn off backlight, depending on if the screen is on
- // the scale is because the brightness ramp isn't linear and this biases
- // it so the later parts take longer.
- final float scale = 1.5f;
- // 每次變化比例
- float ratio = (((float)Power.BRIGHTNESS_DIM)/preferredBrightness);
- if (ratio > 1.0f) ratio = 1.0f;
- if ((newState & SCREEN_ON_BIT) == 0) { // 下一個狀態爲OFF
- if ((oldState & SCREEN_BRIGHT_BIT) != 0) {
- // was bright
- steps = ANIM_STEPS;
- } else {
- // was dim
- steps = (int)(ANIM_STEPS*ratio*scale);
- }
- brightness = Power.BRIGHTNESS_OFF; // 設置brightness
- } else { // 下一個狀態爲DIM
- if ((oldState & SCREEN_ON_BIT) != 0) {
- // was bright
- steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale);
- } else {
- // was dim
- steps = (int)(ANIM_STEPS*ratio);
- }
- if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
- // If the "stay on while plugged in" option is
- // turned on, then the screen will often not
- // automatically turn off while plugged in. To
- // still have a sense of when it is inactive, we
- // will then count going dim as turning off.
- mScreenOffTime = SystemClock.elapsedRealtime();
- }
- brightness = Power.BRIGHTNESS_DIM;
- }
- }
- long identity = Binder.clearCallingIdentity();
- try {
- mBatteryStats.noteScreenBrightness(brightness);
- } catch (RemoteException e) {
- // Nothing interesting to do.
- } finally {
- Binder.restoreCallingIdentity(identity);
- }
- mScreenBrightness.setTargetLocked(brightness, steps, // 設置背光亮度
- INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue);
- }
- if (mSpew) {
- Slog.d(TAG, "offMask=0x" + Integer.toHexString(offMask)
- + " dimMask=0x" + Integer.toHexString(dimMask)
- + " onMask=0x" + Integer.toHexString(onMask)
- + " difference=0x" + Integer.toHexString(difference)
- + " realDifference=0x" + Integer.toHexString(realDifference)
- + " forceState=0x" + Integer.toHexString(forceState)
- );
- }
- if (offMask != 0) { // 如果有燈要關閉
- if (mSpew) Slog.i(TAG, "Setting brightess off: " + offMask);
- setLightBrightness(offMask, Power.BRIGHTNESS_OFF);
- }
- if (dimMask != 0) {
- int brightness = Power.BRIGHTNESS_DIM;
- if ((newState & BATTERY_LOW_BIT) != 0 &&
- brightness > Power.BRIGHTNESS_LOW_BATTERY) {
- brightness = Power.BRIGHTNESS_LOW_BATTERY;
- }
- if (mSpew) Slog.i(TAG, "Setting brightess dim " + brightness + ": " + dimMask);
- setLightBrightness(dimMask, brightness); // 設置leds
- }
- if (onMask != 0) { // 如果有燈要打開
- int brightness = getPreferredBrightness(); // 獲取亮度值,包括按鍵燈的亮度值,都是從用戶設置中獲取
- if ((newState & BATTERY_LOW_BIT) != 0 &&
- brightness > Power.BRIGHTNESS_LOW_BATTERY) {
- brightness = Power.BRIGHTNESS_LOW_BATTERY;
- }
- if (mSpew) Slog.i(TAG, "Setting brightess on " + brightness + ": " + onMask);
- setLightBrightness(onMask, brightness); // 設置leds
- }
- }
private void updateLightsLocked(int newState, int forceState) {
final int oldState = mPowerState;
if ((newState & SCREEN_ON_BIT) != 0) { // 如果屏幕打開則設置按鍵燈
newState = applyButtonState(newState); // 請求button燈狀態
newState = applyKeyboardState(newState); // 請求keyboard燈狀態
}
final int realDifference = (newState ^ oldState); // 判斷狀態是否相同,取得不同的位
final int difference = realDifference | forceState; // 是否強制更新狀態
if (difference == 0) {
return;
}
int offMask = 0;
int dimMask = 0;
int onMask = 0;
int preferredBrightness = getPreferredBrightness(); // 獲取參考亮度
if ((difference & KEYBOARD_BRIGHT_BIT) != 0) { // 如果keyboard有變化
if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
offMask |= KEYBOARD_BRIGHT_BIT;
} else {
onMask |= KEYBOARD_BRIGHT_BIT;
}
}
if ((difference & BUTTON_BRIGHT_BIT) != 0) { // 如果button有變化
if ((newState & BUTTON_BRIGHT_BIT) == 0) {
offMask |= BUTTON_BRIGHT_BIT;
} else {
onMask |= BUTTON_BRIGHT_BIT;
}
}
// 如果狀態不同且forceState有效或者屏幕狀態變化
if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
int nominalCurrentValue = -1;
// If there was an actual difference in the light state, then
// figure out the "ideal" current value based on the previous
// state. Otherwise, this is a change due to the brightness
// override, so we want to animate from whatever the current
// value is.
// 如果屏幕狀態變化
if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) { // 判斷當前屏幕狀態
case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT: // 屏幕全亮狀態
nominalCurrentValue = preferredBrightness;
break;
case SCREEN_ON_BIT: // 屏幕DIM狀態
nominalCurrentValue = Power.BRIGHTNESS_DIM;
break;
case 0: // 屏幕關閉狀態
nominalCurrentValue = Power.BRIGHTNESS_OFF;
break;
case SCREEN_BRIGHT_BIT:
default:
// not possible
nominalCurrentValue = (int)mScreenBrightness.curValue;
break;
}
}
int brightness = preferredBrightness;
int steps = ANIM_STEPS; // 動畫步數
if ((newState & SCREEN_BRIGHT_BIT) == 0) { // 下一個狀態爲DIM或者OFF
// dim or turn off backlight, depending on if the screen is on
// the scale is because the brightness ramp isn't linear and this biases
// it so the later parts take longer.
final float scale = 1.5f;
// 每次變化比例
float ratio = (((float)Power.BRIGHTNESS_DIM)/preferredBrightness);
if (ratio > 1.0f) ratio = 1.0f;
if ((newState & SCREEN_ON_BIT) == 0) { // 下一個狀態爲OFF
if ((oldState & SCREEN_BRIGHT_BIT) != 0) {
// was bright
steps = ANIM_STEPS;
} else {
// was dim
steps = (int)(ANIM_STEPS*ratio*scale);
}
brightness = Power.BRIGHTNESS_OFF; // 設置brightness
} else { // 下一個狀態爲DIM
if ((oldState & SCREEN_ON_BIT) != 0) {
// was bright
steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale);
} else {
// was dim
steps = (int)(ANIM_STEPS*ratio);
}
if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
// If the "stay on while plugged in" option is
// turned on, then the screen will often not
// automatically turn off while plugged in. To
// still have a sense of when it is inactive, we
// will then count going dim as turning off.
mScreenOffTime = SystemClock.elapsedRealtime();
}
brightness = Power.BRIGHTNESS_DIM;
}
}
long identity = Binder.clearCallingIdentity();
try {
mBatteryStats.noteScreenBrightness(brightness);
} catch (RemoteException e) {
// Nothing interesting to do.
} finally {
Binder.restoreCallingIdentity(identity);
}
mScreenBrightness.setTargetLocked(brightness, steps, // 設置背光亮度
INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue);
}
if (mSpew) {
Slog.d(TAG, "offMask=0x" + Integer.toHexString(offMask)
+ " dimMask=0x" + Integer.toHexString(dimMask)
+ " onMask=0x" + Integer.toHexString(onMask)
+ " difference=0x" + Integer.toHexString(difference)
+ " realDifference=0x" + Integer.toHexString(realDifference)
+ " forceState=0x" + Integer.toHexString(forceState)
);
}
if (offMask != 0) { // 如果有燈要關閉
if (mSpew) Slog.i(TAG, "Setting brightess off: " + offMask);
setLightBrightness(offMask, Power.BRIGHTNESS_OFF);
}
if (dimMask != 0) {
int brightness = Power.BRIGHTNESS_DIM;
if ((newState & BATTERY_LOW_BIT) != 0 &&
brightness > Power.BRIGHTNESS_LOW_BATTERY) {
brightness = Power.BRIGHTNESS_LOW_BATTERY;
}
if (mSpew) Slog.i(TAG, "Setting brightess dim " + brightness + ": " + dimMask);
setLightBrightness(dimMask, brightness); // 設置leds
}
if (onMask != 0) { // 如果有燈要打開
int brightness = getPreferredBrightness(); // 獲取亮度值,包括按鍵燈的亮度值,都是從用戶設置中獲取
if ((newState & BATTERY_LOW_BIT) != 0 &&
brightness > Power.BRIGHTNESS_LOW_BATTERY) {
brightness = Power.BRIGHTNESS_LOW_BATTERY;
}
if (mSpew) Slog.i(TAG, "Setting brightess on " + brightness + ": " + onMask);
setLightBrightness(onMask, brightness); // 設置leds
}
}
- private int applyButtonState(int state) {
- int brightness = -1;
- if ((state & BATTERY_LOW_BIT) != 0) {
- // 如果電池電量低則不覆蓋亮度值
- return state;
- }
- if (mButtonBrightnessOverride >= 0) { // 判斷亮度值是否有效
- brightness = mButtonBrightnessOverride;
- } else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) {
- // 根據lightsensor設置亮度值
- // 這裏有android系統的一個bug,如果開啓自動背光後再關閉自動背光
- // 會導致mLightSensorButtonBrightness爲之前自動背光時的值,如果爲0按鍵燈就不會再被點亮了
- brightness = mLightSensorButtonBrightness;
- }
- if (brightness > 0) {
- return state | BUTTON_BRIGHT_BIT; // 如果亮度值大於0則返回狀態
- } else if (brightness == 0) {
- return state & ~BUTTON_BRIGHT_BIT;
- } else {
- return state;
- }
- }
private int applyButtonState(int state) {
int brightness = -1;
if ((state & BATTERY_LOW_BIT) != 0) {
// 如果電池電量低則不覆蓋亮度值
return state;
}
if (mButtonBrightnessOverride >= 0) { // 判斷亮度值是否有效
brightness = mButtonBrightnessOverride;
} else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) {
// 根據lightsensor設置亮度值
// 這裏有android系統的一個bug,如果開啓自動背光後再關閉自動背光
// 會導致mLightSensorButtonBrightness爲之前自動背光時的值,如果爲0按鍵燈就不會再被點亮了
brightness = mLightSensorButtonBrightness;
}
if (brightness > 0) {
return state | BUTTON_BRIGHT_BIT; // 如果亮度值大於0則返回狀態
} else if (brightness == 0) {
return state & ~BUTTON_BRIGHT_BIT;
} else {
return state;
}
}
在setLightBrightness()中將根據標誌依次調用JNI接口去設置leds,代碼分析如下:
- private void setLightBrightness(int mask, int value) {
- int brightnessMode = (mAutoBrightessEnabled
- ? LightsService.BRIGHTNESS_MODE_SENSOR
- : LightsService.BRIGHTNESS_MODE_USER); // 設置背光模式
- if ((mask & SCREEN_BRIGHT_BIT) != 0) {
- mLcdLight.setBrightness(value, brightnessMode); // 調用JNI接口設置lcd亮度
- }
- if ((mask & BUTTON_BRIGHT_BIT) != 0) {
- mButtonLight.setBrightness(value); // 調用JNI接口設置button燈
- }
- if ((mask & KEYBOARD_BRIGHT_BIT) != 0) {
- mKeyboardLight.setBrightness(value); // 調用JNI接口設置keyboard燈
- }
- }
private void setLightBrightness(int mask, int value) {
int brightnessMode = (mAutoBrightessEnabled
? LightsService.BRIGHTNESS_MODE_SENSOR
: LightsService.BRIGHTNESS_MODE_USER); // 設置背光模式
if ((mask & SCREEN_BRIGHT_BIT) != 0) {
mLcdLight.setBrightness(value, brightnessMode); // 調用JNI接口設置lcd亮度
}
if ((mask & BUTTON_BRIGHT_BIT) != 0) {
mButtonLight.setBrightness(value); // 調用JNI接口設置button燈
}
if ((mask & KEYBOARD_BRIGHT_BIT) != 0) {
mKeyboardLight.setBrightness(value); // 調用JNI接口設置keyboard燈
}
}