# -*- coding: utf-8 -*- """「功能」分页改成动态:分组由电脑端声明,手表端拉取后渲染。 原来写死了 6 个分组按钮,现在电脑端有 10 组、80 个功能, 写死就不合适了 —— 以后电脑端加一组,手表端自动就有。 """ 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") # 1. 「功能」分页只留一个占位按钮(真正的按钮动态生成) TAB_OLD = ''' new TabDef("功能", new Act[]{ act("显示", new Runnable() { public void run() { pickFeatureGroup("显示"); } }), act("音量", new Runnable() { public void run() { pickFeatureGroup("音量"); } }), act("系统", new Runnable() { public void run() { pickFeatureGroup("系统"); } }), act("查看", new Runnable() { public void run() { pickFeatureGroup("查看"); } }), act("效率", new Runnable() { public void run() { pickFeatureGroup("效率"); } }), act("文件", new Runnable() { public void run() { pickFeatureGroup("文件"); } }), }),''' TAB_NEW = ''' // 「功能」页的按钮是动态生成的,这里留空 new TabDef("功能", new Act[]{}),''' # 2. selectTab:功能页走动态构建 SELECT_OLD = ''' tabContent.removeAllViews(); cellQueue.clear(); for (Act item : tabDefs[index].acts) { addRow(tabContent, action(item.label, item.task), null); } flushGrid(tabContent); }''' SELECT_NEW = ''' if ("功能".equals(tabDefs[index].name)) { buildFeatureTab(); return; } tabContent.removeAllViews(); cellQueue.clear(); for (Act item : tabDefs[index].acts) { addRow(tabContent, action(item.label, item.task), null); } flushGrid(tabContent); } /** 「功能」页:分组由电脑端声明,这里拉取后动态生成按钮。 */ private void buildFeatureTab() { tabContent.removeAllViews(); cellQueue.clear(); if (featureGroups == null) { TextView hint = new TextView(this); hint.setText("正在获取电脑端功能列表…"); hint.setTextColor(C_DIM); hint.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12); hint.setPadding(dp(4), dp(8), dp(4), dp(8)); tabContent.addView(hint); io.execute(new Runnable() { @Override public void run() { try { Map reply = client.call(Json.obj("cmd", "feature_list"), 20000); final List> items = mapList(reply, "features"); final List groups = new ArrayList(); for (Map item : items) { String group = Json.str(item, "group", "其他"); if (!groups.contains(group)) { groups.add(group); } } featureCache = items; ui.post(new Runnable() { @Override public void run() { featureGroups = groups; selectTab(currentTab); } }); } catch (final IOException e) { ui.post(new Runnable() { @Override public void run() { appendLog("获取功能列表失败:" + e.getMessage()); toast("没能连上电脑"); } }); } } }); return; } if (featureGroups.isEmpty()) { TextView hint = new TextView(this); hint.setText("电脑端没有声明任何功能"); hint.setTextColor(C_DIM); hint.setPadding(dp(4), dp(8), dp(4), dp(8)); tabContent.addView(hint); return; } for (final String group : featureGroups) { addRow(tabContent, action(group, new Runnable() { @Override public void run() { pickFeatureGroup(group); } }), null); } flushGrid(tabContent); }''' # 3. 字段 FIELD_OLD = " private List> featureCache = null;" FIELD_NEW = (" private List> featureCache = null;\n" " private List featureGroups = null;") def main() -> int: text = MAIN.read_text(encoding="utf-8") ok = True for old, new, tag in ((FIELD_OLD, FIELD_NEW, "字段"), (TAB_OLD, TAB_NEW, "功能分页"), (SELECT_OLD, SELECT_NEW, "动态构建")): if old not in text: print(f" [失败] {tag} 锚点未匹配") ok = False else: text = text.replace(old, new, 1) print(f" {tag}: OK") if ok: MAIN.write_text(text, encoding="utf-8") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())