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.view.inputmethod.InputMethodSubtype;
21 
22 import com.android.inputmethod.annotations.UsedForTesting;
23 import com.android.inputmethod.latin.Constants;
24 
25 import java.lang.reflect.Constructor;
26 import java.lang.reflect.Method;
27 
28 public final class InputMethodSubtypeCompatUtils {
29     private static final String TAG = InputMethodSubtypeCompatUtils.class.getSimpleName();
30     // Note that InputMethodSubtype(int nameId, int iconId, String locale, String mode,
31     // String extraValue, boolean isAuxiliary, boolean overridesImplicitlyEnabledSubtype, int id)
32     // has been introduced in API level 17 (Build.VERSION_CODE.JELLY_BEAN_MR1).
33     private static final Constructor<?> CONSTRUCTOR_INPUT_METHOD_SUBTYPE =
34             CompatUtils.getConstructor(InputMethodSubtype.class,
35                     int.class, int.class, String.class, String.class, String.class, boolean.class,
36                     boolean.class, int.class);
37     static {
38         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
39             if (CONSTRUCTOR_INPUT_METHOD_SUBTYPE == null) {
android.util.Log.w(TAG, "Warning!!! Constructor is not defined.")40                 android.util.Log.w(TAG, "Warning!!! Constructor is not defined.");
41             }
42         }
43     }
44 
45     // Note that {@link InputMethodSubtype#isAsciiCapable()} has been introduced in API level 19
46     // (Build.VERSION_CODE.KITKAT).
47     private static final Method METHOD_isAsciiCapable = CompatUtils.getMethod(
48             InputMethodSubtype.class, "isAsciiCapable");
49 
InputMethodSubtypeCompatUtils()50     private InputMethodSubtypeCompatUtils() {
51         // This utility class is not publicly instantiable.
52     }
53 
newInputMethodSubtype(int nameId, int iconId, String locale, String mode, String extraValue, boolean isAuxiliary, boolean overridesImplicitlyEnabledSubtype, int id)54     public static InputMethodSubtype newInputMethodSubtype(int nameId, int iconId, String locale,
55             String mode, String extraValue, boolean isAuxiliary,
56             boolean overridesImplicitlyEnabledSubtype, int id) {
57         if (CONSTRUCTOR_INPUT_METHOD_SUBTYPE == null
58                 || Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
59             return new InputMethodSubtype(nameId, iconId, locale, mode, extraValue, isAuxiliary,
60                     overridesImplicitlyEnabledSubtype);
61         }
62         return (InputMethodSubtype) CompatUtils.newInstance(CONSTRUCTOR_INPUT_METHOD_SUBTYPE,
63                 nameId, iconId, locale, mode, extraValue, isAuxiliary,
64                 overridesImplicitlyEnabledSubtype, id);
65     }
66 
isAsciiCapable(final InputMethodSubtype subtype)67     public static boolean isAsciiCapable(final InputMethodSubtype subtype) {
68         return isAsciiCapableWithAPI(subtype)
69                 || subtype.containsExtraValueKey(Constants.Subtype.ExtraValue.ASCII_CAPABLE);
70     }
71 
72     @UsedForTesting
isAsciiCapableWithAPI(final InputMethodSubtype subtype)73     public static boolean isAsciiCapableWithAPI(final InputMethodSubtype subtype) {
74         return (Boolean)CompatUtils.invoke(subtype, false, METHOD_isAsciiCapable);
75     }
76 }
77