fix: 任务动作编辑器剪贴板注入改用 ClipInject 通道——u2 set_clipboard 在 Android 10+ 静默失效(粘贴模式必抛 UiAutomationError);提取 core/clipboard_helper 公共模块供工具页与任务执行端共用,粘贴模式 atx-agent pasteClipboard + set_text 兜底
This commit is contained in:
+3
-51
@@ -94,57 +94,9 @@ def api_tools_clipboard_set():
|
||||
|
||||
|
||||
def _set_device_clipboard(serial, text):
|
||||
"""向单台设备注入剪贴板文字(ClipInject APK:透明 Activity 聚焦后写入)。
|
||||
|
||||
Android 10+ 禁止后台(atx-agent/shell)写剪贴板,u2.set_clipboard 在
|
||||
Android 12/13 上调用"成功"但内容被系统静默丢弃。ClipInject
|
||||
(com.example.clipinject)通过 am start 启动透明 Activity,窗口聚焦后
|
||||
setPrimaryClip,shell 启动 Activity 不受后台启动限制(广播方式在 MIUI
|
||||
会被 Background activity start 拦截,必须用 am start)。
|
||||
|
||||
- IP:port 设备:先 adb connect(已连接自动跳过;绝不 disconnect,红线)
|
||||
- 设备未装 ClipInject:am start 返回 unable to resolve Intent,明确报错
|
||||
- 注入后 u2 读回比对验证(后台读剪贴板实测可用),不一致也明确报错
|
||||
"""
|
||||
import base64
|
||||
import time
|
||||
import uiautomator2 as u2
|
||||
if ":" in serial:
|
||||
try:
|
||||
adb_connect(serial)
|
||||
except Exception as e:
|
||||
return False, f"adb 连接失败: {e}"
|
||||
b64 = base64.b64encode(text.encode("utf-8")).decode()
|
||||
# 1. am start 透明 Activity 写剪贴板(shell 身份启动,不受后台启动限制)
|
||||
try:
|
||||
r = subprocess.run(
|
||||
[ADB_PATH, "-s", serial, "shell", "am", "start",
|
||||
"-n", "com.example.clipinject/.ClipActivity",
|
||||
"--es", "text_b64", b64],
|
||||
capture_output=True, timeout=30)
|
||||
out = ((r.stdout or b"") + (r.stderr or b"")).decode(errors="replace")
|
||||
if "does not exist" in out or "unable to resolve Intent" in out:
|
||||
return False, ("设备未安装 ClipInject(com.example.clipinject),"
|
||||
f"请先安装: {out.strip()[:100]}")
|
||||
if "Error" in out or ("Starting" not in out and r.returncode != 0):
|
||||
return False, f"am start 失败: {out.strip()[:120]}"
|
||||
except subprocess.TimeoutExpired:
|
||||
return False, "am start 超时(adb 无响应)"
|
||||
except Exception as e:
|
||||
return False, f"{type(e).__name__}: {str(e)[:120]}"
|
||||
# 2. 读回验证(等透明 Activity 完成写入;u2 读剪贴板在 Android 12 实测可用)
|
||||
time.sleep(0.5)
|
||||
try:
|
||||
with ThreadPoolExecutor(max_workers=1) as pool:
|
||||
d = pool.submit(u2.connect, serial).result(timeout=30)
|
||||
with ThreadPoolExecutor(max_workers=1) as pool:
|
||||
got = pool.submit(lambda: d.clipboard).result(timeout=15)
|
||||
if got == text:
|
||||
return True, "已注入"
|
||||
return False, f"剪贴板读回不一致(写入可能被拒): {str(got)[:60]!r}"
|
||||
except Exception as e:
|
||||
# 读回不可用(旧版 u2/连接失败)时按写入成功处理,避免误报,但带上具体原因
|
||||
return True, f"已注入(读回验证不可用: {type(e).__name__}: {str(e)[:80]})"
|
||||
"""向单台设备注入剪贴板文字(ClipInject 通道,见 core.clipboard_helper)。"""
|
||||
from core.clipboard_helper import inject_clipboard
|
||||
return inject_clipboard(serial, text)
|
||||
|
||||
|
||||
def _app_ver_on_device(serial, pkg):
|
||||
|
||||
Reference in New Issue
Block a user