211 lines
7.9 KiB
Python
211 lines
7.9 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
"""
|
|||
|
|
自动手机双清脚本
|
|||
|
|
|
|||
|
|
该脚本提供了一个模块化的框架,用于自动执行Android手机的双清操作(清除数据/恢复出厂设置和清除缓存分区)。
|
|||
|
|
设计遵循SOLID原则,便于后期维护和功能扩展。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import os
|
|||
|
|
import sys
|
|||
|
|
import time
|
|||
|
|
import subprocess
|
|||
|
|
import logging
|
|||
|
|
from typing import List, Optional
|
|||
|
|
|
|||
|
|
# 导入配置、日志和ADB工具
|
|||
|
|
from config import Config
|
|||
|
|
from logger_setup import setup_logger
|
|||
|
|
from adb_tool import ADBTool, DeviceInfo
|
|||
|
|
|
|||
|
|
# 初始化日志
|
|||
|
|
logger = setup_logger()
|
|||
|
|
logger.debug("主脚本初始化完成")
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
class RecoveryOperations:
|
|||
|
|
"""Recovery模式操作类"""
|
|||
|
|
|
|||
|
|
def __init__(self, adb_tool: ADBTool):
|
|||
|
|
self.adb_tool = adb_tool
|
|||
|
|
logger.debug("RecoveryOperations初始化完成")
|
|||
|
|
|
|||
|
|
def wipe_data(self, serial: Optional[str] = None) -> None:
|
|||
|
|
"""清除数据/恢复出厂设置"""
|
|||
|
|
logger.debug(f"开始清除数据/恢复出厂设置,设备序列号: {serial}")
|
|||
|
|
logger.info("开始清除数据/恢复出厂设置...")
|
|||
|
|
command = ["shell", "wipe data"]
|
|||
|
|
if serial:
|
|||
|
|
command = ["-s", serial] + command
|
|||
|
|
|
|||
|
|
self.adb_tool.run_command(command)
|
|||
|
|
logger.debug(f"等待清除数据完成,等待时间: {Config.WIPE_DATA_WAIT_TIME}秒")
|
|||
|
|
time.sleep(Config.WIPE_DATA_WAIT_TIME) # 等待操作完成
|
|||
|
|
logger.debug("清除数据操作完成")
|
|||
|
|
|
|||
|
|
def wipe_cache(self, serial: Optional[str] = None) -> None:
|
|||
|
|
"""清除缓存分区"""
|
|||
|
|
logger.debug(f"开始清除缓存分区,设备序列号: {serial}")
|
|||
|
|
logger.info("开始清除缓存分区...")
|
|||
|
|
command = ["shell", "wipe cache"]
|
|||
|
|
if serial:
|
|||
|
|
command = ["-s", serial] + command
|
|||
|
|
|
|||
|
|
self.adb_tool.run_command(command)
|
|||
|
|
logger.debug(f"等待清除缓存完成,等待时间: {Config.WIPE_CACHE_WAIT_TIME}秒")
|
|||
|
|
time.sleep(Config.WIPE_CACHE_WAIT_TIME) # 等待操作完成
|
|||
|
|
logger.debug("清除缓存操作完成")
|
|||
|
|
|
|||
|
|
def reboot_system(self, serial: Optional[str] = None) -> None:
|
|||
|
|
"""重启系统"""
|
|||
|
|
logger.debug(f"开始重启系统,设备序列号: {serial}")
|
|||
|
|
logger.info("开始重启系统...")
|
|||
|
|
command = ["shell", "reboot"]
|
|||
|
|
if serial:
|
|||
|
|
command = ["-s", serial] + command
|
|||
|
|
|
|||
|
|
self.adb_tool.run_command(command)
|
|||
|
|
logger.debug("重启系统命令已发送")
|
|||
|
|
|
|||
|
|
|
|||
|
|
class AutoCleanController:
|
|||
|
|
"""自动双清控制器类"""
|
|||
|
|
|
|||
|
|
def __init__(self):
|
|||
|
|
logger.debug("初始化AutoCleanController")
|
|||
|
|
self.adb_tool = ADBTool()
|
|||
|
|
self.recovery_ops = RecoveryOperations(self.adb_tool)
|
|||
|
|
logger.debug("AutoCleanController初始化完成")
|
|||
|
|
|
|||
|
|
def run_auto_clean(self, serial: Optional[str] = None) -> None:
|
|||
|
|
"""执行自动双清流程"""
|
|||
|
|
logger.debug("开始执行自动双清流程")
|
|||
|
|
try:
|
|||
|
|
# 检查设备连接
|
|||
|
|
devices = self.adb_tool.get_connected_devices()
|
|||
|
|
if not devices:
|
|||
|
|
logger.error("未检测到已连接的设备")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
logger.info(f"检测到 {len(devices)} 台设备")
|
|||
|
|
for device in devices:
|
|||
|
|
logger.info(f"设备: {device.serial} - {device.model}")
|
|||
|
|
logger.debug(f"设备详细信息: {device}")
|
|||
|
|
|
|||
|
|
# 选择设备
|
|||
|
|
target_serial = serial if serial else devices[0].serial
|
|||
|
|
logger.info(f"选择设备: {target_serial}")
|
|||
|
|
logger.debug(f"目标设备序列号: {target_serial}")
|
|||
|
|
|
|||
|
|
# 重启到Recovery模式
|
|||
|
|
logger.debug("步骤1: 重启设备到Recovery模式")
|
|||
|
|
self.adb_tool.reboot_to_recovery(target_serial)
|
|||
|
|
|
|||
|
|
# 等待Recovery模式
|
|||
|
|
logger.debug("步骤2: 等待设备进入Recovery模式")
|
|||
|
|
if not self.adb_tool.wait_for_recovery_mode(target_serial):
|
|||
|
|
logger.error("设备未进入Recovery模式,操作终止")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 执行双清
|
|||
|
|
logger.debug("步骤3: 执行清除数据操作")
|
|||
|
|
self.recovery_ops.wipe_data(target_serial)
|
|||
|
|
|
|||
|
|
logger.debug("步骤4: 执行清除缓存操作")
|
|||
|
|
self.recovery_ops.wipe_cache(target_serial)
|
|||
|
|
|
|||
|
|
# 重启系统
|
|||
|
|
if Config.AUTO_REBOOT_AFTER_CLEAN:
|
|||
|
|
logger.debug("步骤5: 重启系统")
|
|||
|
|
self.recovery_ops.reboot_system(target_serial)
|
|||
|
|
|
|||
|
|
logger.info("双清操作完成,设备正在重启...")
|
|||
|
|
logger.debug("自动双清流程执行完毕")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
logger.error(f"自动双清过程中发生错误: {e}")
|
|||
|
|
logger.debug(f"错误详情: {str(e)}")
|
|||
|
|
raise
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""主函数"""
|
|||
|
|
logger.debug("主函数开始执行")
|
|||
|
|
try:
|
|||
|
|
controller = AutoCleanController()
|
|||
|
|
logger.debug("AutoCleanController创建成功")
|
|||
|
|
|
|||
|
|
# 显示欢迎信息
|
|||
|
|
print("=" * 60)
|
|||
|
|
print("自动手机双清脚本")
|
|||
|
|
print("=" * 60)
|
|||
|
|
|
|||
|
|
# 检查设备
|
|||
|
|
devices = controller.adb_tool.get_connected_devices()
|
|||
|
|
logger.debug(f"设备列表: {devices}")
|
|||
|
|
|
|||
|
|
if not devices:
|
|||
|
|
print("未检测到已连接的Android设备")
|
|||
|
|
print("请确保设备已开启USB调试并连接到电脑")
|
|||
|
|
logger.warning("未检测到任何已连接的设备")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 选择设备
|
|||
|
|
if len(devices) == 1:
|
|||
|
|
print(f"检测到设备: {devices[0].serial} - {devices[0].model}")
|
|||
|
|
logger.debug(f"单设备模式,设备: {devices[0].serial}")
|
|||
|
|
|
|||
|
|
if Config.CONFIRM_BEFORE_OPERATION:
|
|||
|
|
choice = input("是否开始双清操作? (y/N): ")
|
|||
|
|
if choice.lower() == 'y':
|
|||
|
|
controller.run_auto_clean(devices[0].serial)
|
|||
|
|
else:
|
|||
|
|
print("操作已取消")
|
|||
|
|
logger.info("用户取消操作")
|
|||
|
|
else:
|
|||
|
|
print("自动开始双清操作...")
|
|||
|
|
controller.run_auto_clean(devices[0].serial)
|
|||
|
|
else:
|
|||
|
|
print("检测到多个设备:")
|
|||
|
|
for i, device in enumerate(devices):
|
|||
|
|
print(f"{i+1}. {device.serial} - {device.model}")
|
|||
|
|
|
|||
|
|
logger.debug(f"多设备模式,共{len(devices)}台设备")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
selection = int(input("请选择要操作的设备编号: ")) - 1
|
|||
|
|
if 0 <= selection < len(devices):
|
|||
|
|
print(f"选择设备: {devices[selection].serial} - {devices[selection].model}")
|
|||
|
|
|
|||
|
|
if Config.CONFIRM_BEFORE_OPERATION:
|
|||
|
|
choice = input("是否开始双清操作? (y/N): ")
|
|||
|
|
if choice.lower() == 'y':
|
|||
|
|
controller.run_auto_clean(devices[selection].serial)
|
|||
|
|
else:
|
|||
|
|
print("操作已取消")
|
|||
|
|
logger.info("用户取消操作")
|
|||
|
|
else:
|
|||
|
|
print("自动开始双清操作...")
|
|||
|
|
controller.run_auto_clean(devices[selection].serial)
|
|||
|
|
else:
|
|||
|
|
print("无效的选择")
|
|||
|
|
logger.warning("用户输入无效的设备编号")
|
|||
|
|
except ValueError:
|
|||
|
|
print("请输入有效的数字")
|
|||
|
|
logger.error("用户输入非数字字符")
|
|||
|
|
|
|||
|
|
except KeyboardInterrupt:
|
|||
|
|
print("\n操作已取消")
|
|||
|
|
logger.info("用户通过Ctrl+C取消操作")
|
|||
|
|
except Exception as e:
|
|||
|
|
logger.error(f"程序执行失败: {e}")
|
|||
|
|
print(f"错误: {e}")
|
|||
|
|
finally:
|
|||
|
|
logger.debug("主函数执行完毕")
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|