|
| 1 | +package com.termux.api.apis; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.Intent; |
| 5 | + |
| 6 | +import com.termux.api.TermuxApiReceiver; |
| 7 | +import com.termux.api.util.ResultReturner; |
| 8 | +import com.termux.shared.logger.Logger; |
| 9 | + |
| 10 | +import java.io.PrintWriter; |
| 11 | + |
| 12 | +import com.termux.api.TermuxAccessibilityService; |
| 13 | +import android.view.accessibility.AccessibilityNodeInfo; |
| 14 | +import android.accessibilityservice.GestureDescription; |
| 15 | +import android.graphics.Path; |
| 16 | + |
| 17 | +import javax.xml.parsers.DocumentBuilder; |
| 18 | +import javax.xml.parsers.ParserConfigurationException; |
| 19 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 20 | +import javax.xml.transform.Transformer; |
| 21 | +import javax.xml.transform.TransformerFactory; |
| 22 | +import javax.xml.transform.TransformerException; |
| 23 | +import javax.xml.transform.OutputKeys; |
| 24 | +import javax.xml.transform.dom.DOMSource; |
| 25 | +import javax.xml.transform.stream.StreamResult; |
| 26 | +import org.w3c.dom.Document; |
| 27 | +import org.w3c.dom.Element; |
| 28 | +import java.io.StringWriter; |
| 29 | + |
| 30 | +import android.graphics.Rect; |
| 31 | + |
| 32 | +import android.content.ContentResolver; |
| 33 | + |
| 34 | +public class AccessibilityAPI { |
| 35 | + |
| 36 | + private static final String LOG_TAG = "AccessibilityAPI"; |
| 37 | + |
| 38 | + public static void onReceive(TermuxApiReceiver apiReceiver, final Context context, Intent intent) { |
| 39 | + Logger.logDebug(LOG_TAG, "onReceive"); |
| 40 | + |
| 41 | + ResultReturner.returnData(apiReceiver, intent, out -> { |
| 42 | + final ContentResolver contentResolver = context.getContentResolver(); |
| 43 | + if (intent.hasExtra("dump")) { |
| 44 | + out.print(dump()); |
| 45 | + } |
| 46 | + else if (intent.hasExtra("x") && intent.hasExtra("y")) { |
| 47 | + click(intent.getIntExtra("x", 0), intent.getIntExtra("y", 0)); |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + private static void click(int x, int y) { |
| 53 | + Path swipePath = new Path(); |
| 54 | + swipePath.moveTo(x, y); |
| 55 | + GestureDescription.Builder gestureBuilder = new GestureDescription.Builder(); |
| 56 | + gestureBuilder.addStroke(new GestureDescription.StrokeDescription(swipePath, 0, 1)); |
| 57 | + TermuxAccessibilityService.instance.dispatchGesture(gestureBuilder.build(), null, null); |
| 58 | + } |
| 59 | + |
| 60 | + // The aim of this function is to give a compatible output with `adb` `uiautomator dump`. |
| 61 | + private static String dump() throws TransformerException, ParserConfigurationException { |
| 62 | + // Create a DocumentBuilder |
| 63 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 64 | + DocumentBuilder builder = factory.newDocumentBuilder(); |
| 65 | + |
| 66 | + // Create a new Document |
| 67 | + Document document = builder.newDocument(); |
| 68 | + |
| 69 | + // Create root element |
| 70 | + Element root = document.createElement("hierarchy"); |
| 71 | + document.appendChild(root); |
| 72 | + |
| 73 | + AccessibilityNodeInfo root = TermuxAccessibilityService.instance.getRootInActiveWindow(); |
| 74 | + |
| 75 | + dumpNodeAuxiliary(document, root, node); |
| 76 | + |
| 77 | + // Write as XML |
| 78 | + TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 79 | + Transformer transformer = transformerFactory.newTransformer(); |
| 80 | + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 81 | + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); |
| 82 | + DOMSource source = new DOMSource(document); |
| 83 | + |
| 84 | + StringWriter sw = new StringWriter(); |
| 85 | + StreamResult result = new StreamResult(sw); |
| 86 | + transformer.transform(source, result); |
| 87 | + |
| 88 | + return sw.toString(); |
| 89 | + } |
| 90 | + |
| 91 | + private static void dumpNodeAuxiliary(Document document, Element element, AccessibilityNodeInfo node) { |
| 92 | + for (int i = 0; i < node.getChildCount(); i++) { |
| 93 | + AccessibilityNodeInfo nodeChild = node.getChild(i); |
| 94 | + Element elementChild = document.createElement("node"); |
| 95 | + |
| 96 | + elementChild.setAttribute("index", String.valueOf(i)); |
| 97 | + |
| 98 | + elementChild.setAttribute("text", getCharSequenceAsString(nodeChild.getText())); |
| 99 | + |
| 100 | + String nodeChildViewIdResourceName = nodeChild.getViewIdResourceName(); |
| 101 | + elementChild.setAttribute("resource-id", nodeChildViewIdResourceName != null ? nodeChildViewIdResourceName : ""); |
| 102 | + |
| 103 | + elementChild.setAttribute("class", nodeChild.getClassName().toString()); |
| 104 | + |
| 105 | + elementChild.setAttribute("package", nodeChild.getPackageName().toString()); |
| 106 | + |
| 107 | + elementChild.setAttribute("content-desc", getCharSequenceAsString(nodeChild.getContentDescription())); |
| 108 | + |
| 109 | + elementChild.setAttribute("checkable", String.valueOf(nodeChild.isCheckable())); |
| 110 | + |
| 111 | + elementChild.setAttribute("checked", String.valueOf(nodeChild.isChecked())); |
| 112 | + |
| 113 | + elementChild.setAttribute("clickable", String.valueOf(nodeChild.isClickable())); |
| 114 | + |
| 115 | + elementChild.setAttribute("enabled", String.valueOf(nodeChild.isEnabled())); |
| 116 | + |
| 117 | + elementChild.setAttribute("focusable", String.valueOf(nodeChild.isFocusable())); |
| 118 | + |
| 119 | + elementChild.setAttribute("focused", String.valueOf(nodeChild.isFocused())); |
| 120 | + |
| 121 | + elementChild.setAttribute("scrollable", String.valueOf(nodeChild.isScrollable())); |
| 122 | + |
| 123 | + elementChild.setAttribute("long-clickable", String.valueOf(nodeChild.isLongClickable())); |
| 124 | + |
| 125 | + elementChild.setAttribute("password", String.valueOf(nodeChild.isPassword())); |
| 126 | + |
| 127 | + elementChild.setAttribute("selected", String.valueOf(nodeChild.isSelected())); |
| 128 | + |
| 129 | + Rect nodeChildBounds = new Rect(); |
| 130 | + nodeChild.getBoundsInScreen(nodeChildBounds); |
| 131 | + elementChild.setAttribute("bounds", nodeChildBounds.toShortString()); |
| 132 | + |
| 133 | + elementChild.setAttribute("drawing-order", String.valueOf(nodeChild.getDrawingOrder())); |
| 134 | + |
| 135 | + elementChild.setAttribute("hint", getCharSequenceAsString(nodeChild.getHintText())); |
| 136 | + |
| 137 | + element.appendChild(elementChild); |
| 138 | + dumpNodeAuxiliary(document, elementChild, nodeChild); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static String getCharSequenceAsString(CharSequence charSequence) { |
| 143 | + return charSequence != null ? charSequence.toString() : ""; |
| 144 | + } |
| 145 | +} |
0 commit comments