Merge "SelectionActionModeHelper should use target view's thread" into oc-dev

This commit is contained in:
TreeHugger Robot
2017-05-26 00:45:38 +00:00
committed by Android (Google) Code Review
2 changed files with 37 additions and 7 deletions

View File

@@ -17,14 +17,14 @@
package android.os;
import android.annotation.MainThread;
import android.annotation.Nullable;
import android.annotation.WorkerThread;
import java.util.ArrayDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
@@ -232,6 +232,8 @@ public abstract class AsyncTask<Params, Progress, Result> {
private final AtomicBoolean mCancelled = new AtomicBoolean();
private final AtomicBoolean mTaskInvoked = new AtomicBoolean();
private final Handler mHandler;
private static class SerialExecutor implements Executor {
final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();
Runnable mActive;
@@ -277,15 +279,19 @@ public abstract class AsyncTask<Params, Progress, Result> {
FINISHED,
}
private static Handler getHandler() {
private static Handler getMainHandler() {
synchronized (AsyncTask.class) {
if (sHandler == null) {
sHandler = new InternalHandler();
sHandler = new InternalHandler(Looper.getMainLooper());
}
return sHandler;
}
}
private Handler getHandler() {
return mHandler;
}
/** @hide */
public static void setDefaultExecutor(Executor exec) {
sDefaultExecutor = exec;
@@ -295,6 +301,28 @@ public abstract class AsyncTask<Params, Progress, Result> {
* Creates a new asynchronous task. This constructor must be invoked on the UI thread.
*/
public AsyncTask() {
this((Looper) null);
}
/**
* Creates a new asynchronous task. This constructor must be invoked on the UI thread.
*
* @hide
*/
public AsyncTask(@Nullable Handler handler) {
this(handler != null ? handler.getLooper() : null);
}
/**
* Creates a new asynchronous task. This constructor must be invoked on the UI thread.
*
* @hide
*/
public AsyncTask(@Nullable Looper callbackLooper) {
mHandler = callbackLooper == null || callbackLooper == Looper.getMainLooper()
? getMainHandler()
: new Handler(callbackLooper);
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
mTaskInvoked.set(true);
@@ -670,8 +698,8 @@ public abstract class AsyncTask<Params, Progress, Result> {
}
private static class InternalHandler extends Handler {
public InternalHandler() {
super(Looper.getMainLooper());
public InternalHandler(Looper looper) {
super(looper);
}
@SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})

View File

@@ -74,8 +74,9 @@ final class SelectionActionModeHelper {
startActionMode(null);
} else {
resetTextClassificationHelper(true /* resetSelectionTag */);
final TextView tv = mEditor.getTextView();
mTextClassificationAsyncTask = new TextClassificationAsyncTask(
mEditor.getTextView(),
tv,
TIMEOUT_DURATION,
adjustSelection
? mTextClassificationHelper::suggestSelection
@@ -340,6 +341,7 @@ final class SelectionActionModeHelper {
@NonNull TextView textView, int timeOut,
@NonNull Supplier<SelectionResult> selectionResultSupplier,
@NonNull Consumer<SelectionResult> selectionResultCallback) {
super(textView != null ? textView.getHandler() : null);
mTextView = Preconditions.checkNotNull(textView);
mTimeOutDuration = timeOut;
mSelectionResultSupplier = Preconditions.checkNotNull(selectionResultSupplier);