android-emulator 0.2.0

A Rust library for spawning or discovering Android Emulators and controlling them via the gRPC controller protocol.
Documentation
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
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
//! Android Emulator gRPC Control Library
//!
//! This library provides Rust bindings for controlling Android Emulators via gRPC,
//! along with utilities for starting and managing emulator instances.

use std::io;
use std::net::{Ipv4Addr, SocketAddrV4};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use thiserror::Error;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::{Child, Command};
use tokio::task::JoinHandle;

pub mod auth;
pub mod proto;

#[cfg(windows)]
mod windows;

#[cfg(unix)]
mod unix;

pub use proto::emulator_controller_client::EmulatorControllerClient;
use tonic::transport::Channel;

use crate::auth::AuthProvider;

#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;

const EMULATOR_BIN: &str = const {
    if cfg!(windows) {
        "emulator.exe"
    } else {
        "emulator"
    }
};

#[derive(Error, Debug)]
pub enum EmulatorError {
    #[error(
        "Android SDK not found. Checked:\n  - ANDROID_HOME environment variable\n  - ANDROID_SDK_ROOT environment variable\n  - Platform default locations (e.g., ~/Android/sdk)\nPlease install the Android SDK or set ANDROID_HOME"
    )]
    AndroidHomeNotFound,

    #[error("No emulator AVDs found")]
    NoAvdsFound,

    #[error("Failed to spawn or connect to ADB server: {0}")]
    AdbError(String),

    #[error("Android SDK emulator tool not found at path: {0}")]
    EmulatorToolNotFound(String),

    #[error("Invalid gRPC endpoint URI: {0}")]
    InvalidUri(String),

    #[error("Failed to enumerate running emulators: {0}")]
    EnumerationFailed(String),

    #[error("Failed to start emulator: {0}")]
    EmulatorStartFailed(String),

    #[error("Failed to kill emulator: {0}")]
    EmulatorKillFailed(String),

    #[error("Emulator connection timed out")]
    ConnectionTimeout,

    #[error("Authentication error: {0}")]
    AuthError(#[from] crate::auth::AuthError),

    #[error("gRPC connection error: {0}")]
    GrpcError(#[from] tonic::transport::Error),

    #[error("gRPC status error: {0}")]
    GrpcStatus(#[from] tonic::Status),

    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),
}

pub type Result<T> = std::result::Result<T, EmulatorError>;

/// Find a free gRPC port by querying running emulators via ADB
///
/// Returns the first available port starting from 8554, or None if unable to enumerate devices.
async fn find_free_grpc_port() -> Option<u16> {
    use adb_client::emulator::ADBEmulatorDevice;
    use std::collections::HashSet;

    let mut server = adb_server().await.ok()?;

    tokio::task::spawn_blocking(move || {
        let devices = match server.devices() {
            Ok(d) => d,
            Err(_) => return None,
        };

        // Collect all ports currently in use
        let mut used_ports = HashSet::new();
        for device in devices {
            if device.identifier.starts_with("emulator-")
                && let Ok(mut emulator_device) = ADBEmulatorDevice::new(device.identifier, None)
                && let Ok(discovery_path) = emulator_device.avd_discovery_path()
                && let Ok(ini_content) = std::fs::read_to_string(&discovery_path)
            {
                let metadata = parse_ini(&ini_content);
                if let Some(port_str) = metadata.get("grpc.port")
                    && let Ok(port) = port_str.parse::<u16>()
                {
                    used_ports.insert(port);
                }
            }
        }

        // Find first available port starting from 8554
        (8554..8600).find(|&port| !used_ports.contains(&port))
    })
    .await
    .ok()?
}

/// Parse and log an emulator output line based on its prefix
fn log_emulator_line(line: &str) {
    let trimmed = line.trim_start();

    if let Some(rest) = trimmed.strip_prefix("ERROR ") {
        tracing::error!("{}", rest.trim_start());
    } else if let Some(rest) = trimmed.strip_prefix("WARNING ") {
        tracing::warn!("{}", rest.trim_start());
    } else if let Some(rest) = trimmed.strip_prefix("WARN ") {
        tracing::warn!("{}", rest.trim_start());
    } else if let Some(rest) = trimmed.strip_prefix("INFO ") {
        tracing::info!("{}", rest.trim_start());
    } else if let Some(rest) = trimmed.strip_prefix("DEBUG ") {
        tracing::debug!("{}", rest.trim_start());
    } else if let Some(rest) = trimmed.strip_prefix("TRACE ") {
        tracing::trace!("{}", rest.trim_start());
    } else {
        // No recognized prefix, log as debug
        tracing::debug!("{}", line);
    }
}

/// gRPC authentication configuration for the emulator
#[derive(Debug, Clone)]
pub enum GrpcAuthConfig {
    /// No authentication required
    None,
    /// Basic authentication using console auth token as bearer token
    Basic,
    /// JWKS + JWT authentication
    Jwt {
        /// Issuer identifier. If None, will be derived from the port as "emulator-{port}"
        issuer: Option<String>,
    },
}

impl Default for GrpcAuthConfig {
    fn default() -> Self {
        GrpcAuthConfig::Jwt { issuer: None }
    }
}

/// Configuration for starting an Android emulator
#[derive(Debug)]
pub struct EmulatorConfig {
    /// Name of the AVD to start. If None, will use the first available AVD.
    avd_name: String,
    /// gRPC port. If None, a free port will be automatically selected.
    grpc_port: Option<u16>,
    /// gRPC authentication configuration
    grpc_auth: GrpcAuthConfig,
    /// Whether to show the emulator window
    no_window: bool,
    /// Whether to load snapshots
    no_snapshot_load: bool,
    /// Whether to save snapshots
    no_snapshot_save: bool,
    /// Whether to show the boot animation
    no_boot_anim: bool,
    /// Whether to disable hardware acceleration (e.g., for running in CI without KVM)
    no_acceleration: bool,
    /// Whether to pass -dalvik-vm-checkjni flag to the emulator
    dalvik_vm_check_jni: bool,
    /// Allow running multiple instances of the same AVD (without support for snapshots)
    read_only: bool,
    /// Optionally quit the emulator after it has booted and been idle for the specified duration (for testing purposes)
    quit_after_boot: Option<Duration>,
    /// Additional command-line arguments
    extra_args: Vec<String>,
    /// Custom allowlist configuration for JWT authentication
    /// If None, a default allowlist will be generated
    grpc_allowlist: Option<auth::GrpcAllowlist>,
    /// Stdout redirect configuration
    stdout: Option<std::process::Stdio>,
    /// Stderr redirect configuration
    stderr: Option<std::process::Stdio>,
}

impl EmulatorConfig {
    /// Create a new config with a specific AVD name
    pub fn new(avd_name: impl Into<String>) -> Self {
        Self {
            avd_name: avd_name.into(),
            grpc_port: None,
            grpc_auth: GrpcAuthConfig::default(),
            no_window: true,
            no_snapshot_load: false,
            no_snapshot_save: false,
            no_boot_anim: false,
            no_acceleration: false,
            dalvik_vm_check_jni: false,
            read_only: false,
            quit_after_boot: None,
            extra_args: Vec::new(),
            grpc_allowlist: None,
            stdout: None,
            stderr: None,
        }
    }

    /// Get the AVD name that was specified
    pub fn avd_id(&self) -> &str {
        &self.avd_name
    }

    /// Poll ADB until the emulator appears and we can read its metadata
    async fn poll_for_emulator(
        grpc_port: u16,
    ) -> Result<(String, std::collections::HashMap<String, String>, PathBuf)> {
        use adb_client::emulator::ADBEmulatorDevice;

        let mut server = adb_server().await?;
        tokio::task::spawn_blocking(move || {
            loop {
                std::thread::sleep(Duration::from_millis(500));

                let devices = match server.devices() {
                    Ok(d) => d,
                    Err(_) => continue,
                };

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

                    let mut emulator_device =
                        match ADBEmulatorDevice::new(device.identifier.clone(), None) {
                            Ok(d) => d,
                            Err(_) => continue,
                        };

                    let discovery_path = match emulator_device.avd_discovery_path() {
                        Ok(p) => p,
                        Err(_) => continue,
                    };

                    let ini_content = match std::fs::read_to_string(&discovery_path) {
                        Ok(c) => c,
                        Err(_) => continue,
                    };

                    let metadata = parse_ini(&ini_content);

                    // Check if this is our emulator by matching the gRPC port
                    if let Some(port_str) = metadata.get("grpc.port")
                        && let Ok(found_port) = port_str.parse::<u16>()
                        && found_port == grpc_port
                    {
                        return Ok((device.identifier, metadata, discovery_path));
                    }
                }
            }
        })
        .await
        .map_err(|e| EmulatorError::EmulatorStartFailed(format!("Task join error: {}", e)))?
    }

    /// Configure gRPC authentication
    ///
    /// # Examples
    ///
    /// No authentication:
    /// ```no_run
    /// use android_emulator::{EmulatorConfig, GrpcAuthConfig};
    ///
    /// let config = EmulatorConfig::new("test")
    ///     .with_grpc_auth(GrpcAuthConfig::None);
    /// ```
    ///
    /// Basic authentication (console token as bearer):
    /// ```no_run
    /// use android_emulator::{EmulatorConfig, GrpcAuthConfig};
    ///
    /// let config = EmulatorConfig::new("test")
    ///     .with_grpc_auth(GrpcAuthConfig::Basic)
    ///     .with_grpc_port(8554);
    /// ```
    ///
    /// JWT mode with custom issuer:
    /// ```no_run
    /// use android_emulator::{EmulatorConfig, GrpcAuthConfig};
    ///
    /// let config = EmulatorConfig::new("test")
    ///     .with_grpc_auth(GrpcAuthConfig::Jwt {
    ///         issuer: Some("mytool".to_string()),
    ///     })
    ///     .with_grpc_port(8555);
    /// ```
    ///
    /// JWT mode with auto-derived issuer and auto-selected port:
    /// ```no_run
    /// use android_emulator::{EmulatorConfig, GrpcAuthConfig};
    ///
    /// let config = EmulatorConfig::new("test")
    ///     .with_grpc_auth(GrpcAuthConfig::Jwt {
    ///         issuer: None,  // Will be "emulator-{port}"
    ///     });
    /// ```
    pub fn with_grpc_auth(mut self, auth: GrpcAuthConfig) -> Self {
        self.grpc_auth = auth;
        self
    }

    /// Set the gRPC port
    ///
    /// If not specified, a free port will be automatically selected.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use android_emulator::EmulatorConfig;
    ///
    /// let config = EmulatorConfig::new("test")
    ///     .with_grpc_port(8554);
    /// ```
    pub fn with_grpc_port(mut self, port: u16) -> Self {
        self.grpc_port = Some(port);
        self
    }

    /// Configure whether to show the emulator window
    ///
    /// Default is false (headless). Set to true to show the window.
    pub fn with_window(mut self, show: bool) -> Self {
        self.no_window = !show;
        self
    }

    /// Configure whether to load snapshots
    ///
    /// Default is true (load snapshots). Set to false to disable snapshot
    /// loading on startup.
    pub fn with_snapshot_load(mut self, load: bool) -> Self {
        self.no_snapshot_load = !load;
        self
    }

    /// Configure whether to save snapshots on exit
    ///
    /// Default is true (save snapshots). Set to false to disable snapshot
    /// saving on exit.
    pub fn with_snapshot_save(mut self, save: bool) -> Self {
        self.no_snapshot_save = !save;
        self
    }

    /// Configure whether to show the boot animation
    ///
    /// Default is true (show boot animation). Set to false to disable the boot
    /// animation for faster startup.
    pub fn with_boot_animation(mut self, show: bool) -> Self {
        self.no_boot_anim = !show;
        self
    }

    /// Configure whether to disable hardware acceleration (e.g., for running in
    /// CI without KVM)
    ///
    /// Default is false (use hardware acceleration if available). Set to true
    /// to disable hardware acceleration
    pub fn with_acceleration(mut self, enable: bool) -> Self {
        self.no_acceleration = !enable;
        self
    }

    /// Configure whether to pass -dalvik-vm-checkjni flag to the emulator
    ///
    /// This can be useful for testing JNI-related functionality and catching
    /// errors early. Default is false (don't check JNI). Set to true to enable
    /// JNI checking.
    pub fn with_dalvik_vm_check_jni(mut self, enable: bool) -> Self {
        self.dalvik_vm_check_jni = enable;
        self
    }

    /// Configure whether to allow running multiple instances of the same AVD
    /// (without support for snapshots)
    ///
    /// Default is false (don't allow multiple instances). Set to true to allow
    /// multiple instances of the same AVD, but note that snapshots will not
    /// work in this mode.
    pub fn with_read_only(mut self, read_only: bool) -> Self {
        self.read_only = read_only;
        self
    }

    /// Configure the emulator to automatically quit after it has booted and
    /// been idle for the specified duration (for testing purposes)
    ///
    /// This can be useful for testing emulator startup and shutdown in CI
    /// environments.
    ///
    /// Default is None (don't quit automatically). Set to Some(duration) to
    /// enable this behavior.
    pub fn with_quit_after_boot(mut self, duration: Option<Duration>) -> Self {
        self.quit_after_boot = duration;
        self
    }

    pub fn with_extra_args(mut self, args: Vec<String>) -> Self {
        self.extra_args = args;
        self
    }

    /// Set a custom gRPC allowlist configuration for JWT authentication
    ///
    /// This allows fine-grained control over which gRPC methods are accessible
    /// and under what conditions when using JWT authentication. The allowlist
    /// defines three categories of methods:
    ///
    /// - **Unprotected**: Methods that can be invoked without any
    ///   authentication token
    /// - **Allowed**: Methods that can be called with a valid JWT token, even
    ///   without an `aud` claim
    /// - **Protected**: Methods that require the specific method to be present
    ///   in the JWT token's `aud` claim
    ///
    /// If you don't specify a custom allowlist, a default one will be generated
    /// using
    /// [`GrpcAllowlist::default_for_issuer`](auth::GrpcAllowlist::default_for_issuer).
    ///
    /// # Arguments
    ///
    /// * `allowlist` - A [`GrpcAllowlist`](auth::GrpcAllowlist) configuration
    ///
    /// # Example
    ///
    /// ```no_run
    /// use android_emulator::{EmulatorConfig, GrpcAuthConfig, auth::{GrpcAllowlist, AllowlistEntry}};
    ///
    /// let allowlist = GrpcAllowlist {
    ///     unprotected: vec![],  // No methods accessible without auth
    ///     allowlist: vec![
    ///         AllowlistEntry {
    ///             iss: "mytool".to_string(),
    ///             allowed: vec![
    ///                 "/android.emulation.control.EmulatorController/.*".to_string(),
    ///             ],
    ///             protected: vec![
    ///                 "/android.emulation.control.SnapshotService/.*".to_string(),
    ///             ],
    ///         },
    ///     ],
    /// };
    ///
    /// let config = EmulatorConfig::new("test")
    ///     .with_grpc_auth(GrpcAuthConfig::Jwt {
    ///         issuer: Some("mytool".to_string()),
    ///     })
    ///     .with_grpc_allowlist(allowlist);
    /// ```
    ///
    /// # See Also
    ///
    /// - [`with_grpc_auth`](Self::with_grpc_auth) - For configuring
    ///   authentication mode
    /// - [`GrpcAllowlist::default_for_issuer`](auth::GrpcAllowlist::default_for_issuer)
    ///   - For the default allowlist
    pub fn with_grpc_allowlist(mut self, allowlist: auth::GrpcAllowlist) -> Self {
        self.grpc_allowlist = Some(allowlist);
        self
    }

    /// Configure stdout for the emulator process
    pub fn stdout<T: Into<std::process::Stdio>>(mut self, cfg: T) -> Self {
        self.stdout = Some(cfg.into());
        self
    }

    /// Configure stderr for the emulator process
    pub fn stderr<T: Into<std::process::Stdio>>(mut self, cfg: T) -> Self {
        self.stderr = Some(cfg.into());
        self
    }

    /// Start an Android emulator with the given configuration
    pub async fn spawn(self) -> Result<Emulator> {
        let android_home = get_android_home().await?;
        let emulator_path = android_home.join("emulator").join(EMULATOR_BIN);

        if !tokio::fs::try_exists(&emulator_path).await.unwrap_or(false) {
            return Err(EmulatorError::EmulatorToolNotFound(
                emulator_path.display().to_string(),
            ));
        }

        let mut cmd = Command::new(&emulator_path);
        cmd.arg("-avd").arg(&self.avd_name);

        if self.no_window {
            cmd.arg("-no-window");
        }

        if self.no_snapshot_load {
            cmd.arg("-no-snapshot-load");
        }

        if self.no_acceleration {
            cmd.arg("-accel").arg("off");
        }

        if self.no_boot_anim {
            cmd.arg("-no-boot-anim");
        }

        if self.dalvik_vm_check_jni {
            cmd.arg("-dalvik-vm-checkjni");
        }

        if self.read_only {
            cmd.arg("-read-only");
        }

        if let Some(quit_after) = self.quit_after_boot {
            cmd.arg("-quit-after-boot")
                .arg(quit_after.as_secs().to_string());
        }

        // Track whether we're using default piped stdout/stderr for IO forwarding
        let use_default_stdout = self.stdout.is_none();
        let use_default_stderr = self.stderr.is_none();

        // Configure stdout (default to piped and run task to forward output)
        if let Some(stdout) = self.stdout {
            cmd.stdout(stdout);
        } else {
            cmd.stdout(std::process::Stdio::piped());
        }

        // Configure stderr (default to piped and run task to forward output)
        if let Some(stderr) = self.stderr {
            cmd.stderr(stderr);
        } else {
            cmd.stderr(std::process::Stdio::piped());
        }

        cmd.stdin(std::process::Stdio::null());

        // Configure gRPC based on authentication mode
        // All modes start with -grpc <port>
        let grpc_port = match self.grpc_port {
            Some(port) => port,
            None => find_free_grpc_port().await.unwrap_or(8554),
        };
        cmd.arg("-grpc").arg(grpc_port.to_string());

        let issuer = match self.grpc_auth {
            GrpcAuthConfig::None => {
                // No additional flags needed
                None
            }
            GrpcAuthConfig::Basic => {
                // Add -grpc-use-token for basic authentication
                cmd.arg("-grpc-use-token");
                None
            }
            GrpcAuthConfig::Jwt { issuer } => {
                // Derive issuer if not provided
                let issuer = issuer.unwrap_or_else(|| format!("emulator-{}", grpc_port));

                // Create allowlist file
                let allowlist = self
                    .grpc_allowlist
                    .unwrap_or_else(|| auth::GrpcAllowlist::default_for_issuer(&issuer));

                // Write allowlist to temp file
                let allowlist_json = serde_json::to_string_pretty(&allowlist).map_err(|e| {
                    EmulatorError::EmulatorStartFailed(format!(
                        "Failed to serialize allowlist: {}",
                        e
                    ))
                })?;

                let temp_dir = std::env::temp_dir();
                let allowlist_path =
                    temp_dir.join(format!("emulator-allowlist-{}.json", std::process::id()));
                tokio::fs::write(&allowlist_path, allowlist_json).await?;

                cmd.arg("-grpc-allowlist").arg(&allowlist_path);

                // Add -grpc-use-jwt for JWT mode
                cmd.arg("-grpc-use-jwt");

                Some(issuer)
            }
        };

        for arg in &self.extra_args {
            cmd.arg(arg);
        }

        // On Windows, use a dedicated Job Object per emulator to ensure that when we
        // kill the emulator, all child processes (like qemu) are also terminated.
        // On Unix, use a process group for the same purpose.
        #[cfg(windows)]
        let (job, mut process) = crate::windows::EmulatorJob::spawn(cmd)
            .map_err(|e| EmulatorError::EmulatorStartFailed(e.to_string()))?;

        #[cfg(unix)]
        let (process_group, mut process) = crate::unix::EmulatorProcessGroup::spawn(cmd)
            .map_err(|e| EmulatorError::EmulatorStartFailed(e.to_string()))?;

        #[cfg(not(any(windows, unix)))]
        let mut process = cmd
            .spawn()
            .map_err(|e| EmulatorError::EmulatorStartFailed(e.to_string()))?;

        // Create shutdown channel for IO forwarding tasks
        let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);

        // Create IO forwarding task for stdout if piped (default behavior)
        let stdout_task = if use_default_stdout {
            let child_out = process.stdout.take().expect("stdout should be piped");
            let mut shutdown_rx = shutdown_rx.clone();

            let handle = tokio::spawn(async move {
                tracing::info!("Stdout forwarding task started");
                let reader = BufReader::new(child_out);
                let mut lines = reader.lines();

                loop {
                    tokio::select! {
                        res = shutdown_rx.wait_for(|v| *v) => {
                            match res {
                                Ok(_) => tracing::info!("Stdout forwarding task received shutdown signal"),
                                Err(_) => tracing::info!("Stdout forwarding task: shutdown sender dropped"),
                            }
                            break;
                        }
                        result = lines.next_line() => {
                            match result {
                                Ok(Some(line)) => log_emulator_line(&line),
                                Ok(None) => {
                                    tracing::debug!("Stdout EOF reached");
                                    break;
                                }
                                Err(e) => {
                                    tracing::error!("Error reading stdout: {}", e);
                                    return Err(e);
                                }
                            }
                        }
                    }
                }

                tracing::info!("Stdout forwarding task exiting");
                Ok(())
            });
            Some(handle)
        } else {
            None
        };

        // Create IO forwarding task for stderr if piped (default behavior)
        let stderr_task = if use_default_stderr {
            let child_stderr = process.stderr.take().expect("stderr should be piped");
            let mut shutdown_rx = shutdown_rx.clone();

            let handle = tokio::spawn(async move {
                tracing::info!("Stderr forwarding task started");
                let reader = BufReader::new(child_stderr);
                let mut lines = reader.lines();

                loop {
                    tokio::select! {
                        res = shutdown_rx.wait_for(|v| *v) => {
                            match res {
                                Ok(_) => tracing::info!("Stderr forwarding task received shutdown signal"),
                                Err(_) => tracing::info!("Stderr forwarding task: shutdown sender dropped"),
                            }
                            break;
                        }
                        result = lines.next_line() => {
                            match result {
                                Ok(Some(line)) => log_emulator_line(&line),
                                Ok(None) => {
                                    tracing::debug!("Stderr EOF reached");
                                    break;
                                }
                                Err(e) => {
                                    tracing::error!("Error reading stderr: {}", e);
                                    return Err(e);
                                }
                            }
                        }
                    }
                }

                tracing::info!("Stderr forwarding task exiting");
                Ok(())
            });
            Some(handle)
        } else {
            None
        };

        // Poll ADB until the emulator appears and we can read its metadata
        let (serial, metadata, discovery_path) = Self::poll_for_emulator(grpc_port).await?;

        let owned_process = OwnedProcess {
            process,
            #[cfg(windows)]
            job: Some(job),
            #[cfg(unix)]
            process_group: Some(process_group),
            stdout_task,
            stderr_task,
            shutdown_tx: Some(shutdown_tx),
        };

        Ok(Emulator {
            owned_process: Some(tokio::sync::Mutex::new(Some(owned_process))),
            grpc_port,
            serial,
            metadata,
            discovery_path,
            issuer,
        })
    }
}

