413 lines
15 KiB
Python
413 lines
15 KiB
Python
"""WiFi ADB 主设置对话框。
|
||
|
||
从 ``gui/gui_app.py`` 中抽出。原本是 ``main()`` 内的闭包 ``open_wifi_adb_dialog``,
|
||
通过 "WiFi ADB" 顶部按钮触发。
|
||
|
||
依赖通过显式参数注入:``parent`` (主窗口)、``adb_exe`` (ADB 可执行路径)、
|
||
``on_line`` (日志回调)、``refresh_devices`` (刷新主窗口设备下拉的回调)。
|
||
连接成功后会自递归打开新对话框以反映最新设备状态。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import re
|
||
import threading
|
||
import tkinter as tk
|
||
from tkinter import ttk
|
||
from tkinter.constants import LEFT, W, X
|
||
from typing import Callable
|
||
|
||
from gui.gui_core import (
|
||
auto_connect_wifi_adb,
|
||
connect_wifi_adb,
|
||
disconnect_wifi_adb,
|
||
enable_wifi_adb,
|
||
get_connected_wifi_devices,
|
||
get_device_ip,
|
||
list_adb_devices,
|
||
)
|
||
from gui.gui_core_pkg import (
|
||
clear_last_wifi_target,
|
||
get_last_wifi_target,
|
||
set_last_wifi_target,
|
||
)
|
||
from gui.gui_core_pkg.wifi_adb_scan import query_device_model
|
||
from gui.components.wifi_adb_scan_dialog import open_scan_dialog
|
||
|
||
|
||
def open_wifi_adb_dialog(
|
||
parent: tk.Misc,
|
||
*,
|
||
adb_exe: str,
|
||
on_line: Callable[[str], None],
|
||
refresh_devices: Callable[[], None],
|
||
) -> None:
|
||
"""打开WiFi ADB设置对话框"""
|
||
dialog = tk.Toplevel(parent)
|
||
dialog.title("WiFi ADB 设置")
|
||
dialog.geometry("500x450")
|
||
# 允许用户调整对话框大小,避免窗口内容被裁剪
|
||
dialog.resizable(True, True)
|
||
dialog.minsize(420, 320)
|
||
|
||
# 获取设备列表并分类
|
||
current_devices = list_adb_devices(adb_exe)
|
||
wifi_devices = get_connected_wifi_devices(adb_exe)
|
||
usb_devices = [d for d in current_devices if d not in wifi_devices]
|
||
|
||
# 创建设备选择区域
|
||
device_frame = ttk.LabelFrame(dialog, text="设备选择", padding=10)
|
||
device_frame.pack(fill=X, padx=10, pady=10)
|
||
# 使内部 grid 列随窗口宽度伸缩
|
||
device_frame.grid_columnconfigure(0, weight=3)
|
||
device_frame.grid_columnconfigure(1, weight=1)
|
||
|
||
# USB设备选择
|
||
ttk.Label(device_frame, text="USB设备(用于首次启用):").grid(
|
||
row=0, column=0, sticky=W, pady=(0, 5)
|
||
)
|
||
usb_device_var = tk.StringVar(value=(usb_devices[0] if usb_devices else ""))
|
||
usb_combo = ttk.Combobox(
|
||
device_frame,
|
||
textvariable=usb_device_var,
|
||
values=usb_devices,
|
||
width=25,
|
||
state="readonly" if not usb_devices else "normal",
|
||
)
|
||
usb_combo.grid(row=1, column=0, padx=(0, 10), pady=(0, 10), sticky="we")
|
||
|
||
# 显示USB设备状态
|
||
usb_status_text = (
|
||
f"检测到 {len(usb_devices)} 个USB设备"
|
||
if usb_devices
|
||
else "未检测到USB设备,请确保设备已连接并授权"
|
||
)
|
||
usb_status_label = ttk.Label(
|
||
device_frame,
|
||
text=usb_status_text,
|
||
font=("Arial", 8),
|
||
foreground="red" if not usb_devices else "green",
|
||
)
|
||
usb_status_label.grid(row=2, column=0, sticky=W, pady=(0, 10))
|
||
|
||
# WiFi设备选择
|
||
ttk.Label(device_frame, text="WiFi设备(已连接):").grid(
|
||
row=0, column=1, sticky=W, pady=(0, 5)
|
||
)
|
||
wifi_device_var = tk.StringVar(value=(wifi_devices[0] if wifi_devices else ""))
|
||
wifi_combo = ttk.Combobox(
|
||
device_frame,
|
||
textvariable=wifi_device_var,
|
||
values=wifi_devices,
|
||
width=25,
|
||
state="readonly" if not wifi_devices else "normal",
|
||
)
|
||
wifi_combo.grid(row=1, column=1, pady=(0, 10), sticky="we")
|
||
|
||
# 显示WiFi设备状态
|
||
wifi_status_text = (
|
||
f"已连接 {len(wifi_devices)} 个WiFi设备"
|
||
if wifi_devices
|
||
else "无WiFi设备连接"
|
||
)
|
||
wifi_status_label = ttk.Label(
|
||
device_frame,
|
||
text=wifi_status_text,
|
||
font=("Arial", 8),
|
||
foreground="blue" if wifi_devices else "gray",
|
||
)
|
||
wifi_status_label.grid(row=2, column=1, sticky=W, pady=(0, 10))
|
||
|
||
# IP地址输入区域
|
||
ip_frame = ttk.LabelFrame(dialog, text="连接信息", padding=10)
|
||
ip_frame.pack(fill=X, padx=10, pady=(0, 10))
|
||
# 使 IP/端口 输入框随窗口宽度伸缩
|
||
ip_frame.grid_columnconfigure(0, weight=3)
|
||
ip_frame.grid_columnconfigure(1, weight=1)
|
||
|
||
ttk.Label(ip_frame, text="设备IP地址:").grid(
|
||
row=0, column=0, sticky=W, pady=(0, 5)
|
||
)
|
||
ip_var = tk.StringVar()
|
||
ip_entry = ttk.Entry(ip_frame, textvariable=ip_var, width=25)
|
||
ip_entry.grid(row=1, column=0, padx=(0, 10), pady=(0, 10), sticky="we")
|
||
|
||
ttk.Label(ip_frame, text="端口(默认5555):").grid(
|
||
row=0, column=1, sticky=W, pady=(0, 5)
|
||
)
|
||
port_var = tk.StringVar(value="5555")
|
||
port_entry = ttk.Entry(ip_frame, textvariable=port_var, width=25)
|
||
port_entry.grid(row=1, column=1, pady=(0, 10), sticky="we")
|
||
|
||
# 按钮区域
|
||
button_frame = ttk.Frame(dialog)
|
||
button_frame.pack(fill=X, padx=10, pady=10)
|
||
|
||
# 放大/恢复窗口支持(当系统边框无法调整窗口时提供备用按钮)
|
||
is_maximized = False
|
||
original_geometry = None
|
||
|
||
def toggle_maximize():
|
||
nonlocal is_maximized, original_geometry
|
||
try:
|
||
if not is_maximized:
|
||
original_geometry = dialog.geometry()
|
||
dialog.update_idletasks()
|
||
w = int(dialog.winfo_screenwidth() * 0.8)
|
||
h = int(dialog.winfo_screenheight() * 0.8)
|
||
dialog.geometry(f"{w}x{h}")
|
||
is_maximized = True
|
||
else:
|
||
if original_geometry:
|
||
dialog.geometry(original_geometry)
|
||
is_maximized = False
|
||
except Exception as e:
|
||
on_line(f"[WiFi ADB] 切换窗口大小失败: {e}")
|
||
|
||
def enable_wifi():
|
||
device_id = usb_device_var.get()
|
||
if not device_id:
|
||
on_line("[WiFi ADB] 请先选择USB设备")
|
||
return
|
||
success, msg = enable_wifi_adb(adb_exe, device_id)
|
||
on_line(f"[WiFi ADB] {msg}")
|
||
if success:
|
||
# 尝试获取IP地址
|
||
ip_success, ip_msg = get_device_ip(adb_exe, device_id)
|
||
if ip_success:
|
||
ip_var.set(ip_msg)
|
||
on_line(f"[WiFi ADB] 设备IP地址: {ip_msg}")
|
||
|
||
def connect_wifi():
|
||
ip = ip_var.get().strip()
|
||
port = port_var.get().strip() or "5555"
|
||
if not ip:
|
||
on_line("[WiFi ADB] 请输入IP地址")
|
||
return
|
||
success, msg = connect_wifi_adb(adb_exe, ip, port)
|
||
on_line(f"[WiFi ADB] {msg}")
|
||
if success:
|
||
# 记住,下次启动自动重连
|
||
target = ip if ":" in ip else f"{ip}:{port}"
|
||
model = ""
|
||
try:
|
||
model = query_device_model(adb_exe, target)
|
||
except Exception:
|
||
pass
|
||
try:
|
||
set_last_wifi_target(target, model)
|
||
on_line(
|
||
f"[WiFi ADB] 已记住 {target}"
|
||
+ (f" ({model})" if model else "")
|
||
+ ",下次 GUI 启动会自动重连"
|
||
)
|
||
except Exception:
|
||
pass
|
||
# 刷新设备列表
|
||
refresh_devices()
|
||
# 重新打开对话框以显示新的WiFi设备
|
||
dialog.destroy()
|
||
open_wifi_adb_dialog(parent, adb_exe=adb_exe, on_line=on_line, refresh_devices=refresh_devices)
|
||
|
||
def disconnect_wifi():
|
||
device_id = wifi_device_var.get()
|
||
if not device_id:
|
||
on_line("[WiFi ADB] 请先选择WiFi设备")
|
||
return
|
||
# 从设备ID中提取IP地址
|
||
ip_match = re.search(r"(\d+\.\d+\.\d+\.\d+)", device_id)
|
||
if not ip_match:
|
||
on_line("[WiFi ADB] 无法从设备ID中提取IP地址")
|
||
return
|
||
ip = ip_match.group(1)
|
||
success, msg = disconnect_wifi_adb(adb_exe, ip, "5555")
|
||
on_line(f"[WiFi ADB] {msg}")
|
||
if success:
|
||
# 如果断开的就是上次记住的设备,清掉记录避免下次启动徒劳重试
|
||
try:
|
||
last = get_last_wifi_target()
|
||
last_target = str(last.get("target") or "")
|
||
if last_target.startswith(ip):
|
||
clear_last_wifi_target()
|
||
on_line(
|
||
f"[WiFi ADB] 已清除上次设备记录 {last_target}"
|
||
)
|
||
except Exception:
|
||
pass
|
||
# 刷新设备列表
|
||
refresh_devices()
|
||
# 重新打开对话框以更新设备列表
|
||
dialog.destroy()
|
||
open_wifi_adb_dialog(parent, adb_exe=adb_exe, on_line=on_line, refresh_devices=refresh_devices)
|
||
|
||
def auto_connect_wifi():
|
||
device_id = usb_device_var.get()
|
||
if not device_id:
|
||
on_line("[WiFi ADB] 请先选择USB设备")
|
||
return
|
||
|
||
# 显示进度
|
||
on_line("[WiFi ADB] 开始自动连接...")
|
||
|
||
# 创建进度对话框
|
||
progress_dialog = tk.Toplevel(dialog)
|
||
progress_dialog.title("自动连接中...")
|
||
progress_dialog.geometry("300x120")
|
||
progress_dialog.resizable(False, False)
|
||
|
||
ttk.Label(progress_dialog, text="正在自动连接WiFi ADB,请稍候...").pack(
|
||
pady=10
|
||
)
|
||
ttk.Label(progress_dialog, text="设备将会重启,请等待重启完成").pack(
|
||
pady=(0, 10)
|
||
)
|
||
progress_bar = ttk.Progressbar(progress_dialog, mode="indeterminate")
|
||
progress_bar.pack(fill=X, padx=20, pady=(0, 20))
|
||
progress_bar.start()
|
||
|
||
def do_auto_connect():
|
||
try:
|
||
success, msg = auto_connect_wifi_adb(adb_exe, device_id)
|
||
progress_dialog.destroy()
|
||
on_line(f"[WiFi ADB] {msg}")
|
||
if success:
|
||
# 刷新设备列表
|
||
refresh_devices()
|
||
# 尝试获取并显示IP
|
||
ip_success, ip_msg = get_device_ip(adb_exe, device_id)
|
||
if ip_success:
|
||
ip_var.set(ip_msg)
|
||
# 记住目标设备
|
||
target = (
|
||
ip_msg if ":" in ip_msg else f"{ip_msg}:5555"
|
||
)
|
||
model = ""
|
||
try:
|
||
model = query_device_model(adb_exe, target)
|
||
except Exception:
|
||
pass
|
||
try:
|
||
set_last_wifi_target(target, model)
|
||
on_line(
|
||
f"[WiFi ADB] 已记住 {target}"
|
||
+ (f" ({model})" if model else "")
|
||
+ ",下次 GUI 启动会自动重连"
|
||
)
|
||
except Exception:
|
||
pass
|
||
# 重新打开对话框以显示新的WiFi设备
|
||
dialog.destroy()
|
||
open_wifi_adb_dialog(parent, adb_exe=adb_exe, on_line=on_line, refresh_devices=refresh_devices)
|
||
except Exception as e:
|
||
progress_dialog.destroy()
|
||
on_line(f"[WiFi ADB] 自动连接异常: {str(e)}")
|
||
|
||
# 在后台线程中执行自动连接
|
||
threading.Thread(target=do_auto_connect, daemon=True).start()
|
||
|
||
def refresh_device_lists():
|
||
"""刷新设备列表"""
|
||
nonlocal usb_devices, wifi_devices
|
||
current_devices = list_adb_devices(adb_exe)
|
||
wifi_devices = get_connected_wifi_devices(adb_exe)
|
||
usb_devices = [d for d in current_devices if d not in wifi_devices]
|
||
|
||
# 更新USB设备下拉框
|
||
usb_combo["values"] = usb_devices
|
||
usb_combo["state"] = "readonly" if not usb_devices else "normal"
|
||
if usb_devices:
|
||
usb_device_var.set(usb_devices[0])
|
||
else:
|
||
usb_device_var.set("")
|
||
|
||
# 更新WiFi设备下拉框
|
||
wifi_combo["values"] = wifi_devices
|
||
wifi_combo["state"] = "readonly" if not wifi_devices else "normal"
|
||
if wifi_devices:
|
||
wifi_device_var.set(wifi_devices[0])
|
||
else:
|
||
wifi_device_var.set("")
|
||
|
||
# 更新状态标签
|
||
usb_status_text = (
|
||
f"检测到 {len(usb_devices)} 个USB设备"
|
||
if usb_devices
|
||
else "未检测到USB设备,请确保设备已连接并授权"
|
||
)
|
||
usb_status_label.config(
|
||
text=usb_status_text, foreground="red" if not usb_devices else "green"
|
||
)
|
||
|
||
wifi_status_text = (
|
||
f"已连接 {len(wifi_devices)} 个WiFi设备"
|
||
if wifi_devices
|
||
else "无WiFi设备连接"
|
||
)
|
||
wifi_status_label.config(
|
||
text=wifi_status_text, foreground="blue" if wifi_devices else "gray"
|
||
)
|
||
|
||
on_line("[WiFi ADB] 设备列表已刷新")
|
||
|
||
# USB设备操作按钮
|
||
usb_button_frame = ttk.Frame(button_frame)
|
||
usb_button_frame.pack(fill=X, pady=(0, 10))
|
||
|
||
ttk.Label(usb_button_frame, text="USB设备操作:").pack(anchor=W)
|
||
usb_buttons_frame = ttk.Frame(usb_button_frame)
|
||
usb_buttons_frame.pack(fill=X, pady=(5, 0))
|
||
|
||
ttk.Button(usb_buttons_frame, text="自动连接", command=auto_connect_wifi).pack(
|
||
side=LEFT, padx=(0, 10)
|
||
)
|
||
ttk.Button(usb_buttons_frame, text="启用WiFi ADB", command=enable_wifi).pack(
|
||
side=LEFT, padx=(0, 10)
|
||
)
|
||
ttk.Button(usb_buttons_frame, text="连接", command=connect_wifi).pack(
|
||
side=LEFT, padx=(0, 10)
|
||
)
|
||
ttk.Button(
|
||
usb_buttons_frame, text="刷新设备", command=refresh_device_lists
|
||
).pack(side=LEFT)
|
||
# 如果窗口无法通过边框调整,提供放大/恢复按钮作为备用
|
||
ttk.Button(usb_buttons_frame, text="放大/恢复", command=toggle_maximize).pack(
|
||
side=LEFT, padx=(8, 0)
|
||
)
|
||
|
||
# WiFi设备操作按钮
|
||
wifi_button_frame = ttk.Frame(button_frame)
|
||
wifi_button_frame.pack(fill=X, pady=(10, 0))
|
||
|
||
ttk.Label(wifi_button_frame, text="WiFi设备操作:").pack(anchor=W)
|
||
wifi_buttons_frame = ttk.Frame(wifi_button_frame)
|
||
wifi_buttons_frame.pack(fill=X, pady=(5, 0))
|
||
|
||
ttk.Button(wifi_buttons_frame, text="断开连接", command=disconnect_wifi).pack(
|
||
side=LEFT
|
||
)
|
||
ttk.Button(
|
||
wifi_buttons_frame,
|
||
text="扫描局域网",
|
||
command=lambda: open_scan_dialog(
|
||
dialog, adb_exe, ip_var, on_line, refresh_device_lists
|
||
),
|
||
).pack(side=LEFT, padx=(10, 0))
|
||
|
||
# 说明文本
|
||
info_text = """重要说明:
|
||
• Android系统安全要求:首次启用WiFi ADB必须通过USB连接
|
||
• 工作流程:USB连接 → 启用WiFi ADB → 设备重启 → WiFi连接
|
||
• 设置完成后,设备会出现在"WiFi设备"列表中,无需再次USB连接
|
||
• 设备重启后WiFi ADB模式会保持,可直接通过WiFi连接使用
|
||
|
||
使用方法:
|
||
1. 连接Android设备到电脑(USB)
|
||
2. 点击"刷新设备"确保设备被检测到
|
||
3. 选择USB设备,点击"自动连接"或手动操作
|
||
4. 等待设备重启后即可通过WiFi使用"""
|
||
|
||
info_label = ttk.Label(
|
||
dialog, text=info_text, justify=LEFT, font=("Arial", 9), wraplength=460
|
||
)
|
||
info_label.pack(pady=(10, 0), padx=20, anchor="w")
|