Merge "Merge lock blocks around IMM#checkFocusInternal()"
diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java
index 21b3434..201efe8 100644
--- a/core/java/android/view/inputmethod/InputMethodManager.java
+++ b/core/java/android/view/inputmethod/InputMethodManager.java
@@ -776,20 +776,20 @@
                     "InputMethodManager.DelegateImpl#startInputAsyncOnWindowFocusGain",
                     InputMethodManager.this, null /* icProto */);
 
-            final ViewRootImpl viewRootImpl;
+            boolean checkFocusResult;
             synchronized (mH) {
                 if (mCurRootView == null) {
                     return;
                 }
-                viewRootImpl = mCurRootView;
                 if (mRestartOnNextWindowFocus) {
                     if (DEBUG) Log.v(TAG, "Restarting due to mRestartOnNextWindowFocus as true");
                     mRestartOnNextWindowFocus = false;
                     forceNewFocus = true;
                 }
+                checkFocusResult = checkFocusInternalLocked(forceNewFocus, mCurRootView);
             }
 
-            if (checkFocusInternal(forceNewFocus, viewRootImpl)) {
+            if (checkFocusResult) {
                 // 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
@@ -825,9 +825,11 @@
         }
 
         @Override
-        public void onScheduledCheckFocus(@NonNull ViewRootImpl viewRootImpl) {
-            if (!checkFocusInternal(false, viewRootImpl)) {
-                return;
+        public void onScheduledCheckFocus(ViewRootImpl viewRootImpl) {
+            synchronized (mH) {
+                if (!checkFocusInternalLocked(false, viewRootImpl)) {
+                    return;
+                }
             }
             startInputOnWindowFocusGainInternal(StartInputReason.SCHEDULED_CHECK_FOCUS,
                     null /* focusedView */, 0 /* startInputFlags */, 0 /* softInputMode */,
@@ -1123,7 +1125,7 @@
                         if (mCurRootView == null) {
                             return;
                         }
-                        if (!checkFocusInternal(mRestartOnNextWindowFocus, mCurRootView)) {
+                        if (!checkFocusInternalLocked(mRestartOnNextWindowFocus, mCurRootView)) {
                             return;
                         }
                         final int reason = active ? StartInputReason.ACTIVATED_BY_IMMS
@@ -2657,15 +2659,13 @@
      */
     @UnsupportedAppUsage
     public void checkFocus() {
-        final ViewRootImpl viewRootImpl;
         synchronized (mH) {
             if (mCurRootView == null) {
                 return;
             }
-            viewRootImpl = mCurRootView;
-        }
-        if (!checkFocusInternal(false /* forceNewFocus */, viewRootImpl)) {
-            return;
+            if (!checkFocusInternalLocked(false /* forceNewFocus */, mCurRootView)) {
+                return;
+            }
         }
         startInputOnWindowFocusGainInternal(StartInputReason.CHECK_FOCUS,
                 null /* focusedView */,
@@ -2675,32 +2675,31 @@
     /**
      * Check the next served view if needs to start input.
      */
-    private boolean checkFocusInternal(boolean forceNewFocus, ViewRootImpl viewRootImpl) {
-        synchronized (mH) {
-            if (mCurRootView != viewRootImpl) {
-                return false;
-            }
-            if (mServedView == mNextServedView && !forceNewFocus) {
-                return false;
-            }
-            if (DEBUG) {
-                Log.v(TAG, "checkFocus: view=" + mServedView
-                        + " next=" + mNextServedView
-                        + " force=" + forceNewFocus
-                        + " package="
-                        + (mServedView != null ? mServedView.getContext().getPackageName()
-                        : "<none>"));
-            }
-            // Close the connection when no next served view coming.
-            if (mNextServedView == null) {
-                finishInputLocked();
-                closeCurrentInput();
-                return false;
-            }
-            mServedView = mNextServedView;
-            if (mServedInputConnection != null) {
-                mServedInputConnection.finishComposingTextFromImm();
-            }
+    @GuardedBy("mH")
+    private boolean checkFocusInternalLocked(boolean forceNewFocus, ViewRootImpl viewRootImpl) {
+        if (mCurRootView != viewRootImpl) {
+            return false;
+        }
+        if (mServedView == mNextServedView && !forceNewFocus) {
+            return false;
+        }
+        if (DEBUG) {
+            Log.v(TAG, "checkFocus: view=" + mServedView
+                    + " next=" + mNextServedView
+                    + " force=" + forceNewFocus
+                    + " package="
+                    + (mServedView != null ? mServedView.getContext().getPackageName()
+                    : "<none>"));
+        }
+        // Close the connection when no next served view coming.
+        if (mNextServedView == null) {
+            finishInputLocked();
+            closeCurrentInput();
+            return false;
+        }
+        mServedView = mNextServedView;
+        if (mServedInputConnection != null) {
+            mServedInputConnection.finishComposingTextFromImm();
         }
         return true;
     }