/// High-level client for controlling an Android Emulator via gRPC
///
/// This is a wrapper around the generated `EmulatorControllerClient` that provides
/// a more convenient API.
pub struct EmulatorClient {
    provider: auth::AuthProvider,
    interceptor: EmulatorControllerClient<
        tonic::service::interceptor::InterceptedService<Channel, auth::AuthProvider>,
    >,
    endpoint: String,
}

impl EmulatorClient {
    /// Try to find an emulator running the specified AVD and connect to it
    pub async fn connect_avd(avd: &str) -> Result<Self> {
        let emulators = list_emulators().await?;
        let matching = emulators
            .into_iter()
            .find(|e| e.avd_id().map(|id| id == avd).unwrap_or(false));
        if let Some(emulator) = matching {
            emulator.connect(Some(Duration::from_secs(30)), true).await
        } else {
            Err(EmulatorError::EmulatorStartFailed(
                "No running emulator found".to_string(),
            ))
        }
    }

    /// Connect to an emulator at the specified endpoint without authentication
    pub async fn connect(endpoint: impl Into<String>) -> Result<Self> {
        let endpoint = endpoint.into();
        let channel = Channel::from_shared(endpoint.clone())
            .map_err(|e| EmulatorError::InvalidUri(e.to_string()))?
            .connect()
            .await?;

        let provider = std::sync::Arc::new(auth::NoOpTokenProvider);
        let provider = auth::AuthProvider::new_with_token_provider(provider);

        Ok(Self {
            interceptor: EmulatorControllerClient::with_interceptor(channel, provider.clone()),
            provider,
            endpoint,
        })
    }

