Skip to content
TypeScript

fix(server): Device panel Portrait and Landscape no longer swap on Android tablets (#20182)

shivamf6dedece5a43
diff --git a/apps/server/src/device/DeviceActions.test.ts b/apps/server/src/device/DeviceActions.test.ts
index c22dcfe8661..a91236579ca 100644
--- a/apps/server/src/device/DeviceActions.test.ts
+++ b/apps/server/src/device/DeviceActions.test.ts
@@ -113,18 +113,37 @@ describe("runDeviceAction", () => {
"acceleration",
"9.81:0:0",
]);
- const phone = makeReady();
+ // A device that refuses `wm size` keeps the portrait-native mapping.
+ const phone = makeReady((call) =>
+ call.args.join(" ").endsWith("shell wm size") ? { code: 1 } : {},
+ );
yield* runDeviceAction(phone.ready, "android", {
type: "setOrientation",
deviceId: "R5CT1234",
value: "landscape_right",
});
- expect(phone.calls).toEqual([
- {
- command: "adb",
- args: ["-s", "R5CT1234", "shell", "cmd", "window", "user-rotation", "lock", "3"],
- },
- ]);
+ expect(phone.calls.at(-1)).toEqual({
+ command: "adb",
+ args: ["-s", "R5CT1234", "shell", "cmd", "window", "user-rotation", "lock", "3"],
+ });
+ }),
+ );
+
+ it.effect.each([
+ ["emulator-5554", "Physical size: 2560x1600", "portrait", "-9.81:0:0"],
+ ["emulator-5554", "Physical size: 2560x1600", "landscape_left", "0:9.81:0"],
+ ["R5CT1234", "Physical size: 2560x1600", "portrait", "3"],
+ ["R5CT1234", "Physical size: 2560x1600", "landscape_left", "0"],
+ ["R5CT1234", "Physical size: 1080x1920\nOverride size: 1920x1080", "portrait", "3"],
+ ] as const)(
+ "rotates from the natural orientation reported by wm size: %s %j %s",
+ ([deviceId, wmSize, value, expected]) =>
+ Effect.gen(function* () {
+ const { ready, calls } = makeReady((call) =>
+ call.args.join(" ").endsWith("shell wm size") ? { stdout: `${wmSize}\n` } : {},
+ );
+ yield* runDeviceAction(ready, "android", { type: "setOrientation", deviceId, value });
+ expect(calls.at(-1)?.args.at(-1)).toBe(expected);
}),
);
diff --git a/apps/server/src/device/DeviceActions.ts b/apps/server/src/device/DeviceActions.ts
index aaac59e531d..fec54eade8e 100644
--- a/apps/server/src/device/DeviceActions.ts
+++ b/apps/server/src/device/DeviceActions.ts
@@ -154,6 +154,9 @@ const ANDROID_PERMISSIONS: Record<string, ReadonlyArray<string>> = {
motion: ["android.permission.ACTIVITY_RECOGNITION"],
};
+// Android numbers rotations from the display's natural orientation and the emulator's accelerometer
+// reports gravity in that same frame, so both tables are keyed by the orientation a portrait-native
+// device shows at each rotation.
const ANDROID_GRAVITY: Record<DeviceOrientation, string> = {
portrait: "0:9.81:0",
landscape_left: "9.81:0:0",
@@ -166,6 +169,21 @@ const ANDROID_ROTATION: Record<DeviceOrientation, string> = {
portrait_upside_down: "2",
landscape_right: "3",
};
+// A landscape-native tablet sits a quarter turn further along that sequence: its portrait is
+// rotation 3 and its landscape is rotation 0.
+const ANDROID_LANDSCAPE_NATIVE_ROTATION: Record<DeviceOrientation, DeviceOrientation> = {
+ portrait: "landscape_right",
+ landscape_left: "portrait",
+ portrait_upside_down: "landscape_left",
+ landscape_right: "portrait_upside_down",
+};
+
+// `wm size` reports the natural-orientation size; an `Override size:` line is what the window
+// manager uses and is printed after `Physical size:`.
+const isLandscapeNative = (wmSize: string | undefined) => {
+ const size = wmSize ? [...wmSize.matchAll(/size: (\d+)x(\d+)/g)].at(-1) : undefined;
+ return size !== undefined && Number(size[1]) > Number(size[2]);
+};
export const runDeviceAction = Effect.fn("DeviceActions.run")(function* (
ready: DeviceHostReady,
@@ -370,6 +388,10 @@ const runAndroid = Effect.fn("DeviceActions.runAndroid")(function* (
reason: "unsupported",
});
case "setOrientation": {
+ const wmSize = yield* quiet(shell(["wm", "size"], "orientation"));
+ const rotation = isLandscapeNative(wmSize)
+ ? ANDROID_LANDSCAPE_NATIVE_ROTATION[input.value]
+ : input.value;
// `user-rotation lock` only rotates window content on recent images; the display the
// encoder captures stays put. Tilting the emulator's accelerometer rotates it for real, so
// that is used whenever the target is an emulator.
@@ -379,13 +401,13 @@ const runAndroid = Effect.fn("DeviceActions.runAndroid")(function* (
yield* adb(
run,
serial,
- ["emu", "sensor", "set", "acceleration", ANDROID_GRAVITY[input.value]],
+ ["emu", "sensor", "set", "acceleration", ANDROID_GRAVITY[rotation]],
"orientation",
);
return;
}
yield* shell(
- ["cmd", "window", "user-rotation", "lock", ANDROID_ROTATION[input.value]],
+ ["cmd", "window", "user-rotation", "lock", ANDROID_ROTATION[rotation]],
"orientation",
);
return;