Powermanager source code

Source: Internet
Author: User
Http://hi-android.info/src/android/ OS /PowerManager.java.html
The powermanager class provides interfaces for Android power management. It is located in Android/OS/powermanager. java, But it implements few things. It mainly calls some lower-level (C/C ++) functions. for how to use the powermanager class to manage the android power supply, see powermanager, Android power management overviewSource File: powermanager. java package android. OS; import android. util. log; import COM. android. internal. OS. runtimeinit; public class powermanager {Private Static final string tag = "powermanager"; Private Static final int wake_bit_cpu_strong = 1; Private Static final int wake_bit_cpu_weak = 2; Private Static final int wake_bit_screen_dim = 4; private Static final int wake_bit_screen_bright = 8; Private Static Final int completion = 16; Private Static final int completion = 32; Private Static final int lock_mask = wake_bit_cpu_strong | strong; public static final int partial_wake_lock = wake_bit_cpu_strong; public static final int full_wake_lock = wake_bit _ Cpu_weak | percent; public static final int screen_bright_wake_lock = wake_bit_cpu_weak | wake_bit_screen_bright; public static final int screen_dim_wake_lock = wake_bit_cpu_weak | percent; /*** wake lock that turns the screen off when the proximity sensor activates. * Since not all devices have proximity sensors, use * {@ link # getsupportedwa Kelockflags () getsupportedwakelockflags ()} to determine if * this wake lock mode is supported. ** {@ hide} */public static final int proximity_screen_off_wake_lock = wake_bit_proximity_screen_off;/*** flag for {@ link wakelock # release (INT )} to defer releasing a * {@ link # wake_bit_proximity_screen_off} wakelock until the proximity sensor returns * a negative value. ** {@ hide} */Public s Tatic final int wait_for_proximity_negative = 1; public static final int acquire_causes_wakeup = 0x10000000; public static final int on_after_release = 0x20000000; /*** class lets you say that you need to have the device on. ** <p> call release when you are done and don't need the lock anymore. */public class wakelock {static final int release_wake_lock = 1; runnable mreleaser = new runnable () {Public void run () {release () ;}}; int mflags; string mtag; ibinder mtoken; int mcount = 0; Boolean mrefcounted = true; Boolean mheld = false; worksource mworksource; wakelock (INT flags, string tag) {Switch (flags & lock_mask) {Case partial_wake_lock: Case screen_dim_wake_lock: case when: Case full_wake_lock: case when: break; default: throw new Il Legalargumentexception ();} mflags = flags; mtag = tag; mtoken = New binder ();}/*** sets whether this wakelock is ref counted. ** <p> wake locks are reference counted by default. ** @ Param value true for ref counted, false for not Ref counted. */Public void setreferencecounted (Boolean value) {mrefcounted = value;}/*** makes sure the device is on at the level you asked when you created * t He wake lock. */Public void acquire () {synchronized (mtoken) {If (! Mrefcounted | mcount ++ = 0) {try {mservice. acquirewakelock (mflags, mtoken, mtag, mworksource);} catch (RemoteException e) {} mheld = true ;}}} /*** makes sure the device is on at the level you asked when you created * The wake lock. the lock will be released after the given timeout. ** @ Param timeout release the lock after the give timeout in milliseconds. */Public void acquire (long time Out) {acquire (); mhandler. postdelayed (mreleaser, timeout);}/*** release your claim to the CPU or screen being on. ** <p> * It may turn off shortly after you release it, or it may not if there * are other wake locks held. */Public void release () {release (0);}/*** release your claim to the CPU or screen being on. * @ Param flags combination of flag values to modify the release behavior. * C Urrently only {@ link # wait_for_proximity_negative} is supported. ** <p> * It may turn off shortly after you release it, or it may not if there * are other wake locks held. ** {@ hide} */Public void release (INT flags) {synchronized (mtoken) {If (! Mrefcounted | -- mcount = 0) {try {mservice. releasewakelock (mtoken, flags);} catch (RemoteException e) {} mheld = false;} If (mcount <0) {Throw new runtimeexception ("wakelock under-locked" + mtag) ;}} public Boolean isheld () {synchronized (mtoken) {return mheld ;}} public void setworksource (worksource ws) {synchronized (mtoken) {If (WS! = NULL & ws. size () = 0) {Ws = NULL;} Boolean changed = true; If (Ws = NULL) {mworksource = NULL;} else if (mworksource = NULL) {changed = mworksource! = NULL; mworksource = new worksource (WS);} else {changed = mworksource. diff (WS); If (changed) {mworksource. set (WS) ;}}if (changed & mheld) {try {mservice. updatewakelockworksource (mtoken, mworksource);} catch (RemoteException e) {}}} Public String tostring () {synchronized (mtoken) {return "wakelock {" + integer. tohexstring (system. identityhashcode (this) + "held =" + mheld + ", refcoun T = "+ mcount +"} ";}}@ override protected void finalize () throws throwable {synchronized (mtoken) {If (mheld) {log. WTF (TAG, "wakelock finalized while still held:" + mtag); try {mservice. releasewakelock (mtoken, 0);} catch (RemoteException e) {}}}} public wakelock newwakelock (INT flags, string tag) {If (TAG = NULL) {Throw new nullpointerexception ("tag is null in powermanager. newwake Lock ");} return New wakelock (flags, tag);} public void useractivity (long when, Boolean nochangelights) {try {mservice. useractivity (when, nochangelights);} catch (RemoteException e) {}} public void Gotosleep (long time) {try {mservice. gotosleep (time);} catch (RemoteException e) {}}/*** sets the brightness of the backlights (screen, keyboard, button ). ** @ Param brightness value from 0 To 255 ** {@ hide} */Public void setbacklightbrightness (INT brightness) {try {mservice. setbacklightbrightness (brightness);} catch (RemoteException e) {}}/*** returns the set of flags for {@ link # newwakelock (INT, string) newwakelock ()} * That are supported on the device. * For example, to test to see if the {@ link # proximity_screen_off_wake_lock} * is supported: ** {@ samplecode * powermana Ger pm = (powermanager) getsystemservice (context. power_service); * int supportedflags = PM. getsupportedwakelockflags (); * Boolean proximitysupported = (supportedflags & powermanager. proximity_screen_off_wake_lock) * = powermanager. proximity_screen_off_wake_lock); *} ** @ return the set of supported wakelock flags. ** {@ hide} */Public int getsupportedwakelockflags () {try {return mservice. get Supportedwakelockflags ();} catch (RemoteException e) {return 0 ;}public Boolean isscreenon () {try {return mservice. isscreenon () ;}catch (RemoteException e) {return false ;}} public void reboot (string reason) {try {mservice. reboot (reason);} catch (RemoteException e) {}} private powermanager () {}public powermanager (ipowermanager service, Handler handler) {mservice = service; mhandl ER = handler;} ipowermanager mservice; handler mhandler;} Note: * {@ hide} indicates that this method is hidden from external developers. Android APIs do not provide this method. It can only be used in the framework or application of the Android system.

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.