use of android.os.PowerManager.WakeLock in project Signal-Android by signalapp.
the class WakeLockUtil method runWithLock.
/**
* Run a runnable with a wake lock. Ensures that the lock is safely acquired and released.
*
* @param tag will be prefixed with "signal:" if it does not already start with it.
*/
public static void runWithLock(@NonNull Context context, int lockType, long timeout, @NonNull String tag, @NonNull Runnable task) {
WakeLock wakeLock = null;
try {
wakeLock = acquire(context, lockType, timeout, tag);
task.run();
} finally {
if (wakeLock != null) {
release(wakeLock, tag);
}
}
}
use of android.os.PowerManager.WakeLock in project android_frameworks_opt_telephony by LineageOS.
the class NitzSignalInputFilterPredicateFactory method createBogusElapsedRealtimeCheck.
/**
* Returns a {@link TrivalentPredicate} function that implements a check for a bad reference
* time associated with {@code newSignal}. The function can return {@code false} or
* {@code null}.
*/
@VisibleForTesting
@NonNull
public static TrivalentPredicate createBogusElapsedRealtimeCheck(@NonNull Context context, @NonNull DeviceState deviceState) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
final WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG);
return (oldSignal, newSignal) -> {
Objects.requireNonNull(newSignal);
// Validate the newSignal to reject obviously bogus elapsedRealtime values.
try {
// Acquire the wake lock as we are reading the elapsed realtime clock below.
wakeLock.acquire();
long elapsedRealtime = deviceState.elapsedRealtime();
long millisSinceNitzReceived = elapsedRealtime - newSignal.getReferenceTimeMillis();
if (millisSinceNitzReceived < 0 || millisSinceNitzReceived > Integer.MAX_VALUE) {
if (DBG) {
Rlog.d(LOG_TAG, "mustProcessNitzSignal: Not processing NITZ signal" + " because unexpected elapsedRealtime=" + elapsedRealtime + " nitzSignal=" + newSignal);
}
return false;
}
return null;
} finally {
wakeLock.release();
}
};
}
Aggregations