waterui-cli 0.1.4

Cross-platform tooling for WaterUI applications
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
use color_eyre::eyre::{self, eyre};
use smol::channel::{Receiver, Sender};
use smol::io::AsyncWriteExt;
use smol::process::Command;
use smol::spawn;
use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind};
use tracing::{debug, error};

use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::process::{Output, Stdio};
use std::sync::OnceLock;
use std::time::Duration;

use crate::{
    android::platform::AndroidAbi,
    android::toolchain::AndroidSdk,
    device::{
        ApplicationExit, Artifact, Device, DeviceEvent, FailToRun, LogLevel, RunOptions, Running,
    },
    utils::{parse_whitespace_separated_u32s, run_command_os, run_command_output_os},
};

/// Panic information extracted from logcat.
#[derive(Debug, Clone)]
struct PanicInfo {
    payload: String,
    location: Option<String>,
}

#[derive(Debug, Clone)]
enum AndroidRuntimeEvent {
    Panic(PanicInfo),
    NativeCrash(String),
    ActivityFinished,
}

const ADB_DEVICE_COMMAND_TIMEOUT: Duration = Duration::from_secs(10);
const ANDROID_ACTIVITY_FINISHED_MARKER: &str = "WATERUI_ACTIVITY_FINISHED";

async fn run_bounded_adb_output<A, S>(adb: &Path, args: A, operation: &str) -> eyre::Result<Output>
where
    A: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
{
    let args = args
        .into_iter()
        .map(|argument| argument.as_ref().to_os_string())
        .collect::<Vec<_>>();
    let command = Box::pin(run_command_output_os(adb, &args));
    let timeout = Box::pin(async {
        smol::Timer::after(ADB_DEVICE_COMMAND_TIMEOUT).await;
        Err(eyre!(
            "{operation} timed out after {} seconds",
            ADB_DEVICE_COMMAND_TIMEOUT.as_secs()
        ))
    });

    match futures::future::select(command, timeout).await {
        futures::future::Either::Left((result, _))
        | futures::future::Either::Right((result, _)) => result,
    }
}

async fn run_bounded_adb_command<A, S>(adb: &Path, args: A, operation: &str) -> eyre::Result<String>
where
    A: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
{
    let output = run_bounded_adb_output(adb, args, operation).await?;
    if output.status.success() {
        return Ok(String::from_utf8_lossy(&output.stdout).to_string());
    }

    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);
    let details = if !stderr.is_empty() {
        format!("\nstderr:\n{stderr}")
    } else if !stdout.is_empty() {
        format!("\nstdout:\n{stdout}")
    } else {
        String::new()
    };
    Err(eyre!(
        "{operation} failed with status {}{details}",
        output.status
    ))
}

/// Represents an Android device (physical or emulator).
#[derive(Debug)]
pub struct AndroidDevice {
    identifier: String,
    /// Primary ABI of the device (e.g., "arm64-v8a", "`x86_64`")
    abi: AndroidAbi,
}

impl AndroidDevice {
    /// Create a new Android device with the given identifier and ABI.
    #[must_use]
    pub const fn new(identifier: String, abi: AndroidAbi) -> Self {
        Self { identifier, abi }
    }

    /// Get the device identifier.
    #[must_use]
    pub fn identifier(&self) -> &str {
        &self.identifier
    }

    /// Get the device's primary ABI.
    #[must_use]
    pub const fn abi(&self) -> AndroidAbi {
        self.abi
    }
}

impl Device for AndroidDevice {
    fn name(&self) -> &str {
        &self.identifier
    }

    async fn launch(&self) -> eyre::Result<()> {
        let adb = AndroidSdk::adb_path()
            .ok_or_else(|| eyre::eyre!("Android SDK not found or adb not installed"))?;
        run_command_os(&adb, ["-s", &self.identifier, "wait-for-device"]).await?;
        Ok(())
    }

    async fn run(&self, artifact: Artifact, options: RunOptions) -> Result<Running, FailToRun> {
        run_on_android(&self.identifier, artifact, options).await
    }

    async fn scan() -> eyre::Result<Vec<Self>> {
        let adb = AndroidSdk::adb_path()
            .ok_or_else(|| eyre::eyre!("Android SDK not found or adb not installed"))?;
        Self::scan_with_adb(&adb).await
    }
}

impl AndroidDevice {
    async fn scan_with_adb(adb: &Path) -> eyre::Result<Vec<Self>> {
        let output = run_bounded_adb_command(adb, ["devices", "-l"], "listing Android devices")
            .await
            .map_err(|e| eyre!("Failed to list devices: {e}"))?;

        let mut devices = Vec::new();

        for line in output.lines().skip(1) {
            let parts: Vec<&str> = line.split_whitespace().collect();
            if parts.len() >= 2 && parts[1] == "device" {
                let identifier = parts[0].to_string();

                // Get device ABI
                let abi = run_bounded_adb_command(
                    adb,
                    ["-s", &identifier, "shell", "getprop", "ro.product.cpu.abi"],
                    "querying Android device ABI",
                )
                .await
                .map_err(|e| eyre!("Failed to get device ABI: {e}"))?;
                let abi = abi
                    .trim()
                    .parse::<AndroidAbi>()
                    .map_err(|e| eyre!("Unsupported device ABI: {e}"))?;

                devices.push(Self::new(identifier, abi));
            }
        }

        Ok(devices)
    }
}

/// Provides the Android ABI required to build/package for a target.
pub trait AndroidAbiProvider {
    /// Return the device ABI used for building and packaging.
    fn android_abi(&self) -> AndroidAbi;
}

impl AndroidAbiProvider for AndroidDevice {
    fn android_abi(&self) -> AndroidAbi {
        self.abi()
    }
}

impl AndroidAbiProvider for AndroidEmulator {
    fn android_abi(&self) -> AndroidAbi {
        self.expected_abi()
    }
}