    /// Connect to an emulator with JWT authentication
    pub async fn connect_with_auth(
        endpoint: impl Into<String>,
        provider: auth::AuthProvider,
    ) -> Result<Self> {
        let endpoint = endpoint.into();
        let channel = Channel::from_shared(endpoint.clone())
            .map_err(|e| EmulatorError::InvalidUri(e.to_string()))?
            .connect()
            .await?;

        Ok(Self {
            interceptor: EmulatorControllerClient::with_interceptor(channel, provider.clone()),
            provider,
            endpoint,
        })
    }

    /// Get the authentication scheme used by this client
    pub fn auth_scheme(&self) -> &auth::AuthScheme {
        self.provider.auth_scheme()
    }

    /// Export a bearer token for the given audiences and TTL
    ///
    /// This can be used to export a token with specific, limited audience
    /// claims in order to grant something limited access to the emulator.
    ///
    /// Each audience in `auds` will be included as an `aud` claim in the token
    /// and should correspond to the gRPC method patterns defined in the
    /// allowlist.
    ///
    /// This is only applicable when [`Self::auth_scheme()`] returns
    /// [`auth::AuthScheme::Jwt`].
    pub fn export_token(&self, auds: &[&str], ttl: Duration) -> Result<auth::BearerToken> {
        let token = self.provider.export_token(auds, ttl)?;
        Ok(token)
    }

