Skip to content

Commit 4034c9c

Browse files
committed
fix(platform-android): read the real display extent around the tree dump (#3182)
Resources.getSystem() reports the app display, which excludes a persistent navigation bar, while the tree's getBoundsInScreen rects span the full panel. Read the real extent instead (WindowManager.getMaximumWindowMetrics on API 30+, Display.getRealMetrics below) before and after each dump on both the one-shot and the session path, and publish it only when the two reads agree so a rotation mid-dump omits the viewport instead of pairing one rotation's bounds with the other's dimensions.
1 parent a31e682 commit 4034c9c

6 files changed

Lines changed: 171 additions & 108 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.callstack.agentdevice.snapshothelper;
2+
3+
import android.content.Context;
4+
import android.graphics.Rect;
5+
import android.os.Build;
6+
import android.util.DisplayMetrics;
7+
import android.view.Display;
8+
import android.view.WindowManager;
9+
10+
/**
11+
* The physical display the captured bounds were measured on, in physical pixels, in the current
12+
* rotation (#3182).
13+
*
14+
* Node bounds reach the host through {@code AccessibilityNodeInfo.getBoundsInScreen()}, which is
15+
* absolute screen space across every interactive window, SystemUI's navigation bar included. The
16+
* extent therefore has to be the real display extent rather than {@code Resources.getSystem()}'s
17+
* app display size: the latter excludes a persistent navigation bar, so a nav-bar node's rect would
18+
* land outside the published box. On API 30+ that extent is
19+
* {@code WindowManager.getMaximumWindowMetrics().getBounds()}; below it, {@code
20+
* Display.getRealMetrics}, which likewise carries the full panel and not the app-usable inset.
21+
*
22+
* The caller reads this twice around the tree dump and publishes only what {@link #whenBothAgree}
23+
* returns. A rotation landing inside the dump changes the extent between the reads, and the capture
24+
* then says nothing about its box rather than pairing one rotation's bounds with the other's
25+
* dimensions. A display this process cannot address answers with null. Either way the host publishes
26+
* the absence it reads as unknown, and the host's own guard is what refuses a zero extent.
27+
*/
28+
final class DisplayExtent {
29+
private DisplayExtent() {}
30+
31+
/** The extent one read of the display answered with, in the current rotation. */
32+
static final class Extent {
33+
final int width;
34+
final int height;
35+
36+
Extent(int width, int height) {
37+
this.width = width;
38+
this.height = height;
39+
}
40+
}
41+
42+
/**
43+
* The extent of the display {@code context} is attached to, or null when this process could not
44+
* address it. A zero extent is passed through as read: refusing an unusable box is the host's job,
45+
* and {@code snapshotViewportSizeFrom} already refuses one, so a second refusal here would only
46+
* be a second place that decides what a usable display is.
47+
*/
48+
static Extent read(Context context) {
49+
try {
50+
WindowManager windowManager =
51+
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
52+
if (windowManager == null) {
53+
return null;
54+
}
55+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
56+
Rect bounds = windowManager.getMaximumWindowMetrics().getBounds();
57+
return new Extent(bounds.width(), bounds.height());
58+
}
59+
return legacyExtent(windowManager);
60+
} catch (Throwable error) {
61+
return null;
62+
}
63+
}
64+
65+
@SuppressWarnings("deprecation")
66+
private static Extent legacyExtent(WindowManager windowManager) {
67+
Display display = windowManager.getDefaultDisplay();
68+
DisplayMetrics metrics = new DisplayMetrics();
69+
display.getRealMetrics(metrics);
70+
return new Extent(metrics.widthPixels, metrics.heightPixels);
71+
}
72+
73+
/**
74+
* The extent both reads of one capture agreed on, or null when either read failed or the display
75+
* changed underneath the tree dump. Omitting the viewport on disagreement is what keeps the
76+
* published box a fact about the capture rather than about two different rotations.
77+
*/
78+
static Extent whenBothAgree(Extent before, Extent after) {
79+
if (before == null || after == null) {
80+
return null;
81+
}
82+
if (before.width != after.width || before.height != after.height) {
83+
return null;
84+
}
85+
return before;
86+
}
87+
}

