@@ -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.
157160const 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 ( / s i z e : ( \d + ) x ( \d + ) / g) ] . at ( - 1 ) : undefined ;
185+ return size !== undefined && Number ( size [ 1 ] ) > Number ( size [ 2 ] ) ;
186+ } ;
169187
170188export 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