KeyboardId.java revision f0f726464dcb5b3cef4f8e703659b35ca62430b5
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 final Locale mLocale;
41    public final int mOrientation;
42    public final int mWidth;
43    public final int mMode;
44    public final int mXmlId;
45    public final int mColorScheme;
46    public final boolean mNavigateAction;
47    public final boolean mPasswordInput;
48    public final boolean mHasSettingsKey;
49    public final boolean mVoiceKeyEnabled;
50    public final boolean mHasVoiceKey;
51    public final int mImeAction;
52    public final boolean mEnableShiftLock;
53
54    public final String mXmlName;
55    public final EditorInfo mAttribute;
56
57    private final int mHashCode;
58
59    public KeyboardId(String xmlName, int xmlId, int colorScheme, Locale locale, int orientation,
60            int width, int mode, EditorInfo attribute, boolean hasSettingsKey,
61            boolean voiceKeyEnabled, boolean hasVoiceKey, boolean enableShiftLock) {
62        final int inputType = (attribute != null) ? attribute.inputType : 0;
63        final int imeOptions = (attribute != null) ? attribute.imeOptions : 0;
64        this.mLocale = locale;
65        this.mOrientation = orientation;
66        this.mWidth = width;
67        this.mMode = mode;
68        this.mXmlId = xmlId;
69        this.mColorScheme = colorScheme;
70        // Note: Turn off checking navigation flag to show TAB key for now.
71        this.mNavigateAction = InputTypeCompatUtils.isWebInputType(inputType);
72//                || EditorInfoCompatUtils.hasFlagNavigateNext(imeOptions)
73//                || EditorInfoCompatUtils.hasFlagNavigatePrevious(imeOptions);
74        this.mPasswordInput = InputTypeCompatUtils.isPasswordInputType(inputType)
75                || InputTypeCompatUtils.isVisiblePasswordInputType(inputType);
76        this.mHasSettingsKey = hasSettingsKey;
77        this.mVoiceKeyEnabled = voiceKeyEnabled;
78        this.mHasVoiceKey = hasVoiceKey;
79        // We are interested only in {@link EditorInfo#IME_MASK_ACTION} enum value and
80        // {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION}.
81        this.mImeAction = imeOptions & (
82                EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
83        this.mEnableShiftLock = enableShiftLock;
84
85        this.mXmlName = xmlName;
86        this.mAttribute = attribute;
87
88        this.mHashCode = Arrays.hashCode(new Object[] {
89                locale,
90                orientation,
91                width,
92                mode,
93                xmlId,
94                colorScheme,
95                mNavigateAction,
96                mPasswordInput,
97                hasSettingsKey,
98                voiceKeyEnabled,
99                hasVoiceKey,
100                mImeAction,
101                enableShiftLock,
102        });
103    }
104
105    public KeyboardId cloneWithNewLayout(String xmlName, int xmlId) {
106        return new KeyboardId(xmlName, xmlId, mColorScheme, mLocale, mOrientation, mWidth, mMode,
107                mAttribute, mHasSettingsKey, mVoiceKeyEnabled, mHasVoiceKey, mEnableShiftLock);
108    }
109
110    public KeyboardId cloneWithNewGeometry(int width) {
111        if (mWidth == width)
112            return this;
113        return new KeyboardId(mXmlName, mXmlId, mColorScheme, mLocale, mOrientation, width, mMode,
114                mAttribute, mHasSettingsKey, mVoiceKeyEnabled, mHasVoiceKey, mEnableShiftLock);
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;
127    }
128
129    public boolean isPhoneKeyboard() {
130        return mMode == MODE_PHONE;
131    }
132
133    public boolean isNumberKeyboard() {
134        return mMode == MODE_NUMBER;
135    }
136
137    @Override
138    public boolean equals(Object other) {
139        return other instanceof KeyboardId && equals((KeyboardId) other);
140    }
141
142    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.mColorScheme == this.mColorScheme
149            && other.mNavigateAction == this.mNavigateAction
150            && other.mPasswordInput == this.mPasswordInput
151            && other.mHasSettingsKey == this.mHasSettingsKey
152            && other.mVoiceKeyEnabled == this.mVoiceKeyEnabled
153            && other.mHasVoiceKey == this.mHasVoiceKey
154            && other.mImeAction == this.mImeAction
155            && other.mEnableShiftLock == this.mEnableShiftLock;
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                colorSchemeName(mColorScheme),
172                (mNavigateAction ? " navigateAction" : ""),
173                (mPasswordInput ? " passwordInput" : ""),
174                (mHasSettingsKey ? " hasSettingsKey" : ""),
175                (mVoiceKeyEnabled ? " voiceKeyEnabled" : ""),
176                (mHasVoiceKey ? " hasVoiceKey" : ""),
177                (mEnableShiftLock ? " enableShiftLock" : "")
178        );
179    }
180
181    public static String modeName(int mode) {
182        switch (mode) {
183        case MODE_TEXT: return "text";
184        case MODE_URL: return "url";
185        case MODE_EMAIL: return "email";
186        case MODE_IM: return "im";
187        case MODE_PHONE: return "phone";
188        case MODE_NUMBER: return "number";
189        }
190        return null;
191    }
192
193    public static String colorSchemeName(int colorScheme) {
194        switch (colorScheme) {
195        case KeyboardView.COLOR_SCHEME_WHITE: return "white";
196        case KeyboardView.COLOR_SCHEME_BLACK: return "black";
197        }
198        return null;
199    }
200}
201