That fuck shit the fascists are using
1package org.tm.archive.util;
2
3import android.content.Context;
4import android.os.PowerManager;
5import android.os.PowerManager.WakeLock;
6
7import androidx.annotation.NonNull;
8import androidx.annotation.Nullable;
9
10import org.signal.core.util.logging.Log;
11
12public class WakeLockUtil {
13
14 private static final String TAG = Log.tag(WakeLockUtil.class);
15
16 /**
17 * Run a runnable with a wake lock. Ensures that the lock is safely acquired and released.
18 *
19 * @param tag will be prefixed with "signal:" if it does not already start with it.
20 */
21 public static void runWithLock(@NonNull Context context, int lockType, long timeout, @NonNull String tag, @NonNull Runnable task) {
22 WakeLock wakeLock = null;
23 try {
24 wakeLock = acquire(context, lockType, timeout, tag);
25 task.run();
26 } finally {
27 if (wakeLock != null) {
28 release(wakeLock, tag);
29 }
30 }
31 }
32
33 /**
34 * @param tag will be prefixed with "signal:" if it does not already start with it.
35 */
36 public static WakeLock acquire(@NonNull Context context, int lockType, long timeout, @NonNull String tag) {
37 tag = prefixTag(tag);
38 try {
39 PowerManager powerManager = ServiceUtil.getPowerManager(context);
40 WakeLock wakeLock = powerManager.newWakeLock(lockType, tag);
41
42 wakeLock.acquire(timeout);
43
44 return wakeLock;
45 } catch (Exception e) {
46 Log.w(TAG, "Failed to acquire wakelock with tag: " + tag, e);
47 return null;
48 }
49 }
50
51 /**
52 * @param tag will be prefixed with "signal:" if it does not already start with it.
53 */
54 public static void release(@Nullable WakeLock wakeLock, @NonNull String tag) {
55 tag = prefixTag(tag);
56 try {
57 if (wakeLock == null) {
58 Log.d(TAG, "Wakelock was null. Skipping. Tag: " + tag);
59 } else if (wakeLock.isHeld()) {
60 wakeLock.release();
61 } else {
62 Log.d(TAG, "Wakelock wasn't held at time of release: " + tag);
63 }
64 } catch (Exception e) {
65 Log.w(TAG, "Failed to release wakelock with tag: " + tag, e);
66 }
67 }
68
69 private static String prefixTag(@NonNull String tag) {
70 return tag.startsWith("signal:") ? tag : "signal:" + tag;
71 }
72}