From b20ee4eea574cc4320c35f54a6f5a39ae512373e Mon Sep 17 00:00:00 2001 From: Wilson Wu Date: Wed, 28 Sep 2022 19:11:15 +0800 Subject: [PATCH] Simplify InputMethodManager#startInputInner Wrap the createInputConnection logic into a private method to simplify this complex method. This is a mechanical restructuring without user visible changes. Bug: 236937383 Bug: 236920321 Test: presubmit Change-Id: I21207d22b7f5d55e84cbfde91969e59bb94b3043 --- .../view/inputmethod/InputMethodManager.java | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java index 08a7583a80e9f..5c1e2a58b0991 100644 --- a/core/java/android/view/inputmethod/InputMethodManager.java +++ b/core/java/android/view/inputmethod/InputMethodManager.java @@ -46,6 +46,7 @@ import android.annotation.RequiresFeature; import android.annotation.RequiresPermission; import android.annotation.SystemService; import android.annotation.TestApi; +import android.annotation.UiThread; import android.annotation.UserIdInt; import android.app.ActivityThread; import android.compat.annotation.ChangeId; @@ -78,6 +79,7 @@ import android.provider.Settings; import android.text.TextUtils; import android.text.style.SuggestionSpan; import android.util.Log; +import android.util.Pair; import android.util.Pools.Pool; import android.util.Pools.SimplePool; import android.util.PrintWriterPrinter; @@ -2472,24 +2474,9 @@ public final class InputMethodManager { // Okay we are now ready to call into the served view and have it // do its stuff. // Life is good: let's hook everything up! - EditorInfo editorInfo = new EditorInfo(); - // Note: Use Context#getOpPackageName() rather than Context#getPackageName() so that the - // system can verify the consistency between the uid of this process and package name passed - // from here. See comment of Context#getOpPackageName() for details. - editorInfo.packageName = view.getContext().getOpPackageName(); - editorInfo.autofillId = view.getAutofillId(); - editorInfo.fieldId = view.getId(); - InputConnection ic = view.onCreateInputConnection(editorInfo); - if (DEBUG) Log.v(TAG, "Starting input: editorInfo=" + editorInfo + " ic=" + ic); - - // Clear autofill and field ids if a connection could not be established. - // This ensures that even disconnected EditorInfos have well-defined attributes, - // making them consistently and straightforwardly comparable. - if (ic == null) { - editorInfo.autofillId = AutofillId.NO_AUTOFILL_ID; - editorInfo.fieldId = 0; - } - + final Pair connectionPair = createInputConnection(view); + final InputConnection ic = connectionPair.first; + final EditorInfo editorInfo = connectionPair.second; final Handler icHandler; InputBindResult res = null; synchronized (mH) { @@ -4030,4 +4017,27 @@ public final class InputMethodManager { consumer.accept(mAccessibilityInputMethodSession.valueAt(i)); } } + + @UiThread + private static Pair createInputConnection( + @NonNull View servedView) { + final EditorInfo editorInfo = new EditorInfo(); + // Note: Use Context#getOpPackageName() rather than Context#getPackageName() so that the + // system can verify the consistency between the uid of this process and package name passed + // from here. See comment of Context#getOpPackageName() for details. + editorInfo.packageName = servedView.getContext().getOpPackageName(); + editorInfo.autofillId = servedView.getAutofillId(); + editorInfo.fieldId = servedView.getId(); + final InputConnection ic = servedView.onCreateInputConnection(editorInfo); + if (DEBUG) Log.v(TAG, "Starting input: editorInfo=" + editorInfo + " ic=" + ic); + + // Clear autofill and field ids if a connection could not be established. + // This ensures that even disconnected EditorInfos have well-defined attributes, + // making them consistently and straightforwardly comparable. + if (ic == null) { + editorInfo.autofillId = AutofillId.NO_AUTOFILL_ID; + editorInfo.fieldId = 0; + } + return new Pair<>(ic, editorInfo); + } }