Merge "Logically revert previous attempts on Bug 139806621"
diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java
index dbab81b1..1e6abd9 100644
--- a/core/java/android/view/inputmethod/InputMethodManager.java
+++ b/core/java/android/view/inputmethod/InputMethodManager.java
@@ -93,12 +93,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
-import java.util.concurrent.CancellationException;
 import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 
@@ -415,16 +410,6 @@
     int mCursorCandEnd;
 
     /**
-     * Initial startInput with {@link StartInputReason.WINDOW_FOCUS_GAIN} is executed
-     * in a background thread. Later, if there is an actual startInput it will wait on
-     * main thread till the background thread completes.
-     */
-    private Future<?> mWindowFocusGainFuture;
-
-    private ExecutorService mStartInputWorker = Executors.newSingleThreadExecutor(
-            new ImeThreadFactory("StartInputWorker"));
-
-    /**
      * The instance that has previously been sent to the input method.
      */
     private CursorAnchorInfo mCursorAnchorInfo = null;
@@ -612,41 +597,36 @@
             final boolean forceNewFocus1 = forceNewFocus;
             final int startInputFlags = getStartInputFlags(focusedView, 0);
 
-            if (mWindowFocusGainFuture != null) {
-                mWindowFocusGainFuture.cancel(false /* mayInterruptIfRunning */);
+            final ImeFocusController controller = getFocusController();
+            if (controller == null) {
+                return;
             }
-            mWindowFocusGainFuture = mStartInputWorker.submit(() -> {
-                final ImeFocusController controller = getFocusController();
-                if (controller == null) {
+            if (controller.checkFocus(forceNewFocus1, false)) {
+                // We need to restart input on the current focus view.  This
+                // should be done in conjunction with telling the system service
+                // about the window gaining focus, to help make the transition
+                // smooth.
+                if (startInput(StartInputReason.WINDOW_FOCUS_GAIN,
+                        focusedView, startInputFlags, softInputMode, windowFlags)) {
                     return;
                 }
-                if (controller.checkFocus(forceNewFocus1, false)) {
-                    // We need to restart input on the current focus view.  This
-                    // should be done in conjunction with telling the system service
-                    // about the window gaining focus, to help make the transition
-                    // smooth.
-                    if (startInput(StartInputReason.WINDOW_FOCUS_GAIN,
-                            focusedView, startInputFlags, softInputMode, windowFlags)) {
-                        return;
-                    }
-                }
+            }
 
-                synchronized (mH) {
-                    // For some reason we didn't do a startInput + windowFocusGain, so
-                    // we'll just do a window focus gain and call it a day.
-                    try {
-                        if (DEBUG) Log.v(TAG, "Reporting focus gain, without startInput");
-                        mService.startInputOrWindowGainedFocus(
-                                StartInputReason.WINDOW_FOCUS_GAIN_REPORT_ONLY, mClient,
-                                focusedView.getWindowToken(), startInputFlags, softInputMode,
-                                windowFlags,
-                                null, null, 0 /* missingMethodFlags */,
-                                mCurRootView.mContext.getApplicationInfo().targetSdkVersion);
-                    } catch (RemoteException e) {
-                        throw e.rethrowFromSystemServer();
-                    }
+            synchronized (mH) {
+                // For some reason we didn't do a startInput + windowFocusGain, so
+                // we'll just do a window focus gain and call it a day.
+                try {
+                    if (DEBUG) Log.v(TAG, "Reporting focus gain, without startInput");
+                    mService.startInputOrWindowGainedFocus(
+                            StartInputReason.WINDOW_FOCUS_GAIN_REPORT_ONLY, mClient,
+                            focusedView.getWindowToken(), startInputFlags, softInputMode,
+                            windowFlags,
+                            null, null, 0 /* missingMethodFlags */,
+                            mCurRootView.mContext.getApplicationInfo().targetSdkVersion);
+                } catch (RemoteException e) {
+                    throw e.rethrowFromSystemServer();
                 }
-            });
+            }
         }
 
         /**
@@ -664,10 +644,6 @@
          */
         @Override
         public void setCurrentRootView(ViewRootImpl rootView) {
-            if (mWindowFocusGainFuture != null) {
-                mWindowFocusGainFuture.cancel(false /* mayInterruptIfRunning */);
-                mWindowFocusGainFuture = null;
-            }
             synchronized (mH) {
                 if (mCurRootView != null) {
                     // Reset the last served view and restart window focus state of the root view.
@@ -845,19 +821,16 @@
                             } catch (RemoteException e) {
                             }
                         }
-                    }
-                    // Check focus again in case that "onWindowFocus" is called before
-                    // handling this message.
-                    final View servedView;
-                    synchronized (mH) {
-                        servedView = getServedViewLocked();
-                    }
-                    if (servedView != null && canStartInput(servedView)) {
-                        if (mCurRootView != null && mCurRootView.getImeFocusController()
-                                .checkFocus(mRestartOnNextWindowFocus, false)) {
-                            final int reason = active ? StartInputReason.ACTIVATED_BY_IMMS
-                                    : StartInputReason.DEACTIVATED_BY_IMMS;
-                            mDelegate.startInput(reason, null, 0, 0, 0);
+                        // Check focus again in case that "onWindowFocus" is called before
+                        // handling this message.
+                        final View servedView = getServedViewLocked();
+                        if (servedView != null && canStartInput(servedView)) {
+                            if (mCurRootView != null && mCurRootView.getImeFocusController()
+                                    .checkFocus(mRestartOnNextWindowFocus, false)) {
+                                final int reason = active ? StartInputReason.ACTIVATED_BY_IMMS
+                                        : StartInputReason.DEACTIVATED_BY_IMMS;
+                                mDelegate.startInput(reason, null, 0, 0, 0);
+                            }
                         }
                     }
                     return;
@@ -1430,10 +1403,6 @@
      */
     void clearBindingLocked() {
         if (DEBUG) Log.v(TAG, "Clearing binding!");
-        if (mWindowFocusGainFuture != null) {
-            mWindowFocusGainFuture.cancel(false /* mayInterruptIfRunning */);
-            mWindowFocusGainFuture = null;
-        }
         clearConnectionLocked();
         setInputChannelLocked(null);
         mBindSequence = -1;
@@ -1826,18 +1795,6 @@
     boolean startInputInner(@StartInputReason int startInputReason,
             @Nullable IBinder windowGainingFocus, @StartInputFlags int startInputFlags,
             @SoftInputModeFlags int softInputMode, int windowFlags) {
-        if (startInputReason != StartInputReason.WINDOW_FOCUS_GAIN
-                && mWindowFocusGainFuture != null) {
-            try {
-                mWindowFocusGainFuture.get();
-            } catch (ExecutionException | InterruptedException e) {
-                // do nothing
-            } catch (CancellationException e) {
-                // window no longer has focus.
-                return true;
-            }
-        }
-
         final View view;
         synchronized (mH) {
             view = getServedViewLocked();