    /// Get the endpoint this client is connected to
    pub fn endpoint(&self) -> &str {
        &self.endpoint
    }

    /// Get a mutable reference to the generated gRPC client protobuf binding
    pub fn protocol_mut(
        &mut self,
    ) -> &mut EmulatorControllerClient<
        tonic::service::interceptor::InterceptedService<Channel, auth::AuthProvider>,
    > {
        &mut self.interceptor
    }

    /// Get a reference to the generated gRPC client protobuf binding
    pub fn protocol(
        &self,
    ) -> &EmulatorControllerClient<
        tonic::service::interceptor::InterceptedService<Channel, auth::AuthProvider>,
    > {
        &self.interceptor
    }

    /// Wait until the emulator has fully booted
    ///
    /// This method polls the emulator's boot status until it reports as booted,
    /// or until the specified timeout is reached.
    ///
    /// # Arguments
    ///
    /// * `timeout` - Maximum duration to wait for the emulator to boot
    /// * `poll_interval` - Duration to wait between boot status checks (defaults to 2 seconds if None)
    ///
    /// # Returns
    ///
    /// Returns `Ok(Duration)` with the time elapsed until boot completed, or an error if the
    /// timeout is reached or a gRPC error occurs.
    ///
    /// # Errors
    ///
    /// Returns [`EmulatorError::ConnectionTimeout`] if the emulator doesn't boot within the timeout period.
    /// Returns [`EmulatorError::GrpcStatus`] if there's a gRPC communication error.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use android_emulator::EmulatorClient;
    /// use std::time::Duration;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = EmulatorClient::connect("http://localhost:8554").await?;
    ///
    /// // Wait up to 5 minutes for boot, checking every 2 seconds
    /// let elapsed = client.wait_until_booted(Duration::from_secs(300), None).await?;
    /// println!("Emulator booted in {:.1} seconds", elapsed.as_secs_f64());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn wait_until_booted(
        &mut self,
        timeout: Duration,
        poll_interval: Option<Duration>,
    ) -> Result<Duration> {
        let poll_interval = poll_interval.unwrap_or(Duration::from_secs(2));
        let start = std::time::Instant::now();
        let mut attempt = 0;

        loop {
            attempt += 1;
            let status = self.protocol_mut().get_status(()).await?.into_inner();

            if status.booted {
                let elapsed = start.elapsed();
                tracing::info!(
                    "Emulator fully booted after {:.1} seconds ({} attempts)",
                    elapsed.as_secs_f64(),
                    attempt
                );
                return Ok(elapsed);
            }

            tracing::debug!(
                "Boot status: {} (attempt {}, elapsed: {:.1}s)",
                status.booted,
                attempt,
                start.elapsed().as_secs_f64()
            );

            // Check if we've exceeded the timeout
            if start.elapsed() >= timeout {
                return Err(EmulatorError::ConnectionTimeout);
            }

            // Calculate remaining time and sleep for the minimum of poll_interval or remaining time
            let remaining = timeout.saturating_sub(start.elapsed());
            let sleep_duration = poll_interval.min(remaining);

            if sleep_duration.is_zero() {
                return Err(EmulatorError::ConnectionTimeout);
            }

            tokio::time::sleep(sleep_duration).await;
        }
    }

    /// Request a graceful shutdown of the emulator via the gRPC protocol
    ///
    /// This method requests a graceful shutdown by setting the VM state to
    /// `SHUTDOWN` and polls the VM state until it reaches a terminal state or
    /// the timeout is reached.
    ///
    /// This is a protocol-level operation that does not explicitly kill the
    /// emulator process, but a successful shutdown will typically result in the
    /// emulator process exiting on its own.
    ///
    /// If you spawned the emulator then explicitly calling
    /// `Emulator::kill()` or dropping the `Emulator` after a shutdown
    /// request will kill the process if it hasn't already exited cleanly.
    ///
    /// This is the preferred way to stop an emulator as it allows the guest OS
    /// to shut down cleanly, save snapshots, and perform proper cleanup.
    ///
    /// # Arguments
    ///
    /// * `timeout` - Maximum duration to wait for the VM to shut down (defaults
    ///   to 30 seconds if None)
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if the shutdown request was successful and the VM shut
    /// down, or an error if the operation fails.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Setting the VM state fails
    /// - The shutdown timeout is exceeded
    ///
    /// # Example
    ///
    /// ```no_run
    /// use android_emulator::{EmulatorConfig, EmulatorClient};
    /// use std::time::Duration;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = EmulatorConfig::new("test");
    /// let instance = config.spawn().await?;
    /// let mut client = instance.connect(Some(Duration::from_secs(30)), true).await?;
    ///
    /// // ... use the emulator ...
    ///
    /// // Gracefully shutdown via protocol
    /// client.shutdown(None).await?;
    ///
    /// // Clean up the process if we spawned it
    /// instance.kill().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn shutdown(&mut self, timeout: Option<Duration>) -> Result<()> {
        use crate::proto::{VmRunState, vm_run_state::RunState};

        let timeout = timeout.unwrap_or(Duration::from_secs(30));
        let poll_interval = Duration::from_millis(500);

        tracing::info!("Requesting graceful emulator shutdown...");

        // Request shutdown
        let shutdown_state = VmRunState {
            state: RunState::Shutdown as i32,
        };

        self.protocol_mut()
            .set_vm_state(shutdown_state)
            .await
            .map_err(|e| {
                EmulatorError::EmulatorKillFailed(format!(
                    "Failed to set VM state to SHUTDOWN: {}",
                    e
                ))
            })?;

        tracing::info!("Shutdown request sent, waiting for VM to shut down...");

        // Poll VM state until shutdown completes or timeout
        let start = std::time::Instant::now();
        let mut last_state = None;

        loop {
            match self.protocol_mut().get_vm_state(()).await {
                Ok(response) => {
                    let vm_state = response.into_inner();
                    let state = RunState::try_from(vm_state.state).unwrap_or(RunState::Unknown);

                    if last_state != Some(state) {
                        tracing::debug!("VM state: {:?}", state);
                        last_state = Some(state);
                    }

                    // Check if we've reached a terminal state (or if the connection failed,
                    // which likely means the VM has shut down)
                    match state {
                        RunState::Unknown => {
                            // Unknown state might indicate the emulator is shutting down
                            tracing::info!("VM entered unknown state, proceeding with termination");
                            break;
                        }
                        _ => {
                            // Continue polling
                        }
                    }
                }
                Err(e) => {
                    // Connection error likely means the emulator has shut down
                    tracing::info!(
                        "Lost connection to emulator ({}), assuming shutdown complete",
                        e
                    );
                    break;
                }
            }

            // Check timeout
            if start.elapsed() >= timeout {
                tracing::warn!(
                    "Shutdown timeout reached after {:.1} seconds, forcing termination",
                    timeout.as_secs_f64()
                );
                break;
            }

            // Calculate remaining time and sleep
            let remaining = timeout.saturating_sub(start.elapsed());
            let sleep_duration = poll_interval.min(remaining);

            if sleep_duration.is_zero() {
                break;
            }

            tokio::time::sleep(sleep_duration).await;
        }

        tracing::info!("VM shutdown complete");
        Ok(())
    }
}

