2012-02-26 22:34:23 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-08-02 11:03:58 -07:00
|
|
|
package org.mozilla.gecko.util;
|
2012-02-26 22:34:23 -08:00
|
|
|
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
|
|
|
|
import java.util.concurrent.SynchronousQueue;
|
|
|
|
|
2012-08-02 11:03:58 -07:00
|
|
|
public final class GeckoBackgroundThread extends Thread {
|
2012-02-26 22:34:23 -08:00
|
|
|
private static final String LOOPER_NAME = "GeckoBackgroundThread";
|
|
|
|
|
|
|
|
// Guarded by 'this'.
|
|
|
|
private static Handler sHandler = null;
|
|
|
|
private SynchronousQueue<Handler> mHandlerQueue = new SynchronousQueue<Handler>();
|
|
|
|
|
|
|
|
// Singleton, so private constructor.
|
|
|
|
private GeckoBackgroundThread() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
setName(LOOPER_NAME);
|
|
|
|
Looper.prepare();
|
|
|
|
try {
|
|
|
|
mHandlerQueue.put(new Handler());
|
|
|
|
} catch (InterruptedException ie) {}
|
|
|
|
|
|
|
|
Looper.loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a Handler for a looper thread, or create one if it doesn't yet exist.
|
|
|
|
public static synchronized Handler getHandler() {
|
|
|
|
if (sHandler == null) {
|
|
|
|
GeckoBackgroundThread lt = new GeckoBackgroundThread();
|
|
|
|
lt.start();
|
|
|
|
try {
|
|
|
|
sHandler = lt.mHandlerQueue.take();
|
|
|
|
} catch (InterruptedException ie) {}
|
|
|
|
}
|
|
|
|
return sHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void post(Runnable runnable) {
|
|
|
|
Handler handler = getHandler();
|
|
|
|
if (handler == null) {
|
|
|
|
throw new IllegalStateException("No handler! Must have been interrupted. Not posting.");
|
|
|
|
}
|
|
|
|
handler.post(runnable);
|
|
|
|
}
|
|
|
|
}
|