1 /*
2  * Copyright (C) 2011 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.view.inputmethod.EditorInfo;
20 
21 import java.lang.reflect.Field;
22 import java.util.Locale;
23 
24 public final class EditorInfoCompatUtils {
25     // Note that EditorInfo.IME_FLAG_FORCE_ASCII has been introduced
26     // in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
27     private static final Field FIELD_IME_FLAG_FORCE_ASCII = CompatUtils.getField(
28             EditorInfo.class, "IME_FLAG_FORCE_ASCII");
29     private static final Integer OBJ_IME_FLAG_FORCE_ASCII = (Integer) CompatUtils.getFieldValue(
30             null /* receiver */, null /* defaultValue */, FIELD_IME_FLAG_FORCE_ASCII);
31     private static final Field FIELD_HINT_LOCALES = CompatUtils.getField(
32             EditorInfo.class, "hintLocales");
33 
EditorInfoCompatUtils()34     private EditorInfoCompatUtils() {
35         // This utility class is not publicly instantiable.
36     }
37 
hasFlagForceAscii(final int imeOptions)38     public static boolean hasFlagForceAscii(final int imeOptions) {
39         if (OBJ_IME_FLAG_FORCE_ASCII == null) return false;
40         return (imeOptions & OBJ_IME_FLAG_FORCE_ASCII) != 0;
41     }
42 
imeActionName(final int imeOptions)43     public static String imeActionName(final int imeOptions) {
44         final int actionId = imeOptions & EditorInfo.IME_MASK_ACTION;
45         switch (actionId) {
46         case EditorInfo.IME_ACTION_UNSPECIFIED:
47             return "actionUnspecified";
48         case EditorInfo.IME_ACTION_NONE:
49             return "actionNone";
50         case EditorInfo.IME_ACTION_GO:
51             return "actionGo";
52         case EditorInfo.IME_ACTION_SEARCH:
53             return "actionSearch";
54         case EditorInfo.IME_ACTION_SEND:
55             return "actionSend";
56         case EditorInfo.IME_ACTION_NEXT:
57             return "actionNext";
58         case EditorInfo.IME_ACTION_DONE:
59             return "actionDone";
60         case EditorInfo.IME_ACTION_PREVIOUS:
61             return "actionPrevious";
62         default:
63             return "actionUnknown(" + actionId + ")";
64         }
65     }
66 
imeOptionsName(final int imeOptions)67     public static String imeOptionsName(final int imeOptions) {
68         final String action = imeActionName(imeOptions);
69         final StringBuilder flags = new StringBuilder();
70         if ((imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
71             flags.append("flagNoEnterAction|");
72         }
73         if ((imeOptions & EditorInfo.IME_FLAG_NAVIGATE_NEXT) != 0) {
74             flags.append("flagNavigateNext|");
75         }
76         if ((imeOptions & EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS) != 0) {
77             flags.append("flagNavigatePrevious|");
78         }
79         if (hasFlagForceAscii(imeOptions)) {
80             flags.append("flagForceAscii|");
81         }
82         return (action != null) ? flags + action : flags.toString();
83     }
84 
getPrimaryHintLocale(final EditorInfo editorInfo)85     public static Locale getPrimaryHintLocale(final EditorInfo editorInfo) {
86         if (editorInfo == null) {
87             return null;
88         }
89         final Object localeList = CompatUtils.getFieldValue(editorInfo, null, FIELD_HINT_LOCALES);
90         if (localeList == null) {
91             return null;
92         }
93         if (LocaleListCompatUtils.isEmpty(localeList)) {
94             return null;
95         }
96         return LocaleListCompatUtils.get(localeList, 0);
97     }
98 }
99