下面列出了android.support.v4.view.accessibility.AccessibilityNodeInfoCompat#getInfo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Gets the text of a <code>node</code> by returning the content description
* (if available) or by returning the text. Will use the specified
* <code>CustomLabelManager</code> as a fall back if both are null.
* If the label manager is null, does the same funciton as
* {@code getNodeText} in {@code AccessibilityNodeInfoUtils}
*
* @param node The node.
* @param labelManager The label manager.
* @return The node text.
*/
public static CharSequence getNodeText(AccessibilityNodeInfoCompat node,
CustomLabelManager labelManager) {
CharSequence text = AccessibilityNodeInfoUtils.getNodeText(node);
if (!TextUtils.isEmpty(text)) {
return text;
}
if (labelManager != null && labelManager.isInitialized()) {
// TODO: Don't need to do this when support libs fixed.
final AccessibilityNodeInfo unwrappedNode =
(AccessibilityNodeInfo) node.getInfo();
Label label = labelManager.getLabelForViewIdFromCache(
unwrappedNode.getViewIdResourceName());
if (label != null) {
return label.getText();
}
}
return null;
}
/**
* Gets the node info for which the view represented by this info serves as
* a label for accessibility purposes.
* <p>
* <strong>Note:</strong> It is a client responsibility to recycle the
* received info by calling {@link AccessibilityNodeInfoCompat#recycle()} to
* avoid creating of multiple instances.
* </p>
*
* @return The labeled info.
*/
public static AccessibilityNodeInfoCompat getLabelFor(AccessibilityNodeInfoCompat node) {
final Object info = node.getInfo();
if (info == null) {
Log.e(TAG, "Compat node was missing internal node");
return null;
}
final Object resultInfo = CompatUtils.invoke(info, null, METHOD_getLabelFor);
if (resultInfo == null) {
return null;
}
return new AccessibilityNodeInfoCompat(resultInfo);
}
/**
* Gets the node info which serves as the label of the view represented by
* this info for accessibility purposes.
* <p>
* <strong>Note:</strong> It is a client responsibility to recycle the
* received info by calling {@link AccessibilityNodeInfoCompat#recycle()} to
* avoid creating of multiple instances.
* </p>
*
* @return The label.
*/
public static AccessibilityNodeInfoCompat getLabeledBy(AccessibilityNodeInfoCompat node) {
final Object info = node.getInfo();
if (info == null) {
Log.e(TAG, "Compat node was missing internal node");
return null;
}
final Object resultInfo = CompatUtils.invoke(info, null, METHOD_getLabeledBy);
if (resultInfo == null) {
return null;
}
return new AccessibilityNodeInfoCompat(resultInfo);
}
/**
* Activates the currently selected menu item.
*/
public boolean activateCurrentMenuItem() {
// Grab the state we need before closing the menu, since that
// clears out all state.
final int itemId = mMenuItems.get(mCurrentIndex).getId();
AccessibilityNodeInfoCompat node = mInitialNode.release();
try {
closeMenu();
if (node == null) {
return false;
}
// This must be checked before we try to grab a label.
if (itemId == R.string.menu_item_update_talkback) {
return launchIntentToPlayStoreTalkBack();
}
AccessibilityNodeInfo unwrapped =
(AccessibilityNodeInfo) node.getInfo();
final Label existingLabel =
mLabelManager.getLabelForViewIdFromCache(
unwrapped.getViewIdResourceName());
if (itemId == R.string.menu_item_label_add) {
return LabelOperationUtils.startActivityAddLabelForNode(
mAccessibilityService, unwrapped);
} else if (itemId == R.string.menu_item_label_edit) {
return LabelOperationUtils.startActivityEditLabel(
mAccessibilityService, existingLabel);
} else if (itemId == R.string.menu_item_label_remove) {
return LabelOperationUtils.startActivityRemoveLabel(
mAccessibilityService, existingLabel);
}
} finally {
AccessibilityNodeInfoUtils.recycleNodes(node);
}
return false;
}
public DisplayManager.Content contentForNode(
AccessibilityNodeInfoCompat node) {
if (mNodeStates.isEmpty()) {
return null;
}
AccessibilityNodeInfoCompat match =
AccessibilityNodeInfoUtils.getSelfOrMatchingAncestor(
this, node, mFilterHaveNodeState);
if (match == null) {
return null;
}
AccessibilityNodeInfo unwrappedMatch =
(AccessibilityNodeInfo) match.getInfo();
WriteData writeData = mNodeStates.get(unwrappedMatch).mWriteData;
if (writeData == null) {
return null;
}
SpannableStringBuilder sb = new SpannableStringBuilder(
writeData.getText());
// NOTE: it is important to use a node returned by the accessibility
// framework and not a node from a client of this service.
// The rest of BrailleBack will assume that the node we are adding
// here is sealed, supports actions etc.
DisplaySpans.setAccessibilityNode(sb, match);
int selectionStart = writeData.getSelectionStart();
if (selectionStart >= 0) {
int selectionEnd = writeData.getSelectionEnd();
if (selectionEnd < selectionStart) {
selectionEnd = selectionStart;
}
DisplaySpans.addSelection(sb, selectionStart, selectionEnd);
}
return new DisplayManager.Content(sb)
.setFirstNode(match)
.setLastNode(match)
.setPanStrategy(DisplayManager.Content.PAN_CURSOR);
}
@Override
public boolean accept(Context context,
AccessibilityNodeInfoCompat node) {
AccessibilityNodeInfo unwrappedNode =
(AccessibilityNodeInfo) node.getInfo();
return mNodeStates.containsKey(unwrappedNode);
}
private ArrayList<CopyNode> traverseNode(AccessibilityNodeInfoCompat nodeInfo, int width, int height) {
ArrayList<CopyNode> nodeList = new ArrayList();
if (nodeInfo != null && nodeInfo.getInfo() != null) {
nodeInfo.refresh();
for (int i = 0; i < nodeInfo.getChildCount(); ++i) {
//递归遍历nodeInfo
nodeList.addAll(traverseNode(nodeInfo.getChild(i), width, height));
}
if (nodeInfo.getClassName() != null && nodeInfo.getClassName().equals("android.webkit.WebView")) {
return nodeList;
} else {
String content = null;
String description = content;
if (nodeInfo.getContentDescription() != null) {
description = content;
if (!"".equals(nodeInfo.getContentDescription())) {
description = nodeInfo.getContentDescription().toString();
}
}
content = description;
if (nodeInfo.getText() != null) {
content = description;
if (!"".equals(nodeInfo.getText())) {
content = nodeInfo.getText().toString();
}
}
if (content != null) {
Rect outBounds = new Rect();
nodeInfo.getBoundsInScreen(outBounds);
if (checkBound(outBounds, width, height)) {
nodeList.add(new CopyNode(outBounds, content));
}
}
return nodeList;
}
} else {
return nodeList;
}
}
/**
* Returns a list of menu item strings to be shown for the specified node.
* May be empty if no items needed (already labeled by developer).
*/
private List<MenuItem> getItemsForNode(AccessibilityNodeInfoCompat node) {
List<MenuItem> items = new ArrayList<MenuItem>();
AccessibilityNodeInfo unwrapped =
(AccessibilityNodeInfo) node.getInfo();
boolean hasDescription = !TextUtils.isEmpty(
AccessibilityNodeInfoUtils.getNodeText(node));
final Pair<String, String> parsedId =
CustomLabelManager.splitResourceName(
unwrapped.getViewIdResourceName());
boolean hasParseableId = (parsedId != null);
// TODO: There are a number of views that have a
// different resource namespace than their parent application. It's
// likely we'll need to refine the database structure to accommodate
// these while also allowing the user to modify them through TalkBack
// settings. For now, we'll simply not allow labeling of such views.
boolean isFromKnownApp = false;
if (hasParseableId) {
try {
mAccessibilityService.getPackageManager().getPackageInfo(parsedId.first, 0);
isFromKnownApp = true;
} catch (NameNotFoundException e) {
// Do nothing.
}
}
// Return empty list if it has a description, has no parseable id since
// we don't support those in the label manager right now, or if it's id
// is in a different namespace than a known package.
if (hasDescription || !hasParseableId || !isFromKnownApp) {
return items;
}
// If label manager is not initialized, it is because user has a
// version of TalkBack that doesn't support labeling.
// Tell the user to update.
if (!mLabelManager.isInitialized()) {
items.add(new MenuItem(
R.string.menu_item_update_talkback, mAccessibilityService));
return items;
}
final Label viewLabel = mLabelManager.getLabelForViewIdFromCache(
unwrapped.getViewIdResourceName());
// If no custom label, only have "add" option. If there is already a
// label we have the "edit" and "remove" options.
if (viewLabel == null) {
items.add(new MenuItem(
R.string.menu_item_label_add, mAccessibilityService));
} else {
items.add(new MenuItem(
R.string.menu_item_label_edit, mAccessibilityService));
items.add(new MenuItem(
R.string.menu_item_label_remove, mAccessibilityService));
}
return items;
}