/// Shared implementation for running an app on any Android device.
///
/// This handles:
/// - Passing environment variables as intent extras
/// - Uninstalling previous version (to avoid storage issues)
/// - Installing the APK
/// - Launching the app
/// - Monitoring process state
/// - Streaming logs
async fn run_on_android(
    device_id: &str,
    artifact: Artifact,
    options: RunOptions,
) -> Result<Running, FailToRun> {
    let adb = AndroidSdk::adb_path()
        .ok_or_else(|| FailToRun::Run(eyre!("Android SDK not found or adb not installed")))?;
    let env_vars = collect_android_env_vars(device_id, &options);

    install_android_artifact(&adb, device_id, artifact.path()).await?;
    launch_android_app(
        &adb,
        build_android_start_args(device_id, &artifact, &env_vars),
    )
    .await?;

    // Wait for the process to start and get its PID
    let pid = wait_for_app_pid(&adb, device_id, artifact.bundle_id()).await?;

    let adb_for_kill = adb.clone();
    let device_id_for_kill = device_id.to_string();
    let device_id_for_monitor = device_id.to_string();
    let bundle_id_for_kill = artifact.bundle_id().to_string();
    let bundle_id_for_monitor = artifact.bundle_id().to_string();
    let log_level = options.log_level();

    let (running, sender) = Running::new(move || {
        spawn_android_force_stop(adb_for_kill, device_id_for_kill, bundle_id_for_kill);
    });

    spawn_android_runtime_tasks(
        &adb,
        device_id,
        device_id_for_monitor,
        bundle_id_for_monitor,
        pid,
        log_level,
        sender,
    );

    Ok(running)
}

/// Collects the environment the app process starts with, delivered as
/// `waterui.env.*` intent extras.
///
/// Emulators default to `WGPU_BACKEND=gl` because guest Vulkan in the Android
/// emulator's gfxstream translation is unreliable (its swapchain presentation
/// flips GPU surfaces vertically, and older images ship no guest Vulkan at
/// all). The trade-off is real: wgpu's GLES backend has no compute shaders,
/// so vello-backed components (icons, the GPU map) cannot render under this
/// default — test those on a physical device. Force a backend for one launch
/// with `adb shell am start ... --es waterui.env.WGPU_BACKEND vulkan`;
/// anything already present in `options` wins over the default. Physical
/// devices are never touched — they get whatever wgpu picks, normally Vulkan.
fn collect_android_env_vars(device_id: &str, options: &RunOptions) -> Vec<(String, String)> {
    let mut env_vars = options
        .env_vars()
        .map(|(key, value)| (key.to_string(), value.to_string()))
        .collect::<Vec<_>>();
    if device_id.starts_with("emulator-") && !env_vars.iter().any(|(key, _)| key == "WGPU_BACKEND")
    {
        env_vars.push(("WGPU_BACKEND".to_string(), "gl".to_string()));
    }
    env_vars
}

async fn install_android_artifact(
    adb: &Path,
    device_id: &str,
    artifact_path: &Path,
) -> Result<(), FailToRun> {
    let install_output = Command::new(adb)
        .args(["-s", device_id, "install", "-r"])
        .arg(artifact_path)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .await
        .map_err(|error| FailToRun::Install(eyre!("Failed to install APK: {error}")))?;

    if install_output.status.success() {
        return Ok(());
    }

    Err(FailToRun::Install(eyre!(
        "Failed to install APK:\n{}\n{}",
        String::from_utf8_lossy(&install_output.stdout).trim(),
        String::from_utf8_lossy(&install_output.stderr).trim(),
    )))
}

fn build_android_start_args(
    device_id: &str,
    artifact: &Artifact,
    env_vars: &[(String, String)],
) -> Vec<String> {
    let mut start_args = vec![
        "-s".to_string(),
        device_id.to_string(),
        "shell".to_string(),
        "am".to_string(),
        "start".to_string(),
        "-S".to_string(),
        "-n".to_string(),
        format!("{}/.MainActivity", artifact.bundle_id()),
    ];

    for (key, value) in env_vars {
        start_args.push("--es".to_string());
        start_args.push(format!("waterui.env.{key}"));
        start_args.push(value.clone());
    }

    start_args
}

async fn launch_android_app(adb: &Path, start_args: Vec<String>) -> Result<(), FailToRun> {
    let output = Command::new(adb)
        .args(&start_args)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .await
        .map_err(|error| FailToRun::Launch(eyre!("Failed to launch app: {error}")))?;

    if output.status.success() {
        return Ok(());
    }

    Err(FailToRun::Launch(eyre!(
        "Failed to launch app:\n{}\n{}",
        String::from_utf8_lossy(&output.stdout).trim(),
        String::from_utf8_lossy(&output.stderr).trim(),
    )))
}

fn spawn_android_force_stop(adb: PathBuf, device_id: String, bundle_id: String) {
    let spawn_result = std::thread::Builder::new()
        .name("waterui-android-force-stop".to_string())
        .spawn(move || {
            let result = std::process::Command::new(&adb)
                .args(["-s", &device_id, "shell", "am", "force-stop", &bundle_id])
                .output();

            match result {
                Ok(output) => {
                    tracing::debug!(
                        "Force-stop command executed: status={}, stdout={}, stderr={}",
                        output.status,
                        String::from_utf8_lossy(&output.stdout),
                        String::from_utf8_lossy(&output.stderr)
                    );
                }
                Err(error) => {
                    error!("Failed to stop app {}: {}", bundle_id, error);
                }
            }
        });

    if let Err(error) = spawn_result {
        error!("Failed to spawn force-stop worker thread: {error}");
    }
}

fn spawn_android_runtime_tasks(
    adb: &Path,
    device_id: &str,
    device_id_for_monitor: String,
    bundle_id_for_monitor: String,
    pid: u32,
    log_level: Option<LogLevel>,
    sender: Sender<DeviceEvent>,
) {
    let sender_for_monitor = sender.clone();
    let sender_for_runtime_event = sender.clone();
    let sender_for_logs = sender;
    let adb_for_monitor = adb.to_path_buf();

    smol::spawn(async move {
        monitor_android_process(
            adb_for_monitor,
            &device_id_for_monitor,
            &bundle_id_for_monitor,
            pid,
            sender_for_monitor,
        )
        .await;
    })
    .detach();

    let runtime_event_rx =
        start_android_log_stream(adb, device_id, pid, log_level, sender_for_logs);
    spawn(async move {
        if let Ok(event) = runtime_event_rx.recv().await {
            match event {
                AndroidRuntimeEvent::Panic(info) => {
                    let _ = sender_for_runtime_event
                        .send(DeviceEvent::Crashed(format_android_panic(&info)))
                        .await;
                }
                AndroidRuntimeEvent::NativeCrash(log) => {
                    let _ = sender_for_runtime_event
                        .send(DeviceEvent::Crashed(format!(
                            "Android process crashed.\n\n=== Crash Log ===\n{log}"
                        )))
                        .await;
                }
                AndroidRuntimeEvent::ActivityFinished => {
                    let _ = sender_for_runtime_event
                        .send(DeviceEvent::Exited(ApplicationExit::user_closed()))
                        .await;
                }
            }
        }
    })
    .detach();
}

