# -*- coding: utf-8 -*- """安卓端改造: 用户要求: 1. "安卓端中转设置下面,把那个设备诊断给删了,换成输入配对码, 输入对应配对码就可以连接对应电脑" 2. "既然你现在用中转站了,之前那些局域网的那些东西也可以删掉了" 删: * 「连不上?点这里诊断」按钮 + openDiagPanel() * 「手动输入地址」按钮 + toggleManual() + manualBox(电脑 IP / 端口 输入) * foundBox / discovering(局域网搜索的残留) * Discovery.requestSelfCheck(局域网广播让电脑弹自检窗) 加: * 「输入配对码连接」按钮:弹个框输 6 位数字, 经中转直接连到对应电脑(不用先搜 IP) 保留: * 中转设置(这是现在的核心) * 已配对电脑的令牌自动重连(quickConnect) """ from __future__ import annotations import re import sys from pathlib import Path MAIN = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\android\src" r"\com\lanremote\watch\MainActivity.java") def drop_method(text: str, header_pattern: str) -> tuple: """按花括号配平删掉一个方法(连前面的注释行一起)。""" lines = text.splitlines(keepends=True) start = None for index, line in enumerate(lines): if re.search(header_pattern, line): start = index break if start is None: return text, False depth = 0 seen = False end = None for index in range(start, len(lines)): depth += lines[index].count("{") if "{" in lines[index]: seen = True depth -= lines[index].count("}") if seen and depth <= 0: end = index break if end is None: return text, False while start > 0 and lines[start - 1].strip().startswith("//"): start -= 1 while end + 1 < len(lines) and lines[end + 1].strip() == "": end += 1 del lines[start:end + 1] return "".join(lines), True # ---------- 1. 把设备诊断按钮换成"输入配对码连接" ---------- BTN_OLD = ''' Button selfCheck = bigButton("连不上?点这里诊断", C_WARN, 15); LinearLayout.LayoutParams scParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, dp(52)); scParams.setMargins(0, 0, 0, dp(8)); selfCheck.setLayoutParams(scParams); selfCheck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { openDiagPanel(); } }); box.addView(selfCheck);''' BTN_NEW = ''' // 输入配对码直接连对应电脑。 // 不再需要"先搜局域网 / 手动填 IP"那一套 —— // 现在统一经中转,配对码就是找电脑的唯一凭据。 Button codeButton = bigButton("输入配对码连接", C_OK, 15); LinearLayout.LayoutParams codeParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, dp(52)); codeParams.setMargins(0, 0, 0, dp(8)); codeButton.setLayoutParams(codeParams); codeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { promptPairingCode(); } }); box.addView(codeButton);''' def main() -> int: text = MAIN.read_text(encoding="utf-8") ok = True # ---- 1. 按钮替换 ---- if BTN_OLD in text: text = text.replace(BTN_OLD, BTN_NEW, 1) print(" 首页:设备诊断按钮 -> 输入配对码连接") else: print(" [失败] 找不到设备诊断按钮") ok = False # ---- 2. 删 foundBox / discovering / btnManual 的创建段 ---- text, done = drop_method(text, r"private void toggleManual\(\)") print(f" {'删掉' if done else '[跳过]'} toggleManual()") # btnManual 按钮创建(从 btnManual = bigButton 到 box.addView(btnManual);) idx = text.find("btnManual = bigButton(") if idx >= 0: end = text.find("box.addView(btnManual);", idx) if end >= 0: end = text.find("\n", end) + 1 text = text[:idx] + text[end:] print(" 删掉「手动输入地址」按钮") else: print(" [跳过] 找不到 btnManual 的挂载点") else: print(" [跳过] 没有 btnManual 创建段") # manualBox(电脑 IP / 端口 输入) idx = text.find("manualBox = new LinearLayout(") if idx < 0: idx = text.find("LinearLayout manualBox") if idx >= 0: # 往前找声明行所在语句 end = text.find("box.addView(manualBox);", idx) if end >= 0: end = text.find("\n", end) + 1 text = text[:idx] + text[end:] print(" 删掉 manualBox(电脑 IP / 端口 输入)") # foundBox 创建段 idx = text.find("foundBox = new LinearLayout(this);") if idx >= 0: end = text.find("box.addView(foundBox);", idx) if end >= 0: end = text.find("\n", end) + 1 text = text[:idx] + text[end:] print(" 删掉 foundBox(局域网搜索结果列表)") # ---- 3. 删 openDiagPanel() ---- text, done = drop_method(text, r"private void openDiagPanel\(\)") print(f" {'删掉' if done else '[跳过]'} openDiagPanel()") # ---- 4. 删设备诊断那一整段(诊断面板的辅助方法)---- for pattern, label in ( (r"private void runDiagnostics\(", "runDiagnostics()"), (r"private void copyDiagToClipboard\(", "copyDiagToClipboard()"), (r"private String buildDiagText\(", "buildDiagText()")): text, done = drop_method(text, pattern) if done: print(f" 删掉 {label}") # ---- 5. 删掉局域网广播自检那段 ---- old_block = re.search( r"\n[ \t]*//[^\n]*连不上时:广播请求[^\n]*\n" r"(?:[^\n]*\n)*?[^\n]*Discovery\.requestSelfCheck[^\n]*\n" r"(?:[^\n]*\n)*?[ \t]*\}\);\n[ \t]*\}\)\.start\(\);\n", text) if old_block: text = text[:old_block.start()] + "\n" + text[old_block.end():] print(" 删掉局域网广播自检(Discovery.requestSelfCheck)") else: # 退一步:只把那一个方法删掉 text, done = drop_method(text, r"private void requestSelfCheck") print(f" {'删掉' if done else '[跳过]'} 局域网广播自检") # ---- 6. 删掉相关字段声明 ---- for field in ("private LinearLayout foundBox;", "private boolean discovering = false;", "private Button btnManual;", "private EditText hostInput;", "private EditText portInput;", "private LinearLayout manualBox;", "private boolean manualOpen = false;"): if field in text: # 连同紧跟的注释行一起删 text = re.sub(r"[ \t]*//[^\n]*\n(?=[ \t]*" + re.escape(field) + ")", "", text, count=1) text = text.replace(field + "\n", "", 1) print(f" 删掉字段 {field.split()[-1]}") MAIN.write_text(text, encoding="utf-8", newline="\n") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())