diff options
| author | Jeff Brown <jeffbrown@google.com> | 2014-11-14 15:49:45 -0800 |
|---|---|---|
| committer | Jeff Brown <jeffbrown@google.com> | 2014-11-14 17:22:31 -0800 |
| commit | bba231d7a63b58a8c2b174722ed1487b0f7d8270 (patch) | |
| tree | 87a2d0de987178fcde2c23a07a0db977a7daf4f7 /core/java/android/os/AsyncTask.java | |
| parent | 4d2d97763f0963d7011d04290dc87b2940aefb33 (diff) | |
Explicitly bind AsyncTask to main looper.
It seems we were sort of trying to do this by forcing the AsyncTask
static initializer to run at certain times but it was not sufficiently
reliable. In particular, this resulted in occasional system
server crashes.
Bug: 18192406
Change-Id: Ief73210c60e7680fbed6df74e3e58809b7ec7e4d
Diffstat (limited to 'core/java/android/os/AsyncTask.java')
| -rw-r--r-- | core/java/android/os/AsyncTask.java | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/core/java/android/os/AsyncTask.java b/core/java/android/os/AsyncTask.java index 4f91d1993305..7785f2bb886c 100644 --- a/core/java/android/os/AsyncTask.java +++ b/core/java/android/os/AsyncTask.java @@ -209,9 +209,9 @@ public abstract class AsyncTask<Params, Progress, Result> { private static final int MESSAGE_POST_RESULT = 0x1; private static final int MESSAGE_POST_PROGRESS = 0x2; - private static final InternalHandler sHandler = new InternalHandler(); - private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR; + private static InternalHandler sHandler; + private final WorkerRunnable<Params, Result> mWorker; private final FutureTask<Result> mFuture; @@ -265,9 +265,13 @@ public abstract class AsyncTask<Params, Progress, Result> { FINISHED, } - /** @hide Used to force static handler to be created. */ - public static void init() { - sHandler.getLooper(); + private static Handler getHandler() { + synchronized (AsyncTask.class) { + if (sHandler == null) { + sHandler = new InternalHandler(); + } + return sHandler; + } } /** @hide */ @@ -315,7 +319,7 @@ public abstract class AsyncTask<Params, Progress, Result> { private Result postResult(Result result) { @SuppressWarnings("unchecked") - Message message = sHandler.obtainMessage(MESSAGE_POST_RESULT, + Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT, new AsyncTaskResult<Result>(this, result)); message.sendToTarget(); return result; @@ -620,7 +624,7 @@ public abstract class AsyncTask<Params, Progress, Result> { */ protected final void publishProgress(Progress... values) { if (!isCancelled()) { - sHandler.obtainMessage(MESSAGE_POST_PROGRESS, + getHandler().obtainMessage(MESSAGE_POST_PROGRESS, new AsyncTaskResult<Progress>(this, values)).sendToTarget(); } } @@ -635,10 +639,14 @@ public abstract class AsyncTask<Params, Progress, Result> { } private static class InternalHandler extends Handler { + public InternalHandler() { + super(Looper.getMainLooper()); + } + @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"}) @Override public void handleMessage(Message msg) { - AsyncTaskResult result = (AsyncTaskResult) msg.obj; + AsyncTaskResult<?> result = (AsyncTaskResult<?>) msg.obj; switch (msg.what) { case MESSAGE_POST_RESULT: // There is only one result |