fn format_android_panic(info: &PanicInfo) -> String {
    let mut msg = format!("Panic: {}", info.payload);
    if let Some(location) = &info.location {
        msg.push('\n');
        msg.push_str("  at ");
        msg.push_str(location);
    }
    msg
}

/// Wait for an app to start and return its PID.
async fn wait_for_app_pid(adb: &Path, device_id: &str, bundle_id: &str) -> Result<u32, FailToRun> {
    for _ in 0..10 {
        smol::Timer::after(std::time::Duration::from_millis(200)).await;
        if let Ok(output) = run_bounded_adb_command(
            adb,
            ["-s", device_id, "shell", "pidof", bundle_id],
            "querying the launched Android process",
        )
        .await
            && let Some(pid) = parse_whitespace_separated_u32s(&output).into_iter().next()
        {
            return Ok(pid);
        }
    }

    // App likely crashed on startup - fetch logcat for crash info
    let crash_info = match run_bounded_adb_command(
        adb,
        [
            "-s",
            device_id,
            "logcat",
            "-d",
            "-t",
            "100",
            "-s",
            "AndroidRuntime:E",
            "DEBUG:*",
            "WaterUI:*",
        ],
        "collecting Android startup crash logs",
    )
    .await
    {
        Ok(output) => output,
        Err(err) => format!("(failed to collect logcat crash info: {err})"),
    };

    let mut error_msg = format!("App {bundle_id} crashed on startup (process not found).\n\n");

    if !crash_info.trim().is_empty() {
        error_msg.push_str("=== Crash Log ===\n");
        error_msg.push_str(&crash_info);
    }

    Err(FailToRun::Launch(eyre!("{}", error_msg)))
}

/// Query the AVD name for a running emulator device via `adb emu avd name`.
///
/// # Errors
/// Returns an error if the device isn't an emulator or adb doesn't return a name.
pub async fn emulator_avd_name_with_adb(adb: &Path, emulator_id: &str) -> eyre::Result<String> {
    if !emulator_id.starts_with("emulator-") {
        eyre::bail!("Not an Android emulator identifier: {emulator_id}");
    }

    let output = run_bounded_adb_command(
        adb,
        ["-s", emulator_id, "emu", "avd", "name"],
        "querying the Android emulator name",
    )
    .await?;
    let name = output.lines().next().unwrap_or_default().trim();
    if name.is_empty() {
        eyre::bail!("Failed to query AVD name for {emulator_id}: empty response");
    }
    Ok(name.to_string())
}

/// Resolve the AVD name for a running emulator device.
///
/// # Errors
/// Returns an error if adb isn't available or the emulator doesn't return a name.
pub async fn emulator_avd_name(emulator_id: &str) -> eyre::Result<String> {
    let adb = AndroidSdk::adb_path()
        .ok_or_else(|| eyre!("Android SDK not found or adb not installed"))?;
    emulator_avd_name_with_adb(&adb, emulator_id).await
}

async fn try_find_running_emulator_for_avd(
    adb: &Path,
    avd_name: &str,
) -> eyre::Result<Option<AndroidDevice>> {
    let devices = AndroidDevice::scan_with_adb(adb).await?;

    for device in devices {
        let id = device.identifier();
        if !id.starts_with("emulator-") {
            continue;
        }

        match emulator_avd_name_with_adb(adb, id).await {
            Ok(name) if name == avd_name => return Ok(Some(device)),
            Ok(_) => {}
            Err(e) => {
                // Some emulator builds may not respond until fully booted.
                debug!("Failed to query AVD name for {id}: {e}");
            }
        }
    }

    Ok(None)
}

async fn adb_emulator_states(adb: &Path) -> eyre::Result<String> {
    let output =
        run_bounded_adb_command(adb, ["devices", "-l"], "querying Android emulator state").await?;
    let states: Vec<String> = output
        .lines()
        .skip(1)
        .map(str::trim)
        .filter(|line| line.starts_with("emulator-"))
        .map(ToOwned::to_owned)
        .collect();

    if states.is_empty() {
        return Ok(String::new());
    }

    Ok(states.join("; "))
}

async fn adb_emulator_boot_completed(adb: &Path, emulator_id: &str) -> bool {
    run_bounded_adb_command(
        adb,
        ["-s", emulator_id, "shell", "getprop", "sys.boot_completed"],
        "querying Android emulator boot completion",
    )
    .await
    .is_ok_and(|value| value.trim() == "1")
}

fn adb_reports_device_ready(output: &str, device_id: &str) -> bool {
    output.lines().skip(1).any(|line| {
        let mut fields = line.split_whitespace();
        fields.next() == Some(device_id) && fields.next() == Some("device")
    })
}

async fn adb_device_is_ready(adb: &Path, device_id: &str) -> eyre::Result<bool> {
    let output =
        run_bounded_adb_command(adb, ["devices", "-l"], "querying Android device readiness")
            .await?;
    Ok(adb_reports_device_ready(&output, device_id))
}

fn command_targets_avd(command: &[OsString], avd_name: &OsStr) -> bool {
    command
        .windows(2)
        .any(|arguments| arguments[0] == "-avd" && arguments[1] == avd_name)
}

async fn avd_process_is_running(avd_name: &str) -> bool {
    let avd_name = OsString::from(avd_name);
    smol::unblock(move || {
        let mut processes = System::new();
        processes.refresh_processes_specifics(
            ProcessesToUpdate::All,
            true,
            ProcessRefreshKind::nothing().with_cmd(UpdateKind::Always),
        );
        processes
            .processes()
            .values()
            .any(|process| command_targets_avd(process.cmd(), &avd_name))
    })
    .await
}

async fn adb_package_manager_ready(adb: &Path, emulator_id: &str) -> bool {
    run_bounded_adb_command(
        adb,
        ["-s", emulator_id, "shell", "pm", "path", "android"],
        "querying Android package manager readiness",
    )
    .await
    .is_ok_and(|output| {
        output
            .lines()
            .any(|line| line.trim().starts_with("package:"))
    })
}

