Merge "Make it easier to create asynchronous Handlers." into jb-mr1-dev

This commit is contained in:
Jeff Brown
2012-08-25 13:43:48 -07:00
committed by Android (Google) Code Review
7 changed files with 84 additions and 48 deletions

View File

@@ -101,37 +101,88 @@ public class Handler {
} }
/** /**
* Default constructor associates this handler with the queue for the * Default constructor associates this handler with the {@link Looper} for the
* current thread. * current thread.
* *
* If there isn't one, this handler won't be able to receive messages. * If this thread does not have a looper, this handler won't be able to receive messages
* so an exception is thrown.
*/ */
public Handler() { public Handler() {
if (FIND_POTENTIAL_LEAKS) { this(null, false);
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = null;
mAsynchronous = false;
} }
/** /**
* Constructor associates this handler with the queue for the * Constructor associates this handler with the {@link Looper} for the
* current thread and takes a callback interface in which you can handle * current thread and takes a callback interface in which you can handle
* messages. * messages.
*
* If this thread does not have a looper, this handler won't be able to receive messages
* so an exception is thrown.
*
* @param callback The callback interface in which to handle messages, or null.
*/ */
public Handler(Callback callback) { public Handler(Callback callback) {
this(callback, false);
}
/**
* Use the provided {@link Looper} instead of the default one.
*
* @param looper The looper, must not be null.
*/
public Handler(Looper looper) {
this(looper, null, false);
}
/**
* Use the provided {@link Looper} instead of the default one and take a callback
* interface in which to handle messages.
*
* @param looper The looper, must not be null.
* @param callback The callback interface in which to handle messages, or null.
*/
public Handler(Looper looper, Callback callback) {
this(looper, callback, false);
}
/**
* Use the {@link Looper} for the current thread
* and set whether the handler should be asynchronous.
*
* Handlers are synchronous by default unless this constructor is used to make
* one that is strictly asynchronous.
*
* Asynchronous messages represent interrupts or events that do not require global ordering
* with represent to synchronous messages. Asynchronous messages are not subject to
* the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}.
*
* @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for
* each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
*
* @hide
*/
public Handler(boolean async) {
this(null, async);
}
/**
* Use the {@link Looper} for the current thread with the specified callback interface
* and set whether the handler should be asynchronous.
*
* Handlers are synchronous by default unless this constructor is used to make
* one that is strictly asynchronous.
*
* Asynchronous messages represent interrupts or events that do not require global ordering
* with represent to synchronous messages. Asynchronous messages are not subject to
* the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}.
*
* @param callback The callback interface in which to handle messages, or null.
* @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for
* each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
*
* @hide
*/
public Handler(Callback callback, boolean async) {
if (FIND_POTENTIAL_LEAKS) { if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass(); final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
@@ -148,32 +199,11 @@ public class Handler {
} }
mQueue = mLooper.mQueue; mQueue = mLooper.mQueue;
mCallback = callback; mCallback = callback;
mAsynchronous = false; mAsynchronous = async;
} }
/** /**
* Use the provided queue instead of the default one. * Use the provided {@link Looper} instead of the default one and take a callback
*/
public Handler(Looper looper) {
mLooper = looper;
mQueue = looper.mQueue;
mCallback = null;
mAsynchronous = false;
}
/**
* Use the provided queue instead of the default one and take a callback
* interface in which to handle messages.
*/
public Handler(Looper looper, Callback callback) {
mLooper = looper;
mQueue = looper.mQueue;
mCallback = callback;
mAsynchronous = false;
}
/**
* Use the provided queue instead of the default one and take a callback
* interface in which to handle messages. Also set whether the handler * interface in which to handle messages. Also set whether the handler
* should be asynchronous. * should be asynchronous.
* *
@@ -184,6 +214,8 @@ public class Handler {
* with represent to synchronous messages. Asynchronous messages are not subject to * with represent to synchronous messages. Asynchronous messages are not subject to
* the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}. * the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}.
* *
* @param looper The looper, must not be null.
* @param callback The callback interface in which to handle messages, or null.
* @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for
* each {@link Message} that is sent to it or {@link Runnable} that is posted to it. * each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
* *

View File

@@ -971,7 +971,7 @@ public class KeyguardViewMediator implements KeyguardViewCallback {
* interacts with the keyguard ui should be posted to this handler, rather * interacts with the keyguard ui should be posted to this handler, rather
* than called directly. * than called directly.
*/ */
private Handler mHandler = new Handler(Looper.myLooper(), null, true /*async*/) { private Handler mHandler = new Handler(true /*async*/) {
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {

View File

@@ -241,7 +241,7 @@ final class DockObserver extends UEventObserver {
SCREENSAVER_ACTIVATE_ON_DOCK, DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK) != 0; SCREENSAVER_ACTIVATE_ON_DOCK, DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK) != 0;
} }
private final Handler mHandler = new Handler(Looper.myLooper(), null, true) { private final Handler mHandler = new Handler(true /*async*/) {
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {

View File

@@ -1438,6 +1438,10 @@ public class InputManagerService extends IInputManager.Stub implements Watchdog.
* Private handler for the input manager. * Private handler for the input manager.
*/ */
private final class InputManagerHandler extends Handler { private final class InputManagerHandler extends Handler {
public InputManagerHandler() {
super(true /*async*/);
}
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {

View File

@@ -1112,7 +1112,7 @@ final class DisplayPowerController {
private final class DisplayControllerHandler extends Handler { private final class DisplayControllerHandler extends Handler {
public DisplayControllerHandler(Looper looper) { public DisplayControllerHandler(Looper looper) {
super(looper); super(looper, null, true /*async*/);
} }
@Override @Override

View File

@@ -422,7 +422,7 @@ final class Notifier {
private final class NotifierHandler extends Handler { private final class NotifierHandler extends Handler {
public NotifierHandler(Looper looper) { public NotifierHandler(Looper looper) {
super(looper); super(looper, null, true /*async*/);
} }
@Override @Override

View File

@@ -1952,7 +1952,7 @@ public final class PowerManagerService extends IPowerManager.Stub
*/ */
private final class PowerManagerHandler extends Handler { private final class PowerManagerHandler extends Handler {
public PowerManagerHandler(Looper looper) { public PowerManagerHandler(Looper looper) {
super(looper); super(looper, null, true /*async*/);
} }
@Override @Override