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
This commit is contained in:
@@ -5223,8 +5223,6 @@ public final class ActivityThread {
|
||||
sMainThreadHandler = thread.getHandler();
|
||||
}
|
||||
|
||||
AsyncTask.init();
|
||||
|
||||
if (false) {
|
||||
Looper.myLooper().setMessageLogging(new
|
||||
LogPrinter(Log.DEBUG, "ActivityThread"));
|
||||
|
||||
@@ -1659,12 +1659,6 @@ public abstract class ContentProvider implements ComponentCallbacks2 {
|
||||
}
|
||||
|
||||
private void attachInfo(Context context, ProviderInfo info, boolean testing) {
|
||||
/*
|
||||
* We may be using AsyncTask from binder threads. Make it init here
|
||||
* so its static handler is on the main thread.
|
||||
*/
|
||||
AsyncTask.init();
|
||||
|
||||
mNoPerms = testing;
|
||||
|
||||
/*
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user