Keyboard.java revision 6652e4589981a0f06692b49e86d1a6f0784a434f
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.graphics.drawable.Drawable; 20import android.text.TextUtils; 21import android.util.Log; 22 23import com.android.inputmethod.keyboard.internal.KeyboardIconsSet; 24import com.android.inputmethod.keyboard.internal.KeyboardParams; 25import com.android.inputmethod.keyboard.internal.KeyboardShiftState; 26 27import java.util.Collections; 28import java.util.HashMap; 29import java.util.List; 30import java.util.Map; 31import java.util.Set; 32 33/** 34 * Loads an XML description of a keyboard and stores the attributes of the keys. A keyboard 35 * consists of rows of keys. 36 * <p>The layout file for a keyboard contains XML that looks like the following snippet:</p> 37 * <pre> 38 * <Keyboard 39 * latin:keyWidth="%10p" 40 * latin:keyHeight="50px" 41 * latin:horizontalGap="2px" 42 * latin:verticalGap="2px" > 43 * <Row latin:keyWidth="32px" > 44 * <Key latin:keyLabel="A" /> 45 * ... 46 * </Row> 47 * ... 48 * </Keyboard> 49 * </pre> 50 */ 51public class Keyboard { 52 private static final String TAG = Keyboard.class.getSimpleName(); 53 54 /** Some common keys code. These should be aligned with values/keycodes.xml */ 55 public static final int CODE_ENTER = '\n'; 56 public static final int CODE_TAB = '\t'; 57 public static final int CODE_SPACE = ' '; 58 public static final int CODE_PERIOD = '.'; 59 public static final int CODE_DASH = '-'; 60 public static final int CODE_SINGLE_QUOTE = '\''; 61 public static final int CODE_DOUBLE_QUOTE = '"'; 62 // TODO: Check how this should work for right-to-left languages. It seems to stand 63 // that for rtl languages, a closing parenthesis is a left parenthesis. Is this 64 // managed by the font? Or is it a different char? 65 public static final int CODE_CLOSING_PARENTHESIS = ')'; 66 public static final int CODE_CLOSING_SQUARE_BRACKET = ']'; 67 public static final int CODE_CLOSING_CURLY_BRACKET = '}'; 68 public static final int CODE_CLOSING_ANGLE_BRACKET = '>'; 69 public static final int CODE_DIGIT0 = '0'; 70 public static final int CODE_PLUS = '+'; 71 72 /** Special keys code. These should be aligned with values/keycodes.xml */ 73 public static final int CODE_DUMMY = 0; 74 public static final int CODE_SHIFT = -1; 75 public static final int CODE_SWITCH_ALPHA_SYMBOL = -2; 76 public static final int CODE_CAPSLOCK = -3; 77 public static final int CODE_DELETE = -5; 78 public static final int CODE_SETTINGS = -6; 79 public static final int CODE_SHORTCUT = -7; 80 // Code value representing the code is not specified. 81 public static final int CODE_UNSPECIFIED = -99; 82 83 public final KeyboardId mId; 84 public final int mThemeId; 85 86 /** Total height of the keyboard, including the padding and keys */ 87 public final int mOccupiedHeight; 88 /** Total width of the keyboard, including the padding and keys */ 89 public final int mOccupiedWidth; 90 91 /** The padding above the keyboard */ 92 public final int mTopPadding; 93 /** Default gap between rows */ 94 public final int mVerticalGap; 95 96 public final int mMostCommonKeyHeight; 97 public final int mMostCommonKeyWidth; 98 99 /** More keys keyboard template */ 100 public final int mMoreKeysTemplate; 101 102 /** Maximum column for mini keyboard */ 103 public final int mMaxMiniKeyboardColumn; 104 105 /** True if Right-To-Left keyboard */ 106 public final boolean mIsRtlKeyboard; 107 108 /** List of keys and icons in this keyboard */ 109 public final List<Key> mKeys; 110 public final List<Key> mShiftKeys; 111 public final Set<Key> mShiftLockKeys; 112 public final Map<Key, Drawable> mShiftedIcons; 113 public final Map<Key, Drawable> mUnshiftedIcons; 114 public final KeyboardIconsSet mIconsSet; 115 116 private final Map<Integer, Key> mKeyCache = new HashMap<Integer, Key>(); 117 118 private final ProximityInfo mProximityInfo; 119 120 // TODO: Remove this variable. 121 private final KeyboardShiftState mShiftState = new KeyboardShiftState(); 122 123 public Keyboard(KeyboardParams params) { 124 mId = params.mId; 125 mThemeId = params.mThemeId; 126 mOccupiedHeight = params.mOccupiedHeight; 127 mOccupiedWidth = params.mOccupiedWidth; 128 mMostCommonKeyHeight = params.mMostCommonKeyHeight; 129 mMostCommonKeyWidth = params.mMostCommonKeyWidth; 130 mIsRtlKeyboard = params.mIsRtlKeyboard; 131 mMoreKeysTemplate = params.mMoreKeysTemplate; 132 mMaxMiniKeyboardColumn = params.mMaxMiniKeyboardColumn; 133 134 mTopPadding = params.mTopPadding; 135 mVerticalGap = params.mVerticalGap; 136 137 mKeys = Collections.unmodifiableList(params.mKeys); 138 mShiftKeys = Collections.unmodifiableList(params.mShiftKeys); 139 mShiftLockKeys = Collections.unmodifiableSet(params.mShiftLockKeys); 140 mShiftedIcons = Collections.unmodifiableMap(params.mShiftedIcons); 141 mUnshiftedIcons = Collections.unmodifiableMap(params.mUnshiftedIcons); 142 mIconsSet = params.mIconsSet; 143 144 mProximityInfo = new ProximityInfo( 145 params.GRID_WIDTH, params.GRID_HEIGHT, mOccupiedWidth, mOccupiedHeight, 146 mMostCommonKeyWidth, mMostCommonKeyHeight, mKeys, params.mTouchPositionCorrection); 147 } 148 149 public ProximityInfo getProximityInfo() { 150 return mProximityInfo; 151 } 152 153 public Key getKey(int code) { 154 if (code == CODE_DUMMY) { 155 return null; 156 } 157 final Integer keyCode = code; 158 if (mKeyCache.containsKey(keyCode)) { 159 return mKeyCache.get(keyCode); 160 } 161 162 for (final Key key : mKeys) { 163 if (key.mCode == code) { 164 mKeyCache.put(keyCode, key); 165 return key; 166 } 167 } 168 mKeyCache.put(keyCode, null); 169 return null; 170 } 171 172 // TODO: Remove this method. 173 public boolean hasShiftLockKey() { 174 return !mShiftLockKeys.isEmpty(); 175 } 176 177 // TODO: Remove this method. 178 public void setShiftLocked(boolean newShiftLockState) { 179 for (final Key key : mShiftLockKeys) { 180 // To represent "shift locked" state. The highlight is handled by background image that 181 // might be a StateListDrawable. 182 key.setHighlightOn(newShiftLockState); 183 key.setIcon(newShiftLockState ? mShiftedIcons.get(key) : mUnshiftedIcons.get(key)); 184 } 185 mShiftState.setShiftLocked(newShiftLockState); 186 } 187 188 // TODO: Move this method to KeyboardId. 189 public boolean isShiftLocked() { 190 return mShiftState.isShiftLocked(); 191 } 192 193 // TODO: Remove this method. 194 public void setShifted(boolean newShiftState) { 195 if (!mShiftState.isShiftLocked()) { 196 for (final Key key : mShiftKeys) { 197 key.setIcon(newShiftState ? mShiftedIcons.get(key) : mUnshiftedIcons.get(key)); 198 } 199 } 200 mShiftState.setShifted(newShiftState); 201 } 202 203 // TODO: Move this method to KeyboardId. 204 public boolean isShiftedOrShiftLocked() { 205 return mShiftState.isShiftedOrShiftLocked(); 206 } 207 208 // TODO: Remove this method 209 public void setAutomaticTemporaryUpperCase() { 210 mShiftState.setAutomaticTemporaryUpperCase(); 211 } 212 213 // TODO: Move this method to KeyboardId. 214 public boolean isManualTemporaryUpperCase() { 215 return mShiftState.isManualTemporaryUpperCase(); 216 } 217 218 // TODO: Remove this method. 219 public CharSequence adjustLabelCase(CharSequence label) { 220 if (isShiftedOrShiftLocked() && !TextUtils.isEmpty(label) && label.length() < 3 221 && Character.isLowerCase(label.charAt(0))) { 222 return label.toString().toUpperCase(mId.mLocale); 223 } 224 return label; 225 } 226 227 /** 228 * Returns the indices of the keys that are closest to the given point. 229 * @param x the x-coordinate of the point 230 * @param y the y-coordinate of the point 231 * @return the array of integer indices for the nearest keys to the given point. If the given 232 * point is out of range, then an array of size zero is returned. 233 */ 234 public int[] getNearestKeys(int x, int y) { 235 return mProximityInfo.getNearestKeys(x, y); 236 } 237 238 public static String printableCode(int code) { 239 switch (code) { 240 case CODE_SHIFT: return "shift"; 241 case CODE_SWITCH_ALPHA_SYMBOL: return "symbol"; 242 case CODE_CAPSLOCK: return "capslock"; 243 case CODE_DELETE: return "delete"; 244 case CODE_SHORTCUT: return "shortcut"; 245 case CODE_DUMMY: return "dummy"; 246 case CODE_UNSPECIFIED: return "unspec"; 247 default: 248 if (code < 0) Log.w(TAG, "Unknow negative key code=" + code); 249 if (code < 0x100) return String.format("\\u%02x", code); 250 return String.format("\\u04x", code); 251 } 252 } 253} 254