KeyboardId.java revision c3d175c01ff1956ddb1c2d608d69af1793b4ad8a
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.keyboard;
18
19import android.view.inputmethod.EditorInfo;
20
21import com.android.inputmethod.compat.EditorInfoCompatUtils;
22import com.android.inputmethod.compat.InputTypeCompatUtils;
23import com.android.inputmethod.latin.R;
24
25import java.util.Arrays;
26import java.util.Locale;
27
28/**
29 * Represents the parameters necessary to construct a new LatinKeyboard,
30 * which also serve as a unique identifier for each keyboard type.
31 */
32public class KeyboardId {
33    public static final int MODE_TEXT = 0;
34    public static final int MODE_URL = 1;
35    public static final int MODE_EMAIL = 2;
36    public static final int MODE_IM = 3;
37    public static final int MODE_PHONE = 4;
38    public static final int MODE_NUMBER = 5;
39
40    public static final int F2KEY_MODE_NONE = 0;
41    public static final int F2KEY_MODE_SETTINGS = 1;
42    public static final int F2KEY_MODE_SHORTCUT_IME = 2;
43    public static final int F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS = 3;
44
45    private static final int MINI_KEYBOARD_ID_MARKER = -1;
46
47    public final Locale mLocale;
48    public final int mOrientation;
49    public final int mWidth;
50    public final int mMode;
51    public final int mXmlId;
52    public final boolean mNavigateAction;
53    public final boolean mPasswordInput;
54    // TODO: Clean up these booleans and modes.
55    public final boolean mHasSettingsKey;
56    public final int mF2KeyMode;
57    public final boolean mClobberSettingsKey;
58    public final boolean mVoiceKeyEnabled;
59    public final boolean mHasVoiceKey;
60    public final int mImeAction;
61    public final boolean mEnableShiftLock;
62
63    public final String mXmlName;
64    public final EditorInfo mAttribute;
65
66    private final int mHashCode;
67
68    public KeyboardId(String xmlName, int xmlId, Locale locale, int orientation, int width,
69            int mode, EditorInfo attribute, boolean hasSettingsKey, int f2KeyMode,
70            boolean clobberSettingsKey, boolean voiceKeyEnabled, boolean hasVoiceKey,
71            boolean enableShiftLock) {
72        final int inputType = (attribute != null) ? attribute.inputType : 0;
73        final int imeOptions = (attribute != null) ? attribute.imeOptions : 0;
74        this.mLocale = locale;
75        this.mOrientation = orientation;
76        this.mWidth = width;
77        this.mMode = mode;
78        this.mXmlId = xmlId;
79        // Note: Turn off checking navigation flag to show TAB key for now.
80        this.mNavigateAction = InputTypeCompatUtils.isWebInputType(inputType);
81//                || EditorInfoCompatUtils.hasFlagNavigateNext(imeOptions)
82//                || EditorInfoCompatUtils.hasFlagNavigatePrevious(imeOptions);
83        this.mPasswordInput = InputTypeCompatUtils.isPasswordInputType(inputType)
84                || InputTypeCompatUtils.isVisiblePasswordInputType(inputType);
85        this.mHasSettingsKey = hasSettingsKey;
86        this.mF2KeyMode = f2KeyMode;
87        this.mClobberSettingsKey = clobberSettingsKey;
88        this.mVoiceKeyEnabled = voiceKeyEnabled;
89        this.mHasVoiceKey = hasVoiceKey;
90        // We are interested only in {@link EditorInfo#IME_MASK_ACTION} enum value and
91        // {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION}.
92        this.mImeAction = imeOptions & (
93                EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
94        this.mEnableShiftLock = enableShiftLock;
95
96        this.mXmlName = xmlName;
97        this.mAttribute = attribute;
98
99        this.mHashCode = Arrays.hashCode(new Object[] {
100                locale,
101                orientation,
102                width,
103                mode,
104                xmlId,
105                mNavigateAction,
106                mPasswordInput,
107                hasSettingsKey,
108                f2KeyMode,
109                clobberSettingsKey,
110                voiceKeyEnabled,
111                hasVoiceKey,
112                mImeAction,
113                enableShiftLock,
114        });
115    }
116
117    public KeyboardId cloneAsMiniKeyboard() {
118        return new KeyboardId("mini popup keyboard", MINI_KEYBOARD_ID_MARKER, mLocale, mOrientation,
119                mWidth, mMode, mAttribute, false, F2KEY_MODE_NONE, false, false, false, false);
120    }
121
122    public KeyboardId cloneWithNewGeometry(int orientation, int width) {
123        if (mWidth == width)
124            return this;
125        return new KeyboardId(mXmlName, mXmlId, mLocale, orientation, width, mMode, mAttribute,
126                mHasSettingsKey, mF2KeyMode, mClobberSettingsKey, mVoiceKeyEnabled, mHasVoiceKey,
127                mEnableShiftLock);
128    }
129
130    public int getXmlId() {
131        return mXmlId;
132    }
133
134    public boolean isMiniKeyboard() {
135        return mXmlId == MINI_KEYBOARD_ID_MARKER;
136    }
137
138    public boolean isAlphabetKeyboard() {
139        return mXmlId == R.xml.kbd_qwerty;
140    }
141
142    public boolean isSymbolsKeyboard() {
143        return mXmlId == R.xml.kbd_symbols || mXmlId == R.xml.kbd_symbols_shift;
144    }
145
146    public boolean isPhoneKeyboard() {
147        return mMode == MODE_PHONE;
148    }
149
150    public boolean isPhoneShiftKeyboard() {
151        return mXmlId == R.xml.kbd_phone_shift;
152    }
153
154    public boolean isNumberKeyboard() {
155        return mMode == MODE_NUMBER;
156    }
157
158    @Override
159    public boolean equals(Object other) {
160        return other instanceof KeyboardId && equals((KeyboardId) other);
161    }
162
163    private boolean equals(KeyboardId other) {
164        return other.mLocale.equals(this.mLocale)
165            && other.mOrientation == this.mOrientation
166            && other.mWidth == this.mWidth
167            && other.mMode == this.mMode
168            && other.mXmlId == this.mXmlId
169            && other.mNavigateAction == this.mNavigateAction
170            && other.mPasswordInput == this.mPasswordInput
171            && other.mHasSettingsKey == this.mHasSettingsKey
172            && other.mF2KeyMode == this.mF2KeyMode
173            && other.mClobberSettingsKey == this.mClobberSettingsKey
174            && other.mVoiceKeyEnabled == this.mVoiceKeyEnabled
175            && other.mHasVoiceKey == this.mHasVoiceKey
176            && other.mImeAction == this.mImeAction
177            && other.mEnableShiftLock == this.mEnableShiftLock;
178    }
179
180    @Override
181    public int hashCode() {
182        return mHashCode;
183    }
184
185    @Override
186    public String toString() {
187        return String.format("[%s.xml %s %s%d %s %s %s%s%s%s%s%s%s%s]",
188                mXmlName,
189                mLocale,
190                (mOrientation == 1 ? "port" : "land"), mWidth,
191                modeName(mMode),
192                EditorInfoCompatUtils.imeOptionsName(mImeAction),
193                f2KeyModeName(mF2KeyMode),
194                (mClobberSettingsKey ? " clobberSettingsKey" : ""),
195                (mNavigateAction ? " navigateAction" : ""),
196                (mPasswordInput ? " passwordInput" : ""),
197                (mHasSettingsKey ? " hasSettingsKey" : ""),
198                (mVoiceKeyEnabled ? " voiceKeyEnabled" : ""),
199                (mHasVoiceKey ? " hasVoiceKey" : ""),
200                (mEnableShiftLock ? " enableShiftLock" : "")
201        );
202    }
203
204    public static String modeName(int mode) {
205        switch (mode) {
206        case MODE_TEXT: return "text";
207        case MODE_URL: return "url";
208        case MODE_EMAIL: return "email";
209        case MODE_IM: return "im";
210        case MODE_PHONE: return "phone";
211        case MODE_NUMBER: return "number";
212        default: return null;
213        }
214    }
215
216    public static String f2KeyModeName(int f2KeyMode) {
217        switch (f2KeyMode) {
218        case F2KEY_MODE_NONE: return "none";
219        case F2KEY_MODE_SETTINGS: return "settings";
220        case F2KEY_MODE_SHORTCUT_IME: return "shortcutIme";
221        case F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS: return "shortcutImeOrSettings";
222        default: return null;
223        }
224    }
225}
226