From 03c1e7955abf089f3bf5027d506336c07d970180 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Fri, 3 Oct 2014 23:06:47 +0900 Subject: [PATCH] Add a utility method to SuggestionSpanUtils This is a ground work for subsequent CLs. BUG: 16995261 Change-Id: Icbb708a35412a635a0d625b520069840c0bb0904 --- .../compat/SuggestionSpanUtils.java | 28 +++++++++++++++++++ .../compat/SuggestionSpanUtilsTest.java | 26 +++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java index 27db9b8e8..d3e24e342 100644 --- a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java +++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java @@ -28,9 +28,13 @@ import com.android.inputmethod.latin.SuggestedWords; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver; import com.android.inputmethod.latin.define.DebugFlags; +import com.android.inputmethod.latin.utils.LocaleUtils; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Locale; + +import javax.annotation.Nullable; public final class SuggestionSpanUtils { // Note that SuggestionSpan.FLAG_AUTO_CORRECTION has been introduced @@ -98,4 +102,28 @@ public final class SuggestionSpanUtils { spannable.setSpan(suggestionSpan, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return spannable; } + + /** + * Returns first {@link Locale} found in the given array of {@link SuggestionSpan}. + * @param suggestionSpans the array of {@link SuggestionSpan} to be examined. + * @return the first {@link Locale} found in {@code suggestionSpans}. {@code null} when not + * found. + */ + @UsedForTesting + @Nullable + public static Locale findFirstLocaleFromSuggestionSpans( + final SuggestionSpan[] suggestionSpans) { + for (final SuggestionSpan suggestionSpan : suggestionSpans) { + final String localeString = suggestionSpan.getLocale(); + if (TextUtils.isEmpty(localeString)) { + continue; + } + final Locale locale = LocaleUtils.constructLocaleFromString(localeString); + if (locale == null) { + continue; + } + return locale; + } + return null; + } } diff --git a/tests/src/com/android/inputmethod/compat/SuggestionSpanUtilsTest.java b/tests/src/com/android/inputmethod/compat/SuggestionSpanUtilsTest.java index a6b3af4c2..1c320db1c 100644 --- a/tests/src/com/android/inputmethod/compat/SuggestionSpanUtilsTest.java +++ b/tests/src/com/android/inputmethod/compat/SuggestionSpanUtilsTest.java @@ -178,4 +178,30 @@ public class SuggestionSpanUtilsTest extends AndroidTestCase { typedAndCollectedWords)); } } + + public void testFindFirstLocaleFromSuggestionSpans() { + final String[] suggestions = new String[] {"Quality", "Speed", "Price"}; + final SuggestionSpan nullLocaleSpan = new SuggestionSpan((Locale)null, suggestions, 0); + final SuggestionSpan emptyLocaleSpan = new SuggestionSpan(new Locale(""), suggestions, 0); + final SuggestionSpan enUsLocaleSpan = new SuggestionSpan(Locale.US, suggestions, 0); + final SuggestionSpan jaJpLocaleSpan = new SuggestionSpan(Locale.JAPAN, suggestions, 0); + + assertEquals(null, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans( + new SuggestionSpan[] {})); + + assertEquals(null, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans( + new SuggestionSpan[] {emptyLocaleSpan})); + + assertEquals(Locale.US, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans( + new SuggestionSpan[] {enUsLocaleSpan})); + + assertEquals(Locale.US, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans( + new SuggestionSpan[] {nullLocaleSpan, enUsLocaleSpan})); + + assertEquals(Locale.US, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans( + new SuggestionSpan[] {nullLocaleSpan, emptyLocaleSpan, enUsLocaleSpan})); + + assertEquals(Locale.JAPAN, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans( + new SuggestionSpan[] {nullLocaleSpan, jaJpLocaleSpan, enUsLocaleSpan})); + } }