/TalkBack/src/com/google/android/marvin/talkback/AccessibilityNodeInfoUtils.java

http://eyes-free.googlecode.com/ · Java · 213 lines · 90 code · 31 blank · 92 comment · 31 complexity · 527dd203a87ca32bdbb9096f9a99bd91 MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.android.marvin.talkback;
  17. import android.content.Context;
  18. import android.view.accessibility.AccessibilityNodeInfo;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. /**
  22. * Provides a series of utilities for interacting with AccessibilityNodeInfo
  23. * objects. NOTE: This class only recycles unused nodes that were collected
  24. * internally. Any node passed into or returned from a public method is retained
  25. * and TalkBack should recycle it when appropriate.
  26. *
  27. * @author caseyburkhardt@google.com (Casey Burkhardt)
  28. */
  29. public class AccessibilityNodeInfoUtils {
  30. /**
  31. * Filters {@link AccessibilityNodeInfo}s.
  32. */
  33. public static interface NodeFilter {
  34. /**
  35. * @param node The node info to filter.
  36. * @return {@code true} if the node is accepted
  37. */
  38. public boolean accept(AccessibilityNodeInfo node);
  39. }
  40. private static final Context CONTEXT = TalkBackService.asContext();
  41. private AccessibilityNodeInfoUtils() {
  42. // This class is not instantiable.
  43. }
  44. /**
  45. * Returns whether a node is actionable. That is, the node supports one of
  46. * the following actions:
  47. * <ul>
  48. * <li>{@link AccessibilityNodeInfo#isCheckable()}
  49. * <li>{@link AccessibilityNodeInfo#isClickable()}
  50. * <li>{@link AccessibilityNodeInfo#isFocusable()}
  51. * <li>{@link AccessibilityNodeInfo#isLongClickable()}
  52. * </ul>
  53. *
  54. * @param node The node to check.
  55. * @return Whether the node is actionable.
  56. */
  57. public static boolean isActionable(AccessibilityNodeInfo node) {
  58. return node.isCheckable() || node.isClickable() || node.isFocusable()
  59. || node.isLongClickable();
  60. }
  61. /**
  62. * Determines if the generating class of an {@link AccessibilityNodeInfo}
  63. * matches a given {@link Class} by type.
  64. *
  65. * @param node A sealed {@link AccessibilityNodeInfo} dispatched by the
  66. * accessibility framework.
  67. * @param clazz A {@link Class} to match by type or inherited type.
  68. * @return {@code true} if the {@link AccessibilityNodeInfo} object matches
  69. * the {@link Class} by type or inherited type, {@code false}
  70. * otherwise.
  71. */
  72. public static boolean nodeMatchesClassByType(AccessibilityNodeInfo node, Class<?> clazz) {
  73. if (node == null || clazz == null) {
  74. return false;
  75. }
  76. final ClassLoadingManager classLoader = ClassLoadingManager.getInstance();
  77. final CharSequence className = node.getClassName();
  78. final CharSequence packageName = node.getPackageName();
  79. final Class<?> nodeClass =
  80. classLoader.loadOrGetCachedClass(CONTEXT, className, packageName);
  81. if (nodeClass == null) {
  82. return false;
  83. }
  84. return clazz.isAssignableFrom(nodeClass);
  85. }
  86. /**
  87. * Gets all the parent {@link AccessibilityNodeInfo} nodes, up to the view
  88. * root.
  89. *
  90. * @param node The {@link AccessibilityNodeInfo} child from which to begin
  91. * collecting parents.
  92. * @param parentList The list to populate with predecessor nodes.
  93. */
  94. public static void getPredecessors(AccessibilityNodeInfo node,
  95. List<AccessibilityNodeInfo> parentList) {
  96. if (node == null || parentList == null) {
  97. return;
  98. }
  99. node = node.getParent();
  100. while (node != null) {
  101. parentList.add(node);
  102. node = node.getParent();
  103. }
  104. }
  105. /**
  106. * Recycles the given nodes.
  107. *
  108. * @param nodes The nodes to recycle.
  109. */
  110. public static void recycleNodeList(ArrayList<AccessibilityNodeInfo> nodes) {
  111. if (nodes == null) {
  112. return;
  113. }
  114. for (AccessibilityNodeInfo node : nodes) {
  115. node.recycle();
  116. }
  117. nodes.clear();
  118. }
  119. /**
  120. * Recycles the given nodes.
  121. *
  122. * @param nodes The nodes to recycle.
  123. */
  124. public static void recycleNodes(AccessibilityNodeInfo... nodes) {
  125. if (nodes == null) {
  126. return;
  127. }
  128. for (AccessibilityNodeInfo node : nodes) {
  129. if (node != null) {
  130. node.recycle();
  131. }
  132. }
  133. }
  134. /**
  135. * Adds all descendant nodes of the given {@link AccessibilityNodeInfo} in
  136. * breadth first order.
  137. *
  138. * @param root {@link AccessibilityNodeInfo} for which to add descendants.
  139. * @param outDescendants The list to which to add descendants.
  140. * @param filter Optional filter for selecting sub-set of nodes, null for
  141. * all.
  142. * @return The number of nodes that failed to match the filter.
  143. */
  144. public static int addDescendantsBfs(AccessibilityNodeInfo root,
  145. ArrayList<AccessibilityNodeInfo> outDescendants, NodeFilter filter) {
  146. final int oldOutDescendantsSize = outDescendants.size();
  147. int failedFilter = addChildren(root, outDescendants, filter);
  148. final int newOutDescendantsSize = outDescendants.size();
  149. for (int i = oldOutDescendantsSize; i < newOutDescendantsSize; i++) {
  150. final AccessibilityNodeInfo child = outDescendants.get(i);
  151. failedFilter += addDescendantsBfs(child, outDescendants, filter);
  152. }
  153. return failedFilter;
  154. }
  155. /**
  156. * Adds only the children of the given {@link AccessibilityNodeInfo}.
  157. *
  158. * @param node {@link AccessibilityNodeInfo} for which to add children.
  159. * @param outChildren The list to which to add the children.
  160. * @param filter Optional filter for selecting sub-set of nodes, null for
  161. * all.
  162. * @return The number of nodes that failed to match the filter.
  163. */
  164. private static int addChildren(AccessibilityNodeInfo node,
  165. ArrayList<AccessibilityNodeInfo> outChildren, NodeFilter filter) {
  166. final int childCount = node.getChildCount();
  167. int failedFilter = 0;
  168. for (int i = 0; i < childCount; i++) {
  169. final AccessibilityNodeInfo child = node.getChild(i);
  170. if (child == null) {
  171. continue;
  172. }
  173. if (filter == null || filter.accept(child)) {
  174. outChildren.add(child);
  175. } else {
  176. child.recycle();
  177. failedFilter++;
  178. }
  179. }
  180. return failedFilter;
  181. }
  182. }