/// Monitor an Android process and send events when it crashes or exits.
async fn monitor_android_process(
    adb: PathBuf,
    device_id: &str,
    bundle_id: &str,
    pid: u32,
    sender: smol::channel::Sender<DeviceEvent>,
) {
    // Check process status periodically
    loop {
        smol::Timer::after(std::time::Duration::from_secs(1)).await;

        // Check if process is still running using pidof
        // Note: We use pidof instead of kill -0 because kill -0 returns "Operation not permitted"
        // when the shell user doesn't have permission to send signals to the app process
        let pids = match query_android_process_pids(&adb, device_id, bundle_id).await {
            Ok(pids) => pids,
            Err(err) => {
                if adb_device_is_ready(&adb, device_id)
                    .await
                    .is_ok_and(|ready| !ready)
                {
                    debug!(
                        "Android device {device_id} disconnected while monitoring {bundle_id}: {err}"
                    );
                    let _ = sender
                        .send(DeviceEvent::Exited(ApplicationExit::user_closed()))
                        .await;
                    break;
                }
                debug!(
                    "Failed to query process state via pidof for {bundle_id} on {device_id}: {err}"
                );
                continue;
            }
        };

        // Check if the process with the same PID is still running.
        let still_running = pids.contains(&pid);

        if !still_running {
            // Try to fetch logs for this PID (best signal for distinguishing crash vs normal exit).
            let pid_arg = format!("--pid={pid}");
            let pid_log_args = vec![
                "-s".to_string(),
                device_id.to_string(),
                "logcat".to_string(),
                "-v".to_string(),
                "threadtime".to_string(),
                "-d".to_string(),
                "-t".to_string(),
                "200".to_string(),
                pid_arg,
                "*:V".to_string(),
            ];
            let pid_log = run_bounded_adb_output(
                &adb,
                pid_log_args
                    .iter()
                    .map(|s| std::ffi::OsStr::new(s.as_str())),
                "collecting Android process exit logs",
            )
            .await
            .map_or_else(
                |err| {
                    debug!("Failed to fetch PID-filtered logcat: {err}");
                    String::new()
                },
                |output| {
                    if output.status.success() {
                        String::from_utf8_lossy(&output.stdout).to_string()
                    } else {
                        debug!("PID-filtered logcat exited with status {}", output.status);
                        String::new()
                    }
                },
            );

            if android_log_looks_like_crash(&pid_log, bundle_id, pid) {
                let crash_log = pid_log;

                let error_msg = if crash_log.trim().is_empty() {
                    format!("Process {bundle_id} crashed.")
                } else {
                    format!("Process {bundle_id} crashed.\n\n=== Crash Log ===\n{crash_log}")
                };

                let _ = sender.send(DeviceEvent::Crashed(error_msg)).await;
            } else {
                let _ = sender
                    .send(DeviceEvent::Exited(ApplicationExit::user_closed()))
                    .await;
            }
            break;
        }
    }
}

async fn query_android_process_pids(
    adb: &Path,
    device_id: &str,
    bundle_id: &str,
) -> eyre::Result<Vec<u32>> {
    let output = run_bounded_adb_output(
        adb,
        ["-s", device_id, "shell", "pidof", bundle_id].map(OsStr::new),
        "querying Android process state",
    )
    .await?;
    let stdout = String::from_utf8_lossy(&output.stdout);
    if output.status.success() || stdout.trim().is_empty() {
        return Ok(parse_whitespace_separated_u32s(&stdout));
    }

    let stderr = String::from_utf8_lossy(&output.stderr);
    eyre::bail!(
        "pidof failed with status {}: {}",
        output.status,
        stderr.trim()
    );
}

fn log_mentions_pid(log: &str, pid: u32) -> bool {
    let pid_str = pid.to_string();
    let pid_lower = format!("pid: {pid}");
    let pid_upper = format!("PID: {pid}");

    log.lines().any(|line| {
        line.split_whitespace().any(|part| part == pid_str)
            || line.contains(&pid_lower)
            || line.contains(&pid_upper)
    })
}

fn android_log_looks_like_crash(log: &str, bundle_id: &str, pid: u32) -> bool {
    if log.trim().is_empty() {
        return false;
    }

    let relevant = log.contains(bundle_id) || log_mentions_pid(log, pid);
    if !relevant {
        return false;
    }

    if android_log_line_looks_like_crash(log) {
        return true;
    }

    // Ensure log lines mention this app before applying AndroidRuntime heuristics.
    if !log.contains(bundle_id) {
        return false;
    }

    // Heuristic: treat AndroidRuntime errors for this process as crash.
    log.contains("AndroidRuntime")
        && (log.contains("E AndroidRuntime") || log.contains("Exception"))
}

fn android_log_line_looks_like_crash(line: &str) -> bool {
    line.contains("FATAL EXCEPTION")
        || line.contains("Fatal signal")
        || line.contains("SIGSEGV")
        || line.contains("SIGABRT")
        || line.contains("SIGBUS")
        || line.contains("SIGILL")
        || line.contains("SIGFPE")
        || line.contains("Abort message:")
        || line.contains("backtrace:")
}

/// Start log streaming from an Android process using logcat.
///
/// Always streams at minimum info level to capture lifecycle completion and panics.
/// Returns a receiver that fires when the Activity finishes or the runtime crashes.
fn start_android_log_stream(
    adb: &Path,
    device_id: &str,
    pid: u32,
    log_level: Option<LogLevel>,
    sender: Sender<DeviceEvent>,
) -> Receiver<AndroidRuntimeEvent> {
    use futures::StreamExt;
    use futures::io::{AsyncBufReadExt, BufReader};

    // Bounded channel with capacity 1 acts as a oneshot for the first terminal event.
    let (runtime_event_tx, runtime_event_rx) = smol::channel::bounded::<AndroidRuntimeEvent>(1);

    // Lifecycle completion is logged at info, so the internal stream must include info even
    // when terminal log display is disabled or configured for a stricter level.
    let priority = match log_level {
        Some(LogLevel::Debug) => 'D',
        Some(LogLevel::Verbose) => 'V',
        Some(LogLevel::Error | LogLevel::Warn | LogLevel::Info) | None => 'I',
    };

    // Build logcat command with PID filter and minimum priority
    let pid_arg = format!("--pid={pid}");
    let mut cmd = Command::new(adb);
    cmd.args(["-s", device_id, "logcat", "-v", "threadtime"])
        .arg(pid_arg)
        .arg(format!("*:{priority}"))
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::null());

    let mut child = match cmd.spawn() {
        Ok(c) => c,
        Err(e) => {
            tracing::warn!("Failed to spawn logcat: {e}");
            return runtime_event_rx;
        }
    };

    let Some(stdout) = child.stdout.take() else {
        return runtime_event_rx;
    };

    let reader = BufReader::new(stdout);
    let mut lines = reader.lines();

    spawn(async move {
        // Parse logcat output and send as DeviceEvent::Log
        // Logcat format: "MM-DD HH:MM:SS.mmm  PID  TID LEVEL TAG: message"
        while let Some(result) = lines.next().await {
            let Ok(line) = result else { break };

            // Extract the first terminal event directly from the live process-filtered stream.
            if let Some(event) = android_runtime_event_from_log_line(&line) {
                let _ = runtime_event_tx.try_send(event);
            }

            // Only send log events to display if user requested logs
            if let Some(requested_level) = log_level {
                let (parsed_level, message) = parse_logcat_line(&line);

                if log_level_allows(requested_level, parsed_level)
                    && sender
                        .try_send(DeviceEvent::Log {
                            level: parsed_level,
                            message,
                        })
                        .is_err()
                {
                    break;
                }
            }
        }

        // Clean up child process
        let _ = child.kill();
    })
    .detach();

    runtime_event_rx
}

