2012-02-28 18:01:40 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2012-05-15 22:11:12 +00:00
|
|
|
import android.os.Bundle;
|
2012-02-28 18:01:40 +00:00
|
|
|
import android.support.v4.view.ViewCompat;
|
2012-05-15 22:11:12 +00:00
|
|
|
import android.support.v4.view.accessibility.AccessibilityEventCompat;
|
2012-02-28 18:01:40 +00:00
|
|
|
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;
|
2014-11-06 11:29:29 +00:00
|
|
|
import com.android.inputmethod.latin.common.CoordinateUtils;
|
2013-09-27 21:15:53 +00:00
|
|
|
import com.android.inputmethod.latin.settings.Settings;
|
|
|
|
import com.android.inputmethod.latin.settings.SettingsValues;
|
2012-02-28 18:01:40 +00:00
|
|
|
|
2014-04-22 02:26:08 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2012-02-28 18:01:40 +00:00
|
|
|
/**
|
|
|
|
* Exposes a virtual view sub-tree for {@link KeyboardView} and generates
|
|
|
|
* {@link AccessibilityEvent}s for individual {@link Key}s.
|
|
|
|
* <p>
|
|
|
|
* 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.
|
|
|
|
* </p>
|
|
|
|
*/
|
2014-06-24 22:14:24 +00:00
|
|
|
final class KeyboardAccessibilityNodeProvider<KV extends KeyboardView>
|
|
|
|
extends AccessibilityNodeProviderCompat {
|
2014-05-20 06:07:45 +00:00
|
|
|
private static final String TAG = KeyboardAccessibilityNodeProvider.class.getSimpleName();
|
2014-06-13 07:33:54 +00:00
|
|
|
|
|
|
|
// From {@link android.view.accessibility.AccessibilityNodeInfo#UNDEFINED_ITEM_ID}.
|
|
|
|
private static final int UNDEFINED = Integer.MAX_VALUE;
|
2012-02-28 18:01:40 +00:00
|
|
|
|
|
|
|
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. */
|
2012-11-28 08:25:23 +00:00
|
|
|
private final int[] mParentLocation = CoordinateUtils.newInstance();
|
2012-02-28 18:01:40 +00:00
|
|
|
|
2012-05-15 22:11:12 +00:00
|
|
|
/** The virtual view identifier for the focused node. */
|
|
|
|
private int mAccessibilityFocusedView = UNDEFINED;
|
|
|
|
|
2014-06-20 21:53:40 +00:00
|
|
|
/** The virtual view identifier for the hovering node. */
|
|
|
|
private int mHoveringNodeId = UNDEFINED;
|
|
|
|
|
2014-06-24 22:35:10 +00:00
|
|
|
/** The keyboard view to provide an accessibility node info. */
|
2014-06-24 22:14:24 +00:00
|
|
|
private final KV mKeyboardView;
|
|
|
|
/** The accessibility delegate. */
|
|
|
|
private final KeyboardAccessibilityDelegate<KV> mDelegate;
|
2012-05-30 22:39:48 +00:00
|
|
|
|
2014-04-22 02:26:08 +00:00
|
|
|
/** The current keyboard. */
|
|
|
|
private Keyboard mKeyboard;
|
|
|
|
|
2014-06-24 22:14:24 +00:00
|
|
|
public KeyboardAccessibilityNodeProvider(final KV keyboardView,
|
|
|
|
final KeyboardAccessibilityDelegate<KV> delegate) {
|
2014-05-20 06:07:45 +00:00
|
|
|
super();
|
2012-02-28 18:01:40 +00:00
|
|
|
mKeyCodeDescriptionMapper = KeyCodeDescriptionMapper.getInstance();
|
|
|
|
mAccessibilityUtils = AccessibilityUtils.getInstance();
|
2012-05-30 22:39:48 +00:00
|
|
|
mKeyboardView = keyboardView;
|
2014-06-24 22:14:24 +00:00
|
|
|
mDelegate = delegate;
|
2012-06-05 18:02:54 +00:00
|
|
|
|
|
|
|
// Since this class is constructed lazily, we might not get a subsequent
|
|
|
|
// call to setKeyboard() and therefore need to call it now.
|
2014-04-22 02:26:08 +00:00
|
|
|
setKeyboard(keyboardView.getKeyboard());
|
2012-06-05 18:02:54 +00:00
|
|
|
}
|
2012-05-30 22:39:48 +00:00
|
|
|
|
2012-06-05 18:02:54 +00:00
|
|
|
/**
|
|
|
|
* Sets the keyboard represented by this node provider.
|
2014-04-22 02:26:08 +00:00
|
|
|
*
|
|
|
|
* @param keyboard The keyboard that is being set to the keyboard view.
|
2012-06-05 18:02:54 +00:00
|
|
|
*/
|
2014-04-22 02:26:08 +00:00
|
|
|
public void setKeyboard(final Keyboard keyboard) {
|
|
|
|
mKeyboard = keyboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Key getKeyOf(final int virtualViewId) {
|
|
|
|
if (mKeyboard == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final List<Key> 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<Key> 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;
|
2012-02-28 18:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2013-01-06 02:10:27 +00:00
|
|
|
public AccessibilityEvent createAccessibilityEvent(final Key key, final int eventType) {
|
2014-04-22 02:26:08 +00:00
|
|
|
final int virtualViewId = getVirtualViewIdOf(key);
|
2012-02-28 18:01:40 +00:00
|
|
|
final String keyDescription = getKeyDescription(key);
|
|
|
|
final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
|
|
|
|
event.setPackageName(mKeyboardView.getContext().getPackageName());
|
|
|
|
event.setClassName(key.getClass().getName());
|
2012-05-10 18:33:31 +00:00
|
|
|
event.setContentDescription(keyDescription);
|
2012-04-05 21:56:57 +00:00
|
|
|
event.setEnabled(true);
|
2014-05-27 06:24:39 +00:00
|
|
|
final AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
|
2012-02-28 18:01:40 +00:00
|
|
|
record.setSource(mKeyboardView, virtualViewId);
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:53:40 +00:00
|
|
|
public void onHoverEnterTo(final Key key) {
|
|
|
|
final int id = getVirtualViewIdOf(key);
|
|
|
|
if (id == View.NO_ID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Start hovering on the key. Because our accessibility model is lift-to-type, we should
|
|
|
|
// report the node info without click and long click actions to avoid unnecessary
|
|
|
|
// announcements.
|
|
|
|
mHoveringNodeId = id;
|
|
|
|
// Invalidate the node info of the key.
|
|
|
|
sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_WINDOW_CONTENT_CHANGED);
|
|
|
|
sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onHoverExitFrom(final Key key) {
|
|
|
|
mHoveringNodeId = UNDEFINED;
|
|
|
|
// Invalidate the node info of the key to be able to revert the change we have done
|
|
|
|
// in {@link #onHoverEnterTo(Key)}.
|
|
|
|
sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_WINDOW_CONTENT_CHANGED);
|
|
|
|
sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_VIEW_HOVER_EXIT);
|
|
|
|
}
|
|
|
|
|
2012-02-28 18:01:40 +00:00
|
|
|
/**
|
|
|
|
* Returns an {@link AccessibilityNodeInfoCompat} representing a virtual
|
|
|
|
* view, i.e. a descendant of the host View, with the given <code>virtualViewId</code> or
|
|
|
|
* the host View itself if <code>virtualViewId</code> equals to {@link View#NO_ID}.
|
|
|
|
* <p>
|
|
|
|
* 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.
|
|
|
|
* </p>
|
|
|
|
* <p>
|
|
|
|
* 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.
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @param virtualViewId A client defined virtual view id.
|
2013-01-06 02:10:27 +00:00
|
|
|
* @return A populated {@link AccessibilityNodeInfoCompat} for a virtual descendant or the host
|
|
|
|
* View.
|
2012-02-28 18:01:40 +00:00
|
|
|
* @see AccessibilityNodeInfoCompat
|
|
|
|
*/
|
|
|
|
@Override
|
2013-01-06 02:10:27 +00:00
|
|
|
public AccessibilityNodeInfoCompat createAccessibilityNodeInfo(final int virtualViewId) {
|
2012-05-15 22:11:12 +00:00
|
|
|
if (virtualViewId == UNDEFINED) {
|
|
|
|
return null;
|
2013-01-06 02:10:27 +00:00
|
|
|
}
|
|
|
|
if (virtualViewId == View.NO_ID) {
|
2012-02-28 18:01:40 +00:00
|
|
|
// We are requested to create an AccessibilityNodeInfo describing
|
2014-06-19 20:38:15 +00:00
|
|
|
// this View, i.e. the root of the virtual sub-tree.
|
2013-01-06 02:10:27 +00:00
|
|
|
final AccessibilityNodeInfoCompat rootInfo =
|
|
|
|
AccessibilityNodeInfoCompat.obtain(mKeyboardView);
|
|
|
|
ViewCompat.onInitializeAccessibilityNodeInfo(mKeyboardView, rootInfo);
|
2014-06-19 20:38:15 +00:00
|
|
|
updateParentLocation();
|
|
|
|
|
|
|
|
// Add the virtual children of the root View.
|
|
|
|
final List<Key> 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);
|
|
|
|
}
|
2013-01-06 02:10:27 +00:00
|
|
|
return rootInfo;
|
2012-02-28 18:01:40 +00:00
|
|
|
}
|
|
|
|
|
2014-04-22 02:26:08 +00:00
|
|
|
// Find the key that corresponds to the given virtual view id.
|
|
|
|
final Key key = getKeyOf(virtualViewId);
|
2013-01-06 02:10:27 +00:00
|
|
|
if (key == null) {
|
|
|
|
Log.e(TAG, "Invalid virtual view ID: " + virtualViewId);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final String keyDescription = getKeyDescription(key);
|
2013-08-12 09:05:11 +00:00
|
|
|
final Rect boundsInParent = key.getHitBox();
|
2013-01-06 02:10:27 +00:00
|
|
|
|
|
|
|
// 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);
|
2014-06-19 20:38:15 +00:00
|
|
|
info.setEnabled(key.isEnabled());
|
2013-01-06 02:10:27 +00:00
|
|
|
info.setVisibleToUser(true);
|
2014-06-20 21:53:40 +00:00
|
|
|
// Don't add ACTION_CLICK and ACTION_LONG_CLOCK actions while hovering on the key.
|
|
|
|
// See {@link #onHoverEnterTo(Key)} and {@link #onHoverExitFrom(Key)}.
|
|
|
|
if (virtualViewId != mHoveringNodeId) {
|
|
|
|
info.addAction(AccessibilityNodeInfoCompat.ACTION_CLICK);
|
|
|
|
if (key.isLongPressEnabled()) {
|
|
|
|
info.addAction(AccessibilityNodeInfoCompat.ACTION_LONG_CLICK);
|
|
|
|
}
|
|
|
|
}
|
2013-01-06 02:10:27 +00:00
|
|
|
|
|
|
|
if (mAccessibilityFocusedView == virtualViewId) {
|
|
|
|
info.addAction(AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
|
|
|
|
} else {
|
|
|
|
info.addAction(AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS);
|
|
|
|
}
|
2012-02-28 18:01:40 +00:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2012-05-15 22:11:12 +00:00
|
|
|
@Override
|
2013-01-06 02:10:27 +00:00
|
|
|
public boolean performAction(final int virtualViewId, final int action,
|
|
|
|
final Bundle arguments) {
|
2014-04-22 02:26:08 +00:00
|
|
|
final Key key = getKeyOf(virtualViewId);
|
2012-05-15 22:11:12 +00:00
|
|
|
if (key == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-27 01:54:20 +00:00
|
|
|
return performActionForKey(key, action);
|
2012-05-15 22:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs the specified accessibility action for the given key.
|
|
|
|
*
|
|
|
|
* @param key The on which to perform the action.
|
|
|
|
* @param action The action to perform.
|
2013-01-06 02:10:27 +00:00
|
|
|
* @return The result of performing the action, or false if the action is not supported.
|
2012-05-15 22:11:12 +00:00
|
|
|
*/
|
2014-05-27 01:54:20 +00:00
|
|
|
boolean performActionForKey(final Key key, final int action) {
|
2012-05-15 22:11:12 +00:00
|
|
|
switch (action) {
|
|
|
|
case AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS:
|
2014-05-27 01:54:20 +00:00
|
|
|
mAccessibilityFocusedView = getVirtualViewIdOf(key);
|
2012-05-15 22:11:12 +00:00
|
|
|
sendAccessibilityEventForKey(
|
|
|
|
key, AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
|
|
|
|
return true;
|
|
|
|
case AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS:
|
|
|
|
mAccessibilityFocusedView = UNDEFINED;
|
|
|
|
sendAccessibilityEventForKey(
|
|
|
|
key, AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
|
|
|
|
return true;
|
2014-06-20 21:53:40 +00:00
|
|
|
case AccessibilityNodeInfoCompat.ACTION_CLICK:
|
|
|
|
sendAccessibilityEventForKey(key, AccessibilityEvent.TYPE_VIEW_CLICKED);
|
2014-06-24 22:14:24 +00:00
|
|
|
mDelegate.performClickOn(key);
|
2014-06-20 21:53:40 +00:00
|
|
|
return true;
|
|
|
|
case AccessibilityNodeInfoCompat.ACTION_LONG_CLICK:
|
|
|
|
sendAccessibilityEventForKey(key, AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
|
2014-06-24 22:14:24 +00:00
|
|
|
mDelegate.performLongClickOn(key);
|
2014-06-20 21:53:40 +00:00
|
|
|
return true;
|
2013-01-06 02:10:27 +00:00
|
|
|
default:
|
|
|
|
return false;
|
2012-05-15 22:11:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-01-06 02:10:27 +00:00
|
|
|
void sendAccessibilityEventForKey(final Key key, final int eventType) {
|
2012-05-15 22:11:12 +00:00
|
|
|
final AccessibilityEvent event = createAccessibilityEvent(key, eventType);
|
2012-05-22 03:31:06 +00:00
|
|
|
mAccessibilityUtils.requestSendAccessibilityEvent(event);
|
2012-05-15 22:11:12 +00:00
|
|
|
}
|
|
|
|
|
2012-02-28 18:01:40 +00:00
|
|
|
/**
|
|
|
|
* Returns the context-specific description for a {@link Key}.
|
|
|
|
*
|
|
|
|
* @param key The key to describe.
|
|
|
|
* @return The context-specific description of the key.
|
|
|
|
*/
|
2013-01-06 02:10:27 +00:00
|
|
|
private String getKeyDescription(final Key key) {
|
2014-04-25 21:56:30 +00:00
|
|
|
final EditorInfo editorInfo = mKeyboard.mId.mEditorInfo;
|
2012-02-28 18:01:40 +00:00
|
|
|
final boolean shouldObscure = mAccessibilityUtils.shouldObscureInput(editorInfo);
|
2013-09-27 21:15:53 +00:00
|
|
|
final SettingsValues currentSettings = Settings.getInstance().getCurrent();
|
|
|
|
final String keyCodeDescription = mKeyCodeDescriptionMapper.getDescriptionForKey(
|
2014-04-22 02:26:08 +00:00
|
|
|
mKeyboardView.getContext(), mKeyboard, key, shouldObscure);
|
2013-09-27 21:15:53 +00:00
|
|
|
if (currentSettings.isWordSeparator(key.getCode())) {
|
|
|
|
return mAccessibilityUtils.getAutoCorrectionDescription(
|
|
|
|
keyCodeDescription, shouldObscure);
|
|
|
|
}
|
2014-10-20 05:48:56 +00:00
|
|
|
return keyCodeDescription;
|
2012-02-28 18:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the parent's on-screen location.
|
|
|
|
*/
|
|
|
|
private void updateParentLocation() {
|
|
|
|
mKeyboardView.getLocationOnScreen(mParentLocation);
|
|
|
|
}
|
|
|
|
}
|