Skip to content

Control a real USB-connected iPhone from Linux (no Mac, no paid developer account) and expose it to AI agents as MCP tools

feat(daemon): an action that unlocked the phone first says so (#135)

Qinghe Li8d3e69d0379a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b6cfe9c..43af506 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,10 @@ What changed in each release, one line per change. Versions follow [Semantic Ver
## [Unreleased]
+### Improvements
+
+- An action that had to unlock the phone first says so, and how long it took (#135).
+
## [0.2.7] - 2026-11-15
### Upgrade notes
diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md
index 6043cf3..a6c34f4 100644
--- a/CHANGELOG.zh-CN.md
+++ b/CHANGELOG.zh-CN.md
@@ -6,6 +6,10 @@
## [Unreleased]
+### 改进
+
+- 操作前先自动解锁了的话,结果里会说明,并写明用了多久(#135)。
+
## [0.2.7] - 2026-11-15
### 升级注意事项
diff --git a/crates/ictl-daemon/src/core.rs b/crates/ictl-daemon/src/core.rs
index dc40c7d..6bc2b5b 100644
--- a/crates/ictl-daemon/src/core.rs
+++ b/crates/ictl-daemon/src/core.rs
@@ -438,6 +438,9 @@ pub(crate) struct PerDevice {
pub before_action: Option<crate::tools::Seen>,
/// 最近一个操作开始的时间:原生控制等界面稳定时,从这之后的推送算起
pub acted_at: Option<Instant>,
+ /// How long the request being handled spent unlocking the phone first ([`Daemon::ensure_unlocked`]); taken by
+ /// [`Daemon::dispatch`], which says so in the answer
+ pub unlocked: Option<Duration>,
/// 原生控制:进程号 → Bundle ID(前台 App 由无障碍服务给进程号)
pub bundles: HashMap<i64, String>,
/// 原生控制:进程号 → 是不是网页内容进程(WebKit WebContent,网页的元素来自它)
@@ -1188,6 +1191,7 @@ impl Daemon {
let mut outcome = crate::protocol::Outcome::default();
// the same facts as data, for programs (Response::Text.data)
let mut data = None;
+ g.dev.unlocked = None;
let r = match req {
Request::Status => {
let (text, status) = self.status(g).await;
@@ -1199,7 +1203,8 @@ impl Daemon {
Err("screen_stream is a streaming request: open it with open_screen_stream".into())
}
Request::Screenshot { region } => {
- return self.screenshot(g, region).await.unwrap_or_else(Response::error);
+ let r = self.screenshot(g, region).await.unwrap_or_else(Response::error);
+ return with_unlock_note(r, g.dev.unlocked.take());
}
Request::ListElements { filter, limit, max_age_ms, keep_locked } => self
.list_elements(g, &filter, limit, Duration::from_millis(max_age_ms), keep_locked)
@@ -1279,7 +1284,8 @@ impl Daemon {
| Request::TaskNote { .. }
| Request::TaskFinish { .. } => unreachable!("other lanes"),
};
- r.map(|t| Response::text_data(t, outcome, data)).unwrap_or_else(Response::error)
+ let r = r.map(|t| Response::text_data(t, outcome, data)).unwrap_or_else(Response::error);
+ with_unlock_note(r, g.dev.unlocked.take())
}
pub async fn shutdown(&self) {
@@ -1354,6 +1360,27 @@ fn lane(req: &Request) -> Lane {
}
}
+/// Says in a request's answer that the phone was unlocked for it first, and how long that took: on the first line,
+/// like the other notes about what was done. An action that had to unlock took seconds longer than usual (14-15 s
+/// on an iPad through WDA, 8-9 s natively on an iPhone), and nothing in the answer said why
+fn with_unlock_note(r: Response, unlocked: Option<Duration>) -> Response {
+ let Some(took) = unlocked else { return r };
+ let note = ictl_l10n::trf!(
+ "(手机锁着,先自动解锁了,用时 {:.1} 秒)",
+ " (the phone was locked and was unlocked first, which took {:.1} s)",
+ took.as_secs_f64()
+ );
+ let add = |s: String| match s.split_once('\n') {
+ Some((first, rest)) => format!("{first}{note}\n{rest}"),
+ None => s + &note,
+ };
+ match r {
+ Response::Text { text, outcome, data } => Response::Text { text: add(text), outcome, data },
+ Response::Image { jpeg_base64, caption } => Response::Image { jpeg_base64, caption: add(caption) },
+ Response::Error { message, retry_as_gesture } => Response::Error { message: add(message), retry_as_gesture },
+ }
+}
+
fn cancelled() -> Response {
Response::error(ictl_l10n::tr!(
"调用方已取消,请求没有执行",
@@ -1832,4 +1859,31 @@ pub(crate) mod tests {
assert!(Persisted::load(&p).wda_bundle.is_none());
std::fs::remove_file(&p).unwrap();
}
+
+ #[test]
+ fn an_unlock_first_is_said_on_the_answers_first_line() {
+ let took = Some(Duration::from_millis(14_100));
+ // an action with `then=elements`: the note goes on the action's line, the screen after it stays below
+ let r = with_unlock_note(
+ Response::text_with("已按 home\n--- 操作后的界面 ---\nButton \"设置\"", Default::default()),
+ took,
+ );
+ let Response::Text { text, outcome, .. } = &r else { panic!("{r:?}") };
+ let lines: Vec<_> = text.lines().collect();
+ assert!(lines[0].starts_with("已按 home") && lines[0].contains("14.1"), "{text}");
+ assert_eq!(lines[1..], ["--- 操作后的界面 ---", "Button \"设置\""]);
+ assert!(outcome.is_some());
+ // a screenshot's caption, and an action that failed after the unlock
+ let r = with_unlock_note(Response::Image { jpeg_base64: "x".into(), caption: "430x932".into() }, took);
+ assert!(
+ matches!(&r, Response::Image { caption, .. } if caption.starts_with("430x932") && caption.contains("14.1"))
+ );
+ let r = with_unlock_note(Response::error("没找到"), took);
+ assert!(
+ matches!(&r, Response::Error { message, .. } if message.starts_with("没找到") && message.contains("14.1"))
+ );
+ // no unlock: the answer as it was
+ let plain = Response::text_with("已按 home", Default::default());
+ assert_eq!(with_unlock_note(plain.clone(), None), plain);
+ }
}
diff --git a/crates/ictl-daemon/src/tools/mod.rs b/crates/ictl-daemon/src/tools/mod.rs
index b1bdaea..a243b6c 100644
--- a/crates/ictl-daemon/src/tools/mod.rs
+++ b/crates/ictl-daemon/src/tools/mod.rs
@@ -57,6 +57,7 @@ impl Daemon {
/// 配置了密码就在锁屏时自动解锁;没配置而手机锁着则报错(与 toolkit 一致)
pub(crate) async fn ensure_unlocked(&self, g: &mut Inner) -> crate::error::Result<()> {
let cfg = self.unlock_cfg(g.device.as_ref().map(|d| d.udid.as_str()));
+ let t0 = Instant::now();
if let Some(left) = g.dev.last_lock.and_then(|t| LOCK_SETTLE.checked_sub(t.elapsed())) {
tokio::time::sleep(left).await;
}
@@ -67,7 +68,18 @@ impl Daemon {
None => return Ok(()),
};
match r {
- Ok(_) => Ok(()),
+ Ok(ictl_unlock::Outcome::Unlocked) => {
+ let took = t0.elapsed();
+ // the answer says so (`dispatch`), and the event log: the action took seconds longer than usual
+ g.dev.unlocked = Some(took);
+ self.log.log(&ictl_l10n::log_trf!(
+ "已自动解锁(用时 {:.1} 秒)",
+ "Unlocked automatically ({:.1} s)",
+ took.as_secs_f64()
+ ));
+ Ok(())
+ }
+ Ok(ictl_unlock::Outcome::NotLocked) => Ok(()),
Err(e @ (UnlockError::WrongPasscode | UnlockError::Uncertain(_))) => {
self.notify(g, Event::UnlockDisabled);
Err(ictl_l10n::trf!("{e},已推送通知", "{e}, notification sent").into())
diff --git a/docs/dev/regression-cases.md b/docs/dev/regression-cases.md
index 4373bf2..2107972 100644
--- a/docs/dev/regression-cases.md
+++ b/docs/dev/regression-cases.md
@@ -147,3 +147,4 @@
| 141 | 开着后台启动(`ICTL_WDA_DETACHED=1`)时 `iphone-control service stop`、`service uninstall` 之后,后台启动的 WDA 还在手机上运行(它本来就不随守护进程退出,好让重启、升级不打断它),一直占着 8100 端口 | `service stop`/`uninstall` 停掉服务后(还有守护进程应答时不动),对接着的手机逐个结束记下的后台 WDA(AppService SIGKILL,等它从进程列表消失),忘掉结束了的;没接着、结束不了的留着记录,守护进程下次准备这台手机时处理。`service restart`、升级照旧不结束 | 单测 `only_the_wda_runner_with_that_pid_is_wda` + 真机(`service stop` 后进程列表里没有 WDA、记录清空;`restart` 后接上原进程) |
| 142 | 开着后台启动时切换设备:`state.json` 只记一条后台 WDA,切到另一台就被覆盖,原来那台手机上的 WDA 没人记着、继续运行,切回去时只能重新后台启动 | 每台设备各记一条(`detached_wdas`,按 UDID),切回去直接接上;最近一条照旧写进 `detached_wda` 给旧版本;旧版本留下的那一条在记下别的设备之前先迁进新表 | 单测 `each_device_keeps_its_background_wda`、`an_older_versions_background_wda_is_still_known` + 真机(iPhone ↔ iPad 来回切,日志是「接上…没有重新启动」) |
| 143 | 原生:Spotlight 打开时选中上次的搜索词,再粘贴同样的词会把它替换成自己,输入框的值不变,`iphone_type_text` 报「粘贴了…但输入框里没有这段文字」(带 `submit` 时还不按回车),其实已经输进去了 | 「允许粘贴」点过之后,输入框原来就有这段文字、1.5 秒不变,算输入成功并提示截图确认;没点过「允许粘贴」时仍报错 | 单测 `a_paste_that_replaced_the_same_text_is_told_apart_from_one_that_missed` + 真机(Spotlight 留着「设置」时再输入「设置」,`run.py` 连跑两遍)|
+| 144 | 设备闲置时自动锁屏,下一个操作先自动解锁(iPad 经 WDA 约 15 秒,iPhone 原生约 9 秒),结果和事件日志都没说,看着像「切换设备后第一个操作慢」 | 结果第一行(截图在说明里、出错时在报错里)写「手机锁着,先自动解锁了,用时 X 秒」,事件日志记「已自动解锁」 | 单测 `an_unlock_first_is_said_on_the_answers_first_line` + 真机(锁屏后按名字点击、截图,两台设备)|