droidkraft 2.1.5

DroidKraft — Android device monitor GUI: live logs, one-click commands, flash/root toolkit, and screen mirroring (Zed GPUI)
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
//! Framework-light application model: routing, per-device state, and the
//! ordered [`Fleet`] that routes worker responses to the right device.
//!
//! Everything here is constructible without a running gpui app so the
//! ordering / selection / routing logic is unit-testable.

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

use gpui::{Entity, Subscription, WindowHandle};

use droidkraft_core::features::flash::RootStatus;
use droidkraft_core::features::rom::{
    DeviceProfile, DownloadProgress, FlashSession, InstallMethod, RomBuild,
};
use droidkraft_core::{
    AdbDeviceEntry, DeviceDetails, DeviceHistory, LogEntry, LogcatFilter, LogcatStream,
};
use droidkraft_mirror::{default_output_dir, RecordOptions, ScreenRecorder, ScreenStream};

use crate::mirror_view::{DetachedRoot, MirrorView};

/// Maximum number of log entries retained per device.
pub const MAX_LOG_ENTRIES: usize = 20_000;

/// Workspace tabs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Tab {
    Overview,
    Logs,
    Commands,
    Flash,
    Screen,
}

impl Tab {
    pub fn label(&self) -> &'static str {
        match self {
            Tab::Overview => "Overview",
            Tab::Logs => "Logs",
            Tab::Commands => "Commands",
            Tab::Flash => "Flash & Root",
            Tab::Screen => "Screen",
        }
    }

    /// Short label for narrow windows.
    pub fn short_label(&self) -> &'static str {
        match self {
            Tab::Overview => "Stats",
            Tab::Logs => "Logs",
            Tab::Commands => "Cmds",
            Tab::Flash => "Flash",
            Tab::Screen => "Screen",
        }
    }

    /// Single-letter label for very narrow windows.
    pub fn initial(&self) -> &'static str {
        match self {
            Tab::Overview => "O",
            Tab::Logs => "L",
            Tab::Commands => "C",
            Tab::Flash => "F",
            Tab::Screen => "S",
        }
    }

    pub fn all() -> &'static [Tab] {
        &[
            Tab::Overview,
            Tab::Logs,
            Tab::Commands,
            Tab::Flash,
            Tab::Screen,
        ]
    }
}

/// What the main area is showing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Route {
    /// Fleet overview (home).
    Devices,
    /// A device workspace.
    Device(String),
    Settings,
}

/// What a saved capture is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CaptureKind {
    Recording,
    Screenshot,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Capture {
    pub kind: CaptureKind,
    pub path: PathBuf,
}

/// Candidate output folders for recordings / screenshots.
pub fn output_dir_choices() -> Vec<PathBuf> {
    let mut v = vec![default_output_dir()];
    if let Some(home) = std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE")) {
        for sub in ["Desktop", "Downloads"] {
            let d = PathBuf::from(&home).join(sub);
            if d.is_dir() && !v.contains(&d) {
                v.push(d);
            }
        }
    }
    let tmp = std::env::temp_dir();
    if !v.contains(&tmp) {
        v.push(tmp);
    }
    v
}

/// Next bit-rate preset (Mbps), wrapping.
pub fn next_bitrate(cur: u32) -> u32 {
    const P: [u32; 5] = [4, 8, 12, 16, 24];
    P.iter().copied().find(|&p| p > cur).unwrap_or(P[0])
}

/// Next max-height preset (`None` = native), wrapping.
pub fn next_max_height(cur: Option<u32>) -> Option<u32> {
    match cur {
        None => Some(720),
        Some(720) => Some(1080),
        Some(1080) => Some(1440),
        _ => None,
    }
}

/// Next directory in `choices` after `cur` (first when `cur` is unknown).
pub fn next_dir(cur: &std::path::Path, choices: &[PathBuf]) -> PathBuf {
    let i = choices
        .iter()
        .position(|p| p == cur)
        .map(|i| i + 1)
        .unwrap_or(0);
    choices
        .get(i % choices.len().max(1))
        .cloned()
        .unwrap_or_else(|| cur.to_path_buf())
}

/// Everything that belongs to ONE device.
pub struct DeviceState {
    pub serial: String,
    /// adb state ("device", "unauthorized", "offline", …) or "disconnected".
    pub adb_state: String,
    pub connected: bool,
    pub details: Option<DeviceDetails>,
    pub history: DeviceHistory,
    pub tab: Tab,