fn android_runtime_event_from_log_line(line: &str) -> Option<AndroidRuntimeEvent> {
    if line.contains("panic.payload=")
        && let Some(info) = extract_panic_info_from_log(line)
    {
        return Some(AndroidRuntimeEvent::Panic(info));
    }
    if android_log_line_looks_like_crash(line) {
        return Some(AndroidRuntimeEvent::NativeCrash(line.to_string()));
    }
    if line.contains(ANDROID_ACTIVITY_FINISHED_MARKER) {
        return Some(AndroidRuntimeEvent::ActivityFinished);
    }
    None
}

fn log_level_allows(requested: LogLevel, actual: tracing::Level) -> bool {
    match requested {
        LogLevel::Error => actual == tracing::Level::ERROR,
        LogLevel::Warn => matches!(actual, tracing::Level::ERROR | tracing::Level::WARN),
        LogLevel::Info => matches!(
            actual,
            tracing::Level::ERROR | tracing::Level::WARN | tracing::Level::INFO
        ),
        LogLevel::Debug => actual != tracing::Level::TRACE,
        LogLevel::Verbose => true,
    }
}

/// Extract panic information from a log line containing panic.payload and panic.location fields.
fn extract_panic_info_from_log(line: &str) -> Option<PanicInfo> {
    let mut payload = None;
    let mut location = None;

    // Extract panic.payload="..."
    if let Some(start) = line.find("panic.payload=\"") {
        let start = start + 15;
        if let Some(end) = line[start..].find('"') {
            payload = Some(line[start..start + end].to_string());
        }
    }

    // Extract panic.location="..."
    if let Some(start) = line.find("panic.location=\"") {
        let start = start + 16;
        if let Some(end) = line[start..].find('"') {
            location = Some(line[start..start + end].to_string());
        }
    }

    payload.map(|p| PanicInfo {
        payload: p,
        location,
    })
}

/// Parsed logcat line with level, tag, and message.
struct LogcatParsed {
    level: tracing::Level,
    tag: String,
    message: String,
}

/// Parse a logcat line into level, tag, and message.
/// Logcat threadtime format: "MM-DD HH:MM:SS.mmm  PID  TID LEVEL TAG: message"
fn parse_logcat_line(line: &str) -> (tracing::Level, String) {
    // Try to parse the structured format
    if let Some(parsed) = try_parse_logcat(line) {
        let formatted = format!("[{}] {}", parsed.tag, parsed.message);
        return (parsed.level, formatted);
    }

    // Fallback: return raw line with default level
    (tracing::Level::INFO, line.to_string())
}

/// Try to parse a logcat line. Returns None if parsing fails.
fn try_parse_logcat(line: &str) -> Option<LogcatParsed> {
    // Logcat threadtime format: "MM-DD HH:MM:SS.mmm  PID  TID LEVEL TAG: message"
    // Example: "12-10 23:04:40.190 28184 28184 D WaterUI : Touch..."

    // Split by whitespace, but we need to be careful about the message part
    let parts: Vec<&str> = line.splitn(7, char::is_whitespace).collect();

    // We need at least: date, time, pid, tid, level, tag, message
    if parts.len() < 6 {
        return None;
    }

    // Find the level character (should be single char: V, D, I, W, E, F)
    let mut level_idx = None;
    for (i, part) in parts.iter().enumerate() {
        if part.len() == 1 {
            let c = part.chars().next()?;
            if matches!(c, 'V' | 'D' | 'I' | 'W' | 'E' | 'F') {
                level_idx = Some(i);
                break;
            }
        }
    }

    let level_idx = level_idx?;
    if level_idx + 1 >= parts.len() {
        return None;
    }

    let level = match parts[level_idx] {
        "E" | "F" => tracing::Level::ERROR,
        "W" => tracing::Level::WARN,
        "D" => tracing::Level::DEBUG,
        "V" => tracing::Level::TRACE,
        _ => tracing::Level::INFO,
    };

    // The rest after level is "TAG: message" or "TAG     : message"
    // Find the position of the level character in the original line (after timestamp)
    // Skip past timestamp "MM-DD HH:MM:SS.mmm" which is about 18 chars
    let level_char = parts[level_idx].chars().next()?;
    let search_start = 18.min(line.len());
    let level_pos = line[search_start..]
        .find(level_char)
        .map(|p| p + search_start)?;

    let after_level = line.get(level_pos + 1..)?.trim_start();

    // Split by ": " to get tag and message
    after_level.find(": ").map_or_else(
        || {
            Some(LogcatParsed {
                level,
                tag: "unknown".to_string(),
                message: after_level.to_string(),
            })
        },
        |colon_pos| {
            let tag = after_level[..colon_pos].trim();
            let message = after_level[colon_pos + 2..].to_string();
            Some(LogcatParsed {
                level,
                tag: tag.to_string(),
                message,
            })
        },
    )
}

/// Android emulator (AVD) that needs to be launched.
///
/// Unlike `AndroidDevice` which represents an already-connected device,
/// `AndroidEmulator` represents an AVD that will be launched when `launch()` is called.
#[derive(Debug)]
pub struct AndroidEmulator {
    /// AVD name.
    avd_name: String,
    expected_abi: AndroidAbi,
    device: OnceLock<AndroidDevice>,
}

