1 )、 PowerManager.java
该文件定义了一个 PowerManager 类供其它文件使用,该类嵌套了一个 nest class :
public class WakeLock ,该 nest class 有如下方法:
public void setReferenceCounted(boolean value) public void acquire() public void acquire(long timeout) public void release() public void release(int flags) public boolean isHeld() public String toString() @Override protected void finalize() throws Throwable |
该 nest class 的构造方法为:
WakeLock(int flags, String tag)
其中有一个关键的参数 flags ,它有如下几种情况:
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
上面4 种是互斥的,即只能指定其中之一,但可以与下面两种flag 不是互斥的:
ACQUIRE_CAUSES_WAKEUP: 一旦有请求锁时强制打开 Screen 和 keyboard light
ON_AFTER_RELEASE: 在释放锁时 reset activity timer
除了这个内部类, powermanager 类定义了如下方法:
public WakeLock newWakeLock(int flags, String tag) // 该方法 return 一个 nest class 的一个对象。而我们看到, nest class 实际上完成了 WakeLock 的申请与释放 public void userActivity(long when, boolean noChangeLights) public void goToSleep(long time) public void setBacklightBrightness(int brightness) public int getSupportedWakeLockFlags() public boolean isScreenOn() |
该类的构造方法有两个:
private PowerManager()
public PowerManager(IPowerManager service, Handler handler)
2 、 Power.java
PowerManagerSerivive.java 中调用了一些本地方法,该文件作为这些方法的 java 层与 jni 的中间层,声明了本地接口。
public static native void acquireWakeLock(int lock, String id); public static native void releaseWakeLock(String id); public static native int setScreenState(boolean on); public static native int setLastUserActivityTimeout(long ms); @Deprecated public static native void shutdown(); public static void reboot(String reason) throws IOException |
3 、 andriod_ow_Power.cpp
该文件编写了本地实现方法,从本地方法列表可以知道该本地方法与 power.c 是密切相关的:
static JNINativeMethod method_table[] = { { "acquireWakeLock", "(ILjava/lang/String;)V", (void*)acquireWakeLock }, { "releaseWakeLock", "(Ljava/lang/String;)V", (void*)releaseWakeLock }, { "setLastUserActivityTimeout", "(J)I", (void*)setLastUserActivityTimeout }, { "setScreenState", "(Z)I", (void*)setScreenState }, { "shutdown", "()V", (void*)android_os_Power_shutdown }, { "rebootNative", "(Ljava/lang/String;)V", (void*)android_os_Power_reboot }, }; |
4 、 power.c
该文件作为 Android 系统的最底层,与 Linux 内核的 power manager 交互。
static int64_t systemTime() ;
static int open_file_descriptors(const char * const paths[]) ;
static inline void initialize_fds(void) ;
int acquire_wake_lock(int lock, const char* id) ;
int set_last_user_activity_timeout(int64_t delay) ;
int set_screen_state(int on) ;