KeyboardId.java revision 8da9a13760896cd78235b60d0ea680ea13620532
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    public final Locale mLocale;
46    public final int mOrientation;
47    public final int mWidth;
48    public final int mMode;
49    public final int mXmlId;
50    public final boolean mNavigateAction;
51    public final boolean mPasswordInput;
52    // TODO: Clean up these booleans and modes.
53    public final boolean mHasSettingsKey;
54    public final int mF2KeyMode;
55    public final boolean mClobberSettingsKey;
56    public final boolean mShortcutKeyEnabled;
57    public final boolean mHasShortcutKey;
58    public final int mImeAction;
59
60    public final String mXmlName;
61    public final EditorInfo mAttribute;
62
63    private final int mHashCode;
64
65    public KeyboardId(String xmlName, int xmlId, Locale locale, int orientation, int width,
66            int mode, EditorInfo attribute, boolean hasSettingsKey, int f2KeyMode,
67            boolean clobberSettingsKey, boolean shortcutKeyEnabled, boolean hasShortcutKey) {
68        final int inputType = (attribute != null) ? attribute.inputType : 0;
69        final int imeOptions = (attribute != null) ? attribute.imeOptions : 0;
70        this.mLocale = locale;
71        this.mOrientation = orientation;
72        this.mWidth = width;
73        this.mMode = mode;
74        this.mXmlId = xmlId;
75        // Note: Turn off checking navigation flag to show TAB key for now.
76        this.mNavigateAction = InputTypeCompatUtils.isWebInputType(inputType);
77//                || EditorInfoCompatUtils.hasFlagNavigateNext(imeOptions)
78//                || EditorInfoCompatUtils.hasFlagNavigatePrevious(imeOptions);
79        this.mPasswordInput = InputTypeCompatUtils.isPasswordInputType(inputType)
80                || InputTypeCompatUtils.isVisiblePasswordInputType(inputType);
81        this.mHasSettingsKey = hasSettingsKey;
82        this.mF2KeyMode = f2KeyMode;
83        this.mClobberSettingsKey = clobberSettingsKey;
84        this.mShortcutKeyEnabled = shortcutKeyEnabled;
85        this.mHasShortcutKey = hasShortcutKey;
86        // We are interested only in {@link EditorInfo#IME_MASK_ACTION} enum value and
87        // {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION}.
88        this.mImeAction = imeOptions & (
89                EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
90
91        this.mXmlName = xmlName;
92        this.mAttribute = attribute;
93
94        this.mHashCode = Arrays.hashCode(new Object[] {
95                locale,
96                orientation,
97                width,
98                mode,
99                xmlId,
100                mNavigateAction,
101                mPasswordInput,
102                hasSettingsKey,
103                f2KeyMode,
104                clobberSettingsKey,
105                shortcutKeyEnabled,
106                hasShortcutKey,
107                mImeAction,
108        });
109    }
110
111    public KeyboardId cloneWithNewXml(String xmlName, int xmlId) {
112        return new KeyboardId(xmlName, xmlId, mLocale, mOrientation, mWidth, mMode, mAttribute,
113                false, F2KEY_MODE_NONE, false, false, false);
114    }
115
116    public KeyboardId cloneWithNewGeometry(int orientation, int width) {
117        if (mWidth == width)
118            return this;
119        return new KeyboardId(mXmlName, mXmlId, mLocale, orientation, width, mMode, mAttribute,
120                mHasSettingsKey, mF2KeyMode, mClobberSettingsKey, mShortcutKeyEnabled,
121                mHasShortcutKey);
122    }
123
124    public int getXmlId() {
125        return mXmlId;
126    }
127
128    public boolean isAlphabetKeyboard() {
129        return mXmlId == R.xml.kbd_qwerty;
130    }
131
132    public boolean isSymbolsKeyboard() {
133        return mXmlId == R.xml.kbd_symbols || mXmlId == R.xml.kbd_symbols_shift;
134    }
135
136    public boolean isPhoneKeyboard() {
137        return mMode == MODE_PHONE;
138    }
139
140    public boolean isPhoneShiftKeyboard() {
141        return mXmlId == R.xml.kbd_phone_shift;
142    }
143
144    public boolean isNumberKeyboard() {
145        return mMode == MODE_NUMBER;
146    }
147
148    @Override
149    public boolean equals(Object other) {
150        return other instanceof KeyboardId && equals((KeyboardId) other);
151    }
152
153    private boolean equals(KeyboardId other) {
154        return other.mLocale.equals(this.mLocale)
155            && other.mOrientation == this.mOrientation
156            && other.mWidth == this.mWidth
157            && other.mMode == this.mMode
158            && other.mXmlId == this.mXmlId
159            && other.mNavigateAction == this.mNavigateAction
160            && other.mPasswordInput == this.mPasswordInput
161            && other.mHasSettingsKey == this.mHasSettingsKey
162            && other.mF2KeyMode == this.mF2KeyMode
163            && other.mClobberSettingsKey == this.mClobberSettingsKey
164            && other.mShortcutKeyEnabled == this.mShortcutKeyEnabled
165            && other.mHasShortcutKey == this.mHasShortcutKey
166            && other.mImeAction == this.mImeAction;
167    }
168
169    @Override
170    public int hashCode() {
171        return mHashCode;
172    }
173
174    @Override
175    public String toString() {
176        return String.format("[%s.xml %s %s%d %s %s %s%s%s%s%s%s%s]",
177                mXmlName,
178                mLocale,
179                (mOrientation == 1 ? "port" : "land"), mWidth,
180                modeName(mMode),
181                EditorInfoCompatUtils.imeOptionsName(mImeAction),
182                f2KeyModeName(mF2KeyMode),
183                (mClobberSettingsKey ? " clobberSettingsKey" : ""),
184                (mNavigateAction ? " navigateAction" : ""),
185                (mPasswordInput ? " passwordInput" : ""),
186                (mHasSettingsKey ? " hasSettingsKey" : ""),
187                (mShortcutKeyEnabled ? " shortcutKeyEnabled" : ""),
188                (mHasShortcutKey ? " hasShortcutKey" : "")
189        );
190    }
191
192    public static String modeName(int mode) {
193        switch (mode) {
194        case MODE_TEXT: return "text";
195        case MODE_URL: return "url";
196        case MODE_EMAIL: return "email";
197        case MODE_IM: return "im";
198        case MODE_PHONE: return "phone";
199        case MODE_NUMBER: return "number";
200        default: return null;
201        }
202    }
203
204    public static String f2KeyModeName(int f2KeyMode) {
205        switch (f2KeyMode) {
206        case F2KEY_MODE_NONE: return "none";
207        case F2KEY_MODE_SETTINGS: return "settings";
208        case F2KEY_MODE_SHORTCUT_IME: return "shortcutIme";
209        case F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS: return "shortcutImeOrSettings";
210        default: return null;
211        }
212    }
213}
214