# -*- coding: utf-8 -*- """安卓改造第二步:清残余 + 实现"输入配对码连接"。 问题: * requestPcSelfCheck() 还在用局域网广播(Discovery) * onConnectClicked() 还在读 hostInput/portInput(那两个输入框已经删了) * quickConnect() 还在往已删的输入框里写 * 上一个补丁里引用了 promptPairingCode(),但还没实现 做完之后,手表的连接方式只有一种: 经中转 + 配对码。不用管 IP、端口、局域网。 """ from __future__ import annotations 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: """按花括号配平删掉一个方法(连前面的注释行)。""" import re 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 ON_CONNECT_OLD = ''' private void onConnectClicked() { String host = hostInput.getText().toString().trim(); String portText = portInput.getText().toString().trim(); String code = codeInput.getText().toString().trim(); if (host.isEmpty()) { setStatus("请先填写电脑 IP", C_WARN); return; } int port; try { port = portText.isEmpty() ? RemoteClient.DEFAULT_PORT : Integer.parseInt(portText); } catch (NumberFormatException e) { setStatus("端口必须是数字", C_WARN); return; } if (code.isEmpty()) { setStatus("请填写电脑上显示的配对码", C_WARN); return; } connect(host, port, code, null, DeviceStore.deviceKey(host, port, store.deviceId())); } private void quickConnect(String host, int port, String token, String key) { hostInput.setText(host); portInput.setText(String.valueOf(port)); connect(host, port, null, token, key); }''' ON_CONNECT_NEW = ''' /** * 让用户输 6 位配对码,直接连上那台电脑。 * * 现在只有这一种连接方式了 —— 统一经中转。 * 不用管电脑 IP、端口、在不在同一个局域网。 */ private void promptPairingCode() { final EditText input = new EditText(this); input.setInputType(InputType.TYPE_CLASS_NUMBER); input.setHint("6 位数字"); input.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); input.setGravity(Gravity.CENTER); input.setPadding(dp(12), dp(14), dp(12), dp(14)); String last = savedPairingCode(); if (last != null && !last.isEmpty()) { input.setText(last); input.setSelection(input.getText().length()); } new AlertDialog.Builder(this) .setTitle("输入配对码") .setMessage("填电脑上显示的 6 位配对码\\n" + "(中转地址:" + relayUrl() + ")") .setView(input) .setPositiveButton("连接", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { connectByCode(input.getText().toString().trim()); } }) .setNegativeButton("取消", null) .show(); } /** 拿着配对码经中转连过去。 */ private void connectByCode(String code) { if (code == null || code.length() != 6 || !code.matches("\\\\d{6}")) { setStatus("配对码是 6 位数字", C_WARN); toast("配对码要 6 位数字"); return; } String relay = relayUrl(); if (relay.isEmpty()) { setStatus("还没设置中转地址", C_WARN); toast("请先在「中转设置」里填服务器地址"); promptRelayUrl(); return; } appendLog("用配对码连接:" + code); // host 传中转地址:经中转时真正连的是中转,host 只用于区分已配对的设备 connect(relay, 0, code, null, "code:" + code); } private void quickConnect(String host, int port, String token, String key) { // 不再往输入框里写东西(那两个框已经删了)—— // 直接连。host/port 对中转路径没有意义, // 但 key 仍然用来区分"哪台已配对的电脑"。 connect(host, port, null, token, key); }''' def main() -> int: text = MAIN.read_text(encoding="utf-8") ok = True # 1. 替换 onConnectClicked + quickConnect if ON_CONNECT_OLD in text: text = text.replace(ON_CONNECT_OLD, ON_CONNECT_NEW, 1) print(" 连接入口重写(配对码 -> 中转)") else: print(" [失败] 找不到 onConnectClicked/quickConnect 段") ok = False # 2. 删掉局域网广播自检 text, done = drop_method(text, r"private void requestPcSelfCheck\(\)") print(f" {'删掉' if done else '[跳过]'} requestPcSelfCheck()") # 3. 删掉 codeInput 相关(老的手输配对码框,已被弹窗取代) text, done = drop_method(text, r"private EditText codeInput") MAIN.write_text(text, encoding="utf-8", newline="\n") # 4. 残余检查 print() print(" 残余引用:") for name in ("openDiagPanel", "toggleManual", "btnManual", "foundBox", "discovering", "requestSelfCheck", "Discovery", "manualBox", "hostInput", "portInput", "requestPcSelfCheck"): count = text.count(name) if count: print(f" [{name}] 还有 {count} 处") ok = False if ok: print(" 没有残余 ✓") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())