EditorInfoCompatUtils.java revision fde7efd87710dcc9e8376e3ef6db287e254c65fc
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
17package com.android.inputmethod.compat;
18
19import android.view.inputmethod.EditorInfo;
20
21import java.lang.reflect.Field;
22
23public class EditorInfoCompatUtils {
24    // EditorInfo.IME_FLAG_FORCE_ASCII has been introduced since API#16 (JellyBean).
25    private static final Field FIELD_IME_FLAG_FORCE_ASCII = CompatUtils.getField(
26            EditorInfo.class, "IME_FLAG_FORCE_ASCII");
27    private static final Integer OBJ_IME_FLAG_FORCE_ASCII = (Integer) CompatUtils
28            .getFieldValue(null, null, FIELD_IME_FLAG_FORCE_ASCII);
29
30    private EditorInfoCompatUtils() {
31        // This utility class is not publicly instantiable.
32    }
33
34    public static boolean hasFlagForceAscii(int imeOptions) {
35        if (OBJ_IME_FLAG_FORCE_ASCII == null)
36            return false;
37        return (imeOptions & OBJ_IME_FLAG_FORCE_ASCII) != 0;
38    }
39
40    public static String imeActionName(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
64    public static String imeOptionsName(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