From c356266e25ad27e36e3d17122b56109be5e30578 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Wed, 19 Oct 2022 11:34:37 -0700 Subject: [PATCH] Merge lock blocks around IMM#checkFocusInternal() This CL removes unnecessary lock acquisitions when possible. There must be no observable behavior change. Bug: 234882948 Test: presubmit Change-Id: I1b5e5870bdf7ace6a4585c640339c6196d78af42 --- .../view/inputmethod/InputMethodManager.java | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java index 21b34340a0d19..201efe81c102d 100644 --- a/core/java/android/view/inputmethod/InputMethodManager.java +++ b/core/java/android/view/inputmethod/InputMethodManager.java @@ -776,20 +776,20 @@ public final class InputMethodManager { "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 @@ public final class InputMethodManager { } @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 @@ public final class InputMethodManager { 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 @@ public final class InputMethodManager { */ @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 @@ public final class InputMethodManager { /** * 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() - : "")); - } - // 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() + : "")); + } + // 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; }