General event propagation model (simplified): when an input occurs, the kernel generates an event. The input event is read by the event handler and sent to the foreground application. Several keys are sent to a particular application instead of being sent to the foreground application. For example, the Windows button on the keyboard is sent to the Start menu, the play / pause button is sent to the media player application, even if it is not in the foreground.
In the case when Android system_server reads events from the kernel, it goes to the foreground view / view. This is done for all input events, with the exception of the power key, home button, etc., which is handled by the Android window manager or system. PhoneWindowManager.java has the window manager code; it receives the KEYCODE_POWER event when the power key is pressed. In this case, if the screen is turned on and the lock for display is not saved, the screen is locked (by calling goToSleep in the PowerManager), similarly, if the screen is turned off, then the screen turns on (by calling wakeUp in the PowerManager).
isWakeKey at http://androidxref.com/5.1.0_r1/xref/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java#4512 will be set to true when you press and release the power button. The code will call wakeUp or goToSleep depending on the current state of the screen.
source share