Nagram/TMessagesProj/src/main/java/org/telegram/messenger/DispatchQueue.java

77 lines
1.9 KiB
Java
Raw Normal View History

2013-10-25 15:19:00 +00:00
/*
* This is the source code of Telegram for Android v. 1.2.3.
* 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) {
postRunnable(runnable, 0);
}
public void postRunnable(Runnable runnable, int delay) {
if (handler == null) {
try {
synchronized (handlerSyncObject) {
handlerSyncObject.wait();
}
} catch (Throwable t) {
t.printStackTrace();
}
}
if (handler != null) {
if (delay <= 0) {
handler.post(runnable);
} else {
handler.postDelayed(runnable, delay);
}
}
}
public void run() {
Looper.prepare();
handler = new Handler();
synchronized (handlerSyncObject) {
handlerSyncObject.notify();
}
Looper.loop();
}
}