Do you know how your Android reboot when you press the power button?
Whenever we want to perform the restart of our android device, we just the long press the power button and it shows a dialog with 3 options(or more than 3 depending on device customization). The basic dialog shows power off, restart, and emergency. Now upon taping the restart, your device reboots. Let’s see what goes behind the scene, when you long press the power button.
Global Action for long press of power button
The dialog which appears when you long press the power button is called a GlobalAction dialog. The reason it is called GlobalAction because, no matter in which screen you are, what application you have opened, the functionality of the dialog will never change.
The whole event of reboot is performed in 3 steps:
- Event generation for power button.
- Dialog creation when power button is pressed.
- Rebooting the device
Long press event generation
Whenever any button is pressed, an event gets generated. That event is then cooked, processed, and sent to PhoneWindowManager. There are two separate events that get generated.
- For down event.
- For Up event.
And for every event, interceptKeyBeforeQueueing()
and interceptKeyBeforeDispatching()
gets called.Upon long pressing of the power button powerLongPress() method is called. This method then invokes showGlobalActionInternal(). Then this method calls showDialog()
on GlobalActions
object to show the dialog.
Dialog creation when power button is long pressed
The menu or dialog which is displayed is a part of SystemUI
. And as SystemUI
runs in a separate process, so there has to be a binder transaction between framework and SystemUI
. This is done with the help of IStatusBar.aidl
. CommandQueue.java extends it’s stub class and the call then reaches to showGlobalActionMenu(). This method sends a handler message MSG_SHOW_GLOBAL_ACTION
. The message is then received in the handleMessage()
method of handler and calls handleShowGlobalAction()
on the Callback
object.
The GlobalActionComponent then call showGlobalAction() on GlobalActionImpl object. From here the GlobalActionDialog is created and displayed. This dialog actually contains many functionalities. Such as Restart, PowerOff, Bugreport, Emergency, Screenshot and Accessibility but only limited functionalities are shown in the menu, like Restart, PowerOff, and Emergency. The OEM vendor can decide what options to show. Upon pressing the restart option, reboot() method gets called on GlobalActionsManager object.
Rebooting of device
The reboot actually happens from the framework side. So when user press the restart option, there again happens a binder call to framework. This time IStatusBarService.aidl
comes into picture. The call then reaches to StatusBarManagerService‘s reboot() method. This method allows the status bar to reboot the device.
The method then uses a separate thread to perform the common functionality of reboot and shutdown.
ShutdownThread.reboot(getUiContext(),PowerManager.SHUTDOWN_USER_REQUESTED, false);
From this moment, you will see a Shutdown UI, a small round progress bar. As ShutdownThread displays, a UI, that’s the reason it needs to get the UI context. And hence you see the method getUiContext() is being passed as an agrument in reboot() method. This method then calls the beginShutdownSequence() which initializes the PowerManagerService and start the ShutdownThread. The thread then starts shutting down system services
like ActivityManagerService, PackageManagerService, etc on its run() method. Finally, it calls
PowerManagerService.lowLevelReboot(reason);
It is a low-level function to reboot the device. This method sets the system property, “sys.powerctl” with value “reboot” along with the reason for the reboot. This property is then being read in init.cpp and make a sysCall
in kernel to reboot the device.
3 Ways to reboot you android device
There are 3 ways, by which you can perform the reboot operation.
- Long pressing the power button.
- Through ADB command.
- Programmatically
Long Press of power button
As discussed above, the long press will show you a dialog which contains few options like PowerOff, Restart and Emergency. Selecting the Restart option will reboot the android device.
Through ADB command
You can use the below ADB command to reboot your android device.
adb reboot
When this command is executed, it directly sets the “sys.powerctl” property with “reboot” using
android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_string)
in service.cpp that is present in /system/core/adb location. Once the value is set, the property is then read in init.cpp file and make a sysCall to kernel for reboot. When using this command you won’t see any UI dancing on your screen. As soon as you execute the command, your android device will reboot.
Programmatically
To reboot the device programmatically, you need to add reboot permission on your AndroidManifest.xml
file first.
<permission android:name="android.permission.REBOOT"/>
Then use the PowerManagerService#reboot()
api to reboot your device.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
pm.reboot(null);
Summary of the process of Android reboot
The long press of power button is detected in PhoneWindowManager
i.e in framework. The GlobalActions
uses IStatusBar.aidl
to make a call from framework to SystemUI. The SystemUI displays the GlobalActionDialog
and on clicking the Restart option, the call again shifted to framework. This time it uses IStatusBarService.aidl
to make the binder call to framework. The StatusBarManagerService
uses a separate thread i.e ShutdownThread
to reboot. The ShutdownThread then calls PackageManagerService.lowLevelReboot()
method to reboot your android device.
The original article was published in GizmoMind.com.