blob: 97327d7af1fe64cf42a70042d57659a6ce0c437b (
plain)
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
|
package com.libmailcore;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import java.util.HashMap;
class MainThreadUtils {
private static volatile MainThreadUtils instance = new MainThreadUtils();
private Handler handler;
private HashMap<Long, Runnable> runnablesForIdentifiers = new HashMap<Long, Runnable>();
static MainThreadUtils singleton() {
return instance;
}
// private constructor
private MainThreadUtils() {
System.loadLibrary("MailCore");
handler = new Handler(Looper.getMainLooper());
setupNative();
}
private native void setupNative();
private native void runIdentifier(long identifier);
private native void runIdentifierAndNotify(long identifier);
private void runOnMainThread(final long identifier) {
handler.post(new Runnable() {
@Override
public void run() {
runIdentifier(identifier);
}
});
}
private void runOnMainThreadAndWait(final long identifier) {
handler.post(new Runnable() {
@Override
public void run() {
runIdentifierAndNotify(identifier);
}
});
}
private void runAfterDelay(final long identifier, int milliseconds) {
Runnable runnable = new Runnable() {
@Override
public void run() {
runnablesForIdentifiers.remove(new Long(identifier));
runIdentifier(identifier);
}
};
runnablesForIdentifiers.put(new Long(identifier), runnable);
handler.postDelayed(runnable, milliseconds);
}
private void cancelDelayedRun(final long identifier) {
Runnable runnable = runnablesForIdentifiers.get(new Long(identifier));
runnablesForIdentifiers.remove(new Long(identifier));
handler.removeCallbacks(runnable);
}
}
|