diff options
Diffstat (limited to 'java/src')
14 files changed, 738 insertions, 285 deletions
diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java index bd73c6fb8..0f5b43009 100755 --- a/java/src/com/android/inputmethod/latin/CandidateView.java +++ b/java/src/com/android/inputmethod/latin/CandidateView.java @@ -186,7 +186,6 @@ public class CandidateView extends View { return true; } }); - setHorizontalFadingEdgeEnabled(true); setWillNotDraw(false); setHorizontalScrollBarEnabled(false); setVerticalScrollBarEnabled(false); @@ -248,7 +247,9 @@ public class CandidateView extends View { paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setColor(mColorRecommended); existsAutoCompletion = true; - } else if (i != 0) { + } else if (i != 0 || (suggestion.length() == 1 && count > 1)) { + // HACK: even if i == 0, we use mColorOther when this suggestion's length is 1 and + // there are multiple suggestions, such as the default punctuation list. paint.setColor(mColorOther); } final int wordWidth; diff --git a/java/src/com/android/inputmethod/latin/Hints.java b/java/src/com/android/inputmethod/latin/Hints.java index 2434d5195..c467365e7 100644 --- a/java/src/com/android/inputmethod/latin/Hints.java +++ b/java/src/com/android/inputmethod/latin/Hints.java @@ -106,7 +106,7 @@ public class Hints { SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(mContext).edit(); editor.putLong(PREF_VOICE_INPUT_LAST_TIME_USED, System.currentTimeMillis()); - editor.commit(); + SharedPreferencesCompat.apply(editor); mVoiceResultContainedPunctuation = false; for (CharSequence s : SPEAKABLE_PUNCTUATION.keySet()) { @@ -168,7 +168,7 @@ public class Hints { SharedPreferences.Editor editor = sp.edit(); editor.putInt(PREF_VOICE_HINT_NUM_UNIQUE_DAYS_SHOWN, numUniqueDaysShown + 1); editor.putLong(PREF_VOICE_HINT_LAST_TIME_SHOWN, System.currentTimeMillis()); - editor.commit(); + SharedPreferencesCompat.apply(editor); } if (mDisplay != null) { @@ -181,7 +181,7 @@ public class Hints { int value = sp.getInt(pref, 0); SharedPreferences.Editor editor = sp.edit(); editor.putInt(pref, value + 1); - editor.commit(); + SharedPreferencesCompat.apply(editor); return value; } } diff --git a/java/src/com/android/inputmethod/latin/InputLanguageSelection.java b/java/src/com/android/inputmethod/latin/InputLanguageSelection.java index 7258874c0..e811a2cdd 100644 --- a/java/src/com/android/inputmethod/latin/InputLanguageSelection.java +++ b/java/src/com/android/inputmethod/latin/InputLanguageSelection.java @@ -143,7 +143,7 @@ public class InputLanguageSelection extends PreferenceActivity { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); Editor editor = sp.edit(); editor.putString(LatinIME.PREF_SELECTED_LANGUAGES, checkedLanguages); - editor.commit(); + SharedPreferencesCompat.apply(editor); } ArrayList<Loc> getUniqueLocales() { diff --git a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java index 284b29305..ebf2f4e60 100644 --- a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java @@ -16,13 +16,13 @@ package com.android.inputmethod.latin; -import android.content.Context; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; import android.preference.PreferenceManager; import android.view.InflateException; +import java.lang.ref.SoftReference; import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -38,11 +38,29 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha public static final int MODE_IM = 6; public static final int MODE_WEB = 7; + // Main keyboard layouts without the settings key public static final int KEYBOARDMODE_NORMAL = R.id.mode_normal; public static final int KEYBOARDMODE_URL = R.id.mode_url; public static final int KEYBOARDMODE_EMAIL = R.id.mode_email; public static final int KEYBOARDMODE_IM = R.id.mode_im; public static final int KEYBOARDMODE_WEB = R.id.mode_webentry; + // Main keyboard layouts with the settings key + public static final int KEYBOARDMODE_NORMAL_WITH_SETTINGS_KEY = + R.id.mode_normal_with_settings_key; + public static final int KEYBOARDMODE_URL_WITH_SETTINGS_KEY = + R.id.mode_url_with_settings_key; + public static final int KEYBOARDMODE_EMAIL_WITH_SETTINGS_KEY = + R.id.mode_email_with_settings_key; + public static final int KEYBOARDMODE_IM_WITH_SETTINGS_KEY = + R.id.mode_im_with_settings_key; + public static final int KEYBOARDMODE_WEB_WITH_SETTINGS_KEY = + R.id.mode_webentry_with_settings_key; + + // Symbols keyboard layout without the settings key + public static final int KEYBOARDMODE_SYMBOLS = R.id.mode_symbols; + // Symbols keyboard layout with the settings key + public static final int KEYBOARDMODE_SYMBOLS_WITH_SETTINGS_KEY = + R.id.mode_symbols_with_settings_key; public static final String DEFAULT_LAYOUT_ID = "4"; public static final String PREF_KEYBOARD_LAYOUT = "pref_keyboard_layout_20100902"; @@ -68,22 +86,26 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha private static final int SYMBOLS_MODE_STATE_BEGIN = 1; private static final int SYMBOLS_MODE_STATE_SYMBOL = 2; - LatinKeyboardView mInputView; + private LatinKeyboardView mInputView; private static final int[] ALPHABET_MODES = { KEYBOARDMODE_NORMAL, KEYBOARDMODE_URL, KEYBOARDMODE_EMAIL, KEYBOARDMODE_IM, - KEYBOARDMODE_WEB}; + KEYBOARDMODE_WEB, + KEYBOARDMODE_NORMAL_WITH_SETTINGS_KEY, + KEYBOARDMODE_URL_WITH_SETTINGS_KEY, + KEYBOARDMODE_EMAIL_WITH_SETTINGS_KEY, + KEYBOARDMODE_IM_WITH_SETTINGS_KEY, + KEYBOARDMODE_WEB_WITH_SETTINGS_KEY }; - Context mContext; - LatinIME mInputMethodService; + private final LatinIME mInputMethodService; private KeyboardId mSymbolsId; private KeyboardId mSymbolsShiftedId; private KeyboardId mCurrentId; - private Map<KeyboardId, LatinKeyboard> mKeyboards; + private final Map<KeyboardId, SoftReference<LatinKeyboard>> mKeyboards; private int mMode = MODE_NONE; /** One of the MODE_XXX values */ private int mImeOptions; @@ -96,23 +118,32 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha private boolean mPreferSymbols; 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 mLastDisplayWidth; private LanguageSwitcher mLanguageSwitcher; private Locale mInputLocale; private int mLayoutId; - KeyboardSwitcher(Context context, LatinIME ims) { - mContext = context; + public KeyboardSwitcher(LatinIME ims) { + mInputMethodService = ims; final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ims); mLayoutId = Integer.valueOf(prefs.getString(PREF_KEYBOARD_LAYOUT, DEFAULT_LAYOUT_ID)); + updateSettingsKeyState(prefs); prefs.registerOnSharedPreferenceChangeListener(this); - mKeyboards = new HashMap<KeyboardId, LatinKeyboard>(); + mKeyboards = new HashMap<KeyboardId, SoftReference<LatinKeyboard>>(); mSymbolsId = makeSymbolsId(false); mSymbolsShiftedId = makeSymbolsShiftedId(false); - mInputMethodService = ims; } /** @@ -121,24 +152,24 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha * @param locale the current input locale, or null for default locale with no locale * button. */ - void setLanguageSwitcher(LanguageSwitcher languageSwitcher) { + public void setLanguageSwitcher(LanguageSwitcher languageSwitcher) { mLanguageSwitcher = languageSwitcher; mInputLocale = mLanguageSwitcher.getInputLocale(); } - void setInputView(LatinKeyboardView inputView) { - mInputView = inputView; - } - private KeyboardId makeSymbolsId(boolean hasVoice) { - return new KeyboardId(KBD_SYMBOLS[getCharColorId()], hasVoice); + return new KeyboardId(KBD_SYMBOLS[getCharColorId()], mHasSettingsKey ? + KEYBOARDMODE_SYMBOLS_WITH_SETTINGS_KEY : KEYBOARDMODE_SYMBOLS, + false, hasVoice); } private KeyboardId makeSymbolsShiftedId(boolean hasVoice) { - return new KeyboardId(KBD_SYMBOLS_SHIFT[getCharColorId()], hasVoice); + return new KeyboardId(KBD_SYMBOLS_SHIFT[getCharColorId()], mHasSettingsKey ? + KEYBOARDMODE_SYMBOLS_WITH_SETTINGS_KEY : KEYBOARDMODE_SYMBOLS, + false, hasVoice); } - void makeKeyboards(boolean forceCreate) { + public void makeKeyboards(boolean forceCreate) { mSymbolsId = makeSymbolsId(mHasVoice && !mVoiceOnPrimary); mSymbolsShiftedId = makeSymbolsShiftedId(mHasVoice && !mVoiceOnPrimary); @@ -157,10 +188,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha * which also serve as a unique identifier for each keyboard type. */ private static class KeyboardId { - public int mXml; - public int mKeyboardMode; /** A KEYBOARDMODE_XXX value */ - public boolean mEnableShiftLock; - public boolean mHasVoice; + // TODO: should have locale and portrait/landscape orientation? + public final int mXml; + public final int mKeyboardMode; /** A KEYBOARDMODE_XXX value */ + public final boolean mEnableShiftLock; + public final boolean mHasVoice; public KeyboardId(int xml, int mode, boolean enableShiftLock, boolean hasVoice) { this.mXml = xml; @@ -178,10 +210,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha return other instanceof KeyboardId && equals((KeyboardId) other); } - public boolean equals(KeyboardId other) { - return other.mXml == this.mXml - && other.mKeyboardMode == this.mKeyboardMode - && other.mEnableShiftLock == this.mEnableShiftLock; + private boolean equals(KeyboardId other) { + return other.mXml == this.mXml + && other.mKeyboardMode == this.mKeyboardMode + && other.mEnableShiftLock == this.mEnableShiftLock + && other.mHasVoice == this.mHasVoice; } @Override @@ -191,7 +224,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha } } - void setVoiceMode(boolean enableVoice, boolean voiceOnPrimary) { + public void setVoiceMode(boolean enableVoice, boolean voiceOnPrimary) { if (enableVoice != mHasVoice || voiceOnPrimary != mVoiceOnPrimary) { mKeyboards.clear(); } @@ -200,11 +233,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha setKeyboardMode(mMode, mImeOptions, mHasVoice, mIsSymbols); } - boolean hasVoiceButton(boolean isSymbols) { + private boolean hasVoiceButton(boolean isSymbols) { return mHasVoice && (isSymbols != mVoiceOnPrimary); } - void setKeyboardMode(int mode, int imeOptions, boolean enableVoice) { + public void setKeyboardMode(int mode, int imeOptions, boolean enableVoice) { mSymbolsModeState = SYMBOLS_MODE_STATE_NONE; mPreferSymbols = mode == MODE_SYMBOLS; if (mode == MODE_SYMBOLS) { @@ -217,7 +250,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha } } - void setKeyboardMode(int mode, int imeOptions, boolean enableVoice, boolean isSymbols) { + private void setKeyboardMode(int mode, int imeOptions, boolean enableVoice, boolean isSymbols) { if (mInputView == null) return; mMode = mode; mImeOptions = imeOptions; @@ -233,25 +266,28 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha if (mode == MODE_PHONE) { mInputView.setPhoneKeyboard(keyboard); - mInputView.setPreviewEnabled(false); } mCurrentId = id; mInputView.setKeyboard(keyboard); keyboard.setShifted(false); keyboard.setShiftLocked(keyboard.isShiftLocked()); - keyboard.setImeOptions(mContext.getResources(), mMode, imeOptions); + keyboard.setImeOptions(mInputMethodService.getResources(), mMode, imeOptions); keyboard.setColorOfSymbolIcons(mIsAutoCompletionActive, isBlackSym()); + // Update the settings key state because number of enabled IMEs could have been changed + updateSettingsKeyState(PreferenceManager.getDefaultSharedPreferences(mInputMethodService)); } private LatinKeyboard getKeyboard(KeyboardId id) { - if (!mKeyboards.containsKey(id)) { - Resources orig = mContext.getResources(); + SoftReference<LatinKeyboard> ref = mKeyboards.get(id); + LatinKeyboard keyboard = (ref == null) ? null : ref.get(); + if (keyboard == null) { + Resources orig = mInputMethodService.getResources(); Configuration conf = orig.getConfiguration(); Locale saveLocale = conf.locale; conf.locale = mInputLocale; orig.updateConfiguration(conf, null); - LatinKeyboard keyboard = new LatinKeyboard(mContext, id.mXml, id.mKeyboardMode); + keyboard = new LatinKeyboard(mInputMethodService, id.mXml, id.mKeyboardMode); keyboard.setVoiceMode(hasVoiceButton(id.mXml == R.xml.kbd_symbols || id.mXml == R.xml.kbd_symbols_black), mHasVoice); keyboard.setLanguageSwitcher(mLanguageSwitcher, mIsAutoCompletionActive, isBlackSym()); @@ -259,12 +295,12 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha if (id.mEnableShiftLock) { keyboard.enableShiftLock(); } - mKeyboards.put(id, keyboard); + mKeyboards.put(id, new SoftReference<LatinKeyboard>(keyboard)); conf.locale = saveLocale; orig.updateConfiguration(conf, null); } - return mKeyboards.get(id); + return keyboard; } private KeyboardId getKeyboardId(int mode, int imeOptions, boolean isSymbols) { @@ -276,7 +312,9 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha if (mode == MODE_PHONE) { return new KeyboardId(KBD_PHONE_SYMBOLS[charColorId], hasVoice); } else { - return new KeyboardId(KBD_SYMBOLS[charColorId], hasVoice); + return new KeyboardId(KBD_SYMBOLS[charColorId], mHasSettingsKey ? + KEYBOARDMODE_SYMBOLS_WITH_SETTINGS_KEY : KEYBOARDMODE_SYMBOLS, + false, hasVoice); } } switch (mode) { @@ -285,32 +323,36 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha "getKeyboardId:" + mode + "," + imeOptions + "," + isSymbols); /* fall through */ case MODE_TEXT: - return new KeyboardId(keyboardRowsResId, KEYBOARDMODE_NORMAL, true, hasVoice); + return new KeyboardId(keyboardRowsResId, mHasSettingsKey ? + KEYBOARDMODE_NORMAL_WITH_SETTINGS_KEY : KEYBOARDMODE_NORMAL, + true, hasVoice); case MODE_SYMBOLS: - return new KeyboardId(KBD_SYMBOLS[charColorId], hasVoice); + return new KeyboardId(KBD_SYMBOLS[charColorId], mHasSettingsKey ? + KEYBOARDMODE_SYMBOLS_WITH_SETTINGS_KEY : KEYBOARDMODE_SYMBOLS, + false, hasVoice); case MODE_PHONE: return new KeyboardId(KBD_PHONE[charColorId], hasVoice); case MODE_URL: - return new KeyboardId(keyboardRowsResId, KEYBOARDMODE_URL, true, hasVoice); + return new KeyboardId(keyboardRowsResId, mHasSettingsKey ? + KEYBOARDMODE_URL_WITH_SETTINGS_KEY : KEYBOARDMODE_URL, true, hasVoice); case MODE_EMAIL: - return new KeyboardId(keyboardRowsResId, KEYBOARDMODE_EMAIL, true, hasVoice); + return new KeyboardId(keyboardRowsResId, mHasSettingsKey ? + KEYBOARDMODE_EMAIL_WITH_SETTINGS_KEY : KEYBOARDMODE_EMAIL, true, hasVoice); case MODE_IM: - return new KeyboardId(keyboardRowsResId, KEYBOARDMODE_IM, true, hasVoice); + return new KeyboardId(keyboardRowsResId, mHasSettingsKey ? + KEYBOARDMODE_IM_WITH_SETTINGS_KEY : KEYBOARDMODE_IM, true, hasVoice); case MODE_WEB: - return new KeyboardId(keyboardRowsResId, KEYBOARDMODE_WEB, true, hasVoice); + return new KeyboardId(keyboardRowsResId, mHasSettingsKey ? + KEYBOARDMODE_WEB_WITH_SETTINGS_KEY : KEYBOARDMODE_WEB, true, hasVoice); } return null; } - int getKeyboardMode() { + public int getKeyboardMode() { return mMode; } - boolean isTextMode() { - return mMode == MODE_TEXT; - } - - boolean isAlphabetMode() { + public boolean isAlphabetMode() { if (mCurrentId == null) { return false; } @@ -323,19 +365,19 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha return false; } - void setShifted(boolean shifted) { + public void setShifted(boolean shifted) { if (mInputView != null) { mInputView.setShifted(shifted); } } - void setShiftLocked(boolean shiftLocked) { + public void setShiftLocked(boolean shiftLocked) { if (mInputView != null) { mInputView.setShiftLocked(shiftLocked); } } - void toggleShift() { + public void toggleShift() { if (mCurrentId.equals(mSymbolsId)) { LatinKeyboard symbolsShiftedKeyboard = getKeyboard(mSymbolsShiftedId); mCurrentId = mSymbolsShiftedId; @@ -346,7 +388,8 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha // called. symbolsShiftedKeyboard.enableShiftLock(); symbolsShiftedKeyboard.setShiftLocked(true); - symbolsShiftedKeyboard.setImeOptions(mContext.getResources(), mMode, mImeOptions); + symbolsShiftedKeyboard.setImeOptions(mInputMethodService.getResources(), + mMode, mImeOptions); } else if (mCurrentId.equals(mSymbolsShiftedId)) { LatinKeyboard symbolsKeyboard = getKeyboard(mSymbolsId); mCurrentId = mSymbolsId; @@ -355,11 +398,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha // indicator, we need to call enableShiftLock() and setShiftLocked(false). symbolsKeyboard.enableShiftLock(); symbolsKeyboard.setShifted(false); - symbolsKeyboard.setImeOptions(mContext.getResources(), mMode, mImeOptions); + symbolsKeyboard.setImeOptions(mInputMethodService.getResources(), mMode, mImeOptions); } } - void toggleSymbols() { + public void toggleSymbols() { setKeyboardMode(mMode, mImeOptions, mHasVoice, !mIsSymbols); if (mIsSymbols && !mPreferSymbols) { mSymbolsModeState = SYMBOLS_MODE_STATE_BEGIN; @@ -376,7 +419,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha * Updates state machine to figure out when to automatically switch back to alpha mode. * Returns true if the keyboard needs to switch back */ - boolean onKey(int key) { + public boolean 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) { @@ -440,11 +483,14 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha if (PREF_KEYBOARD_LAYOUT.equals(key)) { changeLatinKeyboardView( Integer.valueOf(sharedPreferences.getString(key, DEFAULT_LAYOUT_ID)), false); + } else if (LatinIMESettings.PREF_SETTINGS_KEY.equals(key)) { + updateSettingsKeyState(sharedPreferences); + recreateInputView(); } } public boolean isBlackSym () { - if (mInputView != null && mInputView.getSymbolColorSheme() == 1) { + if (mInputView != null && mInputView.getSymbolColorScheme() == 1) { return true; } return false; @@ -466,4 +512,19 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha .onAutoCompletionStateChanged(isAutoCompletion)); } } + + private void updateSettingsKeyState(SharedPreferences prefs) { + Resources resources = mInputMethodService.getResources(); + 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.hasMultipleEnabledIMEs(mInputMethodService))) { + mHasSettingsKey = true; + } else { + mHasSettingsKey = false; + } + } } diff --git a/java/src/com/android/inputmethod/latin/LanguageSwitcher.java b/java/src/com/android/inputmethod/latin/LanguageSwitcher.java index 578c80526..7b5c30491 100644 --- a/java/src/com/android/inputmethod/latin/LanguageSwitcher.java +++ b/java/src/com/android/inputmethod/latin/LanguageSwitcher.java @@ -188,7 +188,7 @@ public class LanguageSwitcher { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mIme); Editor editor = sp.edit(); editor.putString(LatinIME.PREF_INPUT_LANGUAGE, getInputLanguage()); - editor.commit(); + SharedPreferencesCompat.apply(editor); } static String toTitleCase(String s) { diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 7097844e2..7fa763c5e 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -1,6 +1,6 @@ /* * 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 @@ -228,8 +228,9 @@ public class LatinIME extends InputMethodService private int mDeleteCount; private long mLastKeyTime; - // Shift modifier key state + // Modifier keys state private ModifierKeyState mShiftKeyState = new ModifierKeyState(); + private ModifierKeyState mSymbolKeyState = new ModifierKeyState(); private Tutorial mTutorial; @@ -260,7 +261,7 @@ public class LatinIME extends InputMethodService List<String> candidates; Map<String, List<CharSequence>> alternatives; } - + public abstract static class WordAlternatives { protected CharSequence mChosenWord; @@ -354,7 +355,7 @@ public class LatinIME extends InputMethodService final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); mLanguageSwitcher = new LanguageSwitcher(this); mLanguageSwitcher.loadLocales(prefs); - mKeyboardSwitcher = new KeyboardSwitcher(this, this); + mKeyboardSwitcher = new KeyboardSwitcher(this); mKeyboardSwitcher.setLanguageSwitcher(mLanguageSwitcher); mSystemLocale = conf.locale.toString(); mLanguageSwitcher.setSystemLocale(conf.locale); @@ -596,9 +597,10 @@ public class LatinIME extends InputMethodService switch (attribute.inputType & EditorInfo.TYPE_MASK_CLASS) { case EditorInfo.TYPE_CLASS_NUMBER: case EditorInfo.TYPE_CLASS_DATETIME: - mKeyboardSwitcher.setKeyboardMode(KeyboardSwitcher.MODE_SYMBOLS, - attribute.imeOptions, enableVoiceButton); - break; + // fall through + // NOTE: For now, we use the phone keyboard for NUMBER and DATETIME until we get + // a dedicated number entry keypad. + // TODO: Use a dedicated number entry keypad here when we get one. case EditorInfo.TYPE_CLASS_PHONE: mKeyboardSwitcher.setKeyboardMode(KeyboardSwitcher.MODE_PHONE, attribute.imeOptions, enableVoiceButton); @@ -793,6 +795,37 @@ public class LatinIME extends InputMethodService } } + /** + * This is called when the user has clicked on the extracted text view, + * when running in fullscreen mode. The default implementation hides + * the candidates view when this happens, but only if the extracted text + * editor has a vertical scroll bar because its text doesn't fit. + * Here we override the behavior due to the possibility that a re-correction could + * cause the candidate strip to disappear and re-appear. + */ + @Override + public void onExtractedTextClicked() { + if (mReCorrectionEnabled && isPredictionOn()) return; + + super.onExtractedTextClicked(); + } + + /** + * This is called when the user has performed a cursor movement in the + * extracted text view, when it is running in fullscreen mode. The default + * implementation hides the candidates view when a vertical movement + * happens, but only if the extracted text editor has a vertical scroll bar + * because its text doesn't fit. + * Here we override the behavior due to the possibility that a re-correction could + * cause the candidate strip to disappear and re-appear. + */ + @Override + public void onExtractedCursorMovement(int dx, int dy) { + if (mReCorrectionEnabled && isPredictionOn()) return; + + super.onExtractedCursorMovement(dx, dy); + } + @Override public void hideWindow() { LatinImeLogger.commit(); @@ -951,7 +984,7 @@ public class LatinIME extends InputMethodService private void reloadKeyboards() { if (mKeyboardSwitcher == null) { - mKeyboardSwitcher = new KeyboardSwitcher(this, this); + mKeyboardSwitcher = new KeyboardSwitcher(this); } mKeyboardSwitcher.setLanguageSwitcher(mLanguageSwitcher); if (mKeyboardSwitcher.getInputView() != null @@ -1089,11 +1122,6 @@ public class LatinIME extends InputMethodService } } - private boolean hasMultipleEnabledIMEs() { - return ((InputMethodManager) getSystemService( - INPUT_METHOD_SERVICE)).getEnabledInputMethodList().size() > 1; - } - private void showInputMethodPicker() { ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)) .showInputMethodPicker(); @@ -1101,7 +1129,7 @@ public class LatinIME extends InputMethodService private void onOptionKeyPressed() { if (!isShowingOptionDialog()) { - if (hasMultipleEnabledIMEs()) { + if (LatinIMEUtil.hasMultipleEnabledIMEs(this)) { showOptionsMenu(); } else { launchSettings(); @@ -1111,7 +1139,7 @@ public class LatinIME extends InputMethodService private void onOptionKeyLongPressed() { if (!isShowingOptionDialog()) { - if (hasMultipleEnabledIMEs()) { + if (LatinIMEUtil.hasMultipleEnabledIMEs(this)) { showInputMethodPicker(); } else { launchSettings(); @@ -1132,6 +1160,7 @@ public class LatinIME extends InputMethodService mDeleteCount = 0; } mLastKeyTime = when; + final boolean distinctMultiTouch = mKeyboardSwitcher.hasDistinctMultitouch(); switch (primaryCode) { case Keyboard.KEYCODE_DELETE: handleBackspace(); @@ -1140,9 +1169,14 @@ public class LatinIME extends InputMethodService break; case Keyboard.KEYCODE_SHIFT: // Shift key is handled in onPress() when device has distinct multi-touch panel. - if (!mKeyboardSwitcher.hasDistinctMultitouch()) + if (!distinctMultiTouch) handleShift(); break; + case Keyboard.KEYCODE_MODE_CHANGE: + // Symbol key is handled in onPress() when device has distinct multi-touch panel. + if (!distinctMultiTouch) + changeKeyboardMode(); + break; case Keyboard.KEYCODE_CANCEL: if (!isShowingOptionDialog()) { handleClose(); @@ -1160,10 +1194,6 @@ public class LatinIME extends InputMethodService case LatinKeyboardView.KEYCODE_PREV_LANGUAGE: toggleLanguage(false, false); break; - case Keyboard.KEYCODE_MODE_CHANGE: - // TODO: Mode change (symbol key) should be handled in onPress(). - changeKeyboardMode(); - break; case LatinKeyboardView.KEYCODE_VOICE: if (VOICE_INSTALLED) { startListening(false /* was a button press, was not a swipe */); @@ -1344,14 +1374,21 @@ public class LatinIME extends InputMethodService } } if (mKeyboardSwitcher.getInputView().isShifted()) { - // TODO: This doesn't work with [beta], need to fix it in the next release. if (keyCodes == null || keyCodes[0] < Character.MIN_CODE_POINT || keyCodes[0] > Character.MAX_CODE_POINT) { return; } primaryCode = keyCodes[0]; - if (mKeyboardSwitcher.isAlphabetMode()) { - primaryCode = Character.toUpperCase(primaryCode); + if (mKeyboardSwitcher.isAlphabetMode() && Character.isLowerCase(primaryCode)) { + int upperCaseCode = Character.toUpperCase(primaryCode); + if (upperCaseCode != primaryCode) { + primaryCode = upperCaseCode; + } else { + // Some keys, such as [eszett], have upper case as multi-characters. + String upperCase = new String(new int[] {primaryCode}, 0, 1).toUpperCase(); + onText(upperCase); + return; + } } } if (mPredicting) { @@ -1547,7 +1584,7 @@ public class LatinIME extends InputMethodService SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit(); editor.putBoolean(PREF_HAS_USED_VOICE_INPUT, true); - editor.commit(); + SharedPreferencesCompat.apply(editor); mHasUsedVoiceInput = true; } @@ -1557,7 +1594,7 @@ public class LatinIME extends InputMethodService SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit(); editor.putBoolean(PREF_HAS_USED_VOICE_INPUT_UNSUPPORTED_LOCALE, true); - editor.commit(); + SharedPreferencesCompat.apply(editor); mHasUsedVoiceInputUnsupportedLocale = true; } @@ -2209,13 +2246,16 @@ public class LatinIME extends InputMethodService public void onPress(int primaryCode) { vibrate(); playKeyClick(primaryCode); - if (mKeyboardSwitcher.hasDistinctMultitouch() && primaryCode == Keyboard.KEYCODE_SHIFT) { + final boolean distinctMultiTouch = mKeyboardSwitcher.hasDistinctMultitouch(); + if (distinctMultiTouch && primaryCode == Keyboard.KEYCODE_SHIFT) { mShiftKeyState.onPress(); handleShift(); - } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) { - // TODO: We should handle KEYCODE_MODE_CHANGE (symbol) here as well. + } else if (distinctMultiTouch && primaryCode == Keyboard.KEYCODE_MODE_CHANGE) { + mSymbolKeyState.onPress(); + changeKeyboardMode(); } else { mShiftKeyState.onOtherKeyPressed(); + mSymbolKeyState.onOtherKeyPressed(); } } @@ -2223,12 +2263,15 @@ public class LatinIME extends InputMethodService // Reset any drag flags in the keyboard ((LatinKeyboard) mKeyboardSwitcher.getInputView().getKeyboard()).keyReleased(); //vibrate(); - if (mKeyboardSwitcher.hasDistinctMultitouch() && primaryCode == Keyboard.KEYCODE_SHIFT) { + final boolean distinctMultiTouch = mKeyboardSwitcher.hasDistinctMultitouch(); + if (distinctMultiTouch && primaryCode == Keyboard.KEYCODE_SHIFT) { if (mShiftKeyState.isMomentary()) resetShift(); mShiftKeyState.onRelease(); - } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) { - // TODO: We should handle KEYCODE_MODE_CHANGE (symbol) here as well. + } else if (distinctMultiTouch && primaryCode == Keyboard.KEYCODE_MODE_CHANGE) { + if (mSymbolKeyState.isMomentary()) + changeKeyboardMode(); + mSymbolKeyState.onRelease(); } } diff --git a/java/src/com/android/inputmethod/latin/LatinIMESettings.java b/java/src/com/android/inputmethod/latin/LatinIMESettings.java index 565c1e6e8..f9534d265 100644 --- a/java/src/com/android/inputmethod/latin/LatinIMESettings.java +++ b/java/src/com/android/inputmethod/latin/LatinIMESettings.java @@ -46,6 +46,7 @@ public class LatinIMESettings extends PreferenceActivity private static final String PREDICTION_SETTINGS_KEY = "prediction_settings"; private static final String VOICE_SETTINGS_KEY = "voice_mode"; private static final String DEBUG_MODE_KEY = "debug_mode"; + /* package */ static final String PREF_SETTINGS_KEY = "settings_key"; private static final String TAG = "LatinIMESettings"; @@ -55,6 +56,7 @@ public class LatinIMESettings extends PreferenceActivity private CheckBoxPreference mQuickFixes; private CheckBoxPreference mDebugMode; private ListPreference mVoicePreference; + private ListPreference mSettingsKeyPreference; private boolean mVoiceOn; private VoiceInputLogger mLogger; @@ -68,6 +70,7 @@ public class LatinIMESettings extends PreferenceActivity addPreferencesFromResource(R.xml.prefs); mQuickFixes = (CheckBoxPreference) findPreference(QUICK_FIXES_KEY); mVoicePreference = (ListPreference) findPreference(VOICE_SETTINGS_KEY); + mSettingsKeyPreference = (ListPreference) findPreference(PREF_SETTINGS_KEY); SharedPreferences prefs = getPreferenceManager().getSharedPreferences(); prefs.registerOnSharedPreferenceChangeListener(this); @@ -93,6 +96,7 @@ public class LatinIMESettings extends PreferenceActivity } else { updateVoiceModeSummary(); } + updateSettingsKeySummary(); } @Override @@ -106,7 +110,7 @@ public class LatinIMESettings extends PreferenceActivity (new BackupManager(this)).dataChanged(); // If turning on voice input, show dialog if (key.equals(VOICE_SETTINGS_KEY) && !mVoiceOn) { - if (! prefs.getString(VOICE_SETTINGS_KEY, mVoiceModeOff) + if (!prefs.getString(VOICE_SETTINGS_KEY, mVoiceModeOff) .equals(mVoiceModeOff)) { showVoiceConfirmation(); } @@ -118,6 +122,13 @@ public class LatinIMESettings extends PreferenceActivity } mVoiceOn = !(prefs.getString(VOICE_SETTINGS_KEY, mVoiceModeOff).equals(mVoiceModeOff)); updateVoiceModeSummary(); + updateSettingsKeySummary(); + } + + private void updateSettingsKeySummary() { + mSettingsKeyPreference.setSummary( + getResources().getStringArray(R.array.settings_key_modes) + [mSettingsKeyPreference.findIndexOfValue(mSettingsKeyPreference.getValue())]); } private void updateDebugMode() { diff --git a/java/src/com/android/inputmethod/latin/LatinIMEUtil.java b/java/src/com/android/inputmethod/latin/LatinIMEUtil.java index 93ad4072d..34b52845e 100644 --- a/java/src/com/android/inputmethod/latin/LatinIMEUtil.java +++ b/java/src/com/android/inputmethod/latin/LatinIMEUtil.java @@ -16,6 +16,9 @@ package com.android.inputmethod.latin; +import android.view.inputmethod.InputMethodManager; + +import android.content.Context; import android.os.AsyncTask; import android.text.format.DateUtils; import android.util.Log; @@ -72,4 +75,9 @@ public class LatinIMEUtil { } } } + + public static boolean hasMultipleEnabledIMEs(Context context) { + return ((InputMethodManager) context.getSystemService( + Context.INPUT_METHOD_SERVICE)).getEnabledInputMethodList().size() > 1; + } } diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboard.java b/java/src/com/android/inputmethod/latin/LatinKeyboard.java index caae92363..14a503bc3 100644 --- a/java/src/com/android/inputmethod/latin/LatinKeyboard.java +++ b/java/src/com/android/inputmethod/latin/LatinKeyboard.java @@ -56,25 +56,28 @@ public class LatinKeyboard extends Keyboard { private Drawable mMicPreviewIcon; private Drawable m123MicIcon; private Drawable m123MicPreviewIcon; - private Drawable mButtonArrowLeftIcon; - private Drawable mButtonArrowRightIcon; + private final Drawable mButtonArrowLeftIcon; + private final Drawable mButtonArrowRightIcon; private Key mShiftKey; private Key mEnterKey; private Key mF1Key; private Key mSpaceKey; private Key m123Key; + private final int NUMBER_HINT_COUNT = 10; + private Key[] mNumberHintKeys; + private Drawable[] mNumberHintIcons = new Drawable[NUMBER_HINT_COUNT]; private int mSpaceKeyIndex = -1; private int mSpaceDragStartX; private int mSpaceDragLastDiff; - /* package */ Locale mLocale; + private Locale mLocale; private LanguageSwitcher mLanguageSwitcher; - private Resources mRes; - private Context mContext; + private final Resources mRes; + private final Context mContext; // Whether this keyboard has voice icon on it private boolean mHasVoiceButton; // Whether voice icon is enabled at all private boolean mVoiceEnabled; - private boolean mIsAlphaKeyboard; + private final boolean mIsAlphaKeyboard; private CharSequence m123Label; private boolean mCurrentlyInSpace; private SlidingLocaleDrawable mSlidingLocaleIcon; @@ -96,8 +99,15 @@ public class LatinKeyboard extends Keyboard { private static final float SPACEBAR_DRAG_THRESHOLD = 0.8f; private static final float OVERLAP_PERCENTAGE_LOW_PROB = 0.70f; private static final float OVERLAP_PERCENTAGE_HIGH_PROB = 0.85f; + // Minimum width of space key preview (proportional to keyboard width) + private static final float SPACEBAR_POPUP_MIN_RATIO = 0.4f; + // Height in space key the language name will be drawn. (proportional to space key height) + private static final float SPACEBAR_LANGUAGE_BASELINE = 0.6f; + // If the full language name needs to be smaller than this value to be drawn on space key, + // its short language name will be used instead. + private static final float MINIMUM_SCALE_OF_LANGUAGE_NAME = 0.8f; - static int sSpacebarVerticalCorrection; + private static int sSpacebarVerticalCorrection; public LatinKeyboard(Context context, int xmlLayoutResId) { this(context, xmlLayoutResId, 0); @@ -128,12 +138,22 @@ public class LatinKeyboard extends Keyboard { R.dimen.spacebar_vertical_correction); mIsAlphaKeyboard = xmlLayoutResId == R.xml.kbd_qwerty || xmlLayoutResId == R.xml.kbd_qwerty_black; - mSpaceKeyIndex = indexOf(' '); + mSpaceKeyIndex = indexOf(LatinIME.KEYCODE_SPACE); + initializeNumberHintResources(context); } - public LatinKeyboard(Context context, int layoutTemplateResId, - CharSequence characters, int columns, int horizontalPadding) { - super(context, layoutTemplateResId, characters, columns, horizontalPadding); + private void initializeNumberHintResources(Context context) { + final Resources res = context.getResources(); + mNumberHintIcons[0] = res.getDrawable(R.drawable.keyboard_hint_0); + mNumberHintIcons[1] = res.getDrawable(R.drawable.keyboard_hint_1); + mNumberHintIcons[2] = res.getDrawable(R.drawable.keyboard_hint_2); + mNumberHintIcons[3] = res.getDrawable(R.drawable.keyboard_hint_3); + mNumberHintIcons[4] = res.getDrawable(R.drawable.keyboard_hint_4); + mNumberHintIcons[5] = res.getDrawable(R.drawable.keyboard_hint_5); + mNumberHintIcons[6] = res.getDrawable(R.drawable.keyboard_hint_6); + mNumberHintIcons[7] = res.getDrawable(R.drawable.keyboard_hint_7); + mNumberHintIcons[8] = res.getDrawable(R.drawable.keyboard_hint_8); + mNumberHintIcons[9] = res.getDrawable(R.drawable.keyboard_hint_9); } @Override @@ -141,13 +161,13 @@ public class LatinKeyboard extends Keyboard { XmlResourceParser parser) { Key key = new LatinKey(res, parent, x, y, parser); switch (key.codes[0]) { - case 10: + case LatinIME.KEYCODE_ENTER: mEnterKey = key; break; case LatinKeyboardView.KEYCODE_F1: mF1Key = key; break; - case 32: + case LatinIME.KEYCODE_SPACE: mSpaceKey = key; break; case KEYCODE_MODE_CHANGE: @@ -155,6 +175,23 @@ public class LatinKeyboard extends Keyboard { m123Label = key.label; break; } + + // For number hints on the upper-right corner of key + if (mNumberHintKeys == null) { + // NOTE: This protected method is being called from the base class constructor before + // mNumberHintKeys gets initialized. + mNumberHintKeys = new Key[NUMBER_HINT_COUNT]; + } + int hintNumber = -1; + if (LatinKeyboardBaseView.isNumberAtLeftmostPopupChar(key)) { + hintNumber = key.popupCharacters.charAt(0) - '0'; + } else if (LatinKeyboardBaseView.isNumberAtRightmostPopupChar(key)) { + hintNumber = key.popupCharacters.charAt(key.popupCharacters.length() - 1) - '0'; + } + if (hintNumber >= 0 && hintNumber <= 9) { + mNumberHintKeys[hintNumber] = key; + } + return key; } @@ -298,6 +335,7 @@ public class LatinKeyboard extends Keyboard { if (mSpaceKey != null) { updateSpaceBarForLocale(isAutoCompletion, isBlack); } + updateNumberHintKeys(); } private void setDefaultBounds(Drawable drawable) { @@ -345,12 +383,23 @@ public class LatinKeyboard extends Keyboard { return mSpaceKey; } + private void updateNumberHintKeys() { + for (int i = 0; i < mNumberHintKeys.length; ++i) { + if (mNumberHintKeys[i] != null) { + mNumberHintKeys[i].icon = mNumberHintIcons[i]; + } + } + } + + public boolean isLanguageSwitchEnabled() { + return mLocale != null; + } + private void updateSpaceBarForLocale(boolean isAutoCompletion, boolean isBlack) { // If application locales are explicitly selected. if (mLocale != null) { mSpaceKey.icon = new BitmapDrawable(mRes, drawSpaceBar(OPACITY_FULLY_OPAQUE, isAutoCompletion, isBlack)); - mSpaceKey.repeatable = mLanguageSwitcher.getLocaleCount() < 2; } else { // sym_keyboard_space_led can be shared with Black and White symbol themes. if (isAutoCompletion) { @@ -360,47 +409,97 @@ public class LatinKeyboard extends Keyboard { mSpaceKey.icon = isBlack ? mRes.getDrawable(R.drawable.sym_bkeyboard_space) : mRes.getDrawable(R.drawable.sym_keyboard_space); } - mSpaceKey.repeatable = true; } } + // Compute width of text with specified text size using paint. + private static int getTextWidth(Paint paint, String text, float textSize, Rect bounds) { + paint.setTextSize(textSize); + paint.getTextBounds(text, 0, text.length(), bounds); + return bounds.width(); + } + + // Layout local language name and left and right arrow on space bar. + private static String layoutSpaceBar(Paint paint, Locale locale, Drawable lArrow, + Drawable rArrow, int width, int height, float origTextSize, + boolean allowVariableTextSize) { + final float arrowWidth = lArrow.getIntrinsicWidth(); + final float arrowHeight = lArrow.getIntrinsicHeight(); + final float maxTextWidth = width - (arrowWidth + arrowWidth); + final Rect bounds = new Rect(); + + // Estimate appropriate language name text size to fit in maxTextWidth. + String language = LanguageSwitcher.toTitleCase(locale.getDisplayLanguage(locale)); + int textWidth = getTextWidth(paint, language, origTextSize, bounds); + // Assuming text width and text size are proportional to each other. + float textSize = origTextSize * Math.min(maxTextWidth / textWidth, 1.0f); + + final boolean useShortName; + if (allowVariableTextSize) { + textWidth = getTextWidth(paint, language, textSize, bounds); + // If text size goes too small or text does not fit, use short name + useShortName = textSize / origTextSize < MINIMUM_SCALE_OF_LANGUAGE_NAME + || textWidth > maxTextWidth; + } else { + useShortName = textWidth > maxTextWidth; + textSize = origTextSize; + } + if (useShortName) { + language = LanguageSwitcher.toTitleCase(locale.getLanguage()); + textWidth = getTextWidth(paint, language, origTextSize, bounds); + textSize = origTextSize * Math.min(maxTextWidth / textWidth, 1.0f); + } + paint.setTextSize(textSize); + + // Place left and right arrow just before and after language text. + final float baseline = height * SPACEBAR_LANGUAGE_BASELINE; + final int top = (int)(baseline - arrowHeight); + final float remains = (width - textWidth) / 2; + lArrow.setBounds((int)(remains - arrowWidth), top, (int)remains, (int)baseline); + rArrow.setBounds((int)(remains + textWidth), top, (int)(remains + textWidth + arrowWidth), + (int)baseline); + + return language; + } + private Bitmap drawSpaceBar(int opacity, boolean isAutoCompletion, boolean isBlack) { - int width = mSpaceKey.width; - int height = mSpaceIcon.getIntrinsicHeight(); - Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(buffer); + final int width = mSpaceKey.width; + final int height = mSpaceIcon.getIntrinsicHeight(); + final Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + final Canvas canvas = new Canvas(buffer); canvas.drawColor(mRes.getColor(R.color.latinkeyboard_transparent), PorterDuff.Mode.CLEAR); + // If application locales are explicitly selected. if (mLocale != null) { - Paint paint = new Paint(); - paint.setAntiAlias(true); + final Paint paint = new Paint(); paint.setAlpha(opacity); - // Get the text size from the theme - paint.setTextSize(getTextSizeFromTheme(android.R.style.TextAppearance_Small, 14)); + paint.setAntiAlias(true); paint.setTextAlign(Align.CENTER); - final String language = getInputLanguage(mSpaceKey.width, paint); - final int ascent = (int) -paint.ascent(); - - int shadowColor = isBlack ? - mRes.getColor(R.color.latinkeyboard_bar_language_shadow_black) - : mRes.getColor(R.color.latinkeyboard_bar_language_shadow_white); + final boolean allowVariableTextSize = true; + final String language = layoutSpaceBar(paint, mLanguageSwitcher.getInputLocale(), + mButtonArrowLeftIcon, mButtonArrowRightIcon, width, height, + getTextSizeFromTheme(android.R.style.TextAppearance_Small, 14), + allowVariableTextSize); + + // Draw language text with shadow + final int shadowColor = mRes.getColor(isBlack + ? R.color.latinkeyboard_bar_language_shadow_black + : R.color.latinkeyboard_bar_language_shadow_white); + final float baseline = height * SPACEBAR_LANGUAGE_BASELINE; + final float descent = paint.descent(); paint.setColor(shadowColor); - canvas.drawText(language, width / 2, ascent - 1, paint); + canvas.drawText(language, width / 2, baseline - descent - 1, paint); paint.setColor(mRes.getColor(R.color.latinkeyboard_bar_language_text)); - canvas.drawText(language, width / 2, ascent, paint); - // Put arrows on either side of the text + canvas.drawText(language, width / 2, baseline - descent, paint); + + // Put arrows that are already layed out on either side of the text if (mLanguageSwitcher.getLocaleCount() > 1) { - Rect bounds = new Rect(); - paint.getTextBounds(language, 0, language.length(), bounds); - drawButtonArrow(mButtonArrowLeftIcon, canvas, - (mSpaceKey.width - bounds.right) / 2 - - mButtonArrowLeftIcon.getIntrinsicWidth(), - (int) paint.getTextSize()); - drawButtonArrow(mButtonArrowRightIcon, canvas, - (mSpaceKey.width + bounds.right) / 2, (int) paint.getTextSize()); + mButtonArrowLeftIcon.draw(canvas); + mButtonArrowRightIcon.draw(canvas); } } + // Draw the spacebar icon at the bottom if (isAutoCompletion) { final int iconWidth = width * SPACE_LED_LENGTH_PERCENT / 100; @@ -420,38 +519,13 @@ public class LatinKeyboard extends Keyboard { return buffer; } - private void drawButtonArrow(Drawable arrow, Canvas canvas, int x, int bottomY) { - arrow.setBounds(x, bottomY - arrow.getIntrinsicHeight(), x + arrow.getIntrinsicWidth(), - bottomY); - arrow.draw(canvas); - } - - private String getInputLanguage(int widthAvail, Paint paint) { - return chooseDisplayName(mLanguageSwitcher.getInputLocale(), widthAvail, paint); - } - - private String getNextInputLanguage(int widthAvail, Paint paint) { - return chooseDisplayName(mLanguageSwitcher.getNextInputLocale(), widthAvail, paint); - } - - private String getPrevInputLanguage(int widthAvail, Paint paint) { - return chooseDisplayName(mLanguageSwitcher.getPrevInputLocale(), widthAvail, paint); - } - - private String chooseDisplayName(Locale locale, int widthAvail, Paint paint) { - if (widthAvail < (int) (.35 * getMinWidth())) { - return LanguageSwitcher.toTitleCase(locale.getLanguage().substring(0, 2)); - } else { - return LanguageSwitcher.toTitleCase(locale.getDisplayLanguage(locale)); - } - } - private void updateLocaleDrag(int diff) { if (mSlidingLocaleIcon == null) { - mSlidingLocaleIcon = new SlidingLocaleDrawable(mSpacePreviewIcon, mSpaceKey.width, - mSpacePreviewIcon.getIntrinsicHeight()); - mSlidingLocaleIcon.setBounds(0, 0, mSpaceKey.width, - mSpacePreviewIcon.getIntrinsicHeight()); + final int width = Math.max(mSpaceKey.width, + (int)(getMinWidth() * SPACEBAR_POPUP_MIN_RATIO)); + final int height = mSpacePreviewIcon.getIntrinsicHeight(); + mSlidingLocaleIcon = new SlidingLocaleDrawable(mSpacePreviewIcon, width, height); + mSlidingLocaleIcon.setBounds(0, 0, width, height); mSpaceKey.iconPreview = mSlidingLocaleIcon; } mSlidingLocaleIcon.setDiff(diff); @@ -484,9 +558,8 @@ public class LatinKeyboard extends Keyboard { .equalsIgnoreCase(locale.getLanguage())) { locale = null; } - setColorOfSymbolIcons(isAutoCompletion, isBlackSym); - if (mLocale != null && mLocale.equals(locale)) return; mLocale = locale; + setColorOfSymbolIcons(isAutoCompletion, isBlackSym); } boolean isCurrentlyInSpace() { @@ -737,17 +810,16 @@ public class LatinKeyboard extends Keyboard { */ class SlidingLocaleDrawable extends Drawable { - private int mWidth; - private int mHeight; - private Drawable mBackground; + private final int mWidth; + private final int mHeight; + private final Drawable mBackground; + private final TextPaint mTextPaint; + private final int mMiddleX; + private final Drawable mLeftDrawable; + private final Drawable mRightDrawable; + private final int mThreshold; private int mDiff; - private TextPaint mTextPaint; - private int mMiddleX; - private int mAscent; - private Drawable mLeftDrawable; - private Drawable mRightDrawable; private boolean mHitThreshold; - private int mThreshold; private String mCurrentLanguage; private String mNextLanguage; private String mPrevLanguage; @@ -759,26 +831,20 @@ public class LatinKeyboard extends Keyboard { mWidth = width; mHeight = height; mTextPaint = new TextPaint(); - int textSize = getTextSizeFromTheme(android.R.style.TextAppearance_Medium, 18); - mTextPaint.setTextSize(textSize); + mTextPaint.setTextSize(getTextSizeFromTheme(android.R.style.TextAppearance_Medium, 18)); mTextPaint.setColor(R.color.latinkeyboard_transparent); mTextPaint.setTextAlign(Align.CENTER); mTextPaint.setAlpha(OPACITY_FULLY_OPAQUE); mTextPaint.setAntiAlias(true); - mAscent = (int) mTextPaint.ascent(); mMiddleX = (mWidth - mBackground.getIntrinsicWidth()) / 2; mLeftDrawable = mRes.getDrawable(R.drawable.sym_keyboard_feedback_language_arrows_left); mRightDrawable = mRes.getDrawable(R.drawable.sym_keyboard_feedback_language_arrows_right); - mLeftDrawable.setBounds(0, 0, - mLeftDrawable.getIntrinsicWidth(), mLeftDrawable.getIntrinsicHeight()); - mRightDrawable.setBounds(mWidth - mRightDrawable.getIntrinsicWidth(), 0, - mWidth, mRightDrawable.getIntrinsicHeight()); mThreshold = ViewConfiguration.get(mContext).getScaledTouchSlop(); } - void setDiff(int diff) { + private void setDiff(int diff) { if (diff == Integer.MAX_VALUE) { mHitThreshold = false; mCurrentLanguage = null; @@ -791,25 +857,39 @@ public class LatinKeyboard extends Keyboard { invalidateSelf(); } + private String getLanguageName(Locale locale) { + return LanguageSwitcher.toTitleCase(locale.getDisplayLanguage(locale)); + } + @Override public void draw(Canvas canvas) { canvas.save(); if (mHitThreshold) { - mTextPaint.setColor(mRes.getColor(R.color.latinkeyboard_feedback_language_text)); - canvas.clipRect(0, 0, mWidth, mHeight); + Paint paint = mTextPaint; + final int width = mWidth; + final int height = mHeight; + final int diff = mDiff; + final Drawable lArrow = mLeftDrawable; + final Drawable rArrow = mRightDrawable; + canvas.clipRect(0, 0, width, height); if (mCurrentLanguage == null) { - mCurrentLanguage = getInputLanguage(mWidth, mTextPaint); - mNextLanguage = getNextInputLanguage(mWidth, mTextPaint); - mPrevLanguage = getPrevInputLanguage(mWidth, mTextPaint); + final LanguageSwitcher languageSwitcher = mLanguageSwitcher; + mCurrentLanguage = getLanguageName(languageSwitcher.getInputLocale()); + mNextLanguage = getLanguageName(languageSwitcher.getNextInputLocale()); + mPrevLanguage = getLanguageName(languageSwitcher.getPrevInputLocale()); } - canvas.drawText(mCurrentLanguage, - mWidth / 2 + mDiff, -mAscent + 4, mTextPaint); - canvas.drawText(mNextLanguage, - mDiff - mWidth / 2, -mAscent + 4, mTextPaint); - canvas.drawText(mPrevLanguage, - mDiff + mWidth + mWidth / 2, -mAscent + 4, mTextPaint); - mLeftDrawable.draw(canvas); - mRightDrawable.draw(canvas); + // Draw language text with shadow + final float baseline = mHeight * SPACEBAR_LANGUAGE_BASELINE - paint.descent(); + paint.setColor(mRes.getColor(R.color.latinkeyboard_feedback_language_text)); + canvas.drawText(mCurrentLanguage, width / 2 + diff, baseline, paint); + canvas.drawText(mNextLanguage, diff - width / 2, baseline, paint); + canvas.drawText(mPrevLanguage, diff + width + width / 2, baseline, paint); + + lArrow.setBounds(0, 0, lArrow.getIntrinsicWidth(), lArrow.getIntrinsicHeight()); + rArrow.setBounds(width - rArrow.getIntrinsicWidth(), 0, width, + rArrow.getIntrinsicHeight()); + lArrow.draw(canvas); + rArrow.draw(canvas); } if (mBackground != null) { canvas.translate(mMiddleX, 0); diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java b/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java index 8f1ec6591..dafbb669e 100644 --- a/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java +++ b/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java @@ -48,6 +48,7 @@ import android.widget.TextView; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; +import java.util.List; import java.util.Map; /** @@ -153,7 +154,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx // Timing constants private static final int DELAY_BEFORE_PREVIEW = 0; - private static final int DELAY_AFTER_PREVIEW = 70; + private static final int DELAY_AFTER_PREVIEW = 100; private static final int REPEAT_INTERVAL = PointerTracker.REPEAT_INTERVAL; // Miscellaneous constants @@ -170,6 +171,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx private float mShadowRadius; private Drawable mKeyBackground; private float mBackgroundDimAmount; + private float mKeyHysteresisDistance; private float mVerticalCorrection; private int mPreviewOffset; private int mPreviewHeight; @@ -180,7 +182,6 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx private Key[] mKeys; // Key preview popup - private final static boolean PREVIEW_CENTERED = false; private TextView mPreviewText; private PopupWindow mPreviewPopup; private int mPreviewTextSizeLarge; @@ -188,11 +189,10 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx private int mOldPreviewKeyIndex = NOT_A_KEY; private boolean mShowPreview = true; private boolean mShowTouchPoints = true; - private int mPopupPreviewX; - private int mPopupPreviewY; private int mPopupPreviewOffsetX; private int mPopupPreviewOffsetY; private int mWindowY; + private int mPopupPreviewDisplayedY; // Popup mini keyboard private PopupWindow mMiniKeyboardPopup; @@ -203,13 +203,13 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx private int mMiniKeyboardOriginY; private long mMiniKeyboardPopupTime; private int[] mWindowOffset; + private final float mMiniKeyboardSlideAllowance; /** Listener for {@link OnKeyboardActionListener}. */ private OnKeyboardActionListener mKeyboardActionListener; private final ArrayList<PointerTracker> mPointerTrackers = new ArrayList<PointerTracker>(); private final PointerQueue mPointerQueue = new PointerQueue(); - private final float mDebounceHysteresis; private final boolean mHasDistinctMultitouch; private int mOldPointerCount = 1; @@ -347,7 +347,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx return -1; } - public void releasePointersOlderThan(PointerTracker tracker, long eventTime) { + public void releaseAllPointersOlderThan(PointerTracker tracker, long eventTime) { LinkedList<PointerTracker> queue = mQueue; int oldestPos = 0; for (PointerTracker t = queue.get(oldestPos); t != tracker; t = queue.get(oldestPos)) { @@ -355,11 +355,24 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx oldestPos++; } else { t.onUpEvent(t.getLastX(), t.getLastY(), eventTime); + t.setAlreadyProcessed(); queue.remove(oldestPos); } } } + public void releaseAllPointersExcept(PointerTracker tracker, long eventTime) { + for (PointerTracker t : mQueue) { + if (t == tracker) + continue; + t.onUpEvent(t.getLastX(), t.getLastY(), eventTime); + t.setAlreadyProcessed(); + } + mQueue.clear(); + if (tracker != null) + mQueue.add(tracker); + } + public void remove(PointerTracker tracker) { mQueue.remove(tracker); } @@ -388,6 +401,9 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx case R.styleable.LatinKeyboardBaseView_keyBackground: mKeyBackground = a.getDrawable(attr); break; + case R.styleable.LatinKeyboardBaseView_keyHysteresisDistance: + mKeyHysteresisDistance = a.getDimensionPixelOffset(attr, 0); + break; case R.styleable.LatinKeyboardBaseView_verticalCorrection: mVerticalCorrection = a.getDimensionPixelOffset(attr, 0); break; @@ -472,7 +488,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx mSwipeThreshold = (int) (500 * res.getDisplayMetrics().density); // TODO: Refer frameworks/base/core/res/res/values/config.xml mDisambiguateSwipe = res.getBoolean(R.bool.config_swipeDisambiguation); - mDebounceHysteresis = res.getDimension(R.dimen.key_debounce_hysteresis_distance); + mMiniKeyboardSlideAllowance = res.getDimension(R.dimen.mini_keyboard_slide_allowance); GestureDetector.SimpleOnGestureListener listener = new GestureDetector.SimpleOnGestureListener() { @@ -555,7 +571,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx mKeys = mKeyDetector.setKeyboard(keyboard, -getPaddingLeft(), -getPaddingTop() + mVerticalCorrection); for (PointerTracker tracker : mPointerTrackers) { - tracker.setKeyboard(mKeys, mDebounceHysteresis); + tracker.setKeyboard(mKeys, mKeyHysteresisDistance); } requestLayout(); // Hint to reallocate the buffer if the size changed @@ -629,7 +645,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx return mShowPreview; } - public int getSymbolColorSheme() { + public int getSymbolColorScheme() { return mSymbolColorScheme; } @@ -781,6 +797,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx canvas.translate(key.x + kbdPaddingLeft, key.y + kbdPaddingTop); keyBackground.draw(canvas); + boolean shouldDrawIcon = true; if (label != null) { // For characters, use large font. For labels like "Done", use small font. if (label.length() > 1 && key.codes.length < 2) { @@ -801,14 +818,24 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx paint); // Turn off drop shadow paint.setShadowLayer(0, 0, 0, 0); - } else if (key.icon != null) { + + // Usually don't draw icon if label is not null, but we draw icon for the number + // hint. + shouldDrawIcon = isNumberAtEdgeOfPopupChars(key); + } + if (key.icon != null && shouldDrawIcon) { + // Special handing for the upper-right number hint icons + final int drawableWidth = isNumberAtEdgeOfPopupChars(key) ? + key.width : key.icon.getIntrinsicWidth(); + final int drawableHeight = isNumberAtEdgeOfPopupChars(key) ? + key.height : key.icon.getIntrinsicHeight(); + final int drawableX = (key.width - padding.left - padding.right - - key.icon.getIntrinsicWidth()) / 2 + padding.left; + - drawableWidth) / 2 + padding.left; final int drawableY = (key.height - padding.top - padding.bottom - - key.icon.getIntrinsicHeight()) / 2 + padding.top; + - drawableHeight) / 2 + padding.top; canvas.translate(drawableX, drawableY); - key.icon.setBounds(0, 0, - key.icon.getIntrinsicWidth(), key.icon.getIntrinsicHeight()); + key.icon.setBounds(0, 0, drawableWidth, drawableHeight); key.icon.draw(canvas); canvas.translate(-drawableX, -drawableY); } @@ -854,8 +881,17 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx public void showPreview(int keyIndex, PointerTracker tracker) { int oldKeyIndex = mOldPreviewKeyIndex; mOldPreviewKeyIndex = keyIndex; - // If key changed and preview is on ... - if (oldKeyIndex != keyIndex && mShowPreview) { + final boolean isLanguageSwitchEnabled = (mKeyboard instanceof LatinKeyboard) + && ((LatinKeyboard)mKeyboard).isLanguageSwitchEnabled(); + // We should re-draw popup preview when 1) we need to hide the preview, 2) we will show + // the space key preview and 3) pointer moves off the space key to other letter key, we + // should hide the preview of the previous key. + final boolean hidePreviewOrShowSpaceKeyPreview = (tracker == null) + || tracker.isSpaceKey(keyIndex) || tracker.isSpaceKey(oldKeyIndex); + // If key changed and preview is on or the key is space (language switch is enabled) + if (oldKeyIndex != keyIndex + && (mShowPreview + || (hidePreviewOrShowSpaceKeyPreview && isLanguageSwitchEnabled))) { if (keyIndex == NOT_A_KEY) { mHandler.cancelPopupPreview(); mHandler.dismissPreview(DELAY_AFTER_PREVIEW); @@ -869,8 +905,8 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx Key key = tracker.getKey(keyIndex); if (key == null) return; - final PopupWindow previewPopup = mPreviewPopup; - if (key.icon != null) { + // Should not draw number hint icons + if (key.icon != null && !isNumberAtEdgeOfPopupChars(key)) { mPreviewText.setCompoundDrawables(null, null, null, key.iconPreview != null ? key.iconPreview : key.icon); mPreviewText.setText(null); @@ -895,51 +931,48 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx lp.width = popupWidth; lp.height = popupHeight; } - if (PREVIEW_CENTERED) { - // TODO: Fix this if centering is brought back - mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2; - mPopupPreviewY = - mPreviewText.getMeasuredHeight(); - } else { - mPopupPreviewX = key.x - mPreviewText.getPaddingLeft() + getPaddingLeft(); - mPopupPreviewY = key.y - popupHeight + mPreviewOffset; - } + + int popupPreviewX = key.x - (popupWidth - key.width) / 2; + int popupPreviewY = key.y - popupHeight + mPreviewOffset; + mHandler.cancelDismissPreview(); if (mOffsetInWindow == null) { mOffsetInWindow = new int[2]; getLocationInWindow(mOffsetInWindow); mOffsetInWindow[0] += mPopupPreviewOffsetX; // Offset may be zero mOffsetInWindow[1] += mPopupPreviewOffsetY; // Offset may be zero - int[] mWindowLocation = new int[2]; - getLocationOnScreen(mWindowLocation); - mWindowY = mWindowLocation[1]; + int[] windowLocation = new int[2]; + getLocationOnScreen(windowLocation); + mWindowY = windowLocation[1]; } // Set the preview background state mPreviewText.getBackground().setState( key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET); - mPopupPreviewX += mOffsetInWindow[0]; - mPopupPreviewY += mOffsetInWindow[1]; + popupPreviewX += mOffsetInWindow[0]; + popupPreviewY += mOffsetInWindow[1]; // If the popup cannot be shown above the key, put it on the side - if (mPopupPreviewY + mWindowY < 0) { + if (popupPreviewY + mWindowY < 0) { // If the key you're pressing is on the left side of the keyboard, show the popup on // the right, offset by enough to see at least one key to the left/right. if (key.x + key.width <= getWidth() / 2) { - mPopupPreviewX += (int) (key.width * 2.5); + popupPreviewX += (int) (key.width * 2.5); } else { - mPopupPreviewX -= (int) (key.width * 2.5); + popupPreviewX -= (int) (key.width * 2.5); } - mPopupPreviewY += popupHeight; + popupPreviewY += popupHeight; } - if (previewPopup.isShowing()) { - previewPopup.update(mPopupPreviewX, mPopupPreviewY, - popupWidth, popupHeight); + if (mPreviewPopup.isShowing()) { + mPreviewPopup.update(popupPreviewX, popupPreviewY, popupWidth, popupHeight); } else { - previewPopup.setWidth(popupWidth); - previewPopup.setHeight(popupHeight); - previewPopup.showAtLocation(mMiniKeyboardParent, Gravity.NO_GRAVITY, - mPopupPreviewX, mPopupPreviewY); + mPreviewPopup.setWidth(popupWidth); + mPreviewPopup.setHeight(popupHeight); + mPreviewPopup.showAtLocation(mMiniKeyboardParent, Gravity.NO_GRAVITY, + popupPreviewX, popupPreviewY); } + // Record popup preview position to display mini-keyboard later at the same positon + mPopupPreviewDisplayedY = popupPreviewY; mPreviewText.setVisibility(VISIBLE); } @@ -998,8 +1031,9 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx if (container == null) throw new NullPointerException(); - mMiniKeyboard = (LatinKeyboardBaseView)container.findViewById(R.id.LatinKeyboardBaseView); - mMiniKeyboard.setOnKeyboardActionListener(new OnKeyboardActionListener() { + LatinKeyboardBaseView miniKeyboard = + (LatinKeyboardBaseView)container.findViewById(R.id.LatinKeyboardBaseView); + miniKeyboard.setOnKeyboardActionListener(new OnKeyboardActionListener() { public void onKey(int primaryCode, int[] keyCodes, int x, int y) { mKeyboardActionListener.onKey(primaryCode, keyCodes, x, y); dismissPopupKeyboard(); @@ -1029,6 +1063,8 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx mKeyboardActionListener.onRelease(primaryCode); } }); + // Override default ProximityKeyDetector. + miniKeyboard.mKeyDetector = new MiniKeyboardKeyDetector(mMiniKeyboardSlideAllowance); Keyboard keyboard; if (popupKey.popupCharacters != null) { @@ -1037,8 +1073,8 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx } else { keyboard = new Keyboard(getContext(), popupKeyboardId); } - mMiniKeyboard.setKeyboard(keyboard); - mMiniKeyboard.setPopupParent(this); + miniKeyboard.setKeyboard(keyboard); + miniKeyboard.setPopupParent(this); container.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST)); @@ -1046,6 +1082,19 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx return container; } + private static boolean isOneRowKeyboard(Keyboard keyboard) { + final List<Key> keys = keyboard.getKeys(); + if (keys.size() == 0) return false; + final int edgeFlags = keys.get(0).edgeFlags; + // HACK: The first key of mini keyboard which was inflated from xml and has multiple rows, + // does not have both top and bottom edge flags on at the same time. On the other hand, + // the first key of mini keyboard that was created with popupCharacters must have both top + // and bottom edge flags on. + // When you want to use one row mini-keyboard from xml file, make sure that the row has + // both top and bottom edge flags set. + return (edgeFlags & Keyboard.EDGE_TOP) != 0 && (edgeFlags & Keyboard.EDGE_BOTTOM) != 0; + } + /** * Called when a key is long pressed. By default this will open any popup keyboard associated * with this key through the attributes popupLayout and popupCharacters. @@ -1070,17 +1119,35 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx mWindowOffset = new int[2]; getLocationInWindow(mWindowOffset); } - int popupX = popupKey.x + popupKey.width + getPaddingLeft(); - int popupY = popupKey.y + getPaddingTop(); - popupX -= container.getMeasuredWidth(); + + // HACK: Have the leftmost number in the popup characters right above the key + boolean isNumberAtLeftmost = + hasMultiplePopupChars(popupKey) && isNumberAtLeftmostPopupChar(popupKey); + int popupX = popupKey.x + mWindowOffset[0]; + int popupY = popupKey.y + mWindowOffset[1]; + if (isNumberAtLeftmost) { + popupX -= container.getPaddingLeft(); + } else { + popupX += popupKey.width + getPaddingLeft(); + popupX -= container.getMeasuredWidth(); + popupX += container.getPaddingRight(); + } + popupY += getPaddingTop(); popupY -= container.getMeasuredHeight(); - popupX += mWindowOffset[0]; - popupY += mWindowOffset[1]; - final int x = popupX + container.getPaddingRight(); - final int y = popupY + container.getPaddingBottom(); - mMiniKeyboardOriginX = (x < 0 ? 0 : x) + container.getPaddingLeft(); - mMiniKeyboardOriginY = y + container.getPaddingTop(); - mMiniKeyboard.setPopupOffset((x < 0) ? 0 : x, y); + popupY += container.getPaddingBottom(); + final int x = popupX; + final int y = mShowPreview && isOneRowKeyboard(mMiniKeyboard.getKeyboard()) + ? mPopupPreviewDisplayedY : popupY; + + int adjustedX = x; + if (x < 0) { + adjustedX = 0; + } else if (x > (getMeasuredWidth() - container.getMeasuredWidth())) { + adjustedX = getMeasuredWidth() - container.getMeasuredWidth(); + } + mMiniKeyboardOriginX = adjustedX + container.getPaddingLeft() - mWindowOffset[0]; + mMiniKeyboardOriginY = y + container.getPaddingTop() - mWindowOffset[1]; + mMiniKeyboard.setPopupOffset(adjustedX, y); mMiniKeyboard.setShifted(isShifted()); // Mini keyboard needs no pop-up key preview displayed. mMiniKeyboard.setPreviewEnabled(false); @@ -1101,9 +1168,40 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx return true; } + private static boolean hasMultiplePopupChars(Key key) { + if (key.popupCharacters != null && key.popupCharacters.length() > 1) { + return true; + } + return false; + } + + private static boolean isNumberAtEdgeOfPopupChars(Key key) { + return isNumberAtLeftmostPopupChar(key) || isNumberAtRightmostPopupChar(key); + } + + /* package */ static boolean isNumberAtLeftmostPopupChar(Key key) { + if (key.popupCharacters != null && key.popupCharacters.length() > 0 + && isAsciiDigit(key.popupCharacters.charAt(0))) { + return true; + } + return false; + } + + /* package */ static boolean isNumberAtRightmostPopupChar(Key key) { + if (key.popupCharacters != null && key.popupCharacters.length() > 0 + && isAsciiDigit(key.popupCharacters.charAt(key.popupCharacters.length() - 1))) { + return true; + } + return false; + } + + private static boolean isAsciiDigit(char c) { + return (c < 0x80) && Character.isDigit(c); + } + private MotionEvent generateMiniKeyboardMotionEvent(int action, int x, int y, long eventTime) { return MotionEvent.obtain(mMiniKeyboardPopupTime, eventTime, action, - x - mMiniKeyboardOriginX, y - mMiniKeyboardOriginY, 0); + x - mMiniKeyboardOriginX, y - mMiniKeyboardOriginY, 0); } private PointerTracker getPointerTracker(final int id) { @@ -1116,7 +1214,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx final PointerTracker tracker = new PointerTracker(i, mHandler, mKeyDetector, this, mHasDistinctMultitouch); if (keys != null) - tracker.setKeyboard(keys, mDebounceHysteresis); + tracker.setKeyboard(keys, mKeyHysteresisDistance); if (listener != null) tracker.setOnKeyboardActionListener(listener); pointers.add(tracker); @@ -1227,17 +1325,28 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx } private void onDownEvent(PointerTracker tracker, int x, int y, long eventTime) { + if (tracker.isOnModifierKey(x, y)) { + // Before processing a down event of modifier key, all pointers already being tracked + // should be released. + mPointerQueue.releaseAllPointersExcept(null, eventTime); + } tracker.onDownEvent(x, y, eventTime); mPointerQueue.add(tracker); } private void onUpEvent(PointerTracker tracker, int x, int y, long eventTime) { - int index = mPointerQueue.lastIndexOf(tracker); - if (index >= 0) { - mPointerQueue.releasePointersOlderThan(tracker, eventTime); + if (tracker.isModifier()) { + // Before processing an up event of modifier key, all pointers already being tracked + // should be released. + mPointerQueue.releaseAllPointersExcept(tracker, eventTime); } else { - Log.w(TAG, "onUpEvent: corresponding down event not found for pointer " - + tracker.mPointerId); + int index = mPointerQueue.lastIndexOf(tracker); + if (index >= 0) { + mPointerQueue.releaseAllPointersOlderThan(tracker, eventTime); + } else { + Log.w(TAG, "onUpEvent: corresponding down event not found for pointer " + + tracker.mPointerId); + } } tracker.onUpEvent(x, y, eventTime); mPointerQueue.remove(tracker); diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboardView.java b/java/src/com/android/inputmethod/latin/LatinKeyboardView.java index 8f20a22d0..2872f6b46 100644 --- a/java/src/com/android/inputmethod/latin/LatinKeyboardView.java +++ b/java/src/com/android/inputmethod/latin/LatinKeyboardView.java @@ -24,6 +24,7 @@ import android.inputmethodservice.Keyboard.Key; import android.os.Handler; import android.os.Message; import android.os.SystemClock; +import android.text.TextUtils; import android.util.AttributeSet; import android.view.MotionEvent; @@ -52,9 +53,6 @@ public class LatinKeyboardView extends LatinKeyboardBaseView { /** The y coordinate of the last row */ private int mLastRowY; - // This is local working variable for onLongPress(). - private int[] mKeyCodes = new int[1]; - public LatinKeyboardView(Context context, AttributeSet attrs) { super(context, attrs); } @@ -68,6 +66,16 @@ public class LatinKeyboardView extends LatinKeyboardBaseView { } @Override + public void setPreviewEnabled(boolean previewEnabled) { + if (getKeyboard() == mPhoneKeyboard) { + // Phone keyboard never shows popup preview (except language switch). + super.setPreviewEnabled(false); + } else { + super.setPreviewEnabled(previewEnabled); + } + } + + @Override public void setKeyboard(Keyboard k) { super.setKeyboard(k); // One-seventh of the keyboard width seems like a reasonable threshold @@ -104,7 +112,7 @@ public class LatinKeyboardView extends LatinKeyboardBaseView { if (keyboard.isShifted() && keyboard instanceof LatinKeyboard && ((LatinKeyboard) keyboard).isAlphaKeyboard() - && label != null && label.length() < 3 + && !TextUtils.isEmpty(label) && label.length() < 3 && Character.isLowerCase(label.charAt(0))) { label = label.toString().toUpperCase(); } diff --git a/java/src/com/android/inputmethod/latin/MiniKeyboardKeyDetector.java b/java/src/com/android/inputmethod/latin/MiniKeyboardKeyDetector.java new file mode 100644 index 000000000..356e62d48 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/MiniKeyboardKeyDetector.java @@ -0,0 +1,59 @@ +/* + * 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.inputmethodservice.Keyboard.Key; + +class MiniKeyboardKeyDetector extends KeyDetector { + private static final int MAX_NEARBY_KEYS = 1; + + private final int mSlideAllowanceSquare; + private final int mSlideAllowanceSquareTop; + + public MiniKeyboardKeyDetector(float slideAllowance) { + super(); + mSlideAllowanceSquare = (int)(slideAllowance * slideAllowance); + // Top slide allowance is slightly longer (sqrt(2) times) than other edges. + mSlideAllowanceSquareTop = mSlideAllowanceSquare * 2; + } + + @Override + protected int getMaxNearbyKeys() { + return MAX_NEARBY_KEYS; + } + + @Override + public int getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys) { + final Key[] keys = getKeys(); + final int touchX = getTouchX(x); + final int touchY = getTouchY(y); + int closestKeyIndex = LatinKeyboardBaseView.NOT_A_KEY; + int closestKeyDist = (y < 0) ? mSlideAllowanceSquareTop : mSlideAllowanceSquare; + final int keyCount = keys.length; + for (int i = 0; i < keyCount; i++) { + final Key key = keys[i]; + int dist = key.squaredDistanceFrom(touchX, touchY); + if (dist < closestKeyDist) { + closestKeyIndex = i; + closestKeyDist = dist; + } + } + if (allKeys != null && closestKeyIndex != LatinKeyboardBaseView.NOT_A_KEY) + allKeys[0] = keys[closestKeyIndex].codes[0]; + return closestKeyIndex; + } +} diff --git a/java/src/com/android/inputmethod/latin/PointerTracker.java b/java/src/com/android/inputmethod/latin/PointerTracker.java index 8b1f019d4..cb717cbe7 100644 --- a/java/src/com/android/inputmethod/latin/PointerTracker.java +++ b/java/src/com/android/inputmethod/latin/PointerTracker.java @@ -55,7 +55,7 @@ public class PointerTracker { private final boolean mHasDistinctMultitouch; private Key[] mKeys; - private int mKeyDebounceThresholdSquared = -1; + private int mKeyHysteresisDistanceSquared = -1; private int mCurrentKey = NOT_A_KEY; private int mStartX; @@ -106,11 +106,13 @@ public class PointerTracker { mListener = listener; } - public void setKeyboard(Key[] keys, float hysteresisPixel) { - if (keys == null || hysteresisPixel < 1.0f) + public void setKeyboard(Key[] keys, float keyHysteresisDistance) { + if (keys == null || keyHysteresisDistance < 0) throw new IllegalArgumentException(); mKeys = keys; - mKeyDebounceThresholdSquared = (int)(hysteresisPixel * hysteresisPixel); + mKeyHysteresisDistanceSquared = (int)(keyHysteresisDistance * keyHysteresisDistance); + // Update current key index because keyboard layout has been changed. + mCurrentKey = mKeyDetector.getKeyIndexAndNearbyCodes(mStartX, mStartY, null); } private boolean isValidKeyIndex(int keyIndex) { @@ -121,13 +123,26 @@ public class PointerTracker { return isValidKeyIndex(keyIndex) ? mKeys[keyIndex] : null; } - public boolean isModifier() { - Key key = getKey(mCurrentKey); + private boolean isModifierInternal(int keyIndex) { + Key key = getKey(keyIndex); if (key == null) return false; int primaryCode = key.codes[0]; - // TODO: KEYCODE_MODE_CHANGE (symbol) will be also a modifier key - return primaryCode == Keyboard.KEYCODE_SHIFT; + return primaryCode == Keyboard.KEYCODE_SHIFT + || primaryCode == Keyboard.KEYCODE_MODE_CHANGE; + } + + public boolean isModifier() { + return isModifierInternal(mCurrentKey); + } + + public boolean isOnModifierKey(int x, int y) { + return isModifierInternal(mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null)); + } + + public boolean isSpaceKey(int keyIndex) { + Key key = getKey(keyIndex); + return key != null && key.codes[0] == LatinIME.KEYCODE_SPACE; } public void updateKey(int keyIndex) { @@ -173,6 +188,8 @@ public class PointerTracker { } public void onDownEvent(int x, int y, long eventTime) { + if (DEBUG) + debugLog("onDownEvent:", x, y); int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null); mCurrentKey = keyIndex; mStartX = x; @@ -186,6 +203,8 @@ public class PointerTracker { if (mListener != null) { int primaryCode = isValidKeyIndex(keyIndex) ? mKeys[keyIndex].codes[0] : 0; mListener.onPress(primaryCode); + // This onPress call may have changed keyboard layout and have updated mCurrentKey + keyIndex = mCurrentKey; } if (isValidKeyIndex(keyIndex)) { if (mKeys[keyIndex].repeatable) { @@ -197,11 +216,11 @@ public class PointerTracker { } showKeyPreviewAndUpdateKey(keyIndex); updateMoveDebouncing(x, y); - if (DEBUG) - debugLog("onDownEvent:", x, y); } public void onMoveEvent(int x, int y, long eventTime) { + if (DEBUG_MOVE) + debugLog("onMoveEvent:", x, y); if (mKeyAlreadyProcessed) return; int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null); @@ -242,15 +261,13 @@ public class PointerTracker { */ showKeyPreviewAndUpdateKey(isMinorTimeBounce() ? mLastKey : mCurrentKey); updateMoveDebouncing(x, y); - if (DEBUG_MOVE) - debugLog("onMoveEvent:", x, y); } public void onUpEvent(int x, int y, long eventTime) { - if (mKeyAlreadyProcessed) - return; if (DEBUG) debugLog("onUpEvent :", x, y); + if (mKeyAlreadyProcessed) + return; mHandler.cancelKeyTimers(); mHandler.cancelPopupPreview(); int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null); @@ -331,13 +348,12 @@ public class PointerTracker { } private boolean isMinorMoveBounce(int x, int y, int newKey, int curKey) { - if (mKeys == null || mKeyDebounceThresholdSquared < 0) + if (mKeys == null || mKeyHysteresisDistanceSquared < 0) throw new IllegalStateException("keyboard and/or hysteresis not set"); if (newKey == curKey) { return true; } else if (isValidKeyIndex(curKey)) { - return getSquareDistanceToKeyEdge(x, y, mKeys[curKey]) - < mKeyDebounceThresholdSquared; + return getSquareDistanceToKeyEdge(x, y, mKeys[curKey]) < mKeyHysteresisDistanceSquared; } else { return false; } @@ -384,8 +400,11 @@ public class PointerTracker { // The modifier key, such as shift key, should not be shown as preview when multi-touch is // supported. On thge other hand, if multi-touch is not supported, the modifier key should // be shown as preview. - if (!isModifier() || !mHasDistinctMultitouch) + if (mHasDistinctMultitouch && isModifier()) { + mProxy.showPreview(NOT_A_KEY, this); + } else { mProxy.showPreview(keyIndex, this); + } } private void detectAndSendKey(int index, int x, int y, long eventTime) { @@ -478,7 +497,7 @@ public class PointerTracker { } private void debugLog(String title, int x, int y) { - Key key = getKey(mCurrentKey); + Key key = getKey(mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null)); final String code; if (key == null) { code = "----"; diff --git a/java/src/com/android/inputmethod/latin/SharedPreferencesCompat.java b/java/src/com/android/inputmethod/latin/SharedPreferencesCompat.java new file mode 100644 index 000000000..8364c90fa --- /dev/null +++ b/java/src/com/android/inputmethod/latin/SharedPreferencesCompat.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2010 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 android.content.SharedPreferences; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Reflection utils to call SharedPreferences$Editor.apply when possible, + * falling back to commit when apply isn't available. + */ +public class SharedPreferencesCompat { + private static final Method sApplyMethod = findApplyMethod(); + + private static Method findApplyMethod() { + try { + Class cls = SharedPreferences.Editor.class; + return cls.getMethod("apply"); + } catch (NoSuchMethodException unused) { + // fall through + } + return null; + } + + public static void apply(SharedPreferences.Editor editor) { + if (sApplyMethod != null) { + try { + sApplyMethod.invoke(editor); + return; + } catch (InvocationTargetException unused) { + // fall through + } catch (IllegalAccessException unused) { + // fall through + } + } + editor.commit(); + } +} |