aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/accessibility
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/accessibility')
-rw-r--r--java/src/com/android/inputmethod/accessibility/KeyCodeDescriptionMapper.java42
-rw-r--r--java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityDelegate.java232
-rw-r--r--java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityNodeProvider.java (renamed from java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityNodeProvider.java)7
-rw-r--r--java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java234
4 files changed, 275 insertions, 240 deletions
diff --git a/java/src/com/android/inputmethod/accessibility/KeyCodeDescriptionMapper.java b/java/src/com/android/inputmethod/accessibility/KeyCodeDescriptionMapper.java
index 0499a3456..46caef625 100644
--- a/java/src/com/android/inputmethod/accessibility/KeyCodeDescriptionMapper.java
+++ b/java/src/com/android/inputmethod/accessibility/KeyCodeDescriptionMapper.java
@@ -17,6 +17,7 @@
package com.android.inputmethod.accessibility;
import android.content.Context;
+import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseIntArray;
@@ -27,21 +28,18 @@ import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardId;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.R;
-import com.android.inputmethod.latin.utils.CollectionUtils;
-import java.util.HashMap;
+import java.util.Locale;
public final class KeyCodeDescriptionMapper {
private static final String TAG = KeyCodeDescriptionMapper.class.getSimpleName();
+ private static final String SPOKEN_EMOJI_RESOURCE_NAME_FORMAT = "spoken_emoji_%04X";
// The resource ID of the string spoken for obscured keys
private static final int OBSCURED_KEY_RES_ID = R.string.spoken_description_dot;
private static KeyCodeDescriptionMapper sInstance = new KeyCodeDescriptionMapper();
- // Map of key labels to spoken description resource IDs
- private final HashMap<CharSequence, Integer> mKeyLabelMap = CollectionUtils.newHashMap();
-
// Sparse array of spoken description resource IDs indexed by key codes
private final SparseIntArray mKeyCodeMap;
@@ -114,17 +112,12 @@ public final class KeyCodeDescriptionMapper {
return getDescriptionForActionKey(context, keyboard, key);
}
- if (!TextUtils.isEmpty(key.getLabel())) {
- final String label = key.getLabel().trim();
-
- // First, attempt to map the label to a pre-defined description.
- if (mKeyLabelMap.containsKey(label)) {
- return context.getString(mKeyLabelMap.get(label));
- }
+ if (code == Constants.CODE_OUTPUT_TEXT) {
+ return key.getOutputText();
}
// Just attempt to speak the description.
- if (key.getCode() != Constants.CODE_UNSPECIFIED) {
+ if (code != Constants.CODE_UNSPECIFIED) {
return getDescriptionForKeyCode(context, keyboard, key, shouldObscure);
}
return null;
@@ -139,7 +132,7 @@ public final class KeyCodeDescriptionMapper {
* @param keyboard The keyboard on which the key resides.
* @return a character sequence describing the action performed by pressing the key
*/
- private String getDescriptionForSwitchAlphaSymbol(final Context context,
+ private static String getDescriptionForSwitchAlphaSymbol(final Context context,
final Keyboard keyboard) {
final KeyboardId keyboardId = keyboard.mId;
final int elementId = keyboardId.mElementId;
@@ -177,7 +170,8 @@ public final class KeyCodeDescriptionMapper {
* @param keyboard The keyboard on which the key resides.
* @return A context-sensitive description of the "Shift" key.
*/
- private String getDescriptionForShiftKey(final Context context, final Keyboard keyboard) {
+ private static String getDescriptionForShiftKey(final Context context,
+ final Keyboard keyboard) {
final KeyboardId keyboardId = keyboard.mId;
final int elementId = keyboardId.mElementId;
final int resId;
@@ -211,7 +205,7 @@ public final class KeyCodeDescriptionMapper {
* @param key The key to describe.
* @return Returns a context-sensitive description of the "Enter" action key.
*/
- private String getDescriptionForActionKey(final Context context, final Keyboard keyboard,
+ private static String getDescriptionForActionKey(final Context context, final Keyboard keyboard,
final Key key) {
final KeyboardId keyboardId = keyboard.mId;
final int actionId = keyboardId.imeAction();
@@ -280,6 +274,13 @@ public final class KeyCodeDescriptionMapper {
if (mKeyCodeMap.indexOfKey(code) >= 0) {
return context.getString(mKeyCodeMap.get(code));
}
+ final int spokenEmojiId = getSpokenDescriptionId(
+ context, code, SPOKEN_EMOJI_RESOURCE_NAME_FORMAT);
+ if (spokenEmojiId != 0) {
+ final String spokenEmoji = context.getString(spokenEmojiId);
+ mKeyCodeMap.append(code, spokenEmojiId);
+ return spokenEmoji;
+ }
if (isDefinedNonCtrl) {
return Character.toString((char) code);
}
@@ -288,4 +289,13 @@ public final class KeyCodeDescriptionMapper {
}
return context.getString(R.string.spoken_description_unknown, code);
}
+
+ private static int getSpokenDescriptionId(final Context context, final int code,
+ final String resourceNameFormat) {
+ final String resourceName = String.format(Locale.ROOT, resourceNameFormat, code);
+ final Resources resources = context.getResources();
+ final String packageName = resources.getResourcePackageName(
+ R.string.spoken_description_unknown);
+ return resources.getIdentifier(resourceName, "string", packageName);
+ }
}
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..eed40f4a9
--- /dev/null
+++ b/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityDelegate.java
@@ -0,0 +1,232 @@
+/*
+ * 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.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.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.KeyboardView;
+
+public class KeyboardAccessibilityDelegate<KV extends KeyboardView>
+ extends AccessibilityDelegateCompat {
+ protected final KV mKeyboardView;
+ protected final KeyDetector mKeyDetector;
+ private Keyboard mKeyboard;
+ private KeyboardAccessibilityNodeProvider mAccessibilityNodeProvider;
+ private Key mLastHoverKey;
+
+ public KeyboardAccessibilityDelegate(final KV keyboardView, final KeyDetector keyDetector) {
+ super();
+ mKeyboardView = keyboardView;
+ mKeyDetector = keyDetector;
+
+ // Ensure that the view has an accessibility delegate.
+ ViewCompat.setAccessibilityDelegate(keyboardView, this);
+ }
+
+ /**
+ * Called when the keyboard layout changes.
+ * <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(final Keyboard keyboard) {
+ if (keyboard == null) {
+ return;
+ }
+ if (mAccessibilityNodeProvider != null) {
+ mAccessibilityNodeProvider.setKeyboard(keyboard);
+ }
+ mKeyboard = keyboard;
+ }
+
+ protected Keyboard getKeyboard() {
+ return mKeyboard;
+ }
+
+ /**
+ * Sends a window state change event with the specified text.
+ *
+ * @param text The text to send with the event.
+ */
+ protected 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();
+ }
+
+ /**
+ * @return A lazily-instantiated node provider for this view delegate.
+ */
+ protected 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;
+ }
+
+ /**
+ * 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 = mKeyDetector.detectHitKey(x, y);
+ 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;
+ }
+
+ /**
+ * 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/MainKeyboardAccessibilityNodeProvider.java b/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityNodeProvider.java
index f69d316c9..cddd1c7ed 100644
--- a/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityNodeProvider.java
+++ b/java/src/com/android/inputmethod/accessibility/KeyboardAccessibilityNodeProvider.java
@@ -47,8 +47,8 @@ import java.util.List;
* virtual views, thus conveying their logical structure.
* </p>
*/
-public final class MainKeyboardAccessibilityNodeProvider extends AccessibilityNodeProviderCompat {
- private static final String TAG = MainKeyboardAccessibilityNodeProvider.class.getSimpleName();
+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;
@@ -69,7 +69,8 @@ public final class MainKeyboardAccessibilityNodeProvider extends AccessibilityNo
/** The current keyboard. */
private Keyboard mKeyboard;
- public MainKeyboardAccessibilityNodeProvider(final KeyboardView keyboardView) {
+ public KeyboardAccessibilityNodeProvider(final KeyboardView keyboardView) {
+ super();
mKeyCodeDescriptionMapper = KeyCodeDescriptionMapper.getInstance();
mAccessibilityUtils = AccessibilityUtils.getInstance();
mKeyboardView = keyboardView;
diff --git a/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java b/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java
index 10929424b..c114551c8 100644
--- a/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java
+++ b/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2011 The Android Open Source Project
+ * Copyright (C) 2014 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.
@@ -18,15 +18,10 @@ 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;
@@ -36,7 +31,8 @@ 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 {
+public final class MainKeyboardAccessibilityDelegate
+ extends KeyboardAccessibilityDelegate<MainKeyboardView> {
/** Map of keyboard modes to resource IDs. */
private static final SparseIntArray KEYBOARD_MODE_RES_IDS = new SparseIntArray();
@@ -52,48 +48,26 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
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);
+ public MainKeyboardAccessibilityDelegate(final MainKeyboardView mainKeyboardView,
+ final KeyDetector keyDetector) {
+ super(mainKeyboardView, keyDetector);
}
/**
- * Called when the keyboard layout changes.
- * <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.
+ * {@inheritDoc}
*/
+ @Override
public void setKeyboard(final Keyboard keyboard) {
if (keyboard == null) {
return;
}
- if (mAccessibilityNodeProvider != null) {
- mAccessibilityNodeProvider.setKeyboard(keyboard);
- }
- final Keyboard lastKeyboard = mKeyboard;
+ final Keyboard lastKeyboard = getKeyboard();
+ super.setKeyboard(keyboard);
final int lastKeyboardMode = mLastKeyboardMode;
- mKeyboard = keyboard;
mLastKeyboardMode = keyboard.mId.mMode;
// Since this method is called even when accessibility is off, make sure
@@ -144,7 +118,7 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
* @param keyboard The new keyboard.
*/
private void announceKeyboardMode(final Keyboard keyboard) {
- final Context context = mView.getContext();
+ final Context context = mKeyboardView.getContext();
final int modeTextResId = KEYBOARD_MODE_RES_IDS.get(keyboard.mId.mMode);
if (modeTextResId == 0) {
return;
@@ -194,7 +168,7 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
default:
return;
}
- final String text = mView.getContext().getString(resId);
+ final String text = mKeyboardView.getContext().getString(resId);
sendWindowStateChanged(text);
}
@@ -202,191 +176,9 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
* Announces that the keyboard has been hidden.
*/
private void announceKeyboardHidden() {
- final Context context = mView.getContext();
+ 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);
- 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 <code>event</code>
- * 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;
- }
}