/** * Initializes a {@link Tree tree} by a given root data. The data will be used to initialize a root {@link Node * node}. * * @param rootData * - data for the root {@link Node node} */ public Tree(T rootData) { root = new Node<T>(rootData); }
/** * Adds all child {@link Node nodes} to the list of children. * * @param children * - the child {@link Node nodes} to be added */ public void addChildren(List<Node<T>> children) { for (Node<T> child : children) { addChild(child); } }
/** * Initializes a {@link Node node} with the given data and children. * * @param data * - the node's data * @param children * - the node's children */ public Node(T data, List<Node<T>> children) { this(data); addChildren(children); }
boolean visibleOnly) { if (nodeInfo == null) { return new Node<AccessibilityElement>(null); return new Node<AccessibilityElement>(null); AccessibilityElement elementData = accessibilityElementBuilder.build(nodeInfo, index); Node<AccessibilityElement> builtNode = new Node<AccessibilityElement>(elementData); builtNode.addChild(childNode);
@Override public String getPathRepresentation(Node<AccessibilityElement> element) { Node<AccessibilityElement> parent = element.getParent(); if (parent != null) { return Integer.toString(parent.getChildren().indexOf(element)); } return ""; }
private static void serialize(Node<AccessibilityElement> node, AccessibilityXmlSerializer xmlBuilder) { AccessibilityElement element = node.getData(); xmlBuilder.addAttribute("bounds", boundsToString(element.getBounds())); for (Node<AccessibilityElement> child : node.getChildren()) { serialize(child, xmlBuilder);
/** * Clears the list of child {@link Node nodes}. */ public void clearChildren() { int childCount = children.size(); for (int i = 0; i < childCount; i++) { removeChild(i); } }
/** * Builds a {@link Tree tree} with {@link AccessibilityElement} based on the given root * {@link AccessibilityNodeInfo}. * * @param root * - the root {@link AccessibilityNodeInfo} on which the {@link Tree tree} is being built * @param visibleOnly * - if <code>true</code> only the visible nodes will be used; if <code>false</code> all nodes will be used * @return a {@link Tree tree} with {@link AccessibilityElement} based on the given root * {@link AccessibilityNodeInfo} */ public static Tree<AccessibilityElement> buildTree(AccessibilityNodeInfo root, boolean visibleOnly) { Node<AccessibilityElement> rootNode = buildNode(root, 0, visibleOnly); Tree<AccessibilityElement> builtTree = new Tree<AccessibilityElement>(rootNode); return builtTree; } }
/** * Produces XML representation of the provided {@link AccessibilityElement accessibility elements} hierarchy, * compatible with the Android`s AccessibilityNodeInfo XML dump (and the screen dump as it is the same thing). * * @param hierarchy * - the hierarchy of {@link AccessibilityElement elements} as a tree * @return the hierarchy as an XML document */ public static String serialize(Tree<AccessibilityElement> hierarchy) { AccessibilityXmlSerializer serializer = new AccessibilityXmlSerializer(); serializer.startTag("hierarchy"); // if the hierarchy starts to contain information about the rotation this should be changed serializer.addAttribute("rotation", "0"); serialize(hierarchy.getRoot(), serializer); serializer.endTag("hierarchy"); return serializer.xmlBuilder.toString(); }