Skip to content

Commit f6dedec

Browse files
shivamhwpPR Batch Testerclaudejuliusmarminge
authored
fix(server): Device panel Portrait and Landscape no longer swap on Android tablets (pingdotgg#20182)
Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 03c5bc8 commit f6dedec

2 files changed

Lines changed: 50 additions & 9 deletions

File tree

‎apps/server/src/device/DeviceActions.test.ts‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,37 @@ describe("runDeviceAction", () => {
113113
"acceleration",
114114
"9.81:0:0",
115115
]);
116-
const phone = makeReady();
116+
// A device that refuses `wm size` keeps the portrait-native mapping.
117+
const phone = makeReady((call) =>
118+
call.args.join(" ").endsWith("shell wm size") ? { code: 1 } : {},
119+
);
117120
yield* runDeviceAction(phone.ready, "android", {
118121
type: "setOrientation",
119122
deviceId: "R5CT1234",
120123
value: "landscape_right",
121124
});
122-
expect(phone.calls).toEqual([
123-
{
124-
command: "adb",
125-
args: ["-s", "R5CT1234", "shell", "cmd", "window", "user-rotation", "lock", "3"],
126-
},
127-
]);
125+
expect(phone.calls.at(-1)).toEqual({
126+
command: "adb",
127+
args: ["-s", "R5CT1234", "shell", "cmd", "window", "user-rotation", "lock", "3"],
128+
});
129+
}),
130+
);
131+
132+
it.effect.each([
133+
["emulator-5554", "Physical size: 2560x1600", "portrait", "-9.81:0:0"],
134+
["emulator-5554", "Physical size: 2560x1600", "landscape_left", "0:9.81:0"],
135+
["R5CT1234", "Physical size: 2560x1600", "portrait", "3"],
136+
["R5CT1234", "Physical size: 2560x1600", "landscape_left", "0"],
137+
["R5CT1234", "Physical size: 1080x1920\nOverride size: 1920x1080", "portrait", "3"],
138+
] as const)(
139+
"rotates from the natural orientation reported by wm size: %s %j %s",
140+
([deviceId, wmSize, value, expected]) =>
141+
Effect.gen(function* () {
142+
const { ready, calls } = makeReady((call) =>
143+
call.args.join(" ").endsWith("shell wm size") ? { stdout: `${wmSize}\n` } : {},
144+
);
145+
yield* runDeviceAction(ready, "android", { type: "setOrientation", deviceId, value });
146+
expect(calls.at(-1)?.args.at(-1)).toBe(expected);
128147
}),
129148
);
130149

‎apps/server/src/device/DeviceActions.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ const ANDROID_PERMISSIONS: Record<string, ReadonlyArray<string>> = {
154154
motion: ["android.permission.ACTIVITY_RECOGNITION"],
155155
};
156156

157+
// Android numbers rotations from the display's natural orientation and the emulator's accelerometer
158+
// reports gravity in that same frame, so both tables are keyed by the orientation a portrait-native
159+
// device shows at each rotation.
157160
const ANDROID_GRAVITY: Record<DeviceOrientation, string> = {
158161
portrait: "0:9.81:0",
159162
landscape_left: "9.81:0:0",
@@ -166,6 +169,21 @@ const ANDROID_ROTATION: Record<DeviceOrientation, string> = {
166169
portrait_upside_down: "2",
167170
landscape_right: "3",
168171
};
172+
// A landscape-native tablet sits a quarter turn further along that sequence: its portrait is
173+
// rotation 3 and its landscape is rotation 0.
174+
const ANDROID_LANDSCAPE_NATIVE_ROTATION: Record<DeviceOrientation, DeviceOrientation> = {
175+
portrait: "landscape_right",
176+
landscape_left: "portrait",
177+
portrait_upside_down: "landscape_left",
178+
landscape_right: "portrait_upside_down",
179+
};
180+
181+
// `wm size` reports the natural-orientation size; an `Override size:` line is what the window
182+
// manager uses and is printed after `Physical size:`.
183+
const isLandscapeNative = (wmSize: string | undefined) => {
184+
const size = wmSize ? [...wmSize.matchAll(/size: (\d+)x(\d+)/g)].at(-1) : undefined;
185+
return size !== undefined && Number(size[1]) > Number(size[2]);
186+
};
169187

170188
export const runDeviceAction = Effect.fn("DeviceActions.run")(function* (
171189
ready: DeviceHostReady,
@@ -370,6 +388,10 @@ const runAndroid = Effect.fn("DeviceActions.runAndroid")(function* (
370388
reason: "unsupported",
371389
});
372390
case "setOrientation": {
391+
const wmSize = yield* quiet(shell(["wm", "size"], "orientation"));
392+
const rotation = isLandscapeNative(wmSize)
393+
? ANDROID_LANDSCAPE_NATIVE_ROTATION[input.value]
394+
: input.value;
373395
// `user-rotation lock` only rotates window content on recent images; the display the
374396
// encoder captures stays put. Tilting the emulator's accelerometer rotates it for real, so
375397
// that is used whenever the target is an emulator.
@@ -379,13 +401,13 @@ const runAndroid = Effect.fn("DeviceActions.runAndroid")(function* (
379401
yield* adb(
380402
run,
381403
serial,
382-
["emu", "sensor", "set", "acceleration", ANDROID_GRAVITY[input.value]],
404+
["emu", "sensor", "set", "acceleration", ANDROID_GRAVITY[rotation]],
383405
"orientation",
384406
);
385407
return;
386408
}
387409
yield* shell(
388-
["cmd", "window", "user-rotation", "lock", ANDROID_ROTATION[input.value]],
410+
["cmd", "window", "user-rotation", "lock", ANDROID_ROTATION[rotation]],
389411
"orientation",
390412
);
391413
return;

0 commit comments

Comments
 (0)