    // Logs
    pub log_stream: LogcatStream,
    pub log_entries: Vec<LogEntry>,
    pub log_filter: LogcatFilter,
    pub log_streaming: bool,

    // Commands / flash
    pub last_output: Option<(String, Result<String, String>)>,
    pub root_status: Option<RootStatus>,
    pub busy_label: Option<String>,
    pub profile: Option<DeviceProfile>,
    pub rom_builds: Vec<RomBuild>,
    pub rom_status: Option<String>,
    pub download_progress: Option<DownloadProgress>,
    pub downloaded_path: Option<PathBuf>,
    pub pending_method: Option<InstallMethod>,
    pub flash_session: Option<FlashSession>,
    pub flash_busy: bool,

    // Screen
    pub stream: Arc<ScreenStream>,
    pub recorder: ScreenRecorder,
    pub rec_opts: RecordOptions,
    pub out_dir: PathBuf,
    pub captures: Vec<Capture>,
    pub capture_status: Option<String>,
    /// Whether the recorder's current `Done` result was already harvested.
    pub rec_harvested: bool,
    pub mirror: Option<Entity<MirrorView>>,
    pub detached: Option<WindowHandle<DetachedRoot>>,
    pub detach_subs: Vec<Subscription>,
    pub mirror_subs: Vec<Subscription>,
}

impl DeviceState {
    pub fn new(serial: impl Into<String>) -> Self {
        Self {
            serial: serial.into(),
            adb_state: "device".into(),
            connected: true,
            details: None,
            history: DeviceHistory::default(),
            tab: Tab::Overview,
            log_stream: LogcatStream::new(),
            log_entries: Vec::new(),
            log_filter: LogcatFilter::default(),
            log_streaming: false,
            last_output: None,
            root_status: None,
            busy_label: None,
            profile: None,
            rom_builds: Vec::new(),
            rom_status: None,
            download_progress: None,
            downloaded_path: None,
            pending_method: None,
            flash_session: None,
            flash_busy: false,
            stream: Arc::new(ScreenStream::new()),
            recorder: ScreenRecorder::new(),
            rec_opts: RecordOptions::default(),
            out_dir: default_output_dir(),
            captures: Vec::new(),
            capture_status: None,
            rec_harvested: true,
            mirror: None,
            detached: None,
            detach_subs: Vec::new(),
            mirror_subs: Vec::new(),
        }
    }

    pub fn is_online(&self) -> bool {
        self.connected && self.adb_state == "device"
    }

    /// "Manufacturer Model" once details are known, else the serial.
    pub fn display_name(&self) -> String {
        match &self.details {
            Some(d) if d.online => d.display_name(),
            _ => self.serial.clone(),
        }
    }

    /// Battery % when known.
    pub fn battery_pct(&self) -> Option<u8> {
        self.details
            .as_ref()
            .filter(|d| d.online)
            .map(|d| d.battery.level)
    }

    /// Append raw logcat lines, capping retained entries.
    pub fn push_log_lines(&mut self, lines: &[String]) {
        for l in lines {
            self.log_entries.push(LogEntry::parse(l));
        }
        if self.log_entries.len() > MAX_LOG_ENTRIES {
            let excess = self.log_entries.len() - MAX_LOG_ENTRIES;
            self.log_entries.drain(0..excess);
        }
    }

    /// Record finished recording files (deduplicated).
    pub fn add_recordings(&mut self, files: &[PathBuf]) {
        for f in files {
            let c = Capture {
                kind: CaptureKind::Recording,
                path: f.clone(),
            };
            if !self.captures.contains(&c) {
                self.captures.push(c);
            }
        }
    }

    pub fn add_screenshot(&mut self, path: PathBuf) {
        self.captures.push(Capture {
            kind: CaptureKind::Screenshot,
            path,
        });
    }
}

/// Sort rank for the device list: online first, then other connected, then gone.
fn rank(d: &DeviceState) -> u8 {
    if d.is_online() {
        0
    } else if d.connected {
        1
    } else {
        2
    }
}

/// All known devices in stable first-seen order.
#[derive(Default)]
pub struct Fleet {
    seen: Vec<String>,
    devices: HashMap<String, DeviceState>,
}

impl Fleet {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn len(&self) -> usize {
        self.seen.len()
    }

