From da59acb74b11d7a6f4f08ad5fd0c4af1cc0947ab Mon Sep 17 00:00:00 2001 From: "Randall E. Barker" Date: Tue, 24 Sep 2019 16:43:53 -0700 Subject: [PATCH] Add support for Bluetooth keyboards. Fixes #775 --- .../mozilla/vrbrowser/VRBrowserActivity.java | 10 ++++- .../vrbrowser/ui/widgets/KeyboardWidget.java | 40 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java index 31112f635e..33809b8934 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java @@ -608,12 +608,15 @@ public void onBackPressed() { @Override public boolean dispatchKeyEvent(KeyEvent event) { + if (mKeyboard.dispatchKeyEvent(event)) { + return true; + } + final int keyCode = event.getKeyCode(); if (DeviceType.isOculusBuild()) { int action = event.getAction(); if (action != KeyEvent.ACTION_DOWN) { return super.dispatchKeyEvent(event); } - int keyCode = event.getKeyCode(); boolean result; switch (keyCode) { case KeyEvent.KEYCODE_VOLUME_UP: @@ -622,6 +625,9 @@ public boolean dispatchKeyEvent(KeyEvent event) { case KeyEvent.KEYCODE_VOLUME_DOWN: result = callOnAudioManager((AudioManager aManager) -> aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI)); break; + case KeyEvent.KEYCODE_VOLUME_MUTE: + result = callOnAudioManager((AudioManager aManager) -> aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, AudioManager.FLAG_SHOW_UI)); + break; default: return super.dispatchKeyEvent(event); } @@ -629,7 +635,7 @@ public boolean dispatchKeyEvent(KeyEvent event) { } else if (DeviceType.isGoogleVR()) { boolean result; - switch( event.getKeyCode() ) { + switch (keyCode) { case KeyEvent.KEYCODE_VOLUME_UP: case KeyEvent.KEYCODE_VOLUME_DOWN: result = true; diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java index 9f53b92823..56a7cbd03d 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/KeyboardWidget.java @@ -15,6 +15,8 @@ import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; +import android.view.KeyCharacterMap; +import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; @@ -308,6 +310,10 @@ private void resetKeyboardLayout() { updateCandidates(); } + private boolean isAttachToWindowWidget() { + return mFocusedView instanceof WindowWidget; + } + public void updateFocusedView(View aFocusedView) { if (mFocusedView != null && mFocusedView instanceof TextView) { ((TextView)mFocusedView).removeTextChangedListener(this); @@ -905,6 +911,40 @@ private void displayComposingText(String aText, ComposingAction aAction) { } } + @Override + public boolean dispatchKeyEvent(final KeyEvent event) { + final int keyCode = event.getKeyCode(); + final InputConnection connection = mInputConnection; + if (connection != null) { + if (isAttachToWindowWidget()) { + connection.sendKeyEvent(event); + hide(UIWidget.KEEP_WIDGET); + return true; + } + // Android Components do not support InputConnection.sendKeyEvent() + if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode > 0)) { + switch (keyCode) { + case KeyEvent.KEYCODE_DEL: + handleBackspace(event.isLongPress()); + return true; + case KeyEvent.KEYCODE_ENTER: + case KeyEvent.KEYCODE_NUMPAD_ENTER: + handleDone(); + return true; + default: + break; + } + if (event.getUnicodeChar() != 0) { + KeyCharacterMap map = event.getKeyCharacterMap(); + String value = String.valueOf((char) map.get(keyCode, event.getMetaState())); + connection.commitText(value, 1); + return true; + } + } + } + return false; + } + // GeckoSession.TextInputDelegate @Override