diff options
| author | 2018-12-04 15:45:00 +0000 | |
|---|---|---|
| committer | 2018-12-06 10:39:34 +0000 | |
| commit | 6c7ad1d0180935679c4f70aa870ed02812a6f67c (patch) | |
| tree | 66b8c82bd2ed1df92f011acbedb86347a10e0b5b | |
| parent | 188cf112d2e71eefe6657c3677c91cb546ff4cc8 (diff) | |
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
| -rw-r--r-- | api/current.txt | 3 | ||||
| -rw-r--r-- | core/java/android/widget/Editor.java | 15 | ||||
| -rw-r--r-- | core/java/android/widget/TextView.java | 57 |
3 files changed, 68 insertions, 7 deletions
diff --git a/api/current.txt b/api/current.txt index 70a56b08fb85..7e19b1fccc42 100644 --- a/api/current.txt +++ b/api/current.txt @@ -55956,6 +55956,7 @@ package android.widget { method public java.lang.CharSequence getText(); method public android.view.textclassifier.TextClassifier getTextClassifier(); method public final android.content.res.ColorStateList getTextColors(); + method public android.graphics.drawable.Drawable getTextCursorDrawable(); method public android.text.TextDirectionHeuristic getTextDirectionHeuristic(); method public java.util.Locale getTextLocale(); method public android.os.LocaleList getTextLocales(); @@ -56086,6 +56087,8 @@ package android.widget { method public void setTextClassifier(android.view.textclassifier.TextClassifier); method public void setTextColor(int); 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 final void setTextKeepState(java.lang.CharSequence); method public final void setTextKeepState(java.lang.CharSequence, android.widget.TextView.BufferType); diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index c6155ced9c9f..3c32bb29cf66 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -2051,8 +2051,8 @@ public class Editor { } void updateCursorPosition() { - if (mTextView.mCursorDrawableRes == 0) { - mDrawableForCursor = null; + loadCursorDrawable(); + if (mDrawableForCursor == null) { return; } @@ -2462,10 +2462,7 @@ public class Editor { } private void updateCursorPosition(int top, int bottom, float horizontal) { - if (mDrawableForCursor == null) { - mDrawableForCursor = mTextView.getContext().getDrawable( - mTextView.mCursorDrawableRes); - } + loadCursorDrawable(); final int left = clampHorizontalPosition(mDrawableForCursor, horizontal); final int width = mDrawableForCursor.getIntrinsicWidth(); mDrawableForCursor.setBounds(left, top - mTempRect.top, left + width, @@ -5698,6 +5695,12 @@ public class Editor { public boolean isActive(); } + void loadCursorDrawable() { + if (mDrawableForCursor == null) { + mDrawableForCursor = mTextView.getTextCursorDrawable(); + } + } + private class InsertionPointCursorController implements CursorController { private InsertionHandleView mHandle; diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 085f8f1d678f..90cf871830fd 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -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 // they are defined by the TextView's style and are theme-dependent. - @UnsupportedAppUsage + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) int mCursorDrawableRes; + private Drawable mCursorDrawable; // 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. @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 * 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 * @@ -3655,6 +3658,58 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } /** + * 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. * <p> * Use a framework-defined {@code TextAppearance} style like |