diff options
author | 2010-12-02 20:54:32 +0900 | |
---|---|---|
committer | 2010-12-02 23:03:59 +0900 | |
commit | 571bdb401f670b92bd7710a12a990cb65a99b7d3 (patch) | |
tree | b0d50937339cfea2caca8311dd2c0d5b4ec82e5d /java/src/com/android/inputmethod/latin | |
parent | 26dae3f4e8ffd0f25b78c58598752cd393419bcc (diff) | |
download | latinime-571bdb401f670b92bd7710a12a990cb65a99b7d3.tar.gz latinime-571bdb401f670b92bd7710a12a990cb65a99b7d3.tar.xz latinime-571bdb401f670b92bd7710a12a990cb65a99b7d3.zip |
Refactor LatinKeyboard and LatinKey
This change also moves KeyboardSwitcher and related classes to
com.android.inputmethod.keyboard package.
Multi project change with Ib23017b2
Change-Id: Id7d54b24615fe22ebb540ca847549909e346ee24
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
8 files changed, 50 insertions, 844 deletions
diff --git a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java deleted file mode 100644 index e8487e798..000000000 --- a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java +++ /dev/null @@ -1,644 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * 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; - -import com.android.inputmethod.keyboard.KeyboardView; -import com.android.inputmethod.keyboard.KeyboardId; -import com.android.inputmethod.keyboard.LatinKeyboard; -import com.android.inputmethod.keyboard.LatinKeyboardView; - -import android.content.Context; -import android.content.SharedPreferences; -import android.content.res.Resources; -import android.util.Log; -import android.view.InflateException; -import android.view.inputmethod.InputMethodManager; - -import java.lang.ref.SoftReference; -import java.util.HashMap; -import java.util.Locale; - -public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceChangeListener { - private static final String TAG = "KeyboardSwitcher"; - private static final boolean DEBUG = false; - public static final boolean DEBUG_STATE = false; - - // Changing DEFAULT_LAYOUT_ID also requires prefs_for_debug.xml to be matched with. - public static final String DEFAULT_LAYOUT_ID = "5"; - public static final String PREF_KEYBOARD_LAYOUT = "pref_keyboard_layout_20100902"; - private static final int[] THEMES = new int [] { - R.layout.input_basic, - R.layout.input_basic_highcontrast, - R.layout.input_stone_normal, - R.layout.input_stone_bold, - R.layout.input_gingerbread, - R.layout.input_honeycomb, // DEFAULT_LAYOUT_ID - }; - - 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; - - private SubtypeSwitcher mSubtypeSwitcher; - private SharedPreferences mPrefs; - - private LatinKeyboardView mInputView; - private LatinIME mInputMethodService; - - private ShiftKeyState mShiftKeyState = new ShiftKeyState("Shift"); - private ModifierKeyState mSymbolKeyState = new ModifierKeyState("Symbol"); - - private KeyboardId mSymbolsId; - private KeyboardId mSymbolsShiftedId; - - private KeyboardId mCurrentId; - private final HashMap<KeyboardId, SoftReference<LatinKeyboard>> mKeyboardCache = - new HashMap<KeyboardId, SoftReference<LatinKeyboard>>(); - - private int mMode = KeyboardId.MODE_TEXT; /* default value */ - private int mImeOptions; - private boolean mIsSymbols; - /** mIsAutoCompletionActive indicates that auto completed word will be input instead of - * what user actually typed. */ - private boolean mIsAutoCompletionActive; - private boolean mVoiceKeyEnabled; - private boolean mVoiceButtonOnPrimary; - private int mSymbolsModeState = SYMBOLS_MODE_STATE_NONE; - - // Indicates whether or not we have the settings key - private boolean mHasSettingsKey; - private static final int SETTINGS_KEY_MODE_AUTO = R.string.settings_key_mode_auto; - private static final int SETTINGS_KEY_MODE_ALWAYS_SHOW = - R.string.settings_key_mode_always_show; - // 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; - - private int mLayoutId; - - private static final KeyboardSwitcher sInstance = new KeyboardSwitcher(); - - public static KeyboardSwitcher getInstance() { - return sInstance; - } - - private KeyboardSwitcher() { - } - - public static void init(LatinIME ims, SharedPreferences prefs) { - sInstance.mInputMethodService = ims; - sInstance.mPrefs = prefs; - sInstance.mSubtypeSwitcher = SubtypeSwitcher.getInstance(); - - sInstance.mLayoutId = Integer.valueOf( - prefs.getString(PREF_KEYBOARD_LAYOUT, DEFAULT_LAYOUT_ID)); - prefs.registerOnSharedPreferenceChangeListener(sInstance); - } - - private void makeSymbolsKeyboardIds() { - final Locale locale = mSubtypeSwitcher.getInputLocale(); - final int orientation = mInputMethodService.getResources().getConfiguration().orientation; - final int mode = mMode; - final int colorScheme = getColorScheme(); - final boolean hasSettingsKey = mHasSettingsKey; - final boolean voiceKeyEnabled = mVoiceKeyEnabled; - final boolean hasVoiceKey = voiceKeyEnabled && !mVoiceButtonOnPrimary; - final int imeOptions = mImeOptions; - // Note: This comment is only applied for phone number keyboard layout. - // On non-xlarge device, "@integer/key_switch_alpha_symbol" key code is used to switch - // between "phone keyboard" and "phone symbols keyboard". But on xlarge device, - // "@integer/key_shift" key code is used for that purpose in order to properly display - // "more" and "locked more" key labels. To achieve these behavior, we should initialize - // mSymbolsId and mSymbolsShiftedId to "phone keyboard" and "phone symbols keyboard" - // respectively here for xlarge device's layout switching. - mSymbolsId = new KeyboardId(locale, orientation, mode, - mode == KeyboardId.MODE_PHONE ? R.xml.kbd_phone : R.xml.kbd_symbols, - colorScheme, hasSettingsKey, voiceKeyEnabled, hasVoiceKey, imeOptions, true); - mSymbolsShiftedId = new KeyboardId(locale, orientation, mode, - mode == KeyboardId.MODE_PHONE ? R.xml.kbd_phone_symbols : R.xml.kbd_symbols_shift, - colorScheme, hasSettingsKey, voiceKeyEnabled, hasVoiceKey, imeOptions, true); - } - - private boolean hasVoiceKey(boolean isSymbols) { - return mVoiceKeyEnabled && (isSymbols != mVoiceButtonOnPrimary); - } - - public void loadKeyboard(int mode, int imeOptions, boolean voiceKeyEnabled, - boolean voiceButtonOnPrimary) { - mSymbolsModeState = SYMBOLS_MODE_STATE_NONE; - try { - loadKeyboardInternal(mode, imeOptions, voiceKeyEnabled, voiceButtonOnPrimary, - false); - } catch (RuntimeException e) { - Log.w(TAG, e); - LatinImeLogger.logOnException(mode + "," + imeOptions, e); - } - } - - private void loadKeyboardInternal(int mode, int imeOptions, boolean voiceButtonEnabled, - boolean voiceButtonOnPrimary, boolean isSymbols) { - if (mInputView == null) return; - mInputView.setPreviewEnabled(mInputMethodService.getPopupOn()); - - mMode = mode; - mImeOptions = imeOptions; - mVoiceKeyEnabled = voiceButtonEnabled; - mVoiceButtonOnPrimary = voiceButtonOnPrimary; - mIsSymbols = isSymbols; - // Update the settings key state because number of enabled IMEs could have been changed - mHasSettingsKey = getSettingsKeyMode(mPrefs, mInputMethodService); - makeSymbolsKeyboardIds(); - - KeyboardId id = getKeyboardId(mode, imeOptions, isSymbols); - LatinKeyboard keyboard = getKeyboard(id); - - mCurrentId = id; - mInputView.setKeyboard(keyboard); - } - - private LatinKeyboard getKeyboard(KeyboardId id) { - final SoftReference<LatinKeyboard> ref = mKeyboardCache.get(id); - LatinKeyboard keyboard = (ref == null) ? null : ref.get(); - if (keyboard == null) { - final Locale savedLocale = mSubtypeSwitcher.changeSystemLocale( - mSubtypeSwitcher.getInputLocale()); - - keyboard = new LatinKeyboard(mInputMethodService, id); - - if (id.mEnableShiftLock) { - keyboard.enableShiftLock(); - } - - mKeyboardCache.put(id, new SoftReference<LatinKeyboard>(keyboard)); - if (DEBUG) - Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": " - + ((ref == null) ? "LOAD" : "GCed") + " id=" + id); - - mSubtypeSwitcher.changeSystemLocale(savedLocale); - } else if (DEBUG) { - Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": HIT id=" + id); - } - - keyboard.onAutoCompletionStateChanged(mIsAutoCompletionActive); - keyboard.setShifted(false); - return keyboard; - } - - private KeyboardId getKeyboardId(int mode, int imeOptions, boolean isSymbols) { - final boolean hasVoiceKey = hasVoiceKey(isSymbols); - final int charColorId = getColorScheme(); - final int xmlId; - final boolean enableShiftLock; - - if (isSymbols) { - if (mode == KeyboardId.MODE_PHONE) { - xmlId = R.xml.kbd_phone_symbols; - } else if (mode == KeyboardId.MODE_NUMBER) { - // Note: MODE_NUMBER keyboard layout has no "switch alpha symbol" key. - xmlId = R.xml.kbd_number; - } else { - xmlId = R.xml.kbd_symbols; - } - enableShiftLock = false; - } else { - if (mode == KeyboardId.MODE_PHONE) { - xmlId = R.xml.kbd_phone; - enableShiftLock = false; - } else if (mode == KeyboardId.MODE_NUMBER) { - xmlId = R.xml.kbd_number; - enableShiftLock = false; - } else { - xmlId = R.xml.kbd_qwerty; - enableShiftLock = true; - } - } - final int orientation = mInputMethodService.getResources().getConfiguration().orientation; - final Locale locale = mSubtypeSwitcher.getInputLocale(); - return new KeyboardId(locale, orientation, mode, xmlId, charColorId, - mHasSettingsKey, mVoiceKeyEnabled, hasVoiceKey, imeOptions, enableShiftLock); - } - - public int getKeyboardMode() { - return mMode; - } - - public boolean isAlphabetMode() { - return mCurrentId != null && mCurrentId.isAlphabetMode(); - } - - public boolean isInputViewShown() { - return mInputView != null && mInputView.isShown(); - } - - public boolean isKeyboardAvailable() { - if (mInputView != null) - return mInputView.getLatinKeyboard() != null; - return false; - } - - private LatinKeyboard getLatinKeyboard() { - if (mInputView != null) - return mInputView.getLatinKeyboard(); - return null; - } - - public void setPreferredLetters(int[] frequencies) { - LatinKeyboard latinKeyboard = getLatinKeyboard(); - if (latinKeyboard != null) - latinKeyboard.setPreferredLetters(frequencies); - } - - public void keyReleased() { - LatinKeyboard latinKeyboard = getLatinKeyboard(); - if (latinKeyboard != null) - latinKeyboard.keyReleased(); - } - - public boolean isShiftedOrShiftLocked() { - LatinKeyboard latinKeyboard = getLatinKeyboard(); - if (latinKeyboard != null) - return latinKeyboard.isShiftedOrShiftLocked(); - return false; - } - - public boolean isShiftLocked() { - LatinKeyboard latinKeyboard = getLatinKeyboard(); - if (latinKeyboard != null) - return latinKeyboard.isShiftLocked(); - return false; - } - - public boolean isAutomaticTemporaryUpperCase() { - LatinKeyboard latinKeyboard = getLatinKeyboard(); - if (latinKeyboard != null) - return latinKeyboard.isAutomaticTemporaryUpperCase(); - return false; - } - - public boolean isManualTemporaryUpperCase() { - LatinKeyboard latinKeyboard = getLatinKeyboard(); - if (latinKeyboard != null) - return latinKeyboard.isManualTemporaryUpperCase(); - return false; - } - - private void setManualTemporaryUpperCase(boolean shifted) { - LatinKeyboard latinKeyboard = getLatinKeyboard(); - if (latinKeyboard != null) { - // On non-distinct multi touch panel device, we should also turn off the shift locked - // state when shift key is pressed to go to normal mode. - // On the other hand, on distinct multi touch panel device, turning off the shift locked - // state with shift key pressing is handled by onReleaseShift(). - if (!hasDistinctMultitouch() && !shifted && latinKeyboard.isShiftLocked()) { - latinKeyboard.setShiftLocked(false); - } - if (latinKeyboard.setShifted(shifted)) { - mInputView.invalidateAllKeys(); - } - } - } - - private void setShiftLocked(boolean shiftLocked) { - LatinKeyboard latinKeyboard = getLatinKeyboard(); - if (latinKeyboard != null && latinKeyboard.setShiftLocked(shiftLocked)) { - mInputView.invalidateAllKeys(); - } - } - - /** - * Toggle keyboard shift state triggered by user touch event. - */ - public void toggleShift() { - mInputMethodService.mHandler.cancelUpdateShiftState(); - if (DEBUG_STATE) - Log.d(TAG, "toggleShift:" - + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + mShiftKeyState); - if (isAlphabetMode()) { - setManualTemporaryUpperCase(!isShiftedOrShiftLocked()); - } else { - toggleShiftInSymbol(); - } - } - - public void toggleCapsLock() { - mInputMethodService.mHandler.cancelUpdateShiftState(); - if (DEBUG_STATE) - Log.d(TAG, "toggleCapsLock:" - + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + mShiftKeyState); - if (isAlphabetMode()) { - if (isShiftLocked()) { - // Shift key is long pressed while caps lock state, we will toggle back to normal - // state. And mark as if shift key is released. - setShiftLocked(false); - mShiftKeyState.onRelease(); - } else { - setShiftLocked(true); - } - } - } - - private void setAutomaticTemporaryUpperCase() { - LatinKeyboard latinKeyboard = getLatinKeyboard(); - if (latinKeyboard != null) { - latinKeyboard.setAutomaticTemporaryUpperCase(); - mInputView.invalidateAllKeys(); - } - } - - /** - * Update keyboard shift state triggered by connected EditText status change. - */ - public void updateShiftState() { - final ShiftKeyState shiftKeyState = mShiftKeyState; - if (DEBUG_STATE) - Log.d(TAG, "updateShiftState:" - + " autoCaps=" + mInputMethodService.getCurrentAutoCapsState() - + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + shiftKeyState); - if (isAlphabetMode()) { - if (!isShiftLocked() && !shiftKeyState.isIgnoring()) { - if (shiftKeyState.isReleasing() && mInputMethodService.getCurrentAutoCapsState()) { - // Only when shift key is releasing, automatic temporary upper case will be set. - setAutomaticTemporaryUpperCase(); - } else { - setManualTemporaryUpperCase(shiftKeyState.isMomentary()); - } - } - } else { - // In symbol keyboard mode, we should clear shift key state because only alphabet - // keyboard has shift key. - shiftKeyState.onRelease(); - } - } - - public void changeKeyboardMode() { - if (DEBUG_STATE) - Log.d(TAG, "changeKeyboardMode:" - + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + mShiftKeyState); - toggleKeyboardMode(); - if (isShiftLocked() && isAlphabetMode()) - setShiftLocked(true); - updateShiftState(); - } - - public void onPressShift() { - if (!isKeyboardAvailable()) - return; - ShiftKeyState shiftKeyState = mShiftKeyState; - if (DEBUG_STATE) - Log.d(TAG, "onPressShift:" - + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + shiftKeyState); - if (isAlphabetMode()) { - if (isShiftLocked()) { - // Shift key is pressed while caps lock state, we will treat this state as shifted - // caps lock state and mark as if shift key pressed while normal state. - shiftKeyState.onPress(); - setManualTemporaryUpperCase(true); - } else if (isAutomaticTemporaryUpperCase()) { - // Shift key is pressed while automatic temporary upper case, we have to move to - // manual temporary upper case. - shiftKeyState.onPress(); - setManualTemporaryUpperCase(true); - } else if (isShiftedOrShiftLocked()) { - // In manual upper case state, we just record shift key has been pressing while - // shifted state. - shiftKeyState.onPressOnShifted(); - } else { - // In base layout, chording or manual temporary upper case mode is started. - shiftKeyState.onPress(); - toggleShift(); - } - } else { - // In symbol mode, just toggle symbol and symbol more keyboard. - shiftKeyState.onPress(); - toggleShift(); - } - } - - public void onReleaseShift() { - if (!isKeyboardAvailable()) - return; - ShiftKeyState shiftKeyState = mShiftKeyState; - if (DEBUG_STATE) - Log.d(TAG, "onReleaseShift:" - + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + shiftKeyState); - if (isAlphabetMode()) { - if (shiftKeyState.isMomentary()) { - // After chording input while normal state. - toggleShift(); - } else if (isShiftLocked() && !shiftKeyState.isIgnoring()) { - // Shift has been pressed without chording while caps lock state. - toggleCapsLock(); - } else if (isShiftedOrShiftLocked() && shiftKeyState.isPressingOnShifted()) { - // Shift has been pressed without chording while shifted state. - toggleShift(); - } - } - shiftKeyState.onRelease(); - } - - public void onPressSymbol() { - if (DEBUG_STATE) - Log.d(TAG, "onReleaseShift:" - + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " symbolKeyState=" + mSymbolKeyState); - changeKeyboardMode(); - mSymbolKeyState.onPress(); - } - - public void onReleaseSymbol() { - if (DEBUG_STATE) - Log.d(TAG, "onReleaseShift:" - + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " symbolKeyState=" + mSymbolKeyState); - if (mSymbolKeyState.isMomentary()) - changeKeyboardMode(); - mSymbolKeyState.onRelease(); - } - - public void onOtherKeyPressed() { - if (DEBUG_STATE) - Log.d(TAG, "onOtherKeyPressed:" - + " keyboard=" + getLatinKeyboard().getKeyboardShiftState() - + " shiftKeyState=" + mShiftKeyState - + " symbolKeyState=" + mSymbolKeyState); - mShiftKeyState.onOtherKeyPressed(); - mSymbolKeyState.onOtherKeyPressed(); - } - - private void toggleShiftInSymbol() { - if (isAlphabetMode()) - return; - final LatinKeyboard keyboard; - if (mCurrentId.equals(mSymbolsId) || !mCurrentId.equals(mSymbolsShiftedId)) { - mCurrentId = mSymbolsShiftedId; - keyboard = getKeyboard(mCurrentId); - // 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. - keyboard.setShiftLocked(true); - } else { - mCurrentId = mSymbolsId; - keyboard = getKeyboard(mCurrentId); - // 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). - keyboard.setShifted(false); - } - mInputView.setKeyboard(keyboard); - } - - private void toggleKeyboardMode() { - loadKeyboardInternal(mMode, mImeOptions, mVoiceKeyEnabled, mVoiceButtonOnPrimary, - !mIsSymbols); - if (mIsSymbols) { - mSymbolsModeState = SYMBOLS_MODE_STATE_BEGIN; - } else { - mSymbolsModeState = SYMBOLS_MODE_STATE_NONE; - } - } - - public boolean hasDistinctMultitouch() { - return mInputView != null && mInputView.hasDistinctMultitouch(); - } - - /** - * Updates state machine to figure out when to automatically switch back to alpha mode. - */ - public void onKey(int key) { - // Switch back to alpha mode if user types one or more non-space/enter - // characters followed by a space/enter - switch (mSymbolsModeState) { - case SYMBOLS_MODE_STATE_BEGIN: - if (key != LatinIME.KEYCODE_SPACE && key != LatinIME.KEYCODE_ENTER && key > 0) { - mSymbolsModeState = SYMBOLS_MODE_STATE_SYMBOL; - } - break; - case SYMBOLS_MODE_STATE_SYMBOL: - if (key == LatinIME.KEYCODE_ENTER || key == LatinIME.KEYCODE_SPACE) { - changeKeyboardMode(); - } - break; - } - } - - public LatinKeyboardView getInputView() { - return mInputView; - } - - public LatinKeyboardView onCreateInputView() { - createInputViewInternal(mLayoutId, true); - return mInputView; - } - - private void createInputViewInternal(int newLayout, boolean forceReset) { - 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) { - Log.w(TAG, "load keyboard failed: " + e); - tryGC = LatinIMEUtil.GCUtils.getInstance().tryGCOrWait( - mLayoutId + "," + newLayout, e); - } catch (InflateException e) { - Log.w(TAG, "load keyboard failed: " + e); - tryGC = LatinIMEUtil.GCUtils.getInstance().tryGCOrWait( - mLayoutId + "," + newLayout, e); - } - } - mInputView.setOnKeyboardActionListener(mInputMethodService); - mLayoutId = newLayout; - } - } - - private void postSetInputView() { - mInputMethodService.mHandler.post(new Runnable() { - @Override - public void run() { - if (mInputView != null) { - mInputMethodService.setInputView(mInputView); - } - mInputMethodService.updateInputViewShown(); - } - }); - } - - @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (PREF_KEYBOARD_LAYOUT.equals(key)) { - final int layoutId = Integer.valueOf( - sharedPreferences.getString(key, DEFAULT_LAYOUT_ID)); - createInputViewInternal(layoutId, false); - postSetInputView(); - } else if (LatinIMESettings.PREF_SETTINGS_KEY.equals(key)) { - mHasSettingsKey = getSettingsKeyMode(sharedPreferences, mInputMethodService); - createInputViewInternal(mLayoutId, true); - postSetInputView(); - } - } - - private int getColorScheme() { - return (mInputView != null) - ? mInputView.getColorScheme() : KeyboardView.COLOR_SCHEME_WHITE; - } - - public void onAutoCompletionStateChanged(boolean isAutoCompletion) { - if (isAutoCompletion != mIsAutoCompletionActive) { - LatinKeyboardView keyboardView = getInputView(); - mIsAutoCompletionActive = isAutoCompletion; - keyboardView.invalidateKey(((LatinKeyboard) keyboardView.getKeyboard()) - .onAutoCompletionStateChanged(isAutoCompletion)); - } - } - - private static boolean getSettingsKeyMode(SharedPreferences prefs, Context context) { - Resources resources = context.getResources(); - final boolean showSettingsKeyOption = resources.getBoolean( - R.bool.config_enable_show_settings_key_option); - 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)) - && LatinIMEUtil.hasMultipleEnabledIMEsOrSubtypes( - ((InputMethodManager) context.getSystemService( - Context.INPUT_METHOD_SERVICE))))) { - return true; - } - } - return false; - } -} diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 4f4112fa8..5bf635fd3 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -19,8 +19,8 @@ package com.android.inputmethod.latin; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.KeyboardActionListener; import com.android.inputmethod.keyboard.KeyboardId; +import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.KeyboardView; -import com.android.inputmethod.keyboard.LatinKeyboard; import com.android.inputmethod.keyboard.LatinKeyboardView; import com.android.inputmethod.latin.LatinIMEUtil.RingCharBuffer; import com.android.inputmethod.voice.VoiceIMEConnector; @@ -109,11 +109,6 @@ public class LatinIME extends InputMethodService // Key events coming any faster than this are long-presses. private static final int QUICK_PRESS = 200; - public static final int KEYCODE_ENTER = '\n'; - public static final int KEYCODE_TAB = '\t'; - public static final int KEYCODE_SPACE = ' '; - public static final int KEYCODE_PERIOD = '.'; - // Contextual menu positions private static final int POS_METHOD = 0; private static final int POS_SETTINGS = 1; @@ -255,7 +250,7 @@ public class LatinIME extends InputMethodService } } - /* package */ final UIHandler mHandler = new UIHandler(); + public final UIHandler mHandler = new UIHandler(); public class UIHandler extends Handler { private static final int MSG_UPDATE_SUGGESTIONS = 0; @@ -968,7 +963,8 @@ public class LatinIME extends InputMethodService if (ic == null) return; CharSequence lastTwo = ic.getTextBeforeCursor(2, 0); if (lastTwo != null && lastTwo.length() == 2 - && lastTwo.charAt(0) == KEYCODE_SPACE && isSentenceSeparator(lastTwo.charAt(1))) { + && lastTwo.charAt(0) == Keyboard.CODE_SPACE + && isSentenceSeparator(lastTwo.charAt(1))) { ic.beginBatchEdit(); ic.deleteSurroundingText(2, 0); ic.commitText(lastTwo.charAt(1) + " ", 1); @@ -983,9 +979,9 @@ public class LatinIME extends InputMethodService if (ic == null) return; CharSequence lastThree = ic.getTextBeforeCursor(3, 0); if (lastThree != null && lastThree.length() == 3 - && lastThree.charAt(0) == KEYCODE_PERIOD - && lastThree.charAt(1) == KEYCODE_SPACE - && lastThree.charAt(2) == KEYCODE_PERIOD) { + && lastThree.charAt(0) == Keyboard.CODE_PERIOD + && lastThree.charAt(1) == Keyboard.CODE_SPACE + && lastThree.charAt(2) == Keyboard.CODE_PERIOD) { ic.beginBatchEdit(); ic.deleteSurroundingText(3, 0); ic.commitText(" ..", 1); @@ -1002,7 +998,8 @@ public class LatinIME extends InputMethodService CharSequence lastThree = ic.getTextBeforeCursor(3, 0); if (lastThree != null && lastThree.length() == 3 && Character.isLetterOrDigit(lastThree.charAt(0)) - && lastThree.charAt(1) == KEYCODE_SPACE && lastThree.charAt(2) == KEYCODE_SPACE) { + && lastThree.charAt(1) == Keyboard.CODE_SPACE + && lastThree.charAt(2) == Keyboard.CODE_SPACE) { ic.beginBatchEdit(); ic.deleteSurroundingText(2, 0); ic.commitText(". ", 1); @@ -1020,8 +1017,8 @@ public class LatinIME extends InputMethodService // if there is one. CharSequence lastOne = ic.getTextBeforeCursor(1, 0); if (lastOne != null && lastOne.length() == 1 - && lastOne.charAt(0) == KEYCODE_PERIOD - && text.charAt(0) == KEYCODE_PERIOD) { + && lastOne.charAt(0) == Keyboard.CODE_PERIOD + && text.charAt(0) == Keyboard.CODE_PERIOD) { ic.deleteSurroundingText(1, 0); } } @@ -1032,7 +1029,7 @@ public class LatinIME extends InputMethodService CharSequence lastOne = ic.getTextBeforeCursor(1, 0); if (lastOne != null && lastOne.length() == 1 - && lastOne.charAt(0) == KEYCODE_SPACE) { + && lastOne.charAt(0) == Keyboard.CODE_SPACE) { ic.deleteSurroundingText(1, 0); } } @@ -1082,57 +1079,57 @@ public class LatinIME extends InputMethodService @Override public void onKey(int primaryCode, int[] keyCodes, int x, int y) { long when = SystemClock.uptimeMillis(); - if (primaryCode != Keyboard.KEYCODE_DELETE || when > mLastKeyTime + QUICK_PRESS) { + if (primaryCode != Keyboard.CODE_DELETE || when > mLastKeyTime + QUICK_PRESS) { mDeleteCount = 0; } mLastKeyTime = when; KeyboardSwitcher switcher = mKeyboardSwitcher; final boolean distinctMultiTouch = switcher.hasDistinctMultitouch(); switch (primaryCode) { - case Keyboard.KEYCODE_DELETE: + case Keyboard.CODE_DELETE: handleBackspace(); mDeleteCount++; LatinImeLogger.logOnDelete(); break; - case Keyboard.KEYCODE_SHIFT: + case Keyboard.CODE_SHIFT: // Shift key is handled in onPress() when device has distinct multi-touch panel. if (!distinctMultiTouch) switcher.toggleShift(); break; - case Keyboard.KEYCODE_MODE_CHANGE: + case Keyboard.CODE_MODE_CHANGE: // Symbol key is handled in onPress() when device has distinct multi-touch panel. if (!distinctMultiTouch) switcher.changeKeyboardMode(); break; - case Keyboard.KEYCODE_CANCEL: + case Keyboard.CODE_CANCEL: if (!isShowingOptionDialog()) { handleClose(); } break; - case LatinKeyboard.KEYCODE_OPTIONS: + case Keyboard.CODE_OPTIONS: onOptionKeyPressed(); break; - case LatinKeyboard.KEYCODE_OPTIONS_LONGPRESS: + case Keyboard.CODE_OPTIONS_LONGPRESS: onOptionKeyLongPressed(); break; - case LatinKeyboard.KEYCODE_NEXT_LANGUAGE: + case Keyboard.CODE_NEXT_LANGUAGE: toggleLanguage(false, true); break; - case LatinKeyboard.KEYCODE_PREV_LANGUAGE: + case Keyboard.CODE_PREV_LANGUAGE: toggleLanguage(false, false); break; - case LatinKeyboard.KEYCODE_CAPSLOCK: + case Keyboard.CODE_CAPSLOCK: switcher.toggleCapsLock(); break; - case LatinKeyboard.KEYCODE_VOICE: /* was a button press, was not a swipe */ + case Keyboard.CODE_VOICE: /* was a button press, was not a swipe */ mVoiceConnector.startListening(false, mKeyboardSwitcher.getInputView().getWindowToken(), mConfigurationChanging); break; - case KEYCODE_TAB: + case Keyboard.CODE_TAB: handleTab(); break; default: - if (primaryCode != KEYCODE_ENTER) { + if (primaryCode != Keyboard.CODE_ENTER) { mJustAddedAutoSpace = false; } RingCharBuffer.getInstance().push((char)primaryCode, x, y); @@ -1344,14 +1341,14 @@ public class LatinIME extends InputMethodService pickedDefault = pickDefaultSuggestion(); // Picked the suggestion by the space key. We consider this // as "added an auto space". - if (primaryCode == KEYCODE_SPACE) { + if (primaryCode == Keyboard.CODE_SPACE) { mJustAddedAutoSpace = true; } } else { commitTyped(ic); } } - if (mJustAddedAutoSpace && primaryCode == KEYCODE_ENTER) { + if (mJustAddedAutoSpace && primaryCode == Keyboard.CODE_ENTER) { removeTrailingSpace(); mJustAddedAutoSpace = false; } @@ -1360,15 +1357,15 @@ public class LatinIME extends InputMethodService // Handle the case of ". ." -> " .." with auto-space if necessary // before changing the TextEntryState. if (TextEntryState.getState() == TextEntryState.State.PUNCTUATION_AFTER_ACCEPTED - && primaryCode == KEYCODE_PERIOD) { + && primaryCode == Keyboard.CODE_PERIOD) { reswapPeriodAndSpace(); } TextEntryState.typedCharacter((char) primaryCode, true); if (TextEntryState.getState() == TextEntryState.State.PUNCTUATION_AFTER_ACCEPTED - && primaryCode != KEYCODE_ENTER) { + && primaryCode != Keyboard.CODE_ENTER) { swapPunctuationAndSpace(); - } else if (isPredictionOn() && primaryCode == KEYCODE_SPACE) { + } else if (isPredictionOn() && primaryCode == Keyboard.CODE_SPACE) { doubleSpace(); } if (pickedDefault) { @@ -1629,7 +1626,7 @@ public class LatinIME extends InputMethodService // Fool the state watcher so that a subsequent backspace will not do a revert, unless // we just did a correction, in which case we need to stay in // TextEntryState.State.PICKED_SUGGESTION state. - TextEntryState.typedCharacter((char) KEYCODE_SPACE, true); + TextEntryState.typedCharacter((char) Keyboard.CODE_SPACE, true); setPunctuationSuggestions(); } else if (!showingAddToDictionaryHint) { // If we're not showing the "Touch again to save", then show corrections again. @@ -1857,7 +1854,7 @@ public class LatinIME extends InputMethodService } private void sendSpace() { - sendKeyChar((char)KEYCODE_SPACE); + sendKeyChar((char)Keyboard.CODE_SPACE); mKeyboardSwitcher.updateShiftState(); //onKey(KEY_SPACE[0], KEY_SPACE); } @@ -1930,9 +1927,9 @@ public class LatinIME extends InputMethodService playKeyClick(primaryCode); KeyboardSwitcher switcher = mKeyboardSwitcher; final boolean distinctMultiTouch = switcher.hasDistinctMultitouch(); - if (distinctMultiTouch && primaryCode == Keyboard.KEYCODE_SHIFT) { + if (distinctMultiTouch && primaryCode == Keyboard.CODE_SHIFT) { switcher.onPressShift(); - } else if (distinctMultiTouch && primaryCode == Keyboard.KEYCODE_MODE_CHANGE) { + } else if (distinctMultiTouch && primaryCode == Keyboard.CODE_MODE_CHANGE) { switcher.onPressSymbol(); } else { switcher.onOtherKeyPressed(); @@ -1945,9 +1942,9 @@ public class LatinIME extends InputMethodService // Reset any drag flags in the keyboard switcher.keyReleased(); final boolean distinctMultiTouch = switcher.hasDistinctMultitouch(); - if (distinctMultiTouch && primaryCode == Keyboard.KEYCODE_SHIFT) { + if (distinctMultiTouch && primaryCode == Keyboard.CODE_SHIFT) { switcher.onReleaseShift(); - } else if (distinctMultiTouch && primaryCode == Keyboard.KEYCODE_MODE_CHANGE) { + } else if (distinctMultiTouch && primaryCode == Keyboard.CODE_MODE_CHANGE) { switcher.onReleaseSymbol(); } } @@ -1984,13 +1981,13 @@ public class LatinIME extends InputMethodService // FIXME: These should be triggered after auto-repeat logic int sound = AudioManager.FX_KEYPRESS_STANDARD; switch (primaryCode) { - case Keyboard.KEYCODE_DELETE: + case Keyboard.CODE_DELETE: sound = AudioManager.FX_KEYPRESS_DELETE; break; - case KEYCODE_ENTER: + case Keyboard.CODE_ENTER: sound = AudioManager.FX_KEYPRESS_RETURN; break; - case KEYCODE_SPACE: + case Keyboard.CODE_SPACE: sound = AudioManager.FX_KEYPRESS_SPACEBAR; break; } diff --git a/java/src/com/android/inputmethod/latin/LatinIMESettings.java b/java/src/com/android/inputmethod/latin/LatinIMESettings.java index 187b6d394..3aa89dd59 100644 --- a/java/src/com/android/inputmethod/latin/LatinIMESettings.java +++ b/java/src/com/android/inputmethod/latin/LatinIMESettings.java @@ -48,7 +48,7 @@ public class LatinIMESettings extends PreferenceActivity private static final String VOICE_SETTINGS_KEY = "voice_mode"; private static final String PREF_AUTO_COMPLETION_THRESHOLD = "auto_completion_threshold"; private static final String PREF_BIGRAM_SUGGESTIONS = "bigram_suggestion"; - /* package */ static final String PREF_SETTINGS_KEY = "settings_key"; + public static final String PREF_SETTINGS_KEY = "settings_key"; /* package */ static final String PREF_VIBRATE_ON = "vibrate_on"; private static final String TAG = "LatinIMESettings"; diff --git a/java/src/com/android/inputmethod/latin/ModifierKeyState.java b/java/src/com/android/inputmethod/latin/ModifierKeyState.java deleted file mode 100644 index eb1204f70..000000000 --- a/java/src/com/android/inputmethod/latin/ModifierKeyState.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) 2010 Google Inc. - * - * 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; - -import android.util.Log; - -public class ModifierKeyState { - protected static final String TAG = "ModifierKeyState"; - protected static final boolean DEBUG = KeyboardSwitcher.DEBUG_STATE; - - protected static final int RELEASING = 0; - protected static final int PRESSING = 1; - protected static final int MOMENTARY = 2; - - protected final String mName; - protected int mState = RELEASING; - - public ModifierKeyState(String name) { - mName = name; - } - - public void onPress() { - final int oldState = mState; - mState = PRESSING; - if (DEBUG) - Log.d(TAG, mName + ".onPress: " + toString(oldState) + " > " + this); - } - - public void onRelease() { - final int oldState = mState; - mState = RELEASING; - if (DEBUG) - Log.d(TAG, mName + ".onRelease: " + toString(oldState) + " > " + this); - } - - public void onOtherKeyPressed() { - final int oldState = mState; - if (oldState == PRESSING) - mState = MOMENTARY; - if (DEBUG) - Log.d(TAG, mName + ".onOtherKeyPressed: " + toString(oldState) + " > " + this); - } - - public boolean isReleasing() { - return mState == RELEASING; - } - - public boolean isMomentary() { - return mState == MOMENTARY; - } - - @Override - public String toString() { - return toString(mState); - } - - protected String toString(int state) { - switch (state) { - case RELEASING: return "RELEASING"; - case PRESSING: return "PRESSING"; - case MOMENTARY: return "MOMENTARY"; - default: return "UNKNOWN"; - } - } -} diff --git a/java/src/com/android/inputmethod/latin/ShiftKeyState.java b/java/src/com/android/inputmethod/latin/ShiftKeyState.java deleted file mode 100644 index 7412a566d..000000000 --- a/java/src/com/android/inputmethod/latin/ShiftKeyState.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2010 Google Inc. - * - * 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; - -import android.util.Log; - -public class ShiftKeyState extends ModifierKeyState { - private static final int PRESSING_ON_SHIFTED = 3; // both temporary shifted & shift locked - private static final int IGNORING = 4; - - public ShiftKeyState(String name) { - super(name); - } - - @Override - public void onOtherKeyPressed() { - int oldState = mState; - if (oldState == PRESSING) { - mState = MOMENTARY; - } else if (oldState == PRESSING_ON_SHIFTED) { - mState = IGNORING; - } - if (DEBUG) - Log.d(TAG, mName + ".onOtherKeyPressed: " + toString(oldState) + " > " + this); - } - - public void onPressOnShifted() { - int oldState = mState; - mState = PRESSING_ON_SHIFTED; - if (DEBUG) - Log.d(TAG, mName + ".onPressOnShifted: " + toString(oldState) + " > " + this); - } - - public boolean isPressingOnShifted() { - return mState == PRESSING_ON_SHIFTED; - } - - public boolean isIgnoring() { - return mState == IGNORING; - } - - @Override - public String toString() { - return toString(mState); - } - - @Override - protected String toString(int state) { - switch (state) { - case PRESSING_ON_SHIFTED: return "PRESSING_ON_SHIFTED"; - case IGNORING: return "IGNORING"; - default: return super.toString(state); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index a98917689..3ee4eb891 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -16,7 +16,7 @@ package com.android.inputmethod.latin; -import com.android.inputmethod.keyboard.LatinKeyboard; +import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.voice.SettingsUtil; import com.android.inputmethod.voice.VoiceIMEConnector; import com.android.inputmethod.voice.VoiceInput; @@ -198,7 +198,7 @@ public class SubtypeSwitcher { || VoiceIMEConnector.getInstance().needsToShowWarningDialog()) { if (mVoiceInput != null) { // TODO: Call proper function to trigger VoiceIME - mService.onKey(LatinKeyboard.KEYCODE_VOICE, null, 0, 0); + mService.onKey(Keyboard.CODE_VOICE, null, 0, 0); } } } else { @@ -351,7 +351,7 @@ public class SubtypeSwitcher { if (DBG) { Log.d(TAG, "Set and call voice input."); } - mService.onKey(LatinKeyboard.KEYCODE_VOICE, null, 0, 0); + mService.onKey(Keyboard.CODE_VOICE, null, 0, 0); return true; } } diff --git a/java/src/com/android/inputmethod/latin/TextEntryState.java b/java/src/com/android/inputmethod/latin/TextEntryState.java index 31a192bfe..34babdcb5 100644 --- a/java/src/com/android/inputmethod/latin/TextEntryState.java +++ b/java/src/com/android/inputmethod/latin/TextEntryState.java @@ -261,13 +261,13 @@ public class TextEntryState { } public static void keyPressedAt(Key key, int x, int y) { - if (LOGGING && sKeyLocationFile != null && key.codes[0] >= 32) { - String out = - "KEY: " + (char) key.codes[0] - + " X: " + x + if (LOGGING && sKeyLocationFile != null && key.mCodes[0] >= 32) { + String out = + "KEY: " + (char) key.mCodes[0] + + " X: " + x + " Y: " + y - + " MX: " + (key.x + key.width / 2) - + " MY: " + (key.y + key.height / 2) + + " MX: " + (key.mX + key.mWidth / 2) + + " MY: " + (key.mY + key.mHeight / 2) + "\n"; try { sKeyLocationFile.write(out.getBytes()); diff --git a/java/src/com/android/inputmethod/latin/Tutorial.java b/java/src/com/android/inputmethod/latin/Tutorial.java index bd069bd0d..20767de4d 100644 --- a/java/src/com/android/inputmethod/latin/Tutorial.java +++ b/java/src/com/android/inputmethod/latin/Tutorial.java @@ -16,6 +16,7 @@ package com.android.inputmethod.latin; +import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.LatinKeyboardView; import android.content.Context; |