Skip to content

Instantly share code, notes, and snippets.

@AshuTyagi16
Created September 25, 2020 13:36
Show Gist options
  • Save AshuTyagi16/be091af1abbd8640ff2301714decc452 to your computer and use it in GitHub Desktop.
Save AshuTyagi16/be091af1abbd8640ff2301714decc452 to your computer and use it in GitHub Desktop.
@SystemService(Context.POWER_SERVICE)
public final class PowerManager {
final IPowerManager mService;
public final class WakeLock {
@UnsupportedAppUsage
private int mFlags;
@UnsupportedAppUsage
private String mTag;
private final String mPackageName;
private final IBinder mToken;
private final String mTraceName;
WakeLock(int flags, String tag, String packageName) {
// Create a token that uniquely identifies this wake lock.
mToken = new Binder();
mFlags = flags;
mTag = tag;
mPackageName = packageName;
mTraceName = "WakeLock (" + mTag + ")";
}
public void acquire() {
// Send the power manager service a request to acquire a wake
// lock for the application. Include the token as part of the
// request so that the power manager service can validate the
// application's identity when it requests to release the wake
// lock later on.
synchronized (mToken) {
try {
mService.acquireWakeLock(mToken, mFlags, mTag);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}
public void release(int flags) {
// Send the power manager service a request to release the
// wake lock associated with 'mToken'.
synchronized (mToken) {
try {
mService.releaseWakeLock(mToken, flags);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}
}
public WakeLock newWakeLock(int levelAndFlags, String tag) {
return new WakeLock(levelAndFlags, tag);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment