From dd5ca6bf569dde5609e3f6eddaa3cc95e80e537f Mon Sep 17 00:00:00 2001 From: yingleiw Date: Tue, 9 Nov 2021 17:45:55 -0800 Subject: [PATCH] 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 --- core/api/current.txt | 11 ++++++ .../text/style/SuggestionRangeSpan.java | 34 +++++++++++++------ 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 c5f85d9f78180..a0cd430e8e61e 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 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 2b04a7ac2ab84..1eee99aaac62e 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 + CREATOR = new Parcelable.Creator() { + @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 29425b5364a6e..96952089ef519 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;