隨筆-1  評論-68  文章-98  trackbacks-0
          整理:徐建祥(netpirate@gmail.com)
          日期:2009/09/16
          網址: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

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

          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
          主站蜘蛛池模板: 喀什市| 全南县| 龙江县| 富顺县| 饶河县| 贵德县| 宜兴市| 德江县| 墨竹工卡县| 镇安县| 嘉荫县| 合肥市| 乌拉特前旗| 丹寨县| 南岸区| 德保县| 香格里拉县| 隆回县| 乌拉特前旗| 托克托县| 丰宁| 桂东县| 台中市| 江陵县| 平泉县| 沾化县| 拜城县| 准格尔旗| 壤塘县| 专栏| 伊春市| 建宁县| 德清县| 宁津县| 德庆县| 旺苍县| 罗甸县| 贵州省| 安化县| 柘荣县| 海阳市|