Skip to content

Commit 2819d7b

Browse files
fix: fix quest navigation
1 parent 37e490c commit 2819d7b

4 files changed

Lines changed: 58 additions & 4 deletions

File tree

โ€Žandroid/sharedCode/src/main/cpp/VROInputControllerOpenXR.cppโ€Ž

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,12 @@ void VROInputControllerOpenXR::onProcess(XrSession session, XrSpace baseSpace,
376376
xrGetActionStateBoolean(session, &info, &state);
377377
if (state.isActive) {
378378
bool pressed = (state.currentState == XR_TRUE);
379-
if (pressed && !_prevBButton)
379+
if (pressed && !_prevBButton) {
380380
VROInputControllerBase::onButtonEvent(ViroOculus::BackButton, VROEventDelegate::ClickState::ClickDown);
381-
else if (!pressed && _prevBButton)
381+
if (_backButtonCallback) _backButtonCallback();
382+
} else if (!pressed && _prevBButton) {
382383
VROInputControllerBase::onButtonEvent(ViroOculus::BackButton, VROEventDelegate::ClickState::ClickUp);
384+
}
383385
_prevBButton = pressed;
384386
}
385387
}
@@ -424,10 +426,12 @@ void VROInputControllerOpenXR::onProcess(XrSession session, XrSpace baseSpace,
424426
xrGetActionStateBoolean(session, &info, &state);
425427
if (state.isActive) {
426428
bool pressed = (state.currentState == XR_TRUE);
427-
if (pressed && !_prevMenuButton)
429+
if (pressed && !_prevMenuButton) {
428430
VROInputControllerBase::onButtonEvent(ViroOculus::BackButton, VROEventDelegate::ClickState::ClickDown);
429-
else if (!pressed && _prevMenuButton)
431+
if (_backButtonCallback) _backButtonCallback();
432+
} else if (!pressed && _prevMenuButton) {
430433
VROInputControllerBase::onButtonEvent(ViroOculus::BackButton, VROEventDelegate::ClickState::ClickUp);
434+
}
431435
_prevMenuButton = pressed;
432436
}
433437
}

โ€Žandroid/sharedCode/src/main/cpp/VROInputControllerOpenXR.hโ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#ifndef ANDROID_VROINPUTCONTROLLEROPENXR_H
2828
#define ANDROID_VROINPUTCONTROLLEROPENXR_H
2929

30+
#include <functional>
3031
#include <memory>
3132
#include <openxr/openxr.h>
3233
#include "VROInputControllerBase.h"
@@ -89,6 +90,15 @@ class VROInputControllerOpenXR : public VROInputControllerBase {
8990
std::string getHeadset() override { return "quest"; }
9091
std::string getController() override { return "touch"; }
9192

93+
/*
94+
* Set a callback invoked on the render thread when the B/Menu button is
95+
* pressed (ClickDown). Used to dispatch KEYCODE_BACK to the host Activity
96+
* so React Native's BackHandler fires in VRActivity.
97+
*/
98+
void setBackButtonCallback(std::function<void()> callback) {
99+
_backButtonCallback = std::move(callback);
100+
}
101+
92102
protected:
93103
std::shared_ptr<VROInputPresenter> createPresenter(
94104
std::shared_ptr<VRODriver> driver) override;
@@ -128,6 +138,9 @@ class VROInputControllerOpenXR : public VROInputControllerBase {
128138
XrSpace _leftSpace = XR_NULL_HANDLE;
129139
XrSpace _rightSpace = XR_NULL_HANDLE;
130140

141+
// โ”€โ”€ Back button callback โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
142+
std::function<void()> _backButtonCallback;
143+
131144
// โ”€โ”€ Edge-detection state (previous frame) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
132145
bool _prevTriggerLeft = false;
133146
bool _prevTriggerRight = false;

โ€Žandroid/sharedCode/src/main/cpp/VROSceneRendererOpenXR.cppโ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,29 @@ VROSceneRendererOpenXR::VROSceneRendererOpenXR(VRORendererConfiguration config,
140140
_inputController->createActionSet(_instance, _session);
141141
initHandTracking(); // no-op if XR_EXT_hand_tracking not available on this device
142142

143+
// Wire the B/Menu button back to Android's back-press so React Native's
144+
// BackHandler fires in VRActivity. The callback runs on the render thread;
145+
// ViroViewOpenXR.onNativeBackButton() posts to the UI thread internally.
146+
{
147+
JavaVM *jvm = _jvm;
148+
jobject jview = _jview; // global ref โ€” safe to capture
149+
_inputController->setBackButtonCallback([jvm, jview]() {
150+
JNIEnv *env = nullptr;
151+
bool attached = false;
152+
if (jvm->GetEnv((void **)&env, JNI_VERSION_1_6) == JNI_EDETACHED) {
153+
jvm->AttachCurrentThread(&env, nullptr);
154+
attached = true;
155+
}
156+
if (env) {
157+
jclass cls = env->GetObjectClass(jview);
158+
jmethodID mid = env->GetMethodID(cls, "onNativeBackButton", "()V");
159+
env->DeleteLocalRef(cls);
160+
if (mid) env->CallVoidMethod(jview, mid);
161+
}
162+
if (attached) jvm->DetachCurrentThread();
163+
});
164+
}
165+
143166
// Create the shared VRORenderer and wire it to the input controller.
144167
// VROSceneRenderer::_renderer is null until explicitly set here โ€” every other
145168
// platform (GVR, OVR) does the equivalent in their constructor.

โ€Žandroid/sharedCode/src/main/java/com/viro/core/ViroViewOpenXR.javaโ€Ž

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,20 @@ public boolean onTouchEvent(MotionEvent event) {
603603

604604
// โ”€โ”€ Callbacks from native (VROSceneRendererOpenXR) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
605605

606+
/**
607+
* Called by native code (render thread) when the B or Menu controller button
608+
* is pressed. Posts Activity.onBackPressed() to the UI thread so React Native's
609+
* BackHandler receives the event in VRActivity.
610+
*
611+
* @hide
612+
*/
613+
void onNativeBackButton() {
614+
Activity activity = mWeakActivity.get();
615+
if (activity != null) {
616+
activity.runOnUiThread(activity::onBackPressed);
617+
}
618+
}
619+
606620
/**
607621
* Called by native code each frame on the render thread so that queued
608622
* Java callbacks (frame listeners) are executed on the correct thread.

0 commit comments

Comments
ย (0)