Support fine grained hover event handling
This CL also adds visual feedback of hovering key in accessibility mode. Bug: 12491371 Change-Id: I7b1f28a2e421ca60b2738ed94e7bdb406f75039b
This commit is contained in:
parent
d3642a2a58
commit
639e431fa2
1 changed files with 113 additions and 57 deletions
|
@ -17,11 +17,11 @@
|
||||||
package com.android.inputmethod.accessibility;
|
package com.android.inputmethod.accessibility;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.SystemClock;
|
|
||||||
import android.support.v4.view.AccessibilityDelegateCompat;
|
import android.support.v4.view.AccessibilityDelegateCompat;
|
||||||
import android.support.v4.view.ViewCompat;
|
import android.support.v4.view.ViewCompat;
|
||||||
import android.support.v4.view.accessibility.AccessibilityEventCompat;
|
import android.support.v4.view.accessibility.AccessibilityEventCompat;
|
||||||
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
|
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewParent;
|
import android.view.ViewParent;
|
||||||
|
@ -31,6 +31,7 @@ import com.android.inputmethod.keyboard.Key;
|
||||||
import com.android.inputmethod.keyboard.KeyDetector;
|
import com.android.inputmethod.keyboard.KeyDetector;
|
||||||
import com.android.inputmethod.keyboard.Keyboard;
|
import com.android.inputmethod.keyboard.Keyboard;
|
||||||
import com.android.inputmethod.keyboard.KeyboardView;
|
import com.android.inputmethod.keyboard.KeyboardView;
|
||||||
|
import com.android.inputmethod.keyboard.PointerTracker;
|
||||||
|
|
||||||
public class KeyboardAccessibilityDelegate<KV extends KeyboardView>
|
public class KeyboardAccessibilityDelegate<KV extends KeyboardView>
|
||||||
extends AccessibilityDelegateCompat {
|
extends AccessibilityDelegateCompat {
|
||||||
|
@ -66,10 +67,18 @@ public class KeyboardAccessibilityDelegate<KV extends KeyboardView>
|
||||||
mKeyboard = keyboard;
|
mKeyboard = keyboard;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Keyboard getKeyboard() {
|
protected final Keyboard getKeyboard() {
|
||||||
return mKeyboard;
|
return mKeyboard;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected final void setCurrentHoverKey(final Key key) {
|
||||||
|
mCurrentHoverKey = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final Key getCurrentHoverKey() {
|
||||||
|
return mCurrentHoverKey;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a window state change event with the specified string resource id.
|
* Sends a window state change event with the specified string resource id.
|
||||||
*
|
*
|
||||||
|
@ -127,6 +136,19 @@ public class KeyboardAccessibilityDelegate<KV extends KeyboardView>
|
||||||
return mAccessibilityNodeProvider;
|
return mAccessibilityNodeProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a key that a hover event is on.
|
||||||
|
*
|
||||||
|
* @param event The hover event.
|
||||||
|
* @return key The key that the <code>event</code> is on.
|
||||||
|
*/
|
||||||
|
protected final Key getHoverKey(final MotionEvent event) {
|
||||||
|
final int actionIndex = event.getActionIndex();
|
||||||
|
final int x = (int)event.getX(actionIndex);
|
||||||
|
final int y = (int)event.getY(actionIndex);
|
||||||
|
return mKeyDetector.detectHitKey(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Receives hover events when touch exploration is turned on in SDK versions ICS and higher.
|
* Receives hover events when touch exploration is turned on in SDK versions ICS and higher.
|
||||||
*
|
*
|
||||||
|
@ -134,77 +156,107 @@ public class KeyboardAccessibilityDelegate<KV extends KeyboardView>
|
||||||
* @return {@code true} if the event is handled.
|
* @return {@code true} if the event is handled.
|
||||||
*/
|
*/
|
||||||
public boolean onHoverEvent(final MotionEvent event) {
|
public boolean onHoverEvent(final MotionEvent event) {
|
||||||
final int x = (int) event.getX();
|
switch (event.getActionMasked()) {
|
||||||
final int y = (int) event.getY();
|
|
||||||
final Key previousKey = mCurrentHoverKey;
|
|
||||||
final Key key = mKeyDetector.detectHitKey(x, y);
|
|
||||||
mCurrentHoverKey = 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);
|
|
||||||
onHoverExitKey(key);
|
|
||||||
}
|
|
||||||
mCurrentHoverKey = null;
|
|
||||||
break;
|
|
||||||
case MotionEvent.ACTION_HOVER_ENTER:
|
case MotionEvent.ACTION_HOVER_ENTER:
|
||||||
if (key != null) {
|
onHoverEnter(event);
|
||||||
onHoverEnterKey(key);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case MotionEvent.ACTION_HOVER_MOVE:
|
case MotionEvent.ACTION_HOVER_MOVE:
|
||||||
if (key != previousKey) {
|
onHoverMove(event);
|
||||||
if (previousKey != null) {
|
break;
|
||||||
onHoverExitKey(previousKey);
|
case MotionEvent.ACTION_HOVER_EXIT:
|
||||||
}
|
onHoverExit(event);
|
||||||
if (key != null) {
|
break;
|
||||||
onHoverEnterKey(key);
|
default:
|
||||||
}
|
Log.w(getClass().getSimpleName(), "Unknown hover event: " + event);
|
||||||
}
|
|
||||||
if (key != null) {
|
|
||||||
onHoverMoveKey(key);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simulates a key press by injecting touch an event into the keyboard view.
|
* Process {@link MotionEvent#ACTION_HOVER_ENTER} event.
|
||||||
* This avoids the complexity of trackers and listeners within the keyboard.
|
|
||||||
*
|
*
|
||||||
* @param key The key to press.
|
* @param event A hover enter event.
|
||||||
* @return the event time of the simulated key press.
|
|
||||||
*/
|
*/
|
||||||
private long simulateKeyPress(final Key key) {
|
protected void onHoverEnter(final MotionEvent event) {
|
||||||
final int x = key.getHitBox().centerX();
|
final Key key = getHoverKey(event);
|
||||||
final int y = key.getHitBox().centerY();
|
if (key != null) {
|
||||||
final long downTime = SystemClock.uptimeMillis();
|
onHoverEnterKey(key);
|
||||||
final MotionEvent downEvent = MotionEvent.obtain(
|
}
|
||||||
downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0);
|
setCurrentHoverKey(key);
|
||||||
mKeyboardView.onTouchEvent(downEvent);
|
|
||||||
downEvent.recycle();
|
|
||||||
return downTime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simulates a key release by injecting touch an event into the keyboard view.
|
* Process {@link MotionEvent#ACTION_HOVER_MOVE} event.
|
||||||
* This avoids the complexity of trackers and listeners within the keyboard.
|
|
||||||
*
|
*
|
||||||
* @param key The key to release.
|
* @param event A hover move event.
|
||||||
* @param downTime the event time of the key press.
|
|
||||||
*/
|
*/
|
||||||
private void simulateKeyRelease(final Key key, final long downTime) {
|
protected void onHoverMove(final MotionEvent event) {
|
||||||
final int x = key.getHitBox().centerX();
|
final Key previousKey = getCurrentHoverKey();
|
||||||
final int y = key.getHitBox().centerY();
|
final Key key = getHoverKey(event);
|
||||||
final MotionEvent upEvent = MotionEvent.obtain(
|
if (key != previousKey) {
|
||||||
downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0);
|
if (previousKey != null) {
|
||||||
mKeyboardView.onTouchEvent(upEvent);
|
onHoverExitKey(previousKey);
|
||||||
upEvent.recycle();
|
}
|
||||||
|
if (key != null) {
|
||||||
|
onHoverEnterKey(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (key != null) {
|
||||||
|
onHoverMoveKey(key);
|
||||||
|
}
|
||||||
|
setCurrentHoverKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process {@link MotionEvent#ACTION_HOVER_EXIT} event.
|
||||||
|
*
|
||||||
|
* @param event A hover exit event.
|
||||||
|
*/
|
||||||
|
protected void onHoverExit(final MotionEvent event) {
|
||||||
|
final Key key = getHoverKey(event);
|
||||||
|
// 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) {
|
||||||
|
simulateTouchEvent(MotionEvent.ACTION_DOWN, event);
|
||||||
|
simulateTouchEvent(MotionEvent.ACTION_UP, event);
|
||||||
|
onHoverExitKey(key);
|
||||||
|
}
|
||||||
|
setCurrentHoverKey(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simulating a touch event by injecting a synthesized touch event into {@link PointerTracker}.
|
||||||
|
*
|
||||||
|
* @param touchAction The action of the synthesizing touch event.
|
||||||
|
* @param hoverEvent The base hover event from that the touch event is synthesized.
|
||||||
|
*/
|
||||||
|
protected void simulateTouchEvent(final int touchAction, final MotionEvent hoverEvent) {
|
||||||
|
final MotionEvent touchEvent = synthesizeTouchEvent(touchAction, hoverEvent);
|
||||||
|
final int actionIndex = touchEvent.getActionIndex();
|
||||||
|
final int pointerId = touchEvent.getPointerId(actionIndex);
|
||||||
|
final PointerTracker tracker = PointerTracker.getPointerTracker(pointerId);
|
||||||
|
tracker.processMotionEvent(touchEvent, mKeyDetector);
|
||||||
|
touchEvent.recycle();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synthesize a touch event from a hover event.
|
||||||
|
*
|
||||||
|
* @param touchAction The action of the synthesizing touch event.
|
||||||
|
* @param event The base hover event from that the touch event is synthesized.
|
||||||
|
* @return The synthesized touch event of <code>touchAction</code> that has pointer information
|
||||||
|
* of <code>event</code>.
|
||||||
|
*/
|
||||||
|
protected static MotionEvent synthesizeTouchEvent(final int touchAction,
|
||||||
|
final MotionEvent event) {
|
||||||
|
final long downTime = event.getDownTime();
|
||||||
|
final long eventTime = event.getEventTime();
|
||||||
|
final int actionIndex = event.getActionIndex();
|
||||||
|
final float x = event.getX(actionIndex);
|
||||||
|
final float y = event.getY(actionIndex);
|
||||||
|
final int pointerId = event.getPointerId(actionIndex);
|
||||||
|
return MotionEvent.obtain(downTime, eventTime, touchAction, x, y, pointerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -213,6 +265,8 @@ public class KeyboardAccessibilityDelegate<KV extends KeyboardView>
|
||||||
* @param key The currently hovered key.
|
* @param key The currently hovered key.
|
||||||
*/
|
*/
|
||||||
protected void onHoverEnterKey(final Key key) {
|
protected void onHoverEnterKey(final Key key) {
|
||||||
|
key.onPressed();
|
||||||
|
mKeyboardView.invalidateKey(key);
|
||||||
final KeyboardAccessibilityNodeProvider provider = getAccessibilityNodeProvider();
|
final KeyboardAccessibilityNodeProvider provider = getAccessibilityNodeProvider();
|
||||||
provider.sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER);
|
provider.sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER);
|
||||||
provider.performActionForKey(key, AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS);
|
provider.performActionForKey(key, AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS);
|
||||||
|
@ -231,6 +285,8 @@ public class KeyboardAccessibilityDelegate<KV extends KeyboardView>
|
||||||
* @param key The currently hovered key.
|
* @param key The currently hovered key.
|
||||||
*/
|
*/
|
||||||
protected void onHoverExitKey(final Key key) {
|
protected void onHoverExitKey(final Key key) {
|
||||||
|
key.onReleased();
|
||||||
|
mKeyboardView.invalidateKey(key);
|
||||||
final KeyboardAccessibilityNodeProvider provider = getAccessibilityNodeProvider();
|
final KeyboardAccessibilityNodeProvider provider = getAccessibilityNodeProvider();
|
||||||
provider.sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_VIEW_HOVER_EXIT);
|
provider.sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_VIEW_HOVER_EXIT);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue