Use "languageTag" in RichInputMethodSubtype.

With this CL, RichInputMethodSubtype#getLocale() starts returning
a Locale object that is initialized with "languageTag" when it is
specified.  No behavior change is intended when "languageTag" attribute
is not available or specified.

Bug: 22858221
Change-Id: I23f2e479b8e284ce589c6950b071ba84c5dd8ce1
This commit is contained in:
Yohei Yukawa 2016-01-21 23:50:32 -08:00
parent f6997344e6
commit 995ce45572
2 changed files with 19 additions and 1 deletions

View file

@ -17,14 +17,17 @@
package com.android.inputmethod.compat;
import android.os.Build;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.RichInputMethodSubtype;
import com.android.inputmethod.latin.common.Constants;
import com.android.inputmethod.latin.common.LocaleUtils;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Locale;
import javax.annotation.Nonnull;
@ -78,6 +81,21 @@ public final class InputMethodSubtypeCompatUtils {
|| subtype.containsExtraValueKey(Constants.Subtype.ExtraValue.ASCII_CAPABLE);
}
// Note that InputMethodSubtype.getLanguageTag() is expected to be available in Android N+.
private static final Method GET_LANGUAGE_TAG =
CompatUtils.getMethod(InputMethodSubtype.class, "getLanguageTag");
public static Locale getLocaleObject(final InputMethodSubtype subtype) {
// Locale.forLanguageTag() is available only in Android L and later.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final String languageTag = (String) CompatUtils.invoke(subtype, null, GET_LANGUAGE_TAG);
if (!TextUtils.isEmpty(languageTag)) {
return Locale.forLanguageTag(languageTag);
}
}
return LocaleUtils.constructLocaleFromString(subtype.getLocale());
}
@UsedForTesting
public static boolean isAsciiCapableWithAPI(final InputMethodSubtype subtype) {
return (Boolean)CompatUtils.invoke(subtype, false, METHOD_isAsciiCapable);

View file

@ -47,7 +47,7 @@ public class RichInputMethodSubtype {
public RichInputMethodSubtype(@Nonnull final InputMethodSubtype subtype) {
mSubtype = subtype;
mLocale = LocaleUtils.constructLocaleFromString(mSubtype.getLocale());
mLocale = InputMethodSubtypeCompatUtils.getLocaleObject(mSubtype);
}
// Extra values are determined by the primary subtype. This is probably right, but