当用户按下Power button:
0) android/frameworks/base/services/java/com/android/server/WindowManagerService.java
boolean screenIsOff = !mPowerManager.isScreenOn();
boolean screenIsDim = !mPowerManager.isScreenBright();
int actions = mPolicy.interceptKeyTq(event, !screenIsOff); ===> Step 1
if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
mPowerManager.goToSleep(event.when); ===> Step 2
}
1) android/frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java
interceptKeyTq function
if (type == RawInputEvent.EV_KEY) {
if (code == KeyEvent.KEYCODE_ENDCALL
|| code == KeyEvent.KEYCODE_POWER) {
if (down) {
。。。。。。。。。。。。。。。。。。。
// only try to turn off the screen if we didn't already hang up
mShouldTurnOffOnKeyUp = true;
mHandler.postDelayed(mPowerLongPress,
ViewConfiguration.getGlobalActionKeyTimeout()); ==> 过500ms弹出对话框
result &= ~ACTION_PASS_TO_USER;
}
else {
Log.d(TAG, "Power button up /r/n");
mHandler.removeCallbacks(mPowerLongPress); ==> 如果500ms内有power up键,取消对话框
。。。。。。。。。。。。。。。。。
result |= ACTION_GO_TO_SLEEP;
}
2) android/framework/base/services/java/com/android/server/PowerManagerService.java
public void goToSleep(long time)
{
goToSleepWithReason(time, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
}
3) Jave native interface:
android/frameworks/base/core/jni/android_os_Power.cpp
static int
setScreenState(JNIEnv *env, jobject clazz, jboolean on)
{
return set_screen_state(on);
}
4) android/hardware/libhardware_legacy/power/power.c
int
set_screen_state(int on)
{
QEMU_FALLBACK(set_screen_state(on));
initialize_fds(); ===> open("/sys/power/state", O_RDWR);
if (g_error) return g_error;
char buf[32];
int len;
if(on)
len = sprintf(buf, on_state);
else
len = sprintf(buf, off_state);
len = write(g_fds[REQUEST_STATE], buf, len); ===> echo mem > /sys/power/state
if(len < 0) {
LOGE("Failed setting last user activity: g_error=%d/n", g_error);
}
return 0;
}
5) System will enter suspend mode and it will start normal linux suspend flow