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

Commit 02d2f81b authored by Lan Wei's avatar Lan Wei Committed by Android (Google) Code Review
Browse files

Merge "IMA API InputConnection#replaceText()"

parents 1611b758 d5d530ad
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -53317,6 +53317,7 @@ package android.view.inputmethod {
    method public default void performHandwritingGesture(@NonNull android.view.inputmethod.HandwritingGesture, @Nullable java.util.concurrent.Executor, @Nullable java.util.function.IntConsumer);
    method public boolean performPrivateCommand(String, android.os.Bundle);
    method public default boolean performSpellCheck();
    method public default boolean replaceText(@IntRange(from=0) int, @IntRange(from=0) int, @NonNull CharSequence, int, @Nullable android.view.inputmethod.TextAttribute);
    method public boolean reportFullscreenMode(boolean);
    method public boolean requestCursorUpdates(int);
    method public default boolean requestCursorUpdates(int, int);
+29 −0
Original line number Diff line number Diff line
@@ -775,4 +775,33 @@ final class IRemoteInputConnectionInvoker {
            return false;
        }
    }

    /**
     * Invokes {@link IRemoteInputConnection#replaceText(InputConnectionCommandHeader, int, int,
     * CharSequence, TextAttribute)}.
     *
     * @param start the character index where the replacement should start.
     * @param end the character index where the replacement should end.
     * @param newCursorPosition the new cursor position around the text. If > 0, this is relative to
     *     the end of the text - 1; if <= 0, this is relative to the start of the text. So a value
     *     of 1 will always advance you to the position after the full text being inserted. Note
     *     that this means you can't position the cursor within the text.
     * @param text the text to replace. This may include styles.
     * @param textAttribute The extra information about the text. This value may be null.
     */
    @AnyThread
    public boolean replaceText(
            int start,
            int end,
            @NonNull CharSequence text,
            int newCursorPosition,
            @Nullable TextAttribute textAttribute) {
        try {
            mConnection.replaceText(
                    createHeader(), start, end, text, newCursorPosition, textAttribute);
            return true;
        } catch (RemoteException e) {
            return false;
        }
    }
}
+11 −0
Original line number Diff line number Diff line
@@ -498,6 +498,17 @@ final class RemoteInputConnection implements InputConnection {
        return mInvoker.setImeConsumesInput(imeConsumesInput);
    }

    /** See {@link InputConnection#replaceText(int, int, CharSequence, int, TextAttribute)}. */
    @AnyThread
    public boolean replaceText(
            int start,
            int end,
            @NonNull CharSequence text,
            int newCursorPosition,
            @Nullable TextAttribute textAttribute) {
        return mInvoker.replaceText(start, end, text, newCursorPosition, textAttribute);
    }

    @AnyThread
    @Override
    public String toString() {
+47 −5
Original line number Diff line number Diff line
@@ -897,8 +897,43 @@ public class BaseInputConnection implements InputConnection {
        }
    }

    private void replaceText(CharSequence text, int newCursorPosition,
            boolean composing) {
    @Override
    public boolean replaceText(
            @IntRange(from = 0) int start,
            @IntRange(from = 0) int end,
            @NonNull CharSequence text,
            int newCursorPosition,
            @Nullable TextAttribute textAttribute) {
        Preconditions.checkArgumentNonnegative(start);
        Preconditions.checkArgumentNonnegative(end);

        if (DEBUG) {
            Log.v(
                    TAG,
                    "replaceText " + start + ", " + end + ", " + text + ", " + newCursorPosition);
        }

        final Editable content = getEditable();
        if (content == null) {
            return false;
        }
        beginBatchEdit();
        removeComposingSpans(content);

        int len = content.length();
        start = Math.min(start, len);
        end = Math.min(end, len);
        if (end < start) {
            int tmp = start;
            start = end;
            end = tmp;
        }
        replaceTextInternal(start, end, text, newCursorPosition, /*composing=*/ false);
        endBatchEdit();
        return true;
    }

    private void replaceText(CharSequence text, int newCursorPosition, boolean composing) {
        final Editable content = getEditable();
        if (content == null) {
            return;
@@ -931,6 +966,16 @@ public class BaseInputConnection implements InputConnection {
                b = tmp;
            }
        }
        replaceTextInternal(a, b, text, newCursorPosition, composing);
        endBatchEdit();
    }

    private void replaceTextInternal(
            int a, int b, CharSequence text, int newCursorPosition, boolean composing) {
        final Editable content = getEditable();
        if (content == null) {
            return;
        }

        if (composing) {
            Spannable sp = null;
@@ -974,7 +1019,6 @@ public class BaseInputConnection implements InputConnection {
        if (newCursorPosition < 0) newCursorPosition = 0;
        if (newCursorPosition > content.length()) newCursorPosition = content.length();
        Selection.setSelection(content, newCursorPosition);

        content.replace(a, b, text);

        if (DEBUG) {
@@ -982,8 +1026,6 @@ public class BaseInputConnection implements InputConnection {
            lp.println("Final text:");
            TextUtils.dumpSpans(content, lp, "  ");
        }

        endBatchEdit();
    }

    /**
+40 −0
Original line number Diff line number Diff line
@@ -1329,4 +1329,44 @@ public interface InputConnection {
        // existing APIs.
        return null;
    }

    /**
     * Replace the specific range in the editor with suggested text.
     *
     * <p>This method finishes whatever composing text is currently active and leaves the text
     * as-it, replaces the specific range of text with the passed CharSequence, and then moves the
     * cursor according to {@code newCursorPosition}. This behaves like calling {@link
     * #finishComposingText()}, {@link #setSelection(int, int) setSelection(start, end)}, and then
     * {@link #commitText(CharSequence, int, TextAttribute) commitText(text, newCursorPosition,
     * textAttribute)}.
     *
     * <p>Similar to {@link #setSelection(int, int)}, the order of start and end is not important.
     * In effect, the region from start to end and the region from end to start is the same. Editor
     * authors, be ready to accept a start that is greater than end.
     *
     * @param start the character index where the replacement should start.
     * @param end the character index where the replacement should end.
     * @param newCursorPosition the new cursor position around the text. If > 0, this is relative to
     *     the end of the text - 1; if <= 0, this is relative to the start of the text. So a value
     *     of 1 will always advance you to the position after the full text being inserted. Note
     *     that this means you can't position the cursor within the text.
     * @param text the text to replace. This may include styles.
     * @param textAttribute The extra information about the text. This value may be null.
     */
    default boolean replaceText(
            @IntRange(from = 0) int start,
            @IntRange(from = 0) int end,
            @NonNull CharSequence text,
            int newCursorPosition,
            @Nullable TextAttribute textAttribute) {
        Preconditions.checkArgumentNonnegative(start);
        Preconditions.checkArgumentNonnegative(end);

        beginBatchEdit();
        finishComposingText();
        setSelection(start, end);
        commitText(text, newCursorPosition, textAttribute);
        endBatchEdit();
        return true;
    }
}
Loading