From c0c74d22a020002bcd33deae1990d588e5c07280 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Mon, 27 Jan 2014 17:22:13 +0900 Subject: [PATCH] Add SubtypeLocaleUtils.isRtlLanguage method Change-Id: I2e399ae9ca111638b583c5681de08b0e6db86e3a --- .../latin/utils/SubtypeLocaleUtils.java | 21 ++++++++++++++++ .../latin/utils/SubtypeLocaleUtilsTests.java | 24 ++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java b/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java index fdbe81ab6..fb1be30f1 100644 --- a/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java @@ -25,9 +25,11 @@ import android.os.Build; import android.util.Log; import android.view.inputmethod.InputMethodSubtype; +import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.latin.DictionaryFactory; import com.android.inputmethod.latin.R; +import java.util.Arrays; import java.util.HashMap; import java.util.Locale; @@ -334,4 +336,23 @@ public final class SubtypeLocaleUtils { final Locale locale = getSubtypeLocale(subtype); return StringUtils.capitalizeFirstCodePoint(locale.getLanguage(), locale); } + + // TODO: Get this information from the framework instead of maintaining here by ourselves. + // Sorted list of known Right-To-Left language codes. + private static final String[] SORTED_RTL_LANGUAGES = { + "ar", // Arabic + "fa", // Persian + "iw", // Hebrew + }; + static { + Arrays.sort(SORTED_RTL_LANGUAGES); + } + + // TODO: Remove @UsedForTesting annotation. + @UsedForTesting + public static boolean isRtlLanguage(final InputMethodSubtype subtype) { + final Locale locale = getSubtypeLocale(subtype); + final String language = locale.getLanguage(); + return Arrays.binarySearch(SORTED_RTL_LANGUAGES, language) >= 0; + } } diff --git a/tests/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtilsTests.java b/tests/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtilsTests.java index 939dedba1..25f57eba6 100644 --- a/tests/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtilsTests.java @@ -101,7 +101,6 @@ public class SubtypeLocaleUtilsTests extends AndroidTestCase { SubtypeLocaleUtils.NO_LANGUAGE, "azerty", null); ZZ_PC = AdditionalSubtypeUtils.createAdditionalSubtype( SubtypeLocaleUtils.NO_LANGUAGE, "pcqwerty", null); - } public void testAllFullDisplayName() { @@ -423,4 +422,27 @@ public class SubtypeLocaleUtilsTests extends AndroidTestCase { public void testAdditionalSubtypeForSpacebarInFrench() { testsAdditionalSubtypesForSpacebar.runInLocale(mRes, Locale.FRENCH); } + + public void testIsRtlLanguage() { + // Known Right-to-Left language subtypes. + final InputMethodSubtype ARABIC = mRichImm + .findSubtypeByLocaleAndKeyboardLayoutSet("ar", "arabic"); + assertNotNull("Arabic", ARABIC); + final InputMethodSubtype FARSI = mRichImm + .findSubtypeByLocaleAndKeyboardLayoutSet("fa", "farsi"); + assertNotNull("Farsi", FARSI); + final InputMethodSubtype HEBREW = mRichImm + .findSubtypeByLocaleAndKeyboardLayoutSet("iw", "hebrew"); + assertNotNull("Hebrew", HEBREW); + + for (final InputMethodSubtype subtype : mSubtypesList) { + final String subtypeName = SubtypeLocaleUtils + .getSubtypeDisplayNameInSystemLocale(subtype); + if (subtype.equals(ARABIC) || subtype.equals(FARSI) || subtype.equals(HEBREW)) { + assertTrue(subtypeName, SubtypeLocaleUtils.isRtlLanguage(subtype)); + } else { + assertFalse(subtypeName, SubtypeLocaleUtils.isRtlLanguage(subtype)); + } + } + } }