satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.text.style; |
| 18 | |
Qingqing Deng | ef20b23 | 2019-01-09 12:22:02 -0800 | [diff] [blame] | 19 | import android.annotation.ColorInt; |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Nullable; |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 23 | import android.content.Context; |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
| 25 | import android.graphics.Color; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 26 | import android.os.Parcel; |
| 27 | import android.os.Parcelable; |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 28 | import android.os.SystemClock; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 29 | import android.text.ParcelableSpan; |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 30 | import android.text.TextPaint; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 31 | import android.text.TextUtils; |
Gilles Debunne | 6e0b22b | 2012-01-25 14:57:40 -0800 | [diff] [blame] | 32 | import android.util.Log; |
Gilles Debunne | 4dacef2 | 2011-07-08 11:45:39 -0700 | [diff] [blame] | 33 | import android.widget.TextView; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 34 | |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 35 | import java.util.Arrays; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 36 | import java.util.Locale; |
| 37 | |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 38 | /** |
Gilles Debunne | 4dacef2 | 2011-07-08 11:45:39 -0700 | [diff] [blame] | 39 | * Holds suggestion candidates for the text enclosed in this span. |
| 40 | * |
| 41 | * When such a span is edited in an EditText, double tapping on the text enclosed in this span will |
| 42 | * display a popup dialog listing suggestion replacement for that text. The user can then replace |
| 43 | * the original text by one of the suggestions. |
| 44 | * |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 45 | * These spans should typically be created by the input method to provide correction and alternates |
Gilles Debunne | 4dacef2 | 2011-07-08 11:45:39 -0700 | [diff] [blame] | 46 | * for the text. |
| 47 | * |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 48 | * @see TextView#isSuggestionsEnabled() |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 49 | */ |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 50 | public class SuggestionSpan extends CharacterStyle implements ParcelableSpan { |
| 51 | |
Luca Zanolin | 0c96b81f | 2012-08-29 11:33:12 +0100 | [diff] [blame] | 52 | private static final String TAG = "SuggestionSpan"; |
| 53 | |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 54 | /** |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 55 | * Sets this flag if the suggestions should be easily accessible with few interactions. |
| 56 | * This flag should be set for every suggestions that the user is likely to use. |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 57 | */ |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 58 | public static final int FLAG_EASY_CORRECT = 0x0001; |
| 59 | |
| 60 | /** |
| 61 | * Sets this flag if the suggestions apply to a misspelled word/text. This type of suggestion is |
| 62 | * rendered differently to highlight the error. |
| 63 | */ |
| 64 | public static final int FLAG_MISSPELLED = 0x0002; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 65 | |
satok | 9ca4b43 | 2011-10-07 15:08:19 +0900 | [diff] [blame] | 66 | /** |
| 67 | * Sets this flag if the auto correction is about to be applied to a word/text |
| 68 | * that the user is typing/composing. This type of suggestion is rendered differently |
| 69 | * to indicate the auto correction is happening. |
satok | 9ca4b43 | 2011-10-07 15:08:19 +0900 | [diff] [blame] | 70 | */ |
| 71 | public static final int FLAG_AUTO_CORRECTION = 0x0004; |
| 72 | |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 73 | /** |
| 74 | * This action is deprecated in {@link android.os.Build.VERSION_CODES#Q}. |
| 75 | * |
| 76 | * @deprecated For IMEs to receive this kind of user interaction signals, implement IMEs' own |
| 77 | * suggestion picker UI instead of relying on {@link SuggestionSpan}. To retrieve |
| 78 | * bounding boxes for each character of the composing text, use |
| 79 | * {@link android.view.inputmethod.CursorAnchorInfo}. |
| 80 | */ |
| 81 | @Deprecated |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 82 | public static final String ACTION_SUGGESTION_PICKED = "android.text.style.SUGGESTION_PICKED"; |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * This is deprecated in {@link android.os.Build.VERSION_CODES#Q}. |
| 86 | * |
| 87 | * @deprecated See {@link #ACTION_SUGGESTION_PICKED}. |
| 88 | */ |
| 89 | @Deprecated |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 90 | public static final String SUGGESTION_SPAN_PICKED_AFTER = "after"; |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 91 | /** |
| 92 | * This is deprecated in {@link android.os.Build.VERSION_CODES#Q}. |
| 93 | * |
| 94 | * @deprecated See {@link #ACTION_SUGGESTION_PICKED}. |
| 95 | */ |
| 96 | @Deprecated |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 97 | public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before"; |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 98 | /** |
| 99 | * This is deprecated in {@link android.os.Build.VERSION_CODES#Q}. |
| 100 | * |
| 101 | * @deprecated See {@link #ACTION_SUGGESTION_PICKED}. |
| 102 | */ |
| 103 | @Deprecated |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 104 | public static final String SUGGESTION_SPAN_PICKED_HASHCODE = "hashcode"; |
| 105 | |
| 106 | public static final int SUGGESTIONS_MAX_SIZE = 5; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * TODO: Needs to check the validity and add a feature that TextView will change |
satok | b3fc1a5 | 2011-04-06 18:28:55 +0900 | [diff] [blame] | 110 | * the current IME to the other IME which is specified in SuggestionSpan. |
| 111 | * An IME needs to set the span by specifying the target IME and Subtype of SuggestionSpan. |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 112 | * And the current IME might want to specify any IME as the target IME including other IMEs. |
| 113 | */ |
| 114 | |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 115 | private int mFlags; |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 116 | private final String[] mSuggestions; |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 117 | /** |
| 118 | * Kept for compatibility for apps that rely on invalid locale strings e.g. |
| 119 | * {@code new Locale(" an ", " i n v a l i d ", "data")}, which cannot be handled by |
| 120 | * {@link #mLanguageTag}. |
| 121 | */ |
| 122 | @NonNull |
| 123 | private final String mLocaleStringForCompatibility; |
| 124 | @NonNull |
| 125 | private final String mLanguageTag; |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 126 | private final int mHashCode; |
| 127 | |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame] | 128 | @UnsupportedAppUsage |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 129 | private float mEasyCorrectUnderlineThickness; |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame] | 130 | @UnsupportedAppUsage |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 131 | private int mEasyCorrectUnderlineColor; |
| 132 | |
| 133 | private float mMisspelledUnderlineThickness; |
| 134 | private int mMisspelledUnderlineColor; |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 135 | |
satok | 9ca4b43 | 2011-10-07 15:08:19 +0900 | [diff] [blame] | 136 | private float mAutoCorrectionUnderlineThickness; |
| 137 | private int mAutoCorrectionUnderlineColor; |
| 138 | |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 139 | /** |
| 140 | * @param context Context for the application |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 141 | * @param suggestions Suggestions for the string under the span |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 142 | * @param flags Additional flags indicating how this span is handled in TextView |
| 143 | */ |
satok | b3fc1a5 | 2011-04-06 18:28:55 +0900 | [diff] [blame] | 144 | public SuggestionSpan(Context context, String[] suggestions, int flags) { |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 145 | this(context, null, suggestions, flags, null); |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @param locale Locale of the suggestions |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 150 | * @param suggestions Suggestions for the string under the span |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 151 | * @param flags Additional flags indicating how this span is handled in TextView |
| 152 | */ |
satok | b3fc1a5 | 2011-04-06 18:28:55 +0900 | [diff] [blame] | 153 | public SuggestionSpan(Locale locale, String[] suggestions, int flags) { |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 154 | this(null, locale, suggestions, flags, null); |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @param context Context for the application |
| 159 | * @param locale locale Locale of the suggestions |
Gilles Debunne | c9fd978 | 2011-09-09 09:20:12 -0700 | [diff] [blame] | 160 | * @param suggestions Suggestions for the string under the span. Only the first up to |
Gilles Debunne | 6e0b22b | 2012-01-25 14:57:40 -0800 | [diff] [blame] | 161 | * {@link SuggestionSpan#SUGGESTIONS_MAX_SIZE} will be considered. Null values not permitted. |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 162 | * @param flags Additional flags indicating how this span is handled in TextView |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 163 | * @param notificationTargetClass if not null, this class will get notified when the user |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 164 | * selects one of the suggestions. On Android |
| 165 | * {@link android.os.Build.VERSION_CODES#Q} and later this |
| 166 | * parameter is always ignored. |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 167 | */ |
satok | b3fc1a5 | 2011-04-06 18:28:55 +0900 | [diff] [blame] | 168 | public SuggestionSpan(Context context, Locale locale, String[] suggestions, int flags, |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 169 | Class<?> notificationTargetClass) { |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 170 | final int N = Math.min(SUGGESTIONS_MAX_SIZE, suggestions.length); |
| 171 | mSuggestions = Arrays.copyOf(suggestions, N); |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 172 | mFlags = flags; |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 173 | final Locale sourceLocale; |
Gilles Debunne | 6e0b22b | 2012-01-25 14:57:40 -0800 | [diff] [blame] | 174 | if (locale != null) { |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 175 | sourceLocale = locale; |
Gilles Debunne | 6e0b22b | 2012-01-25 14:57:40 -0800 | [diff] [blame] | 176 | } else if (context != null) { |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 177 | // TODO: Consider to context.getResources().getResolvedLocale() instead. |
| 178 | sourceLocale = context.getResources().getConfiguration().locale; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 179 | } else { |
Gilles Debunne | 6e0b22b | 2012-01-25 14:57:40 -0800 | [diff] [blame] | 180 | Log.e("SuggestionSpan", "No locale or context specified in SuggestionSpan constructor"); |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 181 | sourceLocale = null; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 182 | } |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 183 | mLocaleStringForCompatibility = sourceLocale == null ? "" : sourceLocale.toString(); |
| 184 | mLanguageTag = sourceLocale == null ? "" : sourceLocale.toLanguageTag(); |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 185 | mHashCode = hashCodeInternal(mSuggestions, mLanguageTag, mLocaleStringForCompatibility); |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 186 | |
| 187 | initStyle(context); |
| 188 | } |
| 189 | |
| 190 | private void initStyle(Context context) { |
Gilles Debunne | c5436f2 | 2011-12-06 10:27:00 -0800 | [diff] [blame] | 191 | if (context == null) { |
| 192 | mMisspelledUnderlineThickness = 0; |
| 193 | mEasyCorrectUnderlineThickness = 0; |
| 194 | mAutoCorrectionUnderlineThickness = 0; |
| 195 | mMisspelledUnderlineColor = Color.BLACK; |
| 196 | mEasyCorrectUnderlineColor = Color.BLACK; |
| 197 | mAutoCorrectionUnderlineColor = Color.BLACK; |
| 198 | return; |
| 199 | } |
| 200 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 201 | int defStyleAttr = com.android.internal.R.attr.textAppearanceMisspelledSuggestion; |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 202 | TypedArray typedArray = context.obtainStyledAttributes( |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 203 | null, com.android.internal.R.styleable.SuggestionSpan, defStyleAttr, 0); |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 204 | mMisspelledUnderlineThickness = typedArray.getDimension( |
Luca Zanolin | e6d3682 | 2011-08-30 18:04:34 +0100 | [diff] [blame] | 205 | com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0); |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 206 | mMisspelledUnderlineColor = typedArray.getColor( |
| 207 | com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK); |
| 208 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 209 | defStyleAttr = com.android.internal.R.attr.textAppearanceEasyCorrectSuggestion; |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 210 | typedArray = context.obtainStyledAttributes( |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 211 | null, com.android.internal.R.styleable.SuggestionSpan, defStyleAttr, 0); |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 212 | mEasyCorrectUnderlineThickness = typedArray.getDimension( |
| 213 | com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0); |
| 214 | mEasyCorrectUnderlineColor = typedArray.getColor( |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 215 | com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK); |
satok | 9ca4b43 | 2011-10-07 15:08:19 +0900 | [diff] [blame] | 216 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 217 | defStyleAttr = com.android.internal.R.attr.textAppearanceAutoCorrectionSuggestion; |
satok | 9ca4b43 | 2011-10-07 15:08:19 +0900 | [diff] [blame] | 218 | typedArray = context.obtainStyledAttributes( |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 219 | null, com.android.internal.R.styleable.SuggestionSpan, defStyleAttr, 0); |
satok | 9ca4b43 | 2011-10-07 15:08:19 +0900 | [diff] [blame] | 220 | mAutoCorrectionUnderlineThickness = typedArray.getDimension( |
| 221 | com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0); |
| 222 | mAutoCorrectionUnderlineColor = typedArray.getColor( |
| 223 | com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK); |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 224 | } |
| 225 | |
satok | b3fc1a5 | 2011-04-06 18:28:55 +0900 | [diff] [blame] | 226 | public SuggestionSpan(Parcel src) { |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 227 | mSuggestions = src.readStringArray(); |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 228 | mFlags = src.readInt(); |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 229 | mLocaleStringForCompatibility = src.readString(); |
| 230 | mLanguageTag = src.readString(); |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 231 | mHashCode = src.readInt(); |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 232 | mEasyCorrectUnderlineColor = src.readInt(); |
| 233 | mEasyCorrectUnderlineThickness = src.readFloat(); |
| 234 | mMisspelledUnderlineColor = src.readInt(); |
| 235 | mMisspelledUnderlineThickness = src.readFloat(); |
satok | 9ca4b43 | 2011-10-07 15:08:19 +0900 | [diff] [blame] | 236 | mAutoCorrectionUnderlineColor = src.readInt(); |
| 237 | mAutoCorrectionUnderlineThickness = src.readFloat(); |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
Gilles Debunne | 4dacef2 | 2011-07-08 11:45:39 -0700 | [diff] [blame] | 241 | * @return an array of suggestion texts for this span |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 242 | */ |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 243 | public String[] getSuggestions() { |
Gilles Debunne | ee511cc | 2011-05-05 14:57:50 -0700 | [diff] [blame] | 244 | return mSuggestions; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /** |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 248 | * @deprecated use {@link #getLocaleObject()} instead. |
| 249 | * @return the locale of the suggestions. An empty string is returned if no locale is specified. |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 250 | */ |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 251 | @NonNull |
| 252 | @Deprecated |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 253 | public String getLocale() { |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 254 | return mLocaleStringForCompatibility; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Returns a well-formed BCP 47 language tag representation of the suggestions, as a |
| 259 | * {@link Locale} object. |
| 260 | * |
| 261 | * <p><b>Caveat</b>: The returned object is guaranteed to be a a well-formed BCP 47 language tag |
| 262 | * representation. For example, this method can return an empty locale rather than returning a |
| 263 | * malformed data when this object is initialized with an malformed {@link Locale} object, e.g. |
| 264 | * {@code new Locale(" a ", " b c d ", " "}.</p> |
| 265 | * |
| 266 | * @return the locale of the suggestions. {@code null} is returned if no locale is specified. |
| 267 | */ |
| 268 | @Nullable |
| 269 | public Locale getLocaleObject() { |
| 270 | return mLanguageTag.isEmpty() ? null : Locale.forLanguageTag(mLanguageTag); |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 274 | * @return {@code null}. |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 275 | * |
| 276 | * @hide |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 277 | * @deprecated Do not use. Always returns {@code null}. |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 278 | */ |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 279 | @Deprecated |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame] | 280 | @UnsupportedAppUsage |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 281 | public String getNotificationTargetClassName() { |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 282 | return null; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | public int getFlags() { |
| 286 | return mFlags; |
| 287 | } |
| 288 | |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 289 | public void setFlags(int flags) { |
| 290 | mFlags = flags; |
| 291 | } |
| 292 | |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 293 | @Override |
| 294 | public int describeContents() { |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public void writeToParcel(Parcel dest, int flags) { |
Alan Viverette | a70d4a9 | 2015-06-02 16:11:00 -0700 | [diff] [blame] | 300 | writeToParcelInternal(dest, flags); |
| 301 | } |
| 302 | |
| 303 | /** @hide */ |
| 304 | public void writeToParcelInternal(Parcel dest, int flags) { |
satok | e3797a1 | 2011-03-22 06:34:48 +0900 | [diff] [blame] | 305 | dest.writeStringArray(mSuggestions); |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 306 | dest.writeInt(mFlags); |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 307 | dest.writeString(mLocaleStringForCompatibility); |
| 308 | dest.writeString(mLanguageTag); |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 309 | dest.writeInt(mHashCode); |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 310 | dest.writeInt(mEasyCorrectUnderlineColor); |
| 311 | dest.writeFloat(mEasyCorrectUnderlineThickness); |
| 312 | dest.writeInt(mMisspelledUnderlineColor); |
| 313 | dest.writeFloat(mMisspelledUnderlineThickness); |
satok | 9ca4b43 | 2011-10-07 15:08:19 +0900 | [diff] [blame] | 314 | dest.writeInt(mAutoCorrectionUnderlineColor); |
| 315 | dest.writeFloat(mAutoCorrectionUnderlineThickness); |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public int getSpanTypeId() { |
Alan Viverette | a70d4a9 | 2015-06-02 16:11:00 -0700 | [diff] [blame] | 320 | return getSpanTypeIdInternal(); |
| 321 | } |
| 322 | |
| 323 | /** @hide */ |
| 324 | public int getSpanTypeIdInternal() { |
Gilles Debunne | a00972a | 2011-04-13 16:07:31 -0700 | [diff] [blame] | 325 | return TextUtils.SUGGESTION_SPAN; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 326 | } |
| 327 | |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 328 | @Override |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 329 | public boolean equals(Object o) { |
| 330 | if (o instanceof SuggestionSpan) { |
| 331 | return ((SuggestionSpan)o).hashCode() == mHashCode; |
| 332 | } |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | @Override |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 337 | public int hashCode() { |
| 338 | return mHashCode; |
| 339 | } |
| 340 | |
Yohei Yukawa | 60fbc8e | 2016-01-14 12:16:06 -0800 | [diff] [blame] | 341 | private static int hashCodeInternal(String[] suggestions, @NonNull String languageTag, |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 342 | @NonNull String localeStringForCompatibility) { |
Gilles Debunne | 6435a56 | 2011-08-04 21:22:30 -0700 | [diff] [blame] | 343 | return Arrays.hashCode(new Object[] {Long.valueOf(SystemClock.uptimeMillis()), suggestions, |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 344 | languageTag, localeStringForCompatibility}); |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 345 | } |
| 346 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 347 | public static final @android.annotation.NonNull Parcelable.Creator<SuggestionSpan> CREATOR = |
satok | b3fc1a5 | 2011-04-06 18:28:55 +0900 | [diff] [blame] | 348 | new Parcelable.Creator<SuggestionSpan>() { |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 349 | @Override |
satok | b3fc1a5 | 2011-04-06 18:28:55 +0900 | [diff] [blame] | 350 | public SuggestionSpan createFromParcel(Parcel source) { |
| 351 | return new SuggestionSpan(source); |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | @Override |
satok | b3fc1a5 | 2011-04-06 18:28:55 +0900 | [diff] [blame] | 355 | public SuggestionSpan[] newArray(int size) { |
| 356 | return new SuggestionSpan[size]; |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 357 | } |
| 358 | }; |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 359 | |
| 360 | @Override |
| 361 | public void updateDrawState(TextPaint tp) { |
Gilles Debunne | c9fd978 | 2011-09-09 09:20:12 -0700 | [diff] [blame] | 362 | final boolean misspelled = (mFlags & FLAG_MISSPELLED) != 0; |
| 363 | final boolean easy = (mFlags & FLAG_EASY_CORRECT) != 0; |
satok | 9ca4b43 | 2011-10-07 15:08:19 +0900 | [diff] [blame] | 364 | final boolean autoCorrection = (mFlags & FLAG_AUTO_CORRECTION) != 0; |
Gilles Debunne | c9fd978 | 2011-09-09 09:20:12 -0700 | [diff] [blame] | 365 | if (easy) { |
| 366 | if (!misspelled) { |
| 367 | tp.setUnderlineText(mEasyCorrectUnderlineColor, mEasyCorrectUnderlineThickness); |
| 368 | } else if (tp.underlineColor == 0) { |
| 369 | // Spans are rendered in an arbitrary order. Since misspelled is less prioritary |
| 370 | // than just easy, do not apply misspelled if an easy (or a mispelled) has been set |
| 371 | tp.setUnderlineText(mMisspelledUnderlineColor, mMisspelledUnderlineThickness); |
| 372 | } |
satok | 9ca4b43 | 2011-10-07 15:08:19 +0900 | [diff] [blame] | 373 | } else if (autoCorrection) { |
| 374 | tp.setUnderlineText(mAutoCorrectionUnderlineColor, mAutoCorrectionUnderlineThickness); |
Luca Zanolin | fe5e983 | 2011-09-02 19:41:42 +0100 | [diff] [blame] | 375 | } |
Luca Zanolin | 7d1c55f | 2011-08-16 14:59:26 +0100 | [diff] [blame] | 376 | } |
Gilles Debunne | fa4e2d9 | 2011-09-08 18:34:22 -0700 | [diff] [blame] | 377 | |
| 378 | /** |
| 379 | * @return The color of the underline for that span, or 0 if there is no underline |
| 380 | */ |
Qingqing Deng | ef20b23 | 2019-01-09 12:22:02 -0800 | [diff] [blame] | 381 | @ColorInt |
Gilles Debunne | fa4e2d9 | 2011-09-08 18:34:22 -0700 | [diff] [blame] | 382 | public int getUnderlineColor() { |
| 383 | // The order here should match what is used in updateDrawState |
Gilles Debunne | c9fd978 | 2011-09-09 09:20:12 -0700 | [diff] [blame] | 384 | final boolean misspelled = (mFlags & FLAG_MISSPELLED) != 0; |
| 385 | final boolean easy = (mFlags & FLAG_EASY_CORRECT) != 0; |
satok | 9ca4b43 | 2011-10-07 15:08:19 +0900 | [diff] [blame] | 386 | final boolean autoCorrection = (mFlags & FLAG_AUTO_CORRECTION) != 0; |
Gilles Debunne | c9fd978 | 2011-09-09 09:20:12 -0700 | [diff] [blame] | 387 | if (easy) { |
| 388 | if (!misspelled) { |
| 389 | return mEasyCorrectUnderlineColor; |
| 390 | } else { |
| 391 | return mMisspelledUnderlineColor; |
| 392 | } |
satok | 9ca4b43 | 2011-10-07 15:08:19 +0900 | [diff] [blame] | 393 | } else if (autoCorrection) { |
| 394 | return mAutoCorrectionUnderlineColor; |
Gilles Debunne | c9fd978 | 2011-09-09 09:20:12 -0700 | [diff] [blame] | 395 | } |
Gilles Debunne | fa4e2d9 | 2011-09-08 18:34:22 -0700 | [diff] [blame] | 396 | return 0; |
| 397 | } |
Luca Zanolin | 0c96b81f | 2012-08-29 11:33:12 +0100 | [diff] [blame] | 398 | |
| 399 | /** |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 400 | * Does nothing. |
Luca Zanolin | 0c96b81f | 2012-08-29 11:33:12 +0100 | [diff] [blame] | 401 | * |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 402 | * @deprecated this is deprecated in {@link android.os.Build.VERSION_CODES#Q}. |
Luca Zanolin | 0c96b81f | 2012-08-29 11:33:12 +0100 | [diff] [blame] | 403 | * @hide |
| 404 | */ |
Mathew Inwood | efeab84 | 2018-08-14 15:21:30 +0100 | [diff] [blame] | 405 | @UnsupportedAppUsage |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 406 | @Deprecated |
Luca Zanolin | 0c96b81f | 2012-08-29 11:33:12 +0100 | [diff] [blame] | 407 | public void notifySelection(Context context, String original, int index) { |
Yohei Yukawa | 17ace29 | 2019-01-21 09:24:26 -0800 | [diff] [blame] | 408 | Log.w(TAG, "notifySelection() is deprecated. Does nothing."); |
Luca Zanolin | 0c96b81f | 2012-08-29 11:33:12 +0100 | [diff] [blame] | 409 | } |
satok | adb4358 | 2011-03-09 10:08:47 +0900 | [diff] [blame] | 410 | } |