From b8c70bef98cd22221a90ab00e5775151229eb3a3 Mon Sep 17 00:00:00 2001 From: ZhanGSKen Date: Mon, 16 Mar 2026 16:42:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4NFC=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=AA=97=E4=BD=93=E6=A0=B9=E6=8D=AE=E5=8A=A8=E4=BD=9C=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=8C=87=E5=AE=9A=E5=AF=B9=E5=BA=94=E7=9A=84=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E8=BF=90=E8=A1=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- winboll/build.properties | 4 +- winboll/src/main/AndroidManifest.xml | 13 ++- .../termux/NfcTermuxBridgeActivity.java | 83 ++++++++++++++----- 3 files changed, 75 insertions(+), 25 deletions(-) diff --git a/winboll/build.properties b/winboll/build.properties index b8c14ac..f82b4aa 100644 --- a/winboll/build.properties +++ b/winboll/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Sun Mar 15 12:22:44 GMT 2026 +#Mon Mar 16 08:40:20 GMT 2026 stageCount=26 libraryProject= baseVersion=15.11 publishVersion=15.11.25 -buildCount=21 +buildCount=26 baseBetaVersion=15.11.26 diff --git a/winboll/src/main/AndroidManifest.xml b/winboll/src/main/AndroidManifest.xml index 85c8ebd..dae7eea 100644 --- a/winboll/src/main/AndroidManifest.xml +++ b/winboll/src/main/AndroidManifest.xml @@ -293,10 +293,17 @@ + android:exported="true"> + + - + + + + + + + diff --git a/winboll/src/main/java/cc/winboll/studio/winboll/termux/NfcTermuxBridgeActivity.java b/winboll/src/main/java/cc/winboll/studio/winboll/termux/NfcTermuxBridgeActivity.java index 7020525..49cc346 100644 --- a/winboll/src/main/java/cc/winboll/studio/winboll/termux/NfcTermuxBridgeActivity.java +++ b/winboll/src/main/java/cc/winboll/studio/winboll/termux/NfcTermuxBridgeActivity.java @@ -1,11 +1,11 @@ /* * 源码说明与描述: - * NFC 与 Termux 桥接活动,用于接收 NFC 传递的 JSON 指令并执行 Termux 脚本命令, - * 支持后台执行与终端窗口唤起两种模式,提供测试注入接口。 + * NFC 与 Termux 桥接活动,用于接收外部应用(包调用)传递的 JSON 指令并执行 Termux 脚本命令。 + * 支持 ACTION_BUILD(后台执行)与 ACTION_BUILD_VIEW(终端窗口唤起)两种动作。 * * 作者:豆包&ZhanGSKen * 创建时间:2025-03-15 14:00:00 - * 最后编辑时间:2026-03-15 15:22:00 + * 最后编辑时间:2026-03-16 10:00:00 */ package cc.winboll.studio.winboll.termux; @@ -23,7 +23,11 @@ public class NfcTermuxBridgeActivity extends Activity { // ========================= 常量与静态属性 ========================= public static final String TAG = "NfcTermuxBridgeActivity"; - public static final String ACTION_BUILD_VIEW = NfcTermuxBridgeActivity.class.getName() + ".ACTION_BUILD_VIEW"; + + // 外部应用调用时使用的 Action 常量 + public static final String ACTION_BUILD = "cc.winboll.studio.winboll.termux.NfcTermuxBridgeActivity.ACTION_BUILD"; + public static final String ACTION_BUILD_VIEW = "cc.winboll.studio.winboll.termux.NfcTermuxBridgeActivity.ACTION_BUILD_VIEW"; + private static final Gson GSON = new Gson(); // ========================= 生命周期方法 ========================= @@ -51,6 +55,10 @@ public class NfcTermuxBridgeActivity extends Activity { } // ========================= 统一 Intent 分发(合并去重) ========================= + /** + * 统一分发 Intent,根据 Action 路由到不同业务逻辑 + * @param intent 外部传入的 Intent + */ private void dispatchIntent(Intent intent) { LogUtils.d(TAG, "dispatchIntent() 分发 intent"); if (intent == null) { @@ -58,17 +66,22 @@ public class NfcTermuxBridgeActivity extends Activity { return; } - if (ACTION_BUILD_VIEW.equals(intent.getAction())) { + String action = intent.getAction(); + if (ACTION_BUILD_VIEW.equals(action)) { ToastUtils.show("ACTION_BUILD_VIEW 命中"); onOpenTermuxProjectBuildView(intent); - } else { + } else if (ACTION_BUILD.equals(action)) { + LogUtils.d(TAG, "ACTION_BUILD 命中"); handleNfcIntent(intent); + } else { + LogUtils.w(TAG, "dispatchIntent() 未知 Action: " + action); + finish(); } } // ========================= 核心业务方法 ========================= /** - * 处理 NFC 传递的 JSON 指令 + * 处理 ACTION_BUILD 动作:后台执行 NFC 指令 */ private void handleNfcIntent(Intent intent) { LogUtils.d(TAG, "handleNfcIntent() 调用"); @@ -83,6 +96,7 @@ public class NfcTermuxBridgeActivity extends Activity { if (json == null || json.isEmpty()) { LogUtils.e(TAG, "handleNfcIntent() 指令为空"); + showToast("指令为空"); finish(); return; } @@ -92,45 +106,53 @@ public class NfcTermuxBridgeActivity extends Activity { if (cmd.script == null || cmd.script.isEmpty()) { LogUtils.e(TAG, "handleNfcIntent() script 为空"); + showToast("script 不能为空"); finish(); return; } - String scriptPath = "/data/data/com.termux/files/home/TermuxWorkSpaces/BashShells/AutoNFC/" + cmd.script; + //String scriptPath = "/data/data/com.termux/files/home/TermuxWorkSpaces/BashShells/AutoNFC/" + cmd.script; + String scriptPath = "/data/data/com.termux/files/home/TermuxWorkSpaces/BashShells/AutoNFC/" + "BuildWinBoLLProject.sh"; LogUtils.d(TAG, "handleNfcIntent() 脚本路径: " + scriptPath); boolean success = TermuxCommandExecutor.executeCommand( - this, scriptPath, cmd.args, cmd.workDir, cmd.background, cmd.resultDir + this, scriptPath, cmd.args, cmd.workDir, cmd.background, cmd.resultDir ); LogUtils.d(TAG, "handleNfcIntent() 执行结果: " + success); if (success) { - Toast.makeText(this, "指令已发送: " + cmd.script, Toast.LENGTH_SHORT).show(); + showToast("指令已发送: " + cmd.script); LogUtils.i(TAG, "执行成功: " + scriptPath); } else { - Toast.makeText(this, "指令发送失败", Toast.LENGTH_SHORT).show(); + showToast("指令发送失败"); LogUtils.e(TAG, "执行失败"); } } catch (Exception e) { LogUtils.e(TAG, "handleNfcIntent() 异常: " + e.getMessage(), e); - Toast.makeText(this, "解析失败", Toast.LENGTH_SHORT).show(); + showToast("解析失败"); } finally { finish(); } } /** - * 唤起 Termux 窗口执行命令(实时输出版) + * 处理 ACTION_BUILD_VIEW 动作:唤起 Termux 窗口执行命令 */ public void onOpenTermuxProjectBuildView(Intent intent) { LogUtils.d(TAG, "onOpenTermuxProjectBuildView() 调用"); + if (intent == null) { + LogUtils.w(TAG, "onOpenTermuxProjectBuildView() intent 为空"); + return; + } + try { String json = intent.getStringExtra(Intent.EXTRA_TEXT); LogUtils.d(TAG, "onOpenTermuxProjectBuildView() json: " + json); if (json == null || json.isEmpty()) { LogUtils.e(TAG, "onOpenTermuxProjectBuildView() 指令为空"); + showToast("指令为空"); finish(); return; } @@ -140,45 +162,50 @@ public class NfcTermuxBridgeActivity extends Activity { if (cmd.script == null || cmd.script.isEmpty()) { LogUtils.e(TAG, "onOpenTermuxProjectBuildView() script 为空"); + showToast("script 不能为空"); finish(); return; } StringBuilder targetCmd = new StringBuilder(); - String nfcScriptFolder = "/data/data/com.termux/files/home/TermuxWorkSpaces/BashShells/AutoNFC/"; + String nfcScriptFolder = "/data/data/com.termux/files/home/TermuxWorkSpaces/BashShells/AutoNFC/"; targetCmd.append("cd " + nfcScriptFolder + " && "); - targetCmd.append("stdbuf -o0 -e0 -i0 bash ").append(cmd.script).append(" "); + //targetCmd.append("stdbuf -o0 -e0 -i0 bash ").append(cmd.script).append(" "); + targetCmd.append("stdbuf -o0 -e0 -i0 bash ").append("BuildWinBoLLProjectView.sh").append(" "); if (cmd.args != null) { for (String arg : cmd.args) { targetCmd.append(arg).append(" "); } } - //targetCmd.append(" && echo '\n✅ 执行完成!' && echo '\n📌 项目: ").append(cmd.args != null && cmd.args.length > 0 ? cmd.args[0] : "").append("' && read -p '按回车关闭...'"); LogUtils.d(TAG, "onOpenTermuxProjectBuildView() 命令: " + targetCmd); boolean cmdSuccess = TermuxCommandExecutor.executeTerminalCommand(this, targetCmd.toString()); LogUtils.d(TAG, "onOpenTermuxProjectBuildView() 执行结果: " + cmdSuccess); if (cmdSuccess) { - Toast.makeText(this, "指令已发送: " + cmd.script + " " + (cmd.args != null && cmd.args.length > 0 ? cmd.args[0] : ""), Toast.LENGTH_SHORT).show(); + showToast("指令已发送: " + cmd.script); } else { - Toast.makeText(this, "指令发送失败", Toast.LENGTH_SHORT).show(); + showToast("指令发送失败"); } } catch (Exception e) { LogUtils.e(TAG, "onOpenTermuxProjectBuildView() 异常: " + e.getMessage(), e); - Toast.makeText(this, "解析失败", Toast.LENGTH_SHORT).show(); + showToast("解析失败"); } finally { finish(); } } // ========================= 公共静态测试方法 ========================= + /** + * 内部测试方法:发送 ACTION_BUILD 指令 + */ public static void testCommand(Context context) { LogUtils.d(TAG, "testCommand()"); try { String testJson = "{\"script\":\"BuildWinBoLLProject.sh\",\"args\":[\"DebugTemp\"],\"workDir\":null,\"background\":true,\"resultDir\":null}"; Intent intent = new Intent(context, NfcTermuxBridgeActivity.class); + intent.setAction(ACTION_BUILD); // 指定 Action intent.putExtra(Intent.EXTRA_TEXT, testJson); context.startActivity(intent); } catch (Exception e) { @@ -186,17 +213,33 @@ public class NfcTermuxBridgeActivity extends Activity { } } + /** + * 内部测试方法:发送 ACTION_BUILD_VIEW 指令 + */ public static void testViewCommand(Context context) { LogUtils.d(TAG, "testViewCommand()"); try { String testJson = "{\"script\":\"BuildWinBoLLProjectView.sh\",\"args\":[\"DebugTemp\"],\"workDir\":null,\"background\":true,\"resultDir\":null}"; Intent intent = new Intent(context, NfcTermuxBridgeActivity.class); - intent.setAction(ACTION_BUILD_VIEW); + intent.setAction(ACTION_BUILD_VIEW); // 指定 Action intent.putExtra(Intent.EXTRA_TEXT, testJson); context.startActivity(intent); } catch (Exception e) { LogUtils.e(TAG, "testViewCommand() 失败: " + e.getMessage()); } } + + // ========================= 工具方法 ========================= + /** + * 统一显示 Toast,确保在主线程调用 + */ + private void showToast(final String message) { + runOnUiThread(new Runnable() { + @Override + public void run() { + Toast.makeText(NfcTermuxBridgeActivity.this, message, Toast.LENGTH_SHORT).show(); + } + }); + } }