impl AndroidEmulator {
    /// Open an Android emulator definition by AVD name (reads config to determine ABI).
    ///
    /// # Errors
    /// Returns an error if the AVD config is missing or malformed.
    pub async fn open(avd_name: String) -> eyre::Result<Self> {
        let expected_abi = read_avd_abi(&avd_name).await?;
        Ok(Self {
            avd_name,
            expected_abi,
            device: OnceLock::new(),
        })
    }

    /// Get the AVD name.
    #[must_use]
    pub fn avd_name(&self) -> &str {
        &self.avd_name
    }

    #[must_use]
    /// ABI expected for this AVD (from its config).
    pub const fn expected_abi(&self) -> AndroidAbi {
        self.expected_abi
    }
}

impl Device for AndroidEmulator {
    fn name(&self) -> &str {
        &self.avd_name
    }

    async fn launch(&self) -> eyre::Result<()> {
        let emulator_path =
            AndroidSdk::emulator_path().ok_or_else(|| eyre::eyre!("Android emulator not found"))?;
        let adb_path = AndroidSdk::adb_path()
            .ok_or_else(|| eyre::eyre!("Android SDK not found or adb not installed"))?;

        let mut emulator_process = if avd_process_is_running(&self.avd_name).await {
            debug!(
                "AVD '{}' already has a running emulator process; waiting for it to become ready",
                self.avd_name
            );
            None
        } else {
            // Start the emulator process (don't wait for it here, we'll poll for readiness).
            // Use std::process::Command so we can isolate process-group behavior.
            let mut emulator_cmd = std::process::Command::new(&emulator_path);
            emulator_cmd
                .arg("-avd")
                .arg(&self.avd_name)
                .arg("-no-snapshot-load")
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null());

            #[cfg(unix)]
            {
                use std::os::unix::process::CommandExt as _;
                // Move emulator into its own process group so Ctrl+C in `water run`
                // only stops the CLI/app and doesn't terminate the emulator process.
                emulator_cmd.process_group(0);
            }

            Some(smol::unblock(move || emulator_cmd.spawn()).await?)
        };

        let start = std::time::Instant::now();
        let timeout = std::time::Duration::from_mins(5);
        let mut last_emulator_states = String::new();

        loop {
            if let Some(process) = emulator_process.as_mut()
                && let Some(status) = process.try_wait()?
            {
                if avd_process_is_running(&self.avd_name).await {
                    debug!(
                        "Launched emulator process exited with {status}, but another process owns AVD '{}'; waiting for that instance",
                        self.avd_name
                    );
                    emulator_process = None;
                } else {
                    eyre::bail!(
                        "Emulator process exited before becoming ready (status: {status}). Check AVD configuration and run `emulator -avd {}` manually for details.",
                        self.avd_name
                    );
                }
            } else if emulator_process.is_none() && !avd_process_is_running(&self.avd_name).await {
                eyre::bail!(
                    "Existing emulator process for AVD '{}' exited before becoming ready.",
                    self.avd_name
                );
            }

            if start.elapsed() > timeout {
                let states = if last_emulator_states.is_empty() {
                    "no emulator device reported by adb".to_string()
                } else {
                    last_emulator_states.clone()
                };

                eyre::bail!(
                    "Emulator launch timed out after 300 seconds (ADB state: {}).",
                    states
                );
            }

            last_emulator_states = match adb_emulator_states(&adb_path).await {
                Ok(states) => states,
                Err(err) => format!("failed to query emulator state via adb: {err}"),
            };

            if let Some(device) =
                try_find_running_emulator_for_avd(&adb_path, &self.avd_name).await?
            {
                if device.abi() != self.expected_abi {
                    eyre::bail!(
                        "AVD '{}' expected ABI {}, but running emulator reports {}",
                        self.avd_name,
                        self.expected_abi.as_str(),
                        device.abi().as_str()
                    );
                }

                let emulator_id = device.identifier().to_string();
                let boot_completed = adb_emulator_boot_completed(&adb_path, &emulator_id).await;
                let package_ready = adb_package_manager_ready(&adb_path, &emulator_id).await;

                if !boot_completed || !package_ready {
                    debug!(
                        "Emulator {} detected but not fully ready yet (boot_completed={}, package_ready={})",
                        emulator_id, boot_completed, package_ready
                    );
                    smol::Timer::after(std::time::Duration::from_secs(2)).await;
                    continue;
                }

                self.device
                    .set(device)
                    .map_err(|_| eyre::eyre!("Emulator device already initialized"))?;
                return Ok(());
            }

            smol::Timer::after(std::time::Duration::from_secs(2)).await;
        }
    }

    async fn run(&self, artifact: Artifact, options: RunOptions) -> Result<Running, FailToRun> {
        let device = self.device.get().ok_or_else(|| {
            FailToRun::Run(eyre!(
                "Android emulator '{}' is not launched. Launch it before running.",
                self.avd_name
            ))
        })?;
        run_on_android(device.identifier(), artifact, options).await
    }

    async fn scan() -> eyre::Result<Vec<Self>> {
        // List available AVDs using avdmanager or emulator -list-avds
        let emulator_path =
            AndroidSdk::emulator_path().ok_or_else(|| eyre::eyre!("Android emulator not found"))?;

        let output = Command::new(&emulator_path)
            .arg("-list-avds")
            .output()
            .await
            .map_err(|e| eyre!("Failed to list AVDs: {e}"))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            eyre::bail!("Failed to list AVDs: {}", stderr.trim());
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let mut avds = Vec::new();
        for name in stdout.lines().map(str::trim).filter(|l| !l.is_empty()) {
            avds.push(Self::open(name.to_string()).await?);
        }

        Ok(avds)
    }
}

async fn read_avd_abi(avd_name: &str) -> eyre::Result<AndroidAbi> {
    let home = dirs::home_dir().ok_or_else(|| eyre!("Failed to resolve home directory"))?;
    let config_path = home
        .join(".android/avd")
        .join(format!("{avd_name}.avd"))
        .join("config.ini");

    let content = smol::fs::read_to_string(&config_path)
        .await
        .map_err(|e| eyre!("Failed to read AVD config {}: {e}", config_path.display()))?;

    let abi_value = content
        .lines()
        .filter_map(|line| line.split_once('=').map(|(k, v)| (k.trim(), v.trim())))
        .find_map(|(k, v)| (k == "abi.type").then_some(v))
        .ok_or_else(|| eyre!("AVD config {} missing key abi.type", config_path.display()))?;

    abi_value
        .parse::<AndroidAbi>()
        .map_err(|e| eyre!("Unsupported AVD ABI '{abi_value}': {e}"))
}

