diff options
Diffstat (limited to 'java/src/com')
78 files changed, 2998 insertions, 966 deletions
diff --git a/java/src/com/android/inputmethod/accessibility/AccessibilityUtils.java b/java/src/com/android/inputmethod/accessibility/AccessibilityUtils.java new file mode 100644 index 000000000..ae614b7e0 --- /dev/null +++ b/java/src/com/android/inputmethod/accessibility/AccessibilityUtils.java @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2011 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.accessibility; + +import android.accessibilityservice.AccessibilityServiceInfo; +import android.content.Context; +import android.content.SharedPreferences; +import android.inputmethodservice.InputMethodService; +import android.os.SystemClock; +import android.util.Log; +import android.view.MotionEvent; +import android.view.accessibility.AccessibilityEvent; +import android.view.accessibility.AccessibilityManager; + +import com.android.inputmethod.compat.AccessibilityEventCompatUtils; +import com.android.inputmethod.compat.AccessibilityManagerCompatWrapper; +import com.android.inputmethod.compat.MotionEventCompatUtils; + +public class AccessibilityUtils { + private static final String TAG = AccessibilityUtils.class.getSimpleName(); + private static final String CLASS = AccessibilityUtils.class.getClass().getName(); + private static final String PACKAGE = AccessibilityUtils.class.getClass().getPackage() + .getName(); + + private static final AccessibilityUtils sInstance = new AccessibilityUtils(); + + private AccessibilityManager mAccessibilityManager; + private AccessibilityManagerCompatWrapper mCompatManager; + + /* + * Setting this constant to {@code false} will disable all keyboard + * accessibility code, regardless of whether Accessibility is turned on in + * the system settings. It should ONLY be used in the event of an emergency. + */ + private static final boolean ENABLE_ACCESSIBILITY = true; + + public static void init(InputMethodService inputMethod, SharedPreferences prefs) { + if (!ENABLE_ACCESSIBILITY) + return; + + // These only need to be initialized if the kill switch is off. + sInstance.initInternal(inputMethod, prefs); + KeyCodeDescriptionMapper.init(inputMethod, prefs); + AccessibleInputMethodServiceProxy.init(inputMethod, prefs); + AccessibleKeyboardViewProxy.init(inputMethod, prefs); + } + + public static AccessibilityUtils getInstance() { + return sInstance; + } + + private AccessibilityUtils() { + // This class is not publicly instantiable. + } + + private void initInternal(Context context, SharedPreferences prefs) { + mAccessibilityManager = (AccessibilityManager) context + .getSystemService(Context.ACCESSIBILITY_SERVICE); + mCompatManager = new AccessibilityManagerCompatWrapper(mAccessibilityManager); + } + + /** + * Returns {@code true} if touch exploration is enabled. Currently, this + * means that the kill switch is off, the device supports touch exploration, + * and a spoken feedback service is turned on. + * + * @return {@code true} if touch exploration is enabled. + */ + public boolean isTouchExplorationEnabled() { + return ENABLE_ACCESSIBILITY + && AccessibilityEventCompatUtils.supportsTouchExploration() + && mAccessibilityManager.isEnabled() + && !mCompatManager.getEnabledAccessibilityServiceList( + AccessibilityServiceInfo.FEEDBACK_SPOKEN).isEmpty(); + } + + /** + * Returns {@true} if the provided event is a touch exploration (e.g. hover) + * event. This is used to determine whether the event should be processed by + * the touch exploration code within the keyboard. + * + * @param event The event to check. + * @return {@true} is the event is a touch exploration event + */ + public boolean isTouchExplorationEvent(MotionEvent event) { + final int action = event.getAction(); + + return action == MotionEventCompatUtils.ACTION_HOVER_ENTER + || action == MotionEventCompatUtils.ACTION_HOVER_EXIT + || action == MotionEventCompatUtils.ACTION_HOVER_MOVE; + } + + /** + * Sends the specified text to the {@link AccessibilityManager} to be + * spoken. + * + * @param text the text to speak + */ + public void speak(CharSequence text) { + if (!mAccessibilityManager.isEnabled()) { + Log.e(TAG, "Attempted to speak when accessibility was disabled!"); + return; + } + + // The following is a hack to avoid using the heavy-weight TextToSpeech + // class. Instead, we're just forcing a fake AccessibilityEvent into + // the screen reader to make it speak. + final AccessibilityEvent event = AccessibilityEvent + .obtain(AccessibilityEventCompatUtils.TYPE_VIEW_HOVER_ENTER); + + event.setPackageName(PACKAGE); + event.setClassName(CLASS); + event.setEventTime(SystemClock.uptimeMillis()); + event.setEnabled(true); + event.getText().add(text); + + mAccessibilityManager.sendAccessibilityEvent(event); + } +} diff --git a/java/src/com/android/inputmethod/accessibility/AccessibleInputMethodServiceProxy.java b/java/src/com/android/inputmethod/accessibility/AccessibleInputMethodServiceProxy.java new file mode 100644 index 000000000..043266c70 --- /dev/null +++ b/java/src/com/android/inputmethod/accessibility/AccessibleInputMethodServiceProxy.java @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2011 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.accessibility; + +import android.content.SharedPreferences; +import android.inputmethodservice.InputMethodService; +import android.os.Handler; +import android.os.Looper; +import android.os.Message; +import android.text.TextUtils; +import android.view.inputmethod.ExtractedText; +import android.view.inputmethod.ExtractedTextRequest; + +import com.android.inputmethod.latin.R; + +public class AccessibleInputMethodServiceProxy implements AccessibleKeyboardActionListener { + private static final AccessibleInputMethodServiceProxy sInstance = + new AccessibleInputMethodServiceProxy(); + + /* + * Delay for the handler event that's fired when Accessibility is on and the + * user hovers outside of any valid keys. This is used to let the user know + * that if they lift their finger, nothing will be typed. + */ + private static final long DELAY_NO_HOVER_SELECTION = 250; + + private InputMethodService mInputMethod; + + private AccessibilityHandler mAccessibilityHandler; + + private class AccessibilityHandler extends Handler { + private static final int MSG_NO_HOVER_SELECTION = 0; + + public AccessibilityHandler(Looper looper) { + super(looper); + } + + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case MSG_NO_HOVER_SELECTION: + notifyNoHoverSelection(); + break; + } + } + + public void postNoHoverSelection() { + removeMessages(MSG_NO_HOVER_SELECTION); + sendEmptyMessageDelayed(MSG_NO_HOVER_SELECTION, DELAY_NO_HOVER_SELECTION); + } + + public void cancelNoHoverSelection() { + removeMessages(MSG_NO_HOVER_SELECTION); + } + } + + public static void init(InputMethodService inputMethod, SharedPreferences prefs) { + sInstance.initInternal(inputMethod, prefs); + } + + public static AccessibleInputMethodServiceProxy getInstance() { + return sInstance; + } + + private AccessibleInputMethodServiceProxy() { + // Not publicly instantiable. + } + + private void initInternal(InputMethodService inputMethod, SharedPreferences prefs) { + mInputMethod = inputMethod; + mAccessibilityHandler = new AccessibilityHandler(inputMethod.getMainLooper()); + } + + /** + * If touch exploration is enabled, cancels the event sent by + * {@link AccessibleInputMethodServiceProxy#onHoverExit(int)} because the + * user is currently hovering above a key. + */ + @Override + public void onHoverEnter(int primaryCode) { + mAccessibilityHandler.cancelNoHoverSelection(); + } + + /** + * If touch exploration is enabled, sends a delayed event to notify the user + * that they are not currently hovering above a key. + */ + @Override + public void onHoverExit(int primaryCode) { + mAccessibilityHandler.postNoHoverSelection(); + } + + /** + * When Accessibility is turned on, notifies the user that they are not + * currently hovering above a key. By default this will speak the currently + * entered text. + */ + private void notifyNoHoverSelection() { + final ExtractedText extracted = mInputMethod.getCurrentInputConnection().getExtractedText( + new ExtractedTextRequest(), 0); + + if (extracted == null) + return; + + final CharSequence text; + + if (TextUtils.isEmpty(extracted.text)) { + text = mInputMethod.getString(R.string.spoken_no_text_entered); + } else { + text = mInputMethod.getString(R.string.spoken_current_text_is, extracted.text); + } + + AccessibilityUtils.getInstance().speak(text); + } +} diff --git a/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardActionListener.java b/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardActionListener.java new file mode 100644 index 000000000..12c59d0fc --- /dev/null +++ b/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardActionListener.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2011 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.accessibility; + +public interface AccessibleKeyboardActionListener { + /** + * Called when the user hovers inside a key. This is sent only when + * Accessibility is turned on. For keys that repeat, this is only called + * once. + * + * @param primaryCode the code of the key that was hovered over + */ + public void onHoverEnter(int primaryCode); + + /** + * Called when the user hovers outside a key. This is sent only when + * Accessibility is turned on. For keys that repeat, this is only called + * once. + * + * @param primaryCode the code of the key that was hovered over + */ + public void onHoverExit(int primaryCode); +} diff --git a/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java b/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java new file mode 100644 index 000000000..96f7fc9f2 --- /dev/null +++ b/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java @@ -0,0 +1,201 @@ +/* + * Copyright (C) 2011 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.accessibility; + +import android.content.Context; +import android.content.SharedPreferences; +import android.graphics.Color; +import android.graphics.Paint; +import android.util.Log; +import android.view.MotionEvent; +import android.view.ViewConfiguration; +import android.view.accessibility.AccessibilityEvent; + +import com.android.inputmethod.compat.AccessibilityEventCompatUtils; +import com.android.inputmethod.compat.MotionEventCompatUtils; +import com.android.inputmethod.keyboard.Key; +import com.android.inputmethod.keyboard.KeyDetector; +import com.android.inputmethod.keyboard.KeyboardView; +import com.android.inputmethod.keyboard.PointerTracker; + +public class AccessibleKeyboardViewProxy { + private static final String TAG = AccessibleKeyboardViewProxy.class.getSimpleName(); + private static final AccessibleKeyboardViewProxy sInstance = new AccessibleKeyboardViewProxy(); + + // Delay in milliseconds between key press DOWN and UP events + private static final long DELAY_KEY_PRESS = 10; + + private int mScaledEdgeSlop; + private KeyboardView mView; + private AccessibleKeyboardActionListener mListener; + + private int mLastHoverKeyIndex = KeyDetector.NOT_A_KEY; + private int mLastX = -1; + private int mLastY = -1; + + public static void init(Context context, SharedPreferences prefs) { + sInstance.initInternal(context, prefs); + sInstance.mListener = AccessibleInputMethodServiceProxy.getInstance(); + } + + public static AccessibleKeyboardViewProxy getInstance() { + return sInstance; + } + + public static void setView(KeyboardView view) { + sInstance.mView = view; + } + + private AccessibleKeyboardViewProxy() { + // Not publicly instantiable. + } + + private void initInternal(Context context, SharedPreferences prefs) { + final Paint paint = new Paint(); + paint.setTextAlign(Paint.Align.LEFT); + paint.setTextSize(14.0f); + paint.setAntiAlias(true); + paint.setColor(Color.YELLOW); + + mScaledEdgeSlop = ViewConfiguration.get(context).getScaledEdgeSlop(); + } + + public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event, + PointerTracker tracker) { + if (mView == null) { + Log.e(TAG, "No keyboard view set!"); + return false; + } + + switch (event.getEventType()) { + case AccessibilityEventCompatUtils.TYPE_VIEW_HOVER_ENTER: + final Key key = tracker.getKey(mLastHoverKeyIndex); + + if (key == null) + break; + + final CharSequence description = KeyCodeDescriptionMapper.getInstance() + .getDescriptionForKey(mView.getContext(), mView.getKeyboard(), key); + + if (description == null) + return false; + + event.getText().add(description); + + break; + } + + return true; + } + + /** + * Receives hover events when accessibility is turned on in API > 11. In + * earlier API levels, events are manually routed from onTouchEvent. + * + * @param event The hover event. + * @return {@code true} if the event is handled + */ + public boolean onHoverEvent(MotionEvent event, PointerTracker tracker) { + return onTouchExplorationEvent(event, tracker); + } + + public boolean dispatchTouchEvent(MotionEvent event) { + // Since touch exploration translates hover double-tap to a regular + // single-tap, we're going to drop non-touch exploration events. + if (!AccessibilityUtils.getInstance().isTouchExplorationEvent(event)) + return true; + + return false; + } + + /** + * Handles touch exploration events when Accessibility is turned on. + * + * @param event The touch exploration hover event. + * @return {@code true} if the event was handled + */ + private boolean onTouchExplorationEvent(MotionEvent event, PointerTracker tracker) { + final int x = (int) event.getX(); + final int y = (int) event.getY(); + + switch (event.getAction()) { + case MotionEventCompatUtils.ACTION_HOVER_ENTER: + case MotionEventCompatUtils.ACTION_HOVER_MOVE: + final int keyIndex = tracker.getKeyIndexOn(x, y); + + if (keyIndex != mLastHoverKeyIndex) { + fireKeyHoverEvent(tracker, mLastHoverKeyIndex, false); + mLastHoverKeyIndex = keyIndex; + mLastX = x; + mLastY = y; + fireKeyHoverEvent(tracker, mLastHoverKeyIndex, true); + } + + return true; + case MotionEventCompatUtils.ACTION_HOVER_EXIT: + final int width = mView.getWidth(); + final int height = mView.getHeight(); + + if (x < mScaledEdgeSlop || y < mScaledEdgeSlop || x >= (width - mScaledEdgeSlop) + || y >= (height - mScaledEdgeSlop)) { + fireKeyHoverEvent(tracker, mLastHoverKeyIndex, false); + mLastHoverKeyIndex = KeyDetector.NOT_A_KEY; + mLastX = -1; + mLastY = -1; + } else if (mLastHoverKeyIndex != KeyDetector.NOT_A_KEY) { + fireKeyPressEvent(tracker, mLastX, mLastY, event.getEventTime()); + } + + return true; + } + + return false; + } + + private void fireKeyHoverEvent(PointerTracker tracker, int keyIndex, boolean entering) { + if (mListener == null) { + Log.e(TAG, "No accessible keyboard action listener set!"); + return; + } + + if (mView == null) { + Log.e(TAG, "No keyboard view set!"); + return; + } + + if (keyIndex == KeyDetector.NOT_A_KEY) + return; + + final Key key = tracker.getKey(keyIndex); + + if (key == null) + return; + + if (entering) { + mListener.onHoverEnter(key.mCode); + mView.sendAccessibilityEvent(AccessibilityEventCompatUtils.TYPE_VIEW_HOVER_ENTER); + } else { + mListener.onHoverExit(key.mCode); + mView.sendAccessibilityEvent(AccessibilityEventCompatUtils.TYPE_VIEW_HOVER_EXIT); + } + } + + private void fireKeyPressEvent(PointerTracker tracker, int x, int y, long eventTime) { + tracker.onDownEvent(x, y, eventTime, null); + tracker.onUpEvent(x, y, eventTime + DELAY_KEY_PRESS, null); + } +} diff --git a/java/src/com/android/inputmethod/accessibility/KeyCodeDescriptionMapper.java b/java/src/com/android/inputmethod/accessibility/KeyCodeDescriptionMapper.java new file mode 100644 index 000000000..154f4af91 --- /dev/null +++ b/java/src/com/android/inputmethod/accessibility/KeyCodeDescriptionMapper.java @@ -0,0 +1,226 @@ +/* + * Copyright (C) 2011 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.accessibility; + +import android.content.Context; +import android.content.SharedPreferences; +import android.text.TextUtils; + +import com.android.inputmethod.keyboard.Key; +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.KeyboardId; +import com.android.inputmethod.latin.R; + +import java.util.HashMap; + +public class KeyCodeDescriptionMapper { + private static KeyCodeDescriptionMapper sInstance = new KeyCodeDescriptionMapper(); + + // Map of key labels to spoken description resource IDs + private final HashMap<CharSequence, Integer> mKeyLabelMap; + + // Map of key codes to spoken description resource IDs + private final HashMap<Integer, Integer> mKeyCodeMap; + + // Map of shifted key codes to spoken description resource IDs + private final HashMap<Integer, Integer> mShiftedKeyCodeMap; + + // Map of shift-locked key codes to spoken description resource IDs + private final HashMap<Integer, Integer> mShiftLockedKeyCodeMap; + + public static void init(Context context, SharedPreferences prefs) { + sInstance.initInternal(context, prefs); + } + + public static KeyCodeDescriptionMapper getInstance() { + return sInstance; + } + + private KeyCodeDescriptionMapper() { + mKeyLabelMap = new HashMap<CharSequence, Integer>(); + mKeyCodeMap = new HashMap<Integer, Integer>(); + mShiftedKeyCodeMap = new HashMap<Integer, Integer>(); + mShiftLockedKeyCodeMap = new HashMap<Integer, Integer>(); + } + + private void initInternal(Context context, SharedPreferences prefs) { + // Manual label substitutions for key labels with no string resource + mKeyLabelMap.put(":-)", R.string.spoken_description_smiley); + + // Symbols that most TTS engines can't speak + mKeyCodeMap.put((int) '.', R.string.spoken_description_period); + mKeyCodeMap.put((int) ',', R.string.spoken_description_comma); + mKeyCodeMap.put((int) '(', R.string.spoken_description_left_parenthesis); + mKeyCodeMap.put((int) ')', R.string.spoken_description_right_parenthesis); + mKeyCodeMap.put((int) ':', R.string.spoken_description_colon); + mKeyCodeMap.put((int) ';', R.string.spoken_description_semicolon); + mKeyCodeMap.put((int) '!', R.string.spoken_description_exclamation_mark); + mKeyCodeMap.put((int) '?', R.string.spoken_description_question_mark); + mKeyCodeMap.put((int) '\"', R.string.spoken_description_double_quote); + mKeyCodeMap.put((int) '\'', R.string.spoken_description_single_quote); + mKeyCodeMap.put((int) '*', R.string.spoken_description_star); + mKeyCodeMap.put((int) '#', R.string.spoken_description_pound); + mKeyCodeMap.put((int) ' ', R.string.spoken_description_space); + + // Non-ASCII symbols (must use escape codes!) + mKeyCodeMap.put((int) '\u2022', R.string.spoken_description_dot); + mKeyCodeMap.put((int) '\u221A', R.string.spoken_description_square_root); + mKeyCodeMap.put((int) '\u03C0', R.string.spoken_description_pi); + mKeyCodeMap.put((int) '\u0394', R.string.spoken_description_delta); + mKeyCodeMap.put((int) '\u2122', R.string.spoken_description_trademark); + mKeyCodeMap.put((int) '\u2105', R.string.spoken_description_care_of); + mKeyCodeMap.put((int) '\u2026', R.string.spoken_description_ellipsis); + mKeyCodeMap.put((int) '\u201E', R.string.spoken_description_low_double_quote); + + // Special non-character codes defined in Keyboard + mKeyCodeMap.put(Keyboard.CODE_DELETE, R.string.spoken_description_delete); + mKeyCodeMap.put(Keyboard.CODE_ENTER, R.string.spoken_description_return); + mKeyCodeMap.put(Keyboard.CODE_SETTINGS, R.string.spoken_description_settings); + mKeyCodeMap.put(Keyboard.CODE_SHIFT, R.string.spoken_description_shift); + mKeyCodeMap.put(Keyboard.CODE_SHORTCUT, R.string.spoken_description_mic); + mKeyCodeMap.put(Keyboard.CODE_SWITCH_ALPHA_SYMBOL, R.string.spoken_description_to_symbol); + mKeyCodeMap.put(Keyboard.CODE_TAB, R.string.spoken_description_tab); + + // Shifted versions of non-character codes defined in Keyboard + mShiftedKeyCodeMap.put(Keyboard.CODE_SHIFT, R.string.spoken_description_shift_shifted); + + // Shift-locked versions of non-character codes defined in Keyboard + mShiftLockedKeyCodeMap.put(Keyboard.CODE_SHIFT, R.string.spoken_description_caps_lock); + } + + /** + * Returns the localized description of the action performed by a specified + * key based on the current keyboard state. + * <p> + * The order of precedence for key descriptions is: + * <ol> + * <li>Manually-defined based on the key label</li> + * <li>Automatic or manually-defined based on the key code</li> + * <li>Automatically based on the key label</li> + * <li>{code null} for keys with no label or key code defined</li> + * </p> + * + * @param context The package's context. + * @param keyboard The keyboard on which the key resides. + * @param key The key from which to obtain a description. + * @return a character sequence describing the action performed by pressing + * the key + */ + public CharSequence getDescriptionForKey(Context context, Keyboard keyboard, Key key) { + if (key.mCode == Keyboard.CODE_SWITCH_ALPHA_SYMBOL) { + final CharSequence description = getDescriptionForSwitchAlphaSymbol(context, keyboard); + if (description != null) + return description; + } + + if (!TextUtils.isEmpty(key.mLabel)) { + final String label = key.mLabel.toString().trim(); + + if (mKeyLabelMap.containsKey(label)) { + return context.getString(mKeyLabelMap.get(label)); + } else if (label.length() == 1 + || (keyboard.isManualTemporaryUpperCase() && !TextUtils + .isEmpty(key.mHintLetter))) { + return getDescriptionForKeyCode(context, keyboard, key); + } else { + return label; + } + } else if (key.mCode != Keyboard.CODE_DUMMY) { + return getDescriptionForKeyCode(context, keyboard, key); + } + + return null; + } + + /** + * Returns a context-specific description for the CODE_SWITCH_ALPHA_SYMBOL + * key or {@code null} if there is not a description provided for the + * current keyboard context. + * + * @param context The package's context. + * @param keyboard The keyboard on which the key resides. + * @return a character sequence describing the action performed by pressing + * the key + */ + private CharSequence getDescriptionForSwitchAlphaSymbol(Context context, Keyboard keyboard) { + final KeyboardId id = keyboard.mId; + + if (id.isAlphabetKeyboard()) { + return context.getString(R.string.spoken_description_to_symbol); + } else if (id.isSymbolsKeyboard()) { + return context.getString(R.string.spoken_description_to_alpha); + } else if (id.isPhoneSymbolsKeyboard()) { + return context.getString(R.string.spoken_description_to_numeric); + } else if (id.isPhoneKeyboard()) { + return context.getString(R.string.spoken_description_to_symbol); + } else { + return null; + } + } + + /** + * Returns the keycode for the specified key given the current keyboard + * state. + * + * @param keyboard The keyboard on which the key resides. + * @param key The key from which to obtain a key code. + * @return the key code for the specified key + */ + private int getCorrectKeyCode(Keyboard keyboard, Key key) { + if (keyboard.isManualTemporaryUpperCase() && !TextUtils.isEmpty(key.mHintLetter)) { + return key.mHintLetter.charAt(0); + } else { + return key.mCode; + } + } + + /** + * Returns a localized character sequence describing what will happen when + * the specified key is pressed based on its key code. + * <p> + * The order of precedence for key code descriptions is: + * <ol> + * <li>Manually-defined shift-locked description</li> + * <li>Manually-defined shifted description</li> + * <li>Manually-defined normal description</li> + * <li>Automatic based on the character represented by the key code</li> + * <li>Fall-back for undefined or control characters</li> + * </ol> + * </p> + * + * @param context The package's context. + * @param keyboard The keyboard on which the key resides. + * @param key The key from which to obtain a description. + * @return a character sequence describing the action performed by pressing + * the key + */ + private CharSequence getDescriptionForKeyCode(Context context, Keyboard keyboard, Key key) { + final int code = getCorrectKeyCode(keyboard, key); + + if (keyboard.isShiftLocked() && mShiftLockedKeyCodeMap.containsKey(code)) { + return context.getString(mShiftLockedKeyCodeMap.get(code)); + } else if (keyboard.isShiftedOrShiftLocked() && mShiftedKeyCodeMap.containsKey(code)) { + return context.getString(mShiftedKeyCodeMap.get(code)); + } else if (mKeyCodeMap.containsKey(code)) { + return context.getString(mKeyCodeMap.get(code)); + } else if (Character.isDefined(code) && !Character.isISOControl(code)) { + return Character.toString((char) code); + } else { + return context.getString(R.string.spoken_description_unknown, code); + } + } +} diff --git a/java/src/com/android/inputmethod/compat/AccessibilityEventCompatUtils.java b/java/src/com/android/inputmethod/compat/AccessibilityEventCompatUtils.java new file mode 100644 index 000000000..50057727a --- /dev/null +++ b/java/src/com/android/inputmethod/compat/AccessibilityEventCompatUtils.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2011 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.compat; + +import android.view.accessibility.AccessibilityEvent; + +import java.lang.reflect.Field; + +public class AccessibilityEventCompatUtils { + public static final int TYPE_VIEW_HOVER_ENTER = 0x80; + public static final int TYPE_VIEW_HOVER_EXIT = 0x100; + + private static final Field FIELD_TYPE_VIEW_HOVER_ENTER = CompatUtils.getField( + AccessibilityEvent.class, "TYPE_VIEW_HOVER_ENTER"); + private static final Field FIELD_TYPE_VIEW_HOVER_EXIT = CompatUtils.getField( + AccessibilityEvent.class, "TYPE_VIEW_HOVER_EXIT"); + private static final Integer OBJ_TYPE_VIEW_HOVER_ENTER = (Integer) CompatUtils + .getFieldValue(null, null, FIELD_TYPE_VIEW_HOVER_ENTER); + private static final Integer OBJ_TYPE_VIEW_HOVER_EXIT = (Integer) CompatUtils + .getFieldValue(null, null, FIELD_TYPE_VIEW_HOVER_EXIT); + + public static boolean supportsTouchExploration() { + return OBJ_TYPE_VIEW_HOVER_ENTER != null && OBJ_TYPE_VIEW_HOVER_EXIT != null; + } +} diff --git a/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatWrapper.java b/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatWrapper.java new file mode 100644 index 000000000..4db1c7a24 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/AccessibilityManagerCompatWrapper.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2011 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.compat; + +import android.accessibilityservice.AccessibilityServiceInfo; +import android.view.accessibility.AccessibilityManager; + +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.List; + +public class AccessibilityManagerCompatWrapper { + private static final Method METHOD_getEnabledAccessibilityServiceList = CompatUtils.getMethod( + AccessibilityManager.class, "getEnabledAccessibilityServiceList", int.class); + + private final AccessibilityManager mManager; + + public AccessibilityManagerCompatWrapper(AccessibilityManager manager) { + mManager = manager; + } + + @SuppressWarnings("unchecked") + public List<AccessibilityServiceInfo> getEnabledAccessibilityServiceList(int feedbackType) { + return (List<AccessibilityServiceInfo>) CompatUtils.invoke(mManager, + Collections.<AccessibilityServiceInfo>emptyList(), + METHOD_getEnabledAccessibilityServiceList, feedbackType); + } +} diff --git a/java/src/com/android/inputmethod/compat/ArraysCompatUtils.java b/java/src/com/android/inputmethod/compat/ArraysCompatUtils.java new file mode 100644 index 000000000..f6afbcfe2 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/ArraysCompatUtils.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2011 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.compat; + +import java.lang.reflect.Method; +import java.util.Arrays; + +public class ArraysCompatUtils { + private static final Method METHOD_Arrays_binarySearch = CompatUtils + .getMethod(Arrays.class, "binarySearch", int[].class, int.class, int.class, int.class); + + public static int binarySearch(int[] array, int startIndex, int endIndex, int value) { + if (METHOD_Arrays_binarySearch != null) { + final Object index = CompatUtils.invoke(null, 0, METHOD_Arrays_binarySearch, + array, startIndex, endIndex, value); + return (Integer)index; + } else { + return compatBinarySearch(array, startIndex, endIndex, value); + } + } + + /* package */ static int compatBinarySearch(int[] array, int startIndex, int endIndex, + int value) { + if (startIndex > endIndex) throw new IllegalArgumentException(); + if (startIndex < 0 || endIndex > array.length) throw new ArrayIndexOutOfBoundsException(); + + final int work[] = new int[endIndex - startIndex]; + System.arraycopy(array, startIndex, work, 0, work.length); + final int index = Arrays.binarySearch(work, value); + if (index >= 0) { + return index + startIndex; + } else { + return ~(~index + startIndex); + } + } +} diff --git a/java/src/com/android/inputmethod/compat/CompatUtils.java b/java/src/com/android/inputmethod/compat/CompatUtils.java index 0b532f7f0..b42633cd9 100644 --- a/java/src/com/android/inputmethod/compat/CompatUtils.java +++ b/java/src/com/android/inputmethod/compat/CompatUtils.java @@ -22,7 +22,6 @@ import android.util.Log; import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -74,76 +73,73 @@ public class CompatUtils { return targetClass.getMethod(name, parameterTypes); } catch (SecurityException e) { // ignore - return null; } catch (NoSuchMethodException e) { // ignore - return null; } + return null; } public static Field getField(Class<?> targetClass, String name) { + if (targetClass == null || TextUtils.isEmpty(name)) return null; try { return targetClass.getField(name); } catch (SecurityException e) { // ignore - return null; } catch (NoSuchFieldException e) { // ignore - return null; } + return null; } - public static Constructor<?> getConstructor(Class<?> targetClass, Class<?>[] types) { + public static Constructor<?> getConstructor(Class<?> targetClass, Class<?> ... types) { if (targetClass == null || types == null) return null; try { return targetClass.getConstructor(types); } catch (SecurityException e) { // ignore - return null; } catch (NoSuchMethodException e) { // ignore - return null; } + return null; + } + + public static Object newInstance(Constructor<?> constructor, Object ... args) { + if (constructor == null) return null; + try { + return constructor.newInstance(args); + } catch (Exception e) { + Log.e(TAG, "Exception in newInstance: " + e.getClass().getSimpleName()); + } + return null; } public static Object invoke( Object receiver, Object defaultValue, Method method, Object... args) { - if (receiver == null || method == null) return defaultValue; + if (method == null) return defaultValue; try { return method.invoke(receiver, args); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Exception in invoke: IllegalArgumentException"); - return defaultValue; - } catch (IllegalAccessException e) { - Log.e(TAG, "Exception in invoke: IllegalAccessException"); - return defaultValue; - } catch (InvocationTargetException e) { - Log.e(TAG, "Exception in invoke: IllegalTargetException"); - return defaultValue; + } catch (Exception e) { + Log.e(TAG, "Exception in invoke: " + e.getClass().getSimpleName()); } + return defaultValue; } public static Object getFieldValue(Object receiver, Object defaultValue, Field field) { - if (receiver == null || field == null) return defaultValue; + if (field == null) return defaultValue; try { return field.get(receiver); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Exception in getFieldValue: IllegalArgumentException"); - return defaultValue; - } catch (IllegalAccessException e) { - Log.e(TAG, "Exception in getFieldValue: IllegalAccessException"); - return defaultValue; + } catch (Exception e) { + Log.e(TAG, "Exception in getFieldValue: " + e.getClass().getSimpleName()); } + return defaultValue; } public static void setFieldValue(Object receiver, Field field, Object value) { - if (receiver == null || field == null) return; + if (field == null) return; try { field.set(receiver, value); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Exception in setFieldValue: IllegalArgumentException"); - } catch (IllegalAccessException e) { - Log.e(TAG, "Exception in setFieldValue: IllegalAccessException"); + } catch (Exception e) { + Log.e(TAG, "Exception in setFieldValue: " + e.getClass().getSimpleName()); } } diff --git a/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java b/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java index f6f4f7a59..bcdcef7dc 100644 --- a/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java @@ -27,7 +27,7 @@ public class EditorInfoCompatUtils { private static final Field FIELD_IME_FLAG_NAVIGATE_PREVIOUS = CompatUtils.getField( EditorInfo.class, "IME_FLAG_NAVIGATE_PREVIOUS"); private static final Field FIELD_IME_ACTION_PREVIOUS = CompatUtils.getField( - EditorInfo.class, "IME_FLAG_ACTION_PREVIOUS"); + EditorInfo.class, "IME_ACTION_PREVIOUS"); private static final Integer OBJ_IME_FLAG_NAVIGATE_NEXT = (Integer) CompatUtils .getFieldValue(null, null, FIELD_IME_FLAG_NAVIGATE_NEXT); private static final Integer OBJ_IME_FLAG_NAVIGATE_PREVIOUS = (Integer) CompatUtils @@ -78,6 +78,9 @@ public class EditorInfoCompatUtils { case EditorInfo.IME_ACTION_SEND: action = "actionSend"; break; + case EditorInfo.IME_ACTION_NEXT: + action = "actionNext"; + break; case EditorInfo.IME_ACTION_DONE: action = "actionDone"; break; diff --git a/java/src/com/android/inputmethod/compat/FrameLayoutCompatUtils.java b/java/src/com/android/inputmethod/compat/FrameLayoutCompatUtils.java new file mode 100644 index 000000000..523bf7d0e --- /dev/null +++ b/java/src/com/android/inputmethod/compat/FrameLayoutCompatUtils.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2011 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.compat; + +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewGroup.MarginLayoutParams; +import android.widget.FrameLayout; +import android.widget.RelativeLayout; + +public class FrameLayoutCompatUtils { + private static final boolean NEEDS_FRAME_LAYOUT_HACK = ( + android.os.Build.VERSION.SDK_INT < 11 /* Honeycomb */); + + public static ViewGroup getPlacer(ViewGroup container) { + if (NEEDS_FRAME_LAYOUT_HACK) { + // Insert RelativeLayout to be able to setMargin because pre-Honeycomb FrameLayout + // could not handle setMargin properly. + final ViewGroup placer = new RelativeLayout(container.getContext()); + container.addView(placer); + return placer; + } else { + return container; + } + } + + public static MarginLayoutParams newLayoutParam(ViewGroup placer, int width, int height) { + if (placer instanceof FrameLayout) { + return new FrameLayout.LayoutParams(width, height); + } else if (placer instanceof RelativeLayout) { + return new RelativeLayout.LayoutParams(width, height); + } else if (placer == null) { + throw new NullPointerException("placer is null"); + } else { + throw new IllegalArgumentException("placer is neither FrameLayout nor RelativeLayout: " + + placer.getClass().getName()); + } + } + + public static void placeViewAt(View view, int x, int y, int w, int h) { + final ViewGroup.LayoutParams lp = view.getLayoutParams(); + if (lp instanceof MarginLayoutParams) { + final MarginLayoutParams marginLayoutParams = (MarginLayoutParams)lp; + marginLayoutParams.width = w; + marginLayoutParams.height = h; + marginLayoutParams.setMargins(x, y, 0, 0); + } + } +} diff --git a/java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java b/java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java index c926be06f..7d00b6007 100644 --- a/java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java @@ -18,15 +18,12 @@ package com.android.inputmethod.compat; import com.android.inputmethod.latin.EditingUtils.SelectedWord; -import android.util.Log; import android.view.inputmethod.InputConnection; import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class InputConnectionCompatUtils { - private static final String TAG = InputConnectionCompatUtils.class.getSimpleName(); private static final Class<?> CLASS_CorrectionInfo = CompatUtils .getClass("android.view.inputmethod.CorrectionInfo"); private static final Class<?>[] INPUT_TYPE_CorrectionInfo = new Class<?>[] { int.class, @@ -53,18 +50,10 @@ public class InputConnectionCompatUtils { return; } Object[] args = { offset, oldText, newText }; - try { - Object correctionInfo = CONSTRUCTOR_CorrectionInfo.newInstance(args); + Object correctionInfo = CompatUtils.newInstance(CONSTRUCTOR_CorrectionInfo, args); + if (correctionInfo != null) { CompatUtils.invoke(ic, null, METHOD_InputConnection_commitCorrection, correctionInfo); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Error in commitCorrection: IllegalArgumentException"); - } catch (InstantiationException e) { - Log.e(TAG, "Error in commitCorrection: InstantiationException"); - } catch (IllegalAccessException e) { - Log.e(TAG, "Error in commitCorrection: IllegalAccessException"); - } catch (InvocationTargetException e) { - Log.e(TAG, "Error in commitCorrection: InvocationTargetException"); } } diff --git a/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java index c04181b55..ec7250c2a 100644 --- a/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java +++ b/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java @@ -16,12 +16,12 @@ package com.android.inputmethod.compat; -import com.android.inputmethod.deprecated.LanguageSwitcherProxy; -import com.android.inputmethod.latin.SubtypeSwitcher; - import android.inputmethodservice.InputMethodService; // import android.view.inputmethod.InputMethodSubtype; +import com.android.inputmethod.deprecated.LanguageSwitcherProxy; +import com.android.inputmethod.latin.SubtypeSwitcher; + public class InputMethodServiceCompatWrapper extends InputMethodService { // CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED needs to be false if the API level is 10 // or previous. Note that InputMethodSubtype was added in the API level 11. diff --git a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java index 806c355a9..667d86c42 100644 --- a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java +++ b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java @@ -48,6 +48,8 @@ public final class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper CompatUtils.getMethod(CLASS_InputMethodSubtype, "containsExtraValueKey", String.class); private static final Method METHOD_getExtraValueOf = CompatUtils.getMethod(CLASS_InputMethodSubtype, "getExtraValueOf", String.class); + private static final Method METHOD_isAuxiliary = + CompatUtils.getMethod(CLASS_InputMethodSubtype, "isAuxiliary"); private final int mDummyNameResId; private final int mDummyIconResId; @@ -116,6 +118,10 @@ public final class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper return (String)CompatUtils.invoke(mObj, null, METHOD_getExtraValueOf, key); } + public boolean isAuxiliary() { + return (Boolean)CompatUtils.invoke(mObj, false, METHOD_isAuxiliary); + } + public boolean isDummy() { return !hasOriginalObject(); } diff --git a/java/src/com/android/inputmethod/compat/InputTypeCompatUtils.java b/java/src/com/android/inputmethod/compat/InputTypeCompatUtils.java index d85174188..6c2f0f799 100644 --- a/java/src/com/android/inputmethod/compat/InputTypeCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/InputTypeCompatUtils.java @@ -37,6 +37,7 @@ public class InputTypeCompatUtils { (Integer) CompatUtils.getFieldValue(null, null, FIELD_InputType_TYPE_NUMBER_VARIATION_PASSWORD); private static final int WEB_TEXT_PASSWORD_INPUT_TYPE; + private static final int WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE; private static final int NUMBER_PASSWORD_INPUT_TYPE; private static final int TEXT_PASSWORD_INPUT_TYPE = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD; @@ -45,20 +46,35 @@ public class InputTypeCompatUtils { static { WEB_TEXT_PASSWORD_INPUT_TYPE = - OBJ_InputType_TYPE_TEXT_VARIATION_WEB_PASSWORD != null - ? InputType.TYPE_CLASS_TEXT | OBJ_InputType_TYPE_TEXT_VARIATION_WEB_PASSWORD - : 0; + OBJ_InputType_TYPE_TEXT_VARIATION_WEB_PASSWORD != null + ? InputType.TYPE_CLASS_TEXT | OBJ_InputType_TYPE_TEXT_VARIATION_WEB_PASSWORD + : 0; + WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE = + OBJ_InputType_TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS != null + ? InputType.TYPE_CLASS_TEXT + | OBJ_InputType_TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS + : 0; NUMBER_PASSWORD_INPUT_TYPE = OBJ_InputType_TYPE_NUMBER_VARIATION_PASSWORD != null ? InputType.TYPE_CLASS_NUMBER | OBJ_InputType_TYPE_NUMBER_VARIATION_PASSWORD : 0; } + private static boolean isWebEditTextInputType(int inputType) { + return inputType == (InputType.TYPE_CLASS_TEXT + | InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT); + } + private static boolean isWebPasswordInputType(int inputType) { return WEB_TEXT_PASSWORD_INPUT_TYPE != 0 && inputType == WEB_TEXT_PASSWORD_INPUT_TYPE; } + private static boolean isWebEmailAddressInputType(int inputType) { + return WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE != 0 + && inputType == WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE; + } + private static boolean isNumberPasswordInputType(int inputType) { return NUMBER_PASSWORD_INPUT_TYPE != 0 && inputType == NUMBER_PASSWORD_INPUT_TYPE; @@ -78,6 +94,13 @@ public class InputTypeCompatUtils { || isWebEmailAddressVariation(variation); } + public static boolean isWebInputType(int inputType) { + final int maskedInputType = + inputType & (InputType.TYPE_MASK_CLASS | InputType.TYPE_MASK_VARIATION); + return isWebEditTextInputType(maskedInputType) || isWebPasswordInputType(maskedInputType) + || isWebEmailAddressInputType(maskedInputType); + } + // Please refer to TextView.isPasswordInputType public static boolean isPasswordInputType(int inputType) { final int maskedInputType = diff --git a/java/src/com/android/inputmethod/compat/LinearLayoutCompatUtils.java b/java/src/com/android/inputmethod/compat/LinearLayoutCompatUtils.java new file mode 100644 index 000000000..674cbe74b --- /dev/null +++ b/java/src/com/android/inputmethod/compat/LinearLayoutCompatUtils.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2011 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.compat; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.util.Log; + +import java.lang.reflect.Field; + +public class LinearLayoutCompatUtils { + private static final String TAG = LinearLayoutCompatUtils.class.getSimpleName(); + + private static final Class<?> CLASS_R_STYLEABLE = CompatUtils.getClass( + "com.android.internal.R$styleable"); + private static final Field STYLEABLE_VIEW = CompatUtils.getField( + CLASS_R_STYLEABLE, "View"); + private static final Field STYLEABLE_VIEW_BACKGROUND = CompatUtils.getField( + CLASS_R_STYLEABLE, "View_background"); + private static final Object VALUE_STYLEABLE_VIEW = CompatUtils.getFieldValue( + null, null, STYLEABLE_VIEW); + private static final Integer VALUE_STYLEABLE_VIEW_BACKGROUND = + (Integer)CompatUtils.getFieldValue(null, null, STYLEABLE_VIEW_BACKGROUND); + + public static Drawable getBackgroundDrawable(Context context, AttributeSet attrs, + int defStyleAttr, int defStyleRes) { + if (!(VALUE_STYLEABLE_VIEW instanceof int[]) || VALUE_STYLEABLE_VIEW_BACKGROUND == null) { + Log.w(TAG, "Can't get View background attribute using reflection"); + return null; + } + + final int[] styleableView = (int[])VALUE_STYLEABLE_VIEW; + final TypedArray a = context.obtainStyledAttributes( + attrs, styleableView, defStyleAttr, defStyleRes); + final Drawable background = a.getDrawable(VALUE_STYLEABLE_VIEW_BACKGROUND); + a.recycle(); + return background; + } +} diff --git a/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java b/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java new file mode 100644 index 000000000..8518a4a78 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/MotionEventCompatUtils.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2011 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.compat; + +public class MotionEventCompatUtils { + public static final int ACTION_HOVER_MOVE = 0x7; + public static final int ACTION_HOVER_ENTER = 0x9; + public static final int ACTION_HOVER_EXIT = 0xA; +} diff --git a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java new file mode 100644 index 000000000..4929dd948 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2011 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.compat; + +import com.android.inputmethod.latin.SuggestedWords; +import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver; + +import android.content.Context; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.Spanned; +import android.text.TextUtils; + +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.Locale; + +public class SuggestionSpanUtils { + // TODO: Use reflection to get field values + public static final String ACTION_SUGGESTION_PICKED = + "android.text.style.SUGGESTION_PICKED"; + public static final String SUGGESTION_SPAN_PICKED_AFTER = "after"; + public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before"; + public static final String SUGGESTION_SPAN_PICKED_HASHCODE = "hashcode"; + public static final int SUGGESTION_MAX_SIZE = 5; + public static final boolean SUGGESTION_SPAN_IS_SUPPORTED; + + private static final Class<?> CLASS_SuggestionSpan = CompatUtils + .getClass("android.text.style.SuggestionSpan"); + private static final Class<?>[] INPUT_TYPE_SuggestionSpan = new Class<?>[] { + Context.class, Locale.class, String[].class, int.class, Class.class }; + private static final Constructor<?> CONSTRUCTOR_SuggestionSpan = CompatUtils + .getConstructor(CLASS_SuggestionSpan, INPUT_TYPE_SuggestionSpan); + static { + SUGGESTION_SPAN_IS_SUPPORTED = + CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null; + } + + public static CharSequence getTextWithSuggestionSpan(Context context, + CharSequence pickedWord, SuggestedWords suggestedWords) { + if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null + || suggestedWords == null || suggestedWords.size() == 0) { + return pickedWord; + } + + final Spannable spannable; + if (pickedWord instanceof Spannable) { + spannable = (Spannable) pickedWord; + } else { + spannable = new SpannableString(pickedWord); + } + final ArrayList<String> suggestionsList = new ArrayList<String>(); + for (int i = 0; i < suggestedWords.size(); ++i) { + if (suggestionsList.size() >= SUGGESTION_MAX_SIZE) { + break; + } + final CharSequence word = suggestedWords.getWord(i); + if (!TextUtils.equals(pickedWord, word)) { + suggestionsList.add(word.toString()); + } + } + + final Object[] args = + { context, null, suggestionsList.toArray(new String[suggestionsList.size()]), 0, + (Class<?>) SuggestionSpanPickedNotificationReceiver.class }; + final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); + if (ss == null) { + return pickedWord; + } + spannable.setSpan(ss, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + return spannable; + } +} diff --git a/java/src/com/android/inputmethod/deprecated/LanguageSwitcherProxy.java b/java/src/com/android/inputmethod/deprecated/LanguageSwitcherProxy.java index e14a49c49..290e6b554 100644 --- a/java/src/com/android/inputmethod/deprecated/LanguageSwitcherProxy.java +++ b/java/src/com/android/inputmethod/deprecated/LanguageSwitcherProxy.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 Google Inc. + * Copyright (C) 2011 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 diff --git a/java/src/com/android/inputmethod/deprecated/VoiceProxy.java b/java/src/com/android/inputmethod/deprecated/VoiceProxy.java index 753dceead..85993ea4d 100644 --- a/java/src/com/android/inputmethod/deprecated/VoiceProxy.java +++ b/java/src/com/android/inputmethod/deprecated/VoiceProxy.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -137,8 +137,8 @@ public class VoiceProxy implements VoiceInput.UiListener { @Override public void showHint(int viewResource) { View view = LayoutInflater.from(mService).inflate(viewResource, null); - mService.setCandidatesView(view); - mService.setCandidatesViewShown(true); +// mService.setCandidatesView(view); +// mService.setCandidatesViewShown(true); mIsShowingHint = true; } }); @@ -441,7 +441,7 @@ public class VoiceProxy implements VoiceInput.UiListener { } builder.setTypedWordValid(true).setHasMinimalSuggestion(true); mService.setSuggestions(builder.build()); - mService.setCandidatesViewShown(true); +// mService.setCandidatesViewShown(true); return true; } return false; @@ -526,7 +526,7 @@ public class VoiceProxy implements VoiceInput.UiListener { mHandler.post(new Runnable() { @Override public void run() { - mService.setCandidatesViewShown(false); +// mService.setCandidatesViewShown(false); mRecognizing = true; mVoiceInput.newView(); View v = mVoiceInput.getView(); @@ -536,7 +536,7 @@ public class VoiceProxy implements VoiceInput.UiListener { ((ViewGroup) p).removeView(v); } - View keyboardView = KeyboardSwitcher.getInstance().getInputView(); + View keyboardView = KeyboardSwitcher.getInstance().getKeyboardView(); // The full height of the keyboard is difficult to calculate // as the dimension is expressed in "mm" and not in "pixel" @@ -691,7 +691,7 @@ public class VoiceProxy implements VoiceInput.UiListener { if (mSubtypeSwitcher.isVoiceMode() && windowToken != null) { // Close keyboard view if it is been shown. if (KeyboardSwitcher.getInstance().isInputViewShown()) - KeyboardSwitcher.getInstance().getInputView().purgeKeyboardAndClosing(); + KeyboardSwitcher.getInstance().getKeyboardView().purgeKeyboardAndClosing(); startListening(false, windowToken); } // If we have no token, onAttachedToWindow will take care of showing dialog and start diff --git a/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java b/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java index b8655d112..cf6cd0f5e 100644 --- a/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java +++ b/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2009 Google Inc. + * Copyright (C) 2008-2009 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 @@ -16,7 +16,7 @@ package com.android.inputmethod.deprecated.languageswitcher; -import com.android.inputmethod.keyboard.KeyboardParser; +import com.android.inputmethod.keyboard.internal.KeyboardParser; import com.android.inputmethod.latin.DictionaryFactory; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.Settings; diff --git a/java/src/com/android/inputmethod/deprecated/languageswitcher/LanguageSwitcher.java b/java/src/com/android/inputmethod/deprecated/languageswitcher/LanguageSwitcher.java index 5ef236e31..1eedb5ee1 100644 --- a/java/src/com/android/inputmethod/deprecated/languageswitcher/LanguageSwitcher.java +++ b/java/src/com/android/inputmethod/deprecated/languageswitcher/LanguageSwitcher.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -20,6 +20,7 @@ import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.Settings; import com.android.inputmethod.latin.SharedPreferencesCompat; +import com.android.inputmethod.latin.Utils; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; @@ -125,8 +126,7 @@ public class LanguageSwitcher { private void constructLocales() { mLocales.clear(); for (final String lang : mSelectedLanguageArray) { - final Locale locale = new Locale(lang.substring(0, 2), - lang.length() > 4 ? lang.substring(3, 5) : ""); + final Locale locale = Utils.constructLocaleFromString(lang); mLocales.add(locale); } } diff --git a/java/src/com/android/inputmethod/deprecated/recorrection/Recorrection.java b/java/src/com/android/inputmethod/deprecated/recorrection/Recorrection.java index bf69d5ced..d40728d25 100644 --- a/java/src/com/android/inputmethod/deprecated/recorrection/Recorrection.java +++ b/java/src/com/android/inputmethod/deprecated/recorrection/Recorrection.java @@ -17,6 +17,7 @@ package com.android.inputmethod.deprecated.recorrection; import com.android.inputmethod.compat.InputConnectionCompatUtils; +import com.android.inputmethod.compat.SuggestionSpanUtils; import com.android.inputmethod.deprecated.VoiceProxy; import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.latin.AutoCorrection; @@ -43,7 +44,6 @@ import java.util.ArrayList; * Manager of re-correction functionalities */ public class Recorrection implements SharedPreferences.OnSharedPreferenceChangeListener { - public static final boolean USE_LEGACY_RECORRECTION = true; private static final Recorrection sInstance = new Recorrection(); private LatinIME mService; @@ -70,7 +70,7 @@ public class Recorrection implements SharedPreferences.OnSharedPreferenceChangeL } private void initInternal(LatinIME context, SharedPreferences prefs) { - if (!USE_LEGACY_RECORRECTION) { + if (SuggestionSpanUtils.SUGGESTION_SPAN_IS_SUPPORTED) { mRecorrectionEnabled = false; return; } @@ -80,7 +80,7 @@ public class Recorrection implements SharedPreferences.OnSharedPreferenceChangeL } public void checkRecorrectionOnStart() { - if (!USE_LEGACY_RECORRECTION || !mRecorrectionEnabled) return; + if (SuggestionSpanUtils.SUGGESTION_SPAN_IS_SUPPORTED || !mRecorrectionEnabled) return; final InputConnection ic = mService.getCurrentInputConnection(); if (ic == null) return; @@ -110,7 +110,7 @@ public class Recorrection implements SharedPreferences.OnSharedPreferenceChangeL CandidateView candidateView, int candidatesStart, int candidatesEnd, int newSelStart, int newSelEnd, int oldSelStart, int lastSelectionStart, int lastSelectionEnd, boolean hasUncommittedTypedChars) { - if (!USE_LEGACY_RECORRECTION || !mRecorrectionEnabled) return; + if (SuggestionSpanUtils.SUGGESTION_SPAN_IS_SUPPORTED || !mRecorrectionEnabled) return; if (!mService.isShowingSuggestionsStrip()) return; if (!keyboardSwitcher.isInputViewShown()) return; if (!mService.isSuggestionsRequested()) return; @@ -142,7 +142,7 @@ public class Recorrection implements SharedPreferences.OnSharedPreferenceChangeL } public void saveRecorrectionSuggestion(WordComposer word, CharSequence result) { - if (!USE_LEGACY_RECORRECTION || !mRecorrectionEnabled) return; + if (SuggestionSpanUtils.SUGGESTION_SPAN_IS_SUPPORTED || !mRecorrectionEnabled) return; if (word.size() <= 1) { return; } @@ -170,7 +170,7 @@ public class Recorrection implements SharedPreferences.OnSharedPreferenceChangeL */ public boolean applyTypedAlternatives(WordComposer word, Suggest suggest, KeyboardSwitcher keyboardSwitcher, EditingUtils.SelectedWord touching) { - if (!USE_LEGACY_RECORRECTION || !mRecorrectionEnabled) return false; + if (SuggestionSpanUtils.SUGGESTION_SPAN_IS_SUPPORTED || !mRecorrectionEnabled) return false; // If we didn't find a match, search for result in typed word history WordComposer foundWord = null; RecorrectionSuggestionEntries alternatives = null; @@ -218,12 +218,12 @@ public class Recorrection implements SharedPreferences.OnSharedPreferenceChangeL mService.showSuggestions(builder.build(), entries.getOriginalWord()); } - public void setRecorrectionSuggestions(VoiceProxy voiceProxy, CandidateView candidateView, - Suggest suggest, KeyboardSwitcher keyboardSwitcher, WordComposer word, - boolean hasUncommittedTypedChars, int lastSelectionStart, int lastSelectionEnd, - String wordSeparators) { + public void fetchAndDisplayRecorrectionSuggestions(VoiceProxy voiceProxy, + CandidateView candidateView, Suggest suggest, KeyboardSwitcher keyboardSwitcher, + WordComposer word, boolean hasUncommittedTypedChars, int lastSelectionStart, + int lastSelectionEnd, String wordSeparators) { if (!InputConnectionCompatUtils.RECORRECTION_SUPPORTED) return; - if (!USE_LEGACY_RECORRECTION || !mRecorrectionEnabled) return; + if (SuggestionSpanUtils.SUGGESTION_SPAN_IS_SUPPORTED || !mRecorrectionEnabled) return; voiceProxy.setShowingVoiceSuggestions(false); if (candidateView != null && candidateView.isShowingAddToDictionaryHint()) { return; @@ -249,7 +249,7 @@ public class Recorrection implements SharedPreferences.OnSharedPreferenceChangeL ic.endBatchEdit(); } else { abortRecorrection(true); - mService.setPunctuationSuggestions(); // Show the punctuation suggestions list + mService.updateBigramPredictions(); } } else { abortRecorrection(true); @@ -257,7 +257,7 @@ public class Recorrection implements SharedPreferences.OnSharedPreferenceChangeL } public void abortRecorrection(boolean force) { - if (!USE_LEGACY_RECORRECTION) return; + if (SuggestionSpanUtils.SUGGESTION_SPAN_IS_SUPPORTED) return; if (force || TextEntryState.isRecorrecting()) { TextEntryState.onAbortRecorrection(); mService.setCandidatesViewShown(mService.isCandidateStripVisible()); @@ -279,7 +279,7 @@ public class Recorrection implements SharedPreferences.OnSharedPreferenceChangeL @Override public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { - if (!USE_LEGACY_RECORRECTION) return; + if (SuggestionSpanUtils.SUGGESTION_SPAN_IS_SUPPORTED) return; if (key.equals(Settings.PREF_RECORRECTION_ENABLED)) { updateRecorrectionEnabled(mService.getResources(), prefs); } diff --git a/java/src/com/android/inputmethod/deprecated/recorrection/RecorrectionSuggestionEntries.java b/java/src/com/android/inputmethod/deprecated/recorrection/RecorrectionSuggestionEntries.java index 914e2cbc1..5e6c87044 100644 --- a/java/src/com/android/inputmethod/deprecated/recorrection/RecorrectionSuggestionEntries.java +++ b/java/src/com/android/inputmethod/deprecated/recorrection/RecorrectionSuggestionEntries.java @@ -57,6 +57,6 @@ public class RecorrectionSuggestionEntries { private static SuggestedWords.Builder getTypedSuggestions( Suggest suggest, KeyboardSwitcher keyboardSwitcher, WordComposer word) { - return suggest.getSuggestedWordBuilder(keyboardSwitcher.getInputView(), word, null); + return suggest.getSuggestedWordBuilder(keyboardSwitcher.getKeyboardView(), word, null); } } diff --git a/java/src/com/android/inputmethod/deprecated/voice/FieldContext.java b/java/src/com/android/inputmethod/deprecated/voice/FieldContext.java index 0ef73d2d7..3c79cc218 100644 --- a/java/src/com/android/inputmethod/deprecated/voice/FieldContext.java +++ b/java/src/com/android/inputmethod/deprecated/voice/FieldContext.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Google Inc. + * Copyright (C) 2009 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 diff --git a/java/src/com/android/inputmethod/deprecated/voice/Hints.java b/java/src/com/android/inputmethod/deprecated/voice/Hints.java index 52a4f4e58..06b234381 100644 --- a/java/src/com/android/inputmethod/deprecated/voice/Hints.java +++ b/java/src/com/android/inputmethod/deprecated/voice/Hints.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Google Inc. + * Copyright (C) 2009 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 diff --git a/java/src/com/android/inputmethod/deprecated/voice/RecognitionView.java b/java/src/com/android/inputmethod/deprecated/voice/RecognitionView.java index b57c16f40..dcb826e8f 100644 --- a/java/src/com/android/inputmethod/deprecated/voice/RecognitionView.java +++ b/java/src/com/android/inputmethod/deprecated/voice/RecognitionView.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Google Inc. + * Copyright (C) 2009 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 diff --git a/java/src/com/android/inputmethod/deprecated/voice/SettingsUtil.java b/java/src/com/android/inputmethod/deprecated/voice/SettingsUtil.java index 7721fe268..855a09a1d 100644 --- a/java/src/com/android/inputmethod/deprecated/voice/SettingsUtil.java +++ b/java/src/com/android/inputmethod/deprecated/voice/SettingsUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Google Inc. + * Copyright (C) 2009 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 diff --git a/java/src/com/android/inputmethod/deprecated/voice/SoundIndicator.java b/java/src/com/android/inputmethod/deprecated/voice/SoundIndicator.java index 8cc79de1e..25b314085 100644 --- a/java/src/com/android/inputmethod/deprecated/voice/SoundIndicator.java +++ b/java/src/com/android/inputmethod/deprecated/voice/SoundIndicator.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 Google Inc. + * Copyright (C) 2011 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 diff --git a/java/src/com/android/inputmethod/deprecated/voice/VoiceInput.java b/java/src/com/android/inputmethod/deprecated/voice/VoiceInput.java index 7ee0de9c9..b718ebbb7 100644 --- a/java/src/com/android/inputmethod/deprecated/voice/VoiceInput.java +++ b/java/src/com/android/inputmethod/deprecated/voice/VoiceInput.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Google Inc. + * Copyright (C) 2009 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 @@ -129,7 +129,7 @@ public class VoiceInput implements OnClickListener { private int mAfterVoiceInputSelectionSpan = 0; private int mState = DEFAULT; - + private final static int MSG_RESET = 1; private final Handler mHandler = new Handler() { @@ -192,7 +192,7 @@ public class VoiceInput implements OnClickListener { } mBlacklist = new Whitelist(); - mBlacklist.addApp("com.android.setupwizard"); + mBlacklist.addApp("com.google.android.setupwizard"); } public void setCursorPos(int pos) { diff --git a/java/src/com/android/inputmethod/deprecated/voice/VoiceInputLogger.java b/java/src/com/android/inputmethod/deprecated/voice/VoiceInputLogger.java index 87b943426..22e8207bf 100644 --- a/java/src/com/android/inputmethod/deprecated/voice/VoiceInputLogger.java +++ b/java/src/com/android/inputmethod/deprecated/voice/VoiceInputLogger.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Google Inc. + * 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 diff --git a/java/src/com/android/inputmethod/deprecated/voice/WaveformImage.java b/java/src/com/android/inputmethod/deprecated/voice/WaveformImage.java index a3025f252..8ed279f42 100644 --- a/java/src/com/android/inputmethod/deprecated/voice/WaveformImage.java +++ b/java/src/com/android/inputmethod/deprecated/voice/WaveformImage.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2009 Google Inc. + * Copyright (C) 2008-2009 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 diff --git a/java/src/com/android/inputmethod/deprecated/voice/Whitelist.java b/java/src/com/android/inputmethod/deprecated/voice/Whitelist.java index 310689cb2..6c5f52ae2 100644 --- a/java/src/com/android/inputmethod/deprecated/voice/Whitelist.java +++ b/java/src/com/android/inputmethod/deprecated/voice/Whitelist.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Google Inc. + * Copyright (C) 2009 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 diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java index 33b55b5d3..2850c95df 100644 --- a/java/src/com/android/inputmethod/keyboard/Key.java +++ b/java/src/com/android/inputmethod/keyboard/Key.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -16,10 +16,6 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.keyboard.KeyStyles.KeyStyle; -import com.android.inputmethod.keyboard.KeyboardParser.ParseException; -import com.android.inputmethod.latin.R; - import android.content.res.Resources; import android.content.res.TypedArray; import android.content.res.XmlResourceParser; @@ -27,6 +23,15 @@ import android.graphics.drawable.Drawable; import android.text.TextUtils; import android.util.Xml; +import com.android.inputmethod.keyboard.internal.KeyStyles; +import com.android.inputmethod.keyboard.internal.KeyboardIconsSet; +import com.android.inputmethod.keyboard.internal.KeyboardParser; +import com.android.inputmethod.keyboard.internal.PopupCharactersParser; +import com.android.inputmethod.keyboard.internal.Row; +import com.android.inputmethod.keyboard.internal.KeyStyles.KeyStyle; +import com.android.inputmethod.keyboard.internal.KeyboardParser.ParseException; +import com.android.inputmethod.latin.R; + import java.util.ArrayList; /** @@ -37,25 +42,26 @@ public class Key { * The key code (unicode or custom code) that this key generates. */ public final int mCode; - /** The unicode that this key generates in manual temporary upper case mode. */ - public final int mManualTemporaryUpperCaseCode; /** Label to display */ public final CharSequence mLabel; + /** Hint letter to display on the key in conjunction with the label */ + public final CharSequence mHintLetter; /** Option of the label */ public final int mLabelOption; + public static final int LABEL_OPTION_ALIGN_LEFT = 0x01; + public static final int LABEL_OPTION_ALIGN_RIGHT = 0x02; + public static final int LABEL_OPTION_ALIGN_BOTTOM = 0x08; + public static final int LABEL_OPTION_FONT_NORMAL = 0x10; + public static final int LABEL_OPTION_FONT_FIXED_WIDTH = 0x20; + public static final int LABEL_OPTION_FOLLOW_KEY_LETTER_RATIO = 0x40; + private static final int LABEL_OPTION_POPUP_HINT = 0x80; + private static final int LABEL_OPTION_HAS_UPPERCASE_LETTER = 0x100; /** Icon to display instead of a label. Icon takes precedence over a label */ private Drawable mIcon; /** Preview version of the icon, for the preview popup */ private Drawable mPreviewIcon; - /** Hint icon to display on the key in conjunction with the label */ - public final Drawable mHintIcon; - /** - * The hint icon to display on the key when keyboard is in manual temporary upper case - * mode. - */ - public final Drawable mManualTemporaryUpperCaseHintIcon; /** Width of the key, not including the gap */ public final int mWidth; @@ -95,11 +101,15 @@ public class Key { private final Keyboard mKeyboard; /** The current pressed state of this key */ - public boolean mPressed; + private boolean mPressed; /** If this is a sticky key, is its highlight on? */ - public boolean mHighlightOn; + private boolean mHighlightOn; /** Key is enabled and responds on press */ - public boolean mEnabled = true; + private boolean mEnabled = true; + + // keyWidth constants + private static final int KEYWIDTH_FILL_RIGHT = 0; + private static final int KEYWIDTH_FILL_BOTH = -1; private final static int[] KEY_STATE_NORMAL_ON = { android.R.attr.state_checkable, @@ -140,7 +150,7 @@ public class Key { }; /** - * This constructor is being used only for key in mini popup keyboard. + * This constructor is being used only for key in popup mini keyboard. */ public Key(Resources res, Keyboard keyboard, CharSequence popupCharacter, int x, int y, int width, int height, int edgeFlags) { @@ -150,9 +160,7 @@ public class Key { mVisualInsetsLeft = mVisualInsetsRight = 0; mWidth = width - mGap; mEdgeFlags = edgeFlags; - mHintIcon = null; - mManualTemporaryUpperCaseHintIcon = null; - mManualTemporaryUpperCaseCode = Keyboard.CODE_DUMMY; + mHintLetter = null; mLabelOption = 0; mFunctional = false; mSticky = false; @@ -163,7 +171,7 @@ public class Key { mLabel = PopupCharactersParser.getLabel(popupSpecification); mOutputText = PopupCharactersParser.getOutputText(popupSpecification); mCode = PopupCharactersParser.getCode(res, popupSpecification); - mIcon = PopupCharactersParser.getIcon(res, popupSpecification); + mIcon = keyboard.mIconsSet.getIcon(PopupCharactersParser.getIconId(popupSpecification)); // Horizontal gap is divided equally to both sides of the key. mX = x + mGap / 2; mY = y; @@ -178,6 +186,7 @@ public class Key { * @param x the x coordinate of the top-left * @param y the y coordinate of the top-left * @param parser the XML parser containing the attributes for this key + * @param keyStyles active key styles set */ public Key(Resources res, Row row, int x, int y, XmlResourceParser parser, KeyStyles keyStyles) { @@ -185,6 +194,7 @@ public class Key { final TypedArray keyboardAttr = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.Keyboard); + int keyWidth; try { mHeight = KeyboardParser.getDimensionOrFraction(keyboardAttr, R.styleable.Keyboard_rowHeight, @@ -192,17 +202,13 @@ public class Key { mGap = KeyboardParser.getDimensionOrFraction(keyboardAttr, R.styleable.Keyboard_horizontalGap, mKeyboard.getDisplayWidth(), row.mDefaultHorizontalGap); - mWidth = KeyboardParser.getDimensionOrFraction(keyboardAttr, + keyWidth = KeyboardParser.getDimensionOrFraction(keyboardAttr, R.styleable.Keyboard_keyWidth, - mKeyboard.getDisplayWidth(), row.mDefaultWidth) - mGap; + mKeyboard.getDisplayWidth(), row.mDefaultWidth); } finally { keyboardAttr.recycle(); } - // Horizontal gap is divided equally to both sides of the key. - mX = x + mGap / 2; - mY = y; - final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.Keyboard_Key); try { @@ -216,6 +222,35 @@ public class Key { style = keyStyles.getEmptyKeyStyle(); } + final int keyboardWidth = mKeyboard.getDisplayWidth(); + int keyXPos = KeyboardParser.getDimensionOrFraction(keyAttr, + R.styleable.Keyboard_Key_keyXPos, keyboardWidth, x); + if (keyXPos < 0) { + // If keyXPos is negative, the actual x-coordinate will be k + keyXPos. + keyXPos += keyboardWidth; + if (keyXPos < x) { + // keyXPos shouldn't be less than x because drawable area for this key starts + // at x. Or, this key will overlaps the adjacent key on its left hand side. + keyXPos = x; + } + } + if (keyWidth == KEYWIDTH_FILL_RIGHT) { + // If keyWidth is zero, the actual key width will be determined to fill out the + // area up to the right edge of the keyboard. + keyWidth = keyboardWidth - keyXPos; + } else if (keyWidth <= KEYWIDTH_FILL_BOTH) { + // If keyWidth is negative, the actual key width will be determined to fill out the + // area between the nearest key on the left hand side and the right edge of the + // keyboard. + keyXPos = x; + keyWidth = keyboardWidth - keyXPos; + } + + // Horizontal gap is divided equally to both sides of the key. + mX = keyXPos + mGap / 2; + mY = y; + mWidth = keyWidth - mGap; + final CharSequence[] popupCharacters = style.getTextArray(keyAttr, R.styleable.Keyboard_Key_popupCharacters); if (res.getBoolean(R.bool.config_digit_popup_characters_enabled)) { @@ -234,24 +269,23 @@ public class Key { mEdgeFlags = style.getFlag(keyAttr, R.styleable.Keyboard_Key_keyEdgeFlags, 0) | row.mRowEdgeFlags; + final KeyboardIconsSet iconsSet = mKeyboard.mIconsSet; mVisualInsetsLeft = KeyboardParser.getDimensionOrFraction(keyAttr, R.styleable.Keyboard_Key_visualInsetsLeft, mKeyboard.getDisplayHeight(), 0); mVisualInsetsRight = KeyboardParser.getDimensionOrFraction(keyAttr, R.styleable.Keyboard_Key_visualInsetsRight, mKeyboard.getDisplayHeight(), 0); - mPreviewIcon = style.getDrawable(keyAttr, R.styleable.Keyboard_Key_iconPreview); + mPreviewIcon = iconsSet.getIcon(style.getInt( + keyAttr, R.styleable.Keyboard_Key_keyIconPreview, + KeyboardIconsSet.ICON_UNDEFINED)); Keyboard.setDefaultBounds(mPreviewIcon); - mIcon = style.getDrawable(keyAttr, R.styleable.Keyboard_Key_keyIcon); + mIcon = iconsSet.getIcon(style.getInt( + keyAttr, R.styleable.Keyboard_Key_keyIcon, + KeyboardIconsSet.ICON_UNDEFINED)); Keyboard.setDefaultBounds(mIcon); - mHintIcon = style.getDrawable(keyAttr, R.styleable.Keyboard_Key_keyHintIcon); - Keyboard.setDefaultBounds(mHintIcon); - mManualTemporaryUpperCaseHintIcon = style.getDrawable(keyAttr, - R.styleable.Keyboard_Key_manualTemporaryUpperCaseHintIcon); - Keyboard.setDefaultBounds(mManualTemporaryUpperCaseHintIcon); + mHintLetter = style.getText(keyAttr, R.styleable.Keyboard_Key_keyHintLetter); mLabel = style.getText(keyAttr, R.styleable.Keyboard_Key_keyLabel); mLabelOption = style.getFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelOption, 0); - mManualTemporaryUpperCaseCode = style.getInt(keyAttr, - R.styleable.Keyboard_Key_manualTemporaryUpperCaseCode, Keyboard.CODE_DUMMY); mOutputText = style.getText(keyAttr, R.styleable.Keyboard_Key_keyOutputText); // Choose the first letter of the label as primary code if not // specified. @@ -265,8 +299,9 @@ public class Key { mCode = Keyboard.CODE_DUMMY; } - final Drawable shiftedIcon = style.getDrawable(keyAttr, - R.styleable.Keyboard_Key_shiftedIcon); + final Drawable shiftedIcon = iconsSet.getIcon(style.getInt( + keyAttr, R.styleable.Keyboard_Key_keyIconShifted, + KeyboardIconsSet.ICON_UNDEFINED)); if (shiftedIcon != null) mKeyboard.getShiftedIcons().put(this, shiftedIcon); } finally { @@ -274,8 +309,16 @@ public class Key { } } + public boolean hasPopupHint() { + return (mLabelOption & LABEL_OPTION_POPUP_HINT) != 0; + } + + public boolean hasUppercaseLetter() { + return (mLabelOption & LABEL_OPTION_HAS_UPPERCASE_LETTER) != 0; + } + private static boolean isDigitPopupCharacter(CharSequence label) { - return label.length() == 1 && Character.isDigit(label.charAt(0)); + return label != null && label.length() == 1 && Character.isDigit(label.charAt(0)); } private static CharSequence[] filterOutDigitPopupCharacters(CharSequence[] popupCharacters) { @@ -338,6 +381,18 @@ public class Key { mPressed = false; } + public void setHighlightOn(boolean highlightOn) { + mHighlightOn = highlightOn; + } + + public boolean isEnabled() { + return mEnabled; + } + + public void setEnabled(boolean enabled) { + mEnabled = enabled; + } + /** * Detects if a point falls on this key. * @param x the x-coordinate of the point diff --git a/java/src/com/android/inputmethod/keyboard/KeyDetector.java b/java/src/com/android/inputmethod/keyboard/KeyDetector.java index 0b13afecb..7add43a6d 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyDetector.java +++ b/java/src/com/android/inputmethod/keyboard/KeyDetector.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java index 492883caf..20327c5b2 100644 --- a/java/src/com/android/inputmethod/keyboard/Keyboard.java +++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -16,15 +16,18 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.latin.R; - -import org.xmlpull.v1.XmlPullParserException; - import android.content.Context; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.util.Log; +import com.android.inputmethod.keyboard.internal.KeyboardIconsSet; +import com.android.inputmethod.keyboard.internal.KeyboardParser; +import com.android.inputmethod.keyboard.internal.KeyboardShiftState; +import com.android.inputmethod.latin.R; + +import org.xmlpull.v1.XmlPullParserException; + import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -51,7 +54,7 @@ import java.util.Map; * </pre> */ public class Keyboard { - private static final String TAG = "Keyboard"; + private static final String TAG = Keyboard.class.getSimpleName(); public static final int EDGE_LEFT = 0x01; public static final int EDGE_RIGHT = 0x02; @@ -130,6 +133,8 @@ public class Keyboard { public final KeyboardId mId; + public final KeyboardIconsSet mIconsSet = new KeyboardIconsSet(); + // Variables for pre-computing nearest keys. // TODO: Change GRID_WIDTH and GRID_HEIGHT to private. @@ -151,16 +156,11 @@ public class Keyboard { * @param context the application or service context * @param xmlLayoutResId the resource file that contains the keyboard layout and keys. * @param id keyboard identifier + * @param width keyboard width */ - public Keyboard(Context context, int xmlLayoutResId, KeyboardId id) { - this(context, xmlLayoutResId, id, - context.getResources().getDisplayMetrics().widthPixels, - context.getResources().getDisplayMetrics().heightPixels); - } - private Keyboard(Context context, int xmlLayoutResId, KeyboardId id, int width, - int height) { - Resources res = context.getResources(); + public Keyboard(Context context, int xmlLayoutResId, KeyboardId id, int width) { + final Resources res = context.getResources(); GRID_WIDTH = res.getInteger(R.integer.config_keyboard_grid_width); GRID_HEIGHT = res.getInteger(R.integer.config_keyboard_grid_height); GRID_SIZE = GRID_WIDTH * GRID_HEIGHT; @@ -168,15 +168,16 @@ public class Keyboard { final int horizontalEdgesPadding = (int)res.getDimension( R.dimen.keyboard_horizontal_edges_padding); mDisplayWidth = width - horizontalEdgesPadding * 2; - mDisplayHeight = height; + // TODO: Adjust the height by referring to the height of area available for drawing as well. + mDisplayHeight = res.getDisplayMetrics().heightPixels; mDefaultHorizontalGap = 0; setKeyWidth(mDisplayWidth / 10); mDefaultVerticalGap = 0; mDefaultHeight = mDefaultWidth; mId = id; - loadKeyboard(context, xmlLayoutResId); mProximityInfo = new ProximityInfo(GRID_WIDTH, GRID_HEIGHT); + loadKeyboard(context, xmlLayoutResId); } public int getProximityInfo() { @@ -295,7 +296,7 @@ public class Keyboard { public boolean setShiftLocked(boolean newShiftLockState) { final Map<Key, Drawable> shiftedIcons = getShiftedIcons(); for (final Key key : getShiftKeys()) { - key.mHighlightOn = newShiftLockState; + key.setHighlightOn(newShiftLockState); key.setIcon(newShiftLockState ? shiftedIcons.get(key) : mNormalShiftIcons.get(key)); } mShiftState.setShiftLocked(newShiftLockState); @@ -438,7 +439,7 @@ public class Keyboard { private void loadKeyboard(Context context, int xmlLayoutResId) { try { - KeyboardParser parser = new KeyboardParser(this, context.getResources()); + KeyboardParser parser = new KeyboardParser(this, context); parser.parseKeyboard(xmlLayoutResId); // mMinWidth is the width of this keyboard which is maximum width of row. mMinWidth = parser.getMaxRowWidth(); @@ -452,7 +453,7 @@ public class Keyboard { } } - protected static void setDefaultBounds(Drawable drawable) { + public static void setDefaultBounds(Drawable drawable) { if (drawable != null) drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java b/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java index 098af214e..7e67d6f6b 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardId.java b/java/src/com/android/inputmethod/keyboard/KeyboardId.java index f68b68f1d..9c63c198c 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardId.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardId.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -16,12 +16,12 @@ package com.android.inputmethod.keyboard; +import android.view.inputmethod.EditorInfo; + import com.android.inputmethod.compat.EditorInfoCompatUtils; import com.android.inputmethod.compat.InputTypeCompatUtils; import com.android.inputmethod.latin.R; -import android.view.inputmethod.EditorInfo; - import java.util.Arrays; import java.util.Locale; @@ -34,38 +34,55 @@ public class KeyboardId { public static final int MODE_URL = 1; public static final int MODE_EMAIL = 2; public static final int MODE_IM = 3; - public static final int MODE_WEB = 4; - public static final int MODE_PHONE = 5; - public static final int MODE_NUMBER = 6; + public static final int MODE_PHONE = 4; + public static final int MODE_NUMBER = 5; + + public static final int F2KEY_MODE_NONE = 0; + public static final int F2KEY_MODE_SETTINGS = 1; + public static final int F2KEY_MODE_SHORTCUT_IME = 2; + public static final int F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS = 3; public final Locale mLocale; public final int mOrientation; + public final int mWidth; public final int mMode; public final int mXmlId; - public final int mColorScheme; + public final boolean mNavigateAction; public final boolean mPasswordInput; + // TODO: Clean up these booleans and modes. public final boolean mHasSettingsKey; + public final int mF2KeyMode; + public final boolean mClobberSettingsKey; public final boolean mVoiceKeyEnabled; public final boolean mHasVoiceKey; public final int mImeAction; public final boolean mEnableShiftLock; + public final String mXmlName; + public final EditorInfo mAttribute; private final int mHashCode; - public KeyboardId(String xmlName, int xmlId, int colorScheme, Locale locale, int orientation, - int mode, EditorInfo attribute, boolean hasSettingsKey, boolean voiceKeyEnabled, - boolean hasVoiceKey, boolean enableShiftLock) { + public KeyboardId(String xmlName, int xmlId, Locale locale, int orientation, int width, + int mode, EditorInfo attribute, boolean hasSettingsKey, int f2KeyMode, + boolean clobberSettingsKey, boolean voiceKeyEnabled, boolean hasVoiceKey, + boolean enableShiftLock) { final int inputType = (attribute != null) ? attribute.inputType : 0; final int imeOptions = (attribute != null) ? attribute.imeOptions : 0; this.mLocale = locale; this.mOrientation = orientation; + this.mWidth = width; this.mMode = mode; this.mXmlId = xmlId; - this.mColorScheme = colorScheme; + // Note: Turn off checking navigation flag to show TAB key for now. + this.mNavigateAction = InputTypeCompatUtils.isWebInputType(inputType); +// || EditorInfoCompatUtils.hasFlagNavigateNext(imeOptions) +// || EditorInfoCompatUtils.hasFlagNavigatePrevious(imeOptions); this.mPasswordInput = InputTypeCompatUtils.isPasswordInputType(inputType) || InputTypeCompatUtils.isVisiblePasswordInputType(inputType); this.mHasSettingsKey = hasSettingsKey; + this.mF2KeyMode = f2KeyMode; + this.mClobberSettingsKey = clobberSettingsKey; this.mVoiceKeyEnabled = voiceKeyEnabled; this.mHasVoiceKey = hasVoiceKey; // We are interested only in {@link EditorInfo#IME_MASK_ACTION} enum value and @@ -73,16 +90,21 @@ public class KeyboardId { this.mImeAction = imeOptions & ( EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION); this.mEnableShiftLock = enableShiftLock; + this.mXmlName = xmlName; + this.mAttribute = attribute; this.mHashCode = Arrays.hashCode(new Object[] { locale, orientation, + width, mode, xmlId, - colorScheme, + mNavigateAction, mPasswordInput, hasSettingsKey, + f2KeyMode, + clobberSettingsKey, voiceKeyEnabled, hasVoiceKey, mImeAction, @@ -90,6 +112,20 @@ public class KeyboardId { }); } + public KeyboardId cloneWithNewLayout(String xmlName, int xmlId) { + return new KeyboardId(xmlName, xmlId, mLocale, mOrientation, mWidth, mMode, mAttribute, + mHasSettingsKey, mF2KeyMode, mClobberSettingsKey, mVoiceKeyEnabled, mHasVoiceKey, + mEnableShiftLock); + } + + public KeyboardId cloneWithNewGeometry(int width) { + if (mWidth == width) + return this; + return new KeyboardId(mXmlName, mXmlId, mLocale, mOrientation, width, mMode, mAttribute, + mHasSettingsKey, mF2KeyMode, mClobberSettingsKey, mVoiceKeyEnabled, mHasVoiceKey, + mEnableShiftLock); + } + public int getXmlId() { return mXmlId; } @@ -99,13 +135,17 @@ public class KeyboardId { } public boolean isSymbolsKeyboard() { - return mXmlId == R.xml.kbd_symbols; + return mXmlId == R.xml.kbd_symbols || mXmlId == R.xml.kbd_symbols_shift; } public boolean isPhoneKeyboard() { return mMode == MODE_PHONE; } + public boolean isPhoneSymbolsKeyboard() { + return mXmlId == R.xml.kbd_phone_symbols; + } + public boolean isNumberKeyboard() { return mMode == MODE_NUMBER; } @@ -118,11 +158,14 @@ public class KeyboardId { boolean equals(KeyboardId other) { return other.mLocale.equals(this.mLocale) && other.mOrientation == this.mOrientation + && other.mWidth == this.mWidth && other.mMode == this.mMode && other.mXmlId == this.mXmlId - && other.mColorScheme == this.mColorScheme + && other.mNavigateAction == this.mNavigateAction && other.mPasswordInput == this.mPasswordInput && other.mHasSettingsKey == this.mHasSettingsKey + && other.mF2KeyMode == this.mF2KeyMode + && other.mClobberSettingsKey == this.mClobberSettingsKey && other.mVoiceKeyEnabled == this.mVoiceKeyEnabled && other.mHasVoiceKey == this.mHasVoiceKey && other.mImeAction == this.mImeAction @@ -136,18 +179,20 @@ public class KeyboardId { @Override public String toString() { - return String.format("[%s.xml %s %s %s imeAction=%s %s%s%s%s%s%s]", + return String.format("[%s.xml %s %s%d %s %s %s%s%s%s%s%s%s%s]", mXmlName, mLocale, - (mOrientation == 1 ? "port" : "land"), + (mOrientation == 1 ? "port" : "land"), mWidth, modeName(mMode), EditorInfoCompatUtils.imeOptionsName(mImeAction), + f2KeyModeName(mF2KeyMode), + (mClobberSettingsKey ? " clobberSettingsKey" : ""), + (mNavigateAction ? " navigateAction" : ""), (mPasswordInput ? " passwordInput" : ""), (mHasSettingsKey ? " hasSettingsKey" : ""), (mVoiceKeyEnabled ? " voiceKeyEnabled" : ""), (mHasVoiceKey ? " hasVoiceKey" : ""), - (mEnableShiftLock ? " enableShiftLock" : ""), - colorSchemeName(mColorScheme) + (mEnableShiftLock ? " enableShiftLock" : "") ); } @@ -157,18 +202,19 @@ public class KeyboardId { case MODE_URL: return "url"; case MODE_EMAIL: return "email"; case MODE_IM: return "im"; - case MODE_WEB: return "web"; case MODE_PHONE: return "phone"; case MODE_NUMBER: return "number"; + default: return null; } - return null; } - public static String colorSchemeName(int colorScheme) { - switch (colorScheme) { - case KeyboardView.COLOR_SCHEME_WHITE: return "white"; - case KeyboardView.COLOR_SCHEME_BLACK: return "black"; + public static String f2KeyModeName(int f2KeyMode) { + switch (f2KeyMode) { + case F2KEY_MODE_NONE: return "none"; + case F2KEY_MODE_SETTINGS: return "settings"; + case F2KEY_MODE_SHORTCUT_IME: return "shortcutIme"; + case F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS: return "shortcutImeOrSettings"; + default: return null; } - return null; } } diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java index 384139112..275e9d1fe 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java @@ -16,7 +16,20 @@ package com.android.inputmethod.keyboard; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Resources; +import android.util.Log; +import android.view.ContextThemeWrapper; +import android.view.InflateException; +import android.view.LayoutInflater; +import android.view.View; +import android.view.inputmethod.EditorInfo; + +import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; +import com.android.inputmethod.keyboard.internal.ModifierKeyState; +import com.android.inputmethod.keyboard.internal.ShiftKeyState; import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.R; @@ -24,37 +37,30 @@ import com.android.inputmethod.latin.Settings; import com.android.inputmethod.latin.SubtypeSwitcher; import com.android.inputmethod.latin.Utils; -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.EditorInfo; - 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; + private static final String TAG = KeyboardSwitcher.class.getSimpleName(); + private static final boolean DEBUG_CACHE = LatinImeLogger.sDBG; public static final boolean DEBUG_STATE = false; - private static String sConfigDefaultKeyboardThemeId; public static final String PREF_KEYBOARD_LAYOUT = "pref_keyboard_layout_20100902"; private static final int[] KEYBOARD_THEMES = { - 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, + R.style.KeyboardTheme, + R.style.KeyboardTheme_HighContrast, + R.style.KeyboardTheme_Stone, + R.style.KeyboardTheme_Stone_Bold, + R.style.KeyboardTheme_Gingerbread, + R.style.KeyboardTheme_IceCreamSandwich, }; private SubtypeSwitcher mSubtypeSwitcher; private SharedPreferences mPrefs; - private LatinKeyboardView mInputView; + private View mCurrentInputView; + private LatinKeyboardView mKeyboardView; private LatinIME mInputMethodService; // TODO: Combine these key state objects with auto mode switch state. @@ -98,7 +104,9 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha // Default is SETTINGS_KEY_MODE_AUTO. private static final int DEFAULT_SETTINGS_KEY_MODE = SETTINGS_KEY_MODE_AUTO; - private int mLayoutId; + private int mThemeIndex = -1; + private Context mThemeContext; + private int mKeyboardWidth; private static final KeyboardSwitcher sInstance = new KeyboardSwitcher(); @@ -114,17 +122,30 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha sInstance.mInputMethodService = ims; sInstance.mPrefs = prefs; sInstance.mSubtypeSwitcher = SubtypeSwitcher.getInstance(); + sInstance.setContextThemeWrapper(ims, getKeyboardThemeIndex(ims, prefs)); + prefs.registerOnSharedPreferenceChangeListener(sInstance); + } + private static int getKeyboardThemeIndex(Context context, SharedPreferences prefs) { + final String defaultThemeId = context.getString(R.string.config_default_keyboard_theme_id); + final String themeId = prefs.getString(PREF_KEYBOARD_LAYOUT, defaultThemeId); try { - sConfigDefaultKeyboardThemeId = ims.getString( - R.string.config_default_keyboard_theme_id); - sInstance.mLayoutId = Integer.valueOf( - prefs.getString(PREF_KEYBOARD_LAYOUT, sConfigDefaultKeyboardThemeId)); + final int themeIndex = Integer.valueOf(themeId); + if (themeIndex >= 0 && themeIndex < KEYBOARD_THEMES.length) + return themeIndex; } catch (NumberFormatException e) { - sConfigDefaultKeyboardThemeId = "0"; - sInstance.mLayoutId = 0; + // Format error, keyboard theme is default to 0. + } + Log.w(TAG, "Illegal keyboard theme in preference: " + themeId + ", default to 0"); + return 0; + } + + private void setContextThemeWrapper(Context context, int themeIndex) { + if (mThemeIndex != themeIndex) { + mThemeIndex = themeIndex; + mThemeContext = new ContextThemeWrapper(context, KEYBOARD_THEMES[themeIndex]); + mKeyboardCache.clear(); } - prefs.registerOnSharedPreferenceChangeListener(sInstance); } public void loadKeyboard(EditorInfo attribute, boolean voiceKeyEnabled, @@ -142,7 +163,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha private void loadKeyboardInternal(EditorInfo attribute, boolean voiceButtonEnabled, boolean voiceButtonOnPrimary, boolean isSymbols) { - if (mInputView == null) return; + if (mKeyboardView == null) return; mAttribute = attribute; mVoiceKeyEnabled = voiceButtonEnabled; @@ -151,17 +172,39 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha // Update the settings key state because number of enabled IMEs could have been changed mSettingsKeyEnabledInSettings = getSettingsKeyMode(mPrefs, mInputMethodService); final KeyboardId id = getKeyboardId(attribute, isSymbols); - makeSymbolsKeyboardIds(id.mMode, attribute); - mCurrentId = id; - final Resources res = mInputMethodService.getResources(); - mInputView.setKeyPreviewPopupEnabled(Settings.Values.isKeyPreviewPopupEnabled(mPrefs, res), - Settings.Values.getKeyPreviewPopupDismissDelay(mPrefs, res)); + + // 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 = makeSiblingKeyboardId(id, R.xml.kbd_symbols, R.xml.kbd_phone); + mSymbolsShiftedId = makeSiblingKeyboardId( + id, R.xml.kbd_symbols_shift, R.xml.kbd_phone_symbols); + setKeyboard(getKeyboard(id)); } + public void onSizeChanged() { + final int width = mInputMethodService.getWindow().getWindow().getDecorView().getWidth(); + if (width == 0 || mCurrentId == null) + return; + mKeyboardWidth = width; + // Set keyboard with new width. + final KeyboardId newId = mCurrentId.cloneWithNewGeometry(width); + setKeyboard(getKeyboard(newId)); + } + private void setKeyboard(final Keyboard newKeyboard) { - final Keyboard oldKeyboard = mInputView.getKeyboard(); - mInputView.setKeyboard(newKeyboard); + final Keyboard oldKeyboard = mKeyboardView.getKeyboard(); + mKeyboardView.setKeyboard(newKeyboard); + mCurrentId = newKeyboard.mId; + final Resources res = mInputMethodService.getResources(); + mKeyboardView.setKeyPreviewPopupEnabled( + Settings.Values.isKeyPreviewPopupEnabled(mPrefs, res), + Settings.Values.getKeyPreviewPopupDismissDelay(mPrefs, res)); final boolean localeChanged = (oldKeyboard == null) || !newKeyboard.mId.mLocale.equals(oldKeyboard.mId.mLocale); mInputMethodService.mHandler.startDisplayLanguageOnSpacebar(localeChanged); @@ -175,19 +218,19 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha final Locale savedLocale = Utils.setSystemLocale(res, mSubtypeSwitcher.getInputLocale()); - keyboard = new LatinKeyboard(mInputMethodService, id); + keyboard = new LatinKeyboard(mThemeContext, id, id.mWidth); if (id.mEnableShiftLock) { keyboard.enableShiftLock(); } mKeyboardCache.put(id, new SoftReference<LatinKeyboard>(keyboard)); - if (DEBUG) + if (DEBUG_CACHE) Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": " + ((ref == null) ? "LOAD" : "GCed") + " id=" + id); Utils.setSystemLocale(res, savedLocale); - } else if (DEBUG) { + } else if (DEBUG_CACHE) { Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": HIT id=" + id); } @@ -215,7 +258,6 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha private KeyboardId getKeyboardId(EditorInfo attribute, boolean isSymbols) { final int mode = Utils.getKeyboardMode(attribute); final boolean hasVoiceKey = hasVoiceKey(isSymbols); - final int charColorId = getColorScheme(); final int xmlId; final boolean enableShiftLock; @@ -242,35 +284,25 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha } } final boolean hasSettingsKey = hasSettingsKey(attribute); + final int f2KeyMode = getF2KeyMode(mPrefs, mInputMethodService, attribute); + final boolean clobberSettingsKey = Utils.inPrivateImeOptions( + mInputMethodService.getPackageName(), LatinIME.IME_OPTION_NO_SETTINGS_KEY, + attribute); final Resources res = mInputMethodService.getResources(); final int orientation = res.getConfiguration().orientation; + if (mKeyboardWidth == 0) + mKeyboardWidth = res.getDisplayMetrics().widthPixels; final Locale locale = mSubtypeSwitcher.getInputLocale(); return new KeyboardId( - res.getResourceEntryName(xmlId), xmlId, charColorId, locale, orientation, mode, - attribute, hasSettingsKey, mVoiceKeyEnabled, hasVoiceKey, enableShiftLock); + res.getResourceEntryName(xmlId), xmlId, locale, orientation, mKeyboardWidth, + mode, attribute, hasSettingsKey, f2KeyMode, clobberSettingsKey, mVoiceKeyEnabled, + hasVoiceKey, enableShiftLock); } - private void makeSymbolsKeyboardIds(final int mode, EditorInfo attribute) { - final Locale locale = mSubtypeSwitcher.getInputLocale(); - final Resources res = mInputMethodService.getResources(); - final int orientation = res.getConfiguration().orientation; - final int colorScheme = getColorScheme(); - final boolean hasVoiceKey = mVoiceKeyEnabled && !mVoiceButtonOnPrimary; - final boolean hasSettingsKey = hasSettingsKey(attribute); - // 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. - int xmlId = mode == KeyboardId.MODE_PHONE ? R.xml.kbd_phone : R.xml.kbd_symbols; - final String xmlName = res.getResourceEntryName(xmlId); - mSymbolsId = new KeyboardId(xmlName, xmlId, colorScheme, locale, orientation, mode, - attribute, hasSettingsKey, mVoiceKeyEnabled, hasVoiceKey, false); - xmlId = mode == KeyboardId.MODE_PHONE ? R.xml.kbd_phone_symbols : R.xml.kbd_symbols_shift; - mSymbolsShiftedId = new KeyboardId(xmlName, xmlId, colorScheme, locale, orientation, mode, - attribute, hasSettingsKey, mVoiceKeyEnabled, hasVoiceKey, false); + private KeyboardId makeSiblingKeyboardId(KeyboardId base, int alphabet, int phone) { + final int xmlId = base.mMode == KeyboardId.MODE_PHONE ? phone : alphabet; + final String xmlName = mInputMethodService.getResources().getResourceEntryName(xmlId); + return base.cloneWithNewLayout(xmlName, xmlId); } public int getKeyboardMode() { @@ -282,18 +314,18 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha } public boolean isInputViewShown() { - return mInputView != null && mInputView.isShown(); + return mCurrentInputView != null && mCurrentInputView.isShown(); } public boolean isKeyboardAvailable() { - if (mInputView != null) - return mInputView.getKeyboard() != null; + if (mKeyboardView != null) + return mKeyboardView.getKeyboard() != null; return false; } public LatinKeyboard getLatinKeyboard() { - if (mInputView != null) { - final Keyboard keyboard = mInputView.getKeyboard(); + if (mKeyboardView != null) { + final Keyboard keyboard = mKeyboardView.getKeyboard(); if (keyboard instanceof LatinKeyboard) return (LatinKeyboard)keyboard; } @@ -346,7 +378,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha latinKeyboard.setShiftLocked(false); } if (latinKeyboard.setShifted(shifted)) { - mInputView.invalidateAllKeys(); + mKeyboardView.invalidateAllKeys(); } } } @@ -354,7 +386,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha private void setShiftLocked(boolean shiftLocked) { LatinKeyboard latinKeyboard = getLatinKeyboard(); if (latinKeyboard != null && latinKeyboard.setShiftLocked(shiftLocked)) { - mInputView.invalidateAllKeys(); + mKeyboardView.invalidateAllKeys(); } } @@ -396,7 +428,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha LatinKeyboard latinKeyboard = getLatinKeyboard(); if (latinKeyboard != null) { latinKeyboard.setAutomaticTemporaryUpperCase(); - mInputView.invalidateAllKeys(); + mKeyboardView.invalidateAllKeys(); } } @@ -491,7 +523,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha // To be able to turn off caps lock by "double tap" on shift key, we should ignore // the second tap of the "double tap" from now for a while because we just have // already turned off caps lock above. - mInputView.startIgnoringDoubleTap(); + mKeyboardView.startIgnoringDoubleTap(); } else if (isShiftedOrShiftLocked() && shiftKeyState.isPressingOnShifted() && !withSliding) { // Shift has been pressed without chording while shifted state. @@ -561,14 +593,12 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha return; final LatinKeyboard keyboard; if (mCurrentId.equals(mSymbolsId) || !mCurrentId.equals(mSymbolsShiftedId)) { - mCurrentId = mSymbolsShiftedId; - keyboard = getKeyboard(mCurrentId); + keyboard = getKeyboard(mSymbolsShiftedId); // Symbol shifted keyboard has an ALT key that has a caps lock style indicator. To // enable the indicator, we need to call setShiftLocked(true). keyboard.setShiftLocked(true); } else { - mCurrentId = mSymbolsId; - keyboard = getKeyboard(mCurrentId); + keyboard = getKeyboard(mSymbolsId); // Symbol keyboard has an ALT key that has a caps lock style indicator. To disable the // indicator, we need to call setShiftLocked(false). keyboard.setShiftLocked(false); @@ -582,11 +612,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha } public boolean isVibrateAndSoundFeedbackRequired() { - return mInputView == null || !mInputView.isInSlidingKeyInput(); + return mKeyboardView == null || !mKeyboardView.isInSlidingKeyInput(); } private int getPointerCount() { - return mInputView == null ? 0 : mInputView.getPointerCount(); + return mKeyboardView == null ? 0 : mKeyboardView.getPointerCount(); } private void toggleKeyboardMode() { @@ -599,7 +629,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha } public boolean hasDistinctMultitouch() { - return mInputView != null && mInputView.hasDistinctMultitouch(); + return mKeyboardView != null && mKeyboardView.hasDistinctMultitouch(); } private static boolean isSpaceCharacter(int c) { @@ -696,53 +726,58 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha } } - public LatinKeyboardView getInputView() { - return mInputView; + public LatinKeyboardView getKeyboardView() { + return mKeyboardView; } - public LatinKeyboardView onCreateInputView() { - createInputViewInternal(mLayoutId, true); - return mInputView; + public View onCreateInputView() { + return createInputView(mThemeIndex, true); } - private void createInputViewInternal(int newLayout, boolean forceReset) { - int layoutId = newLayout; - if (mLayoutId != layoutId || mInputView == null || forceReset) { - if (mInputView != null) { - mInputView.closing(); - } - if (KEYBOARD_THEMES.length <= layoutId) { - layoutId = Integer.valueOf(sConfigDefaultKeyboardThemeId); - } + private View createInputView(final int newThemeIndex, final boolean forceRecreate) { + if (mCurrentInputView != null && mThemeIndex == newThemeIndex && !forceRecreate) + return mCurrentInputView; - Utils.GCUtils.getInstance().reset(); - boolean tryGC = true; - for (int i = 0; i < Utils.GCUtils.GC_TRY_LOOP_MAX && tryGC; ++i) { - try { - mInputView = (LatinKeyboardView) mInputMethodService.getLayoutInflater( - ).inflate(KEYBOARD_THEMES[layoutId], null); - tryGC = false; - } catch (OutOfMemoryError e) { - Log.w(TAG, "load keyboard failed: " + e); - tryGC = Utils.GCUtils.getInstance().tryGCOrWait( - mLayoutId + "," + layoutId, e); - } catch (InflateException e) { - Log.w(TAG, "load keyboard failed: " + e); - tryGC = Utils.GCUtils.getInstance().tryGCOrWait( - mLayoutId + "," + layoutId, e); - } + if (mKeyboardView != null) { + mKeyboardView.closing(); + } + + final int oldThemeIndex = mThemeIndex; + Utils.GCUtils.getInstance().reset(); + boolean tryGC = true; + for (int i = 0; i < Utils.GCUtils.GC_TRY_LOOP_MAX && tryGC; ++i) { + try { + setContextThemeWrapper(mInputMethodService, newThemeIndex); + mCurrentInputView = LayoutInflater.from(mThemeContext).inflate( + R.layout.input_view, null); + tryGC = false; + } catch (OutOfMemoryError e) { + Log.w(TAG, "load keyboard failed: " + e); + tryGC = Utils.GCUtils.getInstance().tryGCOrWait( + oldThemeIndex + "," + newThemeIndex, e); + } catch (InflateException e) { + Log.w(TAG, "load keyboard failed: " + e); + tryGC = Utils.GCUtils.getInstance().tryGCOrWait( + oldThemeIndex + "," + newThemeIndex, e); } - mInputView.setOnKeyboardActionListener(mInputMethodService); - mLayoutId = layoutId; } + + mKeyboardView = (LatinKeyboardView) mCurrentInputView.findViewById(R.id.keyboard_view); + mKeyboardView.setOnKeyboardActionListener(mInputMethodService); + + // This always needs to be set since the accessibility state can + // potentially change without the input view being re-created. + AccessibleKeyboardViewProxy.setView(mKeyboardView); + + return mCurrentInputView; } - private void postSetInputView() { + private void postSetInputView(final View newInputView) { mInputMethodService.mHandler.post(new Runnable() { @Override public void run() { - if (mInputView != null) { - mInputMethodService.setInputView(mInputView); + if (newInputView != null) { + mInputMethodService.setInputView(newInputView); } mInputMethodService.updateInputViewShown(); } @@ -752,43 +787,39 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (PREF_KEYBOARD_LAYOUT.equals(key)) { - final int layoutId = Integer.valueOf( - sharedPreferences.getString(key, sConfigDefaultKeyboardThemeId)); - createInputViewInternal(layoutId, false); - postSetInputView(); + final int layoutId = getKeyboardThemeIndex(mInputMethodService, sharedPreferences); + postSetInputView(createInputView(layoutId, false)); } else if (Settings.PREF_SETTINGS_KEY.equals(key)) { mSettingsKeyEnabledInSettings = getSettingsKeyMode(sharedPreferences, mInputMethodService); - createInputViewInternal(mLayoutId, true); - postSetInputView(); + postSetInputView(createInputView(mThemeIndex, true)); } } - private int getColorScheme() { - return (mInputView != null) - ? mInputView.getColorScheme() : KeyboardView.COLOR_SCHEME_WHITE; - } - public void onAutoCorrectionStateChanged(boolean isAutoCorrection) { - if (isAutoCorrection != mIsAutoCorrectionActive) { - LatinKeyboardView keyboardView = getInputView(); + if (mIsAutoCorrectionActive != isAutoCorrection) { mIsAutoCorrectionActive = isAutoCorrection; - keyboardView.invalidateKey(((LatinKeyboard) keyboardView.getKeyboard()) - .onAutoCorrectionStateChanged(isAutoCorrection)); + final LatinKeyboard keyboard = getLatinKeyboard(); + if (keyboard != null && keyboard.needsAutoCorrectionSpacebarLed()) { + final Key invalidatedKey = keyboard.onAutoCorrectionStateChanged(isAutoCorrection); + final LatinKeyboardView keyboardView = getKeyboardView(); + if (keyboardView != null) + keyboardView.invalidateKey(invalidatedKey); + } } } private static boolean getSettingsKeyMode(SharedPreferences prefs, Context context) { - Resources resources = context.getResources(); - final boolean showSettingsKeyOption = resources.getBoolean( + final Resources res = context.getResources(); + final boolean showSettingsKeyOption = res.getBoolean( R.bool.config_enable_show_settings_key_option); if (showSettingsKeyOption) { final String settingsKeyMode = prefs.getString(Settings.PREF_SETTINGS_KEY, - resources.getString(DEFAULT_SETTINGS_KEY_MODE)); + res.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)) + if (settingsKeyMode.equals(res.getString(SETTINGS_KEY_MODE_ALWAYS_SHOW)) + || (settingsKeyMode.equals(res.getString(SETTINGS_KEY_MODE_AUTO)) && Utils.hasMultipleEnabledIMEsOrSubtypes( (InputMethodManagerCompatWrapper.getInstance(context))))) { return true; @@ -798,4 +829,21 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha // If the show settings key option is disabled, we always try showing the settings key. return true; } + + private static int getF2KeyMode(SharedPreferences prefs, Context context, + EditorInfo attribute) { + final boolean clobberSettingsKey = Utils.inPrivateImeOptions( + context.getPackageName(), LatinIME.IME_OPTION_NO_SETTINGS_KEY, attribute); + final Resources res = context.getResources(); + final String settingsKeyMode = prefs.getString(Settings.PREF_SETTINGS_KEY, + res.getString(DEFAULT_SETTINGS_KEY_MODE)); + if (settingsKeyMode.equals(res.getString(SETTINGS_KEY_MODE_AUTO))) { + return clobberSettingsKey ? KeyboardId.F2KEY_MODE_SHORTCUT_IME + : KeyboardId.F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS; + } else if (settingsKeyMode.equals(res.getString(SETTINGS_KEY_MODE_ALWAYS_SHOW))) { + return clobberSettingsKey ? KeyboardId.F2KEY_MODE_NONE : KeyboardId.F2KEY_MODE_SETTINGS; + } else { // SETTINGS_KEY_MODE_ALWAYS_HIDE + return KeyboardId.F2KEY_MODE_SHORTCUT_IME; + } + } } diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index 4f85c0348..8d4bfd7a7 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -16,9 +16,6 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.latin.LatinImeLogger; -import com.android.inputmethod.latin.R; - import android.content.Context; import android.content.pm.PackageManager; import android.content.res.Resources; @@ -44,12 +41,19 @@ import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; -import android.view.ViewGroup.MarginLayoutParams; -import android.widget.FrameLayout; -import android.widget.LinearLayout; +import android.view.accessibility.AccessibilityEvent; import android.widget.PopupWindow; import android.widget.TextView; +import com.android.inputmethod.accessibility.AccessibilityUtils; +import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy; +import com.android.inputmethod.compat.FrameLayoutCompatUtils; +import com.android.inputmethod.keyboard.internal.MiniKeyboardBuilder; +import com.android.inputmethod.keyboard.internal.PointerTrackerQueue; +import com.android.inputmethod.keyboard.internal.SwipeTracker; +import com.android.inputmethod.latin.LatinImeLogger; +import com.android.inputmethod.latin.R; + import java.util.ArrayList; import java.util.HashMap; import java.util.WeakHashMap; @@ -59,17 +63,21 @@ import java.util.WeakHashMap; * presses and touch movements. * * @attr ref R.styleable#KeyboardView_backgroundDimAmount - * @attr ref R.styleable#KeyboardView_colorScheme * @attr ref R.styleable#KeyboardView_keyBackground * @attr ref R.styleable#KeyboardView_keyHysteresisDistance * @attr ref R.styleable#KeyboardView_keyLetterRatio - * @attr ref R.styleable#KeyboardView_keyLetterStyle + * @attr ref R.styleable#KeyboardView_keyLabelRatio + * @attr ref R.styleable#KeyboardView_keyHintLetterRatio + * @attr ref R.styleable#KeyboardView_keyUppercaseLetterRatio + * @attr ref R.styleable#KeyboardView_keyTextStyle * @attr ref R.styleable#KeyboardView_keyPreviewLayout * @attr ref R.styleable#KeyboardView_keyPreviewOffset * @attr ref R.styleable#KeyboardView_keyPreviewHeight * @attr ref R.styleable#KeyboardView_keyTextColor * @attr ref R.styleable#KeyboardView_keyTextColorDisabled - * @attr ref R.styleable#KeyboardView_labelTextRatio + * @attr ref R.styleable#KeyboardView_keyHintLetterColor + * @attr ref R.styleable#KeyboardView_keyUppercaseLetterInactivatedColor + * @attr ref R.styleable#KeyboardView_keyUppercaseLetterActivatedColor * @attr ref R.styleable#KeyboardView_verticalCorrection * @attr ref R.styleable#KeyboardView_popupLayout * @attr ref R.styleable#KeyboardView_shadowColor @@ -83,9 +91,6 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { private static final boolean ENABLE_CAPSLOCK_BY_LONGPRESS = true; private static final boolean ENABLE_CAPSLOCK_BY_DOUBLETAP = true; - public static final int COLOR_SCHEME_WHITE = 0; - public static final int COLOR_SCHEME_BLACK = 1; - // Timing constants private final int mKeyRepeatInterval; @@ -96,10 +101,11 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { // XML attribute private final float mKeyLetterRatio; private final int mKeyTextColor; - private final int mKeyTextColorDisabled; - private final Typeface mKeyLetterStyle; - private final float mLabelTextRatio; - private final int mColorScheme; + private final int mKeyTextInactivatedColor; + private final Typeface mKeyTextStyle; + private final float mKeyLabelRatio; + private final float mKeyHintLetterRatio; + private final float mKeyUppercaseLetterRatio; private final int mShadowColor; private final float mShadowRadius; private final Drawable mKeyBackground; @@ -109,11 +115,17 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { private final int mPreviewOffset; private final int mPreviewHeight; private final int mPopupLayout; + private final Drawable mKeyPopupHintIcon; + private final int mKeyHintLetterColor; + private final int mKeyUppercaseLetterInactivatedColor; + private final int mKeyUppercaseLetterActivatedColor; // Main keyboard private Keyboard mKeyboard; private int mKeyLetterSize; - private int mLabelTextSize; + private int mKeyLabelSize; + private int mKeyHintLetterSize; + private int mKeyUppercaseLetterSize; // Key preview private boolean mInForeground; @@ -121,7 +133,6 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { private float mPreviewTextRatio; private int mPreviewTextSize; private boolean mShowKeyPreviewPopup = true; - private int mKeyPreviewPopupDisplayedY; private final int mDelayBeforePreview; private int mDelayAfterPreview; private ViewGroup mPreviewPlacer; @@ -168,18 +179,17 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { private Bitmap mBuffer; /** The canvas for the above mutable keyboard bitmap */ private Canvas mCanvas; - private final Paint mPaint; - private final Rect mPadding; + private final Paint mPaint = new Paint(); + private final Rect mPadding = new Rect(); + private final Rect mTextBounds = new Rect(); // This map caches key label text height in pixel as value and key label text size as map key. private final HashMap<Integer, Integer> mTextHeightCache = new HashMap<Integer, Integer>(); + // This map caches key label text width in pixel as value and key label text size as map key. + private final HashMap<Integer, Integer> mTextWidthCache = new HashMap<Integer, Integer>(); // Distance from horizontal center of the key, proportional to key label text height and width. - private final float KEY_LABEL_VERTICAL_ADJUSTMENT_FACTOR_CENTER = 0.45f; - private final float KEY_LABEL_VERTICAL_PADDING_FACTOR = 1.60f; - private final String KEY_LABEL_REFERENCE_CHAR = "H"; - private final int KEY_LABEL_OPTION_ALIGN_LEFT = 1; - private final int KEY_LABEL_OPTION_ALIGN_RIGHT = 2; - private final int KEY_LABEL_OPTION_ALIGN_BOTTOM = 8; - private final int KEY_LABEL_OPTION_FONT_NORMAL = 16; + private static final float KEY_LABEL_VERTICAL_ADJUSTMENT_FACTOR_CENTER = 0.45f; + private static final float KEY_LABEL_VERTICAL_PADDING_FACTOR = 1.60f; + private static final String KEY_LABEL_REFERENCE_CHAR = "M"; private final int mKeyLabelHorizontalPadding; private final UIHandler mHandler = new UIHandler(); @@ -321,18 +331,27 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { mPreviewOffset = a.getDimensionPixelOffset(R.styleable.KeyboardView_keyPreviewOffset, 0); mPreviewHeight = a.getDimensionPixelSize(R.styleable.KeyboardView_keyPreviewHeight, 80); mKeyLetterRatio = getRatio(a, R.styleable.KeyboardView_keyLetterRatio); + mKeyLabelRatio = getRatio(a, R.styleable.KeyboardView_keyLabelRatio); + mKeyHintLetterRatio = getRatio(a, R.styleable.KeyboardView_keyHintLetterRatio); + mKeyUppercaseLetterRatio = getRatio(a, + R.styleable.KeyboardView_keyUppercaseLetterRatio); mKeyTextColor = a.getColor(R.styleable.KeyboardView_keyTextColor, 0xFF000000); - mKeyTextColorDisabled = a.getColor( - R.styleable.KeyboardView_keyTextColorDisabled, 0xFF000000); - mLabelTextRatio = getRatio(a, R.styleable.KeyboardView_labelTextRatio); + mKeyTextInactivatedColor = a.getColor( + R.styleable.KeyboardView_keyTextInactivatedColor, 0xFF000000); + mKeyPopupHintIcon = a.getDrawable(R.styleable.KeyboardView_keyPopupHintIcon); + mKeyHintLetterColor = a.getColor(R.styleable.KeyboardView_keyHintLetterColor, 0); + mKeyUppercaseLetterInactivatedColor = a.getColor( + R.styleable.KeyboardView_keyUppercaseLetterInactivatedColor, 0); + mKeyUppercaseLetterActivatedColor = a.getColor( + R.styleable.KeyboardView_keyUppercaseLetterActivatedColor, 0); + mKeyTextStyle = Typeface.defaultFromStyle( + a.getInt(R.styleable.KeyboardView_keyTextStyle, Typeface.NORMAL)); mPopupLayout = a.getResourceId(R.styleable.KeyboardView_popupLayout, 0); mShadowColor = a.getColor(R.styleable.KeyboardView_shadowColor, 0); mShadowRadius = a.getFloat(R.styleable.KeyboardView_shadowRadius, 0f); // TODO: Use Theme (android.R.styleable.Theme_backgroundDimAmount) mBackgroundDimAmount = a.getFloat(R.styleable.KeyboardView_backgroundDimAmount, 0.5f); - mKeyLetterStyle = Typeface.defaultFromStyle( - a.getInt(R.styleable.KeyboardView_keyLetterStyle, Typeface.NORMAL)); - mColorScheme = a.getInt(R.styleable.KeyboardView_colorScheme, COLOR_SCHEME_WHITE); + a.recycle(); final Resources res = getResources(); @@ -347,12 +366,10 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { mKeyLabelHorizontalPadding = (int)res.getDimension( R.dimen.key_label_horizontal_alignment_padding); - mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setTextAlign(Align.CENTER); mPaint.setAlpha(255); - mPadding = new Rect(0, 0, 0, 0); mKeyBackground.getPadding(mPadding); mSwipeThreshold = (int) (500 * res.getDisplayMetrics().density); @@ -462,6 +479,12 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { return mKeyboardActionListener; } + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + // TODO: Should notify InputMethodService instead? + KeyboardSwitcher.getInstance().onSizeChanged(); + } + /** * Attaches a keyboard to this view. The keyboard can be switched at any time and the * view will re-layout itself to accommodate the keyboard. @@ -490,7 +513,10 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { mPopupPanelCache.clear(); final int keyHeight = keyboard.getRowHeight() - keyboard.getVerticalGap(); mKeyLetterSize = (int)(keyHeight * mKeyLetterRatio); - mLabelTextSize = (int)(keyHeight * mLabelTextRatio); + mKeyLabelSize = (int)(keyHeight * mKeyLabelRatio); + mKeyHintLetterSize = (int)(keyHeight * mKeyHintLetterRatio); + mKeyUppercaseLetterSize = (int)( + keyHeight * mKeyUppercaseLetterRatio); mPreviewTextSize = (int)(keyHeight * mPreviewTextRatio); } @@ -533,10 +559,6 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { return mShowKeyPreviewPopup; } - public int getColorScheme() { - return mColorScheme; - } - /** * When enabled, calls to {@link KeyboardActionListener#onCodeInput} will include key * codes for adjacent keys. When disabled, only the primary key code will be @@ -597,8 +619,14 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { mDirtyRect.union(0, 0, width, height); } if (mBuffer == null || mBuffer.getWidth() != width || mBuffer.getHeight() != height) { + if (mBuffer != null) + mBuffer.recycle(); mBuffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); - mCanvas = new Canvas(mBuffer); + if (mCanvas != null) { + mCanvas.setBitmap(mBuffer); + } else { + mCanvas = new Canvas(mBuffer); + } } final Canvas canvas = mCanvas; canvas.clipRect(mDirtyRect, Op.REPLACE); @@ -677,7 +705,7 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { // Vertical label text alignment. final float baseline; - if ((key.mLabelOption & KEY_LABEL_OPTION_ALIGN_BOTTOM) != 0) { + if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_BOTTOM) != 0) { baseline = key.mHeight - labelCharHeight * KEY_LABEL_VERTICAL_PADDING_FACTOR; if (DEBUG_SHOW_ALIGN) drawHorizontalLine(canvas, (int)baseline, keyDrawWidth, 0xc0008000, @@ -691,12 +719,12 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { } // Horizontal label text alignment final int positionX; - if ((key.mLabelOption & KEY_LABEL_OPTION_ALIGN_LEFT) != 0) { + if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_LEFT) != 0) { positionX = mKeyLabelHorizontalPadding + padding.left; paint.setTextAlign(Align.LEFT); if (DEBUG_SHOW_ALIGN) drawVerticalLine(canvas, positionX, rowHeight, 0xc0800080, new Paint()); - } else if ((key.mLabelOption & KEY_LABEL_OPTION_ALIGN_RIGHT) != 0) { + } else if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_RIGHT) != 0) { positionX = keyDrawWidth - mKeyLabelHorizontalPadding - padding.right; paint.setTextAlign(Align.RIGHT); if (DEBUG_SHOW_ALIGN) @@ -709,12 +737,12 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { drawVerticalLine(canvas, positionX, rowHeight, 0xc0008080, new Paint()); } } - if (key.mManualTemporaryUpperCaseHintIcon != null && isManualTemporaryUpperCase) { - paint.setColor(mKeyTextColorDisabled); + if (key.hasUppercaseLetter() && isManualTemporaryUpperCase) { + paint.setColor(mKeyTextInactivatedColor); } else { paint.setColor(mKeyTextColor); } - if (key.mEnabled) { + if (key.isEnabled()) { // Set a drop shadow for the text paint.setShadowLayer(mShadowRadius, 0, 0, mShadowColor); } else { @@ -726,6 +754,27 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { paint.setShadowLayer(0, 0, 0, 0); } + // Draw hint letter. + if (key.mHintLetter != null) { + final String label = key.mHintLetter.toString(); + final int textColor; + final int textSize; + if (key.hasUppercaseLetter()) { + textColor = isManualTemporaryUpperCase ? mKeyUppercaseLetterActivatedColor + : mKeyUppercaseLetterInactivatedColor; + textSize = mKeyUppercaseLetterSize; + } else { + textColor = mKeyHintLetterColor; + textSize = mKeyHintLetterSize; + } + paint.setColor(textColor); + paint.setTextSize(textSize); + // Note: padding.right for drawX? + final float drawX = keyDrawWidth - getLabelCharWidth(textSize, paint); + final float drawY = -paint.ascent() + padding.top; + canvas.drawText(label, drawX, drawY, paint); + } + // Draw key icon. final Drawable icon = key.getIcon(); if (key.mLabel == null && icon != null) { @@ -733,11 +782,11 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { final int drawableHeight = icon.getIntrinsicHeight(); final int drawableX; final int drawableY = (key.mHeight + padding.top - padding.bottom - drawableHeight) / 2; - if ((key.mLabelOption & KEY_LABEL_OPTION_ALIGN_LEFT) != 0) { + if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_LEFT) != 0) { drawableX = padding.left + mKeyLabelHorizontalPadding; if (DEBUG_SHOW_ALIGN) drawVerticalLine(canvas, drawableX, rowHeight, 0xc0800080, new Paint()); - } else if ((key.mLabelOption & KEY_LABEL_OPTION_ALIGN_RIGHT) != 0) { + } else if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_RIGHT) != 0) { drawableX = keyDrawWidth - padding.right - mKeyLabelHorizontalPadding - drawableWidth; if (DEBUG_SHOW_ALIGN) @@ -755,15 +804,14 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { 0x80c00000, new Paint()); } - // Draw hint icon. - if (key.mHintIcon != null) { + // Draw popup hint icon "...". + // TODO: Draw "..." by text. + if (key.hasPopupHint()) { final int drawableWidth = keyDrawWidth; final int drawableHeight = key.mHeight; final int drawableX = 0; final int drawableY = HINT_ICON_VERTICAL_ADJUSTMENT_PIXEL; - Drawable hintIcon = (isManualTemporaryUpperCase - && key.mManualTemporaryUpperCaseHintIcon != null) - ? key.mManualTemporaryUpperCaseHintIcon : key.mHintIcon; + final Drawable hintIcon = mKeyPopupHintIcon; drawIcon(canvas, hintIcon, drawableX, drawableY, drawableWidth, drawableHeight); if (DEBUG_SHOW_ALIGN) drawRectangle(canvas, drawableX, drawableY, drawableWidth, drawableHeight, @@ -777,16 +825,18 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { // For characters, use large font. For labels like "Done", use small font. final int labelSize; final Typeface labelStyle; + if ((keyLabelOption & Key.LABEL_OPTION_FONT_NORMAL) != 0) { + labelStyle = Typeface.DEFAULT; + } else if ((keyLabelOption & Key.LABEL_OPTION_FONT_FIXED_WIDTH) != 0) { + labelStyle = Typeface.MONOSPACE; + } else { + labelStyle = mKeyTextStyle; + } if (label.length() > 1) { - labelSize = mLabelTextSize; - if ((keyLabelOption & KEY_LABEL_OPTION_FONT_NORMAL) != 0) { - labelStyle = Typeface.DEFAULT; - } else { - labelStyle = Typeface.DEFAULT_BOLD; - } + labelSize = (keyLabelOption & Key.LABEL_OPTION_FOLLOW_KEY_LETTER_RATIO) != 0 + ? mKeyLetterSize : mKeyLabelSize; } else { labelSize = mKeyLetterSize; - labelStyle = mKeyLetterStyle; } paint.setTextSize(labelSize); paint.setTypeface(labelStyle); @@ -799,14 +849,26 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { if (labelHeightValue != null) { labelCharHeight = labelHeightValue; } else { - Rect textBounds = new Rect(); - paint.getTextBounds(KEY_LABEL_REFERENCE_CHAR, 0, 1, textBounds); - labelCharHeight = textBounds.height(); + paint.getTextBounds(KEY_LABEL_REFERENCE_CHAR, 0, 1, mTextBounds); + labelCharHeight = mTextBounds.height(); mTextHeightCache.put(labelSize, labelCharHeight); } return labelCharHeight; } + private int getLabelCharWidth(int labelSize, Paint paint) { + Integer labelWidthValue = mTextWidthCache.get(labelSize); + final int labelCharWidth; + if (labelWidthValue != null) { + labelCharWidth = labelWidthValue; + } else { + paint.getTextBounds(KEY_LABEL_REFERENCE_CHAR, 0, 1, mTextBounds); + labelCharWidth = mTextBounds.width(); + mTextWidthCache.put(labelSize, labelCharWidth); + } + return labelCharWidth; + } + private static void drawIcon(Canvas canvas, Drawable icon, int x, int y, int width, int height) { canvas.translate(x, y); @@ -873,27 +935,12 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { } private void addKeyPreview(TextView keyPreview) { - ViewGroup placer = mPreviewPlacer; - if (placer == null) { - final FrameLayout screenContent = (FrameLayout) getRootView().findViewById( - android.R.id.content); - if (android.os.Build.VERSION.SDK_INT >= /* HONEYCOMB */11) { - placer = screenContent; - } else { - // Insert LinearLayout to be able to setMargin because pre-Honeycomb FrameLayout - // could not handle setMargin properly. - placer = new LinearLayout(getContext()); - screenContent.addView(placer); - } - mPreviewPlacer = placer; - } - if (placer instanceof FrameLayout) { - // Honeycomb or later. - placer.addView(keyPreview, new FrameLayout.LayoutParams(0, 0)); - } else { - // Gingerbread or ealier. - placer.addView(keyPreview, new LinearLayout.LayoutParams(0, 0)); + if (mPreviewPlacer == null) { + mPreviewPlacer = FrameLayoutCompatUtils.getPlacer( + (ViewGroup)getRootView().findViewById(android.R.id.content)); } + final ViewGroup placer = mPreviewPlacer; + placer.addView(keyPreview, FrameLayoutCompatUtils.newLayoutParam(placer, 0, 0)); } // TODO: Introduce minimum duration for displaying key previews @@ -921,14 +968,14 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { if (key.mLabel != null) { // TODO Should take care of temporaryShiftLabel here. previewText.setCompoundDrawables(null, null, null, null); - previewText.setText(adjustCase(tracker.getPreviewText(key))); if (key.mLabel.length() > 1) { previewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyLetterSize); previewText.setTypeface(Typeface.DEFAULT_BOLD); } else { previewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSize); - previewText.setTypeface(mKeyLetterStyle); + previewText.setTypeface(mKeyTextStyle); } + previewText.setText(adjustCase(tracker.getPreviewText(key))); } else { final Drawable previewIcon = key.getPreviewIcon(); previewText.setCompoundDrawables(null, null, null, @@ -947,15 +994,11 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { getLocationInWindow(mCoordinates); final int previewX = keyDrawX - (previewWidth - keyDrawWidth) / 2 + mCoordinates[0]; final int previewY = key.mY - previewHeight + mCoordinates[1] + mPreviewOffset; - // Record key preview position to display mini-keyboard later at the same position - mKeyPreviewPopupDisplayedY = previewY; // Place the key preview. // TODO: Adjust position of key previews which touch screen edges - final MarginLayoutParams lp = (MarginLayoutParams)previewText.getLayoutParams(); - lp.width = previewWidth; - lp.height = previewHeight; - lp.setMargins(previewX, previewY, 0, 0); + FrameLayoutCompatUtils.placeViewAt( + previewText, previewX, previewY, previewWidth, previewHeight); previewText.setVisibility(VISIBLE); } @@ -1070,7 +1113,7 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { miniKeyboardView.setKeyboard(keyboard); container.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST), - MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST)); + MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); return miniKeyboardView; } @@ -1099,7 +1142,7 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { mPopupWindow.setClippingEnabled(false); } mPopupMiniKeyboardPanel = popupPanel; - popupPanel.showPanel(this, parentKey, tracker, mKeyPreviewPopupDisplayedY, mPopupWindow); + popupPanel.showPanel(this, parentKey, tracker, mPopupWindow); invalidateAllKeys(); return true; @@ -1278,4 +1321,37 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { public boolean handleBack() { return dismissMiniKeyboard(); } + + @Override + public boolean dispatchTouchEvent(MotionEvent event) { + if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()) { + return AccessibleKeyboardViewProxy.getInstance().dispatchTouchEvent(event) + || super.dispatchTouchEvent(event); + } + + return super.dispatchTouchEvent(event); + } + + @Override + public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { + if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()) { + final PointerTracker tracker = getPointerTracker(0); + return AccessibleKeyboardViewProxy.getInstance().dispatchPopulateAccessibilityEvent( + event, tracker) || super.dispatchPopulateAccessibilityEvent(event); + } + + return super.dispatchPopulateAccessibilityEvent(event); + } + + public boolean onHoverEvent(MotionEvent event) { + // Since reflection doesn't support calling superclass methods, this + // method checks for the existence of onHoverEvent() in the View class + // before returning a value. + if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()) { + final PointerTracker tracker = getPointerTracker(0); + return AccessibleKeyboardViewProxy.getInstance().onHoverEvent(event, tracker); + } + + return false; + } } diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java index c279769f6..00bf348f2 100644 --- a/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java +++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java @@ -16,9 +16,6 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.SubtypeSwitcher; - import android.content.Context; import android.content.res.Resources; import android.content.res.Resources.Theme; @@ -36,6 +33,10 @@ import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import com.android.inputmethod.keyboard.internal.SlidingLocaleDrawable; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.SubtypeSwitcher; + import java.lang.ref.SoftReference; import java.util.Arrays; import java.util.HashMap; @@ -49,7 +50,8 @@ public class LatinKeyboard extends Keyboard { public static final int CODE_NEXT_LANGUAGE = -100; public static final int CODE_PREV_LANGUAGE = -101; - private final Context mContext; + private final Resources mRes; + private final Theme mTheme; private final SubtypeSwitcher mSubtypeSwitcher = SubtypeSwitcher.getInstance(); /* Space key and its icons, drawables and colors. */ @@ -57,15 +59,16 @@ public class LatinKeyboard extends Keyboard { private final Drawable mSpaceIcon; private final Drawable mSpacePreviewIcon; private final int mSpaceKeyIndex; - private final Drawable mSpaceAutoCorrectionIndicator; - private final Drawable mButtonArrowLeftIcon; - private final Drawable mButtonArrowRightIcon; + private final boolean mAutoCorrectionSpacebarLedEnabled; + private final Drawable mAutoCorrectionSpacebarLedIcon; + private final Drawable mSpacebarArrowLeftIcon; + private final Drawable mSpacebarArrowRightIcon; private final int mSpacebarTextColor; private final int mSpacebarTextShadowColor; private float mSpacebarTextFadeFactor = 0.0f; private final int mSpacebarLanguageSwitchThreshold; private int mSpacebarSlidingLanguageSwitchDiff; - private SlidingLocaleDrawable mSlidingLocaleIcon; + private final SlidingLocaleDrawable mSlidingLocaleIcon; private final HashMap<Integer, SoftReference<BitmapDrawable>> mSpaceDrawableCache = new HashMap<Integer, SoftReference<BitmapDrawable>>(); @@ -78,7 +81,7 @@ public class LatinKeyboard extends Keyboard { // of the most common key width of this keyboard). private static final int SPACEBAR_DRAG_WIDTH = 3; // Minimum width of space key preview (proportional to keyboard width). - private static final float SPACEBAR_POPUP_MIN_RATIO = 0.4f; + private static final float SPACEBAR_POPUP_MIN_RATIO = 0.5f; // Height in space key the language name will be drawn. (proportional to space key height) public 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, @@ -88,10 +91,10 @@ public class LatinKeyboard extends Keyboard { private static final String SMALL_TEXT_SIZE_OF_LANGUAGE_ON_SPACEBAR = "small"; private static final String MEDIUM_TEXT_SIZE_OF_LANGUAGE_ON_SPACEBAR = "medium"; - public LatinKeyboard(Context context, KeyboardId id) { - super(context, id.getXmlId(), id); - final Resources res = context.getResources(); - mContext = context; + public LatinKeyboard(Context context, KeyboardId id, int width) { + super(context, id.getXmlId(), id, width); + mRes = context.getResources(); + mTheme = context.getTheme(); final List<Key> keys = getKeys(); int spaceKeyIndex = -1; @@ -118,21 +121,35 @@ public class LatinKeyboard extends Keyboard { mShortcutKey = (shortcutKeyIndex >= 0) ? keys.get(shortcutKeyIndex) : null; mEnabledShortcutIcon = (mShortcutKey != null) ? mShortcutKey.getIcon() : null; - mSpacebarTextColor = res.getColor(R.color.latinkeyboard_bar_language_text); - if (id.mColorScheme == KeyboardView.COLOR_SCHEME_BLACK) { - mSpacebarTextShadowColor = res.getColor( - R.color.latinkeyboard_bar_language_shadow_black); - mDisabledShortcutIcon = res.getDrawable(R.drawable.sym_bkeyboard_voice_off); - } else { // default color scheme is KeyboardView.COLOR_SCHEME_WHITE - mSpacebarTextShadowColor = res.getColor( - R.color.latinkeyboard_bar_language_shadow_white); - mDisabledShortcutIcon = res.getDrawable(R.drawable.sym_keyboard_voice_off_holo); - } - mSpaceAutoCorrectionIndicator = res.getDrawable(R.drawable.sym_keyboard_space_led); - mButtonArrowLeftIcon = res.getDrawable(R.drawable.sym_keyboard_language_arrows_left); - mButtonArrowRightIcon = res.getDrawable(R.drawable.sym_keyboard_language_arrows_right); + final TypedArray a = context.obtainStyledAttributes( + null, R.styleable.LatinKeyboard, R.attr.latinKeyboardStyle, R.style.LatinKeyboard); + mAutoCorrectionSpacebarLedEnabled = a.getBoolean( + R.styleable.LatinKeyboard_autoCorrectionSpacebarLedEnabled, false); + mAutoCorrectionSpacebarLedIcon = a.getDrawable( + R.styleable.LatinKeyboard_autoCorrectionSpacebarLedIcon); + mDisabledShortcutIcon = a.getDrawable(R.styleable.LatinKeyboard_disabledShortcutIcon); + mSpacebarTextColor = a.getColor(R.styleable.LatinKeyboard_spacebarTextColor, 0); + mSpacebarTextShadowColor = a.getColor( + R.styleable.LatinKeyboard_spacebarTextShadowColor, 0); + mSpacebarArrowLeftIcon = a.getDrawable( + R.styleable.LatinKeyboard_spacebarArrowLeftIcon); + mSpacebarArrowRightIcon = a.getDrawable( + R.styleable.LatinKeyboard_spacebarArrowRightIcon); + a.recycle(); + // The threshold is "key width" x 1.25 mSpacebarLanguageSwitchThreshold = (getMostCommonKeyWidth() * 5) / 4; + + if (mSpaceKey != null && mSpacePreviewIcon != null) { + final int slidingIconWidth = Math.max(mSpaceKey.mWidth, + (int)(getMinWidth() * SPACEBAR_POPUP_MIN_RATIO)); + final int spaceKeyheight = mSpacePreviewIcon.getIntrinsicHeight(); + mSlidingLocaleIcon = new SlidingLocaleDrawable( + context, mSpacePreviewIcon, slidingIconWidth, spaceKeyheight); + mSlidingLocaleIcon.setBounds(0, 0, slidingIconWidth, spaceKeyheight); + } else { + mSlidingLocaleIcon = null; + } } public void setSpacebarTextFadeFactor(float fadeFactor, LatinKeyboardView view) { @@ -157,12 +174,16 @@ public class LatinKeyboard extends Keyboard { public void updateShortcutKey(boolean available, LatinKeyboardView view) { if (mShortcutKey == null) return; - mShortcutKey.mEnabled = available; + mShortcutKey.setEnabled(available); mShortcutKey.setIcon(available ? mEnabledShortcutIcon : mDisabledShortcutIcon); if (view != null) view.invalidateKey(mShortcutKey); } + public boolean needsAutoCorrectionSpacebarLed() { + return mAutoCorrectionSpacebarLedEnabled; + } + /** * @return a key which should be invalidated. */ @@ -193,7 +214,7 @@ public class LatinKeyboard extends Keyboard { } // Layout local language name and left and right arrow on spacebar. - private static String layoutSpacebar(Paint paint, Locale locale, Drawable lArrow, + private static String layoutSpacebar(Paint paint, Locale locale, Drawable icon, Drawable lArrow, Drawable rArrow, int width, int height, float origTextSize) { final float arrowWidth = lArrow.getIntrinsicWidth(); final float arrowHeight = lArrow.getIntrinsicHeight(); @@ -230,7 +251,9 @@ public class LatinKeyboard extends Keyboard { paint.setTextSize(textSize); // Place left and right arrow just before and after language text. - final float baseline = height * SPACEBAR_LANGUAGE_BASELINE; + final float textHeight = -paint.ascent() + paint.descent(); + final float baseline = (icon != null) ? height * SPACEBAR_LANGUAGE_BASELINE + : height / 2 + textHeight / 2; final int top = (int)(baseline - arrowHeight); final float remains = (width - textWidth) / 2; lArrow.setBounds((int)(remains - arrowWidth), top, (int)remains, (int)baseline); @@ -246,7 +269,7 @@ public class LatinKeyboard extends Keyboard { final SoftReference<BitmapDrawable> ref = mSpaceDrawableCache.get(hashCode); BitmapDrawable drawable = (ref == null) ? null : ref.get(); if (drawable == null) { - drawable = new BitmapDrawable(mContext.getResources(), drawSpacebar( + drawable = new BitmapDrawable(mRes, drawSpacebar( locale, isAutoCorrection, mSpacebarTextFadeFactor)); mSpaceDrawableCache.put(hashCode, new SoftReference<BitmapDrawable>(drawable)); } @@ -259,7 +282,7 @@ public class LatinKeyboard extends Keyboard { final int height = mSpaceIcon != null ? mSpaceIcon.getIntrinsicHeight() : mSpaceKey.mHeight; final Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(buffer); - final Resources res = mContext.getResources(); + final Resources res = mRes; canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); // If application locales are explicitly selected. @@ -281,9 +304,9 @@ public class LatinKeyboard extends Keyboard { defaultTextSize = 14; } - final String language = layoutSpacebar(paint, inputLocale, - mButtonArrowLeftIcon, mButtonArrowRightIcon, width, height, - getTextSizeFromTheme(mContext.getTheme(), textStyle, defaultTextSize)); + final String language = layoutSpacebar(paint, inputLocale, mSpaceIcon, + mSpacebarArrowLeftIcon, mSpacebarArrowRightIcon, width, height, + getTextSizeFromTheme(mTheme, textStyle, defaultTextSize)); // Draw language text with shadow // In case there is no space icon, we will place the language text at the center of @@ -303,21 +326,21 @@ public class LatinKeyboard extends Keyboard { if (mSubtypeSwitcher.useSpacebarLanguageSwitcher() && mSubtypeSwitcher.getEnabledKeyboardLocaleCount() > 1 && !(isPhoneKeyboard() || isNumberKeyboard())) { - mButtonArrowLeftIcon.setColorFilter(getSpacebarDrawableFilter(textFadeFactor)); - mButtonArrowRightIcon.setColorFilter(getSpacebarDrawableFilter(textFadeFactor)); - mButtonArrowLeftIcon.draw(canvas); - mButtonArrowRightIcon.draw(canvas); + mSpacebarArrowLeftIcon.setColorFilter(getSpacebarDrawableFilter(textFadeFactor)); + mSpacebarArrowRightIcon.setColorFilter(getSpacebarDrawableFilter(textFadeFactor)); + mSpacebarArrowLeftIcon.draw(canvas); + mSpacebarArrowRightIcon.draw(canvas); } } // Draw the spacebar icon at the bottom if (isAutoCorrection) { final int iconWidth = width * SPACE_LED_LENGTH_PERCENT / 100; - final int iconHeight = mSpaceAutoCorrectionIndicator.getIntrinsicHeight(); + final int iconHeight = mAutoCorrectionSpacebarLedIcon.getIntrinsicHeight(); int x = (width - iconWidth) / 2; int y = height - iconHeight; - mSpaceAutoCorrectionIndicator.setBounds(x, y, x + iconWidth, y + iconHeight); - mSpaceAutoCorrectionIndicator.draw(canvas); + mAutoCorrectionSpacebarLedIcon.setBounds(x, y, x + iconWidth, y + iconHeight); + mAutoCorrectionSpacebarLedIcon.draw(canvas); } else if (mSpaceIcon != null) { final int iconWidth = mSpaceIcon.getIntrinsicWidth(); final int iconHeight = mSpaceIcon.getIntrinsicHeight(); @@ -337,14 +360,8 @@ public class LatinKeyboard extends Keyboard { if (mSpacebarSlidingLanguageSwitchDiff == diff) return; mSpacebarSlidingLanguageSwitchDiff = diff; - if (mSlidingLocaleIcon == null) { - final int width = Math.max(mSpaceKey.mWidth, - (int)(getMinWidth() * SPACEBAR_POPUP_MIN_RATIO)); - final int height = mSpacePreviewIcon.getIntrinsicHeight(); - mSlidingLocaleIcon = - new SlidingLocaleDrawable(mContext, mSpacePreviewIcon, width, height); - mSlidingLocaleIcon.setBounds(0, 0, width, height); - } + if (mSlidingLocaleIcon == null) + return; mSlidingLocaleIcon.setDiff(diff); if (Math.abs(diff) == Integer.MAX_VALUE) { mSpaceKey.setPreviewIcon(mSpacePreviewIcon); @@ -399,7 +416,7 @@ public class LatinKeyboard extends Keyboard { Math.max(0, Math.min(y, getHeight() - 1))); } - private static int getTextSizeFromTheme(Theme theme, int style, int defValue) { + public static int getTextSizeFromTheme(Theme theme, int style, int defValue) { TypedArray array = theme.obtainStyledAttributes( style, new int[] { android.R.attr.textSize }); int textSize = array.getDimensionPixelSize(array.getResourceId(0, 0), defValue); diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java index 583b997ee..901df6ab7 100644 --- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java @@ -16,10 +16,6 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.deprecated.VoiceProxy; -import com.android.inputmethod.latin.LatinImeLogger; -import com.android.inputmethod.latin.Utils; - import android.content.Context; import android.graphics.Canvas; import android.text.TextUtils; @@ -27,6 +23,10 @@ import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; +import com.android.inputmethod.deprecated.VoiceProxy; +import com.android.inputmethod.latin.LatinImeLogger; +import com.android.inputmethod.latin.Utils; + // TODO: We should remove this class public class LatinKeyboardView extends KeyboardView { private static final String TAG = LatinKeyboardView.class.getSimpleName(); @@ -47,7 +47,7 @@ public class LatinKeyboardView extends KeyboardView { private int mLastY; public LatinKeyboardView(Context context, AttributeSet attrs) { - this(context, attrs, 0); + super(context, attrs); } public LatinKeyboardView(Context context, AttributeSet attrs, int defStyle) { @@ -120,7 +120,7 @@ public class LatinKeyboardView extends KeyboardView { && keyboard.isShiftedOrShiftLocked() && !TextUtils.isEmpty(label) && label.length() < 3 && Character.isLowerCase(label.charAt(0))) { - return label.toString().toUpperCase(); + return label.toString().toUpperCase(keyboard.mId.mLocale); } return label; } diff --git a/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java b/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java index 5dde15e94..2d6766f2d 100644 --- a/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java +++ b/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 Google Inc. + * Copyright (C) 2011 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 @@ -23,8 +23,8 @@ import java.util.List; public class MiniKeyboard extends Keyboard { private int mDefaultKeyCoordX; - public MiniKeyboard(Context context, int xmlLayoutResId, KeyboardId id) { - super(context, xmlLayoutResId, id); + public MiniKeyboard(Context context, int xmlLayoutResId, Keyboard parentKeyboard) { + super(context, xmlLayoutResId, null, parentKeyboard.getMinWidth()); } public void setDefaultCoordX(int pos) { diff --git a/java/src/com/android/inputmethod/keyboard/MiniKeyboardKeyDetector.java b/java/src/com/android/inputmethod/keyboard/MiniKeyboardKeyDetector.java index c4459f616..cc5c3bbfe 100644 --- a/java/src/com/android/inputmethod/keyboard/MiniKeyboardKeyDetector.java +++ b/java/src/com/android/inputmethod/keyboard/MiniKeyboardKeyDetector.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java index 953d487dc..c7620f946 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java +++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -16,15 +16,18 @@ package com.android.inputmethod.keyboard; +import android.content.res.Resources; +import android.os.SystemClock; +import android.util.Log; +import android.view.MotionEvent; + import com.android.inputmethod.keyboard.KeyboardView.UIHandler; +import com.android.inputmethod.keyboard.internal.PointerTrackerKeyState; +import com.android.inputmethod.keyboard.internal.PointerTrackerQueue; import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.SubtypeSwitcher; -import android.content.res.Resources; -import android.util.Log; -import android.view.MotionEvent; - import java.util.Arrays; import java.util.List; @@ -146,7 +149,7 @@ public class PointerTracker { + " ignoreModifier=" + ignoreModifierKey); if (ignoreModifierKey) return false; - if (key.mEnabled) { + if (key.isEnabled()) { mListener.onPress(key.mCode, withSliding); final boolean keyboardLayoutHasBeenChanged = mKeyboardLayoutHasBeenChanged; mKeyboardLayoutHasBeenChanged = false; @@ -165,14 +168,14 @@ public class PointerTracker { + " ignoreModifier=" + ignoreModifierKey); if (ignoreModifierKey) return; - if (key.mEnabled) + if (key.isEnabled()) mListener.onCodeInput(primaryCode, keyCodes, x, y); } private void callListenerOnTextInput(Key key) { if (DEBUG_LISTENER) Log.d(TAG, "onTextInput: text=" + key.mOutputText); - if (key.mEnabled) + if (key.isEnabled()) mListener.onTextInput(key.mOutputText); } @@ -185,7 +188,7 @@ public class PointerTracker { + withSliding + " ignoreModifier=" + ignoreModifierKey); if (ignoreModifierKey) return; - if (key.mEnabled) + if (key.isEnabled()) mListener.onRelease(primaryCode, withSliding); } @@ -265,7 +268,7 @@ public class PointerTracker { private void setPressedKeyGraphics(int keyIndex) { final Key key = getKey(keyIndex); - if (key != null && key.mEnabled) { + if (key != null && key.isEnabled()) { key.onPressed(); mProxy.invalidateKey(key); } @@ -540,8 +543,11 @@ public class PointerTracker { public void onLongPressed(PointerTrackerQueue queue) { mKeyAlreadyProcessed = true; - if (queue != null) + if (queue != null) { + // TODO: Support chording + long-press input. + queue.releaseAllPointersExcept(this, SystemClock.uptimeMillis(), true); queue.remove(this); + } } public void onCancelEvent(int x, int y, long eventTime, PointerTrackerQueue queue) { @@ -611,7 +617,7 @@ public class PointerTracker { // The modifier key, such as shift key, should not show its key preview. private boolean isKeyPreviewNotRequired(int keyIndex) { final Key key = getKey(keyIndex); - if (!key.mEnabled) + if (key == null || !key.isEnabled()) return true; // Such as spacebar sliding language switch. if (mKeyboard.needSpacebarPreview(keyIndex)) @@ -633,12 +639,9 @@ public class PointerTracker { private void startLongPressTimer(int keyIndex) { Key key = getKey(keyIndex); - if (!key.mEnabled) - return; if (key.mCode == Keyboard.CODE_SHIFT) { mHandler.startLongPressShiftTimer(mLongPressShiftKeyTimeout, keyIndex, this); - } else if (key.mManualTemporaryUpperCaseCode != Keyboard.CODE_DUMMY - && mKeyboard.isManualTemporaryUpperCase()) { + } else if (key.hasUppercaseLetter() && mKeyboard.isManualTemporaryUpperCase()) { // We need not start long press timer on the key which has manual temporary upper case // code defined and the keyboard is in manual temporary upper case mode. return; @@ -665,10 +668,9 @@ public class PointerTracker { mKeyDetector.getKeyIndexAndNearbyCodes(x, y, codes); // If keyboard is in manual temporary upper case state and key has manual temporary - // shift code, alternate character code should be sent. - if (mKeyboard.isManualTemporaryUpperCase() - && key.mManualTemporaryUpperCaseCode != Keyboard.CODE_DUMMY) { - code = key.mManualTemporaryUpperCaseCode; + // uppercase letter as key hint letter, alternate character code should be sent. + if (mKeyboard.isManualTemporaryUpperCase() && key.hasUppercaseLetter()) { + code = key.mHintLetter.charAt(0); codes[0] = code; } diff --git a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java index 561dcbcef..3b8c36487 100644 --- a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java @@ -16,8 +16,6 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.latin.R; - import android.content.Context; import android.content.res.Resources; import android.os.SystemClock; @@ -27,6 +25,8 @@ import android.view.MotionEvent; import android.view.View; import android.widget.PopupWindow; +import com.android.inputmethod.latin.R; + /** * A view that renders a virtual {@link MiniKeyboard}. It handles rendering of keys and detecting * key presses and touch movements. @@ -37,11 +37,10 @@ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel { private int mOriginX; private int mOriginY; - private int mTrackerId; private long mDownTime; public PopupMiniKeyboardView(Context context, AttributeSet attrs) { - this(context, attrs, R.attr.keyboardViewStyle); + this(context, attrs, R.attr.popupMiniKeyboardViewStyle); } public PopupMiniKeyboardView(Context context, AttributeSet attrs, int defStyle) { @@ -67,7 +66,7 @@ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel { @Override public void showPanel(KeyboardView parentKeyboardView, Key parentKey, - PointerTracker tracker, int keyPreviewY, PopupWindow window) { + PointerTracker tracker, PopupWindow window) { final View container = (View)getParent(); final MiniKeyboard miniKeyboard = (MiniKeyboard)getKeyboard(); final Keyboard parentKeyboard = parentKeyboardView.getKeyboard(); @@ -76,15 +75,14 @@ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel { final int pointX = (mConfigShowMiniKeyboardAtTouchedPoint) ? tracker.getLastX() : parentKey.mX + parentKey.mWidth / 2; final int pointY = parentKey.mY; - final int miniKeyboardX = pointX - miniKeyboard.getDefaultCoordX() - - container.getPaddingLeft() - + parentKeyboardView.getPaddingLeft() + mCoordinates[0]; - final int miniKeyboardY = pointY - parentKeyboard.getVerticalGap() + final int miniKeyboardLeft = pointX - miniKeyboard.getDefaultCoordX() + + parentKeyboardView.getPaddingLeft(); + final int x = Math.max(0, Math.min(miniKeyboardLeft, + parentKeyboardView.getWidth() - miniKeyboard.getMinWidth())) + - container.getPaddingLeft() + mCoordinates[0]; + final int y = pointY - parentKeyboard.getVerticalGap() - (container.getMeasuredHeight() - container.getPaddingBottom()) + parentKeyboardView.getPaddingTop() + mCoordinates[1]; - final int x = miniKeyboardX; - final int y = parentKeyboardView.isKeyPreviewPopupEnabled() && - miniKeyboard.isOneRowKeyboard() ? keyPreviewY : miniKeyboardY; if (miniKeyboard.setShifted(parentKeyboard.isShiftedOrShiftLocked())) { invalidateAllKeys(); @@ -96,30 +94,19 @@ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel { mOriginX = x + container.getPaddingLeft() - mCoordinates[0]; mOriginY = y + container.getPaddingTop() - mCoordinates[1]; - mTrackerId = tracker.mPointerId; mDownTime = SystemClock.uptimeMillis(); // Inject down event on the key to mini keyboard. - final MotionEvent downEvent = translateMotionEvent(MotionEvent.ACTION_DOWN, pointX, - pointY + parentKey.mHeight / 2, mDownTime); + final MotionEvent downEvent = MotionEvent.obtain(mDownTime, mDownTime, + MotionEvent.ACTION_DOWN, pointX - mOriginX, + pointY + parentKey.mHeight / 2 - mOriginY, 0); onTouchEvent(downEvent); downEvent.recycle(); } - private MotionEvent translateMotionEvent(int action, float x, float y, long eventTime) { - return MotionEvent.obtain(mDownTime, eventTime, action, x - mOriginX, y - mOriginY, 0); - } - @Override public boolean onTouchEvent(MotionEvent me) { - final int index = me.getActionIndex(); - final int id = me.getPointerId(index); - if (id == mTrackerId) { - final MotionEvent translated = translateMotionEvent(me.getAction(), me.getX(index), - me.getY(index), me.getEventTime()); - super.onTouchEvent(translated); - translated.recycle(); - } - return true; + me.offsetLocation(-mOriginX, -mOriginY); + return super.onTouchEvent(me); } } diff --git a/java/src/com/android/inputmethod/keyboard/PopupPanel.java b/java/src/com/android/inputmethod/keyboard/PopupPanel.java index 6f2b16148..386e11f2c 100644 --- a/java/src/com/android/inputmethod/keyboard/PopupPanel.java +++ b/java/src/com/android/inputmethod/keyboard/PopupPanel.java @@ -25,11 +25,10 @@ public interface PopupPanel { * @param parentKeyboardView the parent KeyboardView that has the parent key. * @param parentKey the parent key that is the source of this popup panel * @param tracker the pointer tracker that pressesd the parent key - * @param keyPreviewY the Y-coordinate of key preview * @param window PopupWindow to be used to show this popup panel */ public void showPanel(KeyboardView parentKeyboardView, Key parentKey, - PointerTracker tracker, int keyPreviewY, PopupWindow window); + PointerTracker tracker, PopupWindow window); /** * Check if the pointer is in siding key input mode. diff --git a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java index 80d6de952..33acc6907 100644 --- a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java +++ b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 Google Inc. + * Copyright (C) 2011 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 diff --git a/java/src/com/android/inputmethod/keyboard/KeyStyles.java b/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java index d464c2029..983f0649d 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyStyles.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -14,16 +14,15 @@ * the License. */ -package com.android.inputmethod.keyboard; - -import com.android.inputmethod.keyboard.KeyboardParser.ParseException; -import com.android.inputmethod.latin.R; +package com.android.inputmethod.keyboard.internal; import android.content.res.TypedArray; import android.content.res.XmlResourceParser; -import android.graphics.drawable.Drawable; import android.util.Log; +import com.android.inputmethod.keyboard.internal.KeyboardParser.ParseException; +import com.android.inputmethod.latin.R; + import java.util.ArrayList; import java.util.HashMap; @@ -37,7 +36,6 @@ public class KeyStyles { public interface KeyStyle { public CharSequence[] getTextArray(TypedArray a, int index); - public Drawable getDrawable(TypedArray a, int index); public CharSequence getText(TypedArray a, int index); public int getInt(TypedArray a, int index, int defaultValue); public int getFlag(TypedArray a, int index, int defaultValue); @@ -55,11 +53,6 @@ public class KeyStyles { } @Override - public Drawable getDrawable(TypedArray a, int index) { - return a.getDrawable(index); - } - - @Override public CharSequence getText(TypedArray a, int index) { return a.getText(index); } @@ -140,12 +133,6 @@ public class KeyStyles { } @Override - public Drawable getDrawable(TypedArray a, int index) { - return a.hasValue(index) - ? super.getDrawable(a, index) : (Drawable)mAttributes.get(index); - } - - @Override public CharSequence getText(TypedArray a, int index) { return a.hasValue(index) ? super.getText(a, index) : (CharSequence)mAttributes.get(index); @@ -177,25 +164,20 @@ public class KeyStyles { // TODO: Currently not all Key attributes can be declared as style. readInt(keyAttr, R.styleable.Keyboard_Key_code); readText(keyAttr, R.styleable.Keyboard_Key_keyLabel); - readFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelOption); + readText(keyAttr, R.styleable.Keyboard_Key_keyOutputText); + readText(keyAttr, R.styleable.Keyboard_Key_keyHintLetter); readTextArray(keyAttr, R.styleable.Keyboard_Key_popupCharacters); + readFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelOption); + readInt(keyAttr, R.styleable.Keyboard_Key_keyIcon); + readInt(keyAttr, R.styleable.Keyboard_Key_keyIconPreview); + readInt(keyAttr, R.styleable.Keyboard_Key_keyIconShifted); readInt(keyAttr, R.styleable.Keyboard_Key_maxPopupKeyboardColumn); - readText(keyAttr, R.styleable.Keyboard_Key_keyOutputText); - readDrawable(keyAttr, R.styleable.Keyboard_Key_keyIcon); - readDrawable(keyAttr, R.styleable.Keyboard_Key_iconPreview); - readDrawable(keyAttr, R.styleable.Keyboard_Key_keyHintIcon); - readDrawable(keyAttr, R.styleable.Keyboard_Key_shiftedIcon); readBoolean(keyAttr, R.styleable.Keyboard_Key_isFunctional); readBoolean(keyAttr, R.styleable.Keyboard_Key_isSticky); readBoolean(keyAttr, R.styleable.Keyboard_Key_isRepeatable); readBoolean(keyAttr, R.styleable.Keyboard_Key_enabled); } - private void readDrawable(TypedArray a, int index) { - if (a.hasValue(index)) - mAttributes.put(index, a.getDrawable(index)); - } - private void readText(TypedArray a, int index) { if (a.hasValue(index)) mAttributes.put(index, a.getText(index)); diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java new file mode 100644 index 000000000..37b36825a --- /dev/null +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2011 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.keyboard.internal; + +import android.content.res.Resources; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.util.Log; + +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.latin.R; + +public class KeyboardIconsSet { + private static final String TAG = KeyboardIconsSet.class.getSimpleName(); + + public static final int ICON_UNDEFINED = 0; + + // This should be aligned with Keyboard.keyIcon enum. + private static final int ICON_SHIFT_KEY = 1; + private static final int ICON_TO_SYMBOL_KEY = 2; + private static final int ICON_TO_SYMBOL_KEY_WITH_SHORTCUT = 3; + private static final int ICON_DELETE_KEY = 4; + private static final int ICON_SETTINGS_KEY = 5; + private static final int ICON_SHORTCUT_KEY = 6; + private static final int ICON_SPACE_KEY = 7; + private static final int ICON_RETURN_KEY = 8; + private static final int ICON_SEARCH_KEY = 9; + private static final int ICON_TAB_KEY = 10; + private static final int ICON_NUM1_KEY = 11; + private static final int ICON_NUM2_KEY = 12; + private static final int ICON_NUM3_KEY = 13; + private static final int ICON_NUM4_KEY = 14; + private static final int ICON_NUM5_KEY = 15; + private static final int ICON_NUM6_KEY = 16; + private static final int ICON_NUM7_KEY = 17; + private static final int ICON_NUM8_KEY = 18; + private static final int ICON_NUM9_KEY = 19; + private static final int ICON_NUM0_KEY = 20; + // This should be aligned with Keyboard.keyIconShifted enum. + private static final int ICON_SHIFTED_SHIFT_KEY = 21; + // This should be aligned with Keyboard.keyIconPreview enum. + private static final int ICON_PREVIEW_SPACE_KEY = 22; + private static final int ICON_PREVIEW_TAB_KEY = 23; + private static final int ICON_PREVIEW_SETTINGS_KEY = 24; + private static final int ICON_PREVIEW_SHORTCUT_KEY = 25; + + private static final int ICON_LAST = 25; + + private final Drawable mIcons[] = new Drawable[ICON_LAST + 1]; + + private static final int getIconId(int attrIndex) { + switch (attrIndex) { + case R.styleable.Keyboard_iconShiftKey: + return ICON_SHIFT_KEY; + case R.styleable.Keyboard_iconToSymbolKey: + return ICON_TO_SYMBOL_KEY; + case R.styleable.Keyboard_iconToSymbolKeyWithShortcut: + return ICON_TO_SYMBOL_KEY_WITH_SHORTCUT; + case R.styleable.Keyboard_iconDeleteKey: + return ICON_DELETE_KEY; + case R.styleable.Keyboard_iconSettingsKey: + return ICON_SETTINGS_KEY; + case R.styleable.Keyboard_iconShortcutKey: + return ICON_SHORTCUT_KEY; + case R.styleable.Keyboard_iconSpaceKey: + return ICON_SPACE_KEY; + case R.styleable.Keyboard_iconReturnKey: + return ICON_RETURN_KEY; + case R.styleable.Keyboard_iconSearchKey: + return ICON_SEARCH_KEY; + case R.styleable.Keyboard_iconTabKey: + return ICON_TAB_KEY; + case R.styleable.Keyboard_iconNum1Key: + return ICON_NUM1_KEY; + case R.styleable.Keyboard_iconNum2Key: + return ICON_NUM2_KEY; + case R.styleable.Keyboard_iconNum3Key: + return ICON_NUM3_KEY; + case R.styleable.Keyboard_iconNum4Key: + return ICON_NUM4_KEY; + case R.styleable.Keyboard_iconNum5Key: + return ICON_NUM5_KEY; + case R.styleable.Keyboard_iconNum6Key: + return ICON_NUM6_KEY; + case R.styleable.Keyboard_iconNum7Key: + return ICON_NUM7_KEY; + case R.styleable.Keyboard_iconNum8Key: + return ICON_NUM8_KEY; + case R.styleable.Keyboard_iconNum9Key: + return ICON_NUM9_KEY; + case R.styleable.Keyboard_iconNum0Key: + return ICON_NUM0_KEY; + case R.styleable.Keyboard_iconShiftedShiftKey: + return ICON_SHIFTED_SHIFT_KEY; + case R.styleable.Keyboard_iconPreviewSpaceKey: + return ICON_PREVIEW_SPACE_KEY; + case R.styleable.Keyboard_iconPreviewTabKey: + return ICON_PREVIEW_TAB_KEY; + case R.styleable.Keyboard_iconPreviewSettingsKey: + return ICON_PREVIEW_SETTINGS_KEY; + case R.styleable.Keyboard_iconPreviewShortcutKey: + return ICON_PREVIEW_SHORTCUT_KEY; + default: + return ICON_UNDEFINED; + } + } + + public void loadIcons(TypedArray keyboardAttrs) { + final int count = keyboardAttrs.getIndexCount(); + for (int i = 0; i < count; i++) { + final int attrIndex = keyboardAttrs.getIndex(i); + final int iconId = getIconId(attrIndex); + if (iconId != ICON_UNDEFINED) { + try { + final Drawable icon = keyboardAttrs.getDrawable(attrIndex); + Keyboard.setDefaultBounds(icon); + mIcons[iconId] = icon; + } catch (Resources.NotFoundException e) { + Log.w(TAG, "Drawable resource for icon #" + iconId + " not found"); + } + } + } + } + + public Drawable getIcon(int iconId) { + if (iconId == ICON_UNDEFINED) + return null; + if (iconId < 0 || iconId >= mIcons.length) + throw new IllegalArgumentException("icon id is out of range: " + iconId); + return mIcons[iconId]; + } +} diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardParser.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardParser.java index 31fd0bfa3..a6708171f 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardParser.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardParser.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -14,13 +14,7 @@ * the License. */ -package com.android.inputmethod.keyboard; - -import com.android.inputmethod.compat.EditorInfoCompatUtils; -import com.android.inputmethod.latin.R; - -import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; +package com.android.inputmethod.keyboard.internal; import android.content.Context; import android.content.res.Resources; @@ -31,6 +25,15 @@ import android.util.TypedValue; import android.util.Xml; import android.view.InflateException; +import com.android.inputmethod.compat.EditorInfoCompatUtils; +import com.android.inputmethod.keyboard.Key; +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.KeyboardId; +import com.android.inputmethod.latin.R; + +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -88,14 +91,14 @@ import java.util.List; * You can declare Key style and specify styles within Key tags. * <pre> * >switch< - * >case colorScheme="white"< - * >key-style styleName="shift-key" parentStyle="modifier-key" - * keyIcon="@drawable/sym_keyboard_shift" + * >case mode="email"< + * >key-style styleName="f1-key" parentStyle="modifier-key" + * keyLabel=".com" * /< * >/case< - * >case colorScheme="black"< - * >key-style styleName="shift-key" parentStyle="modifier-key" - * keyIcon="@drawable/sym_bkeyboard_shift" + * >case mode="url"< + * >key-style styleName="f1-key" parentStyle="modifier-key" + * keyLabel="http://" * /< * >/case< * >/switch< @@ -121,8 +124,11 @@ public class KeyboardParser { public static final String TAG_KEY_STYLE = "key-style"; private final Keyboard mKeyboard; + private final Context mContext; private final Resources mResources; + private int mKeyboardTopPadding; + private int mKeyboardBottomPadding; private int mHorizontalEdgesPadding; private int mCurrentX = 0; private int mCurrentY = 0; @@ -131,8 +137,10 @@ public class KeyboardParser { private Row mCurrentRow = null; private final KeyStyles mKeyStyles = new KeyStyles(); - public KeyboardParser(Keyboard keyboard, Resources res) { + public KeyboardParser(Keyboard keyboard, Context context) { mKeyboard = keyboard; + mContext = context; + final Resources res = context.getResources(); mResources = res; mHorizontalEdgesPadding = (int)res.getDimension(R.dimen.keyboard_horizontal_edges_padding); } @@ -187,8 +195,9 @@ public class KeyboardParser { private void parseKeyboardAttributes(XmlResourceParser parser) { final Keyboard keyboard = mKeyboard; - final TypedArray keyboardAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser), - R.styleable.Keyboard); + final TypedArray keyboardAttr = mContext.obtainStyledAttributes( + Xml.asAttributeSet(parser), R.styleable.Keyboard, R.attr.keyboardStyle, + R.style.Keyboard); final TypedArray keyAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.Keyboard_Key); try { @@ -226,6 +235,12 @@ public class KeyboardParser { keyboard.setMaxPopupKeyboardColumn(keyAttr.getInt( R.styleable.Keyboard_Key_maxPopupKeyboardColumn, 5)); + + mKeyboard.mIconsSet.loadIcons(keyboardAttr); + mKeyboardTopPadding = keyboardAttr.getDimensionPixelSize( + R.styleable.Keyboard_keyboardTopPadding, 0); + mKeyboardBottomPadding = keyboardAttr.getDimensionPixelSize( + R.styleable.Keyboard_keyboardBottomPadding, 0); } finally { keyAttr.recycle(); keyboardAttr.recycle(); @@ -280,7 +295,7 @@ public class KeyboardParser { if (TAG_KEY.equals(tag)) { parseKey(parser, row, keys); } else if (TAG_SPACER.equals(tag)) { - parseSpacer(parser, keys); + parseSpacer(parser, row, keys); } else if (TAG_INCLUDE.equals(tag)) { parseIncludeRowContent(parser, row, keys); } else if (TAG_SWITCH.equals(tag)) { @@ -317,7 +332,7 @@ public class KeyboardParser { } else { Key key = new Key(mResources, row, mCurrentX, mCurrentY, parser, mKeyStyles); if (DEBUG) Log.d(TAG, String.format("<%s%s keyLabel=%s code=%d popupCharacters=%s />", - TAG_KEY, (key.mEnabled ? "" : " disabled"), key.mLabel, key.mCode, + TAG_KEY, (key.isEnabled() ? "" : " disabled"), key.mLabel, key.mCode, Arrays.toString(key.mPopupCharacters))); checkEndTag(TAG_KEY, parser); keys.add(key); @@ -327,19 +342,32 @@ public class KeyboardParser { } } - private void parseSpacer(XmlResourceParser parser, List<Key> keys) + private void parseSpacer(XmlResourceParser parser, Row row, List<Key> keys) throws XmlPullParserException, IOException { if (keys == null) { checkEndTag(TAG_SPACER, parser); } else { if (DEBUG) Log.d(TAG, String.format("<%s />", TAG_SPACER)); - final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser), + final TypedArray keyboardAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.Keyboard); - final int gap = getDimensionOrFraction(a, R.styleable.Keyboard_horizontalGap, - mKeyboard.getDisplayWidth(), 0); - a.recycle(); + if (keyboardAttr.hasValue(R.styleable.Keyboard_horizontalGap)) + throw new IllegalAttribute(parser, "horizontalGap"); + final int defaultWidth = (row != null) ? row.mDefaultWidth : 0; + final int keyWidth = getDimensionOrFraction(keyboardAttr, R.styleable.Keyboard_keyWidth, + mKeyboard.getDisplayWidth(), defaultWidth); + keyboardAttr.recycle(); + + final TypedArray keyAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser), + R.styleable.Keyboard_Key); + int keyXPos = KeyboardParser.getDimensionOrFraction(keyAttr, + R.styleable.Keyboard_Key_keyXPos, mKeyboard.getDisplayWidth(), mCurrentX); + if (keyXPos < 0) { + // If keyXPos is negative, the actual x-coordinate will be display_width + keyXPos. + keyXPos += mKeyboard.getDisplayWidth(); + } + checkEndTag(TAG_SPACER, parser); - setSpacer(gap); + setSpacer(keyXPos, keyWidth); } } @@ -454,44 +482,53 @@ public class KeyboardParser { final TypedArray viewAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.KeyboardView); try { - final boolean modeMatched = matchInteger(a, - R.styleable.Keyboard_Case_mode, id.mMode); + final boolean modeMatched = matchTypedValue(a, + R.styleable.Keyboard_Case_mode, id.mMode, KeyboardId.modeName(id.mMode)); + final boolean navigateActionMatched = matchBoolean(a, + R.styleable.Keyboard_Case_navigateAction, id.mNavigateAction); final boolean passwordInputMatched = matchBoolean(a, R.styleable.Keyboard_Case_passwordInput, id.mPasswordInput); - final boolean settingsKeyMatched = matchBoolean(a, + final boolean hasSettingsKeyMatched = matchBoolean(a, R.styleable.Keyboard_Case_hasSettingsKey, id.mHasSettingsKey); + final boolean f2KeyModeMatched = matchInteger(a, + R.styleable.Keyboard_Case_f2KeyMode, id.mF2KeyMode); + final boolean clobberSettingsKeyMatched = matchBoolean(a, + R.styleable.Keyboard_Case_clobberSettingsKey, id.mClobberSettingsKey); final boolean voiceEnabledMatched = matchBoolean(a, R.styleable.Keyboard_Case_voiceKeyEnabled, id.mVoiceKeyEnabled); final boolean voiceKeyMatched = matchBoolean(a, R.styleable.Keyboard_Case_hasVoiceKey, id.mHasVoiceKey); - final boolean colorSchemeMatched = matchInteger(viewAttr, - R.styleable.KeyboardView_colorScheme, id.mColorScheme); // As noted at {@link KeyboardId} class, we are interested only in enum value masked by // {@link android.view.inputmethod.EditorInfo#IME_MASK_ACTION} and // {@link android.view.inputmethod.EditorInfo#IME_FLAG_NO_ENTER_ACTION}. So matching // this attribute with id.mImeOptions as integer value is enough for our purpose. final boolean imeActionMatched = matchInteger(a, R.styleable.Keyboard_Case_imeAction, id.mImeAction); + final boolean localeCodeMatched = matchString(a, + R.styleable.Keyboard_Case_localeCode, id.mLocale.toString()); final boolean languageCodeMatched = matchString(a, R.styleable.Keyboard_Case_languageCode, id.mLocale.getLanguage()); final boolean countryCodeMatched = matchString(a, R.styleable.Keyboard_Case_countryCode, id.mLocale.getCountry()); - final boolean selected = modeMatched && passwordInputMatched && settingsKeyMatched - && voiceEnabledMatched && voiceKeyMatched && colorSchemeMatched - && imeActionMatched && languageCodeMatched && countryCodeMatched; - - if (DEBUG) Log.d(TAG, String.format("<%s%s%s%s%s%s%s%s%s%s> %s", TAG_CASE, - textAttr(KeyboardId.modeName( - a.getInt(R.styleable.Keyboard_Case_mode, -1)), "mode"), - textAttr(KeyboardId.colorSchemeName( - viewAttr.getInt( - R.styleable.KeyboardView_colorScheme, -1)), "colorSchemeName"), + final boolean selected = modeMatched && navigateActionMatched && passwordInputMatched + && hasSettingsKeyMatched && f2KeyModeMatched && clobberSettingsKeyMatched + && voiceEnabledMatched && voiceKeyMatched && imeActionMatched && + localeCodeMatched && languageCodeMatched && countryCodeMatched; + + if (DEBUG) Log.d(TAG, String.format("<%s%s%s%s%s%s%s%s%s%s%s%s%s> %s", TAG_CASE, + textAttr(a.getString(R.styleable.Keyboard_Case_mode), "mode"), + booleanAttr(a, R.styleable.Keyboard_Case_navigateAction, "navigateAction"), booleanAttr(a, R.styleable.Keyboard_Case_passwordInput, "passwordInput"), booleanAttr(a, R.styleable.Keyboard_Case_hasSettingsKey, "hasSettingsKey"), + textAttr(KeyboardId.f2KeyModeName( + a.getInt(R.styleable.Keyboard_Case_f2KeyMode, -1)), "f2KeyMode"), + booleanAttr(a, R.styleable.Keyboard_Case_clobberSettingsKey, + "clobberSettingsKey"), booleanAttr(a, R.styleable.Keyboard_Case_voiceKeyEnabled, "voiceKeyEnabled"), booleanAttr(a, R.styleable.Keyboard_Case_hasVoiceKey, "hasVoiceKey"), textAttr(EditorInfoCompatUtils.imeOptionsName( a.getInt(R.styleable.Keyboard_Case_imeAction, -1)), "imeAction"), + textAttr(a.getString(R.styleable.Keyboard_Case_localeCode), "localeCode"), textAttr(a.getString(R.styleable.Keyboard_Case_languageCode), "languageCode"), textAttr(a.getString(R.styleable.Keyboard_Case_countryCode), "countryCode"), Boolean.toString(selected))); @@ -518,7 +555,30 @@ public class KeyboardParser { private static boolean matchString(TypedArray a, int index, String value) { // If <case> does not have "index" attribute, that means this <case> is wild-card for the // attribute. - return !a.hasValue(index) || a.getString(index).equals(value); + return !a.hasValue(index) || stringArrayContains(a.getString(index).split("\\|"), value); + } + + private static boolean matchTypedValue(TypedArray a, int index, int intValue, String strValue) { + // If <case> does not have "index" attribute, that means this <case> is wild-card for the + // attribute. + final TypedValue v = a.peekValue(index); + if (v == null) + return true; + + if (isIntegerValue(v)) { + return intValue == a.getInt(index, 0); + } else if (isStringValue(v)) { + return stringArrayContains(a.getString(index).split("\\|"), strValue); + } + return false; + } + + private static boolean stringArrayContains(String[] array, String value) { + for (final String elem : array) { + if (elem.equals(value)) + return true; + } + return false; } private boolean parseDefault(XmlResourceParser parser, Row row, List<Key> keys) @@ -557,19 +617,19 @@ public class KeyboardParser { } private void startKeyboard() { - mCurrentY += (int)mResources.getDimension(R.dimen.keyboard_top_padding); + mCurrentY += mKeyboardTopPadding; } private void startRow(Row row) { mCurrentX = 0; - setSpacer(mHorizontalEdgesPadding); + setSpacer(mCurrentX, mHorizontalEdgesPadding); mCurrentRow = row; } private void endRow() { if (mCurrentRow == null) throw new InflateException("orphant end row tag"); - setSpacer(mHorizontalEdgesPadding); + setSpacer(mCurrentX, mHorizontalEdgesPadding); if (mCurrentX > mMaxRowWidth) mMaxRowWidth = mCurrentX; mCurrentY += mCurrentRow.mDefaultHeight; @@ -577,31 +637,50 @@ public class KeyboardParser { } private void endKey(Key key) { - mCurrentX += key.mGap + key.mWidth; + mCurrentX = key.mX - key.mGap / 2 + key.mWidth + key.mGap; } private void endKeyboard(int defaultVerticalGap) { - mCurrentY += (int)mResources.getDimension(R.dimen.keyboard_bottom_padding); + mCurrentY += mKeyboardBottomPadding; mTotalHeight = mCurrentY - defaultVerticalGap; } - private void setSpacer(int gap) { - mCurrentX += gap; + private void setSpacer(int keyXPos, int width) { + mCurrentX = keyXPos + width; } public static int getDimensionOrFraction(TypedArray a, int index, int base, int defValue) { final TypedValue value = a.peekValue(index); if (value == null) return defValue; - if (value.type == TypedValue.TYPE_DIMENSION) { - return a.getDimensionPixelOffset(index, defValue); - } else if (value.type == TypedValue.TYPE_FRACTION) { + if (isFractionValue(value)) { // Round it to avoid values like 47.9999 from getting truncated return Math.round(a.getFraction(index, base, base, defValue)); + } else if (isDimensionValue(value)) { + return a.getDimensionPixelOffset(index, defValue); + } else if (isIntegerValue(value)) { + // For enum value. + return a.getInt(index, defValue); } return defValue; } + private static boolean isFractionValue(TypedValue v) { + return v.type == TypedValue.TYPE_FRACTION; + } + + private static boolean isDimensionValue(TypedValue v) { + return v.type == TypedValue.TYPE_DIMENSION; + } + + private static boolean isIntegerValue(TypedValue v) { + return v.type >= TypedValue.TYPE_FIRST_INT && v.type <= TypedValue.TYPE_LAST_INT; + } + + private static boolean isStringValue(TypedValue v) { + return v.type == TypedValue.TYPE_STRING; + } + @SuppressWarnings("serial") public static class ParseException extends InflateException { public ParseException(String msg, XmlResourceParser parser) { @@ -624,6 +703,13 @@ public class KeyboardParser { } @SuppressWarnings("serial") + private static class IllegalAttribute extends ParseException { + public IllegalAttribute(XmlResourceParser parser, String attribute) { + super("Tag " + parser.getName() + " has illegal attribute " + attribute, parser); + } + } + + @SuppressWarnings("serial") private static class NonEmptyTag extends ParseException { public NonEmptyTag(String tag, XmlResourceParser parser) { super(tag + " must be empty tag", parser); diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardShiftState.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardShiftState.java index d5412791d..0cde4e5b5 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardShiftState.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardShiftState.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -14,10 +14,12 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; import android.util.Log; +import com.android.inputmethod.keyboard.KeyboardSwitcher; + public class KeyboardShiftState { private static final String TAG = "KeyboardShiftState"; private static final boolean DEBUG = KeyboardSwitcher.DEBUG_STATE; diff --git a/java/src/com/android/inputmethod/keyboard/MiniKeyboardBuilder.java b/java/src/com/android/inputmethod/keyboard/internal/MiniKeyboardBuilder.java index 2b83c3ff5..040c16ded 100644 --- a/java/src/com/android/inputmethod/keyboard/MiniKeyboardBuilder.java +++ b/java/src/com/android/inputmethod/keyboard/internal/MiniKeyboardBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -14,15 +14,19 @@ * the License. */ -package com.android.inputmethod.keyboard; - -import com.android.inputmethod.latin.R; +package com.android.inputmethod.keyboard.internal; import android.content.Context; import android.content.res.Resources; import android.graphics.Paint; import android.graphics.Rect; +import com.android.inputmethod.keyboard.Key; +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.KeyboardView; +import com.android.inputmethod.keyboard.MiniKeyboard; +import com.android.inputmethod.latin.R; + import java.util.List; public class MiniKeyboardBuilder { @@ -185,7 +189,8 @@ public class MiniKeyboardBuilder { Keyboard parentKeyboard) { final Context context = view.getContext(); mRes = context.getResources(); - final MiniKeyboard keyboard = new MiniKeyboard(context, layoutTemplateResId, null); + final MiniKeyboard keyboard = new MiniKeyboard( + context, layoutTemplateResId, parentKeyboard); mKeyboard = keyboard; mPopupCharacters = parentKey.mPopupCharacters; diff --git a/java/src/com/android/inputmethod/keyboard/ModifierKeyState.java b/java/src/com/android/inputmethod/keyboard/internal/ModifierKeyState.java index f215db876..dae73c4e4 100644 --- a/java/src/com/android/inputmethod/keyboard/ModifierKeyState.java +++ b/java/src/com/android/inputmethod/keyboard/internal/ModifierKeyState.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -14,10 +14,12 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; import android.util.Log; +import com.android.inputmethod.keyboard.KeyboardSwitcher; + public class ModifierKeyState { protected static final String TAG = "ModifierKeyState"; protected static final boolean DEBUG = KeyboardSwitcher.DEBUG_STATE; diff --git a/java/src/com/android/inputmethod/keyboard/PointerTrackerKeyState.java b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerKeyState.java index b3ed1e26f..ddadb1338 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTrackerKeyState.java +++ b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerKeyState.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -14,12 +14,15 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; + +import com.android.inputmethod.keyboard.KeyDetector; +import com.android.inputmethod.keyboard.PointerTracker; /** * This class keeps track of a key index and a position where {@link PointerTracker} is. */ -/* package */ class PointerTrackerKeyState { +public class PointerTrackerKeyState { private final KeyDetector mKeyDetector; // The position and time at which first down event occurred. diff --git a/java/src/com/android/inputmethod/keyboard/PointerTrackerQueue.java b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java index 68de8df8a..f87cd869e 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTrackerQueue.java +++ b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -14,7 +14,9 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; + +import com.android.inputmethod.keyboard.PointerTracker; import java.util.LinkedList; diff --git a/java/src/com/android/inputmethod/keyboard/PopupCharactersParser.java b/java/src/com/android/inputmethod/keyboard/internal/PopupCharactersParser.java index 32c25801d..8276f5d78 100644 --- a/java/src/com/android/inputmethod/keyboard/PopupCharactersParser.java +++ b/java/src/com/android/inputmethod/keyboard/internal/PopupCharactersParser.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -14,13 +14,14 @@ * the License. */ -package com.android.inputmethod.keyboard; - -import com.android.inputmethod.latin.R; +package com.android.inputmethod.keyboard.internal; import android.content.res.Resources; -import android.graphics.drawable.Drawable; import android.text.TextUtils; +import android.util.Log; + +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.latin.R; /** * String parser of popupCharacters attribute of Key. @@ -28,16 +29,19 @@ import android.text.TextUtils; * Each popup key text is one of the following: * - A single letter (Letter) * - Label optionally followed by keyOutputText or code (keyLabel|keyOutputText). - * - Icon followed by keyOutputText or code (@drawable/icon|@integer/key_code) + * - Icon followed by keyOutputText or code (@icon/icon_number|@integer/key_code) * Special character, comma ',' backslash '\', and bar '|' can be escaped by '\' * character. * Note that the character '@' and '\' are also parsed by XML parser and CSV parser as well. + * See {@link KeyboardIconsSet} about icon_number. */ public class PopupCharactersParser { + private static final String TAG = PopupCharactersParser.class.getSimpleName(); + private static final char ESCAPE = '\\'; private static final String LABEL_END = "|"; private static final String PREFIX_AT = "@"; - private static final String PREFIX_ICON = PREFIX_AT + "drawable/"; + private static final String PREFIX_ICON = PREFIX_AT + "icon/"; private static final String PREFIX_CODE = PREFIX_AT + "integer/"; private PopupCharactersParser() { @@ -150,13 +154,18 @@ public class PopupCharactersParser { return Keyboard.CODE_DUMMY; } - public static Drawable getIcon(Resources res, String popupSpec) { + public static int getIconId(String popupSpec) { if (hasIcon(popupSpec)) { int end = popupSpec.indexOf(LABEL_END, PREFIX_ICON.length() + 1); - int resId = getResourceId(res, popupSpec.substring(PREFIX_AT.length(), end)); - return res.getDrawable(resId); + final String iconId = popupSpec.substring(PREFIX_ICON.length(), end); + try { + return Integer.valueOf(iconId); + } catch (NumberFormatException e) { + Log.w(TAG, "illegal icon id specified: " + iconId); + return KeyboardIconsSet.ICON_UNDEFINED; + } } - return null; + return KeyboardIconsSet.ICON_UNDEFINED; } private static int getResourceId(Resources res, String name) { diff --git a/java/src/com/android/inputmethod/keyboard/Row.java b/java/src/com/android/inputmethod/keyboard/internal/Row.java index 3618c0448..06aadcc05 100644 --- a/java/src/com/android/inputmethod/keyboard/Row.java +++ b/java/src/com/android/inputmethod/keyboard/internal/Row.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -14,15 +14,16 @@ * the License. */ -package com.android.inputmethod.keyboard; - -import com.android.inputmethod.latin.R; +package com.android.inputmethod.keyboard.internal; import android.content.res.Resources; import android.content.res.TypedArray; import android.content.res.XmlResourceParser; import android.util.Xml; +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.latin.R; + /** * Container for keys in the keyboard. All keys in a row are at the same Y-coordinate. * Some of the key size defaults can be overridden per row from what the {@link Keyboard} diff --git a/java/src/com/android/inputmethod/keyboard/ShiftKeyState.java b/java/src/com/android/inputmethod/keyboard/internal/ShiftKeyState.java index 9229208a9..6617b917f 100644 --- a/java/src/com/android/inputmethod/keyboard/ShiftKeyState.java +++ b/java/src/com/android/inputmethod/keyboard/internal/ShiftKeyState.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -14,7 +14,7 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; import android.util.Log; diff --git a/java/src/com/android/inputmethod/keyboard/SlidingLocaleDrawable.java b/java/src/com/android/inputmethod/keyboard/internal/SlidingLocaleDrawable.java index b279c1c7e..df4b575f1 100644 --- a/java/src/com/android/inputmethod/keyboard/SlidingLocaleDrawable.java +++ b/java/src/com/android/inputmethod/keyboard/internal/SlidingLocaleDrawable.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -14,41 +14,42 @@ * the License. */ -package com.android.inputmethod.keyboard; - -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.SubtypeSwitcher; +package com.android.inputmethod.keyboard.internal; import android.content.Context; -import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.Paint; -import android.graphics.PixelFormat; import android.graphics.Paint.Align; +import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; import android.text.TextPaint; import android.view.ViewConfiguration; +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.LatinKeyboard; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.SubtypeSwitcher; + /** * Animation to be displayed on the spacebar preview popup when switching languages by swiping the * spacebar. It draws the current, previous and next languages and moves them by the delta of touch * movement on the spacebar. */ public class SlidingLocaleDrawable extends Drawable { - - private final Context mContext; - private final Resources mRes; + private static final int SLIDE_SPEED_MULTIPLIER_RATIO = 150; private final int mWidth; private final int mHeight; private final Drawable mBackground; + private final int mSpacebarTextColor; private final TextPaint mTextPaint; private final int mMiddleX; private final Drawable mLeftDrawable; private final Drawable mRightDrawable; private final int mThreshold; + private int mDiff; private boolean mHitThreshold; private String mCurrentLanguage; @@ -56,41 +57,36 @@ public class SlidingLocaleDrawable extends Drawable { private String mPrevLanguage; public SlidingLocaleDrawable(Context context, Drawable background, int width, int height) { - mContext = context; - mRes = context.getResources(); mBackground = background; Keyboard.setDefaultBounds(mBackground); mWidth = width; mHeight = height; final TextPaint textPaint = new TextPaint(); - textPaint.setTextSize(getTextSizeFromTheme(android.R.style.TextAppearance_Medium, 18)); + textPaint.setTextSize(LatinKeyboard.getTextSizeFromTheme( + context.getTheme(), android.R.style.TextAppearance_Medium, 18)); textPaint.setColor(Color.TRANSPARENT); textPaint.setTextAlign(Align.CENTER); textPaint.setAntiAlias(true); mTextPaint = textPaint; mMiddleX = (mWidth - mBackground.getIntrinsicWidth()) / 2; - final Resources res = mRes; - mLeftDrawable = res.getDrawable( - R.drawable.sym_keyboard_feedback_language_arrows_left); - mRightDrawable = res.getDrawable( - R.drawable.sym_keyboard_feedback_language_arrows_right); - mThreshold = ViewConfiguration.get(mContext).getScaledTouchSlop(); - } - private int getTextSizeFromTheme(int style, int defValue) { - TypedArray array = mContext.getTheme().obtainStyledAttributes( - style, new int[] { android.R.attr.textSize }); - int textSize = array.getDimensionPixelSize(array.getResourceId(0, 0), defValue); - return textSize; + final TypedArray a = context.obtainStyledAttributes( + null, R.styleable.LatinKeyboard, R.attr.latinKeyboardStyle, R.style.LatinKeyboard); + mSpacebarTextColor = a.getColor(R.styleable.LatinKeyboard_spacebarTextColor, 0); + mLeftDrawable = a.getDrawable(R.styleable.LatinKeyboard_spacebarArrowPreviewLeftIcon); + mRightDrawable = a.getDrawable(R.styleable.LatinKeyboard_spacebarArrowPreviewRightIcon); + a.recycle(); + + mThreshold = ViewConfiguration.get(context).getScaledTouchSlop(); } - void setDiff(int diff) { + public void setDiff(int diff) { if (diff == Integer.MAX_VALUE) { mHitThreshold = false; mCurrentLanguage = null; return; } - mDiff = diff; + mDiff = Math.max(diff, diff * SLIDE_SPEED_MULTIPLIER_RATIO / 100); if (mDiff > mWidth) mDiff = mWidth; if (mDiff < -mWidth) mDiff = -mWidth; if (Math.abs(mDiff) > mThreshold) mHitThreshold = true; @@ -118,7 +114,7 @@ public class SlidingLocaleDrawable extends Drawable { // Draw language text with shadow final float baseline = mHeight * LatinKeyboard.SPACEBAR_LANGUAGE_BASELINE - paint.descent(); - paint.setColor(mRes.getColor(R.color.latinkeyboard_feedback_language_text)); + paint.setColor(mSpacebarTextColor); 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); diff --git a/java/src/com/android/inputmethod/keyboard/SwipeTracker.java b/java/src/com/android/inputmethod/keyboard/internal/SwipeTracker.java index 730cdc390..8d192c2f0 100644 --- a/java/src/com/android/inputmethod/keyboard/SwipeTracker.java +++ b/java/src/com/android/inputmethod/keyboard/internal/SwipeTracker.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -14,7 +14,7 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; import android.view.MotionEvent; diff --git a/java/src/com/android/inputmethod/latin/AutoDictionary.java b/java/src/com/android/inputmethod/latin/AutoDictionary.java index 307b81d43..460930f16 100644 --- a/java/src/com/android/inputmethod/latin/AutoDictionary.java +++ b/java/src/com/android/inputmethod/latin/AutoDictionary.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -41,13 +41,8 @@ public class AutoDictionary extends ExpandableDictionary { static final int FREQUENCY_FOR_PICKED = 3; // Weight added to a user typing a new word that doesn't get corrected (or is reverted) static final int FREQUENCY_FOR_TYPED = 1; - // A word that is frequently typed and gets promoted to the user dictionary, uses this - // frequency. - static final int FREQUENCY_FOR_AUTO_ADD = 250; // If the user touches a typed word 2 times or more, it will become valid. private static final int VALIDITY_THRESHOLD = 2 * FREQUENCY_FOR_PICKED; - // If the user touches a typed word 4 times or more, it will be added to the user dict. - private static final int PROMOTION_THRESHOLD = 4 * FREQUENCY_FOR_PICKED; private LatinIME mIme; // Locale for which this auto dictionary is storing words @@ -151,11 +146,6 @@ public class AutoDictionary extends ExpandableDictionary { freq = freq < 0 ? addFrequency : freq + addFrequency; super.addWord(word, freq); - if (freq >= PROMOTION_THRESHOLD) { - mIme.promoteToUserDictionary(word, FREQUENCY_FOR_AUTO_ADD); - freq = 0; - } - synchronized (mPendingWritesLock) { // Write a null frequency if it is to be deleted from the db mPendingWrites.put(word, freq == 0 ? null : new Integer(freq)); diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index d95fb9638..9748d6006 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -196,8 +196,9 @@ public class BinaryDictionary extends Dictionary { Arrays.fill(outputChars, (char) 0); Arrays.fill(scores, 0); + final int proximityInfo = keyboard == null ? 0 : keyboard.getProximityInfo(); return getSuggestionsNative( - mNativeDict, keyboard.getProximityInfo(), + mNativeDict, proximityInfo, codes.getXCoordinates(), codes.getYCoordinates(), mInputCodes, codesSize, mFlags, outputChars, scores); } diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index 562580d41..7ce92920d 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -78,16 +78,20 @@ class BinaryDictionaryGetter { } else { try { // If that was no-go, try to find a publicly exported dictionary. - return BinaryDictionaryFileDumper.getDictSetFromContentProvider(locale, context); + List<AssetFileAddress> listFromContentProvider = + BinaryDictionaryFileDumper.getDictSetFromContentProvider(locale, context); + if (null != listFromContentProvider) { + return listFromContentProvider; + } + // If the list is null, fall through and return the fallback } catch (FileNotFoundException e) { Log.e(TAG, "Unable to create dictionary file from provider for locale " + locale.toString() + ": falling back to internal dictionary"); - return Arrays.asList(loadFallbackResource(context, fallbackResId)); } catch (IOException e) { Log.e(TAG, "Unable to read source data for locale " + locale.toString() + ": falling back to internal dictionary"); - return Arrays.asList(loadFallbackResource(context, fallbackResId)); } + return Arrays.asList(loadFallbackResource(context, fallbackResId)); } } } diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java index abdf30e6b..09fd3b473 100644 --- a/java/src/com/android/inputmethod/latin/CandidateView.java +++ b/java/src/com/android/inputmethod/latin/CandidateView.java @@ -16,10 +16,9 @@ package com.android.inputmethod.latin; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; - import android.content.Context; import android.content.res.Resources; +import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.Typeface; import android.os.Handler; @@ -45,28 +44,53 @@ import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; +import com.android.inputmethod.compat.FrameLayoutCompatUtils; +import com.android.inputmethod.compat.LinearLayoutCompatUtils; +import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; + import java.util.ArrayList; import java.util.List; public class CandidateView extends LinearLayout implements OnClickListener, OnLongClickListener { + public interface Listener { + public boolean addWordToDictionary(String word); + public void pickSuggestionManually(int index, CharSequence word); + } + private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD); private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan(); - private static final int MAX_SUGGESTIONS = 16; + // The maximum number of suggestions available. See {@link Suggest#mPrefMaxSuggestions}. + private static final int MAX_SUGGESTIONS = 18; + private static final int UNSPECIFIED_MEASURESPEC = MeasureSpec.makeMeasureSpec( + 0, MeasureSpec.UNSPECIFIED); private static final boolean DBG = LatinImeLogger.sDBG; - private final ArrayList<View> mWords = new ArrayList<View>(); - private final boolean mConfigCandidateHighlightFontColorEnabled; + private static final int NUM_CANDIDATES_IN_STRIP = 3; + private final ImageView mExpandCandidatesPane; + private final ImageView mCloseCandidatesPane; + private ViewGroup mCandidatesPane; + private ViewGroup mCandidatesPaneContainer; + private View mKeyboardView; + private final ArrayList<TextView> mWords = new ArrayList<TextView>(); + private final ArrayList<TextView> mInfos = new ArrayList<TextView>(); + private final ArrayList<View> mDividers = new ArrayList<View>(); + private final int mCandidatePadding; + private final int mCandidateStripHeight; private final CharacterStyle mInvertedForegroundColorSpan; private final CharacterStyle mInvertedBackgroundColorSpan; - private final int mColorNormal; - private final int mColorRecommended; - private final int mColorOther; + private final int mAutoCorrectHighlight; + private static final int AUTO_CORRECT_BOLD = 0x01; + private static final int AUTO_CORRECT_UNDERLINE = 0x02; + private static final int AUTO_CORRECT_INVERT = 0x04; + private final int mColorTypedWord; + private final int mColorAutoCorrect; + private final int mColorSuggestedCandidate; private final PopupWindow mPreviewPopup; private final TextView mPreviewText; - private LatinIME mService; + private Listener mListener; private SuggestedWords mSuggestions = SuggestedWords.EMPTY; private boolean mShowingAutoCorrectionInverted; private boolean mShowingAddToDictionary; @@ -123,46 +147,113 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo * @param attrs */ public CandidateView(Context context, AttributeSet attrs) { + this(context, attrs, R.attr.candidateViewStyle); + } + + public CandidateView(Context context, AttributeSet attrs, int defStyle) { + // Note: Up to version 10 (Gingerbread) of the API, LinearLayout doesn't have 3-argument + // constructor. + // TODO: Call 3-argument constructor, super(context, attrs, defStyle), when we abandon + // backward compatibility with the version 10 or earlier of the API. super(context, attrs); + if (defStyle != R.attr.candidateViewStyle) { + throw new IllegalArgumentException( + "can't accept defStyle other than R.attr.candidayeViewStyle: defStyle=" + + defStyle); + } + setBackgroundDrawable(LinearLayoutCompatUtils.getBackgroundDrawable( + context, attrs, defStyle, R.style.CandidateViewStyle)); Resources res = context.getResources(); - mPreviewPopup = new PopupWindow(context); LayoutInflater inflater = LayoutInflater.from(context); + inflater.inflate(R.layout.candidates_strip, this); + + mPreviewPopup = new PopupWindow(context); mPreviewText = (TextView) inflater.inflate(R.layout.candidate_preview, null); mPreviewPopup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); mPreviewPopup.setContentView(mPreviewText); mPreviewPopup.setBackgroundDrawable(null); - mConfigCandidateHighlightFontColorEnabled = - res.getBoolean(R.bool.config_candidate_highlight_font_color_enabled); - mColorNormal = res.getColor(R.color.candidate_normal); - mColorRecommended = res.getColor(R.color.candidate_recommended); - mColorOther = res.getColor(R.color.candidate_other); - mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorNormal ^ 0x00ffffff); - mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorNormal); + mCandidatePadding = res.getDimensionPixelOffset(R.dimen.candidate_padding); + mCandidateStripHeight = res.getDimensionPixelOffset(R.dimen.candidate_strip_height); for (int i = 0; i < MAX_SUGGESTIONS; i++) { - View v = inflater.inflate(R.layout.candidate, null); - TextView tv = (TextView)v.findViewById(R.id.candidate_word); - tv.setTag(i); - tv.setOnClickListener(this); + final TextView word, info; + switch (i) { + case 0: + word = (TextView)findViewById(R.id.word_left); + word.setPadding(mCandidatePadding, 0, 0, 0); + info = (TextView)findViewById(R.id.info_left); + break; + case 1: + word = (TextView)findViewById(R.id.word_center); + info = (TextView)findViewById(R.id.info_center); + break; + case 2: + word = (TextView)findViewById(R.id.word_right); + info = (TextView)findViewById(R.id.info_right); + break; + default: + word = (TextView)inflater.inflate(R.layout.candidate_word, null); + info = (TextView)inflater.inflate(R.layout.candidate_info, null); + break; + } + word.setTag(i); + word.setOnClickListener(this); if (i == 0) - tv.setOnLongClickListener(this); - ImageView divider = (ImageView)v.findViewById(R.id.candidate_divider); - // Do not display divider of first candidate. - divider.setVisibility(i == 0 ? INVISIBLE : VISIBLE); - mWords.add(v); + word.setOnLongClickListener(this); + mWords.add(word); + mInfos.add(info); + if (i > 0) { + final View divider = inflater.inflate(R.layout.candidate_divider, null); + divider.measure(UNSPECIFIED_MEASURESPEC, UNSPECIFIED_MEASURESPEC); + mDividers.add(divider); + } } - scrollTo(0, getScrollY()); + final TypedArray a = context.obtainStyledAttributes( + attrs, R.styleable.CandidateView, defStyle, R.style.CandidateViewStyle); + mAutoCorrectHighlight = a.getInt(R.styleable.CandidateView_autoCorrectHighlight, 0); + mColorTypedWord = a.getColor(R.styleable.CandidateView_colorTypedWord, 0); + mColorAutoCorrect = a.getColor(R.styleable.CandidateView_colorAutoCorrect, 0); + mColorSuggestedCandidate = a.getColor(R.styleable.CandidateView_colorSuggested, 0); + mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff); + mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord); + + mExpandCandidatesPane = (ImageView)findViewById(R.id.expand_candidates_pane); + mExpandCandidatesPane.setImageDrawable( + a.getDrawable(R.styleable.CandidateView_iconExpandPane)); + mExpandCandidatesPane.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View view) { + expandCandidatesPane(); + } + }); + mCloseCandidatesPane = (ImageView)findViewById(R.id.close_candidates_pane); + mCloseCandidatesPane.setImageDrawable( + a.getDrawable(R.styleable.CandidateView_iconClosePane)); + mCloseCandidatesPane.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View view) { + closeCandidatesPane(); + } + }); + + a.recycle(); } /** - * A connection back to the service to communicate with the text field + * A connection back to the input method. * @param listener */ - public void setService(LatinIME listener) { - mService = listener; + public void setListener(Listener listener, View inputView) { + mListener = listener; + mKeyboardView = inputView.findViewById(R.id.keyboard_view); + mCandidatesPane = FrameLayoutCompatUtils.getPlacer( + (ViewGroup)inputView.findViewById(R.id.candidates_pane)); + mCandidatesPane.setOnClickListener(this); + mCandidatesPaneContainer = (ViewGroup)inputView.findViewById( + R.id.candidates_pane_container); } public void setSuggestions(SuggestedWords suggestions) { @@ -176,80 +267,170 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo } } + private CharSequence getStyledCandidateWord(CharSequence word, boolean isAutoCorrect) { + if (!isAutoCorrect) + return word; + final Spannable spannedWord = new SpannableString(word); + if ((mAutoCorrectHighlight & AUTO_CORRECT_BOLD) != 0) + spannedWord.setSpan(BOLD_SPAN, 0, word.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); + if ((mAutoCorrectHighlight & AUTO_CORRECT_UNDERLINE) != 0) + spannedWord.setSpan(UNDERLINE_SPAN, 0, word.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); + return spannedWord; + } + + private int getCandidateTextColor(boolean isAutoCorrect, boolean isSuggestedCandidate, + SuggestedWordInfo info) { + final int color; + if (isAutoCorrect) { + color = mColorAutoCorrect; + } else if (isSuggestedCandidate) { + color = mColorSuggestedCandidate; + } else { + color = mColorTypedWord; + } + if (info != null && info.isPreviousSuggestedWord()) { + final int newAlpha = (int)(Color.alpha(color) * 0.5f); + return Color.argb(newAlpha, Color.red(color), Color.green(color), Color.blue(color)); + } else { + return color; + } + } + private void updateSuggestions() { final SuggestedWords suggestions = mSuggestions; + final List<SuggestedWordInfo> suggestedWordInfoList = suggestions.mSuggestedWordInfoList; + clear(); + final int paneWidth = getWidth(); + final int dividerWidth = mDividers.get(0).getMeasuredWidth(); + final int dividerHeight = mDividers.get(0).getMeasuredHeight(); + int x = 0; + int y = 0; + int fromIndex = NUM_CANDIDATES_IN_STRIP; final int count = Math.min(mWords.size(), suggestions.size()); + closeCandidatesPane(); + mExpandCandidatesPane.setEnabled(count >= NUM_CANDIDATES_IN_STRIP); for (int i = 0; i < count; i++) { - CharSequence word = suggestions.getWord(i); - if (word == null) continue; - final int wordLength = word.length(); - final List<SuggestedWordInfo> suggestedWordInfoList = - suggestions.mSuggestedWordInfoList; - - final View v = mWords.get(i); - final TextView tv = (TextView)v.findViewById(R.id.candidate_word); - final TextView dv = (TextView)v.findViewById(R.id.candidate_debug_info); - tv.setTextColor(mColorNormal); - // TODO: Needs safety net? - if (suggestions.mHasMinimalSuggestion + final CharSequence suggestion = suggestions.getWord(i); + if (suggestion == null) continue; + + final SuggestedWordInfo suggestionInfo = (suggestedWordInfoList != null) + ? suggestedWordInfoList.get(i) : null; + final boolean isAutoCorrect = suggestions.mHasMinimalSuggestion && ((i == 1 && !suggestions.mTypedWordValid) - || (i == 0 && suggestions.mTypedWordValid))) { - final CharacterStyle style; - if (mConfigCandidateHighlightFontColorEnabled) { - style = BOLD_SPAN; - tv.setTextColor(mColorRecommended); - } else { - style = UNDERLINE_SPAN; - } - final Spannable spannedWord = new SpannableString(word); - spannedWord.setSpan(style, 0, wordLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); - word = spannedWord; - } else if (i != 0 || (wordLength == 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. - if (mConfigCandidateHighlightFontColorEnabled) - tv.setTextColor(mColorOther); + || (i == 0 && suggestions.mTypedWordValid)); + // 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. + // TODO: Need to revisit this logic with bigram suggestions + final boolean isSuggestedCandidate = (i != 0); + final boolean isPunctuationSuggestions = (suggestion.length() == 1 && count > 1); + + final TextView word = mWords.get(i); + // TODO: Reorder candidates in strip as appropriate. The center candidate should hold + // the word when space is typed (valid typed word or auto corrected word). + word.setTextColor(getCandidateTextColor(isAutoCorrect, + isSuggestedCandidate || isPunctuationSuggestions, suggestionInfo)); + word.setText(getStyledCandidateWord(suggestion, isAutoCorrect)); + // TODO: call TextView.setTextScaleX() to fit the candidate in single line. + word.measure(UNSPECIFIED_MEASURESPEC, UNSPECIFIED_MEASURESPEC); + final int width = word.getMeasuredWidth(); + final int height = word.getMeasuredHeight(); + + final TextView info; + if (DBG && suggestionInfo != null + && !TextUtils.isEmpty(suggestionInfo.getDebugString())) { + info = mInfos.get(i); + info.setText(suggestionInfo.getDebugString()); + info.setVisibility(View.VISIBLE); + info.measure(UNSPECIFIED_MEASURESPEC, UNSPECIFIED_MEASURESPEC); + } else { + info = null; } - tv.setText(word); - tv.setClickable(true); - - if (suggestedWordInfoList != null && suggestedWordInfoList.get(i) != null) { - final SuggestedWordInfo info = suggestedWordInfoList.get(i); - if (info.isPreviousSuggestedWord()) { - int color = tv.getCurrentTextColor(); - tv.setTextColor(Color.argb((int)(Color.alpha(color) * 0.5f), Color.red(color), - Color.green(color), Color.blue(color))); - } - final String debugString = info.getDebugString(); - if (DBG) { - if (TextUtils.isEmpty(debugString)) { - dv.setVisibility(GONE); - } else { - dv.setText(debugString); - dv.setVisibility(VISIBLE); - } - } else { - dv.setVisibility(GONE); + + if (i < NUM_CANDIDATES_IN_STRIP) { + if (info != null) { + final int infoWidth = info.getMeasuredWidth(); + FrameLayoutCompatUtils.placeViewAt( + info, x + width - infoWidth, y, infoWidth, info.getMeasuredHeight()); } } else { - dv.setVisibility(GONE); + // TODO: Handle overflow case. + if (dividerWidth + x + width >= paneWidth) { + centeringCandidates(fromIndex, i - 1, x, paneWidth); + x = 0; + y += mCandidateStripHeight; + fromIndex = i; + } + if (x != 0) { + final View divider = mDividers.get(i - NUM_CANDIDATES_IN_STRIP); + mCandidatesPane.addView(divider); + FrameLayoutCompatUtils.placeViewAt( + divider, x, y + (mCandidateStripHeight - dividerHeight) / 2, + dividerWidth, dividerHeight); + x += dividerWidth; + } + mCandidatesPane.addView(word); + FrameLayoutCompatUtils.placeViewAt( + word, x, y + (mCandidateStripHeight - height) / 2, width, height); + if (info != null) { + mCandidatesPane.addView(info); + final int infoWidth = info.getMeasuredWidth(); + FrameLayoutCompatUtils.placeViewAt( + info, x + width - infoWidth, y, infoWidth, info.getMeasuredHeight()); + } + x += width; } - addView(v); } + if (x != 0) { + // Centering last candidates row. + centeringCandidates(fromIndex, count - 1, x, paneWidth); + } + } + + private void centeringCandidates(int from, int to, int width, int paneWidth) { + final ViewGroup pane = mCandidatesPane; + final int fromIndex = pane.indexOfChild(mWords.get(from)); + final int toIndex; + if (mInfos.get(to).getParent() != null) { + toIndex = pane.indexOfChild(mInfos.get(to)); + } else { + toIndex = pane.indexOfChild(mWords.get(to)); + } + final int offset = (paneWidth - width) / 2; + for (int index = fromIndex; index <= toIndex; index++) { + offsetMargin(pane.getChildAt(index), offset, 0); + } + } + + private static void offsetMargin(View v, int dx, int dy) { + if (v == null) + return; + final ViewGroup.LayoutParams lp = v.getLayoutParams(); + if (lp instanceof ViewGroup.MarginLayoutParams) { + final ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)lp; + mlp.setMargins(mlp.leftMargin + dx, mlp.topMargin + dy, 0, 0); + } + } + + private void expandCandidatesPane() { + mExpandCandidatesPane.setVisibility(View.GONE); + mCloseCandidatesPane.setVisibility(View.VISIBLE); + mCandidatesPaneContainer.setMinimumHeight(mKeyboardView.getMeasuredHeight()); + mCandidatesPaneContainer.setVisibility(View.VISIBLE); + mKeyboardView.setVisibility(View.GONE); + } - scrollTo(0, getScrollY()); - requestLayout(); + private void closeCandidatesPane() { + mExpandCandidatesPane.setVisibility(View.VISIBLE); + mCloseCandidatesPane.setVisibility(View.GONE); + mCandidatesPaneContainer.setVisibility(View.GONE); + mKeyboardView.setVisibility(View.VISIBLE); } public void onAutoCorrectionInverted(CharSequence autoCorrectedWord) { - // Displaying auto corrected word as inverted is enabled only when highlighting candidate - // with color is disabled. - if (mConfigCandidateHighlightFontColorEnabled) + if ((mAutoCorrectHighlight & AUTO_CORRECT_INVERT) == 0) return; - final TextView tv = (TextView)mWords.get(1).findViewById(R.id.candidate_word); + final TextView tv = mWords.get(1); final Spannable word = new SpannableString(autoCorrectedWord); final int wordLength = word.length(); word.setSpan(mInvertedBackgroundColorSpan, 0, wordLength, @@ -260,10 +441,6 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo mShowingAutoCorrectionInverted = true; } - public boolean isConfigCandidateHighlightFontColorEnabled() { - return mConfigCandidateHighlightFontColorEnabled; - } - public boolean isShowingAddToDictionaryHint() { return mShowingAddToDictionary; } @@ -275,7 +452,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo setSuggestions(builder.build()); mShowingAddToDictionary = true; // Disable R.string.hint_add_to_dictionary button - TextView tv = (TextView)getChildAt(1).findViewById(R.id.candidate_word); + TextView tv = mWords.get(1); tv.setClickable(false); } @@ -292,7 +469,11 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo public void clear() { mShowingAddToDictionary = false; mShowingAutoCorrectionInverted = false; - removeAllViews(); + for (int i = 0; i < NUM_CANDIDATES_IN_STRIP; i++) { + mWords.get(i).setText(null); + mInfos.get(i).setVisibility(View.GONE); + } + mCandidatesPane.removeAllViews(); } private void hidePreview() { @@ -304,11 +485,11 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo return; final TextView previewText = mPreviewText; - previewText.setTextColor(mColorNormal); + previewText.setTextColor(mColorTypedWord); previewText.setText(word); previewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); - View v = getChildAt(index); + View v = mWords.get(index); final int[] offsetInWindow = new int[2]; v.getLocationInWindow(offsetInWindow); final int posX = offsetInWindow[0]; @@ -324,15 +505,21 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo } private void addToDictionary(CharSequence word) { - if (mService.addWordToDictionary(word.toString())) { + if (mListener.addWordToDictionary(word.toString())) { showPreview(0, getContext().getString(R.string.added_word, word)); } } @Override public boolean onLongClick(View view) { - int index = (Integer) view.getTag(); - CharSequence word = mSuggestions.getWord(index); + final Object tag = view.getTag(); + if (!(tag instanceof Integer)) + return true; + final int index = (Integer) tag; + if (index >= mSuggestions.size()) + return true; + + final CharSequence word = mSuggestions.getWord(index); if (word.length() < 2) return false; addToDictionary(word); @@ -341,13 +528,22 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo @Override public void onClick(View view) { - int index = (Integer) view.getTag(); - CharSequence word = mSuggestions.getWord(index); + final Object tag = view.getTag(); + if (!(tag instanceof Integer)) + return; + final int index = (Integer) tag; + if (index >= mSuggestions.size()) + return; + + final CharSequence word = mSuggestions.getWord(index); if (mShowingAddToDictionary && index == 0) { addToDictionary(word); } else { - mService.pickSuggestionManually(index, word); + mListener.pickSuggestionManually(index, word); } + // Because some punctuation letters are not treated as word separator depending on locale, + // {@link #setSuggestions} might not be called and candidates pane left opened. + closeCandidatesPane(); } @Override diff --git a/java/src/com/android/inputmethod/latin/ContactsDictionary.java b/java/src/com/android/inputmethod/latin/ContactsDictionary.java index b057cf4e3..66a041508 100644 --- a/java/src/com/android/inputmethod/latin/ContactsDictionary.java +++ b/java/src/com/android/inputmethod/latin/ContactsDictionary.java @@ -40,7 +40,7 @@ public class ContactsDictionary extends ExpandableDictionary { /** * Frequency for contacts information into the dictionary */ - private static final int FREQUENCY_FOR_CONTACTS = 128; + private static final int FREQUENCY_FOR_CONTACTS = 40; private static final int FREQUENCY_FOR_CONTACTS_BIGRAM = 90; private static final int INDEX_NAME = 1; @@ -114,7 +114,7 @@ public class ContactsDictionary extends ExpandableDictionary { while (!cursor.isAfterLast()) { String name = cursor.getString(INDEX_NAME); - if (name != null) { + if (name != null && -1 == name.indexOf('@')) { int len = name.length(); String prevWord = null; @@ -143,8 +143,6 @@ public class ContactsDictionary extends ExpandableDictionary { if (wordLen < maxWordLength && wordLen > 1) { super.addWord(word, FREQUENCY_FOR_CONTACTS); if (!TextUtils.isEmpty(prevWord)) { - // TODO Do not add email address - // Not so critical super.setBigram(prevWord, word, FREQUENCY_FOR_CONTACTS_BIGRAM); } diff --git a/java/src/com/android/inputmethod/latin/DictionaryCollection.java b/java/src/com/android/inputmethod/latin/DictionaryCollection.java index 3fcb6ed55..5e7de3e6b 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryCollection.java +++ b/java/src/com/android/inputmethod/latin/DictionaryCollection.java @@ -54,8 +54,8 @@ public class DictionaryCollection extends Dictionary { @Override public boolean isValidWord(CharSequence word) { - for (final Dictionary dict : mDictionaries) - if (dict.isValidWord(word)) return true; + for (int i = mDictionaries.size() - 1; i >= 0; --i) + if (mDictionaries.get(i).isValidWord(word)) return true; return false; } diff --git a/java/src/com/android/inputmethod/latin/EditingUtils.java b/java/src/com/android/inputmethod/latin/EditingUtils.java index 39e7e402f..e56aa695d 100644 --- a/java/src/com/android/inputmethod/latin/EditingUtils.java +++ b/java/src/com/android/inputmethod/latin/EditingUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Google Inc. + * Copyright (C) 2009 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 diff --git a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java index 26391fe46..97a4a1816 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java @@ -229,6 +229,7 @@ public class ExpandableDictionary extends Dictionary { * Returns the word's frequency or -1 if not found */ protected int getWordFrequency(CharSequence word) { + // Case-sensitive search Node node = searchNode(mRoots, word, 0, word.length()); return (node == null) ? -1 : node.mFrequency; } @@ -366,12 +367,16 @@ public class ExpandableDictionary extends Dictionary { /** * Adds bigrams to the in-memory trie structure that is being used to retrieve any word - * @param frequency frequency for this bigrams - * @param addFrequency if true, it adds to current frequency + * @param frequency frequency for this bigram + * @param addFrequency if true, it adds to current frequency, else it overwrites the old value * @return returns the final frequency */ private int addOrSetBigram(String word1, String word2, int frequency, boolean addFrequency) { - Node firstWord = searchWord(mRoots, word1, 0, null); + // We don't want results to be different according to case of the looked up left hand side + // word. We do want however to return the correct case for the right hand side. + // So we want to squash the case of the left hand side, and preserve that of the right + // hand side word. + Node firstWord = searchWord(mRoots, word1.toLowerCase(), 0, null); Node secondWord = searchWord(mRoots, word2, 0, null); LinkedList<NextWord> bigram = firstWord.mNGrams; if (bigram == null || bigram.size() == 0) { @@ -437,8 +442,12 @@ public class ExpandableDictionary extends Dictionary { } } - private void runReverseLookUp(final CharSequence previousWord, final WordCallback callback) { - Node prevWord = searchNode(mRoots, previousWord, 0, previousWord.length()); + private void runBigramReverseLookUp(final CharSequence previousWord, + final WordCallback callback) { + // Search for the lowercase version of the word only, because that's where bigrams + // store their sons. + Node prevWord = searchNode(mRoots, previousWord.toString().toLowerCase(), 0, + previousWord.length()); if (prevWord != null && prevWord.mNGrams != null) { reverseLookUp(prevWord.mNGrams, callback); } @@ -448,7 +457,7 @@ public class ExpandableDictionary extends Dictionary { public void getBigrams(final WordComposer codes, final CharSequence previousWord, final WordCallback callback) { if (!reloadDictionaryIfRequired()) { - runReverseLookUp(previousWord, callback); + runBigramReverseLookUp(previousWord, callback); } } @@ -494,14 +503,20 @@ public class ExpandableDictionary extends Dictionary { } /** - * Search for the terminal node of the word + * Recursively search for the terminal node of the word. + * + * One iteration takes the full word to search for and the current index of the recursion. + * + * @param children the node of the trie to search under. + * @param word the word to search for. Only read [offset..length] so there may be trailing chars + * @param offset the index in {@code word} this recursion should operate on. + * @param length the length of the input word. * @return Returns the terminal node of the word if the word exists */ private Node searchNode(final NodeArray children, final CharSequence word, final int offset, final int length) { - // TODO Consider combining with addWordRec final int count = children.mLength; - char currentChar = word.charAt(offset); + final char currentChar = word.charAt(offset); for (int j = 0; j < count; j++) { final Node node = children.mData[j]; if (node.mCode == currentChar) { diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 2c39eba55..9c6465dd2 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -16,22 +16,6 @@ package com.android.inputmethod.latin; -import com.android.inputmethod.compat.CompatUtils; -import com.android.inputmethod.compat.EditorInfoCompatUtils; -import com.android.inputmethod.compat.InputConnectionCompatUtils; -import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; -import com.android.inputmethod.compat.InputMethodServiceCompatWrapper; -import com.android.inputmethod.compat.InputTypeCompatUtils; -import com.android.inputmethod.deprecated.LanguageSwitcherProxy; -import com.android.inputmethod.deprecated.VoiceProxy; -import com.android.inputmethod.deprecated.recorrection.Recorrection; -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.keyboard.KeyboardActionListener; -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 android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; @@ -59,7 +43,6 @@ import android.util.PrintWriterPrinter; import android.util.Printer; import android.view.HapticFeedbackConstants; import android.view.KeyEvent; -import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; @@ -69,7 +52,24 @@ import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.ExtractedText; import android.view.inputmethod.InputConnection; -import android.widget.LinearLayout; + +import com.android.inputmethod.accessibility.AccessibilityUtils; +import com.android.inputmethod.compat.CompatUtils; +import com.android.inputmethod.compat.EditorInfoCompatUtils; +import com.android.inputmethod.compat.InputConnectionCompatUtils; +import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; +import com.android.inputmethod.compat.InputMethodServiceCompatWrapper; +import com.android.inputmethod.compat.InputTypeCompatUtils; +import com.android.inputmethod.compat.SuggestionSpanUtils; +import com.android.inputmethod.deprecated.LanguageSwitcherProxy; +import com.android.inputmethod.deprecated.VoiceProxy; +import com.android.inputmethod.deprecated.recorrection.Recorrection; +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.KeyboardActionListener; +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 java.io.FileDescriptor; import java.io.PrintWriter; @@ -78,11 +78,12 @@ import java.util.Locale; /** * Input method implementation for Qwerty'ish keyboard. */ -public class LatinIME extends InputMethodServiceCompatWrapper implements KeyboardActionListener { +public class LatinIME extends InputMethodServiceCompatWrapper implements KeyboardActionListener, + CandidateView.Listener { private static final String TAG = LatinIME.class.getSimpleName(); private static final boolean PERF_DEBUG = false; private static final boolean TRACE = false; - private static boolean DEBUG = LatinImeLogger.sDBG; + private static boolean DEBUG; /** * The private IME option used to indicate that no microphone should be @@ -155,7 +156,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar private UserDictionary mUserDictionary; private UserBigramDictionary mUserBigramDictionary; - private ContactsDictionary mContactsDictionary; private AutoDictionary mAutoDictionary; // TODO: Create an inner class to group options and pseudo-options to improve readability. @@ -173,6 +173,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // Magic space: a space that should disappear on space/apostrophe insertion, move after the // punctuation on punctuation insertion, and become a real space on alpha char insertion. private boolean mJustAddedMagicSpace; // This indicates whether the last char is a magic space. + // This indicates whether the last keypress resulted in processing of double space replacement + // with period-space. + private boolean mJustReplacedDoubleSpace; private int mCorrectionMode; private int mCommittedLength; @@ -181,8 +184,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar private int mLastSelectionStart; private int mLastSelectionEnd; - // Indicates whether the suggestion strip is to be on in landscape - private boolean mJustAccepted; + // Whether we are expecting an onUpdateSelection event to fire. If it does when we don't + // "expect" it, it means the user actually moved the cursor. + private boolean mExpectingUpdateSelection; private int mDeleteCount; private long mLastKeyTime; @@ -217,15 +221,15 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar @Override public void handleMessage(Message msg) { final KeyboardSwitcher switcher = mKeyboardSwitcher; - final LatinKeyboardView inputView = switcher.getInputView(); + final LatinKeyboardView inputView = switcher.getKeyboardView(); switch (msg.what) { case MSG_UPDATE_SUGGESTIONS: updateSuggestions(); break; case MSG_UPDATE_OLD_SUGGESTIONS: - mRecorrection.setRecorrectionSuggestions(mVoiceProxy, mCandidateView, mSuggest, - mKeyboardSwitcher, mWord, mHasUncommittedTypedChars, mLastSelectionStart, - mLastSelectionEnd, mSettingsValues.mWordSeparators); + mRecorrection.fetchAndDisplayRecorrectionSuggestions(mVoiceProxy, mCandidateView, + mSuggest, mKeyboardSwitcher, mWord, mHasUncommittedTypedChars, + mLastSelectionStart, mLastSelectionEnd, mSettingsValues.mWordSeparators); break; case MSG_UPDATE_SHIFT_STATE: switcher.updateShiftState(); @@ -307,7 +311,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar public void startDisplayLanguageOnSpacebar(boolean localeChanged) { removeMessages(MSG_FADEOUT_LANGUAGE_ON_SPACEBAR); removeMessages(MSG_DISMISS_LANGUAGE_ON_SPACEBAR); - final LatinKeyboardView inputView = mKeyboardSwitcher.getInputView(); + final LatinKeyboardView inputView = mKeyboardSwitcher.getKeyboardView(); if (inputView != null) { final LatinKeyboard keyboard = mKeyboardSwitcher.getLatinKeyboard(); // The language is always displayed when the delay is negative. @@ -350,6 +354,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar SubtypeSwitcher.init(this, prefs); KeyboardSwitcher.init(this, prefs); Recorrection.init(this, prefs); + AccessibilityUtils.init(this, prefs); super.onCreate(); @@ -358,6 +363,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mSubtypeSwitcher = SubtypeSwitcher.getInstance(); mKeyboardSwitcher = KeyboardSwitcher.getInstance(); mRecorrection = Recorrection.getInstance(); + DEBUG = LatinImeLogger.sDBG; loadSettings(); @@ -402,11 +408,12 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (null == mPrefs) mPrefs = PreferenceManager.getDefaultSharedPreferences(this); if (null == mSubtypeSwitcher) mSubtypeSwitcher = SubtypeSwitcher.getInstance(); mSettingsValues = new Settings.Values(mPrefs, this, mSubtypeSwitcher.getInputLocaleStr()); + resetContactsDictionary(); } private void initSuggest() { final String localeStr = mSubtypeSwitcher.getInputLocaleStr(); - final Locale keyboardLocale = new Locale(localeStr); + final Locale keyboardLocale = Utils.constructLocaleFromString(localeStr); final Resources res = mResources; final Locale savedLocale = Utils.setSystemLocale(res, keyboardLocale); @@ -424,8 +431,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mUserDictionary = new UserDictionary(this, localeStr); mSuggest.setUserDictionary(mUserDictionary); - mContactsDictionary = new ContactsDictionary(this, Suggest.DIC_CONTACTS); - mSuggest.setContactsDictionary(mContactsDictionary); + resetContactsDictionary(); mAutoDictionary = new AutoDictionary(this, this, localeStr, Suggest.DIC_AUTO); mSuggest.setAutoDictionary(mAutoDictionary); @@ -438,9 +444,16 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar Utils.setSystemLocale(res, savedLocale); } + private void resetContactsDictionary() { + if (null == mSuggest) return; + ContactsDictionary contactsDictionary = mSettingsValues.mUseContactsDict + ? new ContactsDictionary(this, Suggest.DIC_CONTACTS) : null; + mSuggest.setContactsDictionary(contactsDictionary); + } + /* package private */ void resetSuggestMainDict() { final String localeStr = mSubtypeSwitcher.getInputLocaleStr(); - final Locale keyboardLocale = new Locale(localeStr); + final Locale keyboardLocale = Utils.constructLocaleFromString(localeStr); int mainDicResId = Utils.getMainDictionaryResourceId(mResources); mSuggest.resetMainDict(this, mainDicResId, keyboardLocale); } @@ -487,24 +500,29 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } @Override - public View onCreateCandidatesView() { - LayoutInflater inflater = getLayoutInflater(); - LinearLayout container = (LinearLayout)inflater.inflate(R.layout.candidates, null); - mCandidateViewContainer = container; + public void setInputView(View view) { + super.setInputView(view); + mCandidateViewContainer = view.findViewById(R.id.candidates_container); + mCandidateView = (CandidateView) view.findViewById(R.id.candidates); + mCandidateView.setListener(this, view); mCandidateStripHeight = (int)mResources.getDimension(R.dimen.candidate_strip_height); - mCandidateView = (CandidateView) container.findViewById(R.id.candidates); - mCandidateView.setService(this); - setCandidatesViewShown(true); - return container; + } + + @Override + public void setCandidatesView(View view) { + // To ensure that CandidatesView will never be set. + return; } @Override public void onStartInputView(EditorInfo attribute, boolean restarting) { final KeyboardSwitcher switcher = mKeyboardSwitcher; - LatinKeyboardView inputView = switcher.getInputView(); + LatinKeyboardView inputView = switcher.getKeyboardView(); if (DEBUG) { - Log.d(TAG, "onStartInputView: " + inputView); + Log.d(TAG, "onStartInputView: attribute:" + ((attribute == null) ? "none" + : String.format("inputType=0x%08x imeOptions=0x%08x", + attribute.inputType, attribute.imeOptions))); } // In landscape mode, this method gets called without the input view being created. if (inputView == null) { @@ -530,6 +548,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mHasUncommittedTypedChars = false; mDeleteCount = 0; mJustAddedMagicSpace = false; + mJustReplacedDoubleSpace = false; loadSettings(); updateCorrectionMode(); @@ -550,7 +569,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar switcher.updateShiftState(); } - setCandidatesViewShownInternal(isCandidateStripVisible(), false /* needsInputViewShown */ ); + setSuggestionStripShownInternal(isCandidateStripVisible(), /* needsInputViewShown */ false); // Delay updating suggestions because keyboard input view may not be shown at this point. mHandler.postUpdateSuggestions(); @@ -624,6 +643,13 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } @Override + public void onWindowHidden() { + super.onWindowHidden(); + KeyboardView inputView = mKeyboardSwitcher.getKeyboardView(); + if (inputView != null) inputView.closing(); + } + + @Override public void onFinishInput() { super.onFinishInput(); @@ -632,7 +658,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mVoiceProxy.flushVoiceInputLogs(mConfigurationChanging); - KeyboardView inputView = mKeyboardSwitcher.getInputView(); + KeyboardView inputView = mKeyboardSwitcher.getKeyboardView(); if (inputView != null) inputView.closing(); if (mAutoDictionary != null) mAutoDictionary.flushPendingWrites(); if (mUserBigramDictionary != null) mUserBigramDictionary.flushPendingWrites(); @@ -641,7 +667,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar @Override public void onFinishInputView(boolean finishingInput) { super.onFinishInputView(finishingInput); - KeyboardView inputView = mKeyboardSwitcher.getInputView(); + KeyboardView inputView = mKeyboardSwitcher.getKeyboardView(); if (inputView != null) inputView.setForeground(false); // Remove pending messages related to update suggestions mHandler.cancelUpdateSuggestions(); @@ -702,14 +728,17 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar ic.finishComposingText(); } mVoiceProxy.setVoiceInputHighlighted(false); - } else if (!mHasUncommittedTypedChars && !mJustAccepted) { + } else if (!mHasUncommittedTypedChars && !mExpectingUpdateSelection) { if (TextEntryState.isAcceptedDefault() || TextEntryState.isSpaceAfterPicked()) { if (TextEntryState.isAcceptedDefault()) TextEntryState.reset(); - mJustAddedMagicSpace = false; // The user moved the cursor. } } - mJustAccepted = false; + if (!mExpectingUpdateSelection) { + mJustAddedMagicSpace = false; // The user moved the cursor. + mJustReplacedDoubleSpace = false; + } + mExpectingUpdateSelection = false; mHandler.postUpdateShiftKeyState(); // Make a note of the cursor position @@ -797,11 +826,11 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // When in fullscreen mode, show completions generated by the application setSuggestions(builder.build()); mBestWord = null; - setCandidatesViewShown(true); + setSuggestionStripShown(true); } } - private void setCandidatesViewShownInternal(boolean shown, boolean needsInputViewShown) { + private void setSuggestionStripShownInternal(boolean shown, boolean needsInputViewShown) { // TODO: Modify this if we support candidates with hard keyboard if (onEvaluateInputViewShown()) { final boolean shouldShowCandidates = shown @@ -809,27 +838,26 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (isExtractViewShown()) { // No need to have extra space to show the key preview. mCandidateViewContainer.setMinimumHeight(0); - super.setCandidatesViewShown(shown); + mCandidateViewContainer.setVisibility( + shouldShowCandidates ? View.VISIBLE : View.GONE); } else { // We must control the visibility of the suggestion strip in order to avoid clipped // key previews, even when we don't show the suggestion strip. mCandidateViewContainer.setVisibility( shouldShowCandidates ? View.VISIBLE : View.INVISIBLE); - super.setCandidatesViewShown(true); } } } - @Override - public void setCandidatesViewShown(boolean shown) { - setCandidatesViewShownInternal(shown, true /* needsInputViewShown */ ); + private void setSuggestionStripShown(boolean shown) { + setSuggestionStripShownInternal(shown, /* needsInputViewShown */true); } @Override public void onComputeInsets(InputMethodService.Insets outInsets) { super.onComputeInsets(outInsets); - final KeyboardView inputView = mKeyboardSwitcher.getInputView(); - if (inputView == null) + final KeyboardView inputView = mKeyboardSwitcher.getKeyboardView(); + if (inputView == null || mCandidateViewContainer == null) return; final int containerHeight = mCandidateViewContainer.getHeight(); int touchY = containerHeight; @@ -870,8 +898,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: - if (event.getRepeatCount() == 0 && mKeyboardSwitcher.getInputView() != null) { - if (mKeyboardSwitcher.getInputView().handleBack()) { + if (event.getRepeatCount() == 0 && mKeyboardSwitcher.getKeyboardView() != null) { + if (mKeyboardSwitcher.getKeyboardView().handleBack()) { return true; } } @@ -960,6 +988,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar ic.commitText(". ", 1); ic.endBatchEdit(); mKeyboardSwitcher.updateShiftState(); + mJustReplacedDoubleSpace = true; } else { mHandler.startDoubleSpacesTimer(); } @@ -990,6 +1019,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } } + @Override public boolean addWordToDictionary(String word) { mUserDictionary.addWord(word, 128); // Suggestion strip should be updated after the operation of adding word to the @@ -1007,14 +1037,14 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } private void onSettingsKeyPressed() { - if (!isShowingOptionDialog()) { - if (!mSettingsValues.mEnableShowSubtypeSettings) { - showSubtypeSelectorAndSettings(); - } else if (Utils.hasMultipleEnabledIMEsOrSubtypes(mImm)) { - showOptionsMenu(); - } else { - launchSettings(); - } + if (isShowingOptionDialog()) + return; + if (InputMethodServiceCompatWrapper.CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) { + showSubtypeSelectorAndSettings(); + } else if (Utils.hasMultipleEnabledIMEsOrSubtypes(mImm)) { + showOptionsMenu(); + } else { + launchSettings(); } } @@ -1042,10 +1072,13 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mLastKeyTime = when; KeyboardSwitcher switcher = mKeyboardSwitcher; final boolean distinctMultiTouch = switcher.hasDistinctMultitouch(); + final boolean lastStateOfJustReplacedDoubleSpace = mJustReplacedDoubleSpace; + mJustReplacedDoubleSpace = false; switch (primaryCode) { case Keyboard.CODE_DELETE: - handleBackspace(); + handleBackspace(lastStateOfJustReplacedDoubleSpace); mDeleteCount++; + mExpectingUpdateSelection = true; LatinImeLogger.logOnDelete(); break; case Keyboard.CODE_SHIFT: @@ -1083,6 +1116,14 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar break; case Keyboard.CODE_TAB: handleTab(); + // There are two cases for tab. Either we send a "next" event, that may change the + // focus but will never move the cursor. Or, we send a real tab keycode, which some + // applications may accept or ignore, and we don't know whether this will move the + // cursor or not. So actually, we don't really know. + // So to go with the safer option, we'd rather behave as if the user moved the + // cursor when they didn't than the opposite. We also expect that most applications + // will actually use tab only for focus movement. + // To sum it up: do not update mExpectingUpdateSelection here. break; default: if (mSettingsValues.isWordSeparator(primaryCode)) { @@ -1090,6 +1131,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } else { handleCharacter(primaryCode, keyCodes, x, y); } + mExpectingUpdateSelection = true; + break; } switcher.onKey(primaryCode); // Reset after any single keystroke @@ -1119,7 +1162,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar mKeyboardSwitcher.onCancelInput(); } - private void handleBackspace() { + private void handleBackspace(boolean justReplacedDoubleSpace) { if (mVoiceProxy.logAndRevertVoiceInput()) return; final InputConnection ic = getCurrentInputConnection(); @@ -1160,6 +1203,12 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar ic.endBatchEdit(); return; } + if (justReplacedDoubleSpace) { + if (revertDoubleSpace()) { + ic.endBatchEdit(); + return; + } + } if (mEnteredText != null && sameAsTextBeforeCursor(ic, mEnteredText)) { ic.deleteSurroundingText(mEnteredText.length(), 0); @@ -1200,7 +1249,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (EditorInfoCompatUtils.hasFlagNavigateNext(imeOptions) && !isManualTemporaryUpperCase) { EditorInfoCompatUtils.performEditorActionNext(ic); - ic.performEditorAction(EditorInfo.IME_ACTION_NEXT); } else if (EditorInfoCompatUtils.hasFlagNavigatePrevious(imeOptions) && isManualTemporaryUpperCase) { EditorInfoCompatUtils.performEditorActionPrevious(ic); @@ -1228,7 +1276,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar clearSuggestions(); } } - KeyboardSwitcher switcher = mKeyboardSwitcher; + final KeyboardSwitcher switcher = mKeyboardSwitcher; if (switcher.isShiftedOrShiftLocked()) { if (keyCodes == null || keyCodes[0] < Character.MIN_CODE_POINT || keyCodes[0] > Character.MAX_CODE_POINT) { @@ -1236,13 +1284,15 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } code = keyCodes[0]; if (switcher.isAlphabetMode() && Character.isLowerCase(code)) { - int upperCaseCode = Character.toUpperCase(code); - if (upperCaseCode != code) { - code = upperCaseCode; + // In some locales, such as Turkish, Character.toUpperCase() may return a wrong + // character because it doesn't take care of locale. + final String upperCaseString = new String(new int[] {code}, 0, 1) + .toUpperCase(mSubtypeSwitcher.getInputLocale()); + if (upperCaseString.codePointCount(0, upperCaseString.length()) == 1) { + code = upperCaseString.codePointAt(0); } else { // Some keys, such as [eszett], have upper case as multi-characters. - String upperCase = new String(new int[] {code}, 0, 1).toUpperCase(); - onTextInput(upperCase); + onTextInput(upperCaseString); return; } } @@ -1358,7 +1408,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar commitTyped(getCurrentInputConnection()); mVoiceProxy.handleClose(); requestHideSelf(0); - LatinKeyboardView inputView = mKeyboardSwitcher.getInputView(); + LatinKeyboardView inputView = mKeyboardSwitcher.getKeyboardView(); if (inputView != null) inputView.closing(); } @@ -1394,7 +1444,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (DEBUG) { Log.d(TAG, "Switch to keyboard view."); } - View v = mKeyboardSwitcher.getInputView(); + View v = mKeyboardSwitcher.getKeyboardView(); if (v != null) { // Confirms that the keyboard view doesn't have parent view. ViewParent p = v.getParent(); @@ -1403,7 +1453,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } setInputView(v); } - setCandidatesViewShown(isCandidateStripVisible()); + setSuggestionStripShown(isCandidateStripVisible()); updateInputViewShown(); mHandler.postUpdateSuggestions(); } @@ -1413,16 +1463,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } public void setSuggestions(SuggestedWords words) { - if (mVoiceProxy.getAndResetIsShowingHint()) { - setCandidatesView(mCandidateViewContainer); - } - if (mCandidateView != null) { mCandidateView.setSuggestions(words); - if (mCandidateView.isConfigCandidateHighlightFontColorEnabled()) { - mKeyboardSwitcher.onAutoCorrectionStateChanged( - words.hasWordAboveAutoCorrectionScoreThreshold()); - } + mKeyboardSwitcher.onAutoCorrectionStateChanged( + words.hasWordAboveAutoCorrectionScoreThreshold()); } } @@ -1445,7 +1489,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar CharSequence prevWord = EditingUtils.getPreviousWord(getCurrentInputConnection(), mSettingsValues.mWordSeparators); SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder( - mKeyboardSwitcher.getInputView(), word, prevWord); + mKeyboardSwitcher.getKeyboardView(), word, prevWord); boolean correctionAvailable = !mInputTypeNoAutoCorrect && mSuggest.hasAutoCorrection(); final CharSequence typedWord = word.getTypedWord(); @@ -1494,7 +1538,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } else { mBestWord = null; } - setCandidatesViewShown(isCandidateStripVisible()); + setSuggestionStripShown(isCandidateStripVisible()); } private boolean pickDefaultSuggestion(int separatorCode) { @@ -1505,8 +1549,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } if (mBestWord != null && mBestWord.length() > 0) { TextEntryState.acceptedDefault(mWord.getTypedWord(), mBestWord, separatorCode); - mJustAccepted = true; - pickSuggestion(mBestWord); + mExpectingUpdateSelection = true; + commitBestWord(mBestWord); // Add the word to the auto dictionary if it's not a known word addToAutoAndUserBigramDictionaries(mBestWord, AutoDictionary.FREQUENCY_FOR_TYPED); return true; @@ -1514,6 +1558,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar return false; } + @Override public void pickSuggestionManually(int index, CharSequence suggestion) { SuggestedWords suggestions = mCandidateView.getSuggestions(); mVoiceProxy.flushAndLogAllTextModificationCounters(index, suggestion, @@ -1553,7 +1598,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // a magic space even if it was a normal space. This is meant to help in case the user // pressed space on purpose of displaying the suggestion strip punctuation. final char primaryCode = suggestion.charAt(0); - final CharSequence beforeText = ic.getTextBeforeCursor(1, 0); + final CharSequence beforeText = ic != null ? ic.getTextBeforeCursor(1, 0) : ""; final int toLeft = (ic == null || TextUtils.isEmpty(beforeText)) ? 0 : beforeText.charAt(0); final boolean oldMagicSpace = mJustAddedMagicSpace; @@ -1572,8 +1617,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // context - no user input. We should reset the word composer. mWord.reset(); } - mJustAccepted = true; - pickSuggestion(suggestion); + mExpectingUpdateSelection = true; + commitBestWord(suggestion); // Add the word to the auto dictionary if it's not a known word if (index == 0) { addToAutoAndUserBigramDictionaries(suggestion, AutoDictionary.FREQUENCY_FOR_PICKED); @@ -1613,8 +1658,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (!showingAddToDictionaryHint) { // If we're not showing the "Touch again to save", then show corrections again. // In case the cursor position doesn't change, make sure we show the suggestions again. - clearSuggestions(); - mHandler.postUpdateOldSuggestions(); + updateBigramPredictions(); + // Updating the predictions right away may be slow and feel unresponsive on slower + // terminals. On the other hand if we just postUpdateBigramPredictions() it will + // take a noticeable delay to update them which may feel uneasy. } if (showingAddToDictionaryHint) { mCandidateView.showAddToDictionaryHint(suggestion); @@ -1627,25 +1674,25 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar /** * Commits the chosen word to the text field and saves it for later * retrieval. - * @param suggestion the suggestion picked by the user to be committed to - * the text field */ - private void pickSuggestion(CharSequence suggestion) { + private void commitBestWord(CharSequence bestWord) { KeyboardSwitcher switcher = mKeyboardSwitcher; if (!switcher.isKeyboardAvailable()) return; InputConnection ic = getCurrentInputConnection(); if (ic != null) { - mVoiceProxy.rememberReplacedWord(suggestion, mSettingsValues.mWordSeparators); - ic.commitText(suggestion, 1); + mVoiceProxy.rememberReplacedWord(bestWord, mSettingsValues.mWordSeparators); + SuggestedWords suggestedWords = mCandidateView.getSuggestions(); + ic.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan( + this, bestWord, suggestedWords), 1); } - mRecorrection.saveRecorrectionSuggestion(mWord, suggestion); + mRecorrection.saveRecorrectionSuggestion(mWord, bestWord); mHasUncommittedTypedChars = false; - mCommittedLength = suggestion.length(); + mCommittedLength = bestWord.length(); } private static final WordComposer sEmptyWordComposer = new WordComposer(); - private void updateBigramPredictions() { + public void updateBigramPredictions() { if (mSuggest == null || !isSuggestionsRequested()) return; @@ -1657,7 +1704,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar final CharSequence prevWord = EditingUtils.getThisWord(getCurrentInputConnection(), mSettingsValues.mWordSeparators); SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder( - mKeyboardSwitcher.getInputView(), sEmptyWordComposer, prevWord); + mKeyboardSwitcher.getKeyboardView(), sEmptyWordComposer, prevWord); if (builder.size() > 0) { // Explicitly supply an empty typed word (the no-second-arg version of @@ -1670,7 +1717,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar public void setPunctuationSuggestions() { setSuggestions(mSettingsValues.mSuggestPuncList); - setCandidatesViewShown(isCandidateStripVisible()); + setSuggestionStripShown(isCandidateStripVisible()); } private void addToAutoAndUserBigramDictionaries(CharSequence suggestion, int frequencyDelta) { @@ -1780,6 +1827,21 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } } + public boolean revertDoubleSpace() { + mHandler.cancelDoubleSpacesTimer(); + final InputConnection ic = getCurrentInputConnection(); + // Here we test whether we indeed have a period and a space before us. This should not + // be needed, but it's there just in case something went wrong. + final CharSequence textBeforeCursor = ic.getTextBeforeCursor(2, 0); + if (!". ".equals(textBeforeCursor)) + return false; + ic.beginBatchEdit(); + ic.deleteSurroundingText(2, 0); + ic.commitText(" ", 1); + ic.endBatchEdit(); + return true; + } + public boolean isWordSeparator(int code) { return mSettingsValues.isWordSeparator(code); } @@ -1813,7 +1875,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } // The following is necessary because on API levels < 10, we don't get notified when // subtype changes. - onRefreshKeyboard(); + if (!CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) + onRefreshKeyboard(); } @Override @@ -1879,7 +1942,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // if mAudioManager is null, we don't have the ringer state yet // mAudioManager will be set by updateRingerMode if (mAudioManager == null) { - if (mKeyboardSwitcher.getInputView() != null) { + if (mKeyboardSwitcher.getKeyboardView() != null) { updateRingerMode(); } } @@ -1906,7 +1969,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (!mSettingsValues.mVibrateOn) { return; } - LatinKeyboardView inputView = mKeyboardSwitcher.getInputView(); + LatinKeyboardView inputView = mKeyboardSwitcher.getKeyboardView(); if (inputView != null) { inputView.performHapticFeedback( HapticFeedbackConstants.KEYBOARD_TAP, @@ -1914,11 +1977,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar } } - public void promoteToUserDictionary(String word, int frequency) { - if (mUserDictionary.isValidWord(word)) return; - mUserDictionary.addWord(word, frequency); - } - public WordComposer getCurrentWord() { return mWord; } @@ -2030,7 +2088,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar private void showOptionsMenuInternal(CharSequence title, CharSequence[] items, DialogInterface.OnClickListener listener) { - final IBinder windowToken = mKeyboardSwitcher.getInputView().getWindowToken(); + final IBinder windowToken = mKeyboardSwitcher.getKeyboardView().getWindowToken(); if (windowToken == null) return; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setCancelable(true); diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java index 7c323c155..3ad2a5965 100644 --- a/java/src/com/android/inputmethod/latin/Settings.java +++ b/java/src/com/android/inputmethod/latin/Settings.java @@ -79,6 +79,8 @@ public class Settings extends PreferenceActivity public static final String PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY = "pref_key_preview_popup_dismiss_delay"; + public static final String PREF_KEY_USE_CONTACTS_DICT = + "pref_key_use_contacts_dict"; public static final String PREF_USABILITY_STUDY_MODE = "usability_study_mode"; @@ -87,7 +89,6 @@ public class Settings extends PreferenceActivity public static class Values { // From resources: - public final boolean mEnableShowSubtypeSettings; public final boolean mSwipeDownDismissKeyboardEnabled; public final int mDelayBeforeFadeoutLanguageOnSpacebar; public final int mDelayUpdateSuggestions; @@ -115,21 +116,20 @@ public class Settings extends PreferenceActivity public final boolean mBigramSuggestionEnabled; // Prediction: use bigrams to predict the next word when there is no input for it yet public final boolean mBigramPredictionEnabled; + public final boolean mUseContactsDict; public Values(final SharedPreferences prefs, final Context context, final String localeStr) { final Resources res = context.getResources(); final Locale savedLocale; if (null != localeStr) { - final Locale keyboardLocale = new Locale(localeStr); + final Locale keyboardLocale = Utils.constructLocaleFromString(localeStr); savedLocale = Utils.setSystemLocale(res, keyboardLocale); } else { savedLocale = null; } // Get the resources - mEnableShowSubtypeSettings = res.getBoolean( - R.bool.config_enable_show_subtype_settings); mSwipeDownDismissKeyboardEnabled = res.getBoolean( R.bool.config_swipe_down_dismiss_keyboard_enabled); mDelayBeforeFadeoutLanguageOnSpacebar = res.getInteger( @@ -178,6 +178,8 @@ public class Settings extends PreferenceActivity mAutoCorrectionThreshold = getAutoCorrectionThreshold(prefs, res); + mUseContactsDict = prefs.getBoolean(Settings.PREF_KEY_USE_CONTACTS_DICT, true); + Utils.setSystemLocale(res, savedLocale); } @@ -354,8 +356,6 @@ public class Settings extends PreferenceActivity (PreferenceGroup) findPreference(PREF_GENERAL_SETTINGS_KEY); final PreferenceGroup textCorrectionGroup = (PreferenceGroup) findPreference(PREF_CORRECTION_SETTINGS_KEY); - final PreferenceGroup bigramGroup = - (PreferenceGroup) findPreference(PREF_NGRAM_SETTINGS_KEY); final boolean showSettingsKeyOption = res.getBoolean( R.bool.config_enable_show_settings_key_option); @@ -373,10 +373,7 @@ public class Settings extends PreferenceActivity generalSettings.removePreference(findPreference(PREF_VIBRATE_ON)); } - final boolean showSubtypeSettings = res.getBoolean( - R.bool.config_enable_show_subtype_settings); - if (InputMethodServiceCompatWrapper.CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED - && !showSubtypeSettings) { + if (InputMethodServiceCompatWrapper.CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) { generalSettings.removePreference(findPreference(PREF_SUBTYPES)); } @@ -499,7 +496,6 @@ public class Settings extends PreferenceActivity } private void updateSettingsKeySummary() { - final ListPreference lp = mSettingsKeyPreference; mSettingsKeyPreference.setSummary( getResources().getStringArray(R.array.settings_key_modes) [mSettingsKeyPreference.findIndexOfValue(mSettingsKeyPreference.getValue())]); diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index d8012087b..6ca12c0c5 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -92,10 +92,9 @@ public class SubtypeSwitcher { } public static void init(LatinIME service, SharedPreferences prefs) { + SubtypeLocale.init(service); sInstance.initialize(service, prefs); sInstance.updateAllParameters(); - - SubtypeLocale.init(service); } private SubtypeSwitcher() { @@ -281,14 +280,8 @@ public class SubtypeSwitcher { // "en_US" --> language: en & country: US // "en" --> language: en // "" --> the system locale - mLocaleSplitter.setString(inputLocaleStr); - if (mLocaleSplitter.hasNext()) { - String language = mLocaleSplitter.next(); - if (mLocaleSplitter.hasNext()) { - mInputLocale = new Locale(language, mLocaleSplitter.next()); - } else { - mInputLocale = new Locale(language); - } + if (!TextUtils.isEmpty(inputLocaleStr)) { + mInputLocale = Utils.constructLocaleFromString(inputLocaleStr); mInputLocaleStr = inputLocaleStr; } else { mInputLocale = mSystemLocale; @@ -420,7 +413,7 @@ public class SubtypeSwitcher { final KeyboardSwitcher switcher = KeyboardSwitcher.getInstance(); final LatinKeyboard keyboard = switcher.getLatinKeyboard(); if (keyboard != null) { - keyboard.updateShortcutKey(isShortcutImeReady(), switcher.getInputView()); + keyboard.updateShortcutKey(isShortcutImeReady(), switcher.getKeyboardView()); } } @@ -510,7 +503,7 @@ public class SubtypeSwitcher { private void triggerVoiceIME() { if (!mService.isInputViewShown()) return; VoiceProxy.getInstance().startListening(false, - KeyboardSwitcher.getInstance().getInputView().getWindowToken()); + KeyboardSwitcher.getInstance().getKeyboardView().getWindowToken()); } ////////////////////////////////////// @@ -549,12 +542,12 @@ public class SubtypeSwitcher { || mEnabledKeyboardSubtypesOfCurrentInputMethod.size() == 0) return; mCurrentKeyboardSubtypeIndex = getCurrentIndex(); mNextKeyboardSubtype = getNextKeyboardSubtypeInternal(mCurrentKeyboardSubtypeIndex); - Locale locale = new Locale(mNextKeyboardSubtype.getLocale()); - mNextLanguage = getDisplayLanguage(locale); + Locale locale = Utils.constructLocaleFromString(mNextKeyboardSubtype.getLocale()); + mNextLanguage = getFullDisplayName(locale, true); mPreviousKeyboardSubtype = getPreviousKeyboardSubtypeInternal( mCurrentKeyboardSubtypeIndex); - locale = new Locale(mPreviousKeyboardSubtype.getLocale()); - mPreviousLanguage = getDisplayLanguage(locale); + locale = Utils.constructLocaleFromString(mPreviousKeyboardSubtype.getLocale()); + mPreviousLanguage = getFullDisplayName(locale, true); } private int normalize(int index) { @@ -584,29 +577,30 @@ public class SubtypeSwitcher { public static String getFullDisplayName(Locale locale, boolean returnsNameInThisLocale) { if (returnsNameInThisLocale) { - return toTitleCase(SubtypeLocale.getFullDisplayName(locale)); + return toTitleCase(SubtypeLocale.getFullDisplayName(locale), locale); } else { - return toTitleCase(locale.getDisplayName()); + return toTitleCase(locale.getDisplayName(), locale); } } public static String getDisplayLanguage(Locale locale) { - return toTitleCase(locale.getDisplayLanguage(locale)); + return toTitleCase(SubtypeLocale.getFullDisplayName(locale), locale); } public static String getMiddleDisplayLanguage(Locale locale) { - return toTitleCase((new Locale(locale.getLanguage()).getDisplayLanguage(locale))); + return toTitleCase((Utils.constructLocaleFromString( + locale.getLanguage()).getDisplayLanguage(locale)), locale); } public static String getShortDisplayLanguage(Locale locale) { - return toTitleCase(locale.getLanguage()); + return toTitleCase(locale.getLanguage(), locale); } - private static String toTitleCase(String s) { + private static String toTitleCase(String s, Locale locale) { if (s.length() == 0) { return s; } - return Character.toUpperCase(s.charAt(0)) + s.substring(1); + return s.toUpperCase(locale).charAt(0) + s.substring(1); } public String getInputLanguageName() { diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index ca75866c0..eb5ed5a65 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -84,7 +84,7 @@ public class Suggest implements Dictionary.WordCallback { private final Map<String, Dictionary> mUnigramDictionaries = new HashMap<String, Dictionary>(); private final Map<String, Dictionary> mBigramDictionaries = new HashMap<String, Dictionary>(); - private int mPrefMaxSuggestions = 12; + private int mPrefMaxSuggestions = 18; private static final int PREF_MAX_BIGRAMS = 60; @@ -117,30 +117,31 @@ public class Suggest implements Dictionary.WordCallback { } private void init(Context context, Dictionary mainDict) { - if (mainDict != null) { - mMainDict = mainDict; - mUnigramDictionaries.put(DICT_KEY_MAIN, mainDict); - mBigramDictionaries.put(DICT_KEY_MAIN, mainDict); - } + mMainDict = mainDict; + addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, mainDict); + addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, mainDict); mWhiteListDictionary = WhitelistDictionary.init(context); - if (mWhiteListDictionary != null) { - mUnigramDictionaries.put(DICT_KEY_WHITELIST, mWhiteListDictionary); - } + addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_WHITELIST, mWhiteListDictionary); mAutoCorrection = new AutoCorrection(); initPool(); } + private void addOrReplaceDictionary(Map<String, Dictionary> dictionaries, String key, + Dictionary dict) { + final Dictionary oldDict = (dict == null) + ? dictionaries.remove(key) + : dictionaries.put(key, dict); + if (oldDict != null && dict != oldDict) { + oldDict.close(); + } + } + public void resetMainDict(Context context, int dictionaryResId, Locale locale) { final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager( context, locale, dictionaryResId); mMainDict = newMainDict; - if (null == newMainDict) { - mUnigramDictionaries.remove(DICT_KEY_MAIN); - mBigramDictionaries.remove(DICT_KEY_MAIN); - } else { - mUnigramDictionaries.put(DICT_KEY_MAIN, newMainDict); - mBigramDictionaries.put(DICT_KEY_MAIN, newMainDict); - } + addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, newMainDict); + addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, newMainDict); } private void initPool() { @@ -179,28 +180,25 @@ public class Suggest implements Dictionary.WordCallback { * before the main dictionary, if set. */ public void setUserDictionary(Dictionary userDictionary) { - if (userDictionary != null) - mUnigramDictionaries.put(DICT_KEY_USER, userDictionary); + addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER, userDictionary); } /** - * Sets an optional contacts dictionary resource to be loaded. + * Sets an optional contacts dictionary resource to be loaded. It is also possible to remove + * the contacts dictionary by passing null to this method. In this case no contacts dictionary + * won't be used. */ public void setContactsDictionary(Dictionary contactsDictionary) { - if (contactsDictionary != null) { - mUnigramDictionaries.put(DICT_KEY_CONTACTS, contactsDictionary); - mBigramDictionaries.put(DICT_KEY_CONTACTS, contactsDictionary); - } + addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary); + addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary); } public void setAutoDictionary(Dictionary autoDictionary) { - if (autoDictionary != null) - mUnigramDictionaries.put(DICT_KEY_AUTO, autoDictionary); + addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_AUTO, autoDictionary); } public void setUserBigramDictionary(Dictionary userBigramDictionary) { - if (userBigramDictionary != null) - mBigramDictionaries.put(DICT_KEY_USER_BIGRAM, userBigramDictionary); + addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_USER_BIGRAM, userBigramDictionary); } public void setAutoCorrectionThreshold(double threshold) { @@ -252,6 +250,7 @@ public class Suggest implements Dictionary.WordCallback { poolSize > 0 ? (StringBuilder) mStringPool.remove(poolSize - 1) : new StringBuilder(getApproxMaxWordLength()); sb.setLength(0); + // TODO: Must pay attention to locale when changing case. if (all) { sb.append(word.toString().toUpperCase()); } else if (first) { @@ -317,6 +316,7 @@ public class Suggest implements Dictionary.WordCallback { } else { // Word entered: return only bigrams that match the first char of the typed word final char currentChar = typedWord.charAt(0); + // TODO: Must pay attention to locale when changing case. final char currentCharUpper = Character.toUpperCase(currentChar); int count = 0; final int bigramSuggestionSize = mBigramSuggestions.size(); @@ -520,6 +520,7 @@ public class Suggest implements Dictionary.WordCallback { StringBuilder sb = poolSize > 0 ? (StringBuilder) mStringPool.remove(poolSize - 1) : new StringBuilder(getApproxMaxWordLength()); sb.setLength(0); + // TODO: Must pay attention to locale when changing case. if (mIsAllUpperCase) { sb.append(new String(word, offset, length).toUpperCase()); } else if (mIsFirstCharCapitalized) { diff --git a/java/src/com/android/inputmethod/latin/SuggestionSpanPickedNotificationReceiver.java b/java/src/com/android/inputmethod/latin/SuggestionSpanPickedNotificationReceiver.java new file mode 100644 index 000000000..4a3f42d5d --- /dev/null +++ b/java/src/com/android/inputmethod/latin/SuggestionSpanPickedNotificationReceiver.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2011 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.compat.SuggestionSpanUtils; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +public class SuggestionSpanPickedNotificationReceiver extends BroadcastReceiver { + private static final boolean DBG = LatinImeLogger.sDBG; + private static final String TAG = + SuggestionSpanPickedNotificationReceiver.class.getSimpleName(); + + @Override + public void onReceive(Context context, Intent intent) { + if (SuggestionSpanUtils.ACTION_SUGGESTION_PICKED.equals(intent.getAction())) { + if (DBG) { + final String before = intent.getStringExtra( + SuggestionSpanUtils.SUGGESTION_SPAN_PICKED_BEFORE); + final String after = intent.getStringExtra( + SuggestionSpanUtils.SUGGESTION_SPAN_PICKED_AFTER); + Log.d(TAG, "Received notification picked: " + before + "," + after); + } + } + } +} diff --git a/java/src/com/android/inputmethod/latin/UserBigramDictionary.java b/java/src/com/android/inputmethod/latin/UserBigramDictionary.java index a32a6461a..5b615ca29 100644 --- a/java/src/com/android/inputmethod/latin/UserBigramDictionary.java +++ b/java/src/com/android/inputmethod/latin/UserBigramDictionary.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Google Inc. + * 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 @@ -158,7 +158,7 @@ public class UserBigramDictionary extends ExpandableDictionary { * Pair will be added to the userbigram database. */ public int addBigrams(String word1, String word2) { - // remove caps + // remove caps if second word is autocapitalized if (mIme != null && mIme.getCurrentWord().isAutoCapitalized()) { word2 = Character.toLowerCase(word2.charAt(0)) + word2.substring(1); } diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java index 47890e643..245fc20bc 100644 --- a/java/src/com/android/inputmethod/latin/Utils.java +++ b/java/src/com/android/inputmethod/latin/Utils.java @@ -18,6 +18,7 @@ package com.android.inputmethod.latin; import com.android.inputmethod.compat.InputMethodInfoCompatWrapper; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; +import com.android.inputmethod.compat.InputMethodSubtypeCompatWrapper; import com.android.inputmethod.compat.InputTypeCompatUtils; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.KeyboardId; @@ -43,7 +44,10 @@ import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; +import java.util.List; import java.util.Locale; public class Utils { @@ -108,7 +112,34 @@ public class Utils { } public static boolean hasMultipleEnabledIMEsOrSubtypes(InputMethodManagerCompatWrapper imm) { - return imm.getEnabledInputMethodList().size() > 1 + final List<InputMethodInfoCompatWrapper> enabledImis = imm.getEnabledInputMethodList(); + + // Filters out IMEs that have auxiliary subtypes only (including either implicitly or + // explicitly enabled ones). + final ArrayList<InputMethodInfoCompatWrapper> filteredImis = + new ArrayList<InputMethodInfoCompatWrapper>(); + + outerloop: + for (InputMethodInfoCompatWrapper imi : enabledImis) { + // We can return true immediately after we find two or more filtered IMEs. + if (filteredImis.size() > 1) return true; + final List<InputMethodSubtypeCompatWrapper> subtypes = + imm.getEnabledInputMethodSubtypeList(imi, true); + // IMEs that have no subtypes should be included. + if (subtypes.isEmpty()) { + filteredImis.add(imi); + continue; + } + // IMEs that have one or more non-auxiliary subtypes should be included. + for (InputMethodSubtypeCompatWrapper subtype : subtypes) { + if (!subtype.isAuxiliary()) { + filteredImis.add(imi); + continue outerloop; + } + } + } + + return filteredImis.size() > 1 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled // input method subtype (The current IME should be LatinIME.) || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1; @@ -537,8 +568,6 @@ public class Utils { return KeyboardId.MODE_IM; } else if (variation == InputType.TYPE_TEXT_VARIATION_FILTER) { return KeyboardId.MODE_TEXT; - } else if (variation == InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT) { - return KeyboardId.MODE_WEB; } else { return KeyboardId.MODE_TEXT; } @@ -662,4 +691,28 @@ public class Utils { res.updateConfiguration(conf, res.getDisplayMetrics()); return saveLocale; } + + private static final HashMap<String, Locale> sLocaleCache = new HashMap<String, Locale>(); + + public static Locale constructLocaleFromString(String localeStr) { + if (localeStr == null) + return null; + synchronized (sLocaleCache) { + if (sLocaleCache.containsKey(localeStr)) + return sLocaleCache.get(localeStr); + Locale retval = null; + String[] localeParams = localeStr.split("_", 3); + if (localeParams.length == 1) { + retval = new Locale(localeParams[0]); + } else if (localeParams.length == 2) { + retval = new Locale(localeParams[0], localeParams[1]); + } else if (localeParams.length == 3) { + retval = new Locale(localeParams[0], localeParams[1], localeParams[2]); + } + if (retval != null) { + sLocaleCache.put(localeStr, retval); + } + return retval; + } + } } diff --git a/java/src/com/android/inputmethod/latin/WhitelistDictionary.java b/java/src/com/android/inputmethod/latin/WhitelistDictionary.java index 2389d4e3c..4377373d2 100644 --- a/java/src/com/android/inputmethod/latin/WhitelistDictionary.java +++ b/java/src/com/android/inputmethod/latin/WhitelistDictionary.java @@ -39,6 +39,7 @@ public class WhitelistDictionary extends Dictionary { public static WhitelistDictionary init(Context context) { synchronized (sInstance) { if (context != null) { + // Wordlist is initialized by the proper language in Suggestion.java#init sInstance.initWordlist( context.getResources().getStringArray(R.array.wordlist_whitelist)); } else { diff --git a/java/src/com/android/inputmethod/latin/spellcheck/SpellChecker.java b/java/src/com/android/inputmethod/latin/spellcheck/SpellChecker.java new file mode 100644 index 000000000..63c6d69d7 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/spellcheck/SpellChecker.java @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2011 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.spellcheck; + +import android.content.Context; +import android.content.res.Resources; + +import com.android.inputmethod.compat.ArraysCompatUtils; +import com.android.inputmethod.latin.Dictionary; +import com.android.inputmethod.latin.Dictionary.DataType; +import com.android.inputmethod.latin.Dictionary.WordCallback; +import com.android.inputmethod.latin.DictionaryFactory; +import com.android.inputmethod.latin.Utils; +import com.android.inputmethod.latin.WordComposer; + +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; + +/** + * Implements spell checking methods. + */ +public class SpellChecker { + + public final Dictionary mDictionary; + + public SpellChecker(final Context context, final Locale locale) { + final Resources resources = context.getResources(); + final int fallbackResourceId = Utils.getMainDictionaryResourceId(resources); + mDictionary = DictionaryFactory.createDictionaryFromManager(context, locale, + fallbackResourceId); + } + + // Note : this must be reentrant + /** + * Finds out whether a word is in the dictionary or not. + * + * @param text the sequence containing the word to check for. + * @param start the index of the first character of the word in text. + * @param end the index of the next-to-last character in text. + * @return true if the word is in the dictionary, false otherwise. + */ + public boolean isCorrect(final CharSequence text, final int start, final int end) { + return mDictionary.isValidWord(text.subSequence(start, end)); + } + + private static class SuggestionsGatherer implements WordCallback { + private final int DEFAULT_SUGGESTION_LENGTH = 16; + private final List<String> mSuggestions = new LinkedList<String>(); + private int[] mScores = new int[DEFAULT_SUGGESTION_LENGTH]; + private int mLength = 0; + + @Override + synchronized public boolean addWord(char[] word, int wordOffset, int wordLength, int score, + int dicTypeId, DataType dataType) { + if (mLength >= mScores.length) { + final int newLength = mScores.length * 2; + mScores = new int[newLength]; + } + final int positionIndex = ArraysCompatUtils.binarySearch(mScores, 0, mLength, score); + // binarySearch returns the index if the element exists, and -<insertion index> - 1 + // if it doesn't. See documentation for binarySearch. + final int insertionIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1; + System.arraycopy(mScores, insertionIndex, mScores, insertionIndex + 1, + mLength - insertionIndex); + mLength += 1; + mScores[insertionIndex] = score; + mSuggestions.add(insertionIndex, new String(word, wordOffset, wordLength)); + return true; + } + + public List<String> getGatheredSuggestions() { + return mSuggestions; + } + } + + // Note : this must be reentrant + /** + * Gets a list of suggestions for a specific string. + * + * This returns a list of possible corrections for the text passed as an + * arguments. It may split or group words, and even perform grammatical + * analysis. + * + * @param text the sequence containing the word to check for. + * @param start the index of the first character of the word in text. + * @param end the index of the next-to-last character in text. + * @return a list of possible suggestions to replace the text. + */ + public List<String> getSuggestions(final CharSequence text, final int start, final int end) { + final SuggestionsGatherer suggestionsGatherer = new SuggestionsGatherer(); + final WordComposer composer = new WordComposer(); + for (int i = start; i < end; ++i) { + int character = text.charAt(i); + composer.add(character, new int[] { character }, + WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE); + } + mDictionary.getWords(composer, suggestionsGatherer); + return suggestionsGatherer.getGatheredSuggestions(); + } +} |