"""桌面自救共享逻辑 把 fudai_modules.run_once_basic_helpers 和 douyin.other.douyin_yanghao 中重复的"前台不在抖音 → 包名启动 → 失败兜底点击坐标"逻辑抽到这里。 """ from __future__ import annotations import time from typing import Any, Callable, Optional def is_douyin_foreground(top_pkg: str) -> bool: """判断给定的前台包名是否属于抖音相关包。""" return bool(top_pkg) and ( "aweme" in top_pkg or "douyin" in top_pkg or "com.ss.android.ugc" in top_pkg ) def fallback_click_recovery( analyser: Any, log: Optional[Callable[[str], None]] = None, ) -> None: """旧的点击坐标自救:桌面图标 → 直播按钮 → 进入直播间×2。 保留作为 launch_package 失败时的兜底。所有坐标读 analyser 上的属性, 取不到时使用默认值(适配 1080x2400 的位置)。 """ op = analyser.operation device_id = analyser.device_id desktop_x = int(getattr(analyser, "desktop_icon_x", 153) or 153) desktop_y = int(getattr(analyser, "desktop_icon_y", 260) or 260) try: op.click(device_id, desktop_x, desktop_y) except Exception: # 退化为默认硬编码位置 try: op.click(device_id, 153, 260) except Exception: pass op.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) try: if log: log(f"点击直播按钮 x={fallback_x} y={fallback_y}") op.click(device_id, fallback_x, fallback_y) op.delay(1.5) except Exception: pass 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) try: if log: log(f"点击进入直播间 x={enter_x} y={enter_y} 次数=2") op.click(device_id, enter_x, enter_y) op.delay(0.6) op.click(device_id, enter_x, enter_y) op.delay(2.0) except Exception: pass def recover_to_douyin( analyser: Any, log: Optional[Callable[[str], None]] = None, ) -> bool: """统一的桌面自救入口。 流程: 1. 优先 op.launch_package(device_id, ""),按抖音常用包名顺序尝试 2. 包名启动失败或抛异常时,回退到点击坐标自救(fallback_click_recovery) 参数: analyser: 提供 .operation / .device_id / 自救坐标属性的对象 log: 可选的日志回调(接受单字符串) 返回: True 表示已尝试某种自救(不保证一定成功,但已执行动作); False 仅在调用环境本身出现严重异常时返回。 """ try: op = analyser.operation device_id = analyser.device_id except Exception: return False launched_ok = False used_pkg = "" try: launched_ok, used_pkg = op.launch_package(device_id, "") except Exception as exc: if log: log(f"包名启动调用异常 {exc},回退点击坐标") if launched_ok: if log: log(f"包名启动抖音成功 pkg={used_pkg}") try: op.delay(2.5) except Exception: time.sleep(2.5) return True # 兜底:保留旧的点击坐标自救 if log: log("包名启动失败,回退点击桌面图标 + 备用坐标") fallback_click_recovery(analyser, log=log) return True