KeyboardId.java revision f1a81f5eb37df4170de2cf6327c860e3d64dc2f8
1/*
2 * Copyright (C) 2010 Google Inc.
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 com.android.inputmethod.compat.EditorInfoCompatUtils;
20import com.android.inputmethod.compat.InputTypeCompatUtils;
21import com.android.inputmethod.latin.R;
22
23import android.view.inputmethod.EditorInfo;
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_WEB = 4;
38    public static final int MODE_PHONE = 5;
39    public static final int MODE_NUMBER = 6;
40
41    public final Locale mLocale;
42    public final int mOrientation;
43    public final int mMode;
44    public final int mXmlId;
45    public final int mColorScheme;
46    public final boolean mPasswordInput;
47    public final boolean mHasSettingsKey;
48    public final boolean mVoiceKeyEnabled;
49    public final boolean mHasVoiceKey;
50    public final int mImeAction;
51    public final boolean mEnableShiftLock;
52    public final String mXmlName;
53
54    private final int mHashCode;
55
56    public KeyboardId(String xmlName, int xmlId, int colorScheme, Locale locale, int orientation,
57            int mode, EditorInfo attribute, boolean hasSettingsKey, boolean voiceKeyEnabled,
58            boolean hasVoiceKey, boolean enableShiftLock) {
59        final int inputType = (attribute != null) ? attribute.inputType : 0;
60        final int imeOptions = (attribute != null) ? attribute.imeOptions : 0;
61        this.mLocale = locale;
62        this.mOrientation = orientation;
63        this.mMode = mode;
64        this.mXmlId = xmlId;
65        this.mColorScheme = colorScheme;
66        this.mPasswordInput = InputTypeCompatUtils.isPasswordInputType(inputType)
67                || InputTypeCompatUtils.isVisiblePasswordInputType(inputType);
68        this.mHasSettingsKey = hasSettingsKey;
69        this.mVoiceKeyEnabled = voiceKeyEnabled;
70        this.mHasVoiceKey = hasVoiceKey;
71        // We are interested only in {@link EditorInfo#IME_MASK_ACTION} enum value and
72        // {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION}.
73        this.mImeAction = imeOptions & (
74                EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
75        this.mEnableShiftLock = enableShiftLock;
76        this.mXmlName = xmlName;
77
78        this.mHashCode = Arrays.hashCode(new Object[] {
79                locale,
80                orientation,
81                mode,
82                xmlId,
83                colorScheme,
84                mPasswordInput,
85                hasSettingsKey,
86                voiceKeyEnabled,
87                hasVoiceKey,
88                mImeAction,
89                enableShiftLock,
90        });
91    }
92
93    public int getXmlId() {
94        return mXmlId;
95    }
96
97    public boolean isAlphabetKeyboard() {
98        return mXmlId == R.xml.kbd_qwerty;
99    }
100
101    public boolean isSymbolsKeyboard() {
102        return mXmlId == R.xml.kbd_symbols;
103    }
104
105    public boolean isPhoneKeyboard() {
106        return mMode == MODE_PHONE;
107    }
108
109    public boolean isNumberKeyboard() {
110        return mMode == MODE_NUMBER;
111    }
112
113    @Override
114    public boolean equals(Object other) {
115        return other instanceof KeyboardId && equals((KeyboardId) other);
116    }
117
118    boolean equals(KeyboardId other) {
119        return other.mLocale.equals(this.mLocale)
120            && other.mOrientation == this.mOrientation
121            && other.mMode == this.mMode
122            && other.mXmlId == this.mXmlId
123            && other.mColorScheme == this.mColorScheme
124            && other.mPasswordInput == this.mPasswordInput
125            && other.mHasSettingsKey == this.mHasSettingsKey
126            && other.mVoiceKeyEnabled == this.mVoiceKeyEnabled
127            && other.mHasVoiceKey == this.mHasVoiceKey
128            && other.mImeAction == this.mImeAction
129            && other.mEnableShiftLock == this.mEnableShiftLock;
130    }
131
132    @Override
133    public int hashCode() {
134        return mHashCode;
135    }
136
137    @Override
138    public String toString() {
139        return String.format("[%s.xml %s %s %s imeAction=%s %s%s%s%s%s%s]",
140                mXmlName,
141                mLocale,
142                (mOrientation == 1 ? "port" : "land"),
143                modeName(mMode),
144                EditorInfoCompatUtils.imeOptionsName(mImeAction),
145                (mPasswordInput ? " passwordInput" : ""),
146                (mHasSettingsKey ? " hasSettingsKey" : ""),
147                (mVoiceKeyEnabled ? " voiceKeyEnabled" : ""),
148                (mHasVoiceKey ? " hasVoiceKey" : ""),
149                (mEnableShiftLock ? " enableShiftLock" : ""),
150                colorSchemeName(mColorScheme)
151        );
152    }
153
154    public static String modeName(int mode) {
155        switch (mode) {
156        case MODE_TEXT: return "text";
157        case MODE_URL: return "url";
158        case MODE_EMAIL: return "email";
159        case MODE_IM: return "im";
160        case MODE_WEB: return "web";
161        case MODE_PHONE: return "phone";
162        case MODE_NUMBER: return "number";
163        }
164        return null;
165    }
166
167    public static String colorSchemeName(int colorScheme) {
168        switch (colorScheme) {
169        case KeyboardView.COLOR_SCHEME_WHITE: return "white";
170        case KeyboardView.COLOR_SCHEME_BLACK: return "black";
171        }
172        return null;
173    }
174}
175