| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.text.style; |
| 18 |
|
| 19 |
import android.annotation.ColorInt; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.res.TypedArray; |
| 25 |
import android.graphics.Color; |
| 26 |
import android.os.Build; |
| 27 |
import android.os.Parcel; |
| 28 |
import android.os.Parcelable; |
| 29 |
import android.os.SystemClock; |
| 30 |
import android.text.ParcelableSpan; |
| 31 |
import android.text.TextPaint; |
| 32 |
import android.text.TextUtils; |
| 33 |
import android.util.Log; |
| 34 |
import android.widget.TextView; |
| 35 |
|
| 36 |
import java.util.Arrays; |
| 37 |
import java.util.Locale; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class SuggestionSpan extends CharacterStyle implements ParcelableSpan { |
| 52 |
|
| 53 |
private static final String TAG = "SuggestionSpan"; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public static final int FLAG_EASY_CORRECT = 0x0001; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static final int FLAG_MISSPELLED = 0x0002; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static final int FLAG_AUTO_CORRECTION = 0x0004; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static final int FLAG_GRAMMAR_ERROR = 0x0008; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
@Deprecated |
| 89 |
public static final String ACTION_SUGGESTION_PICKED = "android.text.style.SUGGESTION_PICKED"; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
@Deprecated |
| 97 |
public static final String SUGGESTION_SPAN_PICKED_AFTER = "after"; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
@Deprecated |
| 104 |
public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before"; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
@Deprecated |
| 111 |
public static final String SUGGESTION_SPAN_PICKED_HASHCODE = "hashcode"; |
| 112 |
|
| 113 |
public static final int SUGGESTIONS_MAX_SIZE = 5; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
private int mFlags; |
| 123 |
private final String[] mSuggestions; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
@NonNull |
| 130 |
private final String mLocaleStringForCompatibility; |
| 131 |
@NonNull |
| 132 |
private final String mLanguageTag; |
| 133 |
private final int mHashCode; |
| 134 |
|
| 135 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 136 |
private float mEasyCorrectUnderlineThickness; |
| 137 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 138 |
private int mEasyCorrectUnderlineColor; |
| 139 |
|
| 140 |
private float mMisspelledUnderlineThickness; |
| 141 |
private int mMisspelledUnderlineColor; |
| 142 |
|
| 143 |
private float mAutoCorrectionUnderlineThickness; |
| 144 |
private int mAutoCorrectionUnderlineColor; |
| 145 |
|
| 146 |
private float mGrammarErrorUnderlineThickness; |
| 147 |
private int mGrammarErrorUnderlineColor; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public SuggestionSpan(Context context, String[] suggestions, int flags) { |
| 155 |
this(context, null, suggestions, flags, null); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public SuggestionSpan(Locale locale, String[] suggestions, int flags) { |
| 164 |
this(null, locale, suggestions, flags, null); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public SuggestionSpan(Context context, Locale locale, String[] suggestions, int flags, |
| 179 |
Class<?> notificationTargetClass) { |
| 180 |
final int N = Math.min(SUGGESTIONS_MAX_SIZE, suggestions.length); |
| 181 |
mSuggestions = Arrays.copyOf(suggestions, N); |
| 182 |
mFlags = flags; |
| 183 |
final Locale sourceLocale; |
| 184 |
if (locale != null) { |
| 185 |
sourceLocale = locale; |
| 186 |
} else if (context != null) { |
| 187 |
|
| 188 |
sourceLocale = context.getResources().getConfiguration().locale; |
| 189 |
} else { |
| 190 |
Log.e("SuggestionSpan", "No locale or context specified in SuggestionSpan constructor"); |
| 191 |
sourceLocale = null; |
| 192 |
} |
| 193 |
mLocaleStringForCompatibility = sourceLocale == null ? "" : sourceLocale.toString(); |
| 194 |
mLanguageTag = sourceLocale == null ? "" : sourceLocale.toLanguageTag(); |
| 195 |
mHashCode = hashCodeInternal(mSuggestions, mLanguageTag, mLocaleStringForCompatibility); |
| 196 |
|
| 197 |
initStyle(context); |
| 198 |
} |
| 199 |
|
| 200 |
private void initStyle(Context context) { |
| 201 |
if (context == null) { |
| 202 |
mMisspelledUnderlineThickness = 0; |
| 203 |
mGrammarErrorUnderlineThickness = 0; |
| 204 |
mEasyCorrectUnderlineThickness = 0; |
| 205 |
mAutoCorrectionUnderlineThickness = 0; |
| 206 |
mMisspelledUnderlineColor = Color.BLACK; |
| 207 |
mGrammarErrorUnderlineColor = Color.BLACK; |
| 208 |
mEasyCorrectUnderlineColor = Color.BLACK; |
| 209 |
mAutoCorrectionUnderlineColor = Color.BLACK; |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
int defStyleAttr = com.android.internal.R.attr.textAppearanceMisspelledSuggestion; |
| 214 |
TypedArray typedArray = context.obtainStyledAttributes( |
| 215 |
null, com.android.internal.R.styleable.SuggestionSpan, defStyleAttr, 0); |
| 216 |
mMisspelledUnderlineThickness = typedArray.getDimension( |
| 217 |
com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0); |
| 218 |
mMisspelledUnderlineColor = typedArray.getColor( |
| 219 |
com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK); |
| 220 |
typedArray.recycle(); |
| 221 |
|
| 222 |
defStyleAttr = com.android.internal.R.attr.textAppearanceGrammarErrorSuggestion; |
| 223 |
typedArray = context.obtainStyledAttributes( |
| 224 |
null, com.android.internal.R.styleable.SuggestionSpan, defStyleAttr, 0); |
| 225 |
mGrammarErrorUnderlineThickness = typedArray.getDimension( |
| 226 |
com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0); |
| 227 |
mGrammarErrorUnderlineColor = typedArray.getColor( |
| 228 |
com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK); |
| 229 |
typedArray.recycle(); |
| 230 |
|
| 231 |
defStyleAttr = com.android.internal.R.attr.textAppearanceEasyCorrectSuggestion; |
| 232 |
typedArray = context.obtainStyledAttributes( |
| 233 |
null, com.android.internal.R.styleable.SuggestionSpan, defStyleAttr, 0); |
| 234 |
mEasyCorrectUnderlineThickness = typedArray.getDimension( |
| 235 |
com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0); |
| 236 |
mEasyCorrectUnderlineColor = typedArray.getColor( |
| 237 |
com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK); |
| 238 |
typedArray.recycle(); |
| 239 |
|
| 240 |
defStyleAttr = com.android.internal.R.attr.textAppearanceAutoCorrectionSuggestion; |
| 241 |
typedArray = context.obtainStyledAttributes( |
| 242 |
null, com.android.internal.R.styleable.SuggestionSpan, defStyleAttr, 0); |
| 243 |
mAutoCorrectionUnderlineThickness = typedArray.getDimension( |
| 244 |
com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0); |
| 245 |
mAutoCorrectionUnderlineColor = typedArray.getColor( |
| 246 |
com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK); |
| 247 |
typedArray.recycle(); |
| 248 |
} |
| 249 |
|
| 250 |
public SuggestionSpan(Parcel src) { |
| 251 |
mSuggestions = src.readStringArray(); |
| 252 |
mFlags = src.readInt(); |
| 253 |
mLocaleStringForCompatibility = src.readString(); |
| 254 |
mLanguageTag = src.readString(); |
| 255 |
mHashCode = src.readInt(); |
| 256 |
mEasyCorrectUnderlineColor = src.readInt(); |
| 257 |
mEasyCorrectUnderlineThickness = src.readFloat(); |
| 258 |
mMisspelledUnderlineColor = src.readInt(); |
| 259 |
mMisspelledUnderlineThickness = src.readFloat(); |
| 260 |
mAutoCorrectionUnderlineColor = src.readInt(); |
| 261 |
mAutoCorrectionUnderlineThickness = src.readFloat(); |
| 262 |
mGrammarErrorUnderlineColor = src.readInt(); |
| 263 |
mGrammarErrorUnderlineThickness = src.readFloat(); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public String[] getSuggestions() { |
| 270 |
return mSuggestions; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
@NonNull |
| 278 |
@Deprecated |
| 279 |
public String getLocale() { |
| 280 |
return mLocaleStringForCompatibility; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
@Nullable |
| 295 |
public Locale getLocaleObject() { |
| 296 |
return mLanguageTag.isEmpty() ? null : Locale.forLanguageTag(mLanguageTag); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
@Deprecated |
| 306 |
@UnsupportedAppUsage |
| 307 |
public String getNotificationTargetClassName() { |
| 308 |
return null; |
| 309 |
} |
| 310 |
|
| 311 |
public int getFlags() { |
| 312 |
return mFlags; |
| 313 |
} |
| 314 |
|
| 315 |
public void setFlags(int flags) { |
| 316 |
mFlags = flags; |
| 317 |
} |
| 318 |
|
| 319 |
@Override |
| 320 |
public int describeContents() { |
| 321 |
return 0; |
| 322 |
} |
| 323 |
|
| 324 |
@Override |
| 325 |
public void writeToParcel(Parcel dest, int flags) { |
| 326 |
writeToParcelInternal(dest, flags); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
public void writeToParcelInternal(Parcel dest, int flags) { |
| 331 |
dest.writeStringArray(mSuggestions); |
| 332 |
dest.writeInt(mFlags); |
| 333 |
dest.writeString(mLocaleStringForCompatibility); |
| 334 |
dest.writeString(mLanguageTag); |
| 335 |
dest.writeInt(mHashCode); |
| 336 |
dest.writeInt(mEasyCorrectUnderlineColor); |
| 337 |
dest.writeFloat(mEasyCorrectUnderlineThickness); |
| 338 |
dest.writeInt(mMisspelledUnderlineColor); |
| 339 |
dest.writeFloat(mMisspelledUnderlineThickness); |
| 340 |
dest.writeInt(mAutoCorrectionUnderlineColor); |
| 341 |
dest.writeFloat(mAutoCorrectionUnderlineThickness); |
| 342 |
dest.writeInt(mGrammarErrorUnderlineColor); |
| 343 |
dest.writeFloat(mGrammarErrorUnderlineThickness); |
| 344 |
} |
| 345 |
|
| 346 |
@Override |
| 347 |
public int getSpanTypeId() { |
| 348 |
return getSpanTypeIdInternal(); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
public int getSpanTypeIdInternal() { |
| 353 |
return TextUtils.SUGGESTION_SPAN; |
| 354 |
} |
| 355 |
|
| 356 |
@Override |
| 357 |
public boolean equals(@Nullable Object o) { |
| 358 |
if (o instanceof SuggestionSpan) { |
| 359 |
return ((SuggestionSpan)o).hashCode() == mHashCode; |
| 360 |
} |
| 361 |
return false; |
| 362 |
} |
| 363 |
|
| 364 |
@Override |
| 365 |
public int hashCode() { |
| 366 |
return mHashCode; |
| 367 |
} |
| 368 |
|
| 369 |
private static int hashCodeInternal(String[] suggestions, @NonNull String languageTag, |
| 370 |
@NonNull String localeStringForCompatibility) { |
| 371 |
return Arrays.hashCode(new Object[] {Long.valueOf(SystemClock.uptimeMillis()), suggestions, |
| 372 |
languageTag, localeStringForCompatibility}); |
| 373 |
} |
| 374 |
|
| 375 |
public static final @android.annotation.NonNull Parcelable.Creator<SuggestionSpan> CREATOR = |
| 376 |
new Parcelable.Creator<SuggestionSpan>() { |
| 377 |
@Override |
| 378 |
public SuggestionSpan createFromParcel(Parcel source) { |
| 379 |
return new SuggestionSpan(source); |
| 380 |
} |
| 381 |
|
| 382 |
@Override |
| 383 |
public SuggestionSpan[] newArray(int size) { |
| 384 |
return new SuggestionSpan[size]; |
| 385 |
} |
| 386 |
}; |
| 387 |
|
| 388 |
@Override |
| 389 |
public void updateDrawState(TextPaint tp) { |
| 390 |
final boolean misspelled = (mFlags & FLAG_MISSPELLED) != 0; |
| 391 |
final boolean easy = (mFlags & FLAG_EASY_CORRECT) != 0; |
| 392 |
final boolean autoCorrection = (mFlags & FLAG_AUTO_CORRECTION) != 0; |
| 393 |
final boolean grammarError = (mFlags & FLAG_GRAMMAR_ERROR) != 0; |
| 394 |
if (easy) { |
| 395 |
if (!misspelled && !grammarError) { |
| 396 |
tp.setUnderlineText(mEasyCorrectUnderlineColor, mEasyCorrectUnderlineThickness); |
| 397 |
} else if (tp.underlineColor == 0) { |
| 398 |
|
| 399 |
|
| 400 |
if (grammarError) { |
| 401 |
tp.setUnderlineText( |
| 402 |
mGrammarErrorUnderlineColor, mGrammarErrorUnderlineThickness); |
| 403 |
} else { |
| 404 |
tp.setUnderlineText(mMisspelledUnderlineColor, mMisspelledUnderlineThickness); |
| 405 |
} |
| 406 |
} |
| 407 |
} else if (autoCorrection) { |
| 408 |
tp.setUnderlineText(mAutoCorrectionUnderlineColor, mAutoCorrectionUnderlineThickness); |
| 409 |
} else if (misspelled) { |
| 410 |
tp.setUnderlineText(mMisspelledUnderlineColor, mMisspelledUnderlineThickness); |
| 411 |
} else if (grammarError) { |
| 412 |
tp.setUnderlineText(mGrammarErrorUnderlineColor, mGrammarErrorUnderlineThickness); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
@ColorInt |
| 420 |
public int getUnderlineColor() { |
| 421 |
|
| 422 |
final boolean misspelled = (mFlags & FLAG_MISSPELLED) != 0; |
| 423 |
final boolean easy = (mFlags & FLAG_EASY_CORRECT) != 0; |
| 424 |
final boolean autoCorrection = (mFlags & FLAG_AUTO_CORRECTION) != 0; |
| 425 |
final boolean grammarError = (mFlags & FLAG_GRAMMAR_ERROR) != 0; |
| 426 |
if (easy) { |
| 427 |
if (grammarError) { |
| 428 |
return mGrammarErrorUnderlineColor; |
| 429 |
} else if (misspelled) { |
| 430 |
return mMisspelledUnderlineColor; |
| 431 |
} else { |
| 432 |
return mEasyCorrectUnderlineColor; |
| 433 |
} |
| 434 |
} else if (autoCorrection) { |
| 435 |
return mAutoCorrectionUnderlineColor; |
| 436 |
} else if (misspelled) { |
| 437 |
return mMisspelledUnderlineColor; |
| 438 |
} else if (grammarError) { |
| 439 |
return mGrammarErrorUnderlineColor; |
| 440 |
} |
| 441 |
return 0; |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 451 |
@Deprecated |
| 452 |
public void notifySelection(Context context, String original, int index) { |
| 453 |
Log.w(TAG, "notifySelection() is deprecated. Does nothing."); |
| 454 |
} |
| 455 |
} |