/// Capture a screenshot from an Android device.
///
/// Uses `adb exec-out screencap -p` to capture the screen and writes
/// the PNG data directly to the output file.
///
/// # Errors
///
/// Returns an error if the screenshot command fails, the device is not
/// available, or the output file cannot be written.
pub async fn screenshot(device_id: &str, output: &Path) -> eyre::Result<()> {
    let adb = AndroidSdk::adb_path()
        .ok_or_else(|| eyre!("Android SDK not found or adb not installed"))?;

    let output_result = run_bounded_adb_output(
        &adb,
        ["-s", device_id, "exec-out", "screencap", "-p"],
        "capturing the Android device screen",
    )
    .await?;

    if !output_result.status.success() {
        let stderr = String::from_utf8_lossy(&output_result.stderr);
        eyre::bail!("Failed to capture screenshot: {}", stderr.trim());
    }

    // Write the PNG data to the output file
    let mut file = smol::fs::File::create(output).await?;
    file.write_all(&output_result.stdout).await?;
    file.flush().await?;

    Ok(())
}

/// Perform a tap gesture on an Android device at the specified coordinates.
///
/// Uses `adb shell input tap <x> <y>` to simulate a touch event.
///
/// # Errors
///
/// Returns an error if the tap command fails or the device is not available.
pub async fn tap(device_id: &str, x: u32, y: u32) -> eyre::Result<()> {
    let adb = AndroidSdk::adb_path()
        .ok_or_else(|| eyre!("Android SDK not found or adb not installed"))?;

    run_bounded_adb_command(
        &adb,
        [
            "-s",
            device_id,
            "shell",
            "input",
            "tap",
            &x.to_string(),
            &y.to_string(),
        ],
        "performing an Android tap",
    )
    .await?;

    Ok(())
}

/// Perform a swipe gesture on an Android device.
///
/// Uses `adb shell input swipe <x1> <y1> <x2> <y2> [duration_ms]` to simulate a swipe.
///
/// # Arguments
///
/// * `device_id` - The Android device identifier
/// * `from` - Starting coordinates (x, y)
/// * `to` - Ending coordinates (x, y)
/// * `duration_ms` - Optional duration in milliseconds (default ~300ms if not specified)
///
/// # Errors
///
/// Returns an error if the swipe command fails or the device is not available.
pub async fn swipe(
    device_id: &str,
    from: (u32, u32),
    to: (u32, u32),
    duration_ms: Option<u32>,
) -> eyre::Result<()> {
    let adb = AndroidSdk::adb_path()
        .ok_or_else(|| eyre!("Android SDK not found or adb not installed"))?;

    let mut args = vec!["-s", device_id, "shell", "input", "swipe"];

    let x1 = from.0.to_string();
    let y1 = from.1.to_string();
    let x2 = to.0.to_string();
    let y2 = to.1.to_string();
    let duration = duration_ms.map(|d| d.to_string());

    args.push(&x1);
    args.push(&y1);
    args.push(&x2);
    args.push(&y2);

    if let Some(ref d) = duration {
        args.push(d);
    }

    run_bounded_adb_command(&adb, args, "performing an Android swipe").await?;

    Ok(())
}

/// Input text on an Android device.
///
/// Uses `adb shell input text "<string>"` to type text.
/// Note: Special characters may need escaping.
///
/// # Errors
///
/// Returns an error if the text input command fails or the device is not available.
pub async fn text(device_id: &str, input: &str) -> eyre::Result<()> {
    let adb = AndroidSdk::adb_path()
        .ok_or_else(|| eyre!("Android SDK not found or adb not installed"))?;

    // Escape special characters for shell
    let escaped = input
        .replace('\\', "\\\\")
        .replace(' ', "%s")
        .replace('"', "\\\"")
        .replace('\'', "\\'")
        .replace('&', "\\&")
        .replace('<', "\\<")
        .replace('>', "\\>")
        .replace('|', "\\|")
        .replace(';', "\\;")
        .replace('(', "\\(")
        .replace(')', "\\)");

    run_bounded_adb_command(
        &adb,
        ["-s", device_id, "shell", "input", "text", &escaped],
        "entering text on an Android device",
    )
    .await?;

    Ok(())
}

/// Capture a screenshot from an Android device and return the raw PNG bytes.
///
/// This is used for the diff workflow where we need in-memory screenshots.
///
/// # Errors
///
/// Returns an error if the screenshot command fails or the device is not available.
pub async fn screenshot_bytes(device_id: &str) -> eyre::Result<Vec<u8>> {
    let adb = AndroidSdk::adb_path()
        .ok_or_else(|| eyre!("Android SDK not found or adb not installed"))?;

    let output = run_bounded_adb_output(
        &adb,
        ["-s", device_id, "exec-out", "screencap", "-p"],
        "capturing the Android device screen",
    )
    .await?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        eyre::bail!("Failed to capture screenshot: {}", stderr.trim());
    }

    Ok(output.stdout)
}

/// Describe UI elements on the screen.
///
/// Uses `uiautomator dump` to get UI hierarchy as XML, then converts to JSON.
///
/// # Errors
///
/// Returns an error if adb is not available or the command fails.
pub async fn describe(device_id: &str) -> eyre::Result<String> {
    let adb = AndroidSdk::adb_path()
        .ok_or_else(|| eyre!("Android SDK not found or adb not installed"))?;

    // Dump UI hierarchy to a temp file on device
    let dump_path = "/sdcard/window_dump.xml";
    run_bounded_adb_command(
        &adb,
        ["-s", device_id, "shell", "uiautomator", "dump", dump_path],
        "dumping the Android accessibility hierarchy",
    )
    .await?;

    // Read the dump file
    let output = run_bounded_adb_output(
        &adb,
        ["-s", device_id, "shell", "cat", dump_path],
        "reading the Android accessibility hierarchy",
    )
    .await?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        eyre::bail!("Failed to read UI dump: {}", stderr.trim());
    }

    let xml = String::from_utf8_lossy(&output.stdout).to_string();

    // Convert XML to simplified JSON format
    let json = xml_to_ui_json(&xml)?;

    // Clean up
    let _ = run_command_os(&adb, ["-s", device_id, "shell", "rm", dump_path]).await;

    Ok(json)
}

