diff --git a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatUtils.java b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatUtils.java index 58ad4bd4c..d123a1799 100644 --- a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatUtils.java @@ -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); diff --git a/java/src/com/android/inputmethod/latin/RichInputMethodSubtype.java b/java/src/com/android/inputmethod/latin/RichInputMethodSubtype.java index 9d7849ffc..71aaf5e01 100644 --- a/java/src/com/android/inputmethod/latin/RichInputMethodSubtype.java +++ b/java/src/com/android/inputmethod/latin/RichInputMethodSubtype.java @@ -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