/// Process state for an emulator instance that was spawned by this crate
///
/// This encapsulates all the state needed to manage a process we own,
/// including the process handle itself, any IO forwarding tasks, and
/// the shutdown channel to coordinate their termination.
#[derive(Debug)]
struct OwnedProcess {
    /// The spawned child process
    process: Child,
    /// Windows Job Object for killing the entire process tree (emulator.exe + qemu)
    #[cfg(windows)]
    job: Option<crate::windows::EmulatorJob>,
    /// Unix process group for killing the entire process tree (emulator + qemu)
    #[cfg(unix)]
    process_group: Option<crate::unix::EmulatorProcessGroup>,
    /// IO forwarding task for stdout (None if stdout was redirected)
    stdout_task: Option<JoinHandle<io::Result<()>>>,
    /// IO forwarding task for stderr (None if stderr was redirected)
    stderr_task: Option<JoinHandle<io::Result<()>>>,
    /// Shutdown channel sender for IO forwarding tasks (None if no IO tasks)
    shutdown_tx: Option<tokio::sync::watch::Sender<bool>>,
}

/// Kill an owned process and clean up its resources
///
/// This is used by both `Emulator::kill()` and `Emulator::drop()` to ensure
/// consistent cleanup behavior.
async fn kill_owned_process(mut owned_process: OwnedProcess) -> Result<()> {
    let pid = owned_process.process.id();

    // Kill the process (this will cause EOF on stdout/stderr pipes)
    if let Some(pid) = pid {
        tracing::info!("Terminating emulator process with PID {}", pid);
    }

    // On Windows, use the job object to kill the entire process tree
    // This ensures that child processes like qemu are also terminated
    #[cfg(windows)]
    {
        if let Some(job) = &owned_process.job {
            if let Err(err) = job.kill() {
                tracing::error!("Failed to kill emulator job: {}", err);
                return Err(EmulatorError::EmulatorKillFailed(err.to_string()));
            }
        } else {
            // Fallback if no job (shouldn't happen for owned processes)
            if let Err(err) = owned_process.process.start_kill() {
                tracing::error!("Failed to kill emulator process: {}", err);
                return Err(EmulatorError::EmulatorKillFailed(err.to_string()));
            }
        }
    }

    // On Unix, use the process group to kill the entire process tree
    // This ensures that child processes like qemu are also terminated
    #[cfg(unix)]
    {
        if let Some(process_group) = &owned_process.process_group {
            if let Err(err) = process_group.kill() {
                tracing::error!("Failed to kill emulator process group: {}", err);
                return Err(EmulatorError::EmulatorKillFailed(err.to_string()));
            }
        } else {
            // Fallback if no process group (shouldn't happen for owned processes)
            if let Err(err) = owned_process.process.start_kill() {
                tracing::error!("Failed to kill emulator process: {}", err);
                return Err(EmulatorError::EmulatorKillFailed(err.to_string()));
            }
        }
    }

    // On other platforms, just kill the child directly
    #[cfg(not(any(windows, unix)))]
    if let Err(err) = owned_process.process.start_kill() {
        tracing::error!("Failed to kill emulator process: {}", err);
        return Err(EmulatorError::EmulatorKillFailed(err.to_string()));
    }

    // We first signal to kill the emulator and wait for it to exit
    // before shutting down the IO tasks to ensure the emulator doesn't
    // get blocked trying to write to a synchronous pipe with no reader.

    // Wait for the process to exit (we already called start_kill or job.kill above)
    let wait_res = match owned_process.process.wait().await {
        Ok(status) => {
            if let Some(pid) = pid {
                tracing::info!(
                    "Emulator process with PID {} has exited with status: {:?}",
                    pid,
                    status
                );
            }
            Ok(())
        }
        Err(err) => {
            tracing::error!("Failed to wait for emulator process to exit: {}", err);
            Err(EmulatorError::EmulatorKillFailed(err.to_string()))
        }
    };

    // Send shutdown signal to IO forwarding tasks
    if let Some(tx) = &owned_process.shutdown_tx {
        tracing::info!("Sending shutdown signal to IO forwarding tasks");
        let _ = tx.send(true);
    }

    // Join the IO tasks if they exist
    if let Some(stdout_task) = owned_process.stdout_task.take() {
        tracing::info!("Joining stdout forwarding task...");
        match stdout_task.await {
            Ok(Ok(())) => {
                tracing::info!("Stdout forwarding task completed successfully")
            }
            Ok(Err(e)) => {
                tracing::warn!("Stdout forwarding task completed with error: {}", e)
            }
            Err(e) => {
                if e.is_cancelled() {
                    tracing::debug!("Stdout forwarding task was cancelled");
                } else {
                    tracing::error!("Failed to join stdout forwarding task: {}", e);
                }
            }
        }
    }

    if let Some(stderr_task) = owned_process.stderr_task.take() {
        tracing::info!("Joining stderr forwarding task...");
        match stderr_task.await {
            Ok(Ok(())) => {
                tracing::info!("Stderr forwarding task completed successfully")
            }
            Ok(Err(e)) => {
                tracing::warn!("Stderr forwarding task completed with error: {}", e)
            }
            Err(e) => {
                if e.is_cancelled() {
                    tracing::debug!("Stderr forwarding task was cancelled");
                } else {
                    tracing::error!("Failed to join stderr forwarding task: {}", e);
                }
            }
        }
    }

    wait_res
}

