Merge "Optimize filterByLanguage by caching locale objects" into oc-mr1-dev
am: cb5a109d17
Change-Id: I3fe8ae0e5ecbeec9aa41ccfc1470dcda93a2d326
This commit is contained in:
@@ -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;
|
||||
|
||||
/**
|
||||
@@ -372,10 +374,20 @@ public final class InputMethodSubtype implements Parcelable {
|
||||
*/
|
||||
@Nullable
|
||||
public Locale getLocaleObject() {
|
||||
if (!TextUtils.isEmpty(mSubtypeLanguageTag)) {
|
||||
return Locale.forLanguageTag(mSubtypeLanguageTag);
|
||||
if (mCachedLocaleObj != null) {
|
||||
return mCachedLocaleObj;
|
||||
}
|
||||
synchronized (mLock) {
|
||||
if (mCachedLocaleObj != null) {
|
||||
return mCachedLocaleObj;
|
||||
}
|
||||
if (!TextUtils.isEmpty(mSubtypeLanguageTag)) {
|
||||
mCachedLocaleObj = Locale.forLanguageTag(mSubtypeLanguageTag);
|
||||
} else {
|
||||
mCachedLocaleObj = InputMethodUtils.constructLocaleFromString(mSubtypeLocale);
|
||||
}
|
||||
return mCachedLocaleObj;
|
||||
}
|
||||
return InputMethodUtils.constructLocaleFromString(mSubtypeLocale);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -681,4 +693,4 @@ public final class InputMethodSubtype implements Parcelable {
|
||||
}
|
||||
return sortedList;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
@@ -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,
|
||||
@@ -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)
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user