diff --git a/native/cocos/platform/android/AndroidPlatform.cpp b/native/cocos/platform/android/AndroidPlatform.cpp index 303c2cfffb..43aa13cf11 100644 --- a/native/cocos/platform/android/AndroidPlatform.cpp +++ b/native/cocos/platform/android/AndroidPlatform.cpp @@ -376,7 +376,7 @@ public: } case APP_CMD_DESTROY: { CC_LOG_INFO("AndroidPlatform: APP_CMD_DESTROY"); - IXRInterface *xr = _androidPlatform->getInterface(); + IXRInterface *xr = CC_GET_XR_INTERFACE(); if (xr) { xr->onRenderDestroy(); } @@ -532,6 +532,11 @@ int AndroidPlatform::init() { cc::FileUtilsAndroid::setAssetManager(_app->activity->assetManager); _inputProxy = ccnew GameInputProxy(this); _inputProxy->registerAppEventCallback([this](int32_t cmd) { + IXRInterface *xr = CC_GET_XR_INTERFACE(); + if (xr) { + xr->handleAppCommand(cmd); + } + if (APP_CMD_START == cmd || APP_CMD_INIT_WINDOW == cmd) { if (_inputProxy->isAnimating()) { _isLowFrequencyLoopEnabled = false; @@ -541,7 +546,6 @@ int AndroidPlatform::init() { _lowFrequencyTimer.reset(); _loopTimeOut = LOW_FREQUENCY_TIME_INTERVAL; _isLowFrequencyLoopEnabled = true; - IXRInterface *xr = getInterface(); if (xr && !xr->getXRConfig(xr::XRConfigKey::INSTANCE_CREATED).getBool()) { // xr will sleep, -1 we will block forever waiting for events. _loopTimeOut = -1; @@ -584,7 +588,7 @@ int32_t AndroidPlatform::run(int /*argc*/, const char ** /*argv*/) { } int32_t AndroidPlatform::loop() { - IXRInterface *xr = getInterface(); + IXRInterface *xr = CC_GET_XR_INTERFACE(); while (true) { int events; struct android_poll_source *source; diff --git a/native/cocos/platform/android/libcocosxr/src/com/cocos/lib/xr/CocosXRApi.java b/native/cocos/platform/android/libcocosxr/src/com/cocos/lib/xr/CocosXRApi.java index d816dc913b..cef6d5cc41 100644 --- a/native/cocos/platform/android/libcocosxr/src/com/cocos/lib/xr/CocosXRApi.java +++ b/native/cocos/platform/android/libcocosxr/src/com/cocos/lib/xr/CocosXRApi.java @@ -162,6 +162,6 @@ public class CocosXRApi { } // native - //private native void onActivityLifecycleCallback(int id, String activityClassName); - //private native void onAdbCmd(String key, String value); + private native void onActivityLifecycleCallback(int id, String activityClassName); + private native void onAdbCmd(String key, String value); } diff --git a/native/cocos/platform/android/libcocosxr/src/com/cocos/lib/xr/CocosXRVideoPlayer.java b/native/cocos/platform/android/libcocosxr/src/com/cocos/lib/xr/CocosXRVideoPlayer.java index d138bf82ba..e2b3122eb3 100644 --- a/native/cocos/platform/android/libcocosxr/src/com/cocos/lib/xr/CocosXRVideoPlayer.java +++ b/native/cocos/platform/android/libcocosxr/src/com/cocos/lib/xr/CocosXRVideoPlayer.java @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2018-2022 Xiamen Yaji Software Co., Ltd. + * Copyright (c) 2018-2023 Xiamen Yaji Software Co., Ltd. * * http://www.cocos.com * diff --git a/native/cocos/platform/android/libcocosxr/src/com/cocos/lib/xr/CocosXRVideoTexture.java b/native/cocos/platform/android/libcocosxr/src/com/cocos/lib/xr/CocosXRVideoTexture.java index 7fbe3a4b56..c8b53fa4ce 100644 --- a/native/cocos/platform/android/libcocosxr/src/com/cocos/lib/xr/CocosXRVideoTexture.java +++ b/native/cocos/platform/android/libcocosxr/src/com/cocos/lib/xr/CocosXRVideoTexture.java @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2018-2022 Xiamen Yaji Software Co., Ltd. + * Copyright (c) 2018-2023 Xiamen Yaji Software Co., Ltd. * * http://www.cocos.com * diff --git a/native/cocos/platform/interfaces/modules/IXRInterface.h b/native/cocos/platform/interfaces/modules/IXRInterface.h index 55bbdb4e05..5abf603ccf 100644 --- a/native/cocos/platform/interfaces/modules/IXRInterface.h +++ b/native/cocos/platform/interfaces/modules/IXRInterface.h @@ -316,5 +316,12 @@ public: * @param eye */ virtual void bindXREyeWithRenderWindow(void *window, xr::XREye eye) = 0; + + /** + * @en app's lifecycle callback + * @zh 应用程序生命周期回调 + * @param appCmd + */ + virtual void handleAppCommand(int appCmd) = 0; }; } // namespace cc diff --git a/native/cocos/platform/java/modules/XRInterface.cpp b/native/cocos/platform/java/modules/XRInterface.cpp index 54a3a7c10b..d803c1aa22 100644 --- a/native/cocos/platform/java/modules/XRInterface.cpp +++ b/native/cocos/platform/java/modules/XRInterface.cpp @@ -1009,4 +1009,32 @@ void XRInterface::loadAssetsImage(const std::string &imageInfo) { }); } +void XRInterface::handleAppCommand(int appCmd) { +#if CC_USE_XR + setXRConfig(xr::XRConfigKey::APP_COMMAND, appCmd); +#else + CC_UNUSED_PARAM(appCmd); +#endif +} +#if CC_USE_XR + +extern "C" JNIEXPORT void JNICALL Java_com_cocos_lib_xr_CocosXRApi_onAdbCmd(JNIEnv * /*env*/, jobject /*thiz*/, jstring key, jstring value) { + auto cmdKey = cc::JniHelper::jstring2string(key); + auto cmdValue = cc::JniHelper::jstring2string(value); + if (IS_ENABLE_XR_LOG) { + CC_LOG_INFO("CocosXRApi_onAdbCmd_%s_%s", cmdKey.c_str(), cmdValue.c_str()); + } + cc::xr::XrEntry::getInstance()->setXRConfig(cc::xr::XRConfigKey::ADB_COMMAND, cmdKey.append(":").append(cmdValue)); +} + +extern "C" JNIEXPORT void JNICALL Java_com_cocos_lib_xr_CocosXRApi_onActivityLifecycleCallback(JNIEnv * /*env*/, jobject /*thiz*/, jint type, jstring activityClassName) { + auto name = cc::JniHelper::jstring2string(activityClassName); + if (IS_ENABLE_XR_LOG) { + CC_LOG_INFO("CocosXRApi_onActivityLifecycleCallback_%d_%s", type, name.c_str()); + } + cc::xr::XrEntry::getInstance()->setXRConfig(cc::xr::XRConfigKey::ACTIVITY_LIFECYCLE, type); + cc::xr::XrEntry::getInstance()->setXRConfig(cc::xr::XRConfigKey::ACTIVITY_LIFECYCLE, name); +} + +#endif } // namespace cc diff --git a/native/cocos/platform/java/modules/XRInterface.h b/native/cocos/platform/java/modules/XRInterface.h index 8fa3969ec8..9499e0c6d0 100644 --- a/native/cocos/platform/java/modules/XRInterface.h +++ b/native/cocos/platform/java/modules/XRInterface.h @@ -98,7 +98,7 @@ public: // renderwindow xr::XREye getXREyeByRenderWindow(void *window) override; void bindXREyeWithRenderWindow(void *window, xr::XREye eye) override; - + void handleAppCommand(int appCmd) override; private: void loadAssetsImage(const std::string &imageInfo); void dispatchGamepadEventInternal(const xr::XRControllerEvent &xrControllerEvent);