/// Handle to a running emulator instance
///
/// This can represent an emulator we spawned with an [`EmulatorConfig`], or an
/// already-running emulator we discovered with [`list_emulators`].
#[derive(Debug)]
pub struct Emulator {
    /// Process state if this emulator was spawned by this crate
    /// - `None` for discovered emulators (not owned)
    /// - `Some(Mutex(Some(_)))` for owned emulators with live process
    /// - `Some(Mutex(None))` for owned emulators where process has been killed
    owned_process: Option<tokio::sync::Mutex<Option<OwnedProcess>>>,
    serial: String,
    grpc_port: u16,
    /// Path to the discovery .ini file
    discovery_path: PathBuf,
    /// Runtime metadata from the emulator's discovery .ini file
    metadata: std::collections::HashMap<String, String>,
    /// Issuer identifier for JWT authentication (if spawned with custom issuer)
    issuer: Option<String>,
}

impl Emulator {
    /// Get the serial number of this emulator (if known)
    pub fn serial(&self) -> &str {
        &self.serial
    }

    /// Check if this instance represents an emulator we spawned
    pub fn is_owned(&self) -> bool {
        self.owned_process.is_some()
    }

    pub fn discovery_path(&self) -> &Path {
        self.discovery_path.as_path()
    }

    /// Get a reference to all emulator metadata from the runtime .ini file
    pub fn metadata(&self) -> &std::collections::HashMap<String, String> {
        &self.metadata
    }

    /// Get a specific metadata property value
    pub fn get_metadata(&self, key: &str) -> Option<&str> {
        self.metadata.get(key).map(|s| s.as_str())
    }

    /// Check if this emulator requires JWT authentication
    pub fn requires_jwt_auth(&self) -> bool {
        self.get_metadata("grpc.jwk_active").is_some()
    }

    /// Get the AVD name
    pub fn avd_name(&self) -> Option<&str> {
        self.get_metadata("avd.name")
    }

    /// Get the AVD ID
    pub fn avd_id(&self) -> Option<&str> {
        self.get_metadata("avd.id")
    }

    /// Get the AVD directory path
    pub fn avd_dir(&self) -> Option<&str> {
        self.get_metadata("avd.dir")
    }

    /// Get the emulator version
    pub fn emulator_version(&self) -> Option<&str> {
        self.get_metadata("emulator.version")
    }

    /// Get the emulator build ID
    pub fn emulator_build(&self) -> Option<&str> {
        self.get_metadata("emulator.build")
    }

    /// Get the serial port number
    pub fn port_serial(&self) -> Option<u16> {
        self.get_metadata("port.serial")?.parse().ok()
    }

    /// Get the ADB port number
    pub fn port_adb(&self) -> Option<u16> {
        self.get_metadata("port.adb")?.parse().ok()
    }

    /// Get the command line used to launch the emulator
    pub fn cmdline(&self) -> Option<&str> {
        self.get_metadata("cmdline")
    }

    /// Get the gRPC endpoint URL for this emulator
    pub fn grpc_endpoint(&self) -> String {
        format!("http://localhost:{}", self.grpc_port)
    }

    /// Get the gRPC port
    pub fn grpc_port(&self) -> u16 {
        self.grpc_port
    }

    /// Connect to the emulator's gRPC controller
    ///
    /// This method will automatically retry the connection if it fails, waiting up to the
    /// specified timeout duration. At least one connection attempt is always made before
    /// checking the timeout.
    ///
    /// # Arguments
    ///
    /// * `timeout` - Maximum time to wait for the connection. If `None`, will wait indefinitely.
    /// * `allow_basic_auth` - If `true`, will use basic auth (grpc.token / console token) if available.
    ///
    /// # Errors
    ///
    /// Returns an error if JWT authentication setup fails.
    pub async fn connect(
        &self,
        timeout: Option<Duration>,
        allow_basic_auth: bool,
    ) -> Result<EmulatorClient> {
        // Check if this emulator requires JWT authentication
        let basic_auth_token = self.get_metadata("grpc.token");

        if self.requires_jwt_auth() {
            tracing::info!(
                "Emulator requires JWT authentication, setting up ES256 token provider..."
            );

            // Connect with JWT authentication
            match self.connect_with_jwt_auth(timeout).await {
                Ok(client) => {
                    tracing::info!("Connected to emulator with JWT authentication.");
                    return Ok(client);
                }
                Err(err) => {
                    tracing::error!("Failed to connect with JWT authentication: {}", err);
                    if basic_auth_token.is_some() && allow_basic_auth {
                        tracing::warn!("Falling back to basic authentication...");
                    } else {
                        return Err(err);
                    }
                }
            }
        } else {
            tracing::info!("Emulator does not require JWT authentication.");
        }

        // Check if this emulator accepts basic auth (grpc.token)
        // This has a lower priority than JWT auth because its mostly only intended for Android Studio use
        if allow_basic_auth && let Some(token) = basic_auth_token {
            tracing::info!("Emulator accepts basic auth, setting up BasicAuthTokenProvider...");
            return self.connect_with_basic_auth(token, timeout).await;
        }

        // Connect without authentication (use no-op provider)
        self.connect_with_noop_auth(timeout).await
    }

