KeyboardId.java revision e9957752bcaad048746c7a57bbd2c0a59e1918a0
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.InputTypeCompatUtils;
20import com.android.inputmethod.latin.R;
21
22import android.view.inputmethod.EditorInfo;
23
24import java.util.Arrays;
25import java.util.Locale;
26
27/**
28 * Represents the parameters necessary to construct a new LatinKeyboard,
29 * which also serve as a unique identifier for each keyboard type.
30 */
31public class KeyboardId {
32    public static final int MODE_TEXT = 0;
33    public static final int MODE_URL = 1;
34    public static final int MODE_EMAIL = 2;
35    public static final int MODE_IM = 3;
36    public static final int MODE_WEB = 4;
37    public static final int MODE_PHONE = 5;
38    public static final int MODE_NUMBER = 6;
39
40    public final Locale mLocale;
41    public final int mOrientation;
42    public final int mMode;
43    public final int mXmlId;
44    public final int mColorScheme;
45    public final boolean mPasswordInput;
46    public final boolean mHasSettingsKey;
47    public final boolean mVoiceKeyEnabled;
48    public final boolean mHasVoiceKey;
49    public final int mImeAction;
50    public final boolean mEnableShiftLock;
51    public final String mXmlName;
52
53    private final int mHashCode;
54
55    public KeyboardId(String xmlName, int xmlId, int colorScheme, Locale locale, int orientation,
56            int mode, EditorInfo attribute, boolean hasSettingsKey, boolean voiceKeyEnabled,
57            boolean hasVoiceKey, boolean enableShiftLock) {
58        final int inputType = (attribute != null) ? attribute.inputType : 0;
59        final int imeOptions = (attribute != null) ? attribute.imeOptions : 0;
60        this.mLocale = locale;
61        this.mOrientation = orientation;
62        this.mMode = mode;
63        this.mXmlId = xmlId;
64        this.mColorScheme = colorScheme;
65        this.mPasswordInput = InputTypeCompatUtils.isPasswordInputType(inputType)
66                || InputTypeCompatUtils.isVisiblePasswordInputType(inputType);
67        this.mHasSettingsKey = hasSettingsKey;
68        this.mVoiceKeyEnabled = voiceKeyEnabled;
69        this.mHasVoiceKey = hasVoiceKey;
70        // We are interested only in {@link EditorInfo#IME_MASK_ACTION} enum value and
71        // {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION}.
72        this.mImeAction = imeOptions & (
73                EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
74        this.mEnableShiftLock = enableShiftLock;
75        this.mXmlName = xmlName;
76
77        this.mHashCode = Arrays.hashCode(new Object[] {
78                locale,
79                orientation,
80                mode,
81                xmlId,
82                colorScheme,
83                mPasswordInput,
84                hasSettingsKey,
85                voiceKeyEnabled,
86                hasVoiceKey,
87                mImeAction,
88                enableShiftLock,
89        });
90    }
91
92    public int getXmlId() {
93        return mXmlId;
94    }
95
96    public boolean isAlphabetKeyboard() {
97        return mXmlId == R.xml.kbd_qwerty;
98    }
99
100    public boolean isSymbolsKeyboard() {
101        return mXmlId == R.xml.kbd_symbols;
102    }
103
104    public boolean isPhoneKeyboard() {
105        return mMode == MODE_PHONE;
106    }
107
108    public boolean isNumberKeyboard() {
109        return mMode == MODE_NUMBER;
110    }
111
112    @Override
113    public boolean equals(Object other) {
114        return other instanceof KeyboardId && equals((KeyboardId) other);
115    }
116
117    boolean equals(KeyboardId other) {
118        return other.mLocale.equals(this.mLocale)
119            && other.mOrientation == this.mOrientation
120            && other.mMode == this.mMode
121            && other.mXmlId == this.mXmlId
122            && other.mColorScheme == this.mColorScheme
123            && other.mPasswordInput == this.mPasswordInput
124            && other.mHasSettingsKey == this.mHasSettingsKey
125            && other.mVoiceKeyEnabled == this.mVoiceKeyEnabled
126            && other.mHasVoiceKey == this.mHasVoiceKey
127            && other.mImeAction == this.mImeAction
128            && other.mEnableShiftLock == this.mEnableShiftLock;
129    }
130
131    @Override
132    public int hashCode() {
133        return mHashCode;
134    }
135
136    @Override
137    public String toString() {
138        return String.format("[%s.xml %s %s %s imeAction=%s %s%s%s%s%s%s]",
139                mXmlName,
140                mLocale,
141                (mOrientation == 1 ? "port" : "land"),
142                modeName(mMode),
143                imeOptionsName(mImeAction),
144                (mPasswordInput ? " passwordInput" : ""),
145                (mHasSettingsKey ? " hasSettingsKey" : ""),
146                (mVoiceKeyEnabled ? " voiceKeyEnabled" : ""),
147                (mHasVoiceKey ? " hasVoiceKey" : ""),
148                (mEnableShiftLock ? " enableShiftLock" : ""),
149                colorSchemeName(mColorScheme)
150        );
151    }
152
153    public static String modeName(int mode) {
154        switch (mode) {
155        case MODE_TEXT: return "text";
156        case MODE_URL: return "url";
157        case MODE_EMAIL: return "email";
158        case MODE_IM: return "im";
159        case MODE_WEB: return "web";
160        case MODE_PHONE: return "phone";
161        case MODE_NUMBER: return "number";
162        }
163        return null;
164    }
165
166    public static String colorSchemeName(int colorScheme) {
167        switch (colorScheme) {
168        case KeyboardView.COLOR_SCHEME_WHITE: return "white";
169        case KeyboardView.COLOR_SCHEME_BLACK: return "black";
170        }
171        return null;
172    }
173
174    public static String imeOptionsName(int imeOptions) {
175        if (imeOptions == -1) return null;
176        final int actionNo = imeOptions & EditorInfo.IME_MASK_ACTION;
177        final String action;
178        switch (actionNo) {
179        case EditorInfo.IME_ACTION_UNSPECIFIED: action = "actionUnspecified"; break;
180        case EditorInfo.IME_ACTION_NONE: action = "actionNone"; break;
181        case EditorInfo.IME_ACTION_GO: action = "actionGo"; break;
182        case EditorInfo.IME_ACTION_SEARCH: action = "actionSearch"; break;
183        case EditorInfo.IME_ACTION_SEND: action = "actionSend"; break;
184        case EditorInfo.IME_ACTION_DONE: action = "actionDone"; break;
185        case EditorInfo.IME_ACTION_PREVIOUS: action = "actionPrevious"; break;
186        default: action = "actionUnknown(" + actionNo + ")"; break;
187        }
188        if ((imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
189            return "flagNoEnterAction|" + action;
190        } else {
191            return action;
192        }
193    }
194}
195
196