Loading core/java/android/view/inputmethod/InputMethodSubtype.java +16 −4 Original line number Diff line number Diff line Loading @@ -84,6 +84,8 @@ public final class InputMethodSubtype implements Parcelable { private final String mSubtypeLanguageTag; private final String mSubtypeMode; private final String mSubtypeExtraValue; private final Object mLock = new Object(); private volatile Locale mCachedLocaleObj; private volatile HashMap<String, String> mExtraValueHashMapCache; /** Loading Loading @@ -372,10 +374,20 @@ public final class InputMethodSubtype implements Parcelable { */ @Nullable public Locale getLocaleObject() { if (mCachedLocaleObj != null) { return mCachedLocaleObj; } synchronized (mLock) { if (mCachedLocaleObj != null) { return mCachedLocaleObj; } if (!TextUtils.isEmpty(mSubtypeLanguageTag)) { return Locale.forLanguageTag(mSubtypeLanguageTag); mCachedLocaleObj = Locale.forLanguageTag(mSubtypeLanguageTag); } else { mCachedLocaleObj = InputMethodUtils.constructLocaleFromString(mSubtypeLocale); } return mCachedLocaleObj; } return InputMethodUtils.constructLocaleFromString(mSubtypeLocale); } /** Loading core/tests/coretests/src/android/view/inputmethod/InputMethodSubtypeTest.java +45 −2 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ import android.test.InstrumentationTestCase; import android.test.suitebuilder.annotation.SmallTest; import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder; import java.util.Locale; import java.util.Objects; public class InputMethodSubtypeTest extends InstrumentationTestCase { Loading @@ -47,6 +48,38 @@ public class InputMethodSubtypeTest extends InstrumentationTestCase { cloneViaParcel(cloneViaParcel(createDummySubtype(localeString))).hashCode()); } @SmallTest public void testLocaleObj_locale() { final InputMethodSubtype usSubtype = createDummySubtype("en_US"); Locale localeObject = usSubtype.getLocaleObject(); assertEquals("en", localeObject.getLanguage()); assertEquals("US", localeObject.getCountry()); // The locale object should be cached. assertTrue(localeObject == usSubtype.getLocaleObject()); } @SmallTest public void testLocaleObj_languageTag() { final InputMethodSubtype usSubtype = createDummySubtypeUsingLanguageTag("en-US"); Locale localeObject = usSubtype.getLocaleObject(); assertNotNull(localeObject); assertEquals("en", localeObject.getLanguage()); assertEquals("US", localeObject.getCountry()); // The locale object should be cached. assertTrue(localeObject == usSubtype.getLocaleObject()); } @SmallTest public void testLocaleObj_emptyLocale() { final InputMethodSubtype emptyLocaleSubtype = createDummySubtype(""); assertNull(emptyLocaleSubtype.getLocaleObject()); // It should continue returning null when called multiple times. assertNull(emptyLocaleSubtype.getLocaleObject()); assertNull(emptyLocaleSubtype.getLocaleObject()); } @SmallTest public void testLocaleString() throws Exception { // The locale string in InputMethodSubtype has accepted an arbitrary text actually, Loading Loading @@ -94,7 +127,7 @@ public class InputMethodSubtypeTest extends InstrumentationTestCase { } } private static final InputMethodSubtype createDummySubtype(final String locale) { private static InputMethodSubtype createDummySubtype(final String locale) { final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder(); return builder.setSubtypeNameResId(0) .setSubtypeIconResId(0) Loading @@ -102,4 +135,14 @@ public class InputMethodSubtypeTest extends InstrumentationTestCase { .setIsAsciiCapable(true) .build(); } private static InputMethodSubtype createDummySubtypeUsingLanguageTag( final String languageTag) { final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder(); return builder.setSubtypeNameResId(0) .setSubtypeIconResId(0) .setLanguageTag(languageTag) .setIsAsciiCapable(true) .build(); } } No newline at end of file Loading
core/java/android/view/inputmethod/InputMethodSubtype.java +16 −4 Original line number Diff line number Diff line Loading @@ -84,6 +84,8 @@ public final class InputMethodSubtype implements Parcelable { private final String mSubtypeLanguageTag; private final String mSubtypeMode; private final String mSubtypeExtraValue; private final Object mLock = new Object(); private volatile Locale mCachedLocaleObj; private volatile HashMap<String, String> mExtraValueHashMapCache; /** Loading Loading @@ -372,10 +374,20 @@ public final class InputMethodSubtype implements Parcelable { */ @Nullable public Locale getLocaleObject() { if (mCachedLocaleObj != null) { return mCachedLocaleObj; } synchronized (mLock) { if (mCachedLocaleObj != null) { return mCachedLocaleObj; } if (!TextUtils.isEmpty(mSubtypeLanguageTag)) { return Locale.forLanguageTag(mSubtypeLanguageTag); mCachedLocaleObj = Locale.forLanguageTag(mSubtypeLanguageTag); } else { mCachedLocaleObj = InputMethodUtils.constructLocaleFromString(mSubtypeLocale); } return mCachedLocaleObj; } return InputMethodUtils.constructLocaleFromString(mSubtypeLocale); } /** Loading
core/tests/coretests/src/android/view/inputmethod/InputMethodSubtypeTest.java +45 −2 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ import android.test.InstrumentationTestCase; import android.test.suitebuilder.annotation.SmallTest; import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder; import java.util.Locale; import java.util.Objects; public class InputMethodSubtypeTest extends InstrumentationTestCase { Loading @@ -47,6 +48,38 @@ public class InputMethodSubtypeTest extends InstrumentationTestCase { cloneViaParcel(cloneViaParcel(createDummySubtype(localeString))).hashCode()); } @SmallTest public void testLocaleObj_locale() { final InputMethodSubtype usSubtype = createDummySubtype("en_US"); Locale localeObject = usSubtype.getLocaleObject(); assertEquals("en", localeObject.getLanguage()); assertEquals("US", localeObject.getCountry()); // The locale object should be cached. assertTrue(localeObject == usSubtype.getLocaleObject()); } @SmallTest public void testLocaleObj_languageTag() { final InputMethodSubtype usSubtype = createDummySubtypeUsingLanguageTag("en-US"); Locale localeObject = usSubtype.getLocaleObject(); assertNotNull(localeObject); assertEquals("en", localeObject.getLanguage()); assertEquals("US", localeObject.getCountry()); // The locale object should be cached. assertTrue(localeObject == usSubtype.getLocaleObject()); } @SmallTest public void testLocaleObj_emptyLocale() { final InputMethodSubtype emptyLocaleSubtype = createDummySubtype(""); assertNull(emptyLocaleSubtype.getLocaleObject()); // It should continue returning null when called multiple times. assertNull(emptyLocaleSubtype.getLocaleObject()); assertNull(emptyLocaleSubtype.getLocaleObject()); } @SmallTest public void testLocaleString() throws Exception { // The locale string in InputMethodSubtype has accepted an arbitrary text actually, Loading Loading @@ -94,7 +127,7 @@ public class InputMethodSubtypeTest extends InstrumentationTestCase { } } private static final InputMethodSubtype createDummySubtype(final String locale) { private static InputMethodSubtype createDummySubtype(final String locale) { final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder(); return builder.setSubtypeNameResId(0) .setSubtypeIconResId(0) Loading @@ -102,4 +135,14 @@ public class InputMethodSubtypeTest extends InstrumentationTestCase { .setIsAsciiCapable(true) .build(); } private static InputMethodSubtype createDummySubtypeUsingLanguageTag( final String languageTag) { final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder(); return builder.setSubtypeNameResId(0) .setSubtypeIconResId(0) .setLanguageTag(languageTag) .setIsAsciiCapable(true) .build(); } } No newline at end of file