隨筆-1  評論-68  文章-98  trackbacks-0
          整理:徐建祥(netpirate@gmail.com)
          日期:2009/09/16
          網(wǎng)址:http://www.anymobile.org

          Index
          1. Introduction
          2. Architecture
          3. How to Work
          4. Wake Locks
          5. Wake Lock Example
          6. Music Application

          1.Introduction

          Android supports its own Power Management (on top of the standard Linux Power Management) designed with the premise that the CPU shouldn't consume power if no applications or services require power. For more information regarding standard Linux power management, please see Linux Power Management Support at http://kernel.org.
          Android requires that applications and services request CPU resources with "wake locks" through the Android application framework and native Linux libraries. If there are no active wake locks, Android will shut down the CPU.

          2.Architecture




          Framework Layer
          /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
          /frameworks/base/core/jni/android_os_power.cpp
          /hardware/libhardware_legacy/power/power.c
              "/sys/power/wake_lock"
              "/sys/power/wake_unlock"
              "/sys/power/state"
              "/sys/android_power/acquire_partial_wake_lock"
              "/sys/android_power/release_wake_lock"
              "/sys/android_power/request_state"
              … …
          Kernel Layer
          /drivers/android/power.c
          /drivers/power/apm_power.c (Advanced Power Management)


          3.How to Work

          系統(tǒng)正常開機后進入到AWAKE狀態(tài),Backlight會從最亮慢慢調(diào)節(jié)到用戶設(shè)定的亮度,系統(tǒng)screen off timer(settings->sound & display-> Display settings -> Screen timeout)開始計時,在計時時間到之前,如果有任何的activity事件發(fā)生,如Touch click, keyboard pressed等事件,則將Reset screen off timer, 系統(tǒng)保持在AWAKE狀態(tài)。如果有應(yīng)用程序在這段時間內(nèi)申請了Full wake lock,那么系統(tǒng)也將保持在AWAKE狀態(tài),除非用戶按下power key. 在AWAKE狀態(tài)下如果電池電量低或者是用AC供電screen off timer時間到并且選中Keep screen on while pluged in選項,backlight會被強制調(diào)節(jié)到DIM的狀態(tài)。
          如果Screen off timer時間到并且沒有Full wake lock或者用戶按了power key,那么系統(tǒng)狀態(tài)將被切換到NOTIFICATION,并且調(diào)用所有已經(jīng)注冊的g_early_suspend_handlers函數(shù),通常會把LCD和Backlight驅(qū)動注冊成early suspend類型,如有需要也可以把別的驅(qū)動注冊成early suspend,這樣就會在第一階段被關(guān)閉. 接下來系統(tǒng)會判斷是否有partial wake lock acquired,如果有則等待其釋放,在等待的過程中如果有user activity事件發(fā)生,系統(tǒng)則馬上回到AWAKE狀態(tài);如果沒有partial wake lock acquired,則系統(tǒng)會馬上調(diào)用函數(shù)pm_suspend關(guān)閉其它相關(guān)的驅(qū)動,讓CPU進入休眠狀態(tài)。
          系統(tǒng)在Sleep狀態(tài)時如果檢測到任何一個Wakeup source,則CPU會從Sleep狀態(tài)被喚醒,并且調(diào)用相關(guān)的驅(qū)動的resume函數(shù),接下來馬上調(diào)用前期注冊的early suspend驅(qū)動的resume函數(shù),最后系統(tǒng)狀態(tài)回到AWAKE狀態(tài).

          Registering Kernel-level Drivers with the PM Driver
          #Be notified immediately before power down
          android_register_early_suspend(android_early_suspend_t *handler)
          #Be notified immediately after power up
          android_register_early_resume(android_early_resume_t *handler)
          HARDWARE LIGHTS
          #define LIGHT_ID_BACKLIGHT          "backlight"
          #define LIGHT_ID_KEYBOARD           "keyboard"
          #define LIGHT_ID_BUTTONS            "buttons"
          #define LIGHT_ID_BATTERY            "battery"
          #define LIGHT_ID_NOTIFICATIONS      "notifications"
          #define LIGHT_ID_ATTENTION          "attention"
          #define LIGHT_ID_BLUETOOTH          "bluetooth"
          #define LIGHT_ID_WIFI               "wifi"

          4.Wake Locks

          Wake locks are used by applications and services to request CPU resources.
          Types of Wake Locks:
          -ACQUIRE_CAUSES_WAKEUP: Normally wake locks don't actually wake the device, they just cause it to remain on once it's already on.
          -FULL_WAKE_LOCK:  The screen and keyboard are on at full brightness
          -ON_AFTER_RELEASE: When this wake lock is released, poke the user activity timer
          -PARTIAL_WAKE_LOCK: The CPU is running, The screen might not be on.
          -SCREEN_BRIGHT_WAKE_LOCK: The screen is on at full brightness; the keyboard backlight will be allowed to go off.
          -SCREEN_DIM_WAKE_LOCK: The screen is on, but the keyboard backlight will be allowed to go off, and the screen backlight will be allowed to go dim.

          5.Wake Lock Example

          1). Acquire handle to the PowerManager service.
          <uses-permission android:name="android.permission.WAKE_LOCK" />
          <uses-permission android:name="android.permission.DEVICE_POWER" />

          PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);

          2). Create a wake lock and specify the power management flags for screen, timeout, etc.

          PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG);

          3). Acquire wake lock.
          wl.acquire();

          4). Perform operation (play MP3, open HTML page, etc.).

          5). Release wake lock.
          wl.release();

          6.Music Application

          /packages/apps/Music/AndroidManifest.xml
          <uses-permission android:name="android.permission.WAKE_LOCK" />
          /packages/apps/Music/src/.../MediaPlayerService.java PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
          WakeLock mWakeLock =
              pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,    
             this.getClass().getName());
          #Sets this WakeLock is not ref counted.
          mWakeLock.setReferenceCounted(false);

          #The lock will be released after 30 seconds.
          mWakeLock.acquire(30000);

          mHandler.sendEmptyMessage(RELEASE_WAKELOCK);
          #Release the claim to the CPU or screen being on.
          mWakeLock.release();

          Reference

          Android Platform Development Kit:Power Management
          http://www.netmite.com/android/mydroid/development/pdk/docs/power_management.html

          Android Power Management(Steve Guo)
          http://letsgoustc.spaces.live.com/blog/cns!89AD27DFB5E249BA!526.entry

          Android 電源管理(hzdysymbol)
          http://blog.csdn.net/hzdysymbol/archive/2009/03/19/4004791.aspx

          Linux Power Management Support
          http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.24.y.git;a=blob;f=Documentation/pm.txt

           

          posted on 2010-08-12 02:27 Xu Jianxiang 閱讀(2307) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 许昌县| 泸西县| 广元市| 西宁市| 桐城市| 内丘县| 和龙市| 瓮安县| 桐梓县| 平利县| 山丹县| 察隅县| 雷山县| 汉阴县| 荔浦县| 漠河县| 延寿县| 清涧县| 固始县| 阿拉善右旗| 习水县| 嘉兴市| 易门县| 宁阳县| 墨竹工卡县| 尼玛县| 杭锦后旗| 隆回县| 贡觉县| 苍南县| 长葛市| 怀化市| 县级市| 礼泉县| 滦南县| 彩票| 德令哈市| 木里| 吴堡县| 高要市| 淮阳县|