Skip to content

Commit d1d3750

Browse files
PR Batch Testerclaude
andcommitted
fix(server): Device panel Portrait and Landscape match on landscape-native Android tablets
Android numbers rotations from the display's natural orientation, so on a landscape-native tablet rotation 0 is landscape. setOrientation now reads wm size and shifts the rotation and emulator gravity a quarter turn when the display is landscape-native. Closes #20177 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2577baf commit d1d3750

2 files changed

Lines changed: 63 additions & 22 deletions

File tree

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "@effect/vitest";
2-
import type { DeviceActionInput } from "@t3tools/contracts";
2+
import type { DeviceActionInput, DeviceOrientation } from "@t3tools/contracts";
33
import * as Effect from "effect/Effect";
44

55
import { readDeviceDetail, runDeviceAction, supportsAction } from "./DeviceActions.ts";
@@ -119,15 +119,50 @@ describe("runDeviceAction", () => {
119119
deviceId: "R5CT1234",
120120
value: "landscape_right",
121121
});
122-
expect(phone.calls).toEqual([
123-
{
124-
command: "adb",
125-
args: ["-s", "R5CT1234", "shell", "cmd", "window", "user-rotation", "lock", "3"],
126-
},
127-
]);
122+
expect(phone.calls.at(-1)).toEqual({
123+
command: "adb",
124+
args: ["-s", "R5CT1234", "shell", "cmd", "window", "user-rotation", "lock", "3"],
125+
});
128126
}),
129127
);
130128

129+
it.effect("shifts rotations a quarter turn on landscape-native displays", () =>
130+
Effect.gen(function* () {
131+
const respond = (call: Call) =>
132+
call.args.join(" ").endsWith("shell wm size")
133+
? { stdout: "Physical size: 2560x1600\n" }
134+
: {};
135+
const orient = (deviceId: string, value: DeviceOrientation) =>
136+
Effect.gen(function* () {
137+
const { ready, calls } = makeReady(respond);
138+
yield* runDeviceAction(ready, "android", { type: "setOrientation", deviceId, value });
139+
return calls.at(-1)?.args.at(-1);
140+
});
141+
expect(yield* orient("emulator-5554", "portrait")).toBe("-9.81:0:0");
142+
expect(yield* orient("emulator-5554", "landscape_left")).toBe("0:9.81:0");
143+
expect(yield* orient("emulator-5554", "portrait_upside_down")).toBe("9.81:0:0");
144+
expect(yield* orient("emulator-5554", "landscape_right")).toBe("0:-9.81:0");
145+
expect(yield* orient("R5CT1234", "portrait")).toBe("3");
146+
expect(yield* orient("R5CT1234", "landscape_left")).toBe("0");
147+
}),
148+
);
149+
150+
it.effect("uses the override size to find the natural orientation", () =>
151+
Effect.gen(function* () {
152+
const { ready, calls } = makeReady((call) =>
153+
call.args.join(" ").endsWith("shell wm size")
154+
? { stdout: "Physical size: 2560x1600\nOverride size: 1080x1920\n" }
155+
: {},
156+
);
157+
yield* runDeviceAction(ready, "android", {
158+
type: "setOrientation",
159+
deviceId: "R5CT1234",
160+
value: "portrait",
161+
});
162+
expect(calls.at(-1)?.args.at(-1)).toBe("0");
163+
}),
164+
);
165+
131166
it.effect("runs accessibility toggles through the bundled helper via simctl spawn", () =>
132167
Effect.gen(function* () {
133168
const { ready, calls } = makeReady();

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

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

157-
const ANDROID_GRAVITY: Record<DeviceOrientation, string> = {
158-
portrait: "0:9.81:0",
159-
landscape_left: "9.81:0:0",
160-
portrait_upside_down: "0:-9.81:0",
161-
landscape_right: "-9.81:0:0",
157+
// Android numbers rotations from the display's natural orientation. On a landscape-native tablet
158+
// ROTATION_0 is landscape and portrait is ROTATION_270, so the requested orientation is shifted by
159+
// a quarter turn before it becomes a rotation or a gravity vector.
160+
const ANDROID_ROTATION_STEPS: Record<DeviceOrientation, number> = {
161+
portrait: 0,
162+
landscape_left: 1,
163+
portrait_upside_down: 2,
164+
landscape_right: 3,
162165
};
163-
const ANDROID_ROTATION: Record<DeviceOrientation, string> = {
164-
portrait: "0",
165-
landscape_left: "1",
166-
portrait_upside_down: "2",
167-
landscape_right: "3",
166+
const ANDROID_GRAVITY = ["0:9.81:0", "9.81:0:0", "0:-9.81:0", "-9.81:0:0"] as const;
167+
168+
// `wm size` prints the physical size and, when one is set, an override size, both in the natural
169+
// orientation. The override is what the window manager uses, and it is printed last.
170+
const isLandscapeNative = (wmSize: string) => {
171+
const sizes = [...wmSize.matchAll(/size: (\d+)x(\d+)/g)];
172+
const size = sizes.at(-1);
173+
return size ? Number(size[1]) > Number(size[2]) : false;
168174
};
169175

170176
export const runDeviceAction = Effect.fn("DeviceActions.run")(function* (
@@ -370,6 +376,9 @@ const runAndroid = Effect.fn("DeviceActions.runAndroid")(function* (
370376
reason: "unsupported",
371377
});
372378
case "setOrientation": {
379+
const wmSize = yield* quiet(shell(["wm", "size"], "orientation"));
380+
const rotation =
381+
(ANDROID_ROTATION_STEPS[input.value] + (isLandscapeNative(wmSize ?? "") ? 3 : 0)) % 4;
373382
// `user-rotation lock` only rotates window content on recent images; the display the
374383
// encoder captures stays put. Tilting the emulator's accelerometer rotates it for real, so
375384
// that is used whenever the target is an emulator.
@@ -379,15 +388,12 @@ const runAndroid = Effect.fn("DeviceActions.runAndroid")(function* (
379388
yield* adb(
380389
run,
381390
serial,
382-
["emu", "sensor", "set", "acceleration", ANDROID_GRAVITY[input.value]],
391+
["emu", "sensor", "set", "acceleration", ANDROID_GRAVITY[rotation]!],
383392
"orientation",
384393
);
385394
return;
386395
}
387-
yield* shell(
388-
["cmd", "window", "user-rotation", "lock", ANDROID_ROTATION[input.value]],
389-
"orientation",
390-
);
396+
yield* shell(["cmd", "window", "user-rotation", "lock", String(rotation)], "orientation");
391397
return;
392398
}
393399
case "setLocation":

0 commit comments

Comments
 (0)