feat(recovery): 桌面自救改用包名启动抖音,点击坐标降为兜底

当检测到不在抖音前台时,原先点击桌面图标 + 直播 + 进入直播间 3 步坐标的方式
依赖图标位置和分辨率,不同设备/桌面常常失败。改为优先调用 ADB 包名启动:

- underlying_operations 新增 launch_package(device_id, pkg):
  monkey -p <pkg> -c android.intent.category.LAUNCHER 1(主路径)
  失败回退到 am start -n <pkg>/<MainActivity>(多个常见 activity 兜底)
  默认按 com.ss.android.ugc.aweme / .lite / TikTok 顺序尝试

- 新增 is_douyin_foreground 便捷方法

- 改造 3 处自救点:
  fudai_modules/run_once_basic_helpers.py:handle_desktop_recovery
  douyin/other/douyin_yanghao.py(养号循环 + _yanghao_check_and_recover_from_desktop ×2)
  包名启动失败时仍走旧的点击逻辑作为兜底,向后兼容

新增 12 个单元测试覆盖 launch_package 主路径/兜底/失败和自救新行为。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 19:08:09 +08:00
1 parent 3aca02fae4
commit 4c1d95d4b5
5 files changed
+429 -133

No files matched your search

@@ -284,37 +284,57 @@ def handle_desktop_recovery(self: "fudai_analyse") -> bool:
try:
self._log_kv(
"自救",
"发现前台包不在抖音,尝试恢复进入直播间",
"发现前台包不在抖音,尝试通过包名启动抖音",
top_pkg=top_pkg,
)
# 直接点击桌面图标的预设坐标
# 优先:通过包名启动抖音(monkey + am start 兜底)
launched_ok = False
used_pkg = ""
try:
self.click(int(self.desktop_icon_x), int(self.desktop_icon_y))
except Exception:
# 退化为默认硬编码位置
self.click(153, 260)
self.operation.delay(2)
# 点击直播与进入直播间的备用坐标
try:
lx = int(self.yanghao_fallback_live_x)
ly = int(self.yanghao_fallback_live_y)
self._log_kv("自救", "点击直播按钮", x=lx, y=ly)
self.click(lx, ly)
self.operation.delay(1.5)
except Exception:
pass
try:
ex = int(self.yanghao_fallback_enter_live_x)
ey = int(self.yanghao_fallback_enter_live_y)
self._log_kv("自救", "点击进入直播间", x=ex, y=ey, 次数=2)
self.click(ex, ey)
self.operation.delay(0.6)
self.click(ex, ey)
self.operation.delay(2.0)
except Exception:
pass
# 自救完成后,不在这里结束本轮流程,而是立即重新执行一次主流程
launched_ok, used_pkg = self.operation.launch_package(
self.device_id, ""
)
except Exception as exc:
self._log_kv(
"自救",
"包名启动调用异常,回退点击坐标",
error=str(exc),
)
if launched_ok:
self._log_kv(
"自救", "包名启动抖音成功", used_package=used_pkg
)
self.operation.delay(2.5)
else:
# 兜底:保留旧的点击坐标自救
self._log_kv(
"自救", "包名启动失败,回退点击桌面图标 + 备用坐标"
)
try:
self.click(int(self.desktop_icon_x), int(self.desktop_icon_y))
except Exception:
# 退化为默认硬编码位置
self.click(153, 260)
self.operation.delay(2)
try:
lx = int(self.yanghao_fallback_live_x)
ly = int(self.yanghao_fallback_live_y)
self._log_kv("自救", "点击直播按钮", x=lx, y=ly)
self.click(lx, ly)
self.operation.delay(1.5)
except Exception:
pass
try:
ex = int(self.yanghao_fallback_enter_live_x)
ey = int(self.yanghao_fallback_enter_live_y)
self._log_kv("自救", "点击进入直播间", x=ex, y=ey, 次数=2)
self.click(ex, ey)
self.operation.delay(0.6)
self.click(ex, ey)
self.operation.delay(2.0)
except Exception:
pass
# 自救完成后立即重新执行一次主流程
try:
self._log_kv(
"流程",
+65
View File
@@ -1832,6 +1832,71 @@ class underlying_operations:
pass
return ""
# 抖音常见包名(从主到 lite/极速版/海外版),launch_package 默认按此顺序尝试
DOUYIN_PACKAGES = (
"com.ss.android.ugc.aweme", # 抖音正式版
"com.ss.android.ugc.aweme.lite", # 抖音极速版
"com.zhiliaoapp.musically", # TikTok(兼容)
)
def launch_package(
self,
device_id: str,
package_name: str = "",
timeout_seconds: float = 5.0,
) -> Tuple[bool, str]:
"""通过包名启动应用。
策略:先用 `monkey -p <pkg> -c android.intent.category.LAUNCHER 1` 启动主 launcher activity,
失败时回退到 `am start -n <pkg>/.<MainActivity>`(按抖音常用 activity 列表尝试)。
参数:
device_id: ADB 设备 ID
package_name: 要启动的包名;留空则按 DOUYIN_PACKAGES 顺序尝试
timeout_seconds: 单次 adb shell 命令超时
返回:
(success, used_package) — used_package 为实际成功启动的包名(失败返回 "")
"""
candidates = (
[package_name] if package_name else list(self.DOUYIN_PACKAGES)
)
for pkg in candidates:
pkg = (pkg or "").strip()
if not pkg:
continue
# 1) monkey 主入口
cmd = f"monkey -p {pkg} -c android.intent.category.LAUNCHER 1"
ok, out = self._run_adb_shell_capture(
device_id, cmd, timeout_seconds=timeout_seconds
)
# monkey 失败时输出会包含 "No activities found" 或 "monkey aborted"
if ok and "No activities found" not in (out or "") and "aborted" not in (
out or ""
).lower():
return True, pkg
# 2) am start 主入口兜底
for activity in (
".main.MainActivity",
".splash.SplashActivity",
".main.homepage.MainActivity",
):
am_cmd = f"am start -n {pkg}/{activity}"
if self._run_adb_shell(
device_id, am_cmd, timeout_seconds=timeout_seconds
):
return True, pkg
return False, ""
def is_douyin_foreground(self, device_id: str) -> bool:
"""便捷判断:当前前台是否是抖音相关包。"""
top_pkg = self.get_top_package(device_id) or ""
return bool(top_pkg) and (
"aweme" in top_pkg
or "douyin" in top_pkg
or "com.ss.android.ugc" in top_pkg
)
def set_clipboard_text(self, device_id: str, text: str) -> bool:
return _impl_set_clipboard_text(device_id, text)
def paste_from_clipboard(self, device_id: str) -> bool:
+160 -104
View File
@@ -309,62 +309,84 @@ def yanghao_idle(analyser, total_seconds: int) -> None:
or "com.ss.android.ugc" in top_pkg
)
if top_pkg and not is_douyin_pkg:
_yanghao_log(analyser,
_yanghao_log(analyser,
f"[养号] 自救: 检测到不在抖音前台,前台包名={top_pkg}"
)
try:
desktop_x = int(
getattr(analyser, "desktop_icon_x", 153) or 153
)
desktop_y = int(
getattr(analyser, "desktop_icon_y", 260) or 260
)
analyser.operation.click(
analyser.device_id, desktop_x, desktop_y
)
analyser.operation.delay(2)
fallback_x = int(
getattr(
analyser, "yanghao_fallback_live_x", 540
# 优先:通过包名启动抖音
launched_ok = False
used_pkg = ""
try:
launched_ok, used_pkg = (
analyser.operation.launch_package(
analyser.device_id, ""
)
)
or 540
)
fallback_y = int(
getattr(
analyser, "yanghao_fallback_live_y", 2000
)
or 2000
)
analyser.operation.click(
analyser.device_id, fallback_x, fallback_y
)
analyser.operation.delay(1.5)
enter_x = int(
getattr(
except Exception as exc:
_yanghao_log(
analyser,
"yanghao_fallback_enter_live_x",
540,
f"[养号] 自救: 包名启动调用异常 {exc},回退点击坐标",
)
or 540
)
enter_y = int(
getattr(
if launched_ok:
_yanghao_log(
analyser,
"yanghao_fallback_enter_live_y",
1200,
f"[养号] 自救: 包名启动抖音成功,pkg={used_pkg}",
)
or 1200
)
analyser.operation.click(
analyser.device_id, enter_x, enter_y
)
analyser.operation.delay(0.6)
analyser.operation.click(
analyser.device_id, enter_x, enter_y
)
analyser.operation.delay(2.0)
analyser.operation.delay(2.5)
else:
# 兜底:保留旧的点击坐标自救
desktop_x = int(
getattr(analyser, "desktop_icon_x", 153) or 153
)
desktop_y = int(
getattr(analyser, "desktop_icon_y", 260) or 260
)
analyser.operation.click(
analyser.device_id, desktop_x, desktop_y
)
analyser.operation.delay(2)
fallback_x = int(
getattr(
analyser, "yanghao_fallback_live_x", 540
)
or 540
)
fallback_y = int(
getattr(
analyser, "yanghao_fallback_live_y", 2000
)
or 2000
)
analyser.operation.click(
analyser.device_id, fallback_x, fallback_y
)
analyser.operation.delay(1.5)
enter_x = int(
getattr(
analyser,
"yanghao_fallback_enter_live_x",
540,
)
or 540
)
enter_y = int(
getattr(
analyser,
"yanghao_fallback_enter_live_y",
1200,
)
or 1200
)
analyser.operation.click(
analyser.device_id, enter_x, enter_y
)
analyser.operation.delay(0.6)
analyser.operation.click(
analyser.device_id, enter_x, enter_y
)
analyser.operation.delay(2.0)
_yanghao_log(analyser, "[养号] 自救: 恢复操作完成")
except Exception as e:
@@ -500,33 +522,50 @@ def yanghao_idle(analyser, total_seconds: int) -> None:
# 执行自救操作
try:
# 点击桌面图标
desktop_x = int(getattr(analyser, "desktop_icon_x", 153) or 153)
desktop_y = int(getattr(analyser, "desktop_icon_y", 260) or 260)
analyser.operation.click(analyser.device_id, desktop_x, desktop_y)
analyser.operation.delay(2)
# 优先:通过包名启动抖音
launched_ok = False
used_pkg = ""
try:
launched_ok, used_pkg = analyser.operation.launch_package(
analyser.device_id, ""
)
except Exception as exc:
_yanghao_log(
analyser,
f"[养号] 自救: 包名启动调用异常 {exc},回退点击坐标",
)
if launched_ok:
_yanghao_log(
analyser,
f"[养号] 自救: 包名启动抖音成功,pkg={used_pkg}",
)
analyser.operation.delay(2.5)
else:
# 兜底:保留旧的点击坐标自救
desktop_x = int(getattr(analyser, "desktop_icon_x", 153) or 153)
desktop_y = int(getattr(analyser, "desktop_icon_y", 260) or 260)
analyser.operation.click(analyser.device_id, desktop_x, desktop_y)
analyser.operation.delay(2)
# 点击直播按钮(备用坐标)
fallback_x = int(
getattr(analyser, "yanghao_fallback_live_x", 540) or 540
)
fallback_y = int(
getattr(analyser, "yanghao_fallback_live_y", 2000) or 2000
)
analyser.operation.click(analyser.device_id, fallback_x, fallback_y)
analyser.operation.delay(1.5)
fallback_x = int(
getattr(analyser, "yanghao_fallback_live_x", 540) or 540
)
fallback_y = int(
getattr(analyser, "yanghao_fallback_live_y", 2000) or 2000
)
analyser.operation.click(analyser.device_id, fallback_x, fallback_y)
analyser.operation.delay(1.5)
# 点击进入直播间(备用坐标)
enter_x = int(
getattr(analyser, "yanghao_fallback_enter_live_x", 540) or 540
)
enter_y = int(
getattr(analyser, "yanghao_fallback_enter_live_y", 1200) or 1200
)
analyser.operation.click(analyser.device_id, enter_x, enter_y)
analyser.operation.delay(0.6)
analyser.operation.click(analyser.device_id, enter_x, enter_y)
analyser.operation.delay(2.0)
enter_x = int(
getattr(analyser, "yanghao_fallback_enter_live_x", 540) or 540
)
enter_y = int(
getattr(analyser, "yanghao_fallback_enter_live_y", 1200) or 1200
)
analyser.operation.click(analyser.device_id, enter_x, enter_y)
analyser.operation.delay(0.6)
analyser.operation.click(analyser.device_id, enter_x, enter_y)
analyser.operation.delay(2.0)
_yanghao_log(analyser, "[养号] 自救: 恢复操作完成")
return True
@@ -629,39 +668,56 @@ def yanghao_idle(analyser, total_seconds: int) -> None:
# 执行自救操作
try:
# 点击桌面图标
desktop_x = int(getattr(analyser, "desktop_icon_x", 153) or 153)
desktop_y = int(getattr(analyser, "desktop_icon_y", 260) or 260)
analyser.operation.click(
analyser.device_id, desktop_x, desktop_y
)
analyser.operation.delay(2)
# 优先:通过包名启动抖音
launched_ok = False
used_pkg = ""
try:
launched_ok, used_pkg = analyser.operation.launch_package(
analyser.device_id, ""
)
except Exception as exc:
_yanghao_log(
analyser,
f"[养号] 自救: 包名启动调用异常 {exc},回退点击坐标",
)
if launched_ok:
_yanghao_log(
analyser,
f"[养号] 自救: 包名启动抖音成功,pkg={used_pkg}",
)
analyser.operation.delay(2.5)
else:
# 兜底:保留旧的点击坐标自救
desktop_x = int(getattr(analyser, "desktop_icon_x", 153) or 153)
desktop_y = int(getattr(analyser, "desktop_icon_y", 260) or 260)
analyser.operation.click(
analyser.device_id, desktop_x, desktop_y
)
analyser.operation.delay(2)
# 点击直播按钮(备用坐标)
fallback_x = int(
getattr(analyser, "yanghao_fallback_live_x", 540) or 540
)
fallback_y = int(
getattr(analyser, "yanghao_fallback_live_y", 2000) or 2000
)
analyser.operation.click(
analyser.device_id, fallback_x, fallback_y
)
analyser.operation.delay(1.5)
fallback_x = int(
getattr(analyser, "yanghao_fallback_live_x", 540) or 540
)
fallback_y = int(
getattr(analyser, "yanghao_fallback_live_y", 2000) or 2000
)
analyser.operation.click(
analyser.device_id, fallback_x, fallback_y
)
analyser.operation.delay(1.5)
# 点击进入直播间(备用坐标)
enter_x = int(
getattr(analyser, "yanghao_fallback_enter_live_x", 540)
or 540
)
enter_y = int(
getattr(analyser, "yanghao_fallback_enter_live_y", 1200)
or 1200
)
analyser.operation.click(analyser.device_id, enter_x, enter_y)
analyser.operation.delay(0.6)
analyser.operation.click(analyser.device_id, enter_x, enter_y)
analyser.operation.delay(2.0)
enter_x = int(
getattr(analyser, "yanghao_fallback_enter_live_x", 540)
or 540
)
enter_y = int(
getattr(analyser, "yanghao_fallback_enter_live_y", 1200)
or 1200
)
analyser.operation.click(analyser.device_id, enter_x, enter_y)
analyser.operation.delay(0.6)
analyser.operation.click(analyser.device_id, enter_x, enter_y)
analyser.operation.delay(2.0)
_yanghao_log(analyser, "[养号] 自救: 恢复操作完成")
return True
+108
View File
@@ -0,0 +1,108 @@
"""launch_package / is_douyin_foreground 单元测试。
覆盖:
- launch_package:monkey 成功 / monkey 失败 am 兜底成功 / 全部失败
- launch_package:指定包名 vs 默认列表
- is_douyin_foreground:抖音 / 抖音极速版 / 桌面 / 空
"""
from __future__ import annotations
from unittest.mock import patch
import pytest
@pytest.fixture
def op():
"""构造一个 underlying_operations 实例(避免每个测试重复导入)。"""
from douyin.core.underlying_operations import underlying_operations
return underlying_operations()
# ============================================================
# launch_package
# ============================================================
class TestLaunchPackage:
def test_monkey_success_returns_true_and_pkg(self, op):
"""monkey 命令成功(returncode 0 + 输出正常)→ 立即返回。"""
with patch.object(op, "_run_adb_shell_capture", return_value=(True, "Events injected: 1")) as mock_capture, \
patch.object(op, "_run_adb_shell", return_value=False) as mock_shell:
ok, pkg = op.launch_package("MOCK_DEVICE", "com.ss.android.ugc.aweme")
assert ok is True
assert pkg == "com.ss.android.ugc.aweme"
# 应该只调用了 monkey 一次(不需要 am 兜底)
mock_capture.assert_called_once()
mock_shell.assert_not_called()
def test_monkey_no_activities_falls_back_to_am_start(self, op):
"""monkey 输出 'No activities found' 触发 am start 兜底。"""
with patch.object(op, "_run_adb_shell_capture", return_value=(True, "** No activities found to run, monkey aborted.")), \
patch.object(op, "_run_adb_shell", return_value=True) as mock_shell:
ok, pkg = op.launch_package("MOCK_DEVICE", "com.ss.android.ugc.aweme")
assert ok is True
assert pkg == "com.ss.android.ugc.aweme"
# 应该用了 am start 兜底
mock_shell.assert_called()
def test_monkey_aborted_falls_back_to_am_start(self, op):
"""monkey 'aborted' 也触发 am start 兜底。"""
with patch.object(op, "_run_adb_shell_capture", return_value=(True, "Monkey aborted due to error.")), \
patch.object(op, "_run_adb_shell", return_value=True):
ok, pkg = op.launch_package("MOCK_DEVICE", "com.ss.android.ugc.aweme")
assert ok is True
def test_all_methods_fail_returns_false(self, op):
"""monkey 失败 + am start 全部 activity 失败 → 返回 (False, '')。"""
with patch.object(op, "_run_adb_shell_capture", return_value=(False, "")), \
patch.object(op, "_run_adb_shell", return_value=False):
ok, pkg = op.launch_package("MOCK_DEVICE", "com.ss.android.ugc.aweme")
assert ok is False
assert pkg == ""
def test_default_tries_all_packages_in_order(self, op):
"""不指定包名时按 DOUYIN_PACKAGES 顺序尝试,第二个包成功也算成功。"""
call_count = {"n": 0}
def fake_capture(device_id, cmd, **kwargs):
call_count["n"] += 1
# 第一个包失败、第二个包成功
if "com.ss.android.ugc.aweme.lite" in cmd:
return (True, "Events injected: 1")
return (False, "")
with patch.object(op, "_run_adb_shell_capture", side_effect=fake_capture), \
patch.object(op, "_run_adb_shell", return_value=False):
ok, pkg = op.launch_package("MOCK_DEVICE")
assert ok is True
assert pkg == "com.ss.android.ugc.aweme.lite"
def test_empty_package_list_falls_through(self, op):
"""显式传入空字符串使用默认列表。"""
with patch.object(op, "_run_adb_shell_capture", return_value=(True, "Events injected: 1")):
ok, pkg = op.launch_package("MOCK_DEVICE", "")
assert ok is True
# 默认应该尝试主版抖音
assert pkg == "com.ss.android.ugc.aweme"
# ============================================================
# is_douyin_foreground
# ============================================================
class TestIsDouyinForeground:
def test_aweme_main(self, op):
with patch.object(op, "get_top_package", return_value="com.ss.android.ugc.aweme"):
assert op.is_douyin_foreground("MOCK") is True
def test_aweme_lite(self, op):
with patch.object(op, "get_top_package", return_value="com.ss.android.ugc.aweme.lite"):
assert op.is_douyin_foreground("MOCK") is True
def test_launcher_returns_false(self, op):
with patch.object(op, "get_top_package", return_value="com.android.launcher"):
assert op.is_douyin_foreground("MOCK") is False
def test_empty_returns_false(self, op):
with patch.object(op, "get_top_package", return_value=""):
assert op.is_douyin_foreground("MOCK") is False
+48 -1
View File
@@ -102,10 +102,14 @@ class TestHandleDesktopRecovery:
s.operation.get_top_package = MagicMock(return_value="com.ss.android.ugc.aweme")
assert ronce.handle_desktop_recovery(s) is False
def test_outside_douyin_triggers_recovery(self):
def test_outside_douyin_uses_launch_package_first(self):
"""非抖音前台时应优先调用 launch_package,不再点击桌面图标。"""
s = _mk_self()
s.operation = MagicMock()
s.operation.get_top_package = MagicMock(return_value="com.android.launcher")
s.operation.launch_package = MagicMock(
return_value=(True, "com.ss.android.ugc.aweme")
)
s.click = MagicMock(return_value=None)
s.run_once_basic = MagicMock(return_value=None)
s.desktop_icon_x = 153
@@ -117,8 +121,51 @@ class TestHandleDesktopRecovery:
result = ronce.handle_desktop_recovery(s)
assert result is True
s.operation.launch_package.assert_called_once_with(s.device_id, "")
# 包名启动成功时不应再点击桌面坐标
s.click.assert_not_called()
s.run_once_basic.assert_called_once()
def test_launch_package_failure_falls_back_to_clicks(self):
"""包名启动失败时回退到旧的点击坐标自救。"""
s = _mk_self()
s.operation = MagicMock()
s.operation.get_top_package = MagicMock(return_value="com.android.launcher")
s.operation.launch_package = MagicMock(return_value=(False, ""))
s.click = MagicMock(return_value=None)
s.run_once_basic = MagicMock(return_value=None)
s.desktop_icon_x = 153
s.desktop_icon_y = 260
s.yanghao_fallback_live_x = 100
s.yanghao_fallback_live_y = 100
s.yanghao_fallback_enter_live_x = 100
s.yanghao_fallback_enter_live_y = 100
result = ronce.handle_desktop_recovery(s)
assert result is True
# 失败时应回退到点击坐标(桌面图标 + 直播 + 进入直播间×2 = 4 次)
assert s.click.call_count >= 3
s.run_once_basic.assert_called_once()
def test_launch_package_exception_falls_back_to_clicks(self):
"""launch_package 抛异常时也应回退到点击。"""
s = _mk_self()
s.operation = MagicMock()
s.operation.get_top_package = MagicMock(return_value="com.android.launcher")
s.operation.launch_package = MagicMock(side_effect=RuntimeError("adb dead"))
s.click = MagicMock(return_value=None)
s.run_once_basic = MagicMock(return_value=None)
s.desktop_icon_x = 153
s.desktop_icon_y = 260
s.yanghao_fallback_live_x = 100
s.yanghao_fallback_live_y = 100
s.yanghao_fallback_enter_live_x = 100
s.yanghao_fallback_enter_live_y = 100
result = ronce.handle_desktop_recovery(s)
assert result is True
s.click.assert_called()
def test_get_top_package_failed_returns_false(self):
s = _mk_self()
s.operation = MagicMock()