Skip to content

Commit 79fa227

Browse files
committed
Fix Android rotate control
1 parent f3a3a60 commit 79fa227

2 files changed

Lines changed: 53 additions & 13 deletions

File tree

client/src/app/AppShell.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,23 +1855,21 @@ export function AppShell({
18551855
}
18561856
const androidViewport = isAndroidSimulator(selectedSimulator);
18571857
beginZoomAnimation();
1858-
if (sendControl(selectedSimulator.udid, { type: "rotateRight" })) {
1859-
if (androidViewport) {
1858+
if (androidViewport) {
1859+
void runAction(async () => {
1860+
await rotateRight(selectedSimulator.udid);
18601861
setRotationQuarterTurns(0);
1861-
window.setTimeout(() => void refresh(), 300);
1862-
} else {
1863-
setRotationQuarterTurns((current) => (current + 1) % 4);
1864-
}
1862+
await refresh();
1863+
}, false);
1864+
return;
1865+
}
1866+
if (sendControl(selectedSimulator.udid, { type: "rotateRight" })) {
1867+
setRotationQuarterTurns((current) => (current + 1) % 4);
18651868
return;
18661869
}
18671870
void runAction(async () => {
18681871
await rotateRight(selectedSimulator.udid);
1869-
if (androidViewport) {
1870-
setRotationQuarterTurns(0);
1871-
await refresh();
1872-
} else {
1873-
setRotationQuarterTurns((current) => (current + 1) % 4);
1874-
}
1872+
setRotationQuarterTurns((current) => (current + 1) % 4);
18751873
}, false);
18761874
}}
18771875
onStreamEncoderChange={updateStreamEncoder}

server/src/android.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,26 +460,68 @@ impl AndroidBridge {
460460

461461
fn rotate_by_quarter_turns(&self, id: &str, delta: i16) -> Result<(), AppError> {
462462
let serial = self.serial_for_id(id)?;
463+
self.invalidate_display_metrics_for_serial(&serial);
463464
let current = self
464465
.display_metrics_for_serial(&serial)
465466
.map(|metrics| metrics.rotation_quarter_turns)
466467
.unwrap_or(0);
467468
let next = (i16::try_from(current).unwrap_or(0) + delta).rem_euclid(4) as u16;
468469
let rotation = next.to_string();
470+
let _ = self.run_adb([
471+
"-s",
472+
&serial,
473+
"shell",
474+
"cmd",
475+
"window",
476+
"set-ignore-orientation-request",
477+
"-d",
478+
"0",
479+
"true",
480+
]);
481+
let _ = self.run_adb([
482+
"-s",
483+
&serial,
484+
"shell",
485+
"cmd",
486+
"window",
487+
"fixed-to-user-rotation",
488+
"-d",
489+
"0",
490+
"enabled",
491+
]);
469492
self.run_adb([
470493
"-s",
471494
&serial,
472495
"shell",
473496
"cmd",
474497
"window",
475498
"user-rotation",
499+
"-d",
500+
"0",
476501
"lock",
477502
&rotation,
478503
])?;
479504
self.invalidate_display_metrics_for_serial(&serial);
505+
self.wait_for_display_rotation(&serial, next);
480506
Ok(())
481507
}
482508

509+
fn wait_for_display_rotation(&self, serial: &str, rotation: u16) {
510+
let deadline = Instant::now() + Duration::from_secs(4);
511+
while Instant::now() < deadline {
512+
self.invalidate_display_metrics_for_serial(serial);
513+
if self
514+
.display_metrics_for_serial(serial)
515+
.map(|metrics| metrics.rotation_quarter_turns == rotation)
516+
.unwrap_or(false)
517+
{
518+
return;
519+
}
520+
thread::sleep(Duration::from_millis(100));
521+
}
522+
self.invalidate_display_metrics_for_serial(serial);
523+
}
524+
483525
pub fn toggle_appearance(&self, id: &str) -> Result<(), AppError> {
484526
let serial = self.serial_for_id(id)?;
485527
let current = self.run_adb_shell(&serial, "cmd uimode night")?;
@@ -786,7 +828,7 @@ impl AndroidBridge {
786828
"displayWidth": metrics.width,
787829
"displayHeight": metrics.height,
788830
"frameSequence": 0,
789-
"rotationQuarterTurns": 0,
831+
"rotationQuarterTurns": metrics.rotation_quarter_turns,
790832
})
791833
} else {
792834
json!({

0 commit comments

Comments
 (0)