Skip to main content

droidrun_core/driver/
mod.rs

1//! Device driver trait and implementations.
2
3pub mod android;
4pub mod recording;
5
6use std::collections::HashSet;
7use std::path::Path;
8
9use async_trait::async_trait;
10use serde::{Deserialize, Serialize};
11
12use crate::error::Result;
13
14/// Information about an installed app.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct AppInfo {
17    pub package: String,
18    pub label: String,
19}
20
21/// A 2D point on screen.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub struct Point {
24    pub x: i32,
25    pub y: i32,
26}
27
28/// Supported device actions.
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
30pub enum Action {
31    Tap,
32    Swipe,
33    InputText,
34    PressKey,
35    Drag,
36    StartApp,
37    InstallApp,
38    Screenshot,
39    GetUiTree,
40    GetDate,
41    GetApps,
42    ListPackages,
43}
44
45/// Trait for all device drivers.
46///
47/// Concrete drivers implement the methods they support.
48/// Unsupported methods return `DroidrunError::NotSupported`.
49#[async_trait]
50pub trait DeviceDriver: Send + Sync {
51    // ── Lifecycle ──────────────────────────────────────────────
52
53    /// Establish connection to the device.
54    async fn connect(&mut self) -> Result<()>;
55
56    /// Connect if not already connected.
57    async fn ensure_connected(&mut self) -> Result<()>;
58
59    // ── Input actions ──────────────────────────────────────────
60
61    /// Tap at absolute pixel coordinates.
62    async fn tap(&self, x: i32, y: i32) -> Result<()>;
63
64    /// Swipe from (x1, y1) to (x2, y2) over duration_ms.
65    async fn swipe(
66        &self,
67        x1: i32,
68        y1: i32,
69        x2: i32,
70        y2: i32,
71        duration_ms: u32,
72    ) -> Result<()>;
73
74    /// Type text into the currently focused field.
75    async fn input_text(&self, text: &str, clear: bool) -> Result<bool>;
76
77    /// Send a single key event.
78    async fn press_key(&self, keycode: i32) -> Result<()>;
79
80    /// Drag from (x1, y1) to (x2, y2).
81    async fn drag(
82        &self,
83        x1: i32,
84        y1: i32,
85        x2: i32,
86        y2: i32,
87        duration_ms: u32,
88    ) -> Result<()>;
89
90    // ── App management ─────────────────────────────────────────
91
92    /// Launch an application.
93    async fn start_app(&self, package: &str, activity: Option<&str>) -> Result<String>;
94
95    /// Install an APK.
96    async fn install_app(&self, path: &Path) -> Result<String>;
97
98    /// List installed apps with labels.
99    async fn get_apps(&self, include_system: bool) -> Result<Vec<AppInfo>>;
100
101    /// List installed package names.
102    async fn list_packages(&self, include_system: bool) -> Result<Vec<String>>;
103
104    // ── State / observation ────────────────────────────────────
105
106    /// Take a screenshot (PNG bytes).
107    async fn screenshot(&self, hide_overlay: bool) -> Result<Vec<u8>>;
108
109    /// Get the accessibility tree + phone state as JSON.
110    async fn get_ui_tree(&self) -> Result<serde_json::Value>;
111
112    /// Get the device date/time.
113    async fn get_date(&self) -> Result<String>;
114
115    // ── Capabilities ───────────────────────────────────────────
116
117    /// Which actions this driver supports.
118    fn supported_actions(&self) -> &HashSet<Action>;
119}