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 
23 public final class EditorInfoCompatUtils {
24     // Note that EditorInfo.IME_FLAG_FORCE_ASCII has been introduced
25     // in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
26     private static final Field FIELD_IME_FLAG_FORCE_ASCII = CompatUtils.getField(
27             EditorInfo.class, "IME_FLAG_FORCE_ASCII");
28     private static final Integer OBJ_IME_FLAG_FORCE_ASCII = (Integer) CompatUtils.getFieldValue(
29             null /* receiver */, null /* defaultValue */, FIELD_IME_FLAG_FORCE_ASCII);
30 
EditorInfoCompatUtils()31     private EditorInfoCompatUtils() {
32         // This utility class is not publicly instantiable.
33     }
34 
hasFlagForceAscii(final int imeOptions)35     public static boolean hasFlagForceAscii(final int imeOptions) {
36         if (OBJ_IME_FLAG_FORCE_ASCII == null) return false;
37         return (imeOptions & OBJ_IME_FLAG_FORCE_ASCII) != 0;
38     }
39 
imeActionName(final int imeOptions)40     public static String imeActionName(final int imeOptions) {
41         final int actionId = imeOptions & EditorInfo.IME_MASK_ACTION;
42         switch (actionId) {
43         case EditorInfo.IME_ACTION_UNSPECIFIED:
44             return "actionUnspecified";
45         case EditorInfo.IME_ACTION_NONE:
46             return "actionNone";
47         case EditorInfo.IME_ACTION_GO:
48             return "actionGo";
49         case EditorInfo.IME_ACTION_SEARCH:
50             return "actionSearch";
51         case EditorInfo.IME_ACTION_SEND:
52             return "actionSend";
53         case EditorInfo.IME_ACTION_NEXT:
54             return "actionNext";
55         case EditorInfo.IME_ACTION_DONE:
56             return "actionDone";
57         case EditorInfo.IME_ACTION_PREVIOUS:
58             return "actionPrevious";
59         default:
60             return "actionUnknown(" + actionId + ")";
61         }
62     }
63 
imeOptionsName(final int imeOptions)64     public static String imeOptionsName(final int imeOptions) {
65         final String action = imeActionName(imeOptions);
66         final StringBuilder flags = new StringBuilder();
67         if ((imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
68             flags.append("flagNoEnterAction|");
69         }
70         if ((imeOptions & EditorInfo.IME_FLAG_NAVIGATE_NEXT) != 0) {
71             flags.append("flagNavigateNext|");
72         }
73         if ((imeOptions & EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS) != 0) {
74             flags.append("flagNavigatePrevious|");
75         }
76         if (hasFlagForceAscii(imeOptions)) {
77             flags.append("flagForceAscii|");
78         }
79         return (action != null) ? flags + action : flags.toString();
80     }
81 }
82