/// Convert Android UI XML dump to a JSON format similar to iOS IDB output.
fn xml_to_ui_json(xml: &str) -> eyre::Result<String> {
    let mut elements = Vec::new();

    // Simple XML parsing - find all <node> elements
    for line in xml.lines() {
        if !line.contains("<node") {
            continue;
        }

        let mut element = serde_json::Map::new();

        // Extract bounds attribute: bounds="[left,top][right,bottom]"
        if let Some(bounds_start) = line.find("bounds=\"[") {
            let bounds_str = &line[bounds_start + 8..];
            if let Some(bounds_end) = bounds_str.find('"') {
                let bounds = &bounds_str[..bounds_end];
                // Parse [left,top][right,bottom]
                let parts: Vec<&str> = bounds
                    .trim_matches(|c| c == '[' || c == ']')
                    .split("][")
                    .collect();
                if parts.len() == 2 {
                    let lt: Vec<i32> = parts[0].split(',').filter_map(|s| s.parse().ok()).collect();
                    let rb: Vec<i32> = parts[1].split(',').filter_map(|s| s.parse().ok()).collect();
                    if lt.len() == 2 && rb.len() == 2 {
                        let mut frame = serde_json::Map::new();
                        frame.insert("x".to_string(), serde_json::Value::Number(lt[0].into()));
                        frame.insert("y".to_string(), serde_json::Value::Number(lt[1].into()));
                        frame.insert(
                            "width".to_string(),
                            serde_json::Value::Number((rb[0] - lt[0]).into()),
                        );
                        frame.insert(
                            "height".to_string(),
                            serde_json::Value::Number((rb[1] - lt[1]).into()),
                        );
                        element.insert("frame".to_string(), serde_json::Value::Object(frame));
                    }
                }
            }
        }

        // Extract common attributes
        for attr in ["text", "content-desc", "class", "resource-id"] {
            let search = format!("{attr}=\"");
            if let Some(start) = line.find(&search) {
                let value_start = start + search.len();
                let rest = &line[value_start..];
                if let Some(end) = rest.find('"') {
                    let value = &rest[..end];
                    if !value.is_empty() {
                        let key = match attr {
                            "content-desc" => "AXLabel",
                            "class" => "type",
                            "resource-id" => "AXUniqueId",
                            "text" => "AXValue",
                            _ => attr,
                        };
                        element.insert(
                            key.to_string(),
                            serde_json::Value::String(value.to_string()),
                        );
                    }
                }
            }
        }

        // Extract clickable/enabled attributes
        if line.contains("clickable=\"true\"") {
            element.insert("clickable".to_string(), serde_json::Value::Bool(true));
        }
        if line.contains("enabled=\"true\"") {
            element.insert("enabled".to_string(), serde_json::Value::Bool(true));
        }

        if !element.is_empty() {
            elements.push(serde_json::Value::Object(element));
        }
    }

    serde_json::to_string(&elements).map_err(|e| eyre!("Failed to serialize UI elements: {e}"))
}

#[cfg(test)]
mod tests {
    use std::ffi::OsString;

    use super::{
        AndroidRuntimeEvent, adb_reports_device_ready, android_log_looks_like_crash,
        android_runtime_event_from_log_line, command_targets_avd, log_level_allows,
        log_mentions_pid,
    };
    use crate::device::LogLevel;

    #[test]
    fn detects_pid_mentions_in_threadtime_lines() {
        let log = "12-10 23:04:40.190 28184 28184 F libc    : Fatal signal 11 (SIGSEGV)\n";
        assert!(log_mentions_pid(log, 28184));
        assert!(!log_mentions_pid(log, 12345));
    }

    #[test]
    fn avoids_false_positive_from_unrelated_fatal_signal_in_global_dump() {
        let unrelated = "12-10 23:04:40.190 999 999 F libc    : Fatal signal 11 (SIGSEGV)\n";
        assert!(!android_log_looks_like_crash(
            unrelated,
            "com.example.app",
            28184
        ));
    }

    #[test]
    fn detects_native_crash_when_pid_is_mentioned() {
        let log = "I DEBUG : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 1 (main) pid: 28184\n";
        assert!(android_log_looks_like_crash(log, "com.example.app", 28184));
    }

    #[test]
    fn detects_java_crash_for_app() {
        let log = "E AndroidRuntime: FATAL EXCEPTION: main\nE AndroidRuntime: Process: com.example.app, PID: 28184\n";
        assert!(android_log_looks_like_crash(log, "com.example.app", 28184));
    }

    #[test]
    fn detects_activity_completion_marker() {
        let event = android_runtime_event_from_log_line(
            "07-26 20:00:00.000 28184 28184 I WaterUI.MainActivity: WATERUI_ACTIVITY_FINISHED",
        );
        assert!(matches!(event, Some(AndroidRuntimeEvent::ActivityFinished)));
    }

    #[test]
    fn filters_internal_lifecycle_logs_from_stricter_user_log_levels() {
        assert!(!log_level_allows(LogLevel::Error, tracing::Level::INFO));
        assert!(!log_level_allows(LogLevel::Warn, tracing::Level::INFO));
        assert!(log_level_allows(LogLevel::Info, tracing::Level::INFO));
        assert!(log_level_allows(LogLevel::Debug, tracing::Level::DEBUG));
        assert!(log_level_allows(LogLevel::Verbose, tracing::Level::TRACE));
    }

    #[test]
    fn matches_emulator_process_to_exact_avd_argument() {
        let command = [
            OsString::from("qemu-system-aarch64"),
            OsString::from("-netdelay"),
            OsString::from("none"),
            OsString::from("-avd"),
            OsString::from("Pixel_9"),
        ];

        assert!(command_targets_avd(&command, "Pixel_9".as_ref()));
        assert!(!command_targets_avd(&command, "Pixel_9_Pro".as_ref()));
    }

    #[test]
    fn does_not_match_unrelated_avd_text() {
        let command = [
            OsString::from("emulator-helper"),
            OsString::from("--log"),
            OsString::from("starting Pixel_9"),
        ];

        assert!(!command_targets_avd(&command, "Pixel_9".as_ref()));
    }

    #[test]
    fn parses_ready_android_device_state() {
        let output = "List of devices attached\nemulator-5554 device product:sdk_phone64_arm64 transport_id:1\n";

        assert!(adb_reports_device_ready(output, "emulator-5554"));
        assert!(!adb_reports_device_ready(output, "emulator-5556"));
    }

    #[test]
    fn rejects_offline_android_device_state() {
        let output = "List of devices attached\nemulator-5554 offline transport_id:1\n";

        assert!(!adb_reports_device_ready(output, "emulator-5554"));
    }
}