# -*- coding: utf-8 -*- """彻底删掉设备诊断的全部代码。 用户要求:"安卓端中转设置下面,把那个设备诊断给删了" 上次只删了入口(openDiagPanel),面板代码还留着: buildDiagPanel() / closeDiagPanel() / runManualDiag() 以及 diagPanel / diagText / diagStatusLine / diagIpInput / diagRunning 还有 Diagnostics.manualTest 的调用 这次连它们一起删。删完立刻编译,确认没误伤(上次删坏过字段)。 """ from __future__ import annotations import re import subprocess import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") MAIN = ROOT / "android" / "src" / "com" / "lanremote" / "watch" / "MainActivity.java" TOOLCHAIN = Path(r"C:\Users\shu\Desktop\deepseekagent\starklink\toolchain") def method_span(lines, start): """从一个方法的签名行开始,按花括号配平找结束行(含)。""" depth = 0 seen = False 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: return index return None def drop_methods_touching(lines, marker: str) -> int: """删掉所有**方法体里**提到 marker 的方法。返回删了几个。""" removed = 0 changed = True while changed: changed = False for index, line in enumerate(lines): stripped = line.strip() # 只看方法签名行 if not (stripped.startswith(("private ", "public ", "protected ")) and "(" in stripped and stripped.endswith("{")): continue end = method_span(lines, index) if end is None: continue body = "".join(lines[index:end + 1]) if marker in body: # 往前的注释和空行一起删 start = index while start > 0 and (lines[start - 1].strip().startswith("//") or lines[start - 1].strip() == ""): start -= 1 del lines[start:end + 1] removed += 1 changed = True break return removed def main() -> int: text = MAIN.read_text(encoding="utf-8") lines = text.splitlines(keepends=True) # ---- 1. 删掉所有碰 diag 的方法 ---- total = 0 for marker in ("Diagnostics.manualTest", "buildDiagPanel", "diagPanel", "diagText", "diagStatusLine", "diagIpInput", "diagRunning"): n = drop_methods_touching(lines, marker) if n: print(f" 删掉 {n} 个用到 {marker} 的方法") total += n if not total: print(" 没有需要删的方法") text = "".join(lines) # ---- 2. 删掉字段声明 ---- for field in ("private FrameLayout diagPanel;", "private TextView diagText;", "private TextView diagStatusLine;", "private EditText diagIpInput;", "private boolean diagRunning = 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]}") # ---- 3. onBackPressed 里的诊断分支 ---- old_back = """ if (diagPanel != null && diagPanel.getVisibility() == View.VISIBLE) { closeDiagPanel(); return; } """ if old_back in text: text = text.replace(old_back, "", 1) print(" 删掉 onBackPressed 里的诊断分支") # ---- 4. 清掉只剩注释的空注释块 ---- text = text.replace(""" // ================================================================== // 设备诊断(原来是一个单独的「遥控诊断」App,现在合并进来) // ================================================================== """, "") text = re.sub(r"\n{4,}", "\n\n\n", text) MAIN.write_text(text, encoding="utf-8", newline="\n") # ---- 5. 编译验证 ---- print() print(" 编译验证:") out = ROOT / "_build" / "javacheckD" if out.exists(): import shutil shutil.rmtree(out, ignore_errors=True) out.mkdir(parents=True, exist_ok=True) srcs = [str(p) for p in (ROOT / "android" / "src").rglob("*.java")] import os env = dict(os.environ) env["JAVA_TOOL_OPTIONS"] = "-Duser.language=en -Duser.country=US" proc = subprocess.run( [str(TOOLCHAIN / "jdk17" / "bin" / "javac.exe"), "--release", "8", "-encoding", "UTF-8", "-cp", str(TOOLCHAIN / "android-sdk" / "platforms" / "android-34" / "android.jar"), "-d", str(out)] + srcs, capture_output=True, text=True, encoding="utf-8", errors="replace", env=env) if proc.returncode == 0: print(" ✓ 编译通过") else: print(" ✗ 编译失败:") for line in (proc.stdout + proc.stderr).splitlines(): if "error" in line or "symbol" in line: print(f" {line.strip()[:100]}") # ---- 6. 残余 ---- print() print(" 残余检查:") left = MAIN.read_text(encoding="utf-8") for name in ("diagPanel", "diagText", "diagStatusLine", "diagIpInput", "diagRunning", "buildDiagPanel", "closeDiagPanel", "Diagnostics.", "设备诊断"): count = left.count(name) if count: print(f" [{name}] 还有 {count} 处") if not any(left.count(n) for n in ("diagPanel", "diagText", "diagStatusLine", "diagIpInput", "diagRunning", "buildDiagPanel", "closeDiagPanel", "Diagnostics.")): print(" 没有残余 ✓") return 0 if __name__ == "__main__": sys.exit(main())