2010-12-02 09:46:21 +00:00
|
|
|
/*
|
2011-05-20 03:09:57 +00:00
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
2010-12-02 09:46:21 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.keyboard;
|
|
|
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
2011-06-28 07:32:39 +00:00
|
|
|
import android.text.TextUtils;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2011-06-21 14:38:42 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
|
2011-07-29 00:05:40 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyboardParams;
|
2011-06-22 02:53:02 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyboardShiftState;
|
2011-06-15 04:38:58 +00:00
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
import java.util.Collections;
|
2010-12-02 09:46:21 +00:00
|
|
|
import java.util.List;
|
2011-08-02 00:50:49 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads an XML description of a keyboard and stores the attributes of the keys. A keyboard
|
|
|
|
* consists of rows of keys.
|
|
|
|
* <p>The layout file for a keyboard contains XML that looks like the following snippet:</p>
|
|
|
|
* <pre>
|
|
|
|
* <Keyboard
|
|
|
|
* latin:keyWidth="%10p"
|
|
|
|
* latin:keyHeight="50px"
|
|
|
|
* latin:horizontalGap="2px"
|
|
|
|
* latin:verticalGap="2px" >
|
|
|
|
* <Row latin:keyWidth="32px" >
|
|
|
|
* <Key latin:keyLabel="A" />
|
|
|
|
* ...
|
|
|
|
* </Row>
|
|
|
|
* ...
|
|
|
|
* </Keyboard>
|
|
|
|
* </pre>
|
|
|
|
*/
|
|
|
|
public class Keyboard {
|
2010-12-06 03:12:27 +00:00
|
|
|
/** Some common keys code. These should be aligned with values/keycodes.xml */
|
2010-12-02 11:54:32 +00:00
|
|
|
public static final int CODE_ENTER = '\n';
|
|
|
|
public static final int CODE_TAB = '\t';
|
|
|
|
public static final int CODE_SPACE = ' ';
|
|
|
|
public static final int CODE_PERIOD = '.';
|
2011-04-28 06:39:39 +00:00
|
|
|
public static final int CODE_DASH = '-';
|
|
|
|
public static final int CODE_SINGLE_QUOTE = '\'';
|
|
|
|
public static final int CODE_DOUBLE_QUOTE = '"';
|
2011-07-14 05:15:01 +00:00
|
|
|
// TODO: Check how this should work for right-to-left languages. It seems to stand
|
|
|
|
// that for rtl languages, a closing parenthesis is a left parenthesis. Is this
|
|
|
|
// managed by the font? Or is it a different char?
|
|
|
|
public static final int CODE_CLOSING_PARENTHESIS = ')';
|
|
|
|
public static final int CODE_CLOSING_SQUARE_BRACKET = ']';
|
|
|
|
public static final int CODE_CLOSING_CURLY_BRACKET = '}';
|
|
|
|
public static final int CODE_CLOSING_ANGLE_BRACKET = '>';
|
2011-07-28 01:02:05 +00:00
|
|
|
public static final int CODE_DIGIT0 = '0';
|
|
|
|
public static final int CODE_PLUS = '+';
|
2011-07-14 05:15:01 +00:00
|
|
|
|
2010-12-02 11:54:32 +00:00
|
|
|
|
2010-12-06 03:12:27 +00:00
|
|
|
/** Special keys code. These should be aligned with values/keycodes.xml */
|
2010-12-20 11:30:26 +00:00
|
|
|
public static final int CODE_DUMMY = 0;
|
2010-12-02 11:54:32 +00:00
|
|
|
public static final int CODE_SHIFT = -1;
|
2010-12-06 03:12:27 +00:00
|
|
|
public static final int CODE_SWITCH_ALPHA_SYMBOL = -2;
|
2011-04-21 10:43:20 +00:00
|
|
|
public static final int CODE_CAPSLOCK = -3;
|
|
|
|
public static final int CODE_CANCEL = -4;
|
2010-12-02 11:54:32 +00:00
|
|
|
public static final int CODE_DELETE = -5;
|
2011-04-21 10:43:20 +00:00
|
|
|
public static final int CODE_SETTINGS = -6;
|
2011-08-06 01:45:19 +00:00
|
|
|
public static final int CODE_SHORTCUT = -7;
|
2010-12-20 11:30:26 +00:00
|
|
|
// Code value representing the code is not specified.
|
|
|
|
public static final int CODE_UNSPECIFIED = -99;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2011-08-02 00:50:49 +00:00
|
|
|
public final KeyboardId mId;
|
2011-09-28 03:08:48 +00:00
|
|
|
public final int mThemeId;
|
2011-08-02 00:50:49 +00:00
|
|
|
|
|
|
|
/** Total height of the keyboard, including the padding and keys */
|
2011-07-29 00:05:40 +00:00
|
|
|
public final int mOccupiedHeight;
|
|
|
|
/** Total width of the keyboard, including the padding and keys */
|
|
|
|
public final int mOccupiedWidth;
|
2011-08-02 00:50:49 +00:00
|
|
|
|
2011-09-22 10:29:58 +00:00
|
|
|
/** The padding above the keyboard */
|
|
|
|
public final int mTopPadding;
|
2010-12-02 09:46:21 +00:00
|
|
|
/** Default gap between rows */
|
2011-07-29 00:05:40 +00:00
|
|
|
public final int mVerticalGap;
|
|
|
|
|
2011-09-22 10:29:58 +00:00
|
|
|
public final int mMostCommonKeyHeight;
|
2011-07-29 00:05:40 +00:00
|
|
|
public final int mMostCommonKeyWidth;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2011-08-31 06:26:32 +00:00
|
|
|
/** More keys keyboard template */
|
|
|
|
public final int mMoreKeysTemplate;
|
2010-12-14 06:31:47 +00:00
|
|
|
|
2011-08-31 05:22:53 +00:00
|
|
|
/** Maximum column for mini keyboard */
|
|
|
|
public final int mMaxMiniKeyboardColumn;
|
2010-12-14 06:31:47 +00:00
|
|
|
|
2011-07-24 23:35:54 +00:00
|
|
|
/** True if Right-To-Left keyboard */
|
2011-07-29 00:05:40 +00:00
|
|
|
public final boolean mIsRtlKeyboard;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
/** List of keys and icons in this keyboard */
|
|
|
|
public final List<Key> mKeys;
|
|
|
|
public final List<Key> mShiftKeys;
|
|
|
|
public final Set<Key> mShiftLockKeys;
|
|
|
|
public final Map<Key, Drawable> mShiftedIcons;
|
|
|
|
public final Map<Key, Drawable> mUnshiftedIcons;
|
|
|
|
public final KeyboardIconsSet mIconsSet;
|
2011-04-15 07:33:46 +00:00
|
|
|
|
2011-08-02 00:50:49 +00:00
|
|
|
private final KeyboardShiftState mShiftState = new KeyboardShiftState();
|
2011-06-21 14:38:42 +00:00
|
|
|
|
2011-02-22 08:28:55 +00:00
|
|
|
private final ProximityInfo mProximityInfo;
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
public Keyboard(KeyboardParams params) {
|
|
|
|
mId = params.mId;
|
2011-09-28 03:08:48 +00:00
|
|
|
mThemeId = params.mThemeId;
|
2011-07-29 00:05:40 +00:00
|
|
|
mOccupiedHeight = params.mOccupiedHeight;
|
|
|
|
mOccupiedWidth = params.mOccupiedWidth;
|
2011-09-22 10:29:58 +00:00
|
|
|
mMostCommonKeyHeight = params.mMostCommonKeyHeight;
|
2011-07-29 00:05:40 +00:00
|
|
|
mMostCommonKeyWidth = params.mMostCommonKeyWidth;
|
|
|
|
mIsRtlKeyboard = params.mIsRtlKeyboard;
|
2011-08-31 06:26:32 +00:00
|
|
|
mMoreKeysTemplate = params.mMoreKeysTemplate;
|
2011-08-31 05:22:53 +00:00
|
|
|
mMaxMiniKeyboardColumn = params.mMaxMiniKeyboardColumn;
|
2011-07-29 00:05:40 +00:00
|
|
|
|
2011-09-22 10:29:58 +00:00
|
|
|
mTopPadding = params.mTopPadding;
|
2011-07-29 00:05:40 +00:00
|
|
|
mVerticalGap = params.mVerticalGap;
|
|
|
|
|
|
|
|
mKeys = Collections.unmodifiableList(params.mKeys);
|
|
|
|
mShiftKeys = Collections.unmodifiableList(params.mShiftKeys);
|
|
|
|
mShiftLockKeys = Collections.unmodifiableSet(params.mShiftLockKeys);
|
|
|
|
mShiftedIcons = Collections.unmodifiableMap(params.mShiftedIcons);
|
|
|
|
mUnshiftedIcons = Collections.unmodifiableMap(params.mUnshiftedIcons);
|
|
|
|
mIconsSet = params.mIconsSet;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2011-07-12 05:58:46 +00:00
|
|
|
mProximityInfo = new ProximityInfo(
|
2011-07-29 00:05:40 +00:00
|
|
|
params.GRID_WIDTH, params.GRID_HEIGHT, mOccupiedWidth, mOccupiedHeight,
|
2011-10-03 09:34:34 +00:00
|
|
|
mMostCommonKeyWidth, mMostCommonKeyHeight, mKeys, params.mTouchPositionCorrection);
|
2011-02-22 08:28:55 +00:00
|
|
|
}
|
|
|
|
|
2011-08-04 03:08:22 +00:00
|
|
|
public ProximityInfo getProximityInfo() {
|
|
|
|
return mProximityInfo;
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-07-28 00:20:06 +00:00
|
|
|
public boolean hasShiftLockKey() {
|
|
|
|
return !mShiftLockKeys.isEmpty();
|
2010-12-02 11:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setShiftLocked(boolean newShiftLockState) {
|
2011-07-28 00:20:06 +00:00
|
|
|
for (final Key key : mShiftLockKeys) {
|
|
|
|
// To represent "shift locked" state. The highlight is handled by background image that
|
|
|
|
// might be a StateListDrawable.
|
2011-06-23 12:23:44 +00:00
|
|
|
key.setHighlightOn(newShiftLockState);
|
2011-07-28 00:20:06 +00:00
|
|
|
// To represent "shifted" state. The key might have a shifted icon.
|
|
|
|
if (newShiftLockState && mShiftedIcons.containsKey(key)) {
|
|
|
|
key.setIcon(mShiftedIcons.get(key));
|
|
|
|
} else {
|
|
|
|
key.setIcon(mUnshiftedIcons.get(key));
|
|
|
|
}
|
2010-12-02 11:54:32 +00:00
|
|
|
}
|
|
|
|
mShiftState.setShiftLocked(newShiftLockState);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isShiftLocked() {
|
|
|
|
return mShiftState.isShiftLocked();
|
|
|
|
}
|
|
|
|
|
2011-11-17 00:00:02 +00:00
|
|
|
public boolean isShiftLockShifted() {
|
|
|
|
return mShiftState.isShiftLockShifted();
|
|
|
|
}
|
|
|
|
|
2010-12-02 11:54:32 +00:00
|
|
|
public boolean setShifted(boolean newShiftState) {
|
2011-07-28 00:20:06 +00:00
|
|
|
for (final Key key : mShiftKeys) {
|
2010-12-02 11:54:32 +00:00
|
|
|
if (!newShiftState && !mShiftState.isShiftLocked()) {
|
2011-07-28 00:20:06 +00:00
|
|
|
key.setIcon(mUnshiftedIcons.get(key));
|
2010-12-02 11:54:32 +00:00
|
|
|
} else if (newShiftState && !mShiftState.isShiftedOrShiftLocked()) {
|
2011-07-28 00:20:06 +00:00
|
|
|
key.setIcon(mShiftedIcons.get(key));
|
2010-12-02 11:54:32 +00:00
|
|
|
}
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
2010-12-02 11:54:32 +00:00
|
|
|
return mShiftState.setShifted(newShiftState);
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isShiftedOrShiftLocked() {
|
2010-12-02 11:54:32 +00:00
|
|
|
return mShiftState.isShiftedOrShiftLocked();
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
2010-12-02 11:54:32 +00:00
|
|
|
public void setAutomaticTemporaryUpperCase() {
|
|
|
|
setShifted(true);
|
|
|
|
mShiftState.setAutomaticTemporaryUpperCase();
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
2010-12-02 11:54:32 +00:00
|
|
|
public boolean isAutomaticTemporaryUpperCase() {
|
|
|
|
return isAlphaKeyboard() && mShiftState.isAutomaticTemporaryUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isManualTemporaryUpperCase() {
|
|
|
|
return isAlphaKeyboard() && mShiftState.isManualTemporaryUpperCase();
|
|
|
|
}
|
|
|
|
|
2011-01-18 09:12:44 +00:00
|
|
|
public boolean isManualTemporaryUpperCaseFromAuto() {
|
|
|
|
return isAlphaKeyboard() && mShiftState.isManualTemporaryUpperCaseFromAuto();
|
|
|
|
}
|
|
|
|
|
2010-12-02 11:54:32 +00:00
|
|
|
public KeyboardShiftState getKeyboardShiftState() {
|
|
|
|
return mShiftState;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAlphaKeyboard() {
|
2011-06-28 07:32:39 +00:00
|
|
|
return mId.isAlphabetKeyboard();
|
2010-12-02 11:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isPhoneKeyboard() {
|
2011-06-28 07:32:39 +00:00
|
|
|
return mId.isPhoneKeyboard();
|
2010-12-02 11:54:32 +00:00
|
|
|
}
|
|
|
|
|
2011-06-28 07:32:39 +00:00
|
|
|
public CharSequence adjustLabelCase(CharSequence label) {
|
|
|
|
if (isShiftedOrShiftLocked() && !TextUtils.isEmpty(label) && label.length() < 3
|
|
|
|
&& Character.isLowerCase(label.charAt(0))) {
|
|
|
|
return label.toString().toUpperCase(mId.mLocale);
|
|
|
|
}
|
|
|
|
return label;
|
2010-12-02 11:54:32 +00:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
/**
|
|
|
|
* Returns the indices of the keys that are closest to the given point.
|
|
|
|
* @param x the x-coordinate of the point
|
|
|
|
* @param y the y-coordinate of the point
|
|
|
|
* @return the array of integer indices for the nearest keys to the given point. If the given
|
|
|
|
* point is out of range, then an array of size zero is returned.
|
|
|
|
*/
|
|
|
|
public int[] getNearestKeys(int x, int y) {
|
2011-07-12 05:58:46 +00:00
|
|
|
return mProximityInfo.getNearestKeys(x, y);
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
2011-09-28 03:08:48 +00:00
|
|
|
|
|
|
|
public static String themeName(int themeId) {
|
|
|
|
// This should be aligned with theme-*.xml resource files' themeId attribute.
|
|
|
|
switch (themeId) {
|
|
|
|
case 0: return "Basic";
|
|
|
|
case 1: return "BasicHighContrast";
|
|
|
|
case 5: return "IceCreamSandwich";
|
|
|
|
case 6: return "Stone";
|
|
|
|
case 7: return "StoneBold";
|
|
|
|
case 8: return "GingerBread";
|
|
|
|
default: return null;
|
|
|
|
}
|
|
|
|
}
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|