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