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

Commit 159dd47d authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

BaseInputConnection shouldn't rely on @hide APIs.

This is a part of effort to reduce the number of dependencies on @hide
method in BaseInputConnection.

In a nutshell, IMM#notifyUserAction() and IMM#setFullscreenMode() are
something that IME developers should not care about, hence ideally
BaseInputConnection should not rely on them.

  IMM#setFullscreenMode():
    This @hide method is just for updating an internal state flag about
    whether the current IME is in full screen mode or not.

  IMM#notifyUserAction():
    This @hide methods is just for sending a signal to IMMS so that IME
    rotation list (for globe button) can be updated based on the user's
    action.

Depending on those @hide methods in BaseInputConnection is problematic
because:

  A. We cannot implement InputConnection without relying on
     BaseInputConnection, which forces developers to use Editable to
     maintain internal text representations.
  B. If BaseInputConnection#commitText is overridden,
     those @hide method calls can be missed.
  C. Currently some method calls of BaseInputConnection() even from
     application itself can trigger those @hide method calls.  Ideally
     those internal events can be dispatched only when those methods are
     called from the input method rather than the application itself.

With this CL, those @hide API calls will be moved from
BaseInputConnection to ControlledInputConnectionWrapper so that
developers can forget about them.

Note that BaseInputConnection#sendKeyEvent() still relies on @hide
internal details of IMM.  It should be addressed in a subsequent CL.

Bug: 24688781
Change-Id: I571d6cc9c6e461d8994aa7496e7e18be13766411
parent ba75a9b6
Loading
Loading
Loading
Loading
+2 −6
Original line number Diff line number Diff line
@@ -195,7 +195,6 @@ public class BaseInputConnection implements InputConnection {
    public boolean commitText(CharSequence text, int newCursorPosition) {
        if (DEBUG) Log.v(TAG, "commitText " + text);
        replaceText(text, newCursorPosition, false);
        mIMM.notifyUserAction();
        sendCurrentText();
        return true;
    }
@@ -443,7 +442,6 @@ public class BaseInputConnection implements InputConnection {
    public boolean setComposingText(CharSequence text, int newCursorPosition) {
        if (DEBUG) Log.v(TAG, "setComposingText " + text);
        replaceText(text, newCursorPosition, true);
        mIMM.notifyUserAction();
        return true;
    }

@@ -527,7 +525,6 @@ public class BaseInputConnection implements InputConnection {
                viewRootImpl.dispatchKeyFromIme(event);
            }
        }
        mIMM.notifyUserAction();
        return false;
    }

@@ -535,7 +532,6 @@ public class BaseInputConnection implements InputConnection {
     * Updates InputMethodManager with the current fullscreen mode.
     */
    public boolean reportFullscreenMode(boolean enabled) {
        mIMM.setFullscreenMode(enabled);
        return true;
    }

+12 −2
Original line number Diff line number Diff line
@@ -548,6 +548,16 @@ public final class InputMethodManager {
            mActive = false;
        }

        @Override
        protected void onUserAction() {
            mParentInputMethodManager.notifyUserAction();
        }

        @Override
        protected void onReportFullscreenMode(boolean enabled) {
            mParentInputMethodManager.setFullscreenMode(enabled);
        }

        @Override
        public String toString() {
            return "ControlledInputConnectionWrapper{mActive=" + mActive
+22 −7
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ import android.view.inputmethod.InputConnection;

import java.lang.ref.WeakReference;

public class IInputConnectionWrapper extends IInputContext.Stub {
public abstract class IInputConnectionWrapper extends IInputContext.Stub {
    static final String TAG = "IInputConnectionWrapper";

    private static final int DO_GET_TEXT_AFTER_CURSOR = 10;
@@ -80,14 +80,24 @@ public class IInputConnectionWrapper extends IInputContext.Stub {
    }
    
    public IInputConnectionWrapper(Looper mainLooper, InputConnection conn) {
        mInputConnection = new WeakReference<InputConnection>(conn);
        mInputConnection = new WeakReference<>(conn);
        mMainLooper = mainLooper;
        mH = new MyHandler(mMainLooper);
    }

    public boolean isActive() {
        return true;
    }
    abstract protected boolean isActive();

    /**
     * Called when the user took some actions that should be taken into consideration to update the
     * LRU list for input method rotation.
     */
    abstract protected void onUserAction();

    /**
     * Called when the input method started or stopped full-screen mode.
     *
     */
    abstract protected void onReportFullscreenMode(boolean enabled);

    public void getTextAfterCursor(int length, int flags, int seq, IInputContextCallback callback) {
        dispatchMessage(obtainMessageIISC(DO_GET_TEXT_AFTER_CURSOR, length, flags, seq, callback));
@@ -284,6 +294,7 @@ public class IInputConnectionWrapper extends IInputContext.Stub {
                    return;
                }
                ic.commitText((CharSequence)msg.obj, msg.arg1);
                onUserAction();
                return;
            }
            case DO_SET_SELECTION: {
@@ -338,6 +349,7 @@ public class IInputConnectionWrapper extends IInputContext.Stub {
                    return;
                }
                ic.setComposingText((CharSequence)msg.obj, msg.arg1);
                onUserAction();
                return;
            }
            case DO_SET_COMPOSING_REGION: {
@@ -369,6 +381,7 @@ public class IInputConnectionWrapper extends IInputContext.Stub {
                    return;
                }
                ic.sendKeyEvent((KeyEvent)msg.obj);
                onUserAction();
                return;
            }
            case DO_CLEAR_META_KEY_STATES: {
@@ -413,7 +426,9 @@ public class IInputConnectionWrapper extends IInputContext.Stub {
                    Log.w(TAG, "reportFullscreenMode on inexistent InputConnection");
                    return;
                }
                ic.reportFullscreenMode(msg.arg1 == 1);
                final boolean enabled = msg.arg1 == 1;
                ic.reportFullscreenMode(enabled);
                onReportFullscreenMode(enabled);
                return;
            }
            case DO_PERFORM_PRIVATE_COMMAND: {