KeyboardId.java revision 645128af712961456a42cbcc34c0cdf5f0b40a83
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; 24 25import java.util.Arrays; 26import java.util.Locale; 27 28// TODO: Move to com.android.inputmethod.keyboard.internal package. 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 ELEMENT_ALPHABET = 0; 42 /* TODO: Implement alphabet variant shift keyboard. 43 public static final int ELEMENT_ALPHABET_MANUAL_TEMPORARY_SHIFT = 1; 44 public static final int ELEMENT_ALPHABET_AUTOMATIC_TEMPORARY_SHIFT = 2; 45 public static final int ELEMENT_ALPHABET_SHIFT_LOCK = 3; 46 public static final int ELEMENT_ALPHABET_SHIFT_LOCK_SHIFT = 4; 47 */ 48 public static final int ELEMENT_SYMBOLS = 5; 49 public static final int ELEMENT_SYMBOLS_SHIFT = 6; 50 public static final int ELEMENT_PHONE = 7; 51 public static final int ELEMENT_PHONE_SHIFT = 8; 52 public static final int ELEMENT_NUMBER = 9; 53 54 private static final int F2KEY_MODE_NONE = 0; 55 private static final int F2KEY_MODE_SETTINGS = 1; 56 private static final int F2KEY_MODE_SHORTCUT_IME = 2; 57 private static final int F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS = 3; 58 59 public final Locale mLocale; 60 public final int mOrientation; 61 public final int mWidth; 62 public final int mMode; 63 public final int mElementState; 64 private final int mInputType; 65 private final int mImeOptions; 66 private final boolean mSettingsKeyEnabled; 67 public final boolean mClobberSettingsKey; 68 public final boolean mShortcutKeyEnabled; 69 public final boolean mHasShortcutKey; 70 71 private final int mHashCode; 72 73 public KeyboardId(int elementState, Locale locale, int orientation, int width, int mode, 74 int inputType, int imeOptions, boolean settingsKeyEnabled, boolean clobberSettingsKey, 75 boolean shortcutKeyEnabled, boolean hasShortcutKey) { 76 this.mLocale = locale; 77 this.mOrientation = orientation; 78 this.mWidth = width; 79 this.mMode = mode; 80 this.mElementState = elementState; 81 this.mInputType = inputType; 82 this.mImeOptions = imeOptions; 83 this.mSettingsKeyEnabled = settingsKeyEnabled; 84 this.mClobberSettingsKey = clobberSettingsKey; 85 this.mShortcutKeyEnabled = shortcutKeyEnabled; 86 this.mHasShortcutKey = hasShortcutKey; 87 88 this.mHashCode = hashCode(this); 89 } 90 91 private static int hashCode(KeyboardId id) { 92 return Arrays.hashCode(new Object[] { 93 id.mOrientation, 94 id.mElementState, 95 id.mMode, 96 id.mWidth, 97 id.navigateAction(), 98 id.passwordInput(), 99 id.mSettingsKeyEnabled, 100 id.mClobberSettingsKey, 101 id.mShortcutKeyEnabled, 102 id.mHasShortcutKey, 103 id.imeAction(), 104 id.mLocale 105 }); 106 } 107 108 private boolean equals(KeyboardId other) { 109 if (other == this) 110 return true; 111 return other.mOrientation == this.mOrientation 112 && other.mElementState == this.mElementState 113 && other.mMode == this.mMode 114 && other.mWidth == this.mWidth 115 && other.navigateAction() == this.navigateAction() 116 && other.passwordInput() == this.passwordInput() 117 && other.mSettingsKeyEnabled == this.mSettingsKeyEnabled 118 && other.mClobberSettingsKey == this.mClobberSettingsKey 119 && other.mShortcutKeyEnabled == this.mShortcutKeyEnabled 120 && other.mHasShortcutKey == this.mHasShortcutKey 121 && other.imeAction() == this.imeAction() 122 && other.mLocale.equals(this.mLocale); 123 } 124 125 public boolean isAlphabetKeyboard() { 126 return mElementState < ELEMENT_SYMBOLS; 127 } 128 129 public boolean isSymbolsKeyboard() { 130 return mElementState == ELEMENT_SYMBOLS || mElementState == ELEMENT_SYMBOLS_SHIFT; 131 } 132 133 public boolean isPhoneKeyboard() { 134 return mElementState == ELEMENT_PHONE || mElementState == ELEMENT_PHONE_SHIFT; 135 } 136 137 public boolean isPhoneShiftKeyboard() { 138 return mElementState == ELEMENT_PHONE_SHIFT; 139 } 140 141 public boolean navigateAction() { 142 // Note: Turn off checking navigation flag to show TAB key for now. 143 boolean navigateAction = InputTypeCompatUtils.isWebInputType(mInputType); 144// || EditorInfoCompatUtils.hasFlagNavigateNext(mImeOptions) 145// || EditorInfoCompatUtils.hasFlagNavigatePrevious(mImeOptions); 146 return navigateAction; 147 } 148 149 public boolean passwordInput() { 150 return InputTypeCompatUtils.isPasswordInputType(mInputType) 151 || InputTypeCompatUtils.isVisiblePasswordInputType(mInputType); 152 } 153 154 public int imeAction() { 155 // We are interested only in {@link EditorInfo#IME_MASK_ACTION} enum value and 156 // {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION}. 157 return mImeOptions & ( 158 EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION); 159 } 160 161 public boolean hasSettingsKey() { 162 return mSettingsKeyEnabled && !mClobberSettingsKey; 163 } 164 165 public int f2KeyMode() { 166 if (mClobberSettingsKey) { 167 // Never shows the Settings key 168 return KeyboardId.F2KEY_MODE_SHORTCUT_IME; 169 } 170 171 if (mSettingsKeyEnabled) { 172 return KeyboardId.F2KEY_MODE_SETTINGS; 173 } else { 174 // It should be alright to fall back to the Settings key on 7-inch layouts 175 // even when the Settings key is not explicitly enabled. 176 return KeyboardId.F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS; 177 } 178 } 179 180 @Override 181 public boolean equals(Object other) { 182 return other instanceof KeyboardId && equals((KeyboardId) other); 183 } 184 185 @Override 186 public int hashCode() { 187 return mHashCode; 188 } 189 190 @Override 191 public String toString() { 192 return String.format("[%s %s %s%d %s %s %s%s%s%s%s%s%s]", 193 elementStateToString(mElementState), 194 mLocale, 195 (mOrientation == 1 ? "port" : "land"), mWidth, 196 modeName(mMode), 197 EditorInfoCompatUtils.imeOptionsName(imeAction()), 198 f2KeyModeName(f2KeyMode()), 199 (mClobberSettingsKey ? " clobberSettingsKey" : ""), 200 (navigateAction() ? " navigateAction" : ""), 201 (passwordInput() ? " passwordInput" : ""), 202 (hasSettingsKey() ? " hasSettingsKey" : ""), 203 (mShortcutKeyEnabled ? " shortcutKeyEnabled" : ""), 204 (mHasShortcutKey ? " hasShortcutKey" : "") 205 ); 206 } 207 208 public static boolean equivalentEditorInfoForKeyboard(EditorInfo a, EditorInfo b) { 209 if (a == null && b == null) return true; 210 if (a == null || b == null) return false; 211 return a.inputType == b.inputType 212 && a.imeOptions == b.imeOptions 213 && TextUtils.equals(a.privateImeOptions, b.privateImeOptions); 214 } 215 216 public static String elementStateToString(int elementState) { 217 switch (elementState) { 218 case ELEMENT_ALPHABET: return "alphabet"; 219 /* TODO: Implement alphabet variant shift keyboard. 220 case ELEMENT_ALPHABET_MANUAL_TEMPORARY_SHIFT: return "alphabetManualTemporaryShift"; 221 case ELEMENT_ALPHABET_AUTOMATIC_TEMPORARY_SHIFT: return "alphabetAutomaticTemporaryShift"; 222 case ELEMENT_ALPHABET_SHIFT_LOCK: return "alphabetShiftLock"; 223 case ELEMENT_ALPHABET_SHIFT_LOCK_SHIFT: return "alphabetShiftLockShift"; 224 */ 225 case ELEMENT_SYMBOLS: return "symbols"; 226 case ELEMENT_SYMBOLS_SHIFT: return "symbolsShift"; 227 case ELEMENT_PHONE: return "phone"; 228 case ELEMENT_PHONE_SHIFT: return "phoneShift"; 229 case ELEMENT_NUMBER: return "number"; 230 default: return null; 231 } 232 } 233 234 public static String modeName(int mode) { 235 switch (mode) { 236 case MODE_TEXT: return "text"; 237 case MODE_URL: return "url"; 238 case MODE_EMAIL: return "email"; 239 case MODE_IM: return "im"; 240 case MODE_PHONE: return "phone"; 241 case MODE_NUMBER: return "number"; 242 default: return null; 243 } 244 } 245 246 public static String f2KeyModeName(int f2KeyMode) { 247 switch (f2KeyMode) { 248 case F2KEY_MODE_NONE: return "none"; 249 case F2KEY_MODE_SETTINGS: return "settings"; 250 case F2KEY_MODE_SHORTCUT_IME: return "shortcutIme"; 251 case F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS: return "shortcutImeOrSettings"; 252 default: return null; 253 } 254 } 255} 256