KeyboardId.java revision 5a309f57155fb95667c2ccdda730eaf175de8876
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.latin.R;
20
21import android.view.inputmethod.EditorInfo;
22
23import java.util.Arrays;
24import java.util.Locale;
25
26/**
27 * Represents the parameters necessary to construct a new LatinKeyboard,
28 * which also serve as a unique identifier for each keyboard type.
29 */
30public class KeyboardId {
31    public static final int MODE_TEXT = 0;
32    public static final int MODE_URL = 1;
33    public static final int MODE_EMAIL = 2;
34    public static final int MODE_IM = 3;
35    public static final int MODE_WEB = 4;
36    public static final int MODE_PHONE = 5;
37    public static final int MODE_NUMBER = 6;
38
39    public final Locale mLocale;
40    public final int mOrientation;
41    public final int mMode;
42    public final int mXmlId;
43    public final int mColorScheme;
44    public final boolean mHasSettingsKey;
45    public final boolean mVoiceKeyEnabled;
46    public final boolean mHasVoiceKey;
47    public final int mImeOptions;
48    public final boolean mEnableShiftLock;
49
50    private final int mHashCode;
51
52    public KeyboardId(Locale locale, int orientation, int mode,
53            int xmlId, int colorScheme, boolean hasSettingsKey, boolean voiceKeyEnabled,
54            boolean hasVoiceKey, int imeOptions, boolean enableShiftLock) {
55        this.mLocale = locale;
56        this.mOrientation = orientation;
57        this.mMode = mode;
58        this.mXmlId = xmlId;
59        this.mColorScheme = colorScheme;
60        this.mHasSettingsKey = hasSettingsKey;
61        this.mVoiceKeyEnabled = voiceKeyEnabled;
62        this.mHasVoiceKey = hasVoiceKey;
63        // We are interested only in IME_MASK_ACTION enum value and IME_FLAG_NO_ENTER_ACTION.
64        this.mImeOptions = imeOptions
65                & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
66        this.mEnableShiftLock = enableShiftLock;
67
68        this.mHashCode = Arrays.hashCode(new Object[] {
69                locale,
70                orientation,
71                mode,
72                xmlId,
73                colorScheme,
74                hasSettingsKey,
75                voiceKeyEnabled,
76                hasVoiceKey,
77                imeOptions,
78                enableShiftLock,
79        });
80    }
81
82    public int getXmlId() {
83        return mXmlId;
84    }
85
86    public boolean isAlphabetMode() {
87        return mXmlId == R.xml.kbd_qwerty;
88    }
89
90    @Override
91    public boolean equals(Object other) {
92        return other instanceof KeyboardId && equals((KeyboardId) other);
93    }
94
95    boolean equals(KeyboardId other) {
96        return other.mLocale.equals(this.mLocale)
97            && other.mOrientation == this.mOrientation
98            && other.mMode == this.mMode
99            && other.mXmlId == this.mXmlId
100            && other.mColorScheme == this.mColorScheme
101            && other.mHasSettingsKey == this.mHasSettingsKey
102            && other.mVoiceKeyEnabled == this.mVoiceKeyEnabled
103            && other.mHasVoiceKey == this.mHasVoiceKey
104            && other.mImeOptions == this.mImeOptions
105            && other.mEnableShiftLock == this.mEnableShiftLock;
106    }
107
108    @Override
109    public int hashCode() {
110        return mHashCode;
111    }
112
113    @Override
114    public String toString() {
115        return String.format("[%s %s %5s imeOptions=0x%08x xml=0x%08x %s%s%s%s%s]",
116                mLocale,
117                (mOrientation == 1 ? "port" : "land"),
118                modeName(mMode),
119                mImeOptions,
120                mXmlId,
121                colorSchemeName(mColorScheme),
122                (mHasSettingsKey ? " hasSettingsKey" : ""),
123                (mVoiceKeyEnabled ? " voiceKeyEnabled" : ""),
124                (mHasVoiceKey ? " hasVoiceKey" : ""),
125                (mEnableShiftLock ? " enableShiftLock" : ""));
126    }
127
128    private static String modeName(int mode) {
129        switch (mode) {
130        case MODE_TEXT: return "text";
131        case MODE_URL: return "url";
132        case MODE_EMAIL: return "email";
133        case MODE_IM: return "im";
134        case MODE_WEB: return "web";
135        case MODE_PHONE: return "phone";
136        case MODE_NUMBER: return "number";
137        }
138        return null;
139    }
140
141    private static String colorSchemeName(int colorScheme) {
142        switch (colorScheme) {
143        case KeyboardView.COLOR_SCHEME_WHITE: return "white";
144        case KeyboardView.COLOR_SCHEME_BLACK: return "black";
145        }
146        return null;
147    }
148}
149