On AOSP with system app using Java Android API: how to run an app in a virtual display and retrieve its window UI tree?
2/2
Apr 4, 11:57pm
To run an Android app in a virtual display and retrieve its window UI tree on AOSP (Android Open Source Project) using Java/Android APIs, you’ll need to combine several Android system-level features. Here’s a high-level approach and the key APIs/libraries you’ll use:
1. Create a Virtual Display
A virtual display allows you to render an app’s UI into a virtual screen, which you can then capture or inspect.
Key Classes/APIs
VirtualDisplay (via WindowManager or DisplayManager)
SurfaceControl (for capturing the display)
MediaProjection (for screen mirroring/capture)
Steps:
Request MediaProjection permission (if capturing the screen):
java
// Request user permission for screen capture
Intent intent = mediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(intent, REQUEST_CODE);
To inspect the app’s window hierarchy (e.g., for UI testing or accessibility), use:
Key Classes/APIs
AccessibilityService (for UI tree inspection)
WindowManager (for window hierarchy)
UiAutomation (for dumping the UI hierarchy)
Steps:
Implement an AccessibilityService:
java
public class MyAccessibilityService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
// Handle accessibility events (e.g., window state changes)
}
@Override
public void onInterrupt() {}
}
Declare it in your AndroidManifest.xml.
Dump the UI Hierarchy:
java
UiAutomation uiAutomation = getUiAutomation();
AccessibilityNodeInfo root = uiAutomation.getRootInActiveWindow();
// Traverse the tree or dump it to XML/JSON
Log.d("UI_TREE", root.toString());
Use adb shell dumpsys window (for debugging):
bash
adb shell dumpsys window windows
3. Capture the Virtual Display
If you want to capture the virtual display (e.g., for screenshots or video):
Would you like a more detailed code example for a specific part (e.g., virtual display setup or UI tree dumping)? Or are you targeting a specific AOSP version?
Apr 4, 11:57pm
What `getUiAutomation()` consists in?
Apr 5, 12:05am
Empty message
Apr 5, 12:05am
This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.