    /// Connect with no-op authentication (internal helper for unauth connections)
    async fn connect_with_noop_auth(&self, timeout: Option<Duration>) -> Result<EmulatorClient> {
        let start = std::time::Instant::now();

        let provider = Arc::new(auth::NoOpTokenProvider);
        let provider = AuthProvider::new_with_token_provider(provider);
        loop {
            match EmulatorClient::connect_with_auth(self.grpc_endpoint(), provider.clone()).await {
                Ok(mut client) => {
                    // Try a simple call to verify the connection
                    if client.protocol_mut().get_status(()).await.is_ok() {
                        return Ok(client);
                    }
                }
                Err(err) => {
                    tracing::error!("No-auth connection attempt failed: {}", err);
                }
            }

            // Check timeout after the connection attempt
            if let Some(timeout_duration) = timeout
                && start.elapsed() > timeout_duration
            {
                return Err(EmulatorError::ConnectionTimeout);
            }

            tokio::time::sleep(Duration::from_secs(1)).await;
        }
    }

    /// Connect with basic auth token (internal helper for token-based connections)
    async fn connect_with_basic_auth(
        &self,
        token: &str,
        timeout: Option<Duration>,
    ) -> Result<EmulatorClient> {
        let start = std::time::Instant::now();

        let provider = Arc::new(auth::BearerTokenProvider::new(token.to_string()));
        let provider = AuthProvider::new_with_token_provider(provider);

        loop {
            match EmulatorClient::connect_with_auth(self.grpc_endpoint(), provider.clone()).await {
                Ok(mut client) => {
                    // Try a simple call to verify the connection
                    if client.protocol_mut().get_status(()).await.is_ok() {
                        return Ok(client);
                    }
                }
                Err(err) => {
                    tracing::error!("Basic auth connection attempt failed: {}", err);
                }
            }

            // Check timeout after the connection attempt
            if let Some(timeout_duration) = timeout
                && start.elapsed() > timeout_duration
            {
                return Err(EmulatorError::ConnectionTimeout);
            }

            tokio::time::sleep(Duration::from_secs(1)).await;
        }
    }

    /// Connect with ES256 authentication (internal helper for JWT connections)
    ///
    /// This will verify the JWT connection works, then return an unauthenticated client
    /// (since after JWT activation, the emulator will accept unauthenticated connections too)
    async fn connect_with_jwt_auth(&self, timeout: Option<Duration>) -> Result<EmulatorClient> {
        // Get the JWKS directory from metadata
        let jwks_path = self.get_metadata("grpc.jwks").ok_or_else(|| {
            EmulatorError::EmulatorStartFailed(
                "Emulator requires JWT auth but grpc.jwks path not found in metadata".to_string(),
            )
        })?;

        let jwks_dir = PathBuf::from(jwks_path);

        let issuer = self
            .issuer
            .as_deref()
            .unwrap_or("android-studio")
            .to_string();

        // Generate and register key
        let jwt_provider = tokio::task::spawn_blocking(
            move || -> std::result::Result<_, crate::auth::AuthError> {
                tracing::info!(
                    "Generating and registering JWT token provider with issuer '{}'",
                    issuer
                );
                let provider = auth::JwtTokenProvider::new_and_register(&jwks_dir, issuer)?;

                tracing::info!("JWT token provider registered, waiting for activation...");
                // Wait for activation (30 second timeout)
                provider.wait_for_activation(&jwks_dir, Duration::from_secs(10))?;

                let provider = AuthProvider::new_with_token_provider(provider);

                Ok(provider)
            },
        )
        .await
        .map_err(|err| {
            EmulatorError::EmulatorStartFailed(format!(
                "Failure running task to register JWT token provider: {err}"
            ))
        })??;

        let start = std::time::Instant::now();

        loop {
            tracing::info!("Attempting JWT connection...");
            match EmulatorClient::connect_with_auth(self.grpc_endpoint(), jwt_provider.clone())
                .await
            {
                Ok(mut client) => {
                    tracing::info!("JWT authentication successful.");
                    // Try a simple call to verify the JWT connection works
                    match client.protocol_mut().get_status(()).await {
                        Ok(_) => {
                            tracing::info!(
                                "Successfully connected to emulator with JWT authentication."
                            );
                            return Ok(client);
                        }
                        Err(err) => {
                            tracing::error!(
                                "Failed to get status with JWT authentication: {}",
                                err
                            );
                        }
                    }
                }
                Err(err) => {
                    tracing::error!("JWT connection attempt failed: {}", err);
                }
            }

            // Check timeout after the connection attempt
            if let Some(timeout_duration) = timeout
                && start.elapsed() > timeout_duration
            {
                return Err(EmulatorError::ConnectionTimeout);
            }
            tracing::info!("Sleeping before retrying JWT connection...");
            tokio::time::sleep(Duration::from_secs(1)).await;
        }
    }

    /// Kill the emulator process and wait for it to fully exit
    ///
    /// If this instance owns the process (spawned via `spawn()`), this will kill the process
    /// and wait for it to exit.
    ///
    /// If this instance was discovered via `find()`, this returns an error as we don't own
    /// the process.
    ///
    /// To kill a discovered emulator, use ADB commands directly.
    pub async fn kill(&self) -> Result<()> {
        // Check if this emulator is owned
        let Some(mutex) = &self.owned_process else {
            tracing::warn!("kill() called on an emulator that is not owned by this instance");
            return Ok(());
        };

        // Take ownership of the process state
        let owned = mutex.lock().await.take();

        if let Some(owned_process) = owned {
            kill_owned_process(owned_process).await?;
            tracing::info!("Emulator killed successfully");
            Ok(())
        } else {
            tracing::warn!("kill() called but process was already killed");
            Ok(())
        }
    }
}

impl Drop for Emulator {
    fn drop(&mut self) {
        // We have exclusive ownership (&mut self), so we can use get_mut() directly
        // without needing to lock, which avoids panics in async contexts

        if let Some(mutex) = &mut self.owned_process
            && let Some(owned_process) = mutex.get_mut().take()
        {
            // Spawn a background task to kill the process asynchronously
            // This ensures proper cleanup even when dropping outside an async context
            tokio::task::spawn(async move {
                if let Err(e) = kill_owned_process(owned_process).await {
                    tracing::error!("Failed to kill emulator in Drop: {}", e);
                }
            });
        }
    }
}

