diff options
author | 2015-04-17 16:48:10 -0700 | |
---|---|---|
committer | 2015-04-20 20:29:44 -0700 | |
commit | 490fa47a46015f2c8bd8f9010d236bfe5aecd4bb (patch) | |
tree | 55599c8c109d3959fdc8f616b144ed43ffb755b7 /java/src | |
parent | 54e891e2bdce2d3d1f8a319c1f596fd6590314f3 (diff) | |
download | latinime-490fa47a46015f2c8bd8f9010d236bfe5aecd4bb.tar.gz latinime-490fa47a46015f2c8bd8f9010d236bfe5aecd4bb.tar.xz latinime-490fa47a46015f2c8bd8f9010d236bfe5aecd4bb.zip |
Always specify non-null Locale object to SuggestionSpan
Confusingly, specifying a null Locale object to the constructor
of SuggestionSpan does not necessarily mean that
SuggestionSpan#getLocale() returns null. The constructor in
question also receives Context object, and Context's locale can
be used as a fallback locale to initialize locale of
SuggestionSpan.
With this CL, LatinIME always specify non-null Locale object
when instantiating SuggestionSpan object. It basically
corresponds to the active main dictionary, but can be
Locale#ROOT when one locale is not determined for some reasons.
BUG: 20435013
Change-Id: I2c152466410327300e7dba4d7ed9a22f57c17c4f
Diffstat (limited to 'java/src')
-rw-r--r-- | java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java | 11 | ||||
-rw-r--r-- | java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java | 18 |
2 files changed, 21 insertions, 8 deletions
diff --git a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java index 4d2925d30..3f621913c 100644 --- a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java +++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java @@ -33,6 +33,7 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Locale; +import javax.annotation.Nonnull; import javax.annotation.Nullable; public final class SuggestionSpanUtils { @@ -57,13 +58,12 @@ public final class SuggestionSpanUtils { @UsedForTesting public static CharSequence getTextWithAutoCorrectionIndicatorUnderline( - final Context context, final String text) { + final Context context, final String text, @Nonnull final Locale locale) { if (TextUtils.isEmpty(text) || OBJ_FLAG_AUTO_CORRECTION == null) { return text; } final Spannable spannable = new SpannableString(text); - // TODO: Set locale if it is feasible. - final SuggestionSpan suggestionSpan = new SuggestionSpan(context, null /* locale */, + final SuggestionSpan suggestionSpan = new SuggestionSpan(context, locale, new String[] {} /* suggestions */, OBJ_FLAG_AUTO_CORRECTION, null); spannable.setSpan(suggestionSpan, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); @@ -72,7 +72,7 @@ public final class SuggestionSpanUtils { @UsedForTesting public static CharSequence getTextWithSuggestionSpan(final Context context, - final String pickedWord, final SuggestedWords suggestedWords) { + final String pickedWord, final SuggestedWords suggestedWords, final Locale locale) { if (TextUtils.isEmpty(pickedWord) || suggestedWords.isEmpty() || suggestedWords.isPrediction() || suggestedWords.isPunctuationSuggestions()) { return pickedWord; @@ -92,8 +92,7 @@ public final class SuggestionSpanUtils { suggestionsList.add(word.toString()); } } - // TODO: Set locale if it is feasible. - final SuggestionSpan suggestionSpan = new SuggestionSpan(context, null /* locale */, + final SuggestionSpan suggestionSpan = new SuggestionSpan(context, locale, suggestionsList.toArray(new String[suggestionsList.size()]), 0 /* flags */, null); final Spannable spannable = new SpannableString(pickedWord); spannable.setSpan(suggestionSpan, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java index 324ae3a19..f7dbc0a4d 100644 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java +++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java @@ -60,6 +60,7 @@ import com.android.inputmethod.latin.utils.StatsUtils; import com.android.inputmethod.latin.utils.TextRange; import java.util.ArrayList; +import java.util.Locale; import java.util.TreeSet; import java.util.concurrent.TimeUnit; @@ -1903,6 +1904,15 @@ public final class InputLogic { } /** + * @return the {@link Locale} of the {@link #mDictionaryFacilitator} if available. Otherwise + * {@link Locale#ROOT}. + */ + @Nonnull + private Locale getDictionaryFacilitatorLocale() { + return mDictionaryFacilitator != null ? mDictionaryFacilitator.getLocale() : Locale.ROOT; + } + + /** * Gets a chunk of text with or the auto-correction indicator underline span as appropriate. * * This method looks at the old state of the auto-correction indicator to put or not put @@ -1921,8 +1931,10 @@ public final class InputLogic { */ // TODO: Shouldn't this go in some *Utils class instead? private CharSequence getTextWithUnderline(final String text) { + // TODO: Locale should be determined based on context and the text given. return mIsAutoCorrectionIndicatorOn - ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(mLatinIME, text) + ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline( + mLatinIME, text, getDictionaryFacilitatorLocale()) : text; } @@ -2122,9 +2134,11 @@ public final class InputLogic { Log.d(TAG, "commitChosenWord() : [" + chosenWord + "]"); } final SuggestedWords suggestedWords = mSuggestedWords; + // TODO: Locale should be determined based on context and the text given. + final Locale locale = getDictionaryFacilitatorLocale(); final CharSequence chosenWordWithSuggestions = SuggestionSpanUtils.getTextWithSuggestionSpan(mLatinIME, chosenWord, - suggestedWords); + suggestedWords, locale); if (DebugFlags.DEBUG_ENABLED) { long runTimeMillis = System.currentTimeMillis() - startTimeMillis; Log.d(TAG, "commitChosenWord() : " + runTimeMillis + " ms to run " |