From 64669782c2c9d4c8b889b321c487823363b5a27c Mon Sep 17 00:00:00 2001 From: Sergey Zavyalov Date: Wed, 6 Aug 2025 12:31:41 +0300 Subject: [PATCH] ArkTS 1.2 Signed-off-by: Sergey Zavyalov --- .../window_stage_ani/include/ani_window.h | 4 + .../include/ani_window_stage.h | 4 + .../window_stage_ani/src/ani_window.cpp | 9 +- previewer/BUILD.gn | 24 + previewer/include/window.h | 14 + previewer/include/window_impl.h | 4 + previewer/mock/ani_err_utils.h | 34 + previewer/mock/ani_window_listener.cpp | 49 + previewer/mock/ani_window_listener.h | 88 ++ .../mock/ani_window_register_manager.cpp | 429 +++++++ previewer/mock/ani_window_register_manager.h | 117 ++ previewer/mock/ani_window_utils.cpp | 1028 +++++++++++++++++ previewer/mock/ani_window_utils.h | 355 ++++++ previewer/mock/pixel_map_ani.cpp | 38 + previewer/mock/pixel_map_ani.h | 37 + .../window_manager_ani/ani_window_manager.cpp | 223 ++++ .../window_manager_ani/ani_window_manager.h | 57 + previewer/src/window_impl.cpp | 86 ++ 18 files changed, 2599 insertions(+), 1 deletion(-) create mode 100644 previewer/mock/ani_err_utils.h create mode 100644 previewer/mock/ani_window_listener.cpp create mode 100644 previewer/mock/ani_window_listener.h create mode 100644 previewer/mock/ani_window_register_manager.cpp create mode 100644 previewer/mock/ani_window_register_manager.h create mode 100644 previewer/mock/ani_window_utils.cpp create mode 100644 previewer/mock/ani_window_utils.h create mode 100644 previewer/mock/pixel_map_ani.cpp create mode 100644 previewer/mock/pixel_map_ani.h create mode 100644 previewer/mock/window_manager_ani/ani_window_manager.cpp create mode 100644 previewer/mock/window_manager_ani/ani_window_manager.h diff --git a/interfaces/kits/ani/window_runtime/window_stage_ani/include/ani_window.h b/interfaces/kits/ani/window_runtime/window_stage_ani/include/ani_window.h index 2dbb0d3ba1..a867cfc95a 100644 --- a/interfaces/kits/ani/window_runtime/window_stage_ani/include/ani_window.h +++ b/interfaces/kits/ani/window_runtime/window_stage_ani/include/ani_window.h @@ -17,7 +17,11 @@ #define OHOS_ANI_WINDOW_H #include "ani.h" +#if defined(LINUX_PREVIEW) +#include "mock/ani_window_register_manager.h" +#else #include "ani_window_register_manager.h" +#endif // LINUX_PREVIEW #include "window.h" namespace OHOS { diff --git a/interfaces/kits/ani/window_runtime/window_stage_ani/include/ani_window_stage.h b/interfaces/kits/ani/window_runtime/window_stage_ani/include/ani_window_stage.h index 6edf8f9cba..49e9364acd 100644 --- a/interfaces/kits/ani/window_runtime/window_stage_ani/include/ani_window_stage.h +++ b/interfaces/kits/ani/window_runtime/window_stage_ani/include/ani_window_stage.h @@ -17,7 +17,11 @@ #define OHOS_ANI_WINDOW_STAGE_H #include "ani.h" +#ifdef LINUX_PREVIEW +#include "mock/ani_window_register_manager.h" +#else #include "ani_window_register_manager.h" +#endif #include "window_scene.h" namespace OHOS { diff --git a/interfaces/kits/ani/window_runtime/window_stage_ani/src/ani_window.cpp b/interfaces/kits/ani/window_runtime/window_stage_ani/src/ani_window.cpp index f25c9bad0d..045218f44a 100644 --- a/interfaces/kits/ani/window_runtime/window_stage_ani/src/ani_window.cpp +++ b/interfaces/kits/ani/window_runtime/window_stage_ani/src/ani_window.cpp @@ -24,8 +24,10 @@ #include "ani_err_utils.h" #include "ani_window_utils.h" #include "permission.h" +#ifndef LINUX_PREVIEW #include "pixel_map.h" #include "pixel_map_taihe_ani.h" +#endif //LINUX_PREVIEW #include "window_helper.h" #include "window_scene.h" #include "window_manager.h" @@ -2028,6 +2030,7 @@ ani_ref FindAniWindowObject(const std::string& windowName) ani_object AniWindow::Snapshot(ani_env* env) { +#ifndef LINUX_PREVIEW if (windowToken_ == nullptr) { return AniWindowUtils::AniThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY); } @@ -2043,6 +2046,10 @@ ani_object AniWindow::Snapshot(ani_env* env) windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), pixelMap->GetWidth(), pixelMap->GetHeight()); return nativePixelMap; +#else + TLOGE(WmsLogTag::WMS_IMMS, "AniWindow::Snapshot not implemented"); + return nullptr; +#endif } void AniWindow::HideNonSystemFloatingWindows(ani_env* env, ani_boolean shouldHide) @@ -2777,4 +2784,4 @@ ani_status OHOS::Rosen::ANI_Window_Constructor(ani_vm *vm, uint32_t *result) TLOGE(WmsLogTag::DEFAULT, "[ANI] bind ns window func %{public}u", ret); } return ANI_OK; -} \ No newline at end of file +} diff --git a/previewer/BUILD.gn b/previewer/BUILD.gn index 336f4f6caf..c7204fb609 100644 --- a/previewer/BUILD.gn +++ b/previewer/BUILD.gn @@ -72,10 +72,12 @@ if (!ispreview) { ] include_dirs = [ + ".", "include", "${arkui_path}/ace_engine/adapter/preview/external/multimodalinput", "${arkui_path}/ace_engine/", "mock", + "../interfaces/kits/ani/window_runtime/window_stage_ani/include", "../utils/include", "${ide_previewer_external_path}", "${window_base_path}/interfaces/kits/napi/window_animation", @@ -102,9 +104,15 @@ if (!ispreview) { visibility = [ ":*" ] include_dirs = [ + ".", + ] + include_dirs += [ "../interfaces/kits/napi/window_runtime/window_napi", "../interfaces/kits/napi/window_runtime/window_stage_napi", ] + include_dirs += [ + "../interfaces/kits/ani/window_runtime/window_stage_ani/include", + ] } ohos_shared_library("previewer_window_napi") { @@ -158,6 +166,22 @@ if (!ispreview) { cflags_cc = [ "-DWINDOW_PREVIEW" ] cflags = [ "-std=c++11" ] + if (is_linux) { + external_deps += [ + "runtime_core:ani", + ] + sources += [ + "../interfaces/kits/ani/window_runtime/window_stage_ani/src/ani_window.cpp", + "../interfaces/kits/ani/window_runtime/window_stage_ani/src/ani_window_stage.cpp", + "mock/ani_window_listener.cpp", + "mock/ani_window_register_manager.cpp", + "mock/ani_window_utils.cpp", + "mock/pixel_map_ani.cpp", + "mock/window_manager_ani/ani_window_manager.cpp", + ] + cflags_cc += [ "-DLINUX_PREVIEW" ] + } + part_name = "window_manager" subsystem_name = "window" } diff --git a/previewer/include/window.h b/previewer/include/window.h index acc37486b1..7a5158b969 100644 --- a/previewer/include/window.h +++ b/previewer/include/window.h @@ -26,6 +26,12 @@ typedef struct napi_env__* napi_env; typedef struct napi_value__* napi_value; +typedef class __ani_object* ani_object; +#ifdef __cplusplus +typedef struct __ani_env ani_env; +#else +typedef const struct __ani_interaction_api *ani_env; +#endif namespace OHOS::MMI { class PointerEvent; @@ -344,6 +350,9 @@ public: virtual WMError NapiSetUIContent(const std::string& contentInfo, napi_env env, napi_value storage, BackupAndRestoreType type = BackupAndRestoreType::NONE, sptr token = nullptr, AppExecFwk::Ability* ability = nullptr) = 0; + virtual WMError NapiSetUIContent(const std::string& contentInfo, ani_env* env, ani_object storage, + BackupAndRestoreType type = BackupAndRestoreType::NONE, sptr token = nullptr, + AppExecFwk::Ability* ability = nullptr) = 0; virtual WMError NapiSetUIContentByName(const std::string& contentName, napi_env env, napi_value storage, BackupAndRestoreType type = BackupAndRestoreType::NONE, sptr token = nullptr, AppExecFwk::Ability* ability = nullptr) { return WMError::WM_OK; } @@ -352,6 +361,11 @@ public: { return WMError::WM_OK; } + virtual WMError SetUIContentByName(const std::string& contentInfo, ani_env* env, ani_object storage, + AppExecFwk::Ability* ability = nullptr) + { + return WMError::WM_OK; + } virtual WMError SetUIContentByAbc(const std::string& abcPath, napi_env env, napi_value storage, AppExecFwk::Ability* ability = nullptr) { diff --git a/previewer/include/window_impl.h b/previewer/include/window_impl.h index bc13b567cd..01547d64e5 100644 --- a/previewer/include/window_impl.h +++ b/previewer/include/window_impl.h @@ -186,8 +186,12 @@ public: void NotifyTouchDialogTarget(int32_t posX = 0, int32_t posY = 0) override; WMError SetUIContentByName(const std::string& contentInfo, napi_env env, napi_value storage, AppExecFwk::Ability* ability) override; + WMError SetUIContentByName(const std::string& contentInfo, ani_env* env, ani_object storage, + AppExecFwk::Ability* ability) override; virtual WMError NapiSetUIContent(const std::string& contentInfo, napi_env env, napi_value storage, BackupAndRestoreType type, sptr token, AppExecFwk::Ability* ability) override; + virtual WMError NapiSetUIContent(const std::string& contentInfo, ani_env* env, ani_object storage, + BackupAndRestoreType type, sptr token, AppExecFwk::Ability* ability) override; virtual std::string GetContentInfo(BackupAndRestoreType type = BackupAndRestoreType::CONTINUATION) override; virtual const std::shared_ptr GetContext() const override; virtual Ace::UIContent* GetUIContent() const override; diff --git a/previewer/mock/ani_err_utils.h b/previewer/mock/ani_err_utils.h new file mode 100644 index 0000000000..9fdf88b7f1 --- /dev/null +++ b/previewer/mock/ani_err_utils.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WINDOW_WINDOW_MANAGER_ANI_ERROR_UTILS_H +#define WINDOW_WINDOW_MANAGER_ANI_ERROR_UTILS_H + +#include "ani.h" +#include "wm_common.h" + +namespace OHOS::Rosen { +class AniErrUtils { +public: + static ani_status ThrowBusinessError(ani_env* env, WMError error, std::string message = "") { return ANI_OK; }; + static ani_status ThrowBusinessError(ani_env* env, WmErrorCode error, std::string message = "") { return ANI_OK; }; +public: + static ani_status CreateBusinessError(ani_env* env, int32_t error, std::string message, ani_object* err) { return ANI_OK; }; + static std::string GetErrorMsg(WMError error) { return ""; }; + static std::string GetErrorMsg(WmErrorCode error) { return ""; }; +}; +} // namespace OHOS::Rosen + +#endif //WINDOW_WINDOW_MANAGER_ANI_ERROR_UTILS_H diff --git a/previewer/mock/ani_window_listener.cpp b/previewer/mock/ani_window_listener.cpp new file mode 100644 index 0000000000..fe2896e8b0 --- /dev/null +++ b/previewer/mock/ani_window_listener.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ani_window_listener.h" + +#include "ani_window_utils.h" +//#include "event_handler.h" +#include "event_runner.h" +#include "window_manager_hilog.h" + +namespace OHOS { +namespace Rosen { +using namespace AbilityRuntime; + +AniWindowListener::~AniWindowListener() +{ + ani_status ret = env_->GlobalReference_Delete(aniCallBack_); + TLOGI(WmsLogTag::DEFAULT, "[ANI]~AniWindowListener ret:%{public}d", static_cast(ret)); +} + +void AniWindowListener::OnLastStrongRef(const void *) +{ +} + +void AniWindowListener::SetMainEventHandler() +{ + auto mainRunner = AppExecFwk::EventRunner::GetMainEventRunner(); + if (mainRunner == nullptr) { + return; + } +// eventHandler_ = std::make_shared(mainRunner); +} + +void AniWindowListener::OnAvoidAreaChanged(const AvoidArea avoidArea, AvoidAreaType type) +{ +} +} // namespace Rosen +} // namespace OHOS \ No newline at end of file diff --git a/previewer/mock/ani_window_listener.h b/previewer/mock/ani_window_listener.h new file mode 100644 index 0000000000..18cf2f9163 --- /dev/null +++ b/previewer/mock/ani_window_listener.h @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ANI_WINDOW_LISTENER_H +#define OHOS_ANI_WINDOW_LISTENER_H + +#include +#include + +#include "ani_window_utils.h" +#include "class_var_definition.h" +//#include "event_handler.h" +#include "refbase.h" +#include "window.h" +#include "window_manager.h" +#include "wm_common.h" + +namespace OHOS { +namespace Rosen { +enum class CaseType { + CASE_WINDOW_MANAGER = 0, + CASE_WINDOW, + CASE_STAGE +}; + +const std::string WINDOW_SIZE_CHANGE_CB = "windowSizeChange"; +const std::string SYSTEM_BAR_TINT_CHANGE_CB = "systemBarTintChange"; +const std::string SYSTEM_AVOID_AREA_CHANGE_CB = "systemAvoidAreaChange"; +const std::string AVOID_AREA_CHANGE_CB = "avoidAreaChange"; +const std::string LIFECYCLE_EVENT_CB = "lifeCycleEvent"; +const std::string WINDOW_STAGE_EVENT_CB = "windowStageEvent"; +const std::string WINDOW_EVENT_CB = "windowEvent"; +const std::string KEYBOARD_HEIGHT_CHANGE_CB = "keyboardHeightChange"; +const std::string KEYBOARD_DID_SHOW_CB = "keyboardDidShow"; +const std::string KEYBOARD_DID_HIDE_CB = "keyboardDidHide"; +const std::string TOUCH_OUTSIDE_CB = "touchOutside"; +const std::string SCREENSHOT_EVENT_CB = "screenshot"; +const std::string DIALOG_TARGET_TOUCH_CB = "dialogTargetTouch"; +const std::string DIALOG_DEATH_RECIPIENT_CB = "dialogDeathRecipient"; +const std::string GESTURE_NAVIGATION_ENABLED_CHANGE_CB = "gestureNavigationEnabledChange"; +const std::string WATER_MARK_FLAG_CHANGE_CB = "waterMarkFlagChange"; +const std::string WINDOW_VISIBILITY_CHANGE_CB = "windowVisibilityChange"; +const std::string WINDOW_STATUS_CHANGE_CB = "windowStatusChange"; +const std::string WINDOW_TITLE_BUTTON_RECT_CHANGE_CB = "windowTitleButtonRectChange"; +const std::string WINDOW_NO_INTERACTION_DETECT_CB = "noInteractionDetected"; +const std::string WINDOW_RECT_CHANGE_CB = "windowRectChange"; +const std::string SUB_WINDOW_CLOSE_CB = "subWindowClose"; +const std::string WINDOW_HIGHLIGHT_CHANGE_CB = "windowHighlightChange"; +const std::string WINDOW_STAGE_CLOSE_CB = "windowStageClose"; + +class AniWindowListener : public IAvoidAreaChangedListener { +public: + AniWindowListener(ani_env* env, ani_ref callback, CaseType caseType) + : env_(env), aniCallBack_(callback), caseType_(caseType), + weakRef_(wptr (this)) {} + ~AniWindowListener(); + void OnAvoidAreaChanged(const AvoidArea avoidArea, AvoidAreaType type) override; + void SetMainEventHandler(); + +private: + void OnLastStrongRef(const void *) override; + + Rect currRect_ = {0, 0, 0, 0}; + WindowState state_ {WindowState::STATE_INITIAL}; + void LifeCycleCallBack(LifeCycleEventType eventType); + ani_env* env_ = nullptr; + ani_ref aniCallBack_; + CaseType caseType_ = CaseType::CASE_WINDOW; + wptr weakRef_ = nullptr; +// std::shared_ptr eventHandler_ = nullptr; + DEFINE_VAR_DEFAULT_FUNC_SET(bool, IsDeprecatedInterface, isDeprecatedInterface, false) + RectChangeReason currentReason_ = RectChangeReason::UNDEFINED; +}; +} // namespace Rosen +} // namespace OHOS +#endif /* OHOS_ANI_WINDOW_LISTENER_H */ \ No newline at end of file diff --git a/previewer/mock/ani_window_register_manager.cpp b/previewer/mock/ani_window_register_manager.cpp new file mode 100644 index 0000000000..819cbe2691 --- /dev/null +++ b/previewer/mock/ani_window_register_manager.cpp @@ -0,0 +1,429 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ani.h" +#include "ani_window_register_manager.h" +#include "singleton_container.h" +#include "window_manager.h" +#include "window_manager_hilog.h" + +namespace OHOS { +namespace Rosen { +using namespace AbilityRuntime; +namespace { +const std::map WINDOW_MANAGER_LISTENER_MAP { + // white register list for window manager + {SYSTEM_BAR_TINT_CHANGE_CB, RegisterListenerType::SYSTEM_BAR_TINT_CHANGE_CB}, + {GESTURE_NAVIGATION_ENABLED_CHANGE_CB, RegisterListenerType::GESTURE_NAVIGATION_ENABLED_CHANGE_CB}, + {WATER_MARK_FLAG_CHANGE_CB, RegisterListenerType::WATER_MARK_FLAG_CHANGE_CB}, +}; +const std::map WINDOW_LISTENER_MAP { + // white register list for window + {WINDOW_SIZE_CHANGE_CB, RegisterListenerType::WINDOW_SIZE_CHANGE_CB}, + {SYSTEM_AVOID_AREA_CHANGE_CB, RegisterListenerType::SYSTEM_AVOID_AREA_CHANGE_CB}, + {AVOID_AREA_CHANGE_CB, RegisterListenerType::AVOID_AREA_CHANGE_CB}, + {LIFECYCLE_EVENT_CB, RegisterListenerType::LIFECYCLE_EVENT_CB}, + {WINDOW_EVENT_CB, RegisterListenerType::WINDOW_EVENT_CB}, + {KEYBOARD_HEIGHT_CHANGE_CB, RegisterListenerType::KEYBOARD_HEIGHT_CHANGE_CB}, + {KEYBOARD_DID_SHOW_CB, RegisterListenerType::KEYBOARD_DID_SHOW_CB}, + {KEYBOARD_DID_HIDE_CB, RegisterListenerType::KEYBOARD_DID_HIDE_CB}, + {TOUCH_OUTSIDE_CB, RegisterListenerType::TOUCH_OUTSIDE_CB}, + {SCREENSHOT_EVENT_CB, RegisterListenerType::SCREENSHOT_EVENT_CB}, + {DIALOG_TARGET_TOUCH_CB, RegisterListenerType::DIALOG_TARGET_TOUCH_CB}, + {DIALOG_DEATH_RECIPIENT_CB, RegisterListenerType::DIALOG_DEATH_RECIPIENT_CB}, + {WINDOW_STATUS_CHANGE_CB, RegisterListenerType::WINDOW_STATUS_CHANGE_CB}, + {WINDOW_TITLE_BUTTON_RECT_CHANGE_CB, RegisterListenerType::WINDOW_TITLE_BUTTON_RECT_CHANGE_CB}, + {WINDOW_VISIBILITY_CHANGE_CB, RegisterListenerType::WINDOW_VISIBILITY_CHANGE_CB}, + {WINDOW_NO_INTERACTION_DETECT_CB, RegisterListenerType::WINDOW_NO_INTERACTION_DETECT_CB}, + {WINDOW_RECT_CHANGE_CB, RegisterListenerType::WINDOW_RECT_CHANGE_CB}, + {SUB_WINDOW_CLOSE_CB, RegisterListenerType::SUB_WINDOW_CLOSE_CB}, + {WINDOW_HIGHLIGHT_CHANGE_CB, RegisterListenerType::WINDOW_HIGHLIGHT_CHANGE_CB}, +}; +const std::map WINDOW_STAGE_LISTENER_MAP { + // white register list for window stage + {WINDOW_STAGE_EVENT_CB, RegisterListenerType::WINDOW_STAGE_EVENT_CB}, + {WINDOW_STAGE_CLOSE_CB, RegisterListenerType::WINDOW_STAGE_CLOSE_CB}, +}; + +const std::map> LISTENER_CODE_MAP { + {CaseType::CASE_WINDOW_MANAGER, WINDOW_MANAGER_LISTENER_MAP}, + {CaseType::CASE_WINDOW, WINDOW_LISTENER_MAP}, + {CaseType::CASE_STAGE, WINDOW_STAGE_LISTENER_MAP}, +}; +} + +AniWindowRegisterManager::AniWindowRegisterManager() +{ +} + +AniWindowRegisterManager::~AniWindowRegisterManager() +{ +} + +WmErrorCode AniWindowRegisterManager::ProcessWindowChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessSystemAvoidAreaChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + if (window == nullptr) { + TLOGE(WmsLogTag::WMS_IMMS, "[ANI]Window is nullptr"); + return WmErrorCode::WM_ERROR_STATE_ABNORMALLY; + } + if (listener == nullptr) { + TLOGE(WmsLogTag::WMS_IMMS, "[ANI]listener is nullptr"); + return WmErrorCode::WM_ERROR_STATE_ABNORMALLY; + } + listener->SetIsDeprecatedInterface(true); + sptr thisListener(listener); + WmErrorCode ret = WmErrorCode::WM_OK; + if (isRegister) { + ret = WM_JS_TO_ERROR_CODE_MAP.at(window->RegisterAvoidAreaChangeListener(thisListener)); + } else { + ret = WM_JS_TO_ERROR_CODE_MAP.at(window->UnregisterAvoidAreaChangeListener(thisListener)); + } + return ret; +} + +WmErrorCode AniWindowRegisterManager::ProcessAvoidAreaChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + if (window == nullptr) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]Window is nullptr"); + return WmErrorCode::WM_ERROR_STATE_ABNORMALLY; + } + sptr thisListener(listener); + WmErrorCode ret = WmErrorCode::WM_OK; + if (isRegister) { + ret = WM_JS_TO_ERROR_CODE_MAP.at(window->RegisterAvoidAreaChangeListener(thisListener)); + } else { + ret = WM_JS_TO_ERROR_CODE_MAP.at(window->UnregisterAvoidAreaChangeListener(thisListener)); + } + return ret; +} + +WmErrorCode AniWindowRegisterManager::ProcessLifeCycleEventRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessOccupiedAreaChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessKeyboardDidShowRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessKeyboardDidHideRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessSystemBarChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessGestureNavigationEnabledChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessWaterMarkFlagChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} +WmErrorCode AniWindowRegisterManager::ProcessTouchOutsideRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessWindowVisibilityChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessWindowNoInteractionRegister(sptr listener, + sptr window, bool isRegister, ani_env* env, ani_double timeout) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessScreenshotRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessDialogTargetTouchRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessDialogDeathRecipientRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessWindowTitleButtonRectChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +bool AniWindowRegisterManager::IsCallbackRegistered(ani_env* env, std::string type, ani_ref jsListenerObject) +{ + if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { + TLOGI(WmsLogTag::DEFAULT, "[ANI]Method %{public}s has not been registerted", type.c_str()); + return false; + } + + for (auto iter = jsCbMap_[type].begin(); iter != jsCbMap_[type].end(); ++iter) { + ani_boolean isEquals = 0; + env->Reference_StrictEquals(jsListenerObject, iter->first, &isEquals); + if (isEquals) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]Method %{public}s has already been registered", type.c_str()); + return true; + } + } + return false; +} + +WmErrorCode AniWindowRegisterManager::RegisterListener(sptr window, std::string type, + CaseType caseType, ani_env* env, ani_ref callback, ani_double timeout) +{ + std::lock_guard lock(mtx_); + if (IsCallbackRegistered(env, type, callback)) { + return WmErrorCode::WM_OK; + } + auto iterCaseType = LISTENER_CODE_MAP.find(caseType); + if (iterCaseType == LISTENER_CODE_MAP.end()) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]CaseType %{public}u is not supported", static_cast(caseType)); + return WmErrorCode::WM_ERROR_STATE_ABNORMALLY; + } + auto iterCallbackType = iterCaseType->second.find(type); + if (iterCallbackType == iterCaseType->second.end()) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]Type %{public}s is not supported", type.c_str()); + return WmErrorCode::WM_ERROR_STATE_ABNORMALLY; + } + RegisterListenerType listenerType = iterCallbackType->second; + ani_ref cbRef{}; + if (env->GlobalReference_Create(callback, &cbRef) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]create global ref fail"); + return WmErrorCode::WM_ERROR_STATE_ABNORMALLY; + }; + sptr windowManagerListener = new AniWindowListener(env, cbRef, caseType); + if (windowManagerListener == nullptr) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]New AniWindowListener failed"); + return WmErrorCode::WM_ERROR_STATE_ABNORMALLY; + } + windowManagerListener->SetMainEventHandler(); + WmErrorCode ret = ProcessListener(listenerType, caseType, windowManagerListener, window, true, env, timeout); + if (ret != WmErrorCode::WM_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]Register type %{public}s failed", type.c_str()); + return ret; + } + jsCbMap_[type][callback] = windowManagerListener; + TLOGI(WmsLogTag::DEFAULT, "[ANI]Register type %{public}s success! callback map size: %{public}zu", + type.c_str(), jsCbMap_[type].size()); + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessListener(RegisterListenerType registerListenerType, CaseType caseType, + const sptr& windowManagerListener, const sptr& window, bool isRegister, + ani_env* env, ani_double timeout) +{ + if (caseType == CaseType::CASE_WINDOW_MANAGER) { + switch (static_cast(registerListenerType)) { + case static_cast(RegisterListenerType::SYSTEM_BAR_TINT_CHANGE_CB): + return ProcessSystemBarChangeRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::GESTURE_NAVIGATION_ENABLED_CHANGE_CB): + return ProcessGestureNavigationEnabledChangeRegister(windowManagerListener, window, isRegister, + env); + case static_cast(RegisterListenerType::WATER_MARK_FLAG_CHANGE_CB): + return ProcessWaterMarkFlagChangeRegister(windowManagerListener, window, isRegister, env); + default: + TLOGE(WmsLogTag::DEFAULT, "[ANI]RegisterListenerType %{public}u is not supported", + static_cast(registerListenerType)); + return WmErrorCode::WM_ERROR_INVALID_PARAM; + } + } else if (caseType == CaseType::CASE_WINDOW) { + switch (static_cast(registerListenerType)) { + case static_cast(RegisterListenerType::WINDOW_SIZE_CHANGE_CB): + return ProcessWindowChangeRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::SYSTEM_AVOID_AREA_CHANGE_CB): + return ProcessSystemAvoidAreaChangeRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::AVOID_AREA_CHANGE_CB): + return ProcessAvoidAreaChangeRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::LIFECYCLE_EVENT_CB): + return ProcessLifeCycleEventRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::WINDOW_EVENT_CB): + return ProcessLifeCycleEventRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::KEYBOARD_HEIGHT_CHANGE_CB): + return ProcessOccupiedAreaChangeRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::KEYBOARD_DID_SHOW_CB): + return ProcessKeyboardDidShowRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::KEYBOARD_DID_HIDE_CB): + return ProcessKeyboardDidHideRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::TOUCH_OUTSIDE_CB): + return ProcessTouchOutsideRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::SCREENSHOT_EVENT_CB): + return ProcessScreenshotRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::DIALOG_TARGET_TOUCH_CB): + return ProcessDialogTargetTouchRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::DIALOG_DEATH_RECIPIENT_CB): + return ProcessDialogDeathRecipientRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::WINDOW_STATUS_CHANGE_CB): + return ProcessWindowStatusChangeRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::WINDOW_TITLE_BUTTON_RECT_CHANGE_CB): + return ProcessWindowTitleButtonRectChangeRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::WINDOW_VISIBILITY_CHANGE_CB): + return ProcessWindowVisibilityChangeRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::WINDOW_NO_INTERACTION_DETECT_CB): + return ProcessWindowNoInteractionRegister(windowManagerListener, window, isRegister, env, timeout); + case static_cast(RegisterListenerType::WINDOW_RECT_CHANGE_CB): + return ProcessWindowRectChangeRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::SUB_WINDOW_CLOSE_CB): + return ProcessSubWindowCloseRegister(windowManagerListener, window, isRegister, env); + case static_cast(RegisterListenerType::WINDOW_HIGHLIGHT_CHANGE_CB): + return ProcessWindowHighlightChangeRegister(windowManagerListener, window, isRegister, env); + default: + TLOGE(WmsLogTag::DEFAULT, "[ANI]RegisterListenerType %{public}u is not supported", + static_cast(registerListenerType)); + return WmErrorCode::WM_ERROR_INVALID_PARAM; + } + } else if (caseType == CaseType::CASE_STAGE) { + switch (registerListenerType) { + case RegisterListenerType::WINDOW_STAGE_EVENT_CB: + return ProcessLifeCycleEventRegister(windowManagerListener, window, isRegister, env); + case RegisterListenerType::WINDOW_STAGE_CLOSE_CB: + return ProcessMainWindowCloseRegister(windowManagerListener, window, isRegister, env); + default: + TLOGE(WmsLogTag::DEFAULT, "[ANI]RegisterListenerType %{public}u is not supported", + static_cast(registerListenerType)); + return WmErrorCode::WM_ERROR_INVALID_PARAM; + } + } + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::UnregisterListener(sptr window, std::string type, + CaseType caseType, ani_env* env, ani_ref callback) +{ + std::lock_guard lock(mtx_); + if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) { + TLOGW(WmsLogTag::DEFAULT, "[ANI]Type %{public}s was not registerted", type.c_str()); + return WmErrorCode::WM_OK; + } + auto iterCaseType = LISTENER_CODE_MAP.find(caseType); + if (iterCaseType == LISTENER_CODE_MAP.end()) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]CaseType %{public}u is not supported", static_cast(caseType)); + return WmErrorCode::WM_ERROR_STATE_ABNORMALLY; + } + auto iterCallbackType = iterCaseType->second.find(type); + if (iterCallbackType == iterCaseType->second.end()) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]Type %{public}s is not supported", type.c_str()); + return WmErrorCode::WM_ERROR_STATE_ABNORMALLY; + } + RegisterListenerType listenerType = iterCallbackType->second; + if (callback == nullptr) { + for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) { + WmErrorCode ret = ProcessListener(listenerType, caseType, it->second, window, false, env, 0); + if (ret != WmErrorCode::WM_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]Unregister type %{public}s failed, no value", type.c_str()); + return ret; + } + jsCbMap_[type].erase(it++); + } + } else { + bool findFlag = false; + for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end(); ++it) { + ani_boolean isEquals = 0; + env->Reference_StrictEquals(callback, it->first, &isEquals); + if (!isEquals) { + continue; + } + findFlag = true; + WmErrorCode ret = ProcessListener(listenerType, caseType, it->second, window, false, env, 0); + if (ret != WmErrorCode::WM_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]Unregister type %{public}s failed", type.c_str()); + return ret; + } + jsCbMap_[type].erase(it); + break; + } + if (!findFlag) { + TLOGW(WmsLogTag::DEFAULT, + "[ANI]Unregister type %{public}s failed because not found callback!", type.c_str()); + return WmErrorCode::WM_OK; + } + } + TLOGI(WmsLogTag::DEFAULT, "[ANI]Unregister type %{public}s success! callback map size: %{public}zu", + type.c_str(), jsCbMap_[type].size()); + // erase type when there is no callback in one type + if (jsCbMap_[type].empty()) { + jsCbMap_.erase(type); + } + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessWindowStatusChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessWindowRectChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessSubWindowCloseRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessWindowHighlightChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} + +WmErrorCode AniWindowRegisterManager::ProcessMainWindowCloseRegister(const sptr& listener, + const sptr& window, bool isRegister, ani_env* env) +{ + return WmErrorCode::WM_OK; +} +} // namespace Rosen +} // namespace OHOS \ No newline at end of file diff --git a/previewer/mock/ani_window_register_manager.h b/previewer/mock/ani_window_register_manager.h new file mode 100644 index 0000000000..e54d265113 --- /dev/null +++ b/previewer/mock/ani_window_register_manager.h @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ANI_WINDOW_REGISTER_MANAGER_H +#define OHOS_ANI_WINDOW_REGISTER_MANAGER_H + +#include +#include +#include "ani_window_listener.h" +#include "refbase.h" +#include "window.h" + +namespace OHOS { +namespace Rosen { +enum class RegisterListenerType : uint32_t { + SYSTEM_BAR_TINT_CHANGE_CB, + GESTURE_NAVIGATION_ENABLED_CHANGE_CB, + WATER_MARK_FLAG_CHANGE_CB, + WINDOW_SIZE_CHANGE_CB, + SYSTEM_AVOID_AREA_CHANGE_CB, + AVOID_AREA_CHANGE_CB, + LIFECYCLE_EVENT_CB, + WINDOW_EVENT_CB, + KEYBOARD_HEIGHT_CHANGE_CB, + KEYBOARD_DID_SHOW_CB, + KEYBOARD_DID_HIDE_CB, + TOUCH_OUTSIDE_CB, + SCREENSHOT_EVENT_CB, + DIALOG_TARGET_TOUCH_CB, + DIALOG_DEATH_RECIPIENT_CB, + WINDOW_STATUS_CHANGE_CB, + WINDOW_TITLE_BUTTON_RECT_CHANGE_CB, + WINDOW_VISIBILITY_CHANGE_CB, + WINDOW_NO_INTERACTION_DETECT_CB, + WINDOW_RECT_CHANGE_CB, + SUB_WINDOW_CLOSE_CB, + WINDOW_HIGHLIGHT_CHANGE_CB, + WINDOW_STAGE_EVENT_CB, + WINDOW_STAGE_CLOSE_CB, +}; + +class AniWindowRegisterManager { +public: + AniWindowRegisterManager(); + ~AniWindowRegisterManager(); + WmErrorCode RegisterListener(sptr window, std::string type, + CaseType caseType, ani_env* env, ani_ref callback, ani_double timeout); + WmErrorCode UnregisterListener(sptr window, std::string type, + CaseType caseType, ani_env* env, ani_ref callback); +private: + bool IsCallbackRegistered(ani_env* env, std::string type, ani_ref jsListenerObject); + WmErrorCode ProcessWindowChangeRegister(sptr listener, sptr window, bool isRegister, + ani_env* env); + WmErrorCode ProcessSystemAvoidAreaChangeRegister(sptr listener, sptr window, + bool isRegister, ani_env* env); + WmErrorCode ProcessAvoidAreaChangeRegister(sptr listener, sptr window, bool isRegister, + ani_env* env); + WmErrorCode ProcessLifeCycleEventRegister(sptr listener, sptr window, bool isRegister, + ani_env* env); + WmErrorCode ProcessOccupiedAreaChangeRegister(sptr listener, sptr window, + bool isRegister, ani_env* env); + WmErrorCode ProcessKeyboardDidShowRegister(sptr listener, sptr window, + bool isRegister, ani_env* env); + WmErrorCode ProcessKeyboardDidHideRegister(sptr listener, sptr window, + bool isRegister, ani_env* env); + WmErrorCode ProcessSystemBarChangeRegister(sptr listener, sptr window, bool isRegister, + ani_env* env); + WmErrorCode ProcessTouchOutsideRegister(sptr listener, sptr window, bool isRegister, + ani_env* env); + WmErrorCode ProcessScreenshotRegister(sptr listener, sptr window, bool isRegister, + ani_env* env); + WmErrorCode ProcessDialogTargetTouchRegister(sptr listener, sptr window, bool isRegister, + ani_env* env); + WmErrorCode ProcessDialogDeathRecipientRegister(sptr listener, sptr window, + bool isRegister, ani_env* env); + WmErrorCode ProcessGestureNavigationEnabledChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env); + WmErrorCode ProcessWaterMarkFlagChangeRegister(sptr listener, + sptr window, bool isRegister, ani_env* env); + WmErrorCode ProcessWindowVisibilityChangeRegister(sptr listener, sptr window, + bool isRegister, ani_env* env); + WmErrorCode ProcessWindowNoInteractionRegister(sptr listener, sptr window, + bool isRegister, ani_env* env, ani_double timeout); + WmErrorCode ProcessWindowStatusChangeRegister(sptr listener, sptr window, + bool isRegister, ani_env* env); + WmErrorCode ProcessWindowTitleButtonRectChangeRegister(sptr listener, sptr window, + bool isRegister, ani_env* env); + WmErrorCode ProcessWindowRectChangeRegister(sptr listener, sptr window, + bool isRegister, ani_env* env); + WmErrorCode ProcessSubWindowCloseRegister(sptr listener, sptr window, + bool isRegister, ani_env* env); + WmErrorCode ProcessWindowHighlightChangeRegister(sptr listener, sptr window, + bool isRegister, ani_env* env); + WmErrorCode ProcessMainWindowCloseRegister(const sptr& listener, const sptr& window, + bool isRegister, ani_env* env); + WmErrorCode ProcessListener(RegisterListenerType registerListenerType, CaseType caseType, + const sptr& windowManagerListener, const sptr& window, bool isRegister, + ani_env* env, ani_double timeout); + std::map>> jsCbMap_; + std::mutex mtx_; +}; +} // namespace Rosen +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/previewer/mock/ani_window_utils.cpp b/previewer/mock/ani_window_utils.cpp new file mode 100644 index 0000000000..0b81119b38 --- /dev/null +++ b/previewer/mock/ani_window_utils.cpp @@ -0,0 +1,1028 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ani_window_utils.h" + +#include +#include +#include + +#include "ani.h" +#include "ani_err_utils.h" +#include "ani_window.h" +#include "bundle_constants.h" +#include "foundation/arkui/ace_engine/interfaces/inner_api/ace/ui_content.h" +#include "ipc_skeleton.h" +#include "window_manager_hilog.h" + +namespace OHOS { +namespace Rosen { +namespace { +std::string GetHexColor(uint32_t color) +{ + std::stringstream ioss; + std::string temp; + ioss << std::setiosflags(std::ios::uppercase) << std::hex << color; + ioss >> temp; + int count = RGBA_LENGTH - static_cast(temp.length()); + std::string tmpColor(count, '0'); + tmpColor += temp; + std::string finalColor("#"); + finalColor += tmpColor; + return finalColor; +} +} + +ani_status AniWindowUtils::GetStdString(ani_env *env, ani_string ani_str, std::string &result) +{ + ani_size strSize; + ani_status ret = env->String_GetUTF8Size(ani_str, &strSize); + if (ret != ANI_OK) { + return ret; + } + std::vector buffer(strSize + 1); + char* utf8_buffer = buffer.data(); + ani_size bytes_written = 0; + ret = env->String_GetUTF8(ani_str, utf8_buffer, strSize + 1, &bytes_written); + if (ret != ANI_OK) { + return ret; + } + utf8_buffer[bytes_written] = '\0'; + result = std::string(utf8_buffer); + return ret; +} + +ani_status AniWindowUtils::GetStdStringVector(ani_env* env, ani_object ary, std::vector& result) +{ + ani_double length; + ani_status ret = env->Object_GetPropertyByName_Double(ary, "length", &length); + if (ret != ANI_OK) { + return ret; + } + for (int32_t i = 0; i< static_cast(length); i++) { + ani_ref stringRef; + ret = env->Object_CallMethodByName_Ref(ary, "$_get", "I:Lstd/core/Object;", &stringRef, ani_int(i)); + if (ret != ANI_OK) { + return ret; + } + std::string str; + AniWindowUtils::GetStdString(env, static_cast(stringRef), str); + result.emplace_back(str); + } + return ANI_OK; +} + +ani_status AniWindowUtils::GetPropertyIntObject(ani_env* env, const char* propertyName, + ani_object object, int32_t& result) +{ + ani_ref int_ref; + ani_status ret = env->Object_GetPropertyByName_Ref(object, propertyName, &int_ref); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] Object_GetPropertyByName_Ref %{public}s Failed, ret : %{public}u", + propertyName, static_cast(ret)); + return ret; + } + + ani_boolean isUndefined; + if (ANI_OK != env->Reference_IsUndefined(int_ref, &isUndefined)) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] Object_GetPropertyByName_Ref %{public}s Failed", propertyName); + return ret; + } + + if (isUndefined) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] %{public}s is Undefined Now", propertyName); + return ret; + } + + ani_int int_value; + if (ANI_OK != env->Object_CallMethodByName_Int(static_cast(int_ref), "intValue", nullptr, &int_value)) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] Object_GetPropertyByName_Ref %{public}s Failed", propertyName); + return ret; + } + result = static_cast(int_value); + TLOGI(WmsLogTag::DEFAULT, "[ANI] %{public}s is: %{public}u", propertyName, result); + return ret; +} + +ani_status AniWindowUtils::GetPropertyDoubleObject(ani_env* env, const char* propertyName, + ani_object object, double& result) +{ + ani_ref double_ref; + ani_status ret = env->Object_GetPropertyByName_Ref(object, propertyName, &double_ref); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] Object_GetPropertyByName_Ref %{public}s Failed, ret : %{public}u", + propertyName, static_cast(ret)); + return ret; + } + + ani_boolean isUndefined; + if (ANI_OK != env->Reference_IsUndefined(double_ref, &isUndefined)) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] Object_GetPropertyByName_Ref %{public}s Failed", propertyName); + return ret; + } + + if (isUndefined) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] %{public}s is Undefined Now", propertyName); + return ret; + } + + ani_double double_value; + if (ANI_OK != env->Object_CallMethodByName_Double(static_cast(double_ref), + "doubleValue", nullptr, &double_value)) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] Object_GetPropertyByName_Ref %{public}s Failed", propertyName); + return ret; + } + result = static_cast(double_value); + TLOGI(WmsLogTag::DEFAULT, "[ANI] %{public}s is: %{public}f", propertyName, result); + return ret; +} + +ani_status AniWindowUtils::GetDoubleObject(ani_env* env, ani_object double_object, double& result) +{ + ani_boolean isUndefined; + ani_status isUndefinedRet = env->Reference_IsUndefined(double_object, &isUndefined); + if (ANI_OK != isUndefinedRet) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] Check double_object isUndefined fail"); + return isUndefinedRet; + } + + if (isUndefined) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] CallMeWithOptionalDouble Not Pass Value"); + return ANI_INVALID_ARGS; + } + + ani_double double_value; + ani_status ret = env->Object_CallMethodByName_Double(double_object, "doubleValue", nullptr, &double_value); + if (ANI_OK != ret) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] Object_CallMethodByName_Double Failed!"); + return ret; + } + result = static_cast(double_value); + TLOGI(WmsLogTag::DEFAULT, "[ANI] double result is: %{public}f", result); + return ret; +} + +ani_status AniWindowUtils::NewAniObjectNoParams(ani_env* env, const char* cls, ani_object* object) +{ + ani_class aniClass; + ani_status ret = env->FindClass(cls, &aniClass); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] Find class failed"); + return ret; + } + ani_method aniCtor; + ret = env->Class_FindMethod(aniClass, "", nullptr, &aniCtor); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] Find class constructor failed"); + return ret; + } + return env->Object_New(aniClass, aniCtor, object); +} + +ani_status AniWindowUtils::NewAniObject(ani_env* env, const char* cls, const char* signature, ani_object* result, ...) +{ + ani_class aniClass; + ani_status ret = env->FindClass(cls, &aniClass); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] class not found, ret:%{public}d", static_cast(ret)); + return ret; + } + ani_method aniCtor; + ret = env->Class_FindMethod(aniClass, "", signature, &aniCtor); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] method not found, ret:%{public}d", static_cast(ret)); + return ret; + } + va_list args; + va_start(args, result); + ani_status status = env->Object_New(aniClass, aniCtor, result, args); + va_end(args); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] new obj fail, ret:%{public}d", static_cast(ret)); + return ret; + } + return status; +} + +ani_object AniWindowUtils::CreateAniUndefined(ani_env* env) +{ + ani_ref aniRef; + env->GetUndefined(&aniRef); + return static_cast(aniRef); +} + +ani_object AniWindowUtils::AniThrowError(ani_env* env, WMError errorCode, std::string msg) +{ + AniErrUtils::ThrowBusinessError(env, errorCode, msg); + return AniWindowUtils::CreateAniUndefined(env); +} + +ani_object AniWindowUtils::AniThrowError(ani_env* env, WmErrorCode errorCode, std::string msg) +{ + AniErrUtils::ThrowBusinessError(env, errorCode, msg); + return AniWindowUtils::CreateAniUndefined(env); +} + +ani_object AniWindowUtils::CreateAniSize(ani_env* env, int32_t width, int32_t height) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + ani_class aniClass; + ani_status ret = env->FindClass("L@ohos/window/window/SizeInternal;", &aniClass); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] class not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_method aniCtor; + ret = env->Class_FindMethod(aniClass, "", nullptr, &aniCtor); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] ctor not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_object aniRect; + ret = env->Object_New(aniClass, aniCtor, &aniRect); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] fail to new obj"); + return AniWindowUtils::CreateAniUndefined(env); + } + CallAniMethodVoid(env, aniRect, aniClass, "width", nullptr, ani_double(width)); + CallAniMethodVoid(env, aniRect, aniClass, "height", nullptr, ani_double(height)); + return aniRect; +} + +ani_object AniWindowUtils::CreateAniDecorButtonStyle(ani_env* env, const DecorButtonStyle& decorButtonStyle) +{ + TLOGI(WmsLogTag::WMS_DECOR, "[ANI]"); + ani_class aniClass; + ani_status ret = env->FindClass("L@ohos/window/window/DecorButtonStyle;", &aniClass); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::WMS_DECOR, "[ANI] class not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_method aniCtor; + ret = env->Class_FindMethod(aniClass, "", nullptr, &aniCtor); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::WMS_DECOR, "[ANI] ctor not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_object aniDecorButtonStyle; + ret = env->Object_New(aniClass, aniCtor, &aniDecorButtonStyle); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::WMS_DECOR, "[ANI] fail to new obj"); + return AniWindowUtils::CreateAniUndefined(env); + } + CallAniMethodVoid(env, aniDecorButtonStyle, aniClass, + "colorMode", nullptr, ani_long(decorButtonStyle.colorMode)); + CallAniMethodVoid(env, aniDecorButtonStyle, aniClass, + "buttonBackgroundSize", nullptr, ani_double(decorButtonStyle.buttonBackgroundSize)); + CallAniMethodVoid(env, aniDecorButtonStyle, aniClass, + "spacingBetweenButtons", nullptr, ani_double(decorButtonStyle.spacingBetweenButtons)); + CallAniMethodVoid(env, aniDecorButtonStyle, aniClass, + "closeButtonRightMargin", nullptr, ani_double(decorButtonStyle.closeButtonRightMargin)); + return aniDecorButtonStyle; +} + +ani_object AniWindowUtils::CreateAniTitleButtonRect(ani_env* env, const TitleButtonRect& titleButtonRect) +{ + TLOGI(WmsLogTag::WMS_DECOR, "[ANI]"); + ani_class aniClass; + ani_status ret = env->FindClass("L@ohos/window/window/TitleButtonRect;", &aniClass); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::WMS_DECOR, "[ANI] class not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_method aniCtor; + ret = env->Class_FindMethod(aniClass, "", nullptr, &aniCtor); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::WMS_DECOR, "[ANI] ctor not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_object aniTitleButtonRect; + ret = env->Object_New(aniClass, aniCtor, &aniTitleButtonRect); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::WMS_DECOR, "[ANI] fail to new obj"); + return AniWindowUtils::CreateAniUndefined(env); + } + CallAniMethodVoid(env, aniTitleButtonRect, aniClass, "right", nullptr, ani_double(titleButtonRect.posX_)); + CallAniMethodVoid(env, aniTitleButtonRect, aniClass, "top", nullptr, ani_double(titleButtonRect.posY_)); + CallAniMethodVoid(env, aniTitleButtonRect, aniClass, "width", nullptr, ani_double(titleButtonRect.width_)); + CallAniMethodVoid(env, aniTitleButtonRect, aniClass, "height", nullptr, ani_double(titleButtonRect.height_)); + return aniTitleButtonRect; +} + +ani_object AniWindowUtils::CreateAniWindowArray(ani_env* env, std::vector& windows) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + ani_array_ref windowArray = nullptr; + ani_class windowCls; + if (env->FindClass("L@ohos/window/window/WindowInternal;", &windowCls) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] class not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + if (env->Array_New_Ref(windowCls, windows.size(), CreateAniUndefined(env), &windowArray) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] create array fail"); + return AniWindowUtils::CreateAniUndefined(env); + } + for (size_t i = 0; i < windows.size(); i++) { + if (env->Array_Set_Ref(windowArray, i, windows[i]) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] set window array failed"); + return AniWindowUtils::CreateAniUndefined(env); + } + } + return windowArray; +} + +ani_object AniWindowUtils::CreateAniRect(ani_env* env, const Rect& rect) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + ani_class aniClass; + ani_status ret = env->FindClass("L@ohos/window/window/RectInternal;", &aniClass); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] class not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_method aniCtor; + ret = env->Class_FindMethod(aniClass, "", nullptr, &aniCtor); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] ctor not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_object aniRect; + ret = env->Object_New(aniClass, aniCtor, &aniRect); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] fail to create new obj"); + return AniWindowUtils::CreateAniUndefined(env); + } + CallAniMethodVoid(env, aniRect, aniClass, "left", nullptr, ani_double(rect.posX_)); + CallAniMethodVoid(env, aniRect, aniClass, "top", nullptr, ani_double(rect.posY_)); + CallAniMethodVoid(env, aniRect, aniClass, "width", nullptr, ani_double(rect.width_)); + CallAniMethodVoid(env, aniRect, aniClass, "height", nullptr, ani_double(rect.height_)); + return aniRect; +} + +ani_object AniWindowUtils::CreateAniAvoidArea(ani_env* env, const AvoidArea& avoidArea, AvoidAreaType type) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + ani_class aniClass; + ani_status ret = env->FindClass("L@ohos/window/window/AvoidAreaInternal;", &aniClass); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] class not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_method aniCtor; + ret = env->Class_FindMethod(aniClass, "", nullptr, &aniCtor); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] ctor not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_object aniAvoidArea; + ret = env->Object_New(aniClass, aniCtor, &aniAvoidArea); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] fail to new obj"); + return AniWindowUtils::CreateAniUndefined(env); + } + CallAniMethodVoid(env, aniAvoidArea, aniClass, "visible", nullptr, + ani_boolean(type != AvoidAreaType::TYPE_CUTOUT)); + CallAniMethodVoid(env, aniAvoidArea, aniClass, "leftRect", nullptr, + CreateAniRect(env, avoidArea.leftRect_)); + CallAniMethodVoid(env, aniAvoidArea, aniClass, "topRect", nullptr, + CreateAniRect(env, avoidArea.topRect_)); + CallAniMethodVoid(env, aniAvoidArea, aniClass, "rightRect", nullptr, + CreateAniRect(env, avoidArea.rightRect_)); + CallAniMethodVoid(env, aniAvoidArea, aniClass, "bottomRect", nullptr, + CreateAniRect(env, avoidArea.bottomRect_)); + return aniAvoidArea; +} + +ani_object AniWindowUtils::CreateAniSystemBarTintState(ani_env* env, DisplayId displayId, + const SystemBarRegionTints& tints) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + ani_class aniClass; + if (env->FindClass("L@ohos/window/window/SystemBarTintStateInternal;", &aniClass) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] class not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_method aniCtor; + if (env->Class_FindMethod(aniClass, "", nullptr, &aniCtor) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] ctor not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_object state; + if (env->Object_New(aniClass, aniCtor, &state) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] fail to new obj"); + return AniWindowUtils::CreateAniUndefined(env); + } + CallAniMethodVoid(env, state, aniClass, "displayId", nullptr, static_cast(displayId)); + ani_array regionTintArray = nullptr; + if (env->Array_New(tints.size(), CreateAniUndefined(env), ®ionTintArray) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] create array failed"); + return AniWindowUtils::CreateAniUndefined(env); + } + for (size_t i = 0; i < tints.size(); i++) { + if (env->Array_Set(regionTintArray, i, CreateAniSystemBarRegionTint(env, tints[i])) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] create region tint failed"); + return AniWindowUtils::CreateAniUndefined(env); + } + } + CallAniMethodVoid(env, state, aniClass, "regionTint", nullptr, regionTintArray); + return state; +} + +ani_object AniWindowUtils::CreateAniSystemBarRegionTint(ani_env* env, const SystemBarRegionTint& tint) +{ + ani_class regionTintCls; + if (env->FindClass("L@ohos/window/window/SystemBarRegionTintInternal;", ®ionTintCls) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] class not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_method regionTintCtor; + if (env->Class_FindMethod(regionTintCls, "", nullptr, ®ionTintCtor) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] ctor not found"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_object regionTint; + if (env->Object_New(regionTintCls, regionTintCtor, ®ionTint) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] fail to new obj"); + return AniWindowUtils::CreateAniUndefined(env); + } + if (NATIVE_JS_TO_WINDOW_TYPE_MAP.count(tint.type_) != 0) { + CallAniMethodVoid(env, regionTint, regionTintCls, "type", nullptr, + ani_long(NATIVE_JS_TO_WINDOW_TYPE_MAP.at(tint.type_))); + } else { + CallAniMethodVoid(env, regionTint, regionTintCls, "type", nullptr, ani_long(tint.type_)); + } + CallAniMethodVoid(env, regionTint, regionTintCls, "isEnable", nullptr, ani_boolean(tint.prop_.enable_)); + ani_string backgroundColor; + if (GetAniString(env, GetHexColor(tint.prop_.backgroundColor_), &backgroundColor) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] create string failed"); + return AniWindowUtils::CreateAniUndefined(env); + } + CallAniMethodVoid(env, regionTint, regionTintCls, "backgroundColor", nullptr, backgroundColor); + ani_string contentColor; + if (GetAniString(env, GetHexColor(tint.prop_.contentColor_), &contentColor) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] create string failed"); + return AniWindowUtils::CreateAniUndefined(env); + } + CallAniMethodVoid(env, regionTint, regionTintCls, "contentColor", nullptr, contentColor); + CallAniMethodVoid(env, regionTint, regionTintCls, "region", nullptr, CreateAniRect(env, tint.region_)); + return regionTint; +} + +ani_status AniWindowUtils::CallAniFunctionVoid(ani_env *env, const char* ns, + const char* fn, const char* signature, ...) +{ + ani_status ret = ANI_OK; + ani_namespace aniNamespace{}; + if ((ret = env->FindNamespace(ns, &aniNamespace)) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]canot find ns:%{public}s ret:%{public}d", ns, ret); + return ret; + } + ani_function func{}; + if ((ret = env->Namespace_FindFunction(aniNamespace, fn, signature, &func)) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]canot find callBack %{public}d", ret); + return ret; + } + va_list args; + va_start(args, signature); + ret = env->Function_Call_Void_V(func, args); + va_end(args); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]canot run callBack %{public}d", ret); + return ret; + } + return ret; +} + +ani_status AniWindowUtils::CallAniMethodVoid(ani_env *env, ani_object object, const char* cls, + const char* method, const char* signature, ...) +{ + ani_class aniClass; + ani_status ret = env->FindClass(cls, &aniClass); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] class:%{public}s not found", cls); + return ret; + } + ani_method aniMethod; + ret = env->Class_FindMethod(aniClass, method, nullptr, &aniMethod); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI]2 class:%{public}s method:%{public}s not found, ret:%{public}d", + cls, method, ret); + return ret; + } + va_list args; + va_start(args, signature); + ret = env->Object_CallMethod_Void_V(object, aniMethod, args); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] fail to call method:%{public}s", method); + } + va_end(args); + return ret; +} + +ani_status AniWindowUtils::CallAniMethodVoid(ani_env *env, ani_object object, ani_class cls, + const char* method, const char* signature, ...) +{ + ani_method aniMethod; + ani_status ret = env->Class_FindMethod(cls, method, signature, &aniMethod); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] method:%{public}s not found", method); + return ret; + } + va_list args; + va_start(args, signature); + ret = env->Object_CallMethod_Void_V(object, aniMethod, args); + va_end(args); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] fail to call method:%{public}s", method); + } + return ret; +} + +ani_status AniWindowUtils::GetAniString(ani_env* env, const std::string& str, ani_string* result) +{ + return env->String_NewUTF8(str.c_str(), static_cast(str.size()), result); +} + +void AniWindowUtils::SetSystemPropertiesWindowRect(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + Rect rect = window->GetRect(); + ani_object aniWindowRect = CreateAniRect(env, rect); + CallAniMethodVoid(env, systemProperties, clsName, "windowRect", nullptr, aniWindowRect); +} + +void AniWindowUtils::SetSystemPropertiesDrawableRect(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + Ace::UIContent* uiContent = window->GetUIContent(); + Rect drawableRect = g_emptyRect; + if (uiContent == nullptr) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] uicontent is nullptr"); + } else { + uiContent->GetWindowPaintSize(drawableRect); + } + + ani_object aniDrawableRect = CreateAniRect(env, drawableRect); + if (aniDrawableRect == nullptr) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] GetDrawableRect failed!"); + return; + } + + CallAniMethodVoid(env, systemProperties, clsName, "drawableRect", nullptr, aniDrawableRect); +} + +void AniWindowUtils::SetSystemPropertiesWindowType(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + WindowType aniWindowType = window->GetType(); + env->Object_SetFieldByName_Int(systemProperties, "typeInternal", ani_int(aniWindowType)); +} + +void AniWindowUtils::SetSystemPropertiesWindowIsLayoutFullScreen(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + bool isLayotFullScreen = window->IsLayoutFullScreen(); + CallAniMethodVoid(env, systemProperties, clsName, "isLayoutFullScreen", nullptr, + static_cast(isLayotFullScreen)); +} + + +void AniWindowUtils::SetSystemPropertiesWindowIsFullScreen(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + bool isFullScreen = window->IsFullScreen(); + CallAniMethodVoid(env, systemProperties, clsName, "isFullScreen", nullptr, + static_cast(isFullScreen)); +} + +void AniWindowUtils::SetSystemPropertiesWindowTouchable(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + bool windowTouchable = window->GetTouchable(); + CallAniMethodVoid(env, systemProperties, clsName, "touchable", nullptr, + static_cast(windowTouchable)); +} + +void AniWindowUtils::SetSystemPropertiesWindowFousable(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + bool windowFousable = window->GetFocusable(); + CallAniMethodVoid(env, systemProperties, clsName, "focusable", nullptr, + static_cast(windowFousable)); +} + +void AniWindowUtils::SetSystemPropertiesWindowIsPrivacyMode(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + bool windowIsPrivacyMode = window->IsPrivacyMode(); + CallAniMethodVoid(env, systemProperties, clsName, "isPrivacyMode", nullptr, + static_cast(windowIsPrivacyMode)); +} + +void AniWindowUtils::SetSystemPropertiesWindowIsKeepScreenOn(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + bool windowIsKeepScreenOn = window->IsKeepScreenOn(); + CallAniMethodVoid(env, systemProperties, clsName, "isKeepScreenOn", nullptr, + static_cast(windowIsKeepScreenOn)); +} + +void AniWindowUtils::SetSystemPropertiesWindowBrightness(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + float windowBrightness = window->GetBrightness(); + CallAniMethodVoid(env, systemProperties, clsName, "brightness", nullptr, + static_cast(windowBrightness)); +} + + +void AniWindowUtils::SetSystemPropertiesWindowIsTransparent(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + bool isTransparent = window->IsTransparent(); + CallAniMethodVoid(env, systemProperties, clsName, "isTransparent", nullptr, + static_cast(isTransparent)); +} + + +void AniWindowUtils::SetSystemPropertieswindowIsRoundCorner(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + bool windowIsRoundCorner {false}; + CallAniMethodVoid(env, systemProperties, clsName, "isRoundCorner", nullptr, + static_cast(windowIsRoundCorner)); +} + + +void AniWindowUtils::SetSystemPropertiesWindowDimBehindValue(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + int windowDimBehindValue {0}; + CallAniMethodVoid(env, systemProperties, clsName, "dimBehindValue", nullptr, + static_cast(windowDimBehindValue)); +} + + +void AniWindowUtils::SetSystemPropertieswindowId(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + uint32_t windowId = window->GetWindowId(); + CallAniMethodVoid(env, systemProperties, clsName, "id", nullptr, + static_cast(windowId)); +} + +void AniWindowUtils::SetSystemPropertiesdisplayId(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + uint32_t displayId = window->GetDisplayId(); + CallAniMethodVoid(env, systemProperties, clsName, "displayId", nullptr, + static_cast(displayId)); +} + +ani_object AniWindowUtils::CreateWindowsProperties(ani_env* env, const sptr& window) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + if (window == nullptr) { + TLOGE(WmsLogTag::DEFAULT, "window is nullptr or get invalid param"); + return AniWindowUtils::AniThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY); + } + + static const char* clsName = "L@ohos/window/window/WindowPropertiesInternal;"; + ani_object aniSystemProperties; + + NewAniObjectNoParams(env, clsName, &aniSystemProperties); + SetSystemPropertiesWindowRect(env, window, aniSystemProperties, clsName); + SetSystemPropertiesDrawableRect(env, window, aniSystemProperties, clsName); + SetSystemPropertiesWindowType(env, window, aniSystemProperties, clsName); + SetSystemPropertiesWindowIsLayoutFullScreen(env, window, aniSystemProperties, clsName); + SetSystemPropertiesWindowIsFullScreen(env, window, aniSystemProperties, clsName); + SetSystemPropertiesWindowTouchable(env, window, aniSystemProperties, clsName); + SetSystemPropertiesWindowFousable(env, window, aniSystemProperties, clsName); + SetSystemPropertiesWindowIsPrivacyMode(env, window, aniSystemProperties, clsName); + SetSystemPropertiesWindowIsKeepScreenOn(env, window, aniSystemProperties, clsName); + SetSystemPropertiesWindowBrightness(env, window, aniSystemProperties, clsName); + SetSystemPropertiesWindowIsTransparent(env, window, aniSystemProperties, clsName); + SetSystemPropertieswindowIsRoundCorner(env, window, aniSystemProperties, clsName); + SetSystemPropertiesWindowDimBehindValue(env, window, aniSystemProperties, clsName); + SetSystemPropertieswindowId(env, window, aniSystemProperties, clsName); + SetSystemPropertiesdisplayId(env, window, aniSystemProperties, clsName); + + TLOGI(WmsLogTag::DEFAULT, "[ANI] Window [%{public}u, %{public}s] get properties end", window->GetWindowId(), + window->GetWindowName().c_str()); + if (aniSystemProperties == nullptr) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] AniSystemProperties is nullptr"); + return AniWindowUtils::AniThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY); + } + + return aniSystemProperties; +} + +ani_object AniWindowUtils::CreateProperties(ani_env* env, const sptr& window) +{ + return {}; +} + +uint32_t AniWindowUtils::GetColorFromAni(ani_env* env, + const char* name, + uint32_t defaultColor, + bool& flag, + const ani_object& aniObject) +{ + ani_ref result; + env->Object_GetPropertyByName_Ref(aniObject, name, &result); + ani_string aniColor = reinterpret_cast(result); + std::string colorStr; + GetStdString(env, aniColor, colorStr); + std::regex pattern("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$"); + if (!std::regex_match(colorStr, pattern)) { + TLOGD(WmsLogTag::DEFAULT, "Invalid color input"); + return defaultColor; + } + std::string color = colorStr.substr(1); + if (color.length() == RGB_LENGTH) { + color = "FF" + color; // ARGB + } + flag = true; + std::stringstream ss; + uint32_t hexColor; + ss << std::hex << color; + ss >> hexColor; + TLOGI(WmsLogTag::DEFAULT, "Origin %{public}s, process %{public}s, final %{public}x", + colorStr.c_str(), color.c_str(), hexColor); + return hexColor; +} + +bool AniWindowUtils::SetWindowStatusBarContentColor(ani_env* env, + ani_object aniObject, + std::map& properties, + std::map& propertyFlags) +{ + auto statusProperty = properties[WindowType::WINDOW_TYPE_STATUS_BAR]; + ani_ref aniStatusContentColor; + env->Object_GetPropertyByName_Ref(aniObject, "statusBarContentColor", &aniStatusContentColor); + ani_boolean aniStatusIcon; + env->Object_GetPropertyByName_Boolean(aniObject, "isStatusBarLightIcon", &aniStatusIcon); + properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ = GetColorFromAni( + env, + "statusBarContentColor", + statusProperty.contentColor_, + propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].contentColorFlag, + aniObject); + if (aniStatusIcon) { + properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ = SYSTEM_COLOR_WHITE; + } else { + properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ = SYSTEM_COLOR_BLACK; + } + propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].contentColorFlag = true; + return true; +} + +bool AniWindowUtils::SetWindowNavigationBarContentColor(ani_env* env, + ani_object aniObject, + std::map& properties, + std::map& propertyFlags) +{ + auto navProperty = properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR]; + ani_ref aniNaviGationBarColor; + env->Object_GetPropertyByName_Ref(aniObject, "navigationBarContentColor", &aniNaviGationBarColor); + ani_boolean aniNavigationIcon; + env->Object_GetPropertyByName_Boolean(aniObject, "isNavigationBarLightIcon", &aniNavigationIcon); + properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = GetColorFromAni( + env, + "navigationBarContentColor", + navProperty.contentColor_, + propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColorFlag, + aniObject); + if (aniNavigationIcon) { + properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = SYSTEM_COLOR_WHITE; + } else { + properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = SYSTEM_COLOR_BLACK; + } + propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColorFlag = true; + return true; +} + +bool AniWindowUtils::SetDecorButtonStyleFromAni(ani_env* env, DecorButtonStyle& decorButtonStyle, + const ani_object& decorStyle) +{ + int32_t colorMode; + bool emptyParam = true; + if (ANI_OK == env->Object_GetPropertyByName_Int(decorStyle, "colorMode", &colorMode)) { + decorButtonStyle.colorMode = colorMode; + emptyParam = false; + } + int32_t buttonBackgroundSize; + if (ANI_OK == env->Object_GetPropertyByName_Int(decorStyle, "buttonBackgroundSize", &buttonBackgroundSize)) { + decorButtonStyle.buttonBackgroundSize = buttonBackgroundSize; + emptyParam = false; + } + int32_t spacingBetweenButtons; + if (ANI_OK == env->Object_GetPropertyByName_Int(decorStyle, "spacingBetweenButtons", &spacingBetweenButtons)) { + decorButtonStyle.spacingBetweenButtons = spacingBetweenButtons; + emptyParam = false; + } + int32_t closeButtonRightMargin; + if (ANI_OK == env->Object_GetPropertyByName_Int(decorStyle, "closeButtonRightMargin", &closeButtonRightMargin)) { + decorButtonStyle.closeButtonRightMargin = closeButtonRightMargin; + emptyParam = false; + } + return !emptyParam; +} + +bool AniWindowUtils::SetSystemBarPropertiesFromAni(ani_env* env, + std::map& windowBarProperties, + std::map& windowPropertyFlags, + const ani_object& aniProperties, + const sptr& window) +{ + auto statusProperty = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR); + auto navProperty = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_NAVIGATION_BAR); + windowBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR] = statusProperty; + windowBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = navProperty; + windowPropertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR] = SystemBarPropertyFlag(); + windowPropertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = SystemBarPropertyFlag(); + + // statusBarColor + uint32_t aniStatusBarColor = GetColorFromAni( + env, + "statusBarColor", + statusProperty.backgroundColor_, + windowPropertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].backgroundColorFlag, + aniProperties); + windowBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR].backgroundColor_ = aniStatusBarColor; + + // navigationBarColor + uint32_t aniNaviGationBarColor = GetColorFromAni( + env, + "navigationBarColor", + navProperty.backgroundColor_, + windowPropertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].backgroundColorFlag, + aniProperties); + windowBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].backgroundColor_ = aniNaviGationBarColor; + + // windowStatusBarContentColor + if (!SetWindowStatusBarContentColor(env, aniProperties, windowBarProperties, windowPropertyFlags) || + !SetWindowNavigationBarContentColor(env, aniProperties, windowBarProperties, windowPropertyFlags)) { + return false; + } + + ani_boolean aniEnableStatusBarAnimation; + env->Object_GetPropertyByName_Boolean(aniProperties, "enableStatusBarAnimation", &aniEnableStatusBarAnimation); + if (static_cast(aniEnableStatusBarAnimation)) { + windowBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR].enableAnimation_ = aniEnableStatusBarAnimation; + windowPropertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].enableAnimationFlag = true; + } + + ani_boolean aniEnableNavigationBarAnimation; + env->Object_GetPropertyByName_Boolean(aniProperties, "enableNavigationBarAnimation", + &aniEnableNavigationBarAnimation); + if (static_cast(aniEnableNavigationBarAnimation)) { + windowBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enableAnimation_ = aniEnableNavigationBarAnimation; + windowPropertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enableAnimationFlag = true; + } + return true; +} + +void AniWindowUtils::GetSystemBarPropertiesFromAni(sptr& window, + std::map& newProperties, + std::map& newPropertyFlags, + std::map& properties, + std::map& propertyFlags) +{ + for (auto type : {WindowType::WINDOW_TYPE_STATUS_BAR, WindowType::WINDOW_TYPE_NAVIGATION_BAR}) { + auto property = window->GetSystemBarPropertyByType(type); + properties[type] = property; + propertyFlags[type] = SystemBarPropertyFlag(); + + properties[type].backgroundColor_ = newProperties[type].backgroundColor_; + properties[type].contentColor_ = newProperties[type].contentColor_; + properties[type].enableAnimation_ = newProperties[type].enableAnimation_; + propertyFlags[type].backgroundColorFlag = newPropertyFlags[type].backgroundColorFlag; + propertyFlags[type].contentColorFlag = newPropertyFlags[type].contentColorFlag; + propertyFlags[type].enableAnimationFlag = newPropertyFlags[type].enableAnimationFlag; + } +} + +void AniWindowUtils::UpdateSystemBarProperties(std::map& systemBarProperties, + const std::map& systemBarPropertyFlags, sptr windowToken) +{ + for (auto it : systemBarPropertyFlags) { + WindowType type = it.first; + SystemBarPropertyFlag flag = it.second; + auto property = windowToken->GetSystemBarPropertyByType(type); + if (flag.enableFlag == false) { + systemBarProperties[type].enable_ = property.enable_; + } + if (flag.backgroundColorFlag == false) { + systemBarProperties[type].backgroundColor_ = property.backgroundColor_; + } + if (flag.contentColorFlag == false) { + systemBarProperties[type].contentColor_ = property.contentColor_; + } + if (flag.enableAnimationFlag == false) { + systemBarProperties[type].enableAnimation_ = property.enableAnimation_; + } + if (flag.enableFlag == true) { + systemBarProperties[type].settingFlag_ = + static_cast(static_cast(property.settingFlag_) | + static_cast(SystemBarSettingFlag::ENABLE_SETTING)); + } + if (flag.backgroundColorFlag == true || flag.contentColorFlag == true) { + systemBarProperties[type].settingFlag_ = + static_cast(static_cast(property.settingFlag_) | + static_cast(SystemBarSettingFlag::COLOR_SETTING)); + } + } + + return; +} + +bool AniWindowUtils::SetSpecificSystemBarEnabled(ani_env* env, + std::map& systemBarProperties, + ani_string aniName, + ani_boolean aniEnable, + ani_boolean aniEnableAnimation) +{ + std::string barName; + GetStdString(env, aniName, barName); + bool enable = static_cast(aniEnable); + bool enableAnimation = static_cast(aniEnableAnimation); + + if (barName.compare("status") == 0) { + systemBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR].enable_ = enable; + systemBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR].enableAnimation_ = enableAnimation; + } else if (barName.compare("navigation") == 0) { + systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enable_ = enable; + systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enableAnimation_ = enableAnimation; + } else if (barName.compare("navigationIndicator") == 0) { + systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR].enable_ = enable; + systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR].enableAnimation_ = enableAnimation; + } + return true; +} + +void* AniWindowUtils::GetAbilityContext(ani_env *env, ani_object aniObj) +{ + ani_long nativeContextLong; + ani_class cls = nullptr; + ani_field contextField = nullptr; + ani_status status = ANI_ERROR; + if ((status = env->FindClass("Lapplication/UIAbilityContext/UIAbilityContext;", &cls)) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] find class fail, status : %{public}d", status); + return nullptr; + } + if ((status = env->Class_FindField(cls, "nativeContext", &contextField)) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] find field fail, status : %{public}d", status); + return nullptr; + } + if ((status = env->Object_GetField_Long(aniObj, contextField, &nativeContextLong)) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] get field fail, status : %{public}d", status); + return nullptr; + } + return (void*)nativeContextLong; +} + +void AniWindowUtils::GetSpecificBarStatus(sptr& window, const std::string& name, + std::map& newSystemBarProperties, + std::map& systemBarProperties) +{ + auto type = (name.compare("status") == 0) ? WindowType::WINDOW_TYPE_STATUS_BAR : + (name.compare("navigation") == 0) ? WindowType::WINDOW_TYPE_NAVIGATION_BAR : + WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR; + auto property = window->GetSystemBarPropertyByType(type); + systemBarProperties[type] = property; + systemBarProperties[type].enable_ = newSystemBarProperties[type].enable_; + systemBarProperties[type].enableAnimation_ = newSystemBarProperties[type].enableAnimation_; + systemBarProperties[type].settingFlag_ = systemBarProperties[type].settingFlag_ | + SystemBarSettingFlag::ENABLE_SETTING; +} +} // namespace Rosen +} // namespace OHOS diff --git a/previewer/mock/ani_window_utils.h b/previewer/mock/ani_window_utils.h new file mode 100644 index 0000000000..a3488141fa --- /dev/null +++ b/previewer/mock/ani_window_utils.h @@ -0,0 +1,355 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_JS_WINDOW_UTILS_H +#define OHOS_JS_WINDOW_UTILS_H +#include +#include "ani.h" +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" +#include "window.h" + +#ifndef WINDOW_PREVIEW +#include "window_manager.h" +#else +#include "mock/window_manager.h" +#endif + +#include "window_option.h" +#include "window_visibility_info.h" +#include "wm_common.h" + +namespace OHOS { +namespace Rosen { +constexpr int32_t RGB_LENGTH = 6; +constexpr int32_t RGBA_LENGTH = 8; +constexpr Rect g_emptyRect = {0, 0, 0, 0}; + +enum class ApiWindowType : uint32_t { + TYPE_BASE, + TYPE_APP = TYPE_BASE, + TYPE_SYSTEM_ALERT, + TYPE_INPUT_METHOD, + TYPE_STATUS_BAR, + TYPE_PANEL, + TYPE_KEYGUARD, + TYPE_VOLUME_OVERLAY, + TYPE_NAVIGATION_BAR, + TYPE_FLOAT, + TYPE_WALLPAPER, + TYPE_DESKTOP, + TYPE_LAUNCHER_RECENT, + TYPE_LAUNCHER_DOCK, + TYPE_VOICE_INTERACTION, + TYPE_POINTER, + TYPE_FLOAT_CAMERA, + TYPE_DIALOG, + TYPE_SCREENSHOT, + TYPE_SYSTEM_TOAST, + TYPE_DIVIDER, + TYPE_GLOBAL_SEARCH, + TYPE_HANDWRITE, + TYPE_END +}; + +enum class LifeCycleEventType : uint32_t { + FOREGROUND = 1, + ACTIVE, + INACTIVE, + BACKGROUND, + RESUMED, + PAUSED, + DESTROYED, +}; + +const std::map NATIVE_JS_TO_WINDOW_TYPE_MAP { + { WindowType::WINDOW_TYPE_APP_SUB_WINDOW, ApiWindowType::TYPE_APP }, + { WindowType::WINDOW_TYPE_DIALOG, ApiWindowType::TYPE_DIALOG }, + { WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW, ApiWindowType::TYPE_SYSTEM_ALERT }, + { WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT, ApiWindowType::TYPE_INPUT_METHOD }, + { WindowType::WINDOW_TYPE_STATUS_BAR, ApiWindowType::TYPE_STATUS_BAR }, + { WindowType::WINDOW_TYPE_PANEL, ApiWindowType::TYPE_PANEL }, + { WindowType::WINDOW_TYPE_KEYGUARD, ApiWindowType::TYPE_KEYGUARD }, + { WindowType::WINDOW_TYPE_VOLUME_OVERLAY, ApiWindowType::TYPE_VOLUME_OVERLAY }, + { WindowType::WINDOW_TYPE_NAVIGATION_BAR, ApiWindowType::TYPE_NAVIGATION_BAR }, + { WindowType::WINDOW_TYPE_FLOAT, ApiWindowType::TYPE_FLOAT }, + { WindowType::WINDOW_TYPE_FLOAT_CAMERA, ApiWindowType::TYPE_FLOAT_CAMERA }, + { WindowType::WINDOW_TYPE_WALLPAPER, ApiWindowType::TYPE_WALLPAPER }, + { WindowType::WINDOW_TYPE_DESKTOP, ApiWindowType::TYPE_DESKTOP }, + { WindowType::WINDOW_TYPE_LAUNCHER_RECENT, ApiWindowType::TYPE_LAUNCHER_RECENT }, + { WindowType::WINDOW_TYPE_LAUNCHER_DOCK, ApiWindowType::TYPE_LAUNCHER_DOCK }, + { WindowType::WINDOW_TYPE_VOICE_INTERACTION, ApiWindowType::TYPE_VOICE_INTERACTION }, + { WindowType::WINDOW_TYPE_POINTER, ApiWindowType::TYPE_POINTER }, + { WindowType::WINDOW_TYPE_SCREENSHOT, ApiWindowType::TYPE_SCREENSHOT }, + { WindowType::WINDOW_TYPE_SYSTEM_TOAST, ApiWindowType::TYPE_SYSTEM_TOAST }, + { WindowType::WINDOW_TYPE_DOCK_SLICE, ApiWindowType::TYPE_DIVIDER }, + { WindowType::WINDOW_TYPE_GLOBAL_SEARCH, ApiWindowType::TYPE_GLOBAL_SEARCH }, + { WindowType::WINDOW_TYPE_HANDWRITE, ApiWindowType::TYPE_HANDWRITE }, +}; + +const std::map JS_TO_NATIVE_WINDOW_TYPE_MAP { + { ApiWindowType::TYPE_APP, WindowType::WINDOW_TYPE_APP_SUB_WINDOW }, + { ApiWindowType::TYPE_DIALOG, WindowType::WINDOW_TYPE_DIALOG }, + { ApiWindowType::TYPE_SYSTEM_ALERT, WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW }, + { ApiWindowType::TYPE_INPUT_METHOD, WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT }, + { ApiWindowType::TYPE_STATUS_BAR, WindowType::WINDOW_TYPE_STATUS_BAR }, + { ApiWindowType::TYPE_PANEL, WindowType::WINDOW_TYPE_PANEL }, + { ApiWindowType::TYPE_KEYGUARD, WindowType::WINDOW_TYPE_KEYGUARD }, + { ApiWindowType::TYPE_VOLUME_OVERLAY, WindowType::WINDOW_TYPE_VOLUME_OVERLAY }, + { ApiWindowType::TYPE_NAVIGATION_BAR, WindowType::WINDOW_TYPE_NAVIGATION_BAR }, + { ApiWindowType::TYPE_FLOAT, WindowType::WINDOW_TYPE_FLOAT }, + { ApiWindowType::TYPE_FLOAT_CAMERA, WindowType::WINDOW_TYPE_FLOAT_CAMERA }, + { ApiWindowType::TYPE_WALLPAPER, WindowType::WINDOW_TYPE_WALLPAPER }, + { ApiWindowType::TYPE_DESKTOP, WindowType::WINDOW_TYPE_DESKTOP }, + { ApiWindowType::TYPE_LAUNCHER_RECENT, WindowType::WINDOW_TYPE_LAUNCHER_RECENT }, + { ApiWindowType::TYPE_LAUNCHER_DOCK, WindowType::WINDOW_TYPE_LAUNCHER_DOCK }, + { ApiWindowType::TYPE_VOICE_INTERACTION, WindowType::WINDOW_TYPE_VOICE_INTERACTION }, + { ApiWindowType::TYPE_POINTER, WindowType::WINDOW_TYPE_POINTER }, + { ApiWindowType::TYPE_SCREENSHOT, WindowType::WINDOW_TYPE_SCREENSHOT }, + { ApiWindowType::TYPE_SYSTEM_TOAST, WindowType::WINDOW_TYPE_SYSTEM_TOAST }, + { ApiWindowType::TYPE_DIVIDER, WindowType::WINDOW_TYPE_DOCK_SLICE }, + { ApiWindowType::TYPE_GLOBAL_SEARCH, WindowType::WINDOW_TYPE_GLOBAL_SEARCH }, + { ApiWindowType::TYPE_HANDWRITE, WindowType::WINDOW_TYPE_HANDWRITE }, +}; + +enum class ApiWindowMode : uint32_t { + UNDEFINED = 1, + FULLSCREEN, + PRIMARY, + SECONDARY, + FLOATING, + MODE_END = FLOATING +}; + +const std::map NATIVE_TO_JS_WINDOW_MODE_MAP { + { WindowMode::WINDOW_MODE_UNDEFINED, ApiWindowMode::UNDEFINED }, + { WindowMode::WINDOW_MODE_FULLSCREEN, ApiWindowMode::FULLSCREEN }, + { WindowMode::WINDOW_MODE_SPLIT_PRIMARY, ApiWindowMode::PRIMARY }, + { WindowMode::WINDOW_MODE_SPLIT_SECONDARY, ApiWindowMode::SECONDARY }, + { WindowMode::WINDOW_MODE_FLOATING, ApiWindowMode::FLOATING }, +}; + +const std::map JS_TO_NATIVE_WINDOW_MODE_MAP { + {ApiWindowMode::UNDEFINED, WindowMode::WINDOW_MODE_UNDEFINED }, + {ApiWindowMode::FULLSCREEN, WindowMode::WINDOW_MODE_FULLSCREEN }, + {ApiWindowMode::PRIMARY, WindowMode::WINDOW_MODE_SPLIT_PRIMARY }, + {ApiWindowMode::SECONDARY, WindowMode::WINDOW_MODE_SPLIT_SECONDARY }, + {ApiWindowMode::FLOATING, WindowMode::WINDOW_MODE_FLOATING }, +}; + +enum class ApiOrientation : uint32_t { + BEGIN = 0, + UNSPECIFIED = BEGIN, + PORTRAIT = 1, + LANDSCAPE = 2, + PORTRAIT_INVERTED = 3, + LANDSCAPE_INVERTED = 4, + AUTO_ROTATION = 5, + AUTO_ROTATION_PORTRAIT = 6, + AUTO_ROTATION_LANDSCAPE = 7, + AUTO_ROTATION_RESTRICTED = 8, + AUTO_ROTATION_PORTRAIT_RESTRICTED = 9, + AUTO_ROTATION_LANDSCAPE_RESTRICTED = 10, + LOCKED = 11, + AUTO_ROTATION_UNSPECIFIED = 12, + USER_ROTATION_PORTRAIT = 13, + USER_ROTATION_LANDSCAPE = 14, + USER_ROTATION_PORTRAIT_INVERTED = 15, + USER_ROTATION_LANDSCAPE_INVERTED = 16, + FOLLOW_DESKTOP = 17, + END = FOLLOW_DESKTOP, +}; + +const std::map JS_TO_NATIVE_ORIENTATION_MAP { + {ApiOrientation::UNSPECIFIED, Orientation::UNSPECIFIED }, + {ApiOrientation::PORTRAIT, Orientation::VERTICAL }, + {ApiOrientation::LANDSCAPE, Orientation::HORIZONTAL }, + {ApiOrientation::PORTRAIT_INVERTED, Orientation::REVERSE_VERTICAL }, + {ApiOrientation::LANDSCAPE_INVERTED, Orientation::REVERSE_HORIZONTAL }, + {ApiOrientation::AUTO_ROTATION, Orientation::SENSOR }, + {ApiOrientation::AUTO_ROTATION_PORTRAIT, Orientation::SENSOR_VERTICAL }, + {ApiOrientation::AUTO_ROTATION_LANDSCAPE, Orientation::SENSOR_HORIZONTAL }, + {ApiOrientation::AUTO_ROTATION_RESTRICTED, Orientation::AUTO_ROTATION_RESTRICTED }, + {ApiOrientation::AUTO_ROTATION_PORTRAIT_RESTRICTED, Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED }, + {ApiOrientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED, Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED }, + {ApiOrientation::LOCKED, Orientation::LOCKED }, + {ApiOrientation::AUTO_ROTATION_UNSPECIFIED, Orientation::AUTO_ROTATION_UNSPECIFIED }, + {ApiOrientation::USER_ROTATION_PORTRAIT, Orientation::USER_ROTATION_PORTRAIT }, + {ApiOrientation::USER_ROTATION_LANDSCAPE, Orientation::USER_ROTATION_LANDSCAPE }, + {ApiOrientation::USER_ROTATION_PORTRAIT_INVERTED, Orientation::USER_ROTATION_PORTRAIT_INVERTED }, + {ApiOrientation::USER_ROTATION_LANDSCAPE_INVERTED, Orientation::USER_ROTATION_LANDSCAPE_INVERTED }, + {ApiOrientation::FOLLOW_DESKTOP, Orientation::FOLLOW_DESKTOP }, +}; + +const std::map NATIVE_TO_JS_ORIENTATION_MAP { + {Orientation::UNSPECIFIED, ApiOrientation::UNSPECIFIED }, + {Orientation::VERTICAL, ApiOrientation::PORTRAIT }, + {Orientation::HORIZONTAL, ApiOrientation::LANDSCAPE }, + {Orientation::REVERSE_VERTICAL, ApiOrientation::PORTRAIT_INVERTED }, + {Orientation::REVERSE_HORIZONTAL, ApiOrientation::LANDSCAPE_INVERTED }, + {Orientation::SENSOR, ApiOrientation::AUTO_ROTATION }, + {Orientation::SENSOR_VERTICAL, ApiOrientation::AUTO_ROTATION_PORTRAIT }, + {Orientation::SENSOR_HORIZONTAL, ApiOrientation::AUTO_ROTATION_LANDSCAPE }, + {Orientation::AUTO_ROTATION_RESTRICTED, ApiOrientation::AUTO_ROTATION_RESTRICTED }, + {Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED, ApiOrientation::AUTO_ROTATION_PORTRAIT_RESTRICTED }, + {Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED, ApiOrientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED }, + {Orientation::LOCKED, ApiOrientation::LOCKED }, + {Orientation::FOLLOW_RECENT, ApiOrientation::UNSPECIFIED }, + {Orientation::AUTO_ROTATION_UNSPECIFIED, ApiOrientation::AUTO_ROTATION_UNSPECIFIED }, + {Orientation::USER_ROTATION_PORTRAIT, ApiOrientation::USER_ROTATION_PORTRAIT }, + {Orientation::USER_ROTATION_LANDSCAPE, ApiOrientation::USER_ROTATION_LANDSCAPE }, + {Orientation::USER_ROTATION_PORTRAIT_INVERTED, ApiOrientation::USER_ROTATION_PORTRAIT_INVERTED }, + {Orientation::USER_ROTATION_LANDSCAPE_INVERTED, ApiOrientation::USER_ROTATION_LANDSCAPE_INVERTED }, + {Orientation::FOLLOW_DESKTOP, ApiOrientation::FOLLOW_DESKTOP }, +}; + +enum class RectChangeReason : uint32_t { + UNDEFINED = 0, + MAXIMIZE, + RECOVER, + MOVE, + DRAG, + DRAG_START, + DRAG_END, +}; + +const std::map JS_SIZE_CHANGE_REASON { + { WindowSizeChangeReason::UNDEFINED, RectChangeReason::UNDEFINED }, + { WindowSizeChangeReason::MAXIMIZE, RectChangeReason::MAXIMIZE }, + { WindowSizeChangeReason::RECOVER, RectChangeReason::RECOVER }, + { WindowSizeChangeReason::ROTATION, RectChangeReason::UNDEFINED }, + { WindowSizeChangeReason::DRAG, RectChangeReason::DRAG }, + { WindowSizeChangeReason::DRAG_START, RectChangeReason::DRAG_START }, + { WindowSizeChangeReason::DRAG_END, RectChangeReason::DRAG_END }, + { WindowSizeChangeReason::RESIZE, RectChangeReason::UNDEFINED }, + { WindowSizeChangeReason::MOVE, RectChangeReason::MOVE }, + { WindowSizeChangeReason::HIDE, RectChangeReason::UNDEFINED }, + { WindowSizeChangeReason::TRANSFORM, RectChangeReason::UNDEFINED }, + { WindowSizeChangeReason::CUSTOM_ANIMATION_SHOW, RectChangeReason::UNDEFINED }, + { WindowSizeChangeReason::FULL_TO_SPLIT, RectChangeReason::UNDEFINED }, + { WindowSizeChangeReason::SPLIT_TO_FULL, RectChangeReason::UNDEFINED }, + { WindowSizeChangeReason::FULL_TO_FLOATING, RectChangeReason::UNDEFINED }, + { WindowSizeChangeReason::FLOATING_TO_FULL, RectChangeReason::UNDEFINED }, + { WindowSizeChangeReason::END, RectChangeReason::UNDEFINED }, +}; + +enum class ApiModalityType : uint32_t { + BEGIN = 0, + WINDOW_MODALITY = BEGIN, + APPLICATION_MODALITY, + END = APPLICATION_MODALITY, +}; + +inline const std::map JS_TO_NATIVE_MODALITY_TYPE_MAP { + { ApiModalityType::WINDOW_MODALITY, ModalityType::WINDOW_MODALITY }, + { ApiModalityType::APPLICATION_MODALITY, ModalityType::APPLICATION_MODALITY }, +}; + +class AniWindowUtils { +public: + static ani_status GetStdString(ani_env* env, ani_string ani_str, std::string& result); + static ani_status GetStdStringVector(ani_env* env, ani_object ary, std::vector& result); + static ani_status GetPropertyIntObject(ani_env* env, const char* propertyName, ani_object object, int32_t& result); + static ani_status GetPropertyDoubleObject(ani_env* env, const char* propertyName, + ani_object object, double& result); + static ani_status GetDoubleObject(ani_env* env, ani_object double_object, double& result); + static ani_status NewAniObjectNoParams(ani_env* env, const char* cls, ani_object* object); + static ani_status NewAniObject(ani_env* env, const char* cls, const char* signature, ani_object* result, ...); + static ani_object CreateAniUndefined(ani_env* env); + static ani_object AniThrowError(ani_env* env, WMError errorCode, std::string msg = ""); + static ani_object AniThrowError(ani_env* env, WmErrorCode errorCode, std::string msg = ""); + static ani_object CreateAniDecorButtonStyle(ani_env* env, const DecorButtonStyle& decorButtonStyle); + static ani_object CreateAniTitleButtonRect(ani_env* env, const TitleButtonRect& titleButtonRect); + static ani_object CreateAniWindowArray(ani_env* env, std::vector& windows); + static ani_object CreateAniSize(ani_env* env, int32_t width, int32_t height); + static ani_object CreateAniRect(ani_env* env, const Rect& rect); + static ani_object CreateAniAvoidArea(ani_env* env, const AvoidArea& avoidArea, AvoidAreaType type); + static ani_object CreateAniSystemBarTintState(ani_env* env, DisplayId displayId, const SystemBarRegionTints& tints); + static ani_object CreateAniSystemBarRegionTint(ani_env* env, const SystemBarRegionTint& tint); + static ani_status CallAniFunctionVoid(ani_env *env, const char* ns, const char* func, const char* signature, ...); + static ani_status CallAniMethodVoid(ani_env* env, ani_object object, const char* cls, + const char* method, const char* signature, ...); + static ani_status CallAniMethodVoid(ani_env* env, ani_object object, ani_class cls, + const char* method, const char* signature, ...); + static ani_status GetAniString(ani_env* env, const std::string& str, ani_string* result); + static void* GetAbilityContext(ani_env *env, ani_object aniObj); + static ani_object CreateWindowsProperties(ani_env* env, const sptr& window); + static ani_object CreateProperties(ani_env* env, const sptr& window); + static uint32_t GetColorFromAni(ani_env* env, const char* name, + uint32_t defaultColor, bool& flag, const ani_object& aniObject); + static bool SetWindowStatusBarContentColor(ani_env* env, ani_object aniObject, + std::map& properties, std::map& + propertyFlags); + static bool SetWindowNavigationBarContentColor(ani_env* env, ani_object aniObject, + std::map& properties, std::map& + propertyFlags); + static bool SetSystemBarPropertiesFromAni(ani_env* env, + std::map& windowBarProperties, + std::map& windowPropertyFlags, + const ani_object& aniProperties, + const sptr& window); + static bool SetDecorButtonStyleFromAni(ani_env* env, DecorButtonStyle& decorButtonStyle, + const ani_object& decorStyle); + static void GetSystemBarPropertiesFromAni(sptr& window, + std::map& newProperties, + std::map& newPropertyFlags, + std::map& properties, + std::map& propertyFlags); + static void UpdateSystemBarProperties(std::map& systemBarProperties, + const std::map& systemBarPropertyFlags, sptr windowToken); + static bool SetSpecificSystemBarEnabled(ani_env* env, + std::map& systemBarProperties, + ani_string aniName, + ani_boolean aniEnable, + ani_boolean aniEnableAnimation); + static void GetSpecificBarStatus(sptr& window, const std::string& name, + std::map& newSystemBarProperties, + std::map& systemBarProperties); + +private: + static void SetSystemPropertiesWindowRect(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertiesDrawableRect(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertiesWindowType(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertiesWindowIsLayoutFullScreen(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertiesWindowIsFullScreen(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertiesWindowTouchable(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertiesWindowFousable(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertiesWindowIsPrivacyMode(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertiesWindowIsKeepScreenOn(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertiesWindowBrightness(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertiesWindowIsTransparent(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertieswindowIsRoundCorner(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertiesWindowDimBehindValue(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertieswindowId(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); + static void SetSystemPropertiesdisplayId(ani_env* env, const sptr& window, + ani_object& systemProperties, const char* clsName); +}; +} +} +#endif \ No newline at end of file diff --git a/previewer/mock/pixel_map_ani.cpp b/previewer/mock/pixel_map_ani.cpp new file mode 100644 index 0000000000..e3077553da --- /dev/null +++ b/previewer/mock/pixel_map_ani.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "pixel_map_ani.h" + +namespace OHOS { +namespace Media { +using namespace std; + +ani_object PixelMapAni::CreatePixelMap([[maybe_unused]] ani_env* env, std::shared_ptr pixelMap) +{ + return ani_object(); +} + +ani_object PixelMapAni::CreatePixelMapAni([[maybe_unused]] ani_env* env, ani_object obj) +{ + return ani_object(); +} + +ani_status PixelMapAni::Init(ani_env* env) +{ + return ANI_OK; +} +} // Media +} // OHOS \ No newline at end of file diff --git a/previewer/mock/pixel_map_ani.h b/previewer/mock/pixel_map_ani.h new file mode 100644 index 0000000000..17526cde45 --- /dev/null +++ b/previewer/mock/pixel_map_ani.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANI_SRC_INCLUDE_PIXEL_MAP_ANI_H +#define ANI_SRC_INCLUDE_PIXEL_MAP_ANI_H + +#include + +#include +#include "pixel_map.h" + +namespace OHOS { +namespace Media { + +class PixelMapAni { +public: + static ani_object CreatePixelMapAni([[maybe_unused]] ani_env* env, ani_object obj); + static ani_object CreatePixelMap([[maybe_unused]] ani_env* env, std::shared_ptr pixelMap); + static ani_status Init(ani_env* env); +}; + +} // namespace Media +} // namespace OHOS + +#endif // ANI_SRC_INCLUDE_PIXEL_MAP_ANI_H \ No newline at end of file diff --git a/previewer/mock/window_manager_ani/ani_window_manager.cpp b/previewer/mock/window_manager_ani/ani_window_manager.cpp new file mode 100644 index 0000000000..dcc2e313bf --- /dev/null +++ b/previewer/mock/window_manager_ani/ani_window_manager.cpp @@ -0,0 +1,223 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ani_window_manager.h" + +#include "ani.h" +#include "ani_window.h" +#include "ani_window_stage.h" +#include "ani_window.h" +#include "ani_window_stage.h" +#include "ani_window_utils.h" +#include "singleton_container.h" +#include "window_manager.h" +#include "window_manager_hilog.h" +#include "window_scene.h" +#include "ani_window_utils.h" + +namespace OHOS { +namespace Rosen { +namespace { +const std::string PIP_WINDOW = "pip_window"; +} +AniWindowManager::AniWindowManager() : registerManager_(std::make_unique()) +{ +} + +ani_status AniWindowManager::AniWindowManagerInit(ani_env* env) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + ani_namespace ns; + ani_status ret; + if ((ret = env->FindNamespace("L@ohos/window/window;", &ns)) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] find ns %{public}u", ret); + return ANI_NOT_FOUND; + } + std::array functions = { + ani_native_function {"CreateWindowStage", "J:L@ohos/window/window/WindowStageInternal;", + reinterpret_cast(AniWindowManager::WindowStageCreate)}, + ani_native_function {"getLastWindowSync", + "JLapplication/BaseContext/BaseContext;:L@ohos/window/window/Window;", + reinterpret_cast(AniWindowManager::GetLastWindow)}, + ani_native_function {"findWindowSync", + "JLstd/core/String;:L@ohos/window/window/Window;", + reinterpret_cast(AniWindowManager::FindWindow)}, + ani_native_function {"minimizeAllSync", "JD:V", reinterpret_cast(AniWindowManager::MinimizeAll)}, + ani_native_function {"shiftAppWindowFocusSync", "JDD:V", + reinterpret_cast(AniWindowManager::ShiftAppWindowFocus)}, + ani_native_function {"onSync", nullptr, + reinterpret_cast(AniWindowManager::RegisterWindowManagerCallback)}, + ani_native_function {"offSync", nullptr, + reinterpret_cast(AniWindowManager::UnregisterWindowManagerCallback)}, + ani_native_function {"windowDestroyCallback", nullptr, reinterpret_cast(AniWindow::Finalizer)}, + }; + if ((ret = env->Namespace_BindNativeFunctions(ns, functions.data(), functions.size())) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] bind ns func %{public}u", ret); + return ANI_NOT_FOUND; + } + + ani_function setObjFunc = nullptr; + ret = env->Namespace_FindFunction(ns, "setNativeObj", "J:V", &setObjFunc); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] find setNativeObj func fail %{public}u", ret); + return ret; + } + std::unique_ptr aniWinManager = std::make_unique(); + ret = env->Function_Call_Void(setObjFunc, aniWinManager.release()); + if (ret != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] call setNativeObj func fail %{public}u", ret); + return ret; + } + return ret; +} + +ani_object AniWindowManager::WindowStageCreate(ani_env* env, ani_long scene) +{ + TLOGD(WmsLogTag::DEFAULT, "[ANI] create windowstage with scene 0x%{public}p %{public}d", + reinterpret_cast(env), (int32_t)scene); + std::shared_ptr scenePtr; + return CreateAniWindowStage(env, scenePtr); +} + +ani_ref AniWindowManager::GetLastWindow(ani_env* env, ani_long nativeObj, ani_object context) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + AniWindowManager* aniWindowManager = reinterpret_cast(nativeObj); + return aniWindowManager != nullptr ? aniWindowManager->OnGetLastWindow(env, context) : nullptr; +} + +ani_ref AniWindowManager::OnGetLastWindow(ani_env* env, ani_object aniContext) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + auto contextPtr = AniWindowUtils::GetAbilityContext(env, aniContext); + TLOGI(WmsLogTag::DEFAULT, "[ANI] nativeContextLong : %{public}p", contextPtr); + auto context = static_cast*>(contextPtr); + if (context == nullptr) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] context is nullptr"); + return AniWindowUtils::AniThrowError(env, WMError::WM_ERROR_NULLPTR, "Stage mode without context"); + } + auto window = Window::GetTopWindowWithContext(context->lock()); + if (window == nullptr || window->GetWindowState() == WindowState::STATE_DESTROYED) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] window is nullptr or destroyed"); + return AniWindowUtils::AniThrowError(env, WMError::WM_ERROR_NULLPTR, "Get top window failed"); + } + return CreateAniWindowObject(env, window); +} + +ani_ref AniWindowManager::FindWindow(ani_env* env, ani_long nativeObj, ani_string windowName) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + AniWindowManager* aniWindowManager = reinterpret_cast(nativeObj); + return aniWindowManager != nullptr ? aniWindowManager->OnFindWindow(env, windowName) : nullptr; +} + +ani_ref AniWindowManager::OnFindWindow(ani_env* env, ani_string windowName) +{ + std::string name; + AniWindowUtils::GetStdString(env, windowName, name); + TLOGI(WmsLogTag::DEFAULT, "[ANI]Window name=%{public}s", name.c_str()); + if (name.compare(PIP_WINDOW) == 0) { + return AniWindowUtils::AniThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM); + } + ani_ref aniWindowObj = FindAniWindowObject(name); + if (aniWindowObj != nullptr) { + TLOGD(WmsLogTag::DEFAULT, "[ANI]Find window: %{public}s, use exist js window", name.c_str()); + return aniWindowObj; + } else { + sptr window = Window::Find(name); + if (window == nullptr) { + return AniWindowUtils::AniThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY); + } else { + return CreateAniWindowObject(env, window); + } + } +} + +void AniWindowManager::MinimizeAll(ani_env* env, ani_long nativeObj, ani_double displayId) +{ + TLOGI(WmsLogTag::WMS_LIFE, "[ANI]"); + AniWindowManager* aniWindowManager = reinterpret_cast(nativeObj); + if (aniWindowManager != nullptr) { + aniWindowManager->OnMinimizeAll(env, displayId); + } else { + TLOGE(WmsLogTag::WMS_LIFE, "[ANI] aniWindowManager is nullptr"); + } +} + +void AniWindowManager::OnMinimizeAll(ani_env* env, ani_double displayId) +{ +} + +void AniWindowManager::RegisterWindowManagerCallback(ani_env* env, ani_long nativeObj, + ani_string type, ani_ref callback) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + AniWindowManager* aniWindowManager = reinterpret_cast(nativeObj); + if (aniWindowManager != nullptr) { + aniWindowManager->OnRegisterWindowManagerCallback(env, type, callback); + } else { + TLOGE(WmsLogTag::DEFAULT, "[ANI] aniWindowManager is nullptr"); + } +} + +void AniWindowManager::OnRegisterWindowManagerCallback(ani_env* env, ani_string type, ani_ref callback) +{ + std::string cbType; + AniWindowUtils::GetStdString(env, type, cbType); + TLOGI(WmsLogTag::DEFAULT, "[ANI] type:%{public}s", cbType.c_str()); + WmErrorCode ret = registerManager_->RegisterListener(nullptr, cbType, CaseType::CASE_WINDOW_MANAGER, + env, callback, ani_double(0)); + if (ret != WmErrorCode::WM_OK) { + AniWindowUtils::AniThrowError(env, ret); + } +} + +void AniWindowManager::UnregisterWindowManagerCallback(ani_env* env, ani_long nativeObj, + ani_string type, ani_ref callback) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + AniWindowManager* aniWindowManager = reinterpret_cast(nativeObj); + if (aniWindowManager != nullptr) { + aniWindowManager->OnUnregisterWindowManagerCallback(env, type, callback); + } else { + TLOGE(WmsLogTag::DEFAULT, "[ANI] aniWindowManager is nullptr"); + } +} + +void AniWindowManager::OnUnregisterWindowManagerCallback(ani_env* env, ani_string type, ani_ref callback) +{ + std::string cbType; + AniWindowUtils::GetStdString(env, type, cbType); + TLOGI(WmsLogTag::DEFAULT, "[ANI] type:%{public}s", cbType.c_str()); + WmErrorCode ret = registerManager_->UnregisterListener(nullptr, cbType, CaseType::CASE_WINDOW_MANAGER, + env, callback); + if (ret != WmErrorCode::WM_OK) { + AniWindowUtils::AniThrowError(env, ret); + } +} + +void AniWindowManager::ShiftAppWindowFocus(ani_env* env, ani_object obj, ani_long nativeObj, + ani_double sourceWindowId, ani_double targetWindowId) +{ + TLOGI(WmsLogTag::DEFAULT, "[ANI]"); + AniWindowManager* aniWindowManager = reinterpret_cast(nativeObj); + aniWindowManager->OnShiftAppWindowFocus(env, sourceWindowId, targetWindowId); +} + +void AniWindowManager::OnShiftAppWindowFocus(ani_env* env, ani_double sourceWindowId, ani_double targetWindowId) +{ +} +} // namespace Rosen +} // namespace OHOS \ No newline at end of file diff --git a/previewer/mock/window_manager_ani/ani_window_manager.h b/previewer/mock/window_manager_ani/ani_window_manager.h new file mode 100644 index 0000000000..fb305163f5 --- /dev/null +++ b/previewer/mock/window_manager_ani/ani_window_manager.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ANI_WINDOW_MANAGER_H +#define OHOS_ANI_WINDOW_MANAGER_H + +#include "ani.h" +#include "ani_window_register_manager.h" +#include "window.h" + +namespace OHOS { +namespace Rosen { +#ifdef _WIN32 +#define WINDOW_EXPORT __attribute__((dllexport)) +#else +#define WINDOW_EXPORT __attribute__((visibility("default"))) +#endif + +class AniWindowManager { +public: + explicit AniWindowManager(); + + static ani_status AniWindowManagerInit(ani_env* env); + static ani_object WindowStageCreate(ani_env* env, ani_long scene); + static ani_ref GetLastWindow(ani_env* env, ani_long nativeObj, ani_object context); + static ani_ref FindWindow(ani_env* env, ani_long nativeObj, ani_string windowName); + static void MinimizeAll(ani_env* env, ani_long nativeObj, ani_double displayId); + static void RegisterWindowManagerCallback(ani_env* env, ani_long nativeObj, ani_string type, ani_ref callback); + static void UnregisterWindowManagerCallback(ani_env* env, ani_long nativeObj, ani_string type, ani_ref callback); + static void ShiftAppWindowFocus(ani_env* env, ani_object obj, ani_long nativeObj, + ani_double sourceWindowId, ani_double targetWindowId); +private: + ani_ref OnGetLastWindow(ani_env* env, ani_object context); + ani_ref OnFindWindow(ani_env* env, ani_string windowName); + void OnMinimizeAll(ani_env* env, ani_double displayId); + void OnShiftAppWindowFocus(ani_env* env, ani_double sourceWindowId, ani_double targetWindowId); + ani_object GetTopWindowTask(ani_env* env, void* contextPtr, bool newApi); + void OnRegisterWindowManagerCallback(ani_env* env, ani_string type, ani_ref callback); + void OnUnregisterWindowManagerCallback(ani_env* env, ani_string type, ani_ref callback); + + std::unique_ptr registerManager_ = nullptr; +}; +} // namespace Rosen +} // namespace OHOS +#endif // OHOS_ANI_WINDOW_MANAGER_H \ No newline at end of file diff --git a/previewer/src/window_impl.cpp b/previewer/src/window_impl.cpp index c4ff86be05..b6802ed48d 100644 --- a/previewer/src/window_impl.cpp +++ b/previewer/src/window_impl.cpp @@ -15,6 +15,8 @@ #include "window_impl.h" +#include +#include #include #include "dm_common.h" @@ -339,6 +341,64 @@ WMError WindowImpl::SetUIContentByName( return WMError::WM_OK; } +WMError WindowImpl::SetUIContentByName( + const std::string& contentInfo, ani_env* env, ani_object storage, AppExecFwk::Ability* ability) +{ + TLOGD(WmsLogTag::WMS_LIFE, "[ANI] contentInfo: %{public}s", contentInfo.c_str()); + if (uiContent_) { + uiContent_->Destroy(); + } + std::unique_ptr uiContent; + if (ability != nullptr) { + uiContent = Ace::UIContent::Create(ability); + } else { + uiContent = Ace::UIContent::CreateWithAniEnv(context_.get(), reinterpret_cast(env)); + } + if (uiContent == nullptr) { + TLOGE(WmsLogTag::WMS_LIFE, "fail to SetUIContentByName"); + return WMError::WM_ERROR_NULLPTR; + } + uiContent->InitializeByNameWithAniStorage(this, contentInfo, (ani_object)storage); + uiContent_ = std::move(uiContent); + NotifySetIgnoreSafeArea(isIgnoreSafeArea_); + UpdateViewportConfig(); + if (contentInfoCallback_) { + contentInfoCallback_(contentInfo); + } + return WMError::WM_OK; +} + +class UIContentImpl; + + +std::shared_ptr> GetAbcContent(const std::string& abcPath) +{ + std::filesystem::path abcFile { abcPath }; + if (abcFile.empty() || !abcFile.is_absolute() || !std::filesystem::exists(abcFile)) { + WLOGFE("abc file path is not valid"); + return nullptr; + } + int begin, end; + std::fstream file(abcFile, std::ios::in | std::ios::binary); + if (!file) { + return nullptr; + } + begin = file.tellg(); + file.seekg(0, std::ios::end); + end = file.tellg(); + int len = end - begin; + WLOGFD("abc file: %{public}s, size: %{public}d", abcPath.c_str(), len); + + if (len <= 0) { + WLOGFE("abc file size is 0"); + return nullptr; + } + std::vector abcBytes(len); + file.seekg(0, std::ios::beg); + file.read(reinterpret_cast(abcBytes.data()), len); + return std::make_shared>(abcBytes); +} + WMError WindowImpl::NapiSetUIContent(const std::string& contentInfo, napi_env env, napi_value storage, BackupAndRestoreType type, sptr token, AppExecFwk::Ability* ability) { @@ -375,6 +435,32 @@ WMError WindowImpl::NapiSetUIContent(const std::string& contentInfo, napi_env en return WMError::WM_OK; } +WMError WindowImpl::NapiSetUIContent(const std::string& contentInfo, ani_env* env, ani_object storage, + BackupAndRestoreType type, sptr token, AppExecFwk::Ability* ability) +{ + WLOGFD("[ANI]NapiSetUIContent: %{public}s", contentInfo.c_str()); + if (uiContent_) { + uiContent_->Destroy(); + } + std::unique_ptr uiContent; + uiContent = Ace::UIContent::CreateWithAniEnvAndInitialize(this, context_.get(), env, contentInfo, (ani_object)storage); + if (uiContent == nullptr) { + WLOGFE("fail to ANISetUIContent"); + return WMError::WM_ERROR_NULLPTR; + } + uiContent_ = std::move(uiContent); + if (uiContent_ == nullptr) { + WLOGFE("uiContent_ is NULL"); + return WMError::WM_ERROR_NULLPTR; + } + NotifySetIgnoreSafeArea(isIgnoreSafeArea_); + UpdateViewportConfig(); + if (contentInfoCallback_) { + contentInfoCallback_(contentInfo); + } + return WMError::WM_OK; +} + Ace::UIContent* WindowImpl::GetUIContent() const { -- Gitee