2013-10-25 15:19:00 +00:00
|
|
|
/*
|
2013-12-20 19:25:49 +00:00
|
|
|
* This is the source code of Telegram for Android v. 1.3.2.
|
2013-10-25 15:19:00 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.messenger;
|
|
|
|
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.os.Message;
|
|
|
|
|
|
|
|
public class DispatchQueue extends Thread {
|
|
|
|
public Handler handler;
|
|
|
|
private final Object handlerSyncObject = new Object();
|
|
|
|
|
|
|
|
public DispatchQueue(final String threadName) {
|
|
|
|
setName(threadName);
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendMessage(Message msg, int delay) {
|
|
|
|
if (handler == null) {
|
|
|
|
try {
|
|
|
|
synchronized (handlerSyncObject) {
|
|
|
|
handlerSyncObject.wait();
|
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handler != null) {
|
|
|
|
if (delay <= 0) {
|
|
|
|
handler.sendMessage(msg);
|
|
|
|
} else {
|
|
|
|
handler.sendMessageDelayed(msg, delay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void postRunnable(Runnable runnable) {
|
2014-02-04 18:36:55 +00:00
|
|
|
postRunnable(runnable, 0, false);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
public void postRunnable(Runnable runnable, boolean inFront) {
|
|
|
|
postRunnable(runnable, 0, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void postRunnable(Runnable runnable, int delay, boolean inFront) {
|
2013-10-25 15:19:00 +00:00
|
|
|
if (handler == null) {
|
|
|
|
try {
|
|
|
|
synchronized (handlerSyncObject) {
|
|
|
|
handlerSyncObject.wait();
|
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handler != null) {
|
|
|
|
if (delay <= 0) {
|
2014-02-04 18:36:55 +00:00
|
|
|
if (inFront) {
|
|
|
|
handler.postAtFrontOfQueue(runnable);
|
|
|
|
} else {
|
|
|
|
handler.post(runnable);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
|
|
|
handler.postDelayed(runnable, delay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
Looper.prepare();
|
|
|
|
handler = new Handler();
|
|
|
|
synchronized (handlerSyncObject) {
|
|
|
|
handlerSyncObject.notify();
|
|
|
|
}
|
|
|
|
Looper.loop();
|
|
|
|
}
|
|
|
|
}
|