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.javaPowerManagerSerivive.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);
|
|