From 7b90d2c432fd7ffbf0022fac9db921cf39197ac6 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Tue, 20 May 2014 15:07:45 +0900 Subject: Refactor accessibility classes to be more generic Change-Id: Ifad1905f304bccdc39f0d5fbcab8a6353e0b4f76 --- .../KeyboardAccessibilityDelegate.java | 394 +++++++++++++++++++++ .../KeyboardAccessibilityNodeProvider.java | 305 ++++++++++++++++ .../MainKeyboardAccessibilityDelegate.java | 392 -------------------- .../MainKeyboardAccessibilityNodeProvider.java | 304 ---------------- .../inputmethod/keyboard/MainKeyboardView.java | 15 +- 5 files changed, 706 insertions(+), 704 deletions(-) create mode 100644 java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityDelegate.java create mode 100644 java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityNodeProvider.java delete mode 100644 java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java delete mode 100644 java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityNodeProvider.java (limited to 'java/src') diff --git a/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityDelegate.java b/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityDelegate.java new file mode 100644 index 000000000..7bf1c9fb2 --- /dev/null +++ b/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityDelegate.java @@ -0,0 +1,394 @@ +/* + * 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.os.SystemClock; +import android.support.v4.view.AccessibilityDelegateCompat; +import android.support.v4.view.ViewCompat; +import android.support.v4.view.accessibility.AccessibilityEventCompat; +import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; +import android.util.SparseIntArray; +import android.view.MotionEvent; +import android.view.View; +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.KeyboardView; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; + +public class KeyboardAccessibilityDelegate extends AccessibilityDelegateCompat { + /** Map of keyboard modes to resource IDs. */ + private static final SparseIntArray KEYBOARD_MODE_RES_IDS = new SparseIntArray(); + + static { + KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_DATE, R.string.keyboard_mode_date); + KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_DATETIME, R.string.keyboard_mode_date_time); + KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_EMAIL, R.string.keyboard_mode_email); + KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_IM, R.string.keyboard_mode_im); + KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_NUMBER, R.string.keyboard_mode_number); + KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_PHONE, R.string.keyboard_mode_phone); + KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_TEXT, R.string.keyboard_mode_text); + KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_TIME, R.string.keyboard_mode_time); + KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_URL, R.string.keyboard_mode_url); + } + + private final KeyboardView mKeyboardView; + private final KeyDetector mKeyDetector; + private Keyboard mKeyboard; + private KeyboardAccessibilityNodeProvider mAccessibilityNodeProvider; + + private Key mLastHoverKey = null; + + /** + * Inset in pixels to look for keys when the user's finger exits the keyboard area. + */ + private int mEdgeSlop; + + /** The most recently set keyboard mode. */ + private int mLastKeyboardMode = KEYBOARD_IS_HIDDEN; + private static final int KEYBOARD_IS_HIDDEN = -1; + + public KeyboardAccessibilityDelegate(final KeyboardView keyboardView, + final KeyDetector keyDetector) { + super(); + final Context context = keyboardView.getContext(); + mEdgeSlop = context.getResources().getDimensionPixelSize( + R.dimen.config_accessibility_edge_slop); + mKeyboardView = keyboardView; + mKeyDetector = keyDetector; + + // Ensure that the view has an accessibility delegate. + ViewCompat.setAccessibilityDelegate(keyboardView, this); + } + + /** + * Called when the keyboard layout changes. + *

+ * Note: 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(final Keyboard keyboard) { + if (keyboard == null) { + return; + } + if (mAccessibilityNodeProvider != null) { + mAccessibilityNodeProvider.setKeyboard(keyboard); + } + 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. + 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; + } + } + + /** + * Called when the keyboard is hidden and accessibility is enabled. + */ + public void onHideWindow() { + announceKeyboardHidden(); + mLastKeyboardMode = KEYBOARD_IS_HIDDEN; + } + + /** + * Announces which language of keyboard is being displayed. + * + * @param keyboard The new keyboard. + */ + 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 = mKeyboardView.getContext(); + final int modeTextResId = KEYBOARD_MODE_RES_IDS.get(keyboard.mId.mMode); + if (modeTextResId == 0) { + return; + } + 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 = mKeyboardView.getContext().getString(resId); + sendWindowStateChanged(text); + } + + /** + * Announces that the keyboard has been hidden. + */ + private void announceKeyboardHidden() { + final Context context = mKeyboardView.getContext(); + final String text = context.getString(R.string.announce_keyboard_hidden); + + sendWindowStateChanged(text); + } + + /** + * Sends a window state change event with the specified text. + * + * @param text The text to send with the event. + */ + private void sendWindowStateChanged(final String text) { + final AccessibilityEvent stateChange = AccessibilityEvent.obtain( + AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); + mKeyboardView.onInitializeAccessibilityEvent(stateChange); + stateChange.getText().add(text); + stateChange.setContentDescription(null); + + final ViewParent parent = mKeyboardView.getParent(); + if (parent != null) { + parent.requestSendAccessibilityEvent(mKeyboardView, stateChange); + } + } + + /** + * Delegate method for View.getAccessibilityNodeProvider(). This method is called in SDK + * version 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) and higher to obtain the virtual + * node hierarchy provider. + * + * @param host The host view for the provider. + * @return The accessibility node provider for the current keyboard. + */ + @Override + public KeyboardAccessibilityNodeProvider getAccessibilityNodeProvider(final View host) { + return getAccessibilityNodeProvider(); + } + + /** + * Receives hover events when touch exploration is turned on in SDK versions ICS and higher. + * + * @param event The hover event. + * @return {@code true} if the event is handled + */ + public boolean dispatchHoverEvent(final MotionEvent event) { + final int x = (int) event.getX(); + final int y = (int) event.getY(); + final Key previousKey = mLastHoverKey; + final Key key; + + if (pointInView(x, y)) { + key = mKeyDetector.detectHitKey(x, y); + } else { + key = null; + } + mLastHoverKey = key; + + switch (event.getAction()) { + case MotionEvent.ACTION_HOVER_EXIT: + // 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) { + final long downTime = simulateKeyPress(key); + simulateKeyRelease(key, downTime); + } + //$FALL-THROUGH$ + case MotionEvent.ACTION_HOVER_ENTER: + return onHoverKey(key, event); + case MotionEvent.ACTION_HOVER_MOVE: + if (key != previousKey) { + return onTransitionKey(key, previousKey, event); + } + return onHoverKey(key, event); + } + return false; + } + + /** + * @return A lazily-instantiated node provider for this view delegate. + */ + private KeyboardAccessibilityNodeProvider getAccessibilityNodeProvider() { + // Instantiate the provide only when requested. Since the system + // will call this method multiple times it is a good practice to + // cache the provider instance. + if (mAccessibilityNodeProvider == null) { + mAccessibilityNodeProvider = new KeyboardAccessibilityNodeProvider(mKeyboardView); + } + return mAccessibilityNodeProvider; + } + + /** + * Utility method to determine whether the given point, in local coordinates, is inside the + * view, where the area of the view is contracted by the edge slop factor. + * + * @param localX The local x-coordinate. + * @param localY The local y-coordinate. + */ + private boolean pointInView(final int localX, final int localY) { + return (localX >= mEdgeSlop) && (localY >= mEdgeSlop) + && (localX < (mKeyboardView.getWidth() - mEdgeSlop)) + && (localY < (mKeyboardView.getHeight() - mEdgeSlop)); + } + + /** + * 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); + mKeyboardView.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); + mKeyboardView.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. + * + * @param currentKey The currently hovered key. + * @param previousKey The previously hovered key. + * @param event The event that triggered the transition. + * @return {@code true} if the event was handled. + */ + private boolean onTransitionKey(final Key currentKey, final Key previousKey, + final MotionEvent event) { + final int savedAction = event.getAction(); + event.setAction(MotionEvent.ACTION_HOVER_EXIT); + onHoverKey(previousKey, event); + event.setAction(MotionEvent.ACTION_HOVER_ENTER); + onHoverKey(currentKey, event); + event.setAction(MotionEvent.ACTION_HOVER_MOVE); + final boolean handled = onHoverKey(currentKey, event); + event.setAction(savedAction); + return handled; + } + + /** + * Handles a hover event on a key. If {@link Key} extended View, this would be analogous to + * calling View.onHoverEvent(MotionEvent). + * + * @param key The currently hovered key. + * @param event The hover event. + * @return {@code true} if the event was handled. + */ + private boolean onHoverKey(final Key key, final MotionEvent event) { + // Null keys can't receive events. + if (key == null) { + return false; + } + final KeyboardAccessibilityNodeProvider provider = getAccessibilityNodeProvider(); + + switch (event.getAction()) { + case MotionEvent.ACTION_HOVER_ENTER: + provider.sendAccessibilityEventForKey( + key, AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER); + provider.performActionForKey( + key, AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS, null); + break; + case MotionEvent.ACTION_HOVER_EXIT: + provider.sendAccessibilityEventForKey( + key, AccessibilityEventCompat.TYPE_VIEW_HOVER_EXIT); + break; + } + return true; + } +} diff --git a/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityNodeProvider.java b/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityNodeProvider.java new file mode 100644 index 000000000..cddd1c7ed --- /dev/null +++ b/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityNodeProvider.java @@ -0,0 +1,305 @@ +/* + * Copyright (C) 2012 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.graphics.Rect; +import android.os.Bundle; +import android.support.v4.view.ViewCompat; +import android.support.v4.view.accessibility.AccessibilityEventCompat; +import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; +import android.support.v4.view.accessibility.AccessibilityNodeProviderCompat; +import android.support.v4.view.accessibility.AccessibilityRecordCompat; +import android.util.Log; +import android.view.View; +import android.view.accessibility.AccessibilityEvent; +import android.view.inputmethod.EditorInfo; + +import com.android.inputmethod.keyboard.Key; +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.KeyboardView; +import com.android.inputmethod.latin.settings.Settings; +import com.android.inputmethod.latin.settings.SettingsValues; +import com.android.inputmethod.latin.utils.CoordinateUtils; + +import java.util.List; + +/** + * Exposes a virtual view sub-tree for {@link KeyboardView} and generates + * {@link AccessibilityEvent}s for individual {@link Key}s. + *

+ * A virtual sub-tree is composed of imaginary {@link View}s that are reported + * as a part of the view hierarchy for accessibility purposes. This enables + * custom views that draw complex content to report them selves as a tree of + * virtual views, thus conveying their logical structure. + *

+ */ +public final class KeyboardAccessibilityNodeProvider extends AccessibilityNodeProviderCompat { + private static final String TAG = KeyboardAccessibilityNodeProvider.class.getSimpleName(); + private static final int UNDEFINED = Integer.MIN_VALUE; + + private final KeyCodeDescriptionMapper mKeyCodeDescriptionMapper; + private final AccessibilityUtils mAccessibilityUtils; + + /** Temporary rect used to calculate in-screen bounds. */ + private final Rect mTempBoundsInScreen = new Rect(); + + /** The parent view's cached on-screen location. */ + private final int[] mParentLocation = CoordinateUtils.newInstance(); + + /** The virtual view identifier for the focused node. */ + private int mAccessibilityFocusedView = UNDEFINED; + + /** The current keyboard view. */ + private final KeyboardView mKeyboardView; + + /** The current keyboard. */ + private Keyboard mKeyboard; + + public KeyboardAccessibilityNodeProvider(final KeyboardView keyboardView) { + super(); + mKeyCodeDescriptionMapper = KeyCodeDescriptionMapper.getInstance(); + mAccessibilityUtils = AccessibilityUtils.getInstance(); + mKeyboardView = keyboardView; + updateParentLocation(); + + // Since this class is constructed lazily, we might not get a subsequent + // call to setKeyboard() and therefore need to call it now. + setKeyboard(keyboardView.getKeyboard()); + } + + /** + * Sets the keyboard represented by this node provider. + * + * @param keyboard The keyboard that is being set to the keyboard view. + */ + public void setKeyboard(final Keyboard keyboard) { + mKeyboard = keyboard; + } + + private Key getKeyOf(final int virtualViewId) { + if (mKeyboard == null) { + return null; + } + final List sortedKeys = mKeyboard.getSortedKeys(); + // Use a virtual view id as an index of the sorted keys list. + if (virtualViewId >= 0 && virtualViewId < sortedKeys.size()) { + return sortedKeys.get(virtualViewId); + } + return null; + } + + private int getVirtualViewIdOf(final Key key) { + if (mKeyboard == null) { + return View.NO_ID; + } + final List sortedKeys = mKeyboard.getSortedKeys(); + final int size = sortedKeys.size(); + for (int index = 0; index < size; index++) { + if (sortedKeys.get(index) == key) { + // Use an index of the sorted keys list as a virtual view id. + return index; + } + } + return View.NO_ID; + } + + /** + * Creates and populates an {@link AccessibilityEvent} for the specified key + * and event type. + * + * @param key A key on the host keyboard view. + * @param eventType The event type to create. + * @return A populated {@link AccessibilityEvent} for the key. + * @see AccessibilityEvent + */ + public AccessibilityEvent createAccessibilityEvent(final Key key, final int eventType) { + final int virtualViewId = getVirtualViewIdOf(key); + final String keyDescription = getKeyDescription(key); + final AccessibilityEvent event = AccessibilityEvent.obtain(eventType); + event.setPackageName(mKeyboardView.getContext().getPackageName()); + event.setClassName(key.getClass().getName()); + event.setContentDescription(keyDescription); + event.setEnabled(true); + final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event); + record.setSource(mKeyboardView, virtualViewId); + return event; + } + + /** + * Returns an {@link AccessibilityNodeInfoCompat} representing a virtual + * view, i.e. a descendant of the host View, with the given virtualViewId or + * the host View itself if virtualViewId equals to {@link View#NO_ID}. + *

+ * A virtual descendant is an imaginary View that is reported as a part of + * the view hierarchy for accessibility purposes. This enables custom views + * that draw complex content to report them selves as a tree of virtual + * views, thus conveying their logical structure. + *

+ *

+ * The implementer is responsible for obtaining an accessibility node info + * from the pool of reusable instances and setting the desired properties of + * the node info before returning it. + *

+ * + * @param virtualViewId A client defined virtual view id. + * @return A populated {@link AccessibilityNodeInfoCompat} for a virtual descendant or the host + * View. + * @see AccessibilityNodeInfoCompat + */ + @Override + public AccessibilityNodeInfoCompat createAccessibilityNodeInfo(final int virtualViewId) { + if (virtualViewId == UNDEFINED) { + return null; + } + if (virtualViewId == View.NO_ID) { + // We are requested to create an AccessibilityNodeInfo describing + // this View, i.e. the root of the virtual sub-tree. + final AccessibilityNodeInfoCompat rootInfo = + AccessibilityNodeInfoCompat.obtain(mKeyboardView); + ViewCompat.onInitializeAccessibilityNodeInfo(mKeyboardView, rootInfo); + + // Add the virtual children of the root View. + final List sortedKeys = mKeyboard.getSortedKeys(); + final int size = sortedKeys.size(); + for (int index = 0; index < size; index++) { + final Key key = sortedKeys.get(index); + if (key.isSpacer()) { + continue; + } + // Use an index of the sorted keys list as a virtual view id. + rootInfo.addChild(mKeyboardView, index); + } + return rootInfo; + } + + // Find the key that corresponds to the given virtual view id. + final Key key = getKeyOf(virtualViewId); + if (key == null) { + Log.e(TAG, "Invalid virtual view ID: " + virtualViewId); + return null; + } + final String keyDescription = getKeyDescription(key); + final Rect boundsInParent = key.getHitBox(); + + // Calculate the key's in-screen bounds. + mTempBoundsInScreen.set(boundsInParent); + mTempBoundsInScreen.offset( + CoordinateUtils.x(mParentLocation), CoordinateUtils.y(mParentLocation)); + final Rect boundsInScreen = mTempBoundsInScreen; + + // Obtain and initialize an AccessibilityNodeInfo with information about the virtual view. + final AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain(); + info.setPackageName(mKeyboardView.getContext().getPackageName()); + info.setClassName(key.getClass().getName()); + info.setContentDescription(keyDescription); + info.setBoundsInParent(boundsInParent); + info.setBoundsInScreen(boundsInScreen); + info.setParent(mKeyboardView); + info.setSource(mKeyboardView, virtualViewId); + info.setBoundsInScreen(boundsInScreen); + info.setEnabled(true); + info.setVisibleToUser(true); + + if (mAccessibilityFocusedView == virtualViewId) { + info.addAction(AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS); + } else { + info.addAction(AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS); + } + return info; + } + + @Override + public boolean performAction(final int virtualViewId, final int action, + final Bundle arguments) { + final Key key = getKeyOf(virtualViewId); + if (key == null) { + return false; + } + return performActionForKey(key, action, arguments); + } + + /** + * Performs the specified accessibility action for the given key. + * + * @param key The on which to perform the action. + * @param action The action to perform. + * @param arguments The action's arguments. + * @return The result of performing the action, or false if the action is not supported. + */ + boolean performActionForKey(final Key key, final int action, final Bundle arguments) { + final int virtualViewId = getVirtualViewIdOf(key); + + switch (action) { + case AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS: + if (mAccessibilityFocusedView == virtualViewId) { + return false; + } + mAccessibilityFocusedView = virtualViewId; + sendAccessibilityEventForKey( + key, AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUSED); + return true; + case AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS: + if (mAccessibilityFocusedView != virtualViewId) { + return false; + } + mAccessibilityFocusedView = UNDEFINED; + sendAccessibilityEventForKey( + key, AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED); + return true; + default: + return false; + } + } + + /** + * Sends an accessibility event for the given {@link Key}. + * + * @param key The key that's sending the event. + * @param eventType The type of event to send. + */ + void sendAccessibilityEventForKey(final Key key, final int eventType) { + final AccessibilityEvent event = createAccessibilityEvent(key, eventType); + mAccessibilityUtils.requestSendAccessibilityEvent(event); + } + + /** + * Returns the context-specific description for a {@link Key}. + * + * @param key The key to describe. + * @return The context-specific description of the key. + */ + private String getKeyDescription(final Key key) { + final EditorInfo editorInfo = mKeyboard.mId.mEditorInfo; + final boolean shouldObscure = mAccessibilityUtils.shouldObscureInput(editorInfo); + final SettingsValues currentSettings = Settings.getInstance().getCurrent(); + final String keyCodeDescription = mKeyCodeDescriptionMapper.getDescriptionForKey( + mKeyboardView.getContext(), mKeyboard, key, shouldObscure); + if (currentSettings.isWordSeparator(key.getCode())) { + return mAccessibilityUtils.getAutoCorrectionDescription( + keyCodeDescription, shouldObscure); + } else { + return keyCodeDescription; + } + } + + /** + * Updates the parent's on-screen location. + */ + private void updateParentLocation() { + mKeyboardView.getLocationOnScreen(mParentLocation); + } +} diff --git a/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java b/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java deleted file mode 100644 index 10929424b..000000000 --- a/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java +++ /dev/null @@ -1,392 +0,0 @@ -/* - * 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.os.SystemClock; -import android.support.v4.view.AccessibilityDelegateCompat; -import android.support.v4.view.ViewCompat; -import android.support.v4.view.accessibility.AccessibilityEventCompat; -import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; -import android.util.SparseIntArray; -import android.view.MotionEvent; -import android.view.View; -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.latin.R; -import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; - -public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelegateCompat { - /** Map of keyboard modes to resource IDs. */ - private static final SparseIntArray KEYBOARD_MODE_RES_IDS = new SparseIntArray(); - - static { - KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_DATE, R.string.keyboard_mode_date); - KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_DATETIME, R.string.keyboard_mode_date_time); - KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_EMAIL, R.string.keyboard_mode_email); - KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_IM, R.string.keyboard_mode_im); - KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_NUMBER, R.string.keyboard_mode_number); - KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_PHONE, R.string.keyboard_mode_phone); - KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_TEXT, R.string.keyboard_mode_text); - KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_TIME, R.string.keyboard_mode_time); - KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_URL, R.string.keyboard_mode_url); - } - - private final MainKeyboardView mView; - private Keyboard mKeyboard; - private MainKeyboardAccessibilityNodeProvider mAccessibilityNodeProvider; - - private Key mLastHoverKey = null; - - /** - * Inset in pixels to look for keys when the user's finger exits the keyboard area. - */ - private int mEdgeSlop; - - /** The most recently set keyboard mode. */ - private int mLastKeyboardMode = KEYBOARD_IS_HIDDEN; - private static final int KEYBOARD_IS_HIDDEN = -1; - - public MainKeyboardAccessibilityDelegate(final MainKeyboardView view) { - final Context context = view.getContext(); - mEdgeSlop = context.getResources().getDimensionPixelSize( - R.dimen.config_accessibility_edge_slop); - mView = view; - - // Ensure that the view has an accessibility delegate. - ViewCompat.setAccessibilityDelegate(view, this); - } - - /** - * Called when the keyboard layout changes. - *

- * Note: 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(final Keyboard keyboard) { - if (keyboard == null) { - return; - } - if (mAccessibilityNodeProvider != null) { - mAccessibilityNodeProvider.setKeyboard(keyboard); - } - 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. - 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; - } - } - - /** - * Called when the keyboard is hidden and accessibility is enabled. - */ - public void onHideWindow() { - announceKeyboardHidden(); - mLastKeyboardMode = KEYBOARD_IS_HIDDEN; - } - - /** - * Announces which language of keyboard is being displayed. - * - * @param keyboard The new keyboard. - */ - 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 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); - } - - /** - * Announces that the keyboard has been hidden. - */ - private void announceKeyboardHidden() { - final Context context = mView.getContext(); - final String text = context.getString(R.string.announce_keyboard_hidden); - - sendWindowStateChanged(text); - } - - /** - * Sends a window state change event with the specified text. - * - * @param text The text to send with the event. - */ - private void sendWindowStateChanged(final String text) { - final AccessibilityEvent stateChange = AccessibilityEvent.obtain( - AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); - mView.onInitializeAccessibilityEvent(stateChange); - stateChange.getText().add(text); - stateChange.setContentDescription(null); - - final ViewParent parent = mView.getParent(); - if (parent != null) { - parent.requestSendAccessibilityEvent(mView, stateChange); - } - } - - /** - * Delegate method for View.getAccessibilityNodeProvider(). This method is called in SDK - * version 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) and higher to obtain the virtual - * node hierarchy provider. - * - * @param host The host view for the provider. - * @return The accessibility node provider for the current keyboard. - */ - @Override - public MainKeyboardAccessibilityNodeProvider getAccessibilityNodeProvider(final View host) { - return getAccessibilityNodeProvider(); - } - - /** - * 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 event - * is hovering. - * @return {@code true} if the event is handled - */ - public boolean dispatchHoverEvent(final MotionEvent event, final KeyDetector keyDetector) { - final int x = (int) event.getX(); - final int y = (int) event.getY(); - final Key previousKey = mLastHoverKey; - final Key key; - - if (pointInView(x, y)) { - key = keyDetector.detectHitKey(x, y); - } else { - key = null; - } - mLastHoverKey = key; - - switch (event.getAction()) { - case MotionEvent.ACTION_HOVER_EXIT: - // 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) { - final long downTime = simulateKeyPress(key); - simulateKeyRelease(key, downTime); - } - //$FALL-THROUGH$ - case MotionEvent.ACTION_HOVER_ENTER: - return onHoverKey(key, event); - case MotionEvent.ACTION_HOVER_MOVE: - if (key != previousKey) { - return onTransitionKey(key, previousKey, event); - } - return onHoverKey(key, event); - } - return false; - } - - /** - * @return A lazily-instantiated node provider for this view delegate. - */ - private MainKeyboardAccessibilityNodeProvider getAccessibilityNodeProvider() { - // Instantiate the provide only when requested. Since the system - // will call this method multiple times it is a good practice to - // cache the provider instance. - if (mAccessibilityNodeProvider == null) { - mAccessibilityNodeProvider = new MainKeyboardAccessibilityNodeProvider(mView); - } - return mAccessibilityNodeProvider; - } - - /** - * Utility method to determine whether the given point, in local coordinates, is inside the - * view, where the area of the view is contracted by the edge slop factor. - * - * @param localX The local x-coordinate. - * @param localY The local y-coordinate. - */ - private boolean pointInView(final int localX, final int localY) { - return (localX >= mEdgeSlop) && (localY >= mEdgeSlop) - && (localX < (mView.getWidth() - mEdgeSlop)) - && (localY < (mView.getHeight() - mEdgeSlop)); - } - - /** - * 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. - * - * @param currentKey The currently hovered key. - * @param previousKey The previously hovered key. - * @param event The event that triggered the transition. - * @return {@code true} if the event was handled. - */ - private boolean onTransitionKey(final Key currentKey, final Key previousKey, - final MotionEvent event) { - final int savedAction = event.getAction(); - event.setAction(MotionEvent.ACTION_HOVER_EXIT); - onHoverKey(previousKey, event); - event.setAction(MotionEvent.ACTION_HOVER_ENTER); - onHoverKey(currentKey, event); - event.setAction(MotionEvent.ACTION_HOVER_MOVE); - final boolean handled = onHoverKey(currentKey, event); - event.setAction(savedAction); - return handled; - } - - /** - * Handles a hover event on a key. If {@link Key} extended View, this would be analogous to - * calling View.onHoverEvent(MotionEvent). - * - * @param key The currently hovered key. - * @param event The hover event. - * @return {@code true} if the event was handled. - */ - private boolean onHoverKey(final Key key, final MotionEvent event) { - // Null keys can't receive events. - if (key == null) { - return false; - } - final MainKeyboardAccessibilityNodeProvider provider = getAccessibilityNodeProvider(); - - switch (event.getAction()) { - case MotionEvent.ACTION_HOVER_ENTER: - provider.sendAccessibilityEventForKey( - key, AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER); - provider.performActionForKey( - key, AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS, null); - break; - case MotionEvent.ACTION_HOVER_EXIT: - provider.sendAccessibilityEventForKey( - key, AccessibilityEventCompat.TYPE_VIEW_HOVER_EXIT); - break; - } - return true; - } -} diff --git a/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityNodeProvider.java b/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityNodeProvider.java deleted file mode 100644 index f69d316c9..000000000 --- a/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityNodeProvider.java +++ /dev/null @@ -1,304 +0,0 @@ -/* - * Copyright (C) 2012 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.graphics.Rect; -import android.os.Bundle; -import android.support.v4.view.ViewCompat; -import android.support.v4.view.accessibility.AccessibilityEventCompat; -import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; -import android.support.v4.view.accessibility.AccessibilityNodeProviderCompat; -import android.support.v4.view.accessibility.AccessibilityRecordCompat; -import android.util.Log; -import android.view.View; -import android.view.accessibility.AccessibilityEvent; -import android.view.inputmethod.EditorInfo; - -import com.android.inputmethod.keyboard.Key; -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.keyboard.KeyboardView; -import com.android.inputmethod.latin.settings.Settings; -import com.android.inputmethod.latin.settings.SettingsValues; -import com.android.inputmethod.latin.utils.CoordinateUtils; - -import java.util.List; - -/** - * Exposes a virtual view sub-tree for {@link KeyboardView} and generates - * {@link AccessibilityEvent}s for individual {@link Key}s. - *

- * A virtual sub-tree is composed of imaginary {@link View}s that are reported - * as a part of the view hierarchy for accessibility purposes. This enables - * custom views that draw complex content to report them selves as a tree of - * virtual views, thus conveying their logical structure. - *

- */ -public final class MainKeyboardAccessibilityNodeProvider extends AccessibilityNodeProviderCompat { - private static final String TAG = MainKeyboardAccessibilityNodeProvider.class.getSimpleName(); - private static final int UNDEFINED = Integer.MIN_VALUE; - - private final KeyCodeDescriptionMapper mKeyCodeDescriptionMapper; - private final AccessibilityUtils mAccessibilityUtils; - - /** Temporary rect used to calculate in-screen bounds. */ - private final Rect mTempBoundsInScreen = new Rect(); - - /** The parent view's cached on-screen location. */ - private final int[] mParentLocation = CoordinateUtils.newInstance(); - - /** The virtual view identifier for the focused node. */ - private int mAccessibilityFocusedView = UNDEFINED; - - /** The current keyboard view. */ - private final KeyboardView mKeyboardView; - - /** The current keyboard. */ - private Keyboard mKeyboard; - - public MainKeyboardAccessibilityNodeProvider(final KeyboardView keyboardView) { - mKeyCodeDescriptionMapper = KeyCodeDescriptionMapper.getInstance(); - mAccessibilityUtils = AccessibilityUtils.getInstance(); - mKeyboardView = keyboardView; - updateParentLocation(); - - // Since this class is constructed lazily, we might not get a subsequent - // call to setKeyboard() and therefore need to call it now. - setKeyboard(keyboardView.getKeyboard()); - } - - /** - * Sets the keyboard represented by this node provider. - * - * @param keyboard The keyboard that is being set to the keyboard view. - */ - public void setKeyboard(final Keyboard keyboard) { - mKeyboard = keyboard; - } - - private Key getKeyOf(final int virtualViewId) { - if (mKeyboard == null) { - return null; - } - final List sortedKeys = mKeyboard.getSortedKeys(); - // Use a virtual view id as an index of the sorted keys list. - if (virtualViewId >= 0 && virtualViewId < sortedKeys.size()) { - return sortedKeys.get(virtualViewId); - } - return null; - } - - private int getVirtualViewIdOf(final Key key) { - if (mKeyboard == null) { - return View.NO_ID; - } - final List sortedKeys = mKeyboard.getSortedKeys(); - final int size = sortedKeys.size(); - for (int index = 0; index < size; index++) { - if (sortedKeys.get(index) == key) { - // Use an index of the sorted keys list as a virtual view id. - return index; - } - } - return View.NO_ID; - } - - /** - * Creates and populates an {@link AccessibilityEvent} for the specified key - * and event type. - * - * @param key A key on the host keyboard view. - * @param eventType The event type to create. - * @return A populated {@link AccessibilityEvent} for the key. - * @see AccessibilityEvent - */ - public AccessibilityEvent createAccessibilityEvent(final Key key, final int eventType) { - final int virtualViewId = getVirtualViewIdOf(key); - final String keyDescription = getKeyDescription(key); - final AccessibilityEvent event = AccessibilityEvent.obtain(eventType); - event.setPackageName(mKeyboardView.getContext().getPackageName()); - event.setClassName(key.getClass().getName()); - event.setContentDescription(keyDescription); - event.setEnabled(true); - final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event); - record.setSource(mKeyboardView, virtualViewId); - return event; - } - - /** - * Returns an {@link AccessibilityNodeInfoCompat} representing a virtual - * view, i.e. a descendant of the host View, with the given virtualViewId or - * the host View itself if virtualViewId equals to {@link View#NO_ID}. - *

- * A virtual descendant is an imaginary View that is reported as a part of - * the view hierarchy for accessibility purposes. This enables custom views - * that draw complex content to report them selves as a tree of virtual - * views, thus conveying their logical structure. - *

- *

- * The implementer is responsible for obtaining an accessibility node info - * from the pool of reusable instances and setting the desired properties of - * the node info before returning it. - *

- * - * @param virtualViewId A client defined virtual view id. - * @return A populated {@link AccessibilityNodeInfoCompat} for a virtual descendant or the host - * View. - * @see AccessibilityNodeInfoCompat - */ - @Override - public AccessibilityNodeInfoCompat createAccessibilityNodeInfo(final int virtualViewId) { - if (virtualViewId == UNDEFINED) { - return null; - } - if (virtualViewId == View.NO_ID) { - // We are requested to create an AccessibilityNodeInfo describing - // this View, i.e. the root of the virtual sub-tree. - final AccessibilityNodeInfoCompat rootInfo = - AccessibilityNodeInfoCompat.obtain(mKeyboardView); - ViewCompat.onInitializeAccessibilityNodeInfo(mKeyboardView, rootInfo); - - // Add the virtual children of the root View. - final List sortedKeys = mKeyboard.getSortedKeys(); - final int size = sortedKeys.size(); - for (int index = 0; index < size; index++) { - final Key key = sortedKeys.get(index); - if (key.isSpacer()) { - continue; - } - // Use an index of the sorted keys list as a virtual view id. - rootInfo.addChild(mKeyboardView, index); - } - return rootInfo; - } - - // Find the key that corresponds to the given virtual view id. - final Key key = getKeyOf(virtualViewId); - if (key == null) { - Log.e(TAG, "Invalid virtual view ID: " + virtualViewId); - return null; - } - final String keyDescription = getKeyDescription(key); - final Rect boundsInParent = key.getHitBox(); - - // Calculate the key's in-screen bounds. - mTempBoundsInScreen.set(boundsInParent); - mTempBoundsInScreen.offset( - CoordinateUtils.x(mParentLocation), CoordinateUtils.y(mParentLocation)); - final Rect boundsInScreen = mTempBoundsInScreen; - - // Obtain and initialize an AccessibilityNodeInfo with information about the virtual view. - final AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain(); - info.setPackageName(mKeyboardView.getContext().getPackageName()); - info.setClassName(key.getClass().getName()); - info.setContentDescription(keyDescription); - info.setBoundsInParent(boundsInParent); - info.setBoundsInScreen(boundsInScreen); - info.setParent(mKeyboardView); - info.setSource(mKeyboardView, virtualViewId); - info.setBoundsInScreen(boundsInScreen); - info.setEnabled(true); - info.setVisibleToUser(true); - - if (mAccessibilityFocusedView == virtualViewId) { - info.addAction(AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS); - } else { - info.addAction(AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS); - } - return info; - } - - @Override - public boolean performAction(final int virtualViewId, final int action, - final Bundle arguments) { - final Key key = getKeyOf(virtualViewId); - if (key == null) { - return false; - } - return performActionForKey(key, action, arguments); - } - - /** - * Performs the specified accessibility action for the given key. - * - * @param key The on which to perform the action. - * @param action The action to perform. - * @param arguments The action's arguments. - * @return The result of performing the action, or false if the action is not supported. - */ - boolean performActionForKey(final Key key, final int action, final Bundle arguments) { - final int virtualViewId = getVirtualViewIdOf(key); - - switch (action) { - case AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS: - if (mAccessibilityFocusedView == virtualViewId) { - return false; - } - mAccessibilityFocusedView = virtualViewId; - sendAccessibilityEventForKey( - key, AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUSED); - return true; - case AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS: - if (mAccessibilityFocusedView != virtualViewId) { - return false; - } - mAccessibilityFocusedView = UNDEFINED; - sendAccessibilityEventForKey( - key, AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED); - return true; - default: - return false; - } - } - - /** - * Sends an accessibility event for the given {@link Key}. - * - * @param key The key that's sending the event. - * @param eventType The type of event to send. - */ - void sendAccessibilityEventForKey(final Key key, final int eventType) { - final AccessibilityEvent event = createAccessibilityEvent(key, eventType); - mAccessibilityUtils.requestSendAccessibilityEvent(event); - } - - /** - * Returns the context-specific description for a {@link Key}. - * - * @param key The key to describe. - * @return The context-specific description of the key. - */ - private String getKeyDescription(final Key key) { - final EditorInfo editorInfo = mKeyboard.mId.mEditorInfo; - final boolean shouldObscure = mAccessibilityUtils.shouldObscureInput(editorInfo); - final SettingsValues currentSettings = Settings.getInstance().getCurrent(); - final String keyCodeDescription = mKeyCodeDescriptionMapper.getDescriptionForKey( - mKeyboardView.getContext(), mKeyboard, key, shouldObscure); - if (currentSettings.isWordSeparator(key.getCode())) { - return mAccessibilityUtils.getAutoCorrectionDescription( - keyCodeDescription, shouldObscure); - } else { - return keyCodeDescription; - } - } - - /** - * Updates the parent's on-screen location. - */ - private void updateParentLocation() { - mKeyboardView.getLocationOnScreen(mParentLocation); - } -} diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java index 26b6dd06a..a8a23db22 100644 --- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java @@ -38,7 +38,7 @@ import android.view.inputmethod.InputMethodSubtype; import android.widget.TextView; import com.android.inputmethod.accessibility.AccessibilityUtils; -import com.android.inputmethod.accessibility.MainKeyboardAccessibilityDelegate; +import com.android.inputmethod.accessibility.KeyboardAccessibilityDelegate; import com.android.inputmethod.annotations.ExternallyReferenced; import com.android.inputmethod.keyboard.internal.DrawingHandler; import com.android.inputmethod.keyboard.internal.DrawingPreviewPlacerView; @@ -170,7 +170,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack private final DrawingHandler mDrawingHandler = new DrawingHandler(this); - private final MainKeyboardAccessibilityDelegate mAccessibilityDelegate; + private final KeyboardAccessibilityDelegate mAccessibilityDelegate; public MainKeyboardView(final Context context, final AttributeSet attrs) { this(context, attrs, R.attr.mainKeyboardViewStyle); @@ -269,7 +269,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack mLanguageOnSpacebarHorizontalMargin = (int)getResources().getDimension( R.dimen.config_language_on_spacebar_horizontal_margin); - mAccessibilityDelegate = new MainKeyboardAccessibilityDelegate(this); + mAccessibilityDelegate = new KeyboardAccessibilityDelegate(this, mKeyDetector); } @Override @@ -773,12 +773,11 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack */ @Override public boolean dispatchHoverEvent(final MotionEvent event) { - if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()) { - return mAccessibilityDelegate.dispatchHoverEvent(event, mKeyDetector); + if (!AccessibilityUtils.getInstance().isTouchExplorationEnabled()) { + // Reflection doesn't support calling superclass methods. + return false; } - - // Reflection doesn't support calling superclass methods. - return false; + return mAccessibilityDelegate.dispatchHoverEvent(event); } public void updateShortcutKey(final boolean available) { -- cgit v1.2.3-83-g751a