-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathDispatchQueue.java
151 lines (127 loc) · 3.77 KB
/
DispatchQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* This is the source code of Telegram for Android v. 7.x.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Nikolai Kudashov, 2013-2020.
*/
package org.telegram.messenger;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.os.SystemClock;
import java.util.concurrent.CountDownLatch;
public class DispatchQueue extends Thread {
private static final int THREAD_PRIORITY_DEFAULT = -1000;
private volatile Handler handler = null;
private CountDownLatch syncLatch = new CountDownLatch(1);
private long lastTaskTime;
private static int indexPointer = 0;
public final int index = indexPointer++;
private int threadPriority = THREAD_PRIORITY_DEFAULT;
public DispatchQueue(final String threadName) {
this(threadName, true);
}
public DispatchQueue(final String threadName, boolean start) {
setName(threadName);
if (start) {
start();
}
}
public DispatchQueue(final String threadName, boolean start, int priority) {
this.threadPriority = priority;
setName(threadName);
if (start) {
start();
}
}
public void sendMessage(Message msg, int delay) {
try {
syncLatch.await();
if (delay <= 0) {
handler.sendMessage(msg);
} else {
handler.sendMessageDelayed(msg, delay);
}
} catch (Exception ignore) {
}
}
public void cancelRunnable(Runnable runnable) {
try {
syncLatch.await();
handler.removeCallbacks(runnable);
} catch (Exception e) {
FileLog.e(e, false);
}
}
public void cancelRunnables(Runnable[] runnables) {
try {
syncLatch.await();
for (int i = 0; i < runnables.length; i++) {
handler.removeCallbacks(runnables[i]);
}
} catch (Exception e) {
FileLog.e(e, false);
}
}
public boolean postRunnable(Runnable runnable) {
lastTaskTime = SystemClock.elapsedRealtime();
return postRunnable(runnable, 0);
}
public boolean postToFrontRunnable(Runnable runnable) {
try {
syncLatch.await();
} catch (Exception e) {
FileLog.e(e, false);
}
return handler.postAtFrontOfQueue(runnable);
}
public boolean postRunnable(Runnable runnable, long delay) {
try {
syncLatch.await();
} catch (Exception e) {
FileLog.e(e, false);
}
if (delay <= 0) {
return handler.post(runnable);
} else {
return handler.postDelayed(runnable, delay);
}
}
public void cleanupQueue() {
try {
syncLatch.await();
handler.removeCallbacksAndMessages(null);
} catch (Exception e) {
FileLog.e(e, false);
}
}
public void handleMessage(Message inputMessage) {
}
public long getLastTaskTime() {
return lastTaskTime;
}
public void recycle() {
handler.getLooper().quit();
}
@Override
public void run() {
Looper.prepare();
handler = new Handler(Looper.myLooper(), msg -> {
DispatchQueue.this.handleMessage(msg);
return true;
});
syncLatch.countDown();
if (threadPriority != THREAD_PRIORITY_DEFAULT) {
Process.setThreadPriority(threadPriority);
}
Looper.loop();
}
public boolean isReady() {
return syncLatch.getCount() == 0;
}
public Handler getHandler() {
return handler;
}
}