2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2010-03-26 22:07:10 +00:00
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
2009-03-13 22:11:42 +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.latin;
|
|
|
|
|
2010-11-11 07:03:19 +00:00
|
|
|
import android.content.Context;
|
2010-08-20 06:35:02 +01:00
|
|
|
import android.content.SharedPreferences;
|
2009-10-12 21:48:35 +01:00
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.content.res.Resources;
|
2010-08-20 06:35:02 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2010-11-11 07:03:19 +00:00
|
|
|
import android.util.Log;
|
2010-08-20 06:35:02 +01:00
|
|
|
import android.view.InflateException;
|
2009-10-12 21:48:35 +01:00
|
|
|
|
2010-09-08 17:41:58 +01:00
|
|
|
import java.lang.ref.SoftReference;
|
2010-10-08 14:17:16 +01:00
|
|
|
import java.util.Arrays;
|
2010-09-06 06:50:50 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
2010-08-20 06:35:02 +01:00
|
|
|
public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceChangeListener {
|
2010-11-11 07:03:19 +00:00
|
|
|
private static final String TAG = "KeyboardSwitcher";
|
|
|
|
private static final boolean DEBUG = false;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2010-10-19 16:18:50 +01:00
|
|
|
public static final int MODE_TEXT = 0;
|
|
|
|
public static final int MODE_URL = 1;
|
|
|
|
public static final int MODE_EMAIL = 2;
|
|
|
|
public static final int MODE_IM = 3;
|
|
|
|
public static final int MODE_WEB = 4;
|
|
|
|
public static final int MODE_PHONE = 5;
|
|
|
|
|
2010-09-01 07:45:20 +01:00
|
|
|
public static final String DEFAULT_LAYOUT_ID = "4";
|
2010-09-02 05:48:48 +01:00
|
|
|
public static final String PREF_KEYBOARD_LAYOUT = "pref_keyboard_layout_20100902";
|
2010-08-20 06:35:02 +01:00
|
|
|
private static final int[] THEMES = new int [] {
|
|
|
|
R.layout.input_basic, R.layout.input_basic_highcontrast, R.layout.input_stone_normal,
|
2010-09-01 07:45:20 +01:00
|
|
|
R.layout.input_stone_bold, R.layout.input_gingerbread};
|
2010-08-20 06:35:02 +01:00
|
|
|
|
|
|
|
// Ids for each characters' color in the keyboard
|
|
|
|
private static final int CHAR_THEME_COLOR_WHITE = 0;
|
|
|
|
private static final int CHAR_THEME_COLOR_BLACK = 1;
|
|
|
|
|
|
|
|
// Tables which contains resource ids for each character theme color
|
2010-11-11 23:28:14 +00:00
|
|
|
private static final int[] KBD_PHONE = new int[] {
|
|
|
|
R.xml.kbd_phone, R.xml.kbd_phone_black
|
|
|
|
};
|
2010-08-20 06:35:02 +01:00
|
|
|
private static final int[] KBD_PHONE_SYMBOLS = new int[] {
|
2010-11-11 23:28:14 +00:00
|
|
|
R.xml.kbd_phone_symbols, R.xml.kbd_phone_symbols_black
|
|
|
|
};
|
2010-08-20 06:35:02 +01:00
|
|
|
private static final int[] KBD_SYMBOLS = new int[] {
|
2010-11-11 23:28:14 +00:00
|
|
|
R.xml.kbd_symbols, R.xml.kbd_symbols_black
|
|
|
|
};
|
2010-08-20 06:35:02 +01:00
|
|
|
private static final int[] KBD_SYMBOLS_SHIFT = new int[] {
|
2010-11-11 23:28:14 +00:00
|
|
|
R.xml.kbd_symbols_shift, R.xml.kbd_symbols_shift_black
|
|
|
|
};
|
|
|
|
private static final int[] KBD_QWERTY = new int[] {
|
|
|
|
R.xml.kbd_qwerty, R.xml.kbd_qwerty_black
|
|
|
|
};
|
2010-08-20 06:35:02 +01:00
|
|
|
|
2009-07-21 23:47:11 +01:00
|
|
|
private static final int SYMBOLS_MODE_STATE_NONE = 0;
|
|
|
|
private static final int SYMBOLS_MODE_STATE_BEGIN = 1;
|
|
|
|
private static final int SYMBOLS_MODE_STATE_SYMBOL = 2;
|
|
|
|
|
2010-09-21 08:55:18 +01:00
|
|
|
private LatinKeyboardView mInputView;
|
|
|
|
private final LatinIME mInputMethodService;
|
2010-11-11 07:03:19 +00:00
|
|
|
private final LanguageSwitcher mLanguageSwitcher;
|
2010-09-01 07:45:20 +01:00
|
|
|
|
2010-11-16 09:47:39 +00:00
|
|
|
private ShiftKeyState mShiftState = new ShiftKeyState();
|
2010-11-16 08:28:50 +00:00
|
|
|
private ModifierKeyState mSymbolKeyState = new ModifierKeyState();
|
2010-11-16 09:47:39 +00:00
|
|
|
|
2009-04-16 20:56:10 +01:00
|
|
|
private KeyboardId mSymbolsId;
|
|
|
|
private KeyboardId mSymbolsShiftedId;
|
|
|
|
|
|
|
|
private KeyboardId mCurrentId;
|
2010-11-11 07:03:19 +00:00
|
|
|
private final HashMap<KeyboardId, SoftReference<LatinKeyboard>> mKeyboardCache =
|
|
|
|
new HashMap<KeyboardId, SoftReference<LatinKeyboard>>();
|
2010-01-16 20:21:23 +00:00
|
|
|
|
2010-11-11 07:03:19 +00:00
|
|
|
private int mMode = MODE_TEXT; /* default value */
|
2009-03-13 22:11:42 +00:00
|
|
|
private int mImeOptions;
|
2009-04-16 20:56:10 +01:00
|
|
|
private boolean mIsSymbols;
|
2010-09-02 14:54:37 +01:00
|
|
|
/** mIsAutoCompletionActive indicates that auto completed word will be input instead of
|
|
|
|
* what user actually typed. */
|
|
|
|
private boolean mIsAutoCompletionActive;
|
2010-11-11 00:10:10 +00:00
|
|
|
private boolean mVoiceButtonEnabled;
|
|
|
|
private boolean mVoiceButtonOnPrimary;
|
2009-07-21 23:47:11 +01:00
|
|
|
private int mSymbolsModeState = SYMBOLS_MODE_STATE_NONE;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2010-09-15 05:37:52 +01:00
|
|
|
// Indicates whether or not we have the settings key
|
2010-09-21 08:55:18 +01:00
|
|
|
private boolean mHasSettingsKey;
|
2010-09-15 05:37:52 +01:00
|
|
|
private static final int SETTINGS_KEY_MODE_AUTO = R.string.settings_key_mode_auto;
|
2010-10-22 06:28:12 +01:00
|
|
|
private static final int SETTINGS_KEY_MODE_ALWAYS_SHOW =
|
|
|
|
R.string.settings_key_mode_always_show;
|
2010-11-02 09:55:20 +00:00
|
|
|
// NOTE: No need to have SETTINGS_KEY_MODE_ALWAYS_HIDE here because it's not being referred to
|
|
|
|
// in the source code now.
|
|
|
|
// Default is SETTINGS_KEY_MODE_AUTO.
|
|
|
|
private static final int DEFAULT_SETTINGS_KEY_MODE = SETTINGS_KEY_MODE_AUTO;
|
2010-09-15 05:37:52 +01:00
|
|
|
|
2010-08-20 06:35:02 +01:00
|
|
|
private int mLayoutId;
|
|
|
|
|
2010-11-11 07:03:19 +00:00
|
|
|
public KeyboardSwitcher(LatinIME ims, LanguageSwitcher languageSwitcher) {
|
2010-09-15 05:37:52 +01:00
|
|
|
mInputMethodService = ims;
|
2010-11-11 07:03:19 +00:00
|
|
|
mLanguageSwitcher = languageSwitcher;
|
2010-08-20 06:35:02 +01:00
|
|
|
|
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ims);
|
|
|
|
mLayoutId = Integer.valueOf(prefs.getString(PREF_KEYBOARD_LAYOUT, DEFAULT_LAYOUT_ID));
|
|
|
|
prefs.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
}
|
|
|
|
|
2010-10-19 16:18:50 +01:00
|
|
|
private void makeSymbolsKeyboardIds() {
|
2010-11-11 07:03:19 +00:00
|
|
|
final Locale locale = mLanguageSwitcher.getInputLocale();
|
|
|
|
final int orientation = mInputMethodService.getResources().getConfiguration().orientation;
|
|
|
|
final int mode = mMode;
|
|
|
|
final int colorScheme = getCharColorId();
|
|
|
|
final boolean hasSettingsKey = mHasSettingsKey;
|
|
|
|
final boolean hasVoiceKey = mVoiceButtonEnabled && !mVoiceButtonOnPrimary;
|
|
|
|
final int imeOptions = mImeOptions;
|
2010-11-11 23:28:14 +00:00
|
|
|
mSymbolsId = new KeyboardId(locale, orientation, mode,
|
2010-11-11 07:03:19 +00:00
|
|
|
KBD_SYMBOLS, colorScheme, hasSettingsKey, hasVoiceKey, imeOptions, true);
|
2010-11-11 23:28:14 +00:00
|
|
|
mSymbolsShiftedId = new KeyboardId(locale, orientation, mode,
|
2010-11-11 07:03:19 +00:00
|
|
|
KBD_SYMBOLS_SHIFT, colorScheme, hasSettingsKey, hasVoiceKey, imeOptions, true);
|
2009-04-16 20:56:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the parameters necessary to construct a new LatinKeyboard,
|
|
|
|
* which also serve as a unique identifier for each keyboard type.
|
|
|
|
*/
|
2010-11-11 23:28:14 +00:00
|
|
|
public static class KeyboardId {
|
2010-11-11 02:57:33 +00:00
|
|
|
public final Locale mLocale;
|
|
|
|
public final int mOrientation;
|
|
|
|
public final int mMode;
|
|
|
|
public final int[] mXmlArray;
|
|
|
|
public final int mColorScheme;
|
2010-11-11 07:03:19 +00:00
|
|
|
public final boolean mHasSettingsKey;
|
|
|
|
public final boolean mHasVoiceKey;
|
|
|
|
public final int mImeOptions;
|
2010-11-11 02:57:33 +00:00
|
|
|
public final boolean mEnableShiftLock;
|
2009-04-16 20:56:10 +01:00
|
|
|
|
2010-10-08 14:17:16 +01:00
|
|
|
private final int mHashCode;
|
|
|
|
|
2010-11-11 23:28:14 +00:00
|
|
|
public KeyboardId(Locale locale, int orientation, int mode,
|
2010-11-11 07:03:19 +00:00
|
|
|
int[] xmlArray, int colorScheme, boolean hasSettingsKey, boolean hasVoiceKey,
|
|
|
|
int imeOptions, boolean enableShiftLock) {
|
2010-11-11 02:57:33 +00:00
|
|
|
this.mLocale = locale;
|
|
|
|
this.mOrientation = orientation;
|
|
|
|
this.mMode = mode;
|
|
|
|
this.mXmlArray = xmlArray;
|
|
|
|
this.mColorScheme = colorScheme;
|
2010-11-11 07:03:19 +00:00
|
|
|
this.mHasSettingsKey = hasSettingsKey;
|
|
|
|
this.mHasVoiceKey = hasVoiceKey;
|
|
|
|
this.mImeOptions = imeOptions;
|
2010-11-11 02:57:33 +00:00
|
|
|
this.mEnableShiftLock = enableShiftLock;
|
2010-10-08 14:17:16 +01:00
|
|
|
|
|
|
|
this.mHashCode = Arrays.hashCode(new Object[] {
|
2010-11-11 07:03:19 +00:00
|
|
|
locale,
|
|
|
|
orientation,
|
|
|
|
mode,
|
|
|
|
xmlArray,
|
|
|
|
colorScheme,
|
|
|
|
hasSettingsKey,
|
|
|
|
hasVoiceKey,
|
|
|
|
imeOptions,
|
|
|
|
enableShiftLock,
|
2010-10-08 14:17:16 +01:00
|
|
|
});
|
2009-04-16 20:56:10 +01:00
|
|
|
}
|
|
|
|
|
2010-11-11 23:28:14 +00:00
|
|
|
public int getXmlId() {
|
|
|
|
return mXmlArray[mColorScheme];
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAlphabetMode() {
|
|
|
|
return mXmlArray == KBD_QWERTY;
|
|
|
|
}
|
|
|
|
|
2010-08-20 06:35:02 +01:00
|
|
|
@Override
|
2009-04-16 20:56:10 +01:00
|
|
|
public boolean equals(Object other) {
|
|
|
|
return other instanceof KeyboardId && equals((KeyboardId) other);
|
|
|
|
}
|
|
|
|
|
2010-09-08 17:41:58 +01:00
|
|
|
private boolean equals(KeyboardId other) {
|
2010-11-11 02:57:33 +00:00
|
|
|
return other.mLocale.equals(this.mLocale)
|
|
|
|
&& other.mOrientation == this.mOrientation
|
|
|
|
&& other.mMode == this.mMode
|
|
|
|
&& other.mXmlArray == this.mXmlArray
|
|
|
|
&& other.mColorScheme == this.mColorScheme
|
2010-11-11 07:03:19 +00:00
|
|
|
&& other.mHasSettingsKey == this.mHasSettingsKey
|
|
|
|
&& other.mHasVoiceKey == this.mHasVoiceKey
|
|
|
|
&& other.mImeOptions == this.mImeOptions
|
2010-11-11 02:57:33 +00:00
|
|
|
&& other.mEnableShiftLock == this.mEnableShiftLock;
|
2009-04-16 20:56:10 +01:00
|
|
|
}
|
|
|
|
|
2010-08-20 06:35:02 +01:00
|
|
|
@Override
|
2009-04-16 20:56:10 +01:00
|
|
|
public int hashCode() {
|
2010-10-08 14:17:16 +01:00
|
|
|
return mHashCode;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-11-11 07:03:19 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2010-11-11 23:28:14 +00:00
|
|
|
return String.format("[%s %s %5s imeOptions=0x%08x xml=0x%08x %s%s%s%s]",
|
|
|
|
mLocale,
|
|
|
|
(mOrientation == 1 ? "port" : "land"),
|
|
|
|
modeName(mMode),
|
|
|
|
mImeOptions,
|
|
|
|
mXmlArray[0],
|
|
|
|
(mColorScheme == CHAR_THEME_COLOR_WHITE ? "white" : "black"),
|
|
|
|
(mHasSettingsKey ? " hasSettingsKey" : ""),
|
|
|
|
(mHasVoiceKey ? " hasVoiceKey" : ""),
|
|
|
|
(mEnableShiftLock ? " enableShiftLock" : ""));
|
2010-11-11 07:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static String modeName(int mode) {
|
2010-11-11 23:28:14 +00:00
|
|
|
switch (mode) {
|
|
|
|
case MODE_TEXT: return "text";
|
|
|
|
case MODE_URL: return "url";
|
|
|
|
case MODE_EMAIL: return "email";
|
|
|
|
case MODE_IM: return "im";
|
|
|
|
case MODE_WEB: return "web";
|
|
|
|
case MODE_PHONE: return "phone";
|
2010-11-11 07:03:19 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2010-11-11 07:03:19 +00:00
|
|
|
private boolean hasVoiceKey(boolean isSymbols) {
|
2010-11-11 00:10:10 +00:00
|
|
|
return mVoiceButtonEnabled && (isSymbols != mVoiceButtonOnPrimary);
|
2010-01-16 20:21:23 +00:00
|
|
|
}
|
|
|
|
|
2010-11-11 07:03:19 +00:00
|
|
|
public void loadKeyboard(int mode, int imeOptions, boolean voiceButtonEnabled,
|
2010-11-11 00:10:10 +00:00
|
|
|
boolean voiceButtonOnPrimary) {
|
2009-09-01 22:05:19 +01:00
|
|
|
mSymbolsModeState = SYMBOLS_MODE_STATE_NONE;
|
2010-08-20 06:35:02 +01:00
|
|
|
try {
|
2010-11-11 07:03:19 +00:00
|
|
|
loadKeyboardInternal(mode, imeOptions, voiceButtonEnabled, voiceButtonOnPrimary,
|
2010-11-11 00:10:10 +00:00
|
|
|
false);
|
2010-08-20 06:35:02 +01:00
|
|
|
} catch (RuntimeException e) {
|
2010-11-11 23:28:14 +00:00
|
|
|
Log.w(TAG, e);
|
2010-10-19 16:18:50 +01:00
|
|
|
LatinImeLogger.logOnException(mode + "," + imeOptions, e);
|
2010-08-20 06:35:02 +01:00
|
|
|
}
|
2009-04-16 20:56:10 +01:00
|
|
|
}
|
|
|
|
|
2010-11-11 07:03:19 +00:00
|
|
|
private void loadKeyboardInternal(int mode, int imeOptions, boolean voiceButtonEnabled,
|
2010-11-11 00:10:10 +00:00
|
|
|
boolean voiceButtonOnPrimary, boolean isSymbols) {
|
2010-02-24 03:01:43 +00:00
|
|
|
if (mInputView == null) return;
|
2010-11-11 07:03:19 +00:00
|
|
|
mInputView.setPreviewEnabled(mInputMethodService.getPopupOn());
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
mMode = mode;
|
|
|
|
mImeOptions = imeOptions;
|
2010-11-11 07:03:19 +00:00
|
|
|
mVoiceButtonEnabled = voiceButtonEnabled;
|
|
|
|
mVoiceButtonOnPrimary = voiceButtonOnPrimary;
|
2009-04-28 19:38:42 +01:00
|
|
|
mIsSymbols = isSymbols;
|
2010-11-11 07:03:19 +00:00
|
|
|
// Update the settings key state because number of enabled IMEs could have been changed
|
|
|
|
mHasSettingsKey = getSettingsKeyMode(
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(mInputMethodService),
|
|
|
|
mInputMethodService);
|
|
|
|
makeSymbolsKeyboardIds();
|
2010-01-16 20:21:23 +00:00
|
|
|
|
2009-04-16 20:56:10 +01:00
|
|
|
KeyboardId id = getKeyboardId(mode, imeOptions, isSymbols);
|
2010-11-11 07:03:19 +00:00
|
|
|
LatinKeyboard keyboard = getKeyboard(id);
|
2009-04-16 20:56:10 +01:00
|
|
|
|
|
|
|
if (mode == MODE_PHONE) {
|
|
|
|
mInputView.setPhoneKeyboard(keyboard);
|
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentId = id;
|
|
|
|
mInputView.setKeyboard(keyboard);
|
|
|
|
}
|
|
|
|
|
|
|
|
private LatinKeyboard getKeyboard(KeyboardId id) {
|
2010-11-11 07:03:19 +00:00
|
|
|
final SoftReference<LatinKeyboard> ref = mKeyboardCache.get(id);
|
2010-09-08 17:41:58 +01:00
|
|
|
LatinKeyboard keyboard = (ref == null) ? null : ref.get();
|
|
|
|
if (keyboard == null) {
|
2010-11-11 07:03:19 +00:00
|
|
|
final Resources res = mInputMethodService.getResources();
|
|
|
|
final Configuration conf = res.getConfiguration();
|
|
|
|
final Locale saveLocale = conf.locale;
|
|
|
|
conf.locale = mLanguageSwitcher.getInputLocale();
|
2010-11-11 02:57:33 +00:00
|
|
|
res.updateConfiguration(conf, null);
|
2010-11-11 07:03:19 +00:00
|
|
|
|
2010-11-11 23:28:14 +00:00
|
|
|
final int xml = id.getXmlId();
|
|
|
|
keyboard = new LatinKeyboard(mInputMethodService, id);
|
2010-11-11 07:03:19 +00:00
|
|
|
keyboard.setVoiceMode(
|
|
|
|
hasVoiceKey(xml == R.xml.kbd_symbols || xml == R.xml.kbd_symbols_black),
|
|
|
|
mVoiceButtonEnabled);
|
|
|
|
keyboard.setLanguageSwitcher(mLanguageSwitcher);
|
|
|
|
keyboard.setImeOptions(res, id.mMode, id.mImeOptions);
|
|
|
|
keyboard.setColorOfSymbolIcons(isBlackSym(id.mColorScheme));
|
2009-10-22 22:51:39 +01:00
|
|
|
|
2009-04-16 20:56:10 +01:00
|
|
|
if (id.mEnableShiftLock) {
|
|
|
|
keyboard.enableShiftLock();
|
|
|
|
}
|
2010-11-11 07:03:19 +00:00
|
|
|
|
|
|
|
mKeyboardCache.put(id, new SoftReference<LatinKeyboard>(keyboard));
|
|
|
|
if (DEBUG)
|
|
|
|
Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": "
|
|
|
|
+ ((ref == null) ? "LOAD" : "GCed") + " id=" + id);
|
2009-10-12 21:48:35 +01:00
|
|
|
|
|
|
|
conf.locale = saveLocale;
|
2010-11-11 02:57:33 +00:00
|
|
|
res.updateConfiguration(conf, null);
|
2010-11-11 07:03:19 +00:00
|
|
|
} else if (DEBUG) {
|
|
|
|
Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": HIT id=" + id);
|
2009-04-16 20:56:10 +01:00
|
|
|
}
|
2010-11-11 07:03:19 +00:00
|
|
|
|
|
|
|
keyboard.onAutoCompletionStateChanged(mIsAutoCompletionActive);
|
|
|
|
keyboard.setShifted(false);
|
2010-09-08 17:41:58 +01:00
|
|
|
return keyboard;
|
2009-04-16 20:56:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private KeyboardId getKeyboardId(int mode, int imeOptions, boolean isSymbols) {
|
2010-11-11 07:03:19 +00:00
|
|
|
final boolean hasVoiceKey = hasVoiceKey(isSymbols);
|
2010-10-19 16:18:50 +01:00
|
|
|
final int charColorId = getCharColorId();
|
2010-11-11 02:57:33 +00:00
|
|
|
final int[] xmlArray;
|
2010-10-19 16:18:50 +01:00
|
|
|
final boolean enableShiftLock;
|
|
|
|
|
|
|
|
if (isSymbols) {
|
2010-11-11 02:57:33 +00:00
|
|
|
xmlArray = mode == MODE_PHONE ? KBD_PHONE_SYMBOLS : KBD_SYMBOLS;
|
2010-10-19 16:18:50 +01:00
|
|
|
enableShiftLock = false;
|
|
|
|
} else { // QWERTY
|
2010-11-11 02:57:33 +00:00
|
|
|
xmlArray = mode == MODE_PHONE ? KBD_PHONE : KBD_QWERTY;
|
2010-10-19 16:18:50 +01:00
|
|
|
enableShiftLock = mode == MODE_PHONE ? false : true;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-11-11 07:03:19 +00:00
|
|
|
final int orientation = mInputMethodService.getResources().getConfiguration().orientation;
|
|
|
|
final Locale locale = mLanguageSwitcher.getInputLocale();
|
2010-11-11 23:28:14 +00:00
|
|
|
return new KeyboardId(locale, orientation, mode, xmlArray,
|
2010-11-11 07:03:19 +00:00
|
|
|
charColorId, mHasSettingsKey, hasVoiceKey, imeOptions, enableShiftLock);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2010-09-21 08:55:18 +01:00
|
|
|
public int getKeyboardMode() {
|
2009-03-13 22:11:42 +00:00
|
|
|
return mMode;
|
|
|
|
}
|
2010-11-13 08:01:13 +00:00
|
|
|
|
2010-09-21 08:55:18 +01:00
|
|
|
public boolean isAlphabetMode() {
|
2010-11-11 23:28:14 +00:00
|
|
|
return mCurrentId != null && mCurrentId.isAlphabetMode();
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2010-11-13 08:01:13 +00:00
|
|
|
public boolean isInputViewShown() {
|
|
|
|
return mInputView != null && mInputView.isShown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isKeyboardAvailable() {
|
|
|
|
return mInputView != null && mInputView.getLatinKeyboard() != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPreferredLetters(int[] frequencies) {
|
|
|
|
LatinKeyboard latinKeyboard;
|
|
|
|
if (mInputView != null && (latinKeyboard = mInputView.getLatinKeyboard()) != null)
|
|
|
|
latinKeyboard.setPreferredLetters(frequencies);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void keyReleased() {
|
|
|
|
LatinKeyboard latinKeyboard;
|
|
|
|
if (mInputView != null && (latinKeyboard = mInputView.getLatinKeyboard()) != null)
|
|
|
|
latinKeyboard.keyReleased();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isShifted() {
|
|
|
|
LatinKeyboard latinKeyboard;
|
|
|
|
return mInputView != null && (latinKeyboard = mInputView.getLatinKeyboard()) != null
|
|
|
|
&& latinKeyboard.isShifted();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isShiftLocked() {
|
|
|
|
LatinKeyboard latinKeyboard;
|
|
|
|
return mInputView != null && (latinKeyboard = mInputView.getLatinKeyboard()) != null
|
|
|
|
&& latinKeyboard.isShiftLocked();
|
|
|
|
}
|
|
|
|
|
2010-09-21 08:55:18 +01:00
|
|
|
public void setShifted(boolean shifted) {
|
2010-10-26 11:04:48 +01:00
|
|
|
if (mInputView == null) return;
|
|
|
|
LatinKeyboard latinKeyboard = mInputView.getLatinKeyboard();
|
|
|
|
if (latinKeyboard == null) return;
|
|
|
|
if (latinKeyboard.setShifted(shifted)) {
|
|
|
|
mInputView.invalidateAllKeys();
|
2010-08-20 06:35:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-21 08:55:18 +01:00
|
|
|
public void setShiftLocked(boolean shiftLocked) {
|
2010-10-26 11:04:48 +01:00
|
|
|
if (mInputView == null) return;
|
|
|
|
mInputView.setShiftLocked(shiftLocked);
|
2010-08-20 06:35:02 +01:00
|
|
|
}
|
|
|
|
|
2010-11-16 09:47:39 +00:00
|
|
|
public void onPressShift() {
|
|
|
|
mShiftState.onPress();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onPressShiftOnShifted() {
|
|
|
|
mShiftState.onPressOnShifted();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onReleaseShift() {
|
|
|
|
mShiftState.onRelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isShiftMomentary() {
|
|
|
|
return mShiftState.isMomentary();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isShiftPressingOnShifted() {
|
|
|
|
return mShiftState.isPressingOnShifted();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isShiftIgnoring() {
|
|
|
|
return mShiftState.isIgnoring();
|
|
|
|
}
|
|
|
|
|
2010-11-16 08:28:50 +00:00
|
|
|
public void onPressSymbol() {
|
|
|
|
mSymbolKeyState.onPress();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onReleaseSymbol() {
|
|
|
|
mSymbolKeyState.onRelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSymbolMomentary() {
|
|
|
|
return mSymbolKeyState.isMomentary();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onOtherKeyPressed() {
|
2010-11-16 09:47:39 +00:00
|
|
|
mShiftState.onOtherKeyPressed();
|
2010-11-16 08:28:50 +00:00
|
|
|
mSymbolKeyState.onOtherKeyPressed();
|
|
|
|
}
|
|
|
|
|
2010-09-21 08:55:18 +01:00
|
|
|
public void toggleShift() {
|
2010-10-08 14:17:16 +01:00
|
|
|
if (isAlphabetMode())
|
|
|
|
return;
|
2010-11-11 07:03:19 +00:00
|
|
|
final LatinKeyboard keyboard;
|
2010-10-08 14:17:16 +01:00
|
|
|
if (mCurrentId.equals(mSymbolsId) || !mCurrentId.equals(mSymbolsShiftedId)) {
|
2009-04-16 20:56:10 +01:00
|
|
|
mCurrentId = mSymbolsShiftedId;
|
2010-11-11 07:03:19 +00:00
|
|
|
keyboard = getKeyboard(mCurrentId);
|
2010-09-06 06:50:50 +01:00
|
|
|
// Symbol shifted keyboard has an ALT key that has a caps lock style indicator. To
|
|
|
|
// enable the indicator, we need to call enableShiftLock() and setShiftLocked(true).
|
|
|
|
// Thus we can keep the ALT key's Key.on value true while LatinKey.onRelease() is
|
|
|
|
// called.
|
2010-11-11 07:03:19 +00:00
|
|
|
keyboard.setShiftLocked(true);
|
2010-10-08 14:17:16 +01:00
|
|
|
} else {
|
2009-04-16 20:56:10 +01:00
|
|
|
mCurrentId = mSymbolsId;
|
2010-11-11 07:03:19 +00:00
|
|
|
keyboard = getKeyboard(mCurrentId);
|
2010-09-06 06:50:50 +01:00
|
|
|
// Symbol keyboard has an ALT key that has a caps lock style indicator. To disable the
|
|
|
|
// indicator, we need to call enableShiftLock() and setShiftLocked(false).
|
2010-11-11 07:03:19 +00:00
|
|
|
keyboard.setShifted(false);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-11-11 07:03:19 +00:00
|
|
|
mInputView.setKeyboard(keyboard);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2010-09-21 08:55:18 +01:00
|
|
|
public void toggleSymbols() {
|
2010-11-11 07:03:19 +00:00
|
|
|
loadKeyboardInternal(mMode, mImeOptions, mVoiceButtonEnabled, mVoiceButtonOnPrimary,
|
2010-11-11 00:10:10 +00:00
|
|
|
!mIsSymbols);
|
2010-10-19 16:18:50 +01:00
|
|
|
if (mIsSymbols) {
|
2009-07-21 23:47:11 +01:00
|
|
|
mSymbolsModeState = SYMBOLS_MODE_STATE_BEGIN;
|
|
|
|
} else {
|
|
|
|
mSymbolsModeState = SYMBOLS_MODE_STATE_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-06 06:26:46 +01:00
|
|
|
public boolean hasDistinctMultitouch() {
|
|
|
|
return mInputView != null && mInputView.hasDistinctMultitouch();
|
|
|
|
}
|
|
|
|
|
2009-07-21 23:47:11 +01:00
|
|
|
/**
|
|
|
|
* Updates state machine to figure out when to automatically switch back to alpha mode.
|
|
|
|
* Returns true if the keyboard needs to switch back
|
|
|
|
*/
|
2010-09-21 08:55:18 +01:00
|
|
|
public boolean onKey(int key) {
|
2009-07-29 00:33:36 +01:00
|
|
|
// Switch back to alpha mode if user types one or more non-space/enter characters
|
|
|
|
// followed by a space/enter
|
2009-07-21 23:47:11 +01:00
|
|
|
switch (mSymbolsModeState) {
|
|
|
|
case SYMBOLS_MODE_STATE_BEGIN:
|
2009-07-29 00:33:36 +01:00
|
|
|
if (key != LatinIME.KEYCODE_SPACE && key != LatinIME.KEYCODE_ENTER && key > 0) {
|
|
|
|
mSymbolsModeState = SYMBOLS_MODE_STATE_SYMBOL;
|
|
|
|
}
|
2009-07-21 23:47:11 +01:00
|
|
|
break;
|
|
|
|
case SYMBOLS_MODE_STATE_SYMBOL:
|
2009-07-29 00:33:36 +01:00
|
|
|
if (key == LatinIME.KEYCODE_ENTER || key == LatinIME.KEYCODE_SPACE) return true;
|
2009-07-21 23:47:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-08-20 06:35:02 +01:00
|
|
|
|
|
|
|
public LatinKeyboardView getInputView() {
|
|
|
|
return mInputView;
|
|
|
|
}
|
|
|
|
|
2010-11-11 07:03:19 +00:00
|
|
|
public void loadKeyboardView() {
|
|
|
|
loadKeyboardViewInternal(mLayoutId, true);
|
2010-08-20 06:35:02 +01:00
|
|
|
}
|
|
|
|
|
2010-11-11 07:03:19 +00:00
|
|
|
private void loadKeyboardViewInternal(int newLayout, boolean forceReset) {
|
2010-08-20 06:35:02 +01:00
|
|
|
if (mLayoutId != newLayout || mInputView == null || forceReset) {
|
|
|
|
if (mInputView != null) {
|
|
|
|
mInputView.closing();
|
|
|
|
}
|
|
|
|
if (THEMES.length <= newLayout) {
|
|
|
|
newLayout = Integer.valueOf(DEFAULT_LAYOUT_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
LatinIMEUtil.GCUtils.getInstance().reset();
|
|
|
|
boolean tryGC = true;
|
|
|
|
for (int i = 0; i < LatinIMEUtil.GCUtils.GC_TRY_LOOP_MAX && tryGC; ++i) {
|
|
|
|
try {
|
|
|
|
mInputView = (LatinKeyboardView) mInputMethodService.getLayoutInflater(
|
|
|
|
).inflate(THEMES[newLayout], null);
|
|
|
|
tryGC = false;
|
|
|
|
} catch (OutOfMemoryError e) {
|
|
|
|
tryGC = LatinIMEUtil.GCUtils.getInstance().tryGCOrWait(
|
|
|
|
mLayoutId + "," + newLayout, e);
|
|
|
|
} catch (InflateException e) {
|
|
|
|
tryGC = LatinIMEUtil.GCUtils.getInstance().tryGCOrWait(
|
|
|
|
mLayoutId + "," + newLayout, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mInputView.setOnKeyboardActionListener(mInputMethodService);
|
|
|
|
mLayoutId = newLayout;
|
|
|
|
}
|
|
|
|
mInputMethodService.mHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
if (mInputView != null) {
|
|
|
|
mInputMethodService.setInputView(mInputView);
|
|
|
|
}
|
|
|
|
mInputMethodService.updateInputViewShown();
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
|
|
|
if (PREF_KEYBOARD_LAYOUT.equals(key)) {
|
2010-11-11 07:03:19 +00:00
|
|
|
final int layoutId = Integer.valueOf(
|
|
|
|
sharedPreferences.getString(key, DEFAULT_LAYOUT_ID));
|
|
|
|
loadKeyboardViewInternal(layoutId, false);
|
2010-09-15 05:37:52 +01:00
|
|
|
} else if (LatinIMESettings.PREF_SETTINGS_KEY.equals(key)) {
|
2010-11-11 07:03:19 +00:00
|
|
|
mHasSettingsKey = getSettingsKeyMode(sharedPreferences, mInputMethodService);
|
|
|
|
loadKeyboardViewInternal(mLayoutId, true);
|
2010-08-20 06:35:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-11 07:03:19 +00:00
|
|
|
public boolean isBlackSym() {
|
2010-09-15 05:37:52 +01:00
|
|
|
if (mInputView != null && mInputView.getSymbolColorScheme() == 1) {
|
2010-08-20 06:35:02 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-11 07:03:19 +00:00
|
|
|
private boolean isBlackSym(int colorScheme) {
|
|
|
|
return colorScheme == CHAR_THEME_COLOR_BLACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getCharColorId() {
|
2010-08-20 06:35:02 +01:00
|
|
|
if (isBlackSym()) {
|
|
|
|
return CHAR_THEME_COLOR_BLACK;
|
|
|
|
} else {
|
|
|
|
return CHAR_THEME_COLOR_WHITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-02 14:54:37 +01:00
|
|
|
public void onAutoCompletionStateChanged(boolean isAutoCompletion) {
|
|
|
|
if (isAutoCompletion != mIsAutoCompletionActive) {
|
|
|
|
LatinKeyboardView keyboardView = getInputView();
|
|
|
|
mIsAutoCompletionActive = isAutoCompletion;
|
|
|
|
keyboardView.invalidateKey(((LatinKeyboard) keyboardView.getKeyboard())
|
|
|
|
.onAutoCompletionStateChanged(isAutoCompletion));
|
|
|
|
}
|
|
|
|
}
|
2010-09-15 05:37:52 +01:00
|
|
|
|
2010-11-11 07:03:19 +00:00
|
|
|
private static boolean getSettingsKeyMode(SharedPreferences prefs, Context context) {
|
|
|
|
Resources resources = context.getResources();
|
2010-10-22 06:28:12 +01:00
|
|
|
final boolean showSettingsKeyOption = resources.getBoolean(
|
|
|
|
R.bool.config_enable_show_settings_key_option);
|
2010-11-02 09:55:20 +00:00
|
|
|
if (showSettingsKeyOption) {
|
|
|
|
final String settingsKeyMode = prefs.getString(LatinIMESettings.PREF_SETTINGS_KEY,
|
|
|
|
resources.getString(DEFAULT_SETTINGS_KEY_MODE));
|
|
|
|
// We show the settings key when 1) SETTINGS_KEY_MODE_ALWAYS_SHOW or
|
|
|
|
// 2) SETTINGS_KEY_MODE_AUTO and there are two or more enabled IMEs on the system
|
|
|
|
if (settingsKeyMode.equals(resources.getString(SETTINGS_KEY_MODE_ALWAYS_SHOW))
|
|
|
|
|| (settingsKeyMode.equals(resources.getString(SETTINGS_KEY_MODE_AUTO))
|
2010-11-11 07:03:19 +00:00
|
|
|
&& LatinIMEUtil.hasMultipleEnabledIMEs(context))) {
|
|
|
|
return true;
|
2010-11-02 09:55:20 +00:00
|
|
|
}
|
2010-09-15 05:37:52 +01:00
|
|
|
}
|
2010-11-11 07:03:19 +00:00
|
|
|
return false;
|
2010-09-15 05:37:52 +01:00
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|