1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_ADAPTER_PREVIEW_ENTRANCE_UI_CONTENT_IMPL_H
17 #define FOUNDATION_ACE_ADAPTER_PREVIEW_ENTRANCE_UI_CONTENT_IMPL_H
18 
19 #include "interfaces/inner_api/ace/ui_content.h"
20 #include "interfaces/inner_api/ace/viewport_config.h"
21 #include "native_engine/native_engine.h"
22 #include "native_engine/native_value.h"
23 #include "previewer/include/window.h"
24 
25 #include "adapter/preview/entrance/ace_run_args.h"
26 #include "adapter/preview/external/ability/context.h"
27 #include "adapter/preview/external/ability/fa/fa_context.h"
28 #include "adapter/preview/external/ability/stage/stage_context.h"
29 
30 namespace OHOS::Ace {
31 class ACE_FORCE_EXPORT UIContentImpl : public UIContent {
32 public:
33     UIContentImpl(OHOS::AbilityRuntime::Context* context, void* runtime);
34     UIContentImpl(OHOS::AppExecFwk::Ability* ability);
35     UIContentImpl(OHOS::AbilityRuntime::Context* context, void* runtime, bool isCard);
~UIContentImpl()36     ~UIContentImpl()
37     {
38         DestroyUIDirector();
39         DestroyCallback();
40     }
41 
42     // UI content lifeCycles
43     UIContentErrorCode Initialize(OHOS::Rosen::Window* window, const std::string& url, napi_value storage) override;
44     UIContentErrorCode Initialize(OHOS::Rosen::Window* window,
45         const std::shared_ptr<std::vector<uint8_t>>& content, napi_value storage) override {
46         return UIContentErrorCode::NO_ERRORS;
47     }
48     UIContentErrorCode InitializeByName(OHOS::Rosen::Window *window, const std::string &name,
49         napi_value storage) override {
50         return UIContentErrorCode::NO_ERRORS;
51     }
52     void InitializeDynamic(int32_t hostInstanceId, const std::string& hapPath, const std::string& abcPath,
53         const std::string& entryPoint, const std::vector<std::string>& registerComponents) override {}
54     void Initialize(
55         OHOS::Rosen::Window* window, const std::string& url, napi_value storage, uint32_t focusWindowId) override {}
56     void Foreground() override {}
57     void Background() override {}
58     void Focus() override {}
59     void UnFocus() override {}
60     void Destroy() override;
61     void OnNewWant(const OHOS::AAFwk::Want& want) override {}
62 
63     // restore
64     UIContentErrorCode Restore(OHOS::Rosen::Window* window, const std::string& contentInfo,
65         napi_value storage, ContentInfoType type) override {
66         return UIContentErrorCode::NO_ERRORS;
67     }
68     std::string GetContentInfo(ContentInfoType type) const override;
69     void DestroyUIDirector() override;
70 
71     // UI content event process
72     bool ProcessBackPressed() override;
73     bool ProcessPointerEvent(const std::shared_ptr<OHOS::MMI::PointerEvent>& pointerEvent) override;
74     bool ProcessPointerEventWithCallback(
75         const std::shared_ptr<OHOS::MMI::PointerEvent>& pointerEvent, const std::function<void()>& callback) override;
76     bool ProcessKeyEvent(const std::shared_ptr<OHOS::MMI::KeyEvent>& keyEvent, bool isPreIme) override;
77     bool ProcessAxisEvent(const std::shared_ptr<OHOS::MMI::AxisEvent>& axisEvent) override;
78     bool ProcessVsyncEvent(uint64_t timeStampNanos) override;
79     void SetIsFocusActive(bool isFocusActive) override {}
80     void UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config) override;
81     void UpdateViewportConfig(const ViewportConfig& config, OHOS::Rosen::WindowSizeChangeReason reason,
82         const std::shared_ptr<OHOS::Rosen::RSTransaction>& rsTransaction = nullptr,
83         const std::map<OHOS::Rosen::AvoidAreaType, OHOS::Rosen::AvoidArea>& avoidAreas = {}) override;
84     void UpdateWindowMode(OHOS::Rosen::WindowMode mode, bool hasDeco = true) override {}
85     void UpdateDecorVisible(bool visible, bool hasDeco = true) override {};
86     void HideWindowTitleButton(bool hideSplit, bool hideMaximize, bool hideMinimize, bool hideClose) override {}
87     void SetIgnoreViewSafeArea(bool ignoreViewSafeArea) override {}
88     void UpdateTitleInTargetPos(bool isShow, int32_t height) override {}
89 
90     // Window color
91     uint32_t GetBackgroundColor() override;
92     void SetBackgroundColor(uint32_t color) override;
93     void SetWindowContainerColor(uint32_t activeColor, uint32_t inactiveColor) override;
94 
95     void DumpInfo(const std::vector<std::string>& params, std::vector<std::string>& info) override;
96 
97     // Set UIContent callback for custom window animation
98     void SetNextFrameLayoutCallback(std::function<void()>&& callback) override;
99 
100     // Receive memory level notification
101     void NotifyMemoryLevel(int32_t level) override;
102 
103     void SetAppWindowTitle(const std::string& title) override {}
104     void SetAppWindowIcon(const std::shared_ptr<Media::PixelMap>& pixelMap) override {}
105     void SetActionEventHandler(std::function<void(const std::string& action)>&& actionCallback) override {}
106     void SetErrorEventHandler(std::function<void(const std::string&, const std::string&)>&& errorCallback) override {}
107     void SetFormLinkInfoUpdateHandler(std::function<void(const std::vector<std::string>&)>&& callback) override {}
108 
109     // ArkTS Form
110     void PreInitializeForm(OHOS::Rosen::Window* window, const std::string& url, napi_value storage) override {}
111     void RunFormPage() override {}
112     void OnFormSurfaceChange(float width, float height,
113         OHOS::Rosen::WindowSizeChangeReason type = static_cast<OHOS::Rosen::WindowSizeChangeReason>(0),
114         const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr) override {}
115     void UpdateFormData(const std::string& data) override {}
116     void UpdateFormSharedImage(const std::map<std::string, sptr<OHOS::AppExecFwk::FormAshmem>>& imageDataMap) override
117     {}
118     void SetFormWidth(float width) override {}
119     void SetFormHeight(float height) override {}
120     float GetFormWidth() override
121     {
122         return 0.0;
123     }
124     float GetFormHeight() override
125     {
126         return 0.0;
127     }
128     std::shared_ptr<Rosen::RSSurfaceNode> GetFormRootNode() override
129     {
130         return nullptr;
131     }
132 
133     int32_t CreateModalUIExtension(const AAFwk::Want& want,
134         const ModalUIExtensionCallbacks& callbacks, const ModalUIExtensionConfig& config) override;
135     void CloseModalUIExtension(int32_t sessionId) override;
136 
137     void SetParentToken(sptr<IRemoteObject> token) override {}
138     sptr<IRemoteObject> GetParentToken() override
139     {
140         return nullptr;
141     }
142 
143     SerializedGesture GetFormSerializedGesture() override
144     {
145         return SerializedGesture();
146     }
147 
148     void UpdateTransform(const OHOS::Rosen::Transform& transform) override {};
149 
150     void SetContentNodeGrayScale(float grayscale) override {};
151 
152     void PreLayout() override {};
153 
154     void SetStatusBarItemColor(uint32_t color) override;
155 
156     void SetForceSplitEnable(bool isForceSplit, const std::string& homePage) override {};
157 
158 private:
159     UIContentErrorCode CommonInitialize(OHOS::Rosen::Window* window, const std::string& contentInfo,
160         napi_value storage);
161     void DestroyCallback() const;
162 
163 private:
164     int32_t instanceId_ = -1;
165     void* runtime_ = nullptr;
166     // All parameters that need to be passed.
167     std::string assetPath_;
168     std::string systemResourcesPath_;
169     std::string appResourcesPath_;
170     std::string containerSdkPath_;
171     std::string language_;
172     std::string region_;
173     std::string script_;
174     uint32_t themeId_ = 0;
175     int32_t deviceWidth_ = 0;
176     int32_t deviceHeight_ = 0;
177     bool isRound_ = false;
178     Platform::SendCurrentRouterCallback onRouterChange_;
179     DeviceConfig deviceConfig_;
180 
181     std::string bundleName_;
182     std::string moduleName_;
183     std::string compileMode_;
184     std::string pageProfile_;
185     int32_t compatibleVersion_ = 0;
186     int32_t targetVersion_ = 0;
187     bool installationFree_ = false;
188     uint32_t labelId_ = 0;
189     bool useNewPipeline_ = true;
190 
191     sptr<OHOS::Rosen::Window> rsWindow_;
192     // ITouchOutsideListener is used for touching out of hot areas of window.
193     OHOS::sptr<OHOS::Rosen::ITouchOutsideListener> touchOutsideListener_ = nullptr;
194     OHOS::sptr<OHOS::Rosen::IWindowDragListener> dragWindowListener_ = nullptr;
195     OHOS::sptr<OHOS::Rosen::IAvoidAreaChangedListener> avoidAreaChangedListener_ = nullptr;
196     OHOS::sptr<OHOS::Rosen::IIgnoreViewSafeAreaListener> ignoreViewSafeAreaListener_ = nullptr;
197     // ArkTS Form
198     bool isFormRender_ = false;
199 };
200 } // namespace OHOS::Ace
201 
202 #endif // FOUNDATION_ACE_ADAPTER_PREVIEW_ENTRANCE_UI_CONTENT_IMPL_H
203