Background
Back a few years ago, I asked how TeamViewer allows the user to control the device without normal interaction with the device. I was told it's a special "backdoor" that manufacturers allow specifically for this app, and only possible using root priviledge for other apps.
Seeing that an app like "Airplane Mode Shortcut" allows to toggle airplane mode, by automatic navigation to its screen and toggling the switch, it made me realize this situation has changed.
The problem
It is said in the docs:
Starting with Android 4.0 (API Level 14), accessibility services can act on behalf of users, including changing the input focus and selecting (activating) user interface elements. In Android 4.1 (API Level 16) the range of actions has been expanded to include scrolling lists and interacting with text fields. Accessibility services can also take global actions, such as navigating to the Home screen, pressing the Back button, opening the notifications screen and recent applications list. Android 4.1 also includes a new type of focus, Accessibilty Focus, which makes all visible elements selectable by an accessibility service.
These new capabilities make it possible for developers of accessibility services to create alternative navigation modes such as gesture navigation, and give users with disabilities improved control of their Android devices.
But there is no more information about how to use it. Only samples I've found are at the bottom, but those are very old and a part of the apiDemos bundle.
The question
How do I make a service that can query, focus, click, enter text, and perform other UI related operations?
By implementing
AccessibilityService
(https://developer.android.com/training/accessibility/service.html) you get access to that features.You can either inspect or perform action on the element lastly interacted by user or inspect whole application which currently active.
Intercept user events by implementing
onAccessibilityEvent(AccessibilityEvent event)
, here you can retrieve virtual view (representing original view) withevent.getSource()
and then inspect it withgetClassName()
orgetText()
or anything you find in the documentation.Inspect whole application by calling
getRootInActiveWindow()
and iterate throught tree of virtaul views withgetRootInActiveWindow().getChild(index)
.Both
getRootInActiveWindow()
andevent.getSource()
returnAccessibilityNodeInfo
, on which you can invoke performAction(action) and do something like Click, Set Text, etc..Example: Play Store
Search for 'facebook' app and open it's page on play store, once you opened the play store app.
EDIT: full code below:
MyAccessibilityService
AndroidManifest.xml
res/xml/accessibility_service_config.xml
MainActivity