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

Commit 3f696b26 authored by Gilles Debunne's avatar Gilles Debunne Committed by Android (Google) Code Review
Browse files

Merge "Unbalanced batch edit begin and end leave TextView unresponsive"

parents 842e3790 c478c171
Loading
Loading
Loading
Loading
+26 −18
Original line number Diff line number Diff line
@@ -651,25 +651,31 @@ public final class InputMethodManager {
                }
            }
            
            if (mServedInputConnection != null) {
            notifyInputConnectionFinished();
            
            mServedView = null;
            mCompletions = null;
            mServedConnecting = false;
            clearConnectionLocked();
        }
    }

    /**
     * Notifies the served view that the current InputConnection will no longer be used.
     */
    private void notifyInputConnectionFinished() {
        if (mServedView != null && mServedInputConnection != null) {
            // We need to tell the previously served view that it is no
            // longer the input target, so it can reset its state.  Schedule
            // this call on its window's Handler so it will be on the correct
            // thread and outside of our lock.
            Handler vh = mServedView.getHandler();
            if (vh != null) {
                    // This will result in a call to reportFinishInputConnection()
                    // below.
                // This will result in a call to reportFinishInputConnection() below.
                vh.sendMessage(vh.obtainMessage(ViewRootImpl.FINISH_INPUT_CONNECTION,
                        mServedInputConnection));
            }
        }
            
            mServedView = null;
            mCompletions = null;
            mServedConnecting = false;
            clearConnectionLocked();
        }
    }

    /**
@@ -1012,6 +1018,8 @@ public final class InputMethodManager {
            // Hook 'em up and let 'er rip.
            mCurrentTextBoxAttribute = tba;
            mServedConnecting = false;
            // Notify the served view that its previous input connection is finished
            notifyInputConnectionFinished();
            mServedInputConnection = ic;
            IInputContext servedContext;
            if (ic != null) {
@@ -1115,7 +1123,7 @@ public final class InputMethodManager {
        }
    }

    void scheduleCheckFocusLocked(View view) {
    static void scheduleCheckFocusLocked(View view) {
        Handler vh = view.getHandler();
        if (vh != null && !vh.hasMessages(ViewRootImpl.CHECK_FOCUS)) {
            // This will result in a call to checkFocus() below.
+46 −8
Original line number Diff line number Diff line
@@ -35,6 +35,11 @@ public class EditableInputConnection extends BaseInputConnection {

    private final TextView mTextView;

    // Keeps track of nested begin/end batch edit to ensure this connection always has a
    // balanced impact on its associated TextView.
    // A negative value means that this connection has been finished by the InputMethodManager.
    private int mBatchEditNesting;

    public EditableInputConnection(TextView textview) {
        super(textview, true);
        mTextView = textview;
@@ -51,15 +56,31 @@ public class EditableInputConnection extends BaseInputConnection {

    @Override
    public boolean beginBatchEdit() {
        synchronized(this) {
            if (mBatchEditNesting >= 0) {
                mTextView.beginBatchEdit();
                mBatchEditNesting++;
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean endBatchEdit() {
        synchronized(this) {
            if (mBatchEditNesting > 0) {
                // When the connection is reset by the InputMethodManager and finishComposingText
                // is called, some endBatchEdit calls may still be asynchronously received from the
                // IME. Do not take these into account, thus ensuring that this IC's final
                // contribution to mTextView's nested batch edit count is zero.
                mTextView.endBatchEdit();
                mBatchEditNesting--;
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean clearMetaKeyStates(int states) {
@@ -77,6 +98,23 @@ public class EditableInputConnection extends BaseInputConnection {
        return true;
    }

    @Override
    public boolean finishComposingText() {
        final boolean superResult = super.finishComposingText();
        synchronized(this) {
            if (mBatchEditNesting < 0) {
                // The connection was already finished
                return false;
            }
            while (mBatchEditNesting > 0) {
                endBatchEdit();
            }
            // Will prevent any further calls to begin or endBatchEdit
            mBatchEditNesting = -1;
        }
        return superResult;
    }

    @Override
    public boolean commitCompletion(CompletionInfo text) {
        if (DEBUG) Log.v(TAG, "commitCompletion " + text);