/// Get the Android SDK home directory
///
/// This function tries to find the Android SDK in the following order:
/// 1. ANDROID_HOME environment variable
/// 2. ANDROID_SDK_ROOT environment variable
/// 3. Platform-specific default locations:
///    - Linux: $HOME/Android/sdk or $HOME/Android/Sdk
///    - macOS: $HOME/Library/Android/sdk
///    - Windows: %LOCALAPPDATA%/Android/Sdk
pub async fn get_android_home() -> Result<PathBuf> {
    // Try environment variables first
    if let Ok(path) = std::env::var("ANDROID_HOME") {
        return Ok(PathBuf::from(path));
    }

    if let Ok(path) = std::env::var("ANDROID_SDK_ROOT") {
        return Ok(PathBuf::from(path));
    }

    // Try platform-specific default locations
    #[cfg(target_os = "linux")]
    {
        if let Some(home) = dirs::home_dir() {
            // Try $HOME/Android/sdk first (lowercase)
            let sdk_path = home.join("Android").join("sdk");
            if tokio::fs::try_exists(&sdk_path).await.unwrap_or(false) {
                return Ok(sdk_path);
            }

            // Try $HOME/Android/Sdk (capitalized)
            let sdk_path = home.join("Android").join("Sdk");
            if tokio::fs::try_exists(&sdk_path).await.unwrap_or(false) {
                return Ok(sdk_path);
            }
        }
    }

    #[cfg(target_os = "macos")]
    {
        if let Some(home) = dirs::home_dir() {
            let sdk_path = home.join("Library").join("Android").join("sdk");
            if tokio::fs::try_exists(&sdk_path).await.unwrap_or(false) {
                return Ok(sdk_path);
            }
        }
    }

    #[cfg(target_os = "windows")]
    {
        if let Some(local_data) = dirs::data_local_dir() {
            let sdk_path = local_data.join("Android").join("Sdk");
            if tokio::fs::try_exists(&sdk_path).await.unwrap_or(false) {
                return Ok(sdk_path);
            }
        }
    }

    Err(EmulatorError::AndroidHomeNotFound)
}

/// Find available Android Virtual Devices (AVDs)
pub async fn list_avds() -> Result<Vec<String>> {
    let android_home = get_android_home().await?;

    tokio::task::spawn_blocking(move || {
        let emulator_path = android_home.join("emulator").join(EMULATOR_BIN);

        if !emulator_path.exists() {
            return Err(EmulatorError::EmulatorToolNotFound(
                emulator_path.display().to_string(),
            ));
        }

        let output = std::process::Command::new(&emulator_path)
            .arg("-list-avds")
            .output()?;

        let avds: Vec<String> = String::from_utf8_lossy(&output.stdout)
            .lines()
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect();

        if avds.is_empty() {
            Err(EmulatorError::NoAvdsFound)
        } else {
            Ok(avds)
        }
    })
    .await
    .map_err(|e| EmulatorError::EmulatorStartFailed(format!("Task join error: {}", e)))?
}

/// Parse a simple INI-style file with key=value pairs
fn parse_ini(content: &str) -> std::collections::HashMap<String, String> {
    content
        .lines()
        .filter_map(|line| {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                return None;
            }
            line.split_once('=')
                .map(|(k, v)| (k.trim().to_string(), v.trim().to_string()))
        })
        .collect()
}

async fn adb_server() -> Result<adb_client::server::ADBServer> {
    use adb_client::server::ADBServer;

    let android_home = get_android_home().await?;
    let adb_path = android_home.join("platform-tools").join("adb");
    let adb_path: String = adb_path
        .to_str()
        .ok_or_else(|| EmulatorError::AdbError("Invalid Android home path".to_string()))?
        .to_string();
    let addr = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 5037);

    tokio::task::spawn_blocking(move || Ok(ADBServer::new_from_path(addr, Some(adb_path))))
        .await
        .map_err(|e| EmulatorError::AdbError(format!("Task join error: {}", e)))?
}

/// Enumerates all running emulators that are discoverable via ADB and
/// returns their metadata
///
/// This is useful for finding emulators that were launched outside of this
/// library, such as those launched by Android Studio. It reads the emulator
/// metadata from the discovery path .ini files, which include the gRPC port
/// and authentication settings.
///
/// This does not connect to any emulators; use `connect()` on one of the
/// returned instance to establish a gRPC connection.
pub async fn list_emulators() -> Result<Vec<Emulator>> {
    use adb_client::emulator::ADBEmulatorDevice;

    let mut server = adb_server().await?;

    tokio::task::spawn_blocking(move || {
        let mut emulators = vec![];

        let devices = server.devices().map_err(|e| {
            EmulatorError::IoError(std::io::Error::other(format!(
                "Failed to list ADB devices: {}",
                e
            )))
        })?;

        // Find emulator devices that don't require JWT authentication
        for device in devices {
            if device.identifier.starts_with("emulator-") {
                // Convert to ADBEmulatorDevice to access emulator-specific methods
                let mut emulator_device = ADBEmulatorDevice::new(device.identifier.clone(), None)
                    .map_err(|e| {
                    EmulatorError::IoError(std::io::Error::other(format!(
                        "Failed to create ADBEmulatorDevice: {}",
                        e
                    )))
                })?;

                // Get the discovery path for the runtime metadata
                if let Ok(discovery_path) = emulator_device.avd_discovery_path()
                    && let Ok(ini_content) = std::fs::read_to_string(&discovery_path)
                {
                    let metadata = parse_ini(&ini_content);

                    if let Some(port_str) = metadata.get("grpc.port")
                        && let Ok(grpc_port) = port_str.parse::<u16>()
                    {
                        emulators.push(Emulator {
                            owned_process: None,
                            grpc_port,
                            serial: device.identifier.clone(),
                            metadata,
                            discovery_path: discovery_path.clone(),
                            issuer: None,
                        });
                    }
                }
            }
        }

        Ok(emulators)
    })
    .await
    .map_err(|e| EmulatorError::EnumerationFailed(format!("Task join error: {}", e)))?
}

/// Try to connect to an emulator, or start a new one if none is running
///
/// This function attempts to connect to the first emulator it can find
/// that's running the same AVD associated with the provided configuration,
/// but if no running emulator is found, it will start a new one with the provided
/// configuration.
///
/// Returns both the connected client and an optional `Emulator` instance
/// representing any newly spawned emulator (which will be `None` if we
/// connected to an existing emulator).
pub async fn connect_or_start_emulator(
    config: EmulatorConfig,
) -> Result<(EmulatorClient, Option<Emulator>)> {
    // Try to connect to existing emulator first
    if let Ok(client) = EmulatorClient::connect_avd(config.avd_id()).await {
        tracing::info!("Connected to existing emulator");
        return Ok((client, None));
    }

    tracing::info!("No existing emulator found, starting new one...");
    // Start a new emulator
    let instance = config.spawn().await?;
    tracing::info!("Emulator started at: {}", instance.grpc_endpoint());

    // Wait for it to be ready and connect
    tracing::info!("Waiting for emulator to be ready...");
    let client = instance
        .connect(Some(Duration::from_secs(120)), true)
        .await?;
    tracing::info!("Connected to new emulator");

    Ok((client, Some(instance)))
}