Refactor accessibility classes to be more generic

Change-Id: Ifad1905f304bccdc39f0d5fbcab8a6353e0b4f76
main
Tadashi G. Takaoka 2014-05-20 15:07:45 +09:00
parent 5b7c85ec32
commit 7b90d2c432
3 changed files with 39 additions and 37 deletions

View File

@ -32,11 +32,11 @@ 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.KeyboardId; import com.android.inputmethod.keyboard.KeyboardId;
import com.android.inputmethod.keyboard.MainKeyboardView; import com.android.inputmethod.keyboard.KeyboardView;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelegateCompat { public class KeyboardAccessibilityDelegate extends AccessibilityDelegateCompat {
/** Map of keyboard modes to resource IDs. */ /** Map of keyboard modes to resource IDs. */
private static final SparseIntArray KEYBOARD_MODE_RES_IDS = new SparseIntArray(); private static final SparseIntArray KEYBOARD_MODE_RES_IDS = new SparseIntArray();
@ -52,9 +52,10 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_URL, R.string.keyboard_mode_url); KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_URL, R.string.keyboard_mode_url);
} }
private final MainKeyboardView mView; private final KeyboardView mKeyboardView;
private final KeyDetector mKeyDetector;
private Keyboard mKeyboard; private Keyboard mKeyboard;
private MainKeyboardAccessibilityNodeProvider mAccessibilityNodeProvider; private KeyboardAccessibilityNodeProvider mAccessibilityNodeProvider;
private Key mLastHoverKey = null; private Key mLastHoverKey = null;
@ -67,14 +68,17 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
private int mLastKeyboardMode = KEYBOARD_IS_HIDDEN; private int mLastKeyboardMode = KEYBOARD_IS_HIDDEN;
private static final int KEYBOARD_IS_HIDDEN = -1; private static final int KEYBOARD_IS_HIDDEN = -1;
public MainKeyboardAccessibilityDelegate(final MainKeyboardView view) { public KeyboardAccessibilityDelegate(final KeyboardView keyboardView,
final Context context = view.getContext(); final KeyDetector keyDetector) {
super();
final Context context = keyboardView.getContext();
mEdgeSlop = context.getResources().getDimensionPixelSize( mEdgeSlop = context.getResources().getDimensionPixelSize(
R.dimen.config_accessibility_edge_slop); R.dimen.config_accessibility_edge_slop);
mView = view; mKeyboardView = keyboardView;
mKeyDetector = keyDetector;
// Ensure that the view has an accessibility delegate. // Ensure that the view has an accessibility delegate.
ViewCompat.setAccessibilityDelegate(view, this); ViewCompat.setAccessibilityDelegate(keyboardView, this);
} }
/** /**
@ -144,7 +148,7 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
* @param keyboard The new keyboard. * @param keyboard The new keyboard.
*/ */
private void announceKeyboardMode(final Keyboard 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); final int modeTextResId = KEYBOARD_MODE_RES_IDS.get(keyboard.mId.mMode);
if (modeTextResId == 0) { if (modeTextResId == 0) {
return; return;
@ -194,7 +198,7 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
default: default:
return; return;
} }
final String text = mView.getContext().getString(resId); final String text = mKeyboardView.getContext().getString(resId);
sendWindowStateChanged(text); sendWindowStateChanged(text);
} }
@ -202,7 +206,7 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
* Announces that the keyboard has been hidden. * Announces that the keyboard has been hidden.
*/ */
private void announceKeyboardHidden() { private void announceKeyboardHidden() {
final Context context = mView.getContext(); final Context context = mKeyboardView.getContext();
final String text = context.getString(R.string.announce_keyboard_hidden); final String text = context.getString(R.string.announce_keyboard_hidden);
sendWindowStateChanged(text); sendWindowStateChanged(text);
@ -216,13 +220,13 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
private void sendWindowStateChanged(final String text) { private void sendWindowStateChanged(final String text) {
final AccessibilityEvent stateChange = AccessibilityEvent.obtain( final AccessibilityEvent stateChange = AccessibilityEvent.obtain(
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
mView.onInitializeAccessibilityEvent(stateChange); mKeyboardView.onInitializeAccessibilityEvent(stateChange);
stateChange.getText().add(text); stateChange.getText().add(text);
stateChange.setContentDescription(null); stateChange.setContentDescription(null);
final ViewParent parent = mView.getParent(); final ViewParent parent = mKeyboardView.getParent();
if (parent != null) { if (parent != null) {
parent.requestSendAccessibilityEvent(mView, stateChange); parent.requestSendAccessibilityEvent(mKeyboardView, stateChange);
} }
} }
@ -235,7 +239,7 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
* @return The accessibility node provider for the current keyboard. * @return The accessibility node provider for the current keyboard.
*/ */
@Override @Override
public MainKeyboardAccessibilityNodeProvider getAccessibilityNodeProvider(final View host) { public KeyboardAccessibilityNodeProvider getAccessibilityNodeProvider(final View host) {
return getAccessibilityNodeProvider(); return getAccessibilityNodeProvider();
} }
@ -243,18 +247,16 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
* 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.
* *
* @param event The hover event. * @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 * @return {@code true} if the event is handled
*/ */
public boolean dispatchHoverEvent(final MotionEvent event, final KeyDetector keyDetector) { public boolean dispatchHoverEvent(final MotionEvent event) {
final int x = (int) event.getX(); final int x = (int) event.getX();
final int y = (int) event.getY(); final int y = (int) event.getY();
final Key previousKey = mLastHoverKey; final Key previousKey = mLastHoverKey;
final Key key; final Key key;
if (pointInView(x, y)) { if (pointInView(x, y)) {
key = keyDetector.detectHitKey(x, y); key = mKeyDetector.detectHitKey(x, y);
} else { } else {
key = null; key = null;
} }
@ -283,12 +285,12 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
/** /**
* @return A lazily-instantiated node provider for this view delegate. * @return A lazily-instantiated node provider for this view delegate.
*/ */
private MainKeyboardAccessibilityNodeProvider getAccessibilityNodeProvider() { private KeyboardAccessibilityNodeProvider getAccessibilityNodeProvider() {
// Instantiate the provide only when requested. Since the system // Instantiate the provide only when requested. Since the system
// will call this method multiple times it is a good practice to // will call this method multiple times it is a good practice to
// cache the provider instance. // cache the provider instance.
if (mAccessibilityNodeProvider == null) { if (mAccessibilityNodeProvider == null) {
mAccessibilityNodeProvider = new MainKeyboardAccessibilityNodeProvider(mView); mAccessibilityNodeProvider = new KeyboardAccessibilityNodeProvider(mKeyboardView);
} }
return mAccessibilityNodeProvider; return mAccessibilityNodeProvider;
} }
@ -302,8 +304,8 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
*/ */
private boolean pointInView(final int localX, final int localY) { private boolean pointInView(final int localX, final int localY) {
return (localX >= mEdgeSlop) && (localY >= mEdgeSlop) return (localX >= mEdgeSlop) && (localY >= mEdgeSlop)
&& (localX < (mView.getWidth() - mEdgeSlop)) && (localX < (mKeyboardView.getWidth() - mEdgeSlop))
&& (localY < (mView.getHeight() - mEdgeSlop)); && (localY < (mKeyboardView.getHeight() - mEdgeSlop));
} }
/** /**
@ -318,7 +320,7 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
final long downTime = SystemClock.uptimeMillis(); final long downTime = SystemClock.uptimeMillis();
final MotionEvent downEvent = MotionEvent.obtain( final MotionEvent downEvent = MotionEvent.obtain(
downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0); downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0);
mView.onTouchEvent(downEvent); mKeyboardView.onTouchEvent(downEvent);
downEvent.recycle(); downEvent.recycle();
return downTime; return downTime;
} }
@ -334,7 +336,7 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
final int y = key.getHitBox().centerY(); final int y = key.getHitBox().centerY();
final MotionEvent upEvent = MotionEvent.obtain( final MotionEvent upEvent = MotionEvent.obtain(
downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0); downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0);
mView.onTouchEvent(upEvent); mKeyboardView.onTouchEvent(upEvent);
upEvent.recycle(); upEvent.recycle();
} }
@ -373,7 +375,7 @@ public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelega
if (key == null) { if (key == null) {
return false; return false;
} }
final MainKeyboardAccessibilityNodeProvider provider = getAccessibilityNodeProvider(); final KeyboardAccessibilityNodeProvider provider = getAccessibilityNodeProvider();
switch (event.getAction()) { switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER: case MotionEvent.ACTION_HOVER_ENTER:

View File

@ -47,8 +47,8 @@ import java.util.List;
* virtual views, thus conveying their logical structure. * virtual views, thus conveying their logical structure.
* </p> * </p>
*/ */
public final class MainKeyboardAccessibilityNodeProvider extends AccessibilityNodeProviderCompat { public final class KeyboardAccessibilityNodeProvider extends AccessibilityNodeProviderCompat {
private static final String TAG = MainKeyboardAccessibilityNodeProvider.class.getSimpleName(); private static final String TAG = KeyboardAccessibilityNodeProvider.class.getSimpleName();
private static final int UNDEFINED = Integer.MIN_VALUE; private static final int UNDEFINED = Integer.MIN_VALUE;
private final KeyCodeDescriptionMapper mKeyCodeDescriptionMapper; private final KeyCodeDescriptionMapper mKeyCodeDescriptionMapper;
@ -69,7 +69,8 @@ public final class MainKeyboardAccessibilityNodeProvider extends AccessibilityNo
/** The current keyboard. */ /** The current keyboard. */
private Keyboard mKeyboard; private Keyboard mKeyboard;
public MainKeyboardAccessibilityNodeProvider(final KeyboardView keyboardView) { public KeyboardAccessibilityNodeProvider(final KeyboardView keyboardView) {
super();
mKeyCodeDescriptionMapper = KeyCodeDescriptionMapper.getInstance(); mKeyCodeDescriptionMapper = KeyCodeDescriptionMapper.getInstance();
mAccessibilityUtils = AccessibilityUtils.getInstance(); mAccessibilityUtils = AccessibilityUtils.getInstance();
mKeyboardView = keyboardView; mKeyboardView = keyboardView;

View File

@ -38,7 +38,7 @@ import android.view.inputmethod.InputMethodSubtype;
import android.widget.TextView; import android.widget.TextView;
import com.android.inputmethod.accessibility.AccessibilityUtils; 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.annotations.ExternallyReferenced;
import com.android.inputmethod.keyboard.internal.DrawingHandler; import com.android.inputmethod.keyboard.internal.DrawingHandler;
import com.android.inputmethod.keyboard.internal.DrawingPreviewPlacerView; import com.android.inputmethod.keyboard.internal.DrawingPreviewPlacerView;
@ -170,7 +170,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
private final DrawingHandler mDrawingHandler = private final DrawingHandler mDrawingHandler =
new DrawingHandler(this); new DrawingHandler(this);
private final MainKeyboardAccessibilityDelegate mAccessibilityDelegate; private final KeyboardAccessibilityDelegate mAccessibilityDelegate;
public MainKeyboardView(final Context context, final AttributeSet attrs) { public MainKeyboardView(final Context context, final AttributeSet attrs) {
this(context, attrs, R.attr.mainKeyboardViewStyle); this(context, attrs, R.attr.mainKeyboardViewStyle);
@ -269,7 +269,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
mLanguageOnSpacebarHorizontalMargin = (int)getResources().getDimension( mLanguageOnSpacebarHorizontalMargin = (int)getResources().getDimension(
R.dimen.config_language_on_spacebar_horizontal_margin); R.dimen.config_language_on_spacebar_horizontal_margin);
mAccessibilityDelegate = new MainKeyboardAccessibilityDelegate(this); mAccessibilityDelegate = new KeyboardAccessibilityDelegate(this, mKeyDetector);
} }
@Override @Override
@ -773,13 +773,12 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
*/ */
@Override @Override
public boolean dispatchHoverEvent(final MotionEvent event) { public boolean dispatchHoverEvent(final MotionEvent event) {
if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()) { if (!AccessibilityUtils.getInstance().isTouchExplorationEnabled()) {
return mAccessibilityDelegate.dispatchHoverEvent(event, mKeyDetector);
}
// Reflection doesn't support calling superclass methods. // Reflection doesn't support calling superclass methods.
return false; return false;
} }
return mAccessibilityDelegate.dispatchHoverEvent(event);
}
public void updateShortcutKey(final boolean available) { public void updateShortcutKey(final boolean available) {
final Keyboard keyboard = getKeyboard(); final Keyboard keyboard = getKeyboard();