diff options
Diffstat (limited to 'java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java')
-rw-r--r-- | java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java | 265 |
1 files changed, 145 insertions, 120 deletions
diff --git a/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java b/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java index 73896dfd3..15f79f3de 100644 --- a/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java +++ b/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java @@ -17,7 +17,7 @@ package com.android.inputmethod.accessibility; import android.content.Context; -import android.inputmethodservice.InputMethodService; +import android.os.SystemClock; import android.support.v4.view.AccessibilityDelegateCompat; import android.support.v4.view.ViewCompat; import android.support.v4.view.accessibility.AccessibilityEventCompat; @@ -29,11 +29,12 @@ import android.view.ViewParent; import android.view.accessibility.AccessibilityEvent; import com.android.inputmethod.keyboard.Key; +import com.android.inputmethod.keyboard.KeyDetector; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.KeyboardId; import com.android.inputmethod.keyboard.MainKeyboardView; -import com.android.inputmethod.keyboard.PointerTracker; import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateCompat { private static final AccessibleKeyboardViewProxy sInstance = new AccessibleKeyboardViewProxy(); @@ -53,8 +54,8 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_URL, R.string.keyboard_mode_url); } - private InputMethodService mInputMethod; private MainKeyboardView mView; + private Keyboard mKeyboard; private AccessibilityEntityProvider mAccessibilityNodeProvider; private Key mLastHoverKey = null; @@ -65,10 +66,11 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp private int mEdgeSlop; /** The most recently set keyboard mode. */ - private int mLastKeyboardMode; + private int mLastKeyboardMode = KEYBOARD_IS_HIDDEN; + private static final int KEYBOARD_IS_HIDDEN = -1; - public static void init(final InputMethodService inputMethod) { - sInstance.initInternal(inputMethod); + public static void init(final Context context) { + sInstance.initInternal(context); } public static AccessibleKeyboardViewProxy getInstance() { @@ -79,10 +81,9 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp // Not publicly instantiable. } - private void initInternal(final InputMethodService inputMethod) { - mInputMethod = inputMethod; - mEdgeSlop = inputMethod.getResources().getDimensionPixelSize( - R.dimen.accessibility_edge_slop); + private void initInternal(final Context context) { + mEdgeSlop = context.getResources().getDimensionPixelSize( + R.dimen.config_accessibility_edge_slop); } /** @@ -104,6 +105,10 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp return; } mAccessibilityNodeProvider.setView(view); + + // Since this class is constructed lazily, we might not get a subsequent + // call to setKeyboard() and therefore need to call it now. + setKeyboard(view.getKeyboard()); } /** @@ -111,24 +116,40 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp * <p> * <b>Note:</b> This method will be called even if accessibility is not * enabled. + * @param keyboard The keyboard that is being set to the wrapping view. */ - public void setKeyboard() { - if (mView == null) { + public void setKeyboard(final Keyboard keyboard) { + if (keyboard == null) { return; } if (mAccessibilityNodeProvider != null) { - mAccessibilityNodeProvider.setKeyboard(); + mAccessibilityNodeProvider.setKeyboard(keyboard); } - final int keyboardMode = mView.getKeyboard().mId.mMode; + final Keyboard lastKeyboard = mKeyboard; + final int lastKeyboardMode = mLastKeyboardMode; + mKeyboard = keyboard; + mLastKeyboardMode = keyboard.mId.mMode; // Since this method is called even when accessibility is off, make sure - // to check the state before announcing anything. Also, don't announce - // changes within the same mode. - if (AccessibilityUtils.getInstance().isAccessibilityEnabled() - && (mLastKeyboardMode != keyboardMode)) { - announceKeyboardMode(keyboardMode); + // to check the state before announcing anything. + if (!AccessibilityUtils.getInstance().isAccessibilityEnabled()) { + return; + } + // Announce the language name only when the language is changed. + if (lastKeyboard == null || !keyboard.mId.mSubtype.equals(lastKeyboard.mId.mSubtype)) { + announceKeyboardLanguage(keyboard); + return; + } + // Announce the mode only when the mode is changed. + if (keyboard.mId.mMode != lastKeyboardMode) { + announceKeyboardMode(keyboard); + return; + } + // Announce the keyboard type only when the type is changed. + if (keyboard.mId.mElementId != lastKeyboard.mId.mElementId) { + announceKeyboardType(keyboard, lastKeyboard); + return; } - mLastKeyboardMode = keyboardMode; } /** @@ -139,23 +160,78 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp return; } announceKeyboardHidden(); - mLastKeyboardMode = -1; + mLastKeyboardMode = KEYBOARD_IS_HIDDEN; } /** - * Announces which type of keyboard is being displayed. If the keyboard type - * is unknown, no announcement is made. + * Announces which language of keyboard is being displayed. * - * @param mode The new keyboard mode. + * @param keyboard The new keyboard. */ - private void announceKeyboardMode(int mode) { - final int resId = KEYBOARD_MODE_RES_IDS.get(mode); - if (resId == 0) { + private void announceKeyboardLanguage(final Keyboard keyboard) { + final String languageText = SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale( + keyboard.mId.mSubtype); + sendWindowStateChanged(languageText); + } + + /** + * Announces which type of keyboard is being displayed. + * If the keyboard type is unknown, no announcement is made. + * + * @param keyboard The new keyboard. + */ + private void announceKeyboardMode(final Keyboard keyboard) { + final Context context = mView.getContext(); + final int modeTextResId = KEYBOARD_MODE_RES_IDS.get(keyboard.mId.mMode); + if (modeTextResId == 0) { return; } - final Context context = mView.getContext(); - final String keyboardMode = context.getString(resId); - final String text = context.getString(R.string.announce_keyboard_mode, keyboardMode); + final String modeText = context.getString(modeTextResId); + final String text = context.getString(R.string.announce_keyboard_mode, modeText); + sendWindowStateChanged(text); + } + + /** + * Announces which type of keyboard is being displayed. + * + * @param keyboard The new keyboard. + * @param lastKeyboard The last keyboard. + */ + private void announceKeyboardType(final Keyboard keyboard, final Keyboard lastKeyboard) { + final int lastElementId = lastKeyboard.mId.mElementId; + final int resId; + switch (keyboard.mId.mElementId) { + case KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED: + case KeyboardId.ELEMENT_ALPHABET: + if (lastElementId == KeyboardId.ELEMENT_ALPHABET + || lastElementId == KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED) { + return; + } + resId = R.string.spoken_description_mode_alpha; + break; + case KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED: + resId = R.string.spoken_description_shiftmode_on; + break; + case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCK_SHIFTED: + case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCKED: + resId = R.string.spoken_description_shiftmode_locked; + break; + case KeyboardId.ELEMENT_SYMBOLS: + resId = R.string.spoken_description_mode_symbol; + break; + case KeyboardId.ELEMENT_SYMBOLS_SHIFTED: + resId = R.string.spoken_description_mode_symbol_shift; + break; + case KeyboardId.ELEMENT_PHONE: + resId = R.string.spoken_description_mode_phone; + break; + case KeyboardId.ELEMENT_PHONE_SYMBOLS: + resId = R.string.spoken_description_mode_phone_shift; + break; + default: + return; + } + final String text = mView.getContext().getString(resId); sendWindowStateChanged(text); } @@ -204,25 +280,14 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp } /** - * Intercepts touch events before dispatch when touch exploration is turned on in ICS and - * higher. - * - * @param event The motion event being dispatched. - * @return {@code true} if the event is handled - */ - public boolean dispatchTouchEvent(final MotionEvent event) { - // To avoid accidental key presses during touch exploration, always drop - // touch events generated by the user. - return false; - } - - /** * Receives hover events when touch exploration is turned on in SDK versions ICS and higher. * * @param event The hover event. + * @param keyDetector The {@link KeyDetector} to determine on which key the <code>event</code> + * is hovering. * @return {@code true} if the event is handled */ - public boolean dispatchHoverEvent(final MotionEvent event, final PointerTracker tracker) { + public boolean dispatchHoverEvent(final MotionEvent event, final KeyDetector keyDetector) { if (mView == null) { return false; } @@ -233,7 +298,7 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp final Key key; if (pointInView(x, y)) { - key = tracker.getKeyOn(x, y); + key = keyDetector.detectHitKey(x, y); } else { key = null; } @@ -244,7 +309,8 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp // Make sure we're not getting an EXIT event because the user slid // off the keyboard area, then force a key press. if (key != null) { - getAccessibilityNodeProvider().simulateKeyPress(key); + final long downTime = simulateKeyPress(key); + simulateKeyRelease(key, downTime); } //$FALL-THROUGH$ case MotionEvent.ACTION_HOVER_ENTER: @@ -266,7 +332,7 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp // will call this method multiple times it is a good practice to // cache the provider instance. if (mAccessibilityNodeProvider == null) { - mAccessibilityNodeProvider = new AccessibilityEntityProvider(mView, mInputMethod); + mAccessibilityNodeProvider = new AccessibilityEntityProvider(mView); } return mAccessibilityNodeProvider; } @@ -285,6 +351,38 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp } /** + * Simulates a key press by injecting touch an event into the keyboard view. + * This avoids the complexity of trackers and listeners within the keyboard. + * + * @param key The key to press. + */ + private long simulateKeyPress(final Key key) { + final int x = key.getHitBox().centerX(); + final int y = key.getHitBox().centerY(); + final long downTime = SystemClock.uptimeMillis(); + final MotionEvent downEvent = MotionEvent.obtain( + downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0); + mView.onTouchEvent(downEvent); + downEvent.recycle(); + return downTime; + } + + /** + * Simulates a key release by injecting touch an event into the keyboard view. + * This avoids the complexity of trackers and listeners within the keyboard. + * + * @param key The key to release. + */ + private void simulateKeyRelease(final Key key, final long downTime) { + final int x = key.getHitBox().centerX(); + final int y = key.getHitBox().centerY(); + final MotionEvent upEvent = MotionEvent.obtain( + downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0); + mView.onTouchEvent(upEvent); + upEvent.recycle(); + } + + /** * Simulates a transition between two {@link Key}s by sending a HOVER_EXIT on the previous key, * a HOVER_ENTER on the current key, and a HOVER_MOVE on the current key. * @@ -335,77 +433,4 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp } return true; } - - /** - * Notifies the user of changes in the keyboard shift state. - */ - public void notifyShiftState() { - if (mView == null) { - return; - } - - final Keyboard keyboard = mView.getKeyboard(); - final KeyboardId keyboardId = keyboard.mId; - final int elementId = keyboardId.mElementId; - final Context context = mView.getContext(); - final CharSequence text; - - switch (elementId) { - case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCK_SHIFTED: - case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCKED: - text = context.getText(R.string.spoken_description_shiftmode_locked); - break; - case KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED: - case KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED: - case KeyboardId.ELEMENT_SYMBOLS_SHIFTED: - text = context.getText(R.string.spoken_description_shiftmode_on); - break; - default: - text = context.getText(R.string.spoken_description_shiftmode_off); - } - AccessibilityUtils.getInstance().announceForAccessibility(mView, text); - } - - /** - * Notifies the user of changes in the keyboard symbols state. - */ - public void notifySymbolsState() { - if (mView == null) { - return; - } - - final Keyboard keyboard = mView.getKeyboard(); - final Context context = mView.getContext(); - final KeyboardId keyboardId = keyboard.mId; - final int elementId = keyboardId.mElementId; - final int resId; - - switch (elementId) { - case KeyboardId.ELEMENT_ALPHABET: - case KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED: - case KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED: - case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCK_SHIFTED: - case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCKED: - resId = R.string.spoken_description_mode_alpha; - break; - case KeyboardId.ELEMENT_SYMBOLS: - case KeyboardId.ELEMENT_SYMBOLS_SHIFTED: - resId = R.string.spoken_description_mode_symbol; - break; - case KeyboardId.ELEMENT_PHONE: - resId = R.string.spoken_description_mode_phone; - break; - case KeyboardId.ELEMENT_PHONE_SYMBOLS: - resId = R.string.spoken_description_mode_phone_shift; - break; - default: - resId = -1; - } - - if (resId < 0) { - return; - } - final String text = context.getString(resId); - AccessibilityUtils.getInstance().announceForAccessibility(mView, text); - } } |