This source file includes following definitions.
- JNINamespace
- create
- onNativeObjectDestroyed
- getAccessibilityNodeProvider
- createAccessibilityNodeInfo
- findAccessibilityNodeInfosByText
- performAction
- onHoverEvent
- notifyFrameInfoInitialized
- jumpToElementType
- sendAccessibilityEvent
- getOrCreateBundleForAccessibilityEvent
- createNodeForHost
- handlePageLoaded
- handleFocusChanged
- handleCheckStateChanged
- handleTextSelectionChanged
- handleEditableTextChanged
- handleContentChanged
- handleNavigate
- handleScrollPositionChanged
- handleScrolledToAnchor
- announceLiveRegionText
- setAccessibilityNodeInfoParent
- addAccessibilityNodeInfoChild
- setAccessibilityNodeInfoBooleanAttributes
- setAccessibilityNodeInfoClassName
- setAccessibilityNodeInfoContentDescription
- setAccessibilityNodeInfoLocation
- setAccessibilityNodeInfoKitKatAttributes
- setAccessibilityNodeInfoCollectionInfo
- setAccessibilityNodeInfoCollectionItemInfo
- setAccessibilityNodeInfoRangeInfo
- setAccessibilityEventBooleanAttributes
- setAccessibilityEventClassName
- setAccessibilityEventListAttributes
- setAccessibilityEventScrollAttributes
- setAccessibilityEventTextChangedAttrs
- setAccessibilityEventSelectionAttrs
- setAccessibilityEventKitKatAttributes
- setAccessibilityEventCollectionInfo
- setAccessibilityEventCollectionItemInfo
- setAccessibilityEventRangeInfo
- nativeGetRootId
- nativeIsNodeValid
- nativeHitTest
- nativePopulateAccessibilityNodeInfo
- nativePopulateAccessibilityEvent
- nativeClick
- nativeFocus
- nativeBlur
- nativeScrollToMakeNodeVisible
- nativeFindElementType
package org.chromium.content.browser.accessibility;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.URLSpan;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityNodeProvider;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.content.browser.ContentViewCore;
import org.chromium.content.browser.RenderCoordinates;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@JNINamespace("content")
public class BrowserAccessibilityManager {
private static final String TAG = "BrowserAccessibilityManager";
private ContentViewCore mContentViewCore;
private final AccessibilityManager mAccessibilityManager;
private final RenderCoordinates mRenderCoordinates;
private long mNativeObj;
private int mAccessibilityFocusId;
private boolean mIsHovering;
private int mCurrentRootId;
private final int[] mTempLocation = new int[2];
private final View mView;
private boolean mUserHasTouchExplored;
private boolean mPendingScrollToMakeNodeVisible;
private boolean mFrameInfoInitialized;
@CalledByNative
private static BrowserAccessibilityManager create(long nativeBrowserAccessibilityManagerAndroid,
ContentViewCore contentViewCore) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return new JellyBeanBrowserAccessibilityManager(
nativeBrowserAccessibilityManagerAndroid, contentViewCore);
} else {
return new BrowserAccessibilityManager(
nativeBrowserAccessibilityManagerAndroid, contentViewCore);
}
}
protected BrowserAccessibilityManager(long nativeBrowserAccessibilityManagerAndroid,
ContentViewCore contentViewCore) {
mNativeObj = nativeBrowserAccessibilityManagerAndroid;
mContentViewCore = contentViewCore;
mContentViewCore.setBrowserAccessibilityManager(this);
mAccessibilityFocusId = View.NO_ID;
mIsHovering = false;
mCurrentRootId = View.NO_ID;
mView = mContentViewCore.getContainerView();
mRenderCoordinates = mContentViewCore.getRenderCoordinates();
mAccessibilityManager =
(AccessibilityManager) mContentViewCore.getContext()
.getSystemService(Context.ACCESSIBILITY_SERVICE);
}
@CalledByNative
private void onNativeObjectDestroyed() {
if (mContentViewCore.getBrowserAccessibilityManager() == this) {
mContentViewCore.setBrowserAccessibilityManager(null);
}
mNativeObj = 0;
mContentViewCore = null;
}
public AccessibilityNodeProvider getAccessibilityNodeProvider() {
return null;
}
protected AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId) {
if (!mAccessibilityManager.isEnabled() || mNativeObj == 0) {
return null;
}
int rootId = nativeGetRootId(mNativeObj);
if (virtualViewId == View.NO_ID) {
return createNodeForHost(rootId);
}
if (!mFrameInfoInitialized) {
return null;
}
final AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(mView);
info.setPackageName(mContentViewCore.getContext().getPackageName());
info.setSource(mView, virtualViewId);
if (virtualViewId == rootId) {
info.setParent(mView);
}
if (nativePopulateAccessibilityNodeInfo(mNativeObj, info, virtualViewId)) {
return info;
} else {
info.recycle();
return null;
}
}
protected List<AccessibilityNodeInfo> findAccessibilityNodeInfosByText(String text,
int virtualViewId) {
return new ArrayList<AccessibilityNodeInfo>();
}
protected boolean performAction(int virtualViewId, int action, Bundle arguments) {
if (!mAccessibilityManager.isEnabled() || mNativeObj == 0
|| !nativeIsNodeValid(mNativeObj, virtualViewId)) {
return false;
}
switch (action) {
case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS:
if (mAccessibilityFocusId == virtualViewId) {
return true;
}
mAccessibilityFocusId = virtualViewId;
sendAccessibilityEvent(mAccessibilityFocusId,
AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
if (!mIsHovering) {
nativeScrollToMakeNodeVisible(
mNativeObj, mAccessibilityFocusId);
} else {
mPendingScrollToMakeNodeVisible = true;
}
return true;
case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS:
if (mAccessibilityFocusId == virtualViewId) {
sendAccessibilityEvent(mAccessibilityFocusId,
AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
mAccessibilityFocusId = View.NO_ID;
}
return true;
case AccessibilityNodeInfo.ACTION_CLICK:
nativeClick(mNativeObj, virtualViewId);
sendAccessibilityEvent(virtualViewId,
AccessibilityEvent.TYPE_VIEW_CLICKED);
return true;
case AccessibilityNodeInfo.ACTION_FOCUS:
nativeFocus(mNativeObj, virtualViewId);
return true;
case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS:
nativeBlur(mNativeObj);
return true;
case AccessibilityNodeInfo.ACTION_NEXT_HTML_ELEMENT: {
if (arguments == null)
return false;
String elementType = arguments.getString(
AccessibilityNodeInfo.ACTION_ARGUMENT_HTML_ELEMENT_STRING);
if (elementType == null)
return false;
elementType = elementType.toUpperCase(Locale.US);
return jumpToElementType(elementType, true);
}
case AccessibilityNodeInfo.ACTION_PREVIOUS_HTML_ELEMENT: {
if (arguments == null)
return false;
String elementType = arguments.getString(
AccessibilityNodeInfo.ACTION_ARGUMENT_HTML_ELEMENT_STRING);
if (elementType == null)
return false;
elementType = elementType.toUpperCase(Locale.US);
return jumpToElementType(elementType, false);
}
default:
break;
}
return false;
}
public boolean onHoverEvent(MotionEvent event) {
if (!mAccessibilityManager.isEnabled() || mNativeObj == 0) {
return false;
}
if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT) {
mIsHovering = false;
if (mPendingScrollToMakeNodeVisible) {
nativeScrollToMakeNodeVisible(
mNativeObj, mAccessibilityFocusId);
}
mPendingScrollToMakeNodeVisible = false;
return true;
}
mIsHovering = true;
mUserHasTouchExplored = true;
float x = event.getX();
float y = event.getY();
int cssX = (int) (mRenderCoordinates.fromPixToLocalCss(x) +
mRenderCoordinates.getScrollX());
int cssY = (int) (mRenderCoordinates.fromPixToLocalCss(y) +
mRenderCoordinates.getScrollY());
int id = nativeHitTest(mNativeObj, cssX, cssY);
if (mAccessibilityFocusId != id) {
sendAccessibilityEvent(mAccessibilityFocusId, AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
sendAccessibilityEvent(id, AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
}
return true;
}
public void notifyFrameInfoInitialized() {
if (mFrameInfoInitialized) return;
mFrameInfoInitialized = true;
mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
if (mAccessibilityFocusId != View.NO_ID) {
sendAccessibilityEvent(mAccessibilityFocusId,
AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
}
}
private boolean jumpToElementType(String elementType, boolean forwards) {
int id = nativeFindElementType(mNativeObj, mAccessibilityFocusId, elementType, forwards);
if (id == 0)
return false;
mAccessibilityFocusId = id;
sendAccessibilityEvent(id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
return true;
}
private void sendAccessibilityEvent(int virtualViewId, int eventType) {
if (!mAccessibilityManager.isEnabled() || mNativeObj == 0
|| !mFrameInfoInitialized) {
return;
}
final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
event.setPackageName(mContentViewCore.getContext().getPackageName());
event.setSource(mView, virtualViewId);
if (!nativePopulateAccessibilityEvent(mNativeObj, event, virtualViewId, eventType)) {
event.recycle();
return;
}
mContentViewCore.getContainerView().postInvalidate();
mContentViewCore.getContainerView().requestSendAccessibilityEvent(mView, event);
}
private Bundle getOrCreateBundleForAccessibilityEvent(AccessibilityEvent event) {
Bundle bundle = (Bundle) event.getParcelableData();
if (bundle == null) {
bundle = new Bundle();
event.setParcelableData(bundle);
}
return bundle;
}
private AccessibilityNodeInfo createNodeForHost(int rootId) {
final AccessibilityNodeInfo result = AccessibilityNodeInfo.obtain(mView);
final AccessibilityNodeInfo source = AccessibilityNodeInfo.obtain(mView);
mView.onInitializeAccessibilityNodeInfo(source);
Rect rect = new Rect();
source.getBoundsInParent(rect);
result.setBoundsInParent(rect);
source.getBoundsInScreen(rect);
result.setBoundsInScreen(rect);
final ViewParent parent = mView.getParentForAccessibility();
if (parent instanceof View) {
result.setParent((View) parent);
}
result.setVisibleToUser(source.isVisibleToUser());
result.setEnabled(source.isEnabled());
result.setPackageName(source.getPackageName());
result.setClassName(source.getClassName());
if (mFrameInfoInitialized) {
result.addChild(mView, rootId);
}
return result;
}
@CalledByNative
private void handlePageLoaded(int id) {
if (mUserHasTouchExplored) return;
mAccessibilityFocusId = id;
sendAccessibilityEvent(id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
}
@CalledByNative
private void handleFocusChanged(int id) {
sendAccessibilityEvent(id, AccessibilityEvent.TYPE_VIEW_FOCUSED);
if (mAccessibilityFocusId != id) {
sendAccessibilityEvent(id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
mAccessibilityFocusId = id;
}
}
@CalledByNative
private void handleCheckStateChanged(int id) {
sendAccessibilityEvent(id, AccessibilityEvent.TYPE_VIEW_CLICKED);
}
@CalledByNative
private void handleTextSelectionChanged(int id) {
sendAccessibilityEvent(id, AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED);
}
@CalledByNative
private void handleEditableTextChanged(int id) {
sendAccessibilityEvent(id, AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
}
@CalledByNative
private void handleContentChanged(int id) {
int rootId = nativeGetRootId(mNativeObj);
if (rootId != mCurrentRootId) {
mCurrentRootId = rootId;
mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
} else {
sendAccessibilityEvent(id, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
}
}
@CalledByNative
private void handleNavigate() {
mAccessibilityFocusId = View.NO_ID;
mUserHasTouchExplored = false;
mFrameInfoInitialized = false;
mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
}
@CalledByNative
private void handleScrollPositionChanged(int id) {
sendAccessibilityEvent(id, AccessibilityEvent.TYPE_VIEW_SCROLLED);
}
@CalledByNative
private void handleScrolledToAnchor(int id) {
if (mAccessibilityFocusId == id) {
return;
}
mAccessibilityFocusId = id;
sendAccessibilityEvent(id, AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
}
@CalledByNative
private void announceLiveRegionText(String text) {
mView.announceForAccessibility(text);
}
@CalledByNative
private void setAccessibilityNodeInfoParent(AccessibilityNodeInfo node, int parentId) {
node.setParent(mView, parentId);
}
@CalledByNative
private void addAccessibilityNodeInfoChild(AccessibilityNodeInfo node, int childId) {
node.addChild(mView, childId);
}
@CalledByNative
private void setAccessibilityNodeInfoBooleanAttributes(AccessibilityNodeInfo node,
int virtualViewId, boolean checkable, boolean checked, boolean clickable,
boolean enabled, boolean focusable, boolean focused, boolean password,
boolean scrollable, boolean selected, boolean visibleToUser) {
node.setCheckable(checkable);
node.setChecked(checked);
node.setClickable(clickable);
node.setEnabled(enabled);
node.setFocusable(focusable);
node.setFocused(focused);
node.setPassword(password);
node.setScrollable(scrollable);
node.setSelected(selected);
node.setVisibleToUser(visibleToUser);
node.addAction(AccessibilityNodeInfo.ACTION_NEXT_HTML_ELEMENT);
node.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_HTML_ELEMENT);
if (focusable) {
if (focused) {
node.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
} else {
node.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
}
}
if (mAccessibilityFocusId == virtualViewId) {
node.setAccessibilityFocused(true);
node.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
} else {
node.setAccessibilityFocused(false);
node.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
}
if (clickable) {
node.addAction(AccessibilityNodeInfo.ACTION_CLICK);
}
}
@CalledByNative
private void setAccessibilityNodeInfoClassName(AccessibilityNodeInfo node,
String className) {
node.setClassName(className);
}
@CalledByNative
private void setAccessibilityNodeInfoContentDescription(
AccessibilityNodeInfo node, String contentDescription, boolean annotateAsLink) {
if (annotateAsLink) {
SpannableString spannable = new SpannableString(contentDescription);
spannable.setSpan(new URLSpan(""), 0, spannable.length(), 0);
node.setContentDescription(spannable);
} else {
node.setContentDescription(contentDescription);
}
}
@CalledByNative
private void setAccessibilityNodeInfoLocation(AccessibilityNodeInfo node,
int absoluteLeft, int absoluteTop, int parentRelativeLeft, int parentRelativeTop,
int width, int height, boolean isRootNode) {
Rect boundsInParent = new Rect(parentRelativeLeft, parentRelativeTop,
parentRelativeLeft + width, parentRelativeTop + height);
if (isRootNode) {
boundsInParent.offset(0, (int) mRenderCoordinates.getContentOffsetYPix());
}
node.setBoundsInParent(boundsInParent);
Rect rect = new Rect(absoluteLeft, absoluteTop, absoluteLeft + width, absoluteTop + height);
rect.offset(-(int) mRenderCoordinates.getScrollX(),
-(int) mRenderCoordinates.getScrollY());
rect.left = (int) mRenderCoordinates.fromLocalCssToPix(rect.left);
rect.top = (int) mRenderCoordinates.fromLocalCssToPix(rect.top);
rect.bottom = (int) mRenderCoordinates.fromLocalCssToPix(rect.bottom);
rect.right = (int) mRenderCoordinates.fromLocalCssToPix(rect.right);
rect.offset(0,
(int) mRenderCoordinates.getContentOffsetYPix());
final int[] viewLocation = new int[2];
mView.getLocationOnScreen(viewLocation);
rect.offset(viewLocation[0], viewLocation[1]);
node.setBoundsInScreen(rect);
}
@CalledByNative
protected void setAccessibilityNodeInfoKitKatAttributes(AccessibilityNodeInfo node,
boolean canOpenPopup,
boolean contentInvalid,
boolean dismissable,
boolean multiLine,
int inputType,
int liveRegion) {
}
@CalledByNative
protected void setAccessibilityNodeInfoCollectionInfo(AccessibilityNodeInfo node,
int rowCount, int columnCount, boolean hierarchical) {
}
@CalledByNative
protected void setAccessibilityNodeInfoCollectionItemInfo(AccessibilityNodeInfo node,
int rowIndex, int rowSpan, int columnIndex, int columnSpan, boolean heading) {
}
@CalledByNative
protected void setAccessibilityNodeInfoRangeInfo(AccessibilityNodeInfo node,
int rangeType, float min, float max, float current) {
}
@CalledByNative
private void setAccessibilityEventBooleanAttributes(AccessibilityEvent event,
boolean checked, boolean enabled, boolean password, boolean scrollable) {
event.setChecked(checked);
event.setEnabled(enabled);
event.setPassword(password);
event.setScrollable(scrollable);
}
@CalledByNative
private void setAccessibilityEventClassName(AccessibilityEvent event, String className) {
event.setClassName(className);
}
@CalledByNative
private void setAccessibilityEventListAttributes(AccessibilityEvent event,
int currentItemIndex, int itemCount) {
event.setCurrentItemIndex(currentItemIndex);
event.setItemCount(itemCount);
}
@CalledByNative
private void setAccessibilityEventScrollAttributes(AccessibilityEvent event,
int scrollX, int scrollY, int maxScrollX, int maxScrollY) {
event.setScrollX(scrollX);
event.setScrollY(scrollY);
event.setMaxScrollX(maxScrollX);
event.setMaxScrollY(maxScrollY);
}
@CalledByNative
private void setAccessibilityEventTextChangedAttrs(AccessibilityEvent event,
int fromIndex, int addedCount, int removedCount, String beforeText, String text) {
event.setFromIndex(fromIndex);
event.setAddedCount(addedCount);
event.setRemovedCount(removedCount);
event.setBeforeText(beforeText);
event.getText().add(text);
}
@CalledByNative
private void setAccessibilityEventSelectionAttrs(AccessibilityEvent event,
int fromIndex, int addedCount, int itemCount, String text) {
event.setFromIndex(fromIndex);
event.setAddedCount(addedCount);
event.setItemCount(itemCount);
event.getText().add(text);
}
@CalledByNative
protected void setAccessibilityEventKitKatAttributes(AccessibilityEvent event,
boolean canOpenPopup,
boolean contentInvalid,
boolean dismissable,
boolean multiLine,
int inputType,
int liveRegion) {
Bundle bundle = getOrCreateBundleForAccessibilityEvent(event);
bundle.putBoolean("AccessibilityNodeInfo.canOpenPopup", canOpenPopup);
bundle.putBoolean("AccessibilityNodeInfo.contentInvalid", contentInvalid);
bundle.putBoolean("AccessibilityNodeInfo.dismissable", dismissable);
bundle.putBoolean("AccessibilityNodeInfo.multiLine", multiLine);
bundle.putInt("AccessibilityNodeInfo.inputType", inputType);
bundle.putInt("AccessibilityNodeInfo.liveRegion", liveRegion);
}
@CalledByNative
protected void setAccessibilityEventCollectionInfo(AccessibilityEvent event,
int rowCount, int columnCount, boolean hierarchical) {
Bundle bundle = getOrCreateBundleForAccessibilityEvent(event);
bundle.putInt("AccessibilityNodeInfo.CollectionInfo.rowCount", rowCount);
bundle.putInt("AccessibilityNodeInfo.CollectionInfo.columnCount", columnCount);
bundle.putBoolean("AccessibilityNodeInfo.CollectionInfo.hierarchical", hierarchical);
}
@CalledByNative
protected void setAccessibilityEventCollectionItemInfo(AccessibilityEvent event,
int rowIndex, int rowSpan, int columnIndex, int columnSpan, boolean heading) {
Bundle bundle = getOrCreateBundleForAccessibilityEvent(event);
bundle.putInt("AccessibilityNodeInfo.CollectionItemInfo.rowIndex", rowIndex);
bundle.putInt("AccessibilityNodeInfo.CollectionItemInfo.rowSpan", rowSpan);
bundle.putInt("AccessibilityNodeInfo.CollectionItemInfo.columnIndex", columnIndex);
bundle.putInt("AccessibilityNodeInfo.CollectionItemInfo.columnSpan", columnSpan);
bundle.putBoolean("AccessibilityNodeInfo.CollectionItemInfo.heading", heading);
}
@CalledByNative
protected void setAccessibilityEventRangeInfo(AccessibilityEvent event,
int rangeType, float min, float max, float current) {
Bundle bundle = getOrCreateBundleForAccessibilityEvent(event);
bundle.putInt("AccessibilityNodeInfo.RangeInfo.type", rangeType);
bundle.putFloat("AccessibilityNodeInfo.RangeInfo.min", min);
bundle.putFloat("AccessibilityNodeInfo.RangeInfo.max", max);
bundle.putFloat("AccessibilityNodeInfo.RangeInfo.current", current);
}
private native int nativeGetRootId(long nativeBrowserAccessibilityManagerAndroid);
private native boolean nativeIsNodeValid(long nativeBrowserAccessibilityManagerAndroid, int id);
private native int nativeHitTest(long nativeBrowserAccessibilityManagerAndroid, int x, int y);
private native boolean nativePopulateAccessibilityNodeInfo(
long nativeBrowserAccessibilityManagerAndroid, AccessibilityNodeInfo info, int id);
private native boolean nativePopulateAccessibilityEvent(
long nativeBrowserAccessibilityManagerAndroid, AccessibilityEvent event, int id,
int eventType);
private native void nativeClick(long nativeBrowserAccessibilityManagerAndroid, int id);
private native void nativeFocus(long nativeBrowserAccessibilityManagerAndroid, int id);
private native void nativeBlur(long nativeBrowserAccessibilityManagerAndroid);
private native void nativeScrollToMakeNodeVisible(
long nativeBrowserAccessibilityManagerAndroid, int id);
private native int nativeFindElementType(long nativeBrowserAccessibilityManagerAndroid,
int startId, String elementType, boolean forwards);
}