Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 484d4afc authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Always use Context.getSystemService() to get IMM

This is a preparation to deprecate the following two methods.

 * InputMethodManager#getInstance()
 * InputMethodManager#peekInstance()

Since we soon need to stop relying on the current per-process
InputMethodManager singleton model to fully support multi-display,
those two methods really need to be deprecated.

With this CL, framework code no longer depends on
InputMethodManager#peekInstance(), which is also marked as deprecated
in this CL.

InputMethodManager#getInstance() is a bit tricky because it also works
as a constructor of such a per-process singleton instance.  Remaining
two call-sites of this method will be migrated in a subsequent CL.

This is a mechanical refactoring, which in theory should have no
observable logical behavior difference.

There could be a small performance regression, but it is basically not
avoidable to correctly support multi-display scenarios.

Bug: 115891476
Test: atest CtsInputMethodTestCases CtsInputMethodServiceHostTestCases
Test: atest CtsWidgetTestCases:android.widget.cts.EditTextTest
Test: atest CtsWidgetTestCases:android.widget.cts.TextViewTest
Test: atest FrameworksCoreTests:com.android.internal.inputmethod
Test: atest FrameworksServicesTests:com.android.server.textservices
Change-Id: I5db31491f3d47d3ad4a621e956995135c72e007b
parent 7db3ae4f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -420,7 +420,7 @@ public class SuggestionSpan extends CharacterStyle implements ParcelableSpan {
            intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, hashCode());
            context.sendBroadcast(intent);
        } else {
            InputMethodManager imm = InputMethodManager.peekInstance();
            InputMethodManager imm = context.getSystemService(InputMethodManager.class);
            if (imm != null) {
                imm.notifySuggestionPicked(this, original, index);
            }
+5 −5
Original line number Diff line number Diff line
@@ -7315,7 +7315,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
        // Here we check whether we still need the default focus highlight, and switch it on/off.
        switchDefaultFocusHighlight();
        InputMethodManager imm = InputMethodManager.peekInstance();
        InputMethodManager imm = getContext().getSystemService(InputMethodManager.class);
        if (!gainFocus) {
            if (isPressed()) {
                setPressed(false);
@@ -12476,7 +12476,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
        mPrivateFlags3 &= ~PFLAG3_TEMPORARY_DETACH;
        onFinishTemporaryDetach();
        if (hasWindowFocus() && hasFocus()) {
            InputMethodManager.getInstance().focusIn(this);
            getContext().getSystemService(InputMethodManager.class).focusIn(this);
        }
        notifyEnterOrExitForAutoFillIfNeeded(true);
    }
@@ -12867,7 +12867,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
     *        focus, false otherwise.
     */
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        InputMethodManager imm = InputMethodManager.peekInstance();
        InputMethodManager imm = getContext().getSystemService(InputMethodManager.class);
        if (!hasWindowFocus) {
            if (isPressed()) {
                setPressed(false);
@@ -17932,7 +17932,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
        rebuildOutline();
        if (isFocused()) {
            InputMethodManager imm = InputMethodManager.peekInstance();
            InputMethodManager imm = getContext().getSystemService(InputMethodManager.class);
            if (imm != null) {
                imm.focusIn(this);
            }
@@ -18515,7 +18515,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
        onDetachedFromWindow();
        onDetachedFromWindowInternal();
        InputMethodManager imm = InputMethodManager.peekInstance();
        InputMethodManager imm = getContext().getSystemService(InputMethodManager.class);
        if (imm != null) {
            imm.onViewDetachedFromWindow(this);
        }
+4 −4
Original line number Diff line number Diff line
@@ -2579,7 +2579,7 @@ public final class ViewRootImpl implements ViewParent,
                    .mayUseInputMethod(mWindowAttributes.flags);
            if (imTarget != mLastWasImTarget) {
                mLastWasImTarget = imTarget;
                InputMethodManager imm = InputMethodManager.peekInstance();
                InputMethodManager imm = mContext.getSystemService(InputMethodManager.class);
                if (imm != null && imTarget) {
                    imm.onPreWindowFocus(mView, hasWindowFocus);
                    imm.onPostWindowFocus(mView, mView.findFocus(),
@@ -2695,7 +2695,7 @@ public final class ViewRootImpl implements ViewParent,
            mLastWasImTarget = WindowManager.LayoutParams
                    .mayUseInputMethod(mWindowAttributes.flags);

            InputMethodManager imm = InputMethodManager.peekInstance();
            InputMethodManager imm = mContext.getSystemService(InputMethodManager.class);
            if (imm != null && mLastWasImTarget && !isInLocalFocusMode()) {
                imm.onPreWindowFocus(mView, hasWindowFocus);
            }
@@ -4329,7 +4329,7 @@ public final class ViewRootImpl implements ViewParent,
                    enqueueInputEvent(event, null, 0, true);
                } break;
                case MSG_CHECK_FOCUS: {
                    InputMethodManager imm = InputMethodManager.peekInstance();
                    InputMethodManager imm = mContext.getSystemService(InputMethodManager.class);
                    if (imm != null) {
                        imm.checkFocus();
                    }
@@ -4871,7 +4871,7 @@ public final class ViewRootImpl implements ViewParent,
        @Override
        protected int onProcess(QueuedInputEvent q) {
            if (mLastWasImTarget && !isInLocalFocusMode()) {
                InputMethodManager imm = InputMethodManager.peekInstance();
                InputMethodManager imm = mContext.getSystemService(InputMethodManager.class);
                if (imm != null) {
                    final InputEvent event = q.mEvent;
                    if (DEBUG_IMF) Log.v(mTag, "Sending input event to IME: " + event);
+1 −1
Original line number Diff line number Diff line
@@ -468,7 +468,7 @@ public final class WindowManagerGlobal {
        View view = root.getView();

        if (view != null) {
            InputMethodManager imm = InputMethodManager.getInstance();
            InputMethodManager imm = view.getContext().getSystemService(InputMethodManager.class);
            if (imm != null) {
                imm.windowDismissed(mViews.get(index).getWindowToken());
            }
+4 −2
Original line number Diff line number Diff line
@@ -665,10 +665,12 @@ public final class InputMethodManager {
    }

    /**
     * Private optimization: retrieve the global InputMethodManager instance,
     * if it exists.
     * Private optimization: retrieve the global InputMethodManager instance, if it exists.
     * @hide
     * @deprecated Use {@link Context#getSystemService(Class)} instead. This method cannot fully
     *             support multi-display scenario.
     */
    @Deprecated
    @UnsupportedAppUsage
    public static InputMethodManager peekInstance() {
        return sInstance;
Loading