|
|
@ -13,6 +13,8 @@ public class WakeLockUtil {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Run a runnable with a wake lock. Ensures that the lock is safely acquired and released.
|
|
|
|
* 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) {
|
|
|
|
public static void runWithLock(@NonNull Context context, int lockType, long timeout, @NonNull String tag, @NonNull Runnable task) {
|
|
|
|
WakeLock wakeLock = null;
|
|
|
|
WakeLock wakeLock = null;
|
|
|
@ -26,7 +28,11 @@ public class WakeLockUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param tag will be prefixed with "signal:" if it does not already start with it.
|
|
|
|
|
|
|
|
*/
|
|
|
|
public static WakeLock acquire(@NonNull Context context, int lockType, long timeout, @NonNull String tag) {
|
|
|
|
public static WakeLock acquire(@NonNull Context context, int lockType, long timeout, @NonNull String tag) {
|
|
|
|
|
|
|
|
tag = prefixTag(tag);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
PowerManager powerManager = ServiceUtil.getPowerManager(context);
|
|
|
|
PowerManager powerManager = ServiceUtil.getPowerManager(context);
|
|
|
|
WakeLock wakeLock = powerManager.newWakeLock(lockType, tag);
|
|
|
|
WakeLock wakeLock = powerManager.newWakeLock(lockType, tag);
|
|
|
@ -41,7 +47,11 @@ public class WakeLockUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param tag will be prefixed with "signal:" if it does not already start with it.
|
|
|
|
|
|
|
|
*/
|
|
|
|
public static void release(@NonNull WakeLock wakeLock, @NonNull String tag) {
|
|
|
|
public static void release(@NonNull WakeLock wakeLock, @NonNull String tag) {
|
|
|
|
|
|
|
|
tag = prefixTag(tag);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (wakeLock.isHeld()) {
|
|
|
|
if (wakeLock.isHeld()) {
|
|
|
|
wakeLock.release();
|
|
|
|
wakeLock.release();
|
|
|
@ -53,4 +63,8 @@ public class WakeLockUtil {
|
|
|
|
Log.w(TAG, "Failed to release wakelock with tag: " + tag, e);
|
|
|
|
Log.w(TAG, "Failed to release wakelock with tag: " + tag, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String prefixTag(@NonNull String tag) {
|
|
|
|
|
|
|
|
return tag.startsWith("signal:") ? tag : "signal:" + tag;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|