From 0b6c6e78e6890ac2c12f6e2f3092a38b52440904 Mon Sep 17 00:00:00 2001 From: mtvpls Date: Fri, 19 Jun 2026 16:15:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B1=80=E5=9F=9F=E7=BD=91?= =?UTF-8?q?=E9=81=A5=E6=8E=A7=E6=8C=89=E9=94=AE=E6=BA=A2=E5=87=BA=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E8=8F=9C=E5=8D=95=E9=94=AE=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/moontvplus/tv/MainActivity.java | 62 ++++++------------- .../com/moontvplus/tv/LocalRemoteServer.java | 2 +- .../java/com/moontvplus/tv/MainActivity.java | 29 ++++----- src/components/tv/TVRemoteReceiver.tsx | 10 +++ 4 files changed, 40 insertions(+), 63 deletions(-) diff --git a/apps/android-tv/app/src/gecko/java/com/moontvplus/tv/MainActivity.java b/apps/android-tv/app/src/gecko/java/com/moontvplus/tv/MainActivity.java index cd31782..491733f 100644 --- a/apps/android-tv/app/src/gecko/java/com/moontvplus/tv/MainActivity.java +++ b/apps/android-tv/app/src/gecko/java/com/moontvplus/tv/MainActivity.java @@ -4,7 +4,6 @@ import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Looper; -import android.view.KeyCharacterMap; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; @@ -12,6 +11,7 @@ import android.view.Window; import android.view.WindowManager; import android.widget.FrameLayout; +import org.json.JSONObject; import org.mozilla.geckoview.GeckoRuntime; import org.mozilla.geckoview.GeckoSession; import org.mozilla.geckoview.GeckoView; @@ -93,57 +93,31 @@ public class MainActivity extends Activity implements RemoteCommandHandler { @Override public void onRemoteKey(String key, boolean repeat, String digit) { - int keyCode = keyCodeForRemoteKey(key, digit); - if (keyCode == KeyEvent.KEYCODE_UNKNOWN) return; - mainHandler.post(() -> { - if (keyCode == KeyEvent.KEYCODE_BACK) { - onBackPressed(); - return; - } - dispatchKeyCode(keyCode, repeat); - }); + mainHandler.post(() -> dispatchLocalRemoteKey(key, repeat, digit)); } @Override public void onRemoteText(String mode, String text) { - mainHandler.post(() -> { - String safeMode = mode == null ? "replace" : mode; - if ("clear".equals(safeMode) || "replace".equals(safeMode)) { - dispatchCtrlA(); - dispatchKeyCode(KeyEvent.KEYCODE_DEL, false); - } else if ("backspace".equals(safeMode)) { - dispatchKeyCode(KeyEvent.KEYCODE_DEL, false); - return; - } - - if (!"clear".equals(safeMode) && text != null && !text.isEmpty()) { - dispatchText(text); - } - }); + mainHandler.post(() -> dispatchLocalRemoteText(mode, text)); } - private void dispatchKeyCode(int keyCode, boolean repeat) { - long now = System.currentTimeMillis(); - geckoView.dispatchKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, repeat ? 1 : 0)); - geckoView.dispatchKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_UP, keyCode, 0)); + private void dispatchLocalRemoteKey(String key, boolean repeat, String digit) { + if (session == null || key == null) return; + String script = "javascript:window.dispatchEvent(new CustomEvent('moontv:local-remote-key',{detail:{key:" + + JSONObject.quote(key) + + ",repeat:" + (repeat ? "true" : "false") + + ",digit:" + JSONObject.quote(digit == null ? "" : digit) + + "}}));void(0)"; + session.loadUri(script); } - private void dispatchCtrlA() { - long now = System.currentTimeMillis(); - geckoView.dispatchKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A, 0, KeyEvent.META_CTRL_ON)); - geckoView.dispatchKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_A, 0, KeyEvent.META_CTRL_ON)); - } - - private void dispatchText(String text) { - KeyEvent[] events = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD).getEvents(text.toCharArray()); - if (events != null) { - for (KeyEvent event : events) { - geckoView.dispatchKeyEvent(event); - } - return; - } - - geckoView.dispatchKeyEvent(new KeyEvent(System.currentTimeMillis(), text, KeyCharacterMap.VIRTUAL_KEYBOARD, 0)); + private void dispatchLocalRemoteText(String mode, String text) { + if (session == null) return; + String script = "javascript:window.dispatchEvent(new CustomEvent('moontv:local-remote-text',{detail:{mode:" + + JSONObject.quote(mode == null ? "replace" : mode) + + ",text:" + JSONObject.quote(text == null ? "" : text) + + "}}));void(0)"; + session.loadUri(script); } private String withLocalRemoteHash(String url) { diff --git a/apps/android-tv/app/src/main/java/com/moontvplus/tv/LocalRemoteServer.java b/apps/android-tv/app/src/main/java/com/moontvplus/tv/LocalRemoteServer.java index 1158a2b..cd347a5 100644 --- a/apps/android-tv/app/src/main/java/com/moontvplus/tv/LocalRemoteServer.java +++ b/apps/android-tv/app/src/main/java/com/moontvplus/tv/LocalRemoteServer.java @@ -241,7 +241,7 @@ public class LocalRemoteServer { private String remoteHtml() { return "" + "MoonTVPlus 局域网遥控器
LAN REMOTE

电视遥控器

手机与电视在同一局域网时,可直接控制 MoonTVPlus TV。

" + "
文本输入
正在连接电视...