    pub fn is_empty(&self) -> bool {
        self.seen.is_empty()
    }

    pub fn get(&self, serial: &str) -> Option<&DeviceState> {
        self.devices.get(serial)
    }

    pub fn get_mut(&mut self, serial: &str) -> Option<&mut DeviceState> {
        self.devices.get_mut(serial)
    }

    /// Mutable access, creating the device if unknown.
    pub fn ensure(&mut self, serial: &str) -> &mut DeviceState {
        if !self.devices.contains_key(serial) {
            self.seen.push(serial.to_string());
            self.devices
                .insert(serial.to_string(), DeviceState::new(serial));
        }
        self.devices.get_mut(serial).expect("just inserted")
    }

    /// Display order: online, other connected, disconnected; stable within.
    pub fn ordered(&self) -> Vec<&DeviceState> {
        let mut v: Vec<&DeviceState> = self
            .seen
            .iter()
            .filter_map(|s| self.devices.get(s))
            .collect();
        v.sort_by_key(|d| rank(d));
        v
    }

    pub fn serials(&self) -> Vec<String> {
        self.ordered().iter().map(|d| d.serial.clone()).collect()
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut DeviceState> {
        self.devices.values_mut()
    }

    /// Apply a fresh `adb devices` snapshot: new devices appear, missing
    /// ones are marked disconnected (their state is kept).
    pub fn apply_entries(&mut self, entries: &[AdbDeviceEntry]) {
        for e in entries {
            let d = self.ensure(&e.serial);
            d.connected = true;
            d.adb_state = e.state.clone();
        }
        for d in self.devices.values_mut() {
            if !entries.iter().any(|e| e.serial == d.serial) {
                d.connected = false;
                d.adb_state = "disconnected".into();
            }
        }
    }

    /// Store polled details + history for `serial`; ignored for unknown ones.
    pub fn apply_details(
        &mut self,
        serial: &str,
        details: DeviceDetails,
        history: DeviceHistory,
    ) -> bool {
        match self.devices.get_mut(serial) {
            Some(d) => {
                d.details = Some(details);
                d.history = history;
                true
            }
            None => false,
        }
    }

    /// Number of online devices.
    pub fn online_count(&self) -> usize {
        self.devices.values().filter(|d| d.is_online()).count()
    }

    /// Average battery of online devices that reported one.
    pub fn avg_battery(&self) -> Option<u8> {
        let v: Vec<u32> = self
            .devices
            .values()
            .filter(|d| d.is_online())
            .filter_map(|d| d.battery_pct())
            .map(u32::from)
            .collect();
        if v.is_empty() {
            None
        } else {
            Some((v.iter().sum::<u32>() / v.len() as u32) as u8)
        }
    }

    /// Total RAM in use (MiB) and total RAM (MiB) across online devices.
    pub fn ram_totals(&self) -> (u64, u64) {
        self.devices
            .values()
            .filter(|d| d.is_online())
            .filter_map(|d| d.details.as_ref())
            .fold((0, 0), |(u, t), d| {
                (u + d.memory.used_mib(), t + d.memory.total_mib)
            })
    }

    /// Resolve a route: a workspace for an unknown device falls back home.
    pub fn resolve(&self, route: &Route) -> Route {
        match route {
            Route::Device(s) if !self.devices.contains_key(s) => Route::Devices,
            r => r.clone(),
        }
    }

    /// Serial of the open workspace, if any.
    pub fn open_serial(&self, route: &Route) -> Option<String> {
        match self.resolve(route) {
            Route::Device(s) => Some(s),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn entry(s: &str, st: &str) -> AdbDeviceEntry {
        AdbDeviceEntry {
            serial: s.into(),
            state: st.into(),
        }
    }

    #[test]
    fn ordering_online_first_stable() {
        let mut f = Fleet::new();
        f.apply_entries(&[
            entry("b", "unauthorized"),
            entry("a", "device"),
            entry("c", "device"),
        ]);
        assert_eq!(f.serials(), vec!["a", "c", "b"]);
    }

    #[test]
    fn disconnected_devices_kept_and_sorted_last() {
        let mut f = Fleet::new();
        f.apply_entries(&[entry("a", "device"), entry("b", "device")]);
        f.apply_entries(&[entry("b", "device")]);
        assert_eq!(f.serials(), vec!["b", "a"]);
        let a = f.get("a").unwrap();
        assert!(!a.connected && !a.is_online());
        // Reconnect restores.
        f.apply_entries(&[entry("a", "device"), entry("b", "device")]);
        assert_eq!(f.serials(), vec!["a", "b"]);
        assert!(f.get("a").unwrap().is_online());
    }

    #[test]
    fn per_device_state_is_isolated() {
        let mut f = Fleet::new();
        f.apply_entries(&[entry("a", "device"), entry("b", "device")]);
        f.get_mut("a").unwrap().last_output = Some(("x".into(), Ok("out".into())));
        f.get_mut("a").unwrap().tab = Tab::Logs;
        assert!(f.get("b").unwrap().last_output.is_none());
        assert_eq!(f.get("b").unwrap().tab, Tab::Overview);
    }

    #[test]
    fn details_route_by_serial() {
        let mut f = Fleet::new();
        f.apply_entries(&[entry("a", "device")]);
        let mut d = DeviceDetails {
            serial: "a".into(),
            online: true,
            ..Default::default()
        };
        d.battery.level = 77;
        assert!(f.apply_details("a", d.clone(), DeviceHistory::default()));
        assert!(!f.apply_details("zzz", d, DeviceHistory::default()));
        assert_eq!(f.get("a").unwrap().battery_pct(), Some(77));
    }

    #[test]
    fn route_resolution() {
        let mut f = Fleet::new();
        f.apply_entries(&[entry("a", "device")]);
        assert_eq!(
            f.resolve(&Route::Device("a".into())),
            Route::Device("a".into())
        );
        assert_eq!(f.resolve(&Route::Device("gone".into())), Route::Devices);
        assert_eq!(
            f.open_serial(&Route::Device("a".into())).as_deref(),
            Some("a")
        );
        assert_eq!(f.open_serial(&Route::Settings), None);
    }

    #[test]
    fn fleet_summary() {
        let mut f = Fleet::new();
        f.apply_entries(&[
            entry("a", "device"),
            entry("b", "device"),
            entry("c", "offline"),
        ]);
        for (s, lvl, tot) in [("a", 80u8, 4000u64), ("b", 40, 2000)] {
            let mut d = DeviceDetails {
                serial: s.into(),
                online: true,
                ..Default::default()
            };
            d.battery.level = lvl;
            d.memory.total_mib = tot;
            d.memory.available_mib = tot / 2;
            f.apply_details(s, d, DeviceHistory::default());
        }
        assert_eq!(f.online_count(), 2);
        assert_eq!(f.avg_battery(), Some(60));
        assert_eq!(f.ram_totals(), (3000, 6000));
    }

    #[test]
    fn logs_capped_and_recordings_deduped() {
        let mut d = DeviceState::new("a");
        d.push_log_lines(&vec!["I/Tag( 1): hi".to_string(); MAX_LOG_ENTRIES + 5]);
        assert_eq!(d.log_entries.len(), MAX_LOG_ENTRIES);
        let p = PathBuf::from("/tmp/x.mp4");
        d.add_recordings(std::slice::from_ref(&p));
        d.add_recordings(&[p]);
        assert_eq!(d.captures.len(), 1);
    }

    #[test]
    fn option_cycling() {
        assert_eq!(next_bitrate(8), 12);
        assert_eq!(next_bitrate(24), 4);
        assert_eq!(next_max_height(None), Some(720));
        assert_eq!(next_max_height(Some(1440)), None);
        let c = vec![PathBuf::from("/a"), PathBuf::from("/b")];
        assert_eq!(
            next_dir(std::path::Path::new("/a"), &c),
            PathBuf::from("/b")
        );
        assert_eq!(
            next_dir(std::path::Path::new("/b"), &c),
            PathBuf::from("/a")
        );
        assert_eq!(
            next_dir(std::path::Path::new("/zzz"), &c),
            PathBuf::from("/a")
        );
    }

    #[test]
    fn display_name_falls_back_to_serial() {
        let mut d = DeviceState::new("SER1");
        assert_eq!(d.display_name(), "SER1");
        let mut det = DeviceDetails {
            serial: "SER1".into(),
            online: true,
            ..Default::default()
        };
        det.system.manufacturer = "Google".into();
        det.system.model = "Pixel 7".into();
        d.details = Some(det);
        assert_eq!(d.display_name(), "Google Pixel 7");
    }
}