‎android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/DisplaySizeReader.java‎

Lines changed: 0 additions & 47 deletions
This file was deleted.

‎android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/SnapshotInstrumentation.java‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,17 @@ public void onStart() {
7878
return;
7979
}
8080
long startedAtMs = System.currentTimeMillis();
81+
DisplayExtent.Extent beforeDump = DisplayExtent.read(getContext());
8182
AccessibilityTreeCapture.Result capture =
8283
captureXml(waitForIdleQuietMs, waitForIdleTimeoutMs, timeoutMs, maxDepth, maxNodes);
84+
DisplayExtent.Extent display =
85+
DisplayExtent.whenBothAgree(beforeDump, DisplayExtent.read(getContext()));
8386
writeOutputFile(outputPath, capture.xml);
8487
if (emitChunks) {
8588
emitChunks(capture.xml);
8689
}
8790
result.putString("ok", "true");
88-
putCaptureMetadata(result, capture, System.currentTimeMillis() - startedAtMs);
91+
putCaptureMetadata(result, capture, display, System.currentTimeMillis() - startedAtMs);
8992
finishSafely(0, result);
9093
} catch (Throwable error) {
9194
result.putString("ok", "false");
@@ -115,7 +118,10 @@ private static void putBaseMetadata(
115118
}
116119

117120
private static void putCaptureMetadata(
118-
Bundle result, AccessibilityTreeCapture.Result capture, long elapsedMs) {
121+
Bundle result,
122+
AccessibilityTreeCapture.Result capture,
123+
DisplayExtent.Extent display,
124+
long elapsedMs) {
119125
result.putString("rootPresent", Boolean.toString(capture.rootPresent));
120126
result.putString("captureMode", capture.captureMode);
121127
result.putString("windowCount", Integer.toString(capture.windowCount));
@@ -127,13 +133,11 @@ private static void putCaptureMetadata(
127133
// works in dp has the factor beside the pixels instead of a second adb round trip.
128134
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
129135
result.putString("pixelDensity", Float.toString(metrics.density));
130-
// The same display's extent, so the host can publish the box the bounds are measured in without
131-
// reading the tree for it. One metrics read backs both facts, so a rotation landing between the
132-
// density and the size cannot make them describe two configurations. Published by absence: a
133-
// display this helper could not address adds no keys, which the host reads as unknown rather
134-
// than as a screen of no size (#3182).
135-
DisplaySizeReader.Size display =
136-
DisplaySizeReader.resolve(metrics.widthPixels, metrics.heightPixels);
136+
// The real extent of the display those bounds are measured in, read around the tree dump and
137+
// published only when both reads agreed, so the host can publish the box the bounds live in
138+
// without reading the tree for it. Published by absence: a display this helper could not
139+
// address, or one that rotated mid-dump, adds no keys, which the host reads as unknown rather
140+
// than as a screen of no size or of another rotation's size (#3182).
137141
if (display != null) {
138142
result.putString("displayWidth", Integer.toString(display.width));
139143
result.putString("displayHeight", Integer.toString(display.height));
@@ -244,10 +248,13 @@ private void writeSessionSnapshot(
244248
result.putString("requestId", requestId);
245249
try {
246250
long startedAtMs = System.currentTimeMillis();
251+
DisplayExtent.Extent beforeDump = DisplayExtent.read(getContext());
247252
AccessibilityTreeCapture.Result capture =
248253
captureXml(waitForIdleQuietMs, waitForIdleTimeoutMs, timeoutMs, maxDepth, maxNodes);
254+
DisplayExtent.Extent display =
255+
DisplayExtent.whenBothAgree(beforeDump, DisplayExtent.read(getContext()));
249256
result.putString("ok", "true");
250-
putCaptureMetadata(result, capture, System.currentTimeMillis() - startedAtMs);
257+
putCaptureMetadata(result, capture, display, System.currentTimeMillis() - startedAtMs);
251258
result.putString("byteLength", Integer.toString(capture.xml.getBytes(StandardCharsets.UTF_8).length));
252259
SessionResponseWriter.writeSessionResponse(output, result, capture.xml);
253260
} catch (Throwable error) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.callstack.agentdevice.snapshothelper;
2+
3+
public final class DisplayExtentTest {
4+
private DisplayExtentTest() {}
5+
6+
private static final DisplayExtent.Extent PORTRAIT = new DisplayExtent.Extent(1080, 2400);
7+
private static final DisplayExtent.Extent LANDSCAPE = new DisplayExtent.Extent(2400, 1080);
8+
9+
static void run() {
10+
assertTwoReadsOfTheSameDisplayPublishItsExtent();
11+
assertADisplayThatMovedUnderTheDumpPublishesNothing();
12+
assertAReadThatFailedPublishesNothing();
13+
}
14+
15+
private static void assertTwoReadsOfTheSameDisplayPublishItsExtent() {
16+
// The rotation matters: a landscape capture publishes the box its bounds are actually measured
17+
// in, which is why the extent is read per capture rather than kept across a helper session.
18+
assertExtent(
19+
DisplayExtent.whenBothAgree(PORTRAIT, new DisplayExtent.Extent(1080, 2400)),
20+
1080,
21+
2400,
22+
"both reads of one portrait display");
23+
assertExtent(
24+
DisplayExtent.whenBothAgree(LANDSCAPE, LANDSCAPE),
25+
2400,
26+
1080,
27+
"both reads of one landscape display");
28+
}
29+
30+
private static void assertADisplayThatMovedUnderTheDumpPublishesNothing() {
31+
// A rotation landing inside the tree dump would pair that rotation's node bounds with the other
32+
// rotation's dimensions. The capture then names no box at all rather than a wrong one (#3182).
33+
assertNull(
34+
DisplayExtent.whenBothAgree(PORTRAIT, LANDSCAPE), "display rotated mid-dump");
35+
assertNull(
36+
DisplayExtent.whenBothAgree(
37+
PORTRAIT, new DisplayExtent.Extent(1076, 2400)),
38+
"display resized mid-dump");
39+
}
40+
41+
private static void assertAReadThatFailedPublishesNothing() {
42+
// A read that failed on either side of the dump leaves the box unknown: the surviving read says
43+
// nothing about what the tree between the two was measured against.
44+
assertNull(DisplayExtent.whenBothAgree(null, PORTRAIT), "read before the dump failed");
45+
assertNull(DisplayExtent.whenBothAgree(PORTRAIT, null), "read after the dump failed");
46+
assertNull(DisplayExtent.whenBothAgree(null, null), "both reads failed");
47+
}
48+
49+
private static void assertExtent(
50+
DisplayExtent.Extent extent, int width, int height, String label) {
51+
if (extent == null) {
52+
throw new AssertionError("expected an extent for " + label);
53+
}
54+
if (extent.width != width || extent.height != height) {
55+
throw new AssertionError(
56+
"wrong extent for " + label + ": got " + extent.width + "x" + extent.height);
57+
}
58+
}
59+
60+
private static void assertNull(DisplayExtent.Extent extent, String label) {
61+
if (extent != null) {
62+
throw new AssertionError(
63+
"expected no extent for " + label + " but got " + extent.width + "x" + extent.height);
64+
}
65+
}
66+
}

‎android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/DisplaySizeReaderTest.java‎

Lines changed: 0 additions & 50 deletions
This file was deleted.

‎android/snapshot-helper/src/test/java/com/callstack/agentdevice/snapshothelper/SnapshotHelperTestSuite.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ public static void main(String[] args) throws Exception {
88
AccessibilityCaptureStabilizerTest.run();
99
BoundedUiAutomationConnectionTest.run();
1010
GestureViewportReaderTest.run();
11-
DisplaySizeReaderTest.run();
11+
DisplayExtentTest.run();
1212
}
1313
}

0 commit comments

Comments
 (0)