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
This commit is contained in:
Yohei Yukawa
2022-10-19 11:34:37 -07:00
parent 90ce33c315
commit c356266e25

View File

@@ -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()
: "<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;
}