1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.compat; 18 19 import android.os.Build; 20 import android.text.TextUtils; 21 import android.view.inputmethod.InputMethodSubtype; 22 23 import com.android.inputmethod.annotations.UsedForTesting; 24 import com.android.inputmethod.latin.RichInputMethodSubtype; 25 import com.android.inputmethod.latin.common.Constants; 26 import com.android.inputmethod.latin.common.LocaleUtils; 27 28 import java.lang.reflect.Constructor; 29 import java.lang.reflect.Method; 30 import java.util.Locale; 31 32 import javax.annotation.Nonnull; 33 34 public final class InputMethodSubtypeCompatUtils { 35 private static final String TAG = InputMethodSubtypeCompatUtils.class.getSimpleName(); 36 // Note that InputMethodSubtype(int nameId, int iconId, String locale, String mode, 37 // String extraValue, boolean isAuxiliary, boolean overridesImplicitlyEnabledSubtype, int id) 38 // has been introduced in API level 17 (Build.VERSION_CODE.JELLY_BEAN_MR1). 39 private static final Constructor<?> CONSTRUCTOR_INPUT_METHOD_SUBTYPE = 40 CompatUtils.getConstructor(InputMethodSubtype.class, 41 int.class, int.class, String.class, String.class, String.class, boolean.class, 42 boolean.class, int.class); 43 static { 44 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 45 if (CONSTRUCTOR_INPUT_METHOD_SUBTYPE == null) { 46 android.util.Log.w(TAG, "Warning!!! Constructor is not defined."); 47 } 48 } 49 } 50 51 // Note that {@link InputMethodSubtype#isAsciiCapable()} has been introduced in API level 19 52 // (Build.VERSION_CODE.KITKAT). 53 private static final Method METHOD_isAsciiCapable = CompatUtils.getMethod( 54 InputMethodSubtype.class, "isAsciiCapable"); 55 56 private InputMethodSubtypeCompatUtils() { 57 // This utility class is not publicly instantiable. 58 } 59 60 @SuppressWarnings("deprecation") 61 @Nonnull 62 public static InputMethodSubtype newInputMethodSubtype(int nameId, int iconId, String locale, 63 String mode, String extraValue, boolean isAuxiliary, 64 boolean overridesImplicitlyEnabledSubtype, int id) { 65 if (CONSTRUCTOR_INPUT_METHOD_SUBTYPE == null 66 || Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { 67 return new InputMethodSubtype(nameId, iconId, locale, mode, extraValue, isAuxiliary, 68 overridesImplicitlyEnabledSubtype); 69 } 70 return (InputMethodSubtype) CompatUtils.newInstance(CONSTRUCTOR_INPUT_METHOD_SUBTYPE, 71 nameId, iconId, locale, mode, extraValue, isAuxiliary, 72 overridesImplicitlyEnabledSubtype, id); 73 } 74 75 public static boolean isAsciiCapable(final RichInputMethodSubtype subtype) { 76 return isAsciiCapable(subtype.getRawSubtype()); 77 } 78 79 public static boolean isAsciiCapable(final InputMethodSubtype subtype) { 80 return isAsciiCapableWithAPI(subtype) 81 || subtype.containsExtraValueKey(Constants.Subtype.ExtraValue.ASCII_CAPABLE); 82 } 83 84 // Note that InputMethodSubtype.getLanguageTag() is expected to be available in Android N+. 85 private static final Method GET_LANGUAGE_TAG = 86 CompatUtils.getMethod(InputMethodSubtype.class, "getLanguageTag"); 87 88 public static Locale getLocaleObject(final InputMethodSubtype subtype) { 89 // Locale.forLanguageTag() is available only in Android L and later. 90 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 91 final String languageTag = (String) CompatUtils.invoke(subtype, null, GET_LANGUAGE_TAG); 92 if (!TextUtils.isEmpty(languageTag)) { 93 return Locale.forLanguageTag(languageTag); 94 } 95 } 96 return LocaleUtils.constructLocaleFromString(subtype.getLocale()); 97 } 98 99 @UsedForTesting 100 public static boolean isAsciiCapableWithAPI(final InputMethodSubtype subtype) { 101 return (Boolean)CompatUtils.invoke(subtype, false, METHOD_isAsciiCapable); 102 } 103 } 104