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;
20import android.view.inputmethod.InputConnection;
21
22import java.lang.reflect.Field;
23
24public class EditorInfoCompatUtils {
25    private static final Field FIELD_IME_FLAG_NAVIGATE_NEXT = CompatUtils.getField(
26            EditorInfo.class, "IME_FLAG_NAVIGATE_NEXT");
27    private static final Field FIELD_IME_FLAG_NAVIGATE_PREVIOUS = CompatUtils.getField(
28            EditorInfo.class, "IME_FLAG_NAVIGATE_PREVIOUS");
29    private static final Field FIELD_IME_ACTION_PREVIOUS = CompatUtils.getField(
30            EditorInfo.class, "IME_ACTION_PREVIOUS");
31    private static final Integer OBJ_IME_FLAG_NAVIGATE_NEXT = (Integer) CompatUtils
32            .getFieldValue(null, null, FIELD_IME_FLAG_NAVIGATE_NEXT);
33    private static final Integer OBJ_IME_FLAG_NAVIGATE_PREVIOUS = (Integer) CompatUtils
34            .getFieldValue(null, null, FIELD_IME_FLAG_NAVIGATE_PREVIOUS);
35    private static final Integer OBJ_IME_ACTION_PREVIOUS = (Integer) CompatUtils
36            .getFieldValue(null, null, FIELD_IME_ACTION_PREVIOUS);
37
38    public static boolean hasFlagNavigateNext(int imeOptions) {
39        if (OBJ_IME_FLAG_NAVIGATE_NEXT == null)
40            return false;
41        return (imeOptions & OBJ_IME_FLAG_NAVIGATE_NEXT) != 0;
42    }
43
44    public static boolean hasFlagNavigatePrevious(int imeOptions) {
45        if (OBJ_IME_FLAG_NAVIGATE_PREVIOUS == null)
46            return false;
47        return (imeOptions & OBJ_IME_FLAG_NAVIGATE_PREVIOUS) != 0;
48    }
49
50    public static void performEditorActionNext(InputConnection ic) {
51        ic.performEditorAction(EditorInfo.IME_ACTION_NEXT);
52    }
53
54    public static void performEditorActionPrevious(InputConnection ic) {
55        if (OBJ_IME_ACTION_PREVIOUS == null)
56            return;
57        ic.performEditorAction(OBJ_IME_ACTION_PREVIOUS);
58    }
59
60    public static String imeOptionsName(int imeOptions) {
61        if (imeOptions == -1)
62            return null;
63        final int actionId = imeOptions & EditorInfo.IME_MASK_ACTION;
64        final String action;
65        switch (actionId) {
66            case EditorInfo.IME_ACTION_UNSPECIFIED:
67                action = "actionUnspecified";
68                break;
69            case EditorInfo.IME_ACTION_NONE:
70                action = "actionNone";
71                break;
72            case EditorInfo.IME_ACTION_GO:
73                action = "actionGo";
74                break;
75            case EditorInfo.IME_ACTION_SEARCH:
76                action = "actionSearch";
77                break;
78            case EditorInfo.IME_ACTION_SEND:
79                action = "actionSend";
80                break;
81            case EditorInfo.IME_ACTION_NEXT:
82                action = "actionNext";
83                break;
84            case EditorInfo.IME_ACTION_DONE:
85                action = "actionDone";
86                break;
87            default: {
88                if (OBJ_IME_ACTION_PREVIOUS != null && actionId == OBJ_IME_ACTION_PREVIOUS) {
89                    action = "actionPrevious";
90                } else {
91                    action = "actionUnknown(" + actionId + ")";
92                }
93                break;
94            }
95        }
96        if ((imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
97            return "flagNoEnterAction|" + action;
98        } else {
99            return action;
100        }
101    }
102}
103