Android Power Management

Source: Internet
Author: User

I. Android Power Management Application Layer Analysis

Android provides the Android. OS. powermanager class, which is used to control the switching of the Power status of a device.
This class has three interface functions: 1. Void Gotosleep (long time );
Force the device to enter the sleep status
Pay attention to permission issues. 2. newwakelock (INT flags, string tag );
Obtain locks of corresponding Layers
Flags parameter description:
Partial_wake_lock: screen off, keyboard light off
Screen_dim_wake_lock: screen dim, keyboard light off
Screen_bright_wake_lock: screen bright, keyboard light off
Full_wake_lock: screen bright, keyboard bright
Acquire_causes_wakeup: Forces screen and keyboard light to be turned on when a request is locked.
On_after_release: When the lock is released, reset activity timer. If you apply for partial wakelock, the system will not sleep even if you press the power key, for example, during music playback.
If you have applied for another wakelocks, press the power key and the system will still enter sleep. 3. Void useractivity (long when, Boolean nochangelights );
When a user activity event occurs, the device is switched to the full on status, and the reset screen off timer.
1) The following permissions must be added to the manifest. xml file of the application that uses the preceding functions:
<Uses-Permission Android: Name = "android. Permission. wake_lock"/>
<Uses-Permission Android: Name = "android. Permission. device_power"/>
2) All locks must be used in pairs. If the lock is applied and not released in time, system faults may occur. if you have applied for a partial wakelock and you have not released it in time, the system will never be able to enter the sleep mode. Ii. Android power management Java layer analysis of its main code file is as follows:
Frameworks/base/CORE/Java/Android/OS/powermanager. Java
Frameworks/base/services/Java/COM/Android/Server/powermanagerservice. Java
Frameworks/base/CORE/Java/Android/OS/power. Java where powermanagerservice. Java is the core, and powermanager. Java is provided to the application layer for calling,
Power. Java provides underlying function interfaces for interaction with the JNI layer. Role of powermanagerservice. Java class
It provides the powermanager function and the operation of the entire power management state machine. There are many functions and classes,
From the external and internal aspects: 1. First, let's talk about how powermanagerservice manages power supply, so we need to notify it when there is an external event,
This is mainly in frameworks/base/services/Java/COM/Android/Server/windowmanagerservice. java. Windowmanagerservice calls the useractivity function as a user activity event based on the user's click screen and buttons. powermanagerservice then determines the event type in the useractivity to reflect the event type. It is used to light up the screen and provide operations, still ignore it, or turn it off when it is only lit up. The methods used by windowmanagerservice include Gotosleep and some other functions used to obtain power status, such as screenison. 2. Internally, the useractivity Method Used as the external interface mainly uses setpowerstate to complete functions. Call setpowerstate as a parameter to set the power status, such as switching the screen backlight. setpowerstate first determines whether the desired state can be completed, for example, if you want to light up the screen, but now the screen is locked, it cannot be lit up. Otherwise, you can call power. setscreenstate (true): Run JNI to the driver to light up the screen. The State loop of the power supply is mainly implemented through handler. Powermanagerservice starts a handlerthread and a background message loop in init to provide delayed sending of tasks. You can use handler to customize the execution time of a task and implement the state machine loop. For example, if there is no operation after a period of time, the screen should be dimmed and then closed, which is reflected in the Code as follows: useractivity uses settimeoutlocked to set timeout after setpowerstate is called. In settimeoutlocked, the next state and time are calculated based on the current state. After judgment, call mhandler. postattime (mtimeouttask, when) to post a timeouttask. In this way, the timeouttask will be executed in milliseconds after when. In timeouttask, setpowerstate is called Based on the set state to change the power status, and then a new State is set. For example, if the screen is dimmed, settimeoutlocked (now, screen_off. If the screen is turned off this time, the cycle of the timeout state will be over. If you want to customize it, for example, you need to turn off some peripheral devices after the timeout screen is turned off, and so on, you just need to turn off the screen in timeouttask and add the code to turn off other devices. Even if the new status requirement is completely different from the original one, it is not difficult to use handler. After the logic is clarified, we can put the code in a proper place. Iii. Android power management JNI layer analysis of its main code file is as follows:
Frameworks/base/CORE/JNI/android_ OS _power.cpp
Hardware/libhardware/power. the c jni Layer Code is mainly used in the file android_ OS _power.cpp. The interaction with Linux kernel is through power. c. The interaction between andriod and kernel is mainly implemented through the Sys File. Iv. Android kernel Power Management Analysis 1. Its main code is located in the following locations:
Drivers/Android/power. C. Its interface functions for Kernel include:
Export_symbol (android_init_suspend_lock); // initialize the suspend lock before use.
Export_symbol (android_uninit_suspend_lock); // release resources related to suspend lock.
Export_symbol (android_lock_suspend); // apply for a lock. The corresponding unlock must be called to release it.
Export_symbol (android_lock_suspend_auto_expire); // apply for partial wakelock, which is automatically released after the scheduled time.
Export_symbol (android_unlock_suspend); // release lock
Export_symbol (android_power_wakeup); // wake up the system to the on
Export_symbol (android_register_early_suspend); // register the early suspend driver
Export_symbol (android_unregister_early_suspend); // The proc file provided to the android framework layer by canceling the registered early suspend driver is as follows:
"/Sys/android_power/acquire_partial_wake_lock" // apply for partial wake lock
"/Sys/android_power/acquire_full_wake_lock" // apply for full wake lock
"/Sys/android_power/release_wake_lock" // release the corresponding wake lock
"/Sys/android_power/request_state" // The request changes the system status, including standby and wakeup.
"/Sys/android_power/state" // indicates the status of the current system. 2. Android power management is mainly implemented through wake lock, at the bottom layer, the management is implemented through the following three Queues: static list_head (g_inactive_locks );
Static list_head (g_active_partial_wake_locks );
Static list_head (g_active_full_wake_locks); All initialized locks will be inserted into the g_inactive_locks queue, and the currently active partial wake lock will be inserted into the g_active_partial_wake_locks queue, the full wake lock of the activity is inserted into the g_active_full_wake_locks queue. All the partial wake locks and full wake locks will be moved to the inactive queue after expiration or unlock, waiting for the next call. 3. perform the following steps to use wake lock on the kernel layer: 1) Call the android_init_suspend_lock function to initialize a wake lock. 2) Call the android_lock_suspend or android_lock_suspend_auto_expire function to request, here, we can only apply for partial wake lock. If we want to apply for full wake lock, we need to call the number of arguments (this function does not have an export). This name is a bit strange and should not be mixed with android_lock_suspend_auto_expire above. 3) If it is auto expire's wake lock, you can ignore it. Otherwise, you must release the relevant wake lock in time. Otherwise, the system will run at High Power Consumption for a long time.
4) Remember to call android_uninit_suspend_lock to release resources in a timely manner when the driver is uninstalled or no longer using wake lock. 4. system status:
User_awake, // full on status
User_notification, // early suspended driver but CPU keep on
User_sleep // CPU enter sleep mode 5. After the android power management process system is started normally, it enters the awake state. The backlight will slowly adjust from the brightest to the User-Defined brightness, system screen off timer (settings-> sound & Display-> display settings-> screen timeout) starts timing. If any activity event occurs before the timer time reaches, for example, touch click, keyboard pressed, and other events, the reset screen off timer will be kept in awake state. if an application has applied for full wake lock during this period, the system will remain in the awake status unless the user presses the power key. in awake status, if the battery power is low or screen off timer is powered by AC In the keep screen on while pluged in option, backlight will be forcibly adjusted to the dim status. if the screen off timer time is reached and there is no full wake lock or the user presses the power key, the system status will be switched to notification and all registered g_early_suspend_handlers functions will be called, generally, the LCD and backlight drivers are registered as early suspend. If necessary, you can register other drivers as early suspend, which will be disabled in the first phase. next, the system will determine whether there is partial wake lock acquired. If yes, it will wait for it to be released. If a user activity event occurs during the waiting process, the system will return to the awake status immediately; if there is no partial wake lock acquired, the system will immediately call the function P M_suspend Disable other related drive to bring the CPU to sleep. if the system detects any wakeup source during sleep, the CPU will be awakened from sleep and the related driver's Resume function will be called, next, call the previously registered early suspend-driven Resume function, and return the system status to awake. the problem is that all functions that have registered early suspend can be called in the first stage of suspend. However, during resume, Linux will first call all the resume functions of the driver, what is the significance of calling the previously registered early suspend-driven Resume function? I personally think that the early suspend and late resume functions of Android should be used together with suspend and resume in Linux, rather than using a queue for management separately.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.