diff options
| author | 2021-11-09 17:45:55 -0800 | |
|---|---|---|
| committer | 2021-12-06 12:42:53 -0800 | |
| commit | dd5ca6bf569dde5609e3f6eddaa3cc95e80e537f (patch) | |
| tree | 661c06f62029a47e5cfca2f68f22ba7ad90f6b25 | |
| parent | fbfd734049a5efd404bb4b1728d5205bd34a2c13 (diff) | |
Make SuggestionRangeSpan public
when we have overlapped SuggestionSpans, the
range of text being replaced can change depends on
the number of suggestions in the SuggestionSpans, so
accessibility services need to know the text being replaced.
We send text change event with the before text doesn't have the
SuggestionRangeSpan and after text has the SuggestionRangeSpan,
so a11y services can inform the user about the text being
replaced.
Other ui toolkits like compose can convert their style indicating
text being replaced to SuggestionRangeSpan.
Bug: b/143378480
Test: tested the event is sent.
Change-Id: I6d0d33e46f7c8ac9dbcc177ab54718184e715fb6
| -rw-r--r-- | core/api/current.txt | 11 | ||||
| -rw-r--r-- | core/java/android/text/style/SuggestionRangeSpan.java | 34 | ||||
| -rw-r--r-- | core/java/android/widget/Editor.java | 8 |
3 files changed, 43 insertions, 10 deletions
diff --git a/core/api/current.txt b/core/api/current.txt index c5f85d9f7818..a0cd430e8e61 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -45687,6 +45687,17 @@ package android.text.style { method public void writeToParcel(android.os.Parcel, int); } + public final class SuggestionRangeSpan extends android.text.style.CharacterStyle implements android.text.ParcelableSpan { + ctor public SuggestionRangeSpan(); + method public int describeContents(); + method public int getBackgroundColor(); + method public int getSpanTypeId(); + method public void setBackgroundColor(int); + method public void updateDrawState(@NonNull android.text.TextPaint); + method public void writeToParcel(@NonNull android.os.Parcel, int); + field @NonNull public static final android.os.Parcelable.Creator<android.text.style.SuggestionRangeSpan> CREATOR; + } + public class SuggestionSpan extends android.text.style.CharacterStyle implements android.text.ParcelableSpan { ctor public SuggestionSpan(android.content.Context, String[], int); ctor public SuggestionSpan(java.util.Locale, String[], int); diff --git a/core/java/android/text/style/SuggestionRangeSpan.java b/core/java/android/text/style/SuggestionRangeSpan.java index 2b04a7ac2ab8..1eee99aaac62 100644 --- a/core/java/android/text/style/SuggestionRangeSpan.java +++ b/core/java/android/text/style/SuggestionRangeSpan.java @@ -16,8 +16,9 @@ package android.text.style; -import android.compat.annotation.UnsupportedAppUsage; +import android.annotation.NonNull; import android.os.Parcel; +import android.os.Parcelable; import android.text.ParcelableSpan; import android.text.TextPaint; import android.text.TextUtils; @@ -25,30 +26,40 @@ import android.text.TextUtils; /** * A SuggestionRangeSpan is used to show which part of an EditText is affected by a suggestion * popup window. - * - * @hide */ -public class SuggestionRangeSpan extends CharacterStyle implements ParcelableSpan { +public final class SuggestionRangeSpan extends CharacterStyle implements ParcelableSpan { private int mBackgroundColor; - @UnsupportedAppUsage public SuggestionRangeSpan() { // 0 is a fully transparent black. Has to be set using #setBackgroundColor mBackgroundColor = 0; } - @UnsupportedAppUsage - public SuggestionRangeSpan(Parcel src) { + /** @hide */ + public SuggestionRangeSpan(@NonNull Parcel src) { mBackgroundColor = src.readInt(); } + public static final @NonNull Parcelable.Creator<SuggestionRangeSpan> + CREATOR = new Parcelable.Creator<SuggestionRangeSpan>() { + @Override + public SuggestionRangeSpan createFromParcel(Parcel source) { + return new SuggestionRangeSpan(source); + } + + @Override + public SuggestionRangeSpan[] newArray(int size) { + return new SuggestionRangeSpan[size]; + } + }; + @Override public int describeContents() { return 0; } @Override - public void writeToParcel(Parcel dest, int flags) { + public void writeToParcel(@NonNull Parcel dest, int flags) { writeToParcelInternal(dest, flags); } @@ -67,13 +78,16 @@ public class SuggestionRangeSpan extends CharacterStyle implements ParcelableSpa return TextUtils.SUGGESTION_RANGE_SPAN; } - @UnsupportedAppUsage public void setBackgroundColor(int backgroundColor) { mBackgroundColor = backgroundColor; } + public int getBackgroundColor() { + return mBackgroundColor; + } + @Override - public void updateDrawState(TextPaint tp) { + public void updateDrawState(@NonNull TextPaint tp) { tp.bgColor = mBackgroundColor; } } diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 29425b5364a6..96952089ef51 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -69,6 +69,7 @@ import android.text.SpanWatcher; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.Spanned; +import android.text.SpannedString; import android.text.StaticLayout; import android.text.TextUtils; import android.text.method.KeyListener; @@ -4120,8 +4121,15 @@ public class Editor { mSuggestionRangeSpan.setBackgroundColor( (underlineColor & 0x00FFFFFF) + (newAlpha << 24)); } + boolean sendAccessibilityEvent = mTextView.isVisibleToAccessibility(); + CharSequence beforeText = sendAccessibilityEvent + ? new SpannedString(spannable, true) : null; spannable.setSpan(mSuggestionRangeSpan, spanUnionStart, spanUnionEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + if (sendAccessibilityEvent) { + mTextView.sendAccessibilityEventTypeViewTextChanged( + beforeText, spanUnionStart, spanUnionEnd); + } mSuggestionsAdapter.notifyDataSetChanged(); return true; |