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

Commit 6c7ad1d0 authored by Mihai Popa's avatar Mihai Popa
Browse files

Make text cursor drawable public

The CL adds public getter and setters for the drawable used for the
cursor of a TextView.

Bug: 117521190
Test: atest CtsWidgetTestCases:android.widget.cts.TextViewTest
Change-Id: I3801ee8642d277c96c094b7d4155e28fc5bcea87
parent 188cf112
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -55956,6 +55956,7 @@ package android.widget {
    method public java.lang.CharSequence getText();
    method public java.lang.CharSequence getText();
    method public android.view.textclassifier.TextClassifier getTextClassifier();
    method public android.view.textclassifier.TextClassifier getTextClassifier();
    method public final android.content.res.ColorStateList getTextColors();
    method public final android.content.res.ColorStateList getTextColors();
    method public android.graphics.drawable.Drawable getTextCursorDrawable();
    method public android.text.TextDirectionHeuristic getTextDirectionHeuristic();
    method public android.text.TextDirectionHeuristic getTextDirectionHeuristic();
    method public java.util.Locale getTextLocale();
    method public java.util.Locale getTextLocale();
    method public android.os.LocaleList getTextLocales();
    method public android.os.LocaleList getTextLocales();
@@ -56086,6 +56087,8 @@ package android.widget {
    method public void setTextClassifier(android.view.textclassifier.TextClassifier);
    method public void setTextClassifier(android.view.textclassifier.TextClassifier);
    method public void setTextColor(int);
    method public void setTextColor(int);
    method public void setTextColor(android.content.res.ColorStateList);
    method public void setTextColor(android.content.res.ColorStateList);
    method public void setTextCursorDrawable(android.graphics.drawable.Drawable);
    method public void setTextCursorDrawable(int);
    method public void setTextIsSelectable(boolean);
    method public void setTextIsSelectable(boolean);
    method public final void setTextKeepState(java.lang.CharSequence);
    method public final void setTextKeepState(java.lang.CharSequence);
    method public final void setTextKeepState(java.lang.CharSequence, android.widget.TextView.BufferType);
    method public final void setTextKeepState(java.lang.CharSequence, android.widget.TextView.BufferType);
+9 −6
Original line number Original line Diff line number Diff line
@@ -2051,8 +2051,8 @@ public class Editor {
    }
    }


    void updateCursorPosition() {
    void updateCursorPosition() {
        if (mTextView.mCursorDrawableRes == 0) {
        loadCursorDrawable();
            mDrawableForCursor = null;
        if (mDrawableForCursor == null) {
            return;
            return;
        }
        }


@@ -2462,10 +2462,7 @@ public class Editor {
    }
    }


    private void updateCursorPosition(int top, int bottom, float horizontal) {
    private void updateCursorPosition(int top, int bottom, float horizontal) {
        if (mDrawableForCursor == null) {
        loadCursorDrawable();
            mDrawableForCursor = mTextView.getContext().getDrawable(
                    mTextView.mCursorDrawableRes);
        }
        final int left = clampHorizontalPosition(mDrawableForCursor, horizontal);
        final int left = clampHorizontalPosition(mDrawableForCursor, horizontal);
        final int width = mDrawableForCursor.getIntrinsicWidth();
        final int width = mDrawableForCursor.getIntrinsicWidth();
        mDrawableForCursor.setBounds(left, top - mTempRect.top, left + width,
        mDrawableForCursor.setBounds(left, top - mTempRect.top, left + width,
@@ -5698,6 +5695,12 @@ public class Editor {
        public boolean isActive();
        public boolean isActive();
    }
    }


    void loadCursorDrawable() {
        if (mDrawableForCursor == null) {
            mDrawableForCursor = mTextView.getTextCursorDrawable();
        }
    }

    private class InsertionPointCursorController implements CursorController {
    private class InsertionPointCursorController implements CursorController {
        private InsertionHandleView mHandle;
        private InsertionHandleView mHandle;


+56 −1
Original line number Original line Diff line number Diff line
@@ -799,8 +799,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener


    // Although these fields are specific to editable text, they are not added to Editor because
    // Although these fields are specific to editable text, they are not added to Editor because
    // they are defined by the TextView's style and are theme-dependent.
    // they are defined by the TextView's style and are theme-dependent.
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    int mCursorDrawableRes;
    int mCursorDrawableRes;
    private Drawable mCursorDrawable;
    // Note: this might be stale if setTextSelectHandleLeft is used. We could simplify the code
    // Note: this might be stale if setTextSelectHandleLeft is used. We could simplify the code
    // by removing it, but we would break apps targeting <= P that use it by reflection.
    // by removing it, but we would break apps targeting <= P that use it by reflection.
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
@@ -3640,6 +3641,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
    /**
    /**
     * Returns the Drawable corresponding to the right handle used
     * Returns the Drawable corresponding to the right handle used
     * for selecting text.
     * for selecting text.
     * Note that any change applied to the handle Drawable will not be visible
     * until the handle is hidden and then drawn again.
     *
     *
     * @return the right text selection handle drawable
     * @return the right text selection handle drawable
     *
     *
@@ -3654,6 +3657,58 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        return mTextSelectHandleRight;
        return mTextSelectHandleRight;
    }
    }


    /**
     * Sets the Drawable corresponding to the text cursor. The Drawable defaults to the
     * value of the textCursorDrawable attribute.
     * Note that any change applied to the cursor Drawable will not be visible
     * until the cursor is hidden and then drawn again.
     *
     * @see #setTextCursorDrawable(int)
     * @attr ref android.R.styleable#TextView_textCursorDrawable
     */
    public void setTextCursorDrawable(@NonNull Drawable textCursorDrawable) {
        Preconditions.checkNotNull(textCursorDrawable,
                "The cursor drawable should not be null.");
        mCursorDrawable = textCursorDrawable;
        mCursorDrawableRes = 0;
        if (mEditor != null) {
            mEditor.loadCursorDrawable();
        }
    }

    /**
     * Sets the Drawable corresponding to the text cursor. The Drawable defaults to the
     * value of the textCursorDrawable attribute.
     * Note that any change applied to the cursor Drawable will not be visible
     * until the cursor is hidden and then drawn again.
     *
     * @see #setTextCursorDrawable(Drawable)
     * @attr ref android.R.styleable#TextView_textCursorDrawable
     */
    public void setTextCursorDrawable(@DrawableRes int textCursorDrawable) {
        Preconditions.checkArgumentPositive(textCursorDrawable,
                "The cursor drawable should be a valid drawable resource id.");
        setTextCursorDrawable(mContext.getDrawable(textCursorDrawable));
    }

    /**
     * Returns the Drawable corresponding to the text cursor.
     * Note that any change applied to the cursor Drawable will not be visible
     * until the cursor is hidden and then drawn again.
     *
     * @return the text cursor drawable
     *
     * @see #setTextCursorDrawable(Drawable)
     * @see #setTextCursorDrawable(int)
     * @attr ref android.R.styleable#TextView_textCursorDrawable
     */
    @Nullable public Drawable getTextCursorDrawable() {
        if (mCursorDrawable == null && mCursorDrawableRes != 0) {
            mCursorDrawable = mContext.getDrawable(mCursorDrawableRes);
        }
        return mCursorDrawable;
    }

    /**
    /**
     * Sets the text appearance from the specified style resource.
     * Sets the text appearance from the specified style resource.
     * <p>
     * <p>