• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 JSAPPIMPL_H
17 #define JSAPPIMPL_H
18 
19 #include <atomic>
20 #include <memory>
21 #include <thread>
22 
23 #include "JsApp.h"
24 #include "ace_ability.h"
25 #include "ace_run_args.h"
26 #include "device_config.h"
27 #include "device_type.h"
28 #include "window_animation_config.h"
29 
30 namespace OHOS {
31 namespace Previewer {
32     struct PreviewerWindowModel;
33     class PreviewerDisplay;
34 }
35 namespace Rosen {
36     class GlfwRenderContext;
37     enum class FoldStatus: uint32_t;
38     enum class WindowType : uint32_t;
39     struct SystemBarProperty;
40     struct Rect;
41 }
42 #if defined(__APPLE__) || defined(_WIN32)
43 namespace AbilityRuntime {
44     class Simulator;
45     struct Options;
46 }
47 namespace AppExecFwk {
48     class Configuration;
49 }
50 #endif
51 }
52 
53 class JsAppImpl : public JsApp {
54 public:
55     JsAppImpl() noexcept;
56     ~JsAppImpl();
57     JsAppImpl& operator=(const JsAppImpl&) = delete;
58     JsAppImpl(const JsAppImpl&) = delete;
59 
60     static JsAppImpl& GetInstance();
61     void Start() override;
62     void Restart() override;
63     void Interrupt() override;
64     std::string GetJSONTree() override;
65     std::string GetDefaultJSONTree() override;
66     void OrientationChanged(std::string commandOrientation) override;
67     void ResolutionChanged(ResolutionParam& param, int32_t screenDensity, std::string reason) override;
68     OHOS::Ace::WindowSizeChangeReason ConvertResizeReason(std::string reason);
69     void SetResolutionParams(int32_t changedOriginWidth, int32_t changedOriginHeight,
70                            int32_t changedWidth, int32_t changedHeight, int32_t screenDensity);
71     void SetArgsColorMode(const std::string& value) override;
72     void SetArgsAceVersion(const std::string& value) override;
73     void SetDeviceOrentation(const std::string& value);
74     std::string GetOrientation() const override;
75     std::string GetColorMode() const override;
76     void ColorModeChanged(const std::string commandColorMode) override;
77     void ReloadRuntimePage(const std::string currentPage) override;
78     void SetScreenDensity(const std::string value) override;
79     void SetConfigChanges(const std::string value) override;
80     bool MemoryRefresh(const std::string memoryRefreshArgs) const override;
81     void LoadDocument(const std::string, const std::string, const Json2::Value&) override;
82     void FoldStatusChanged(const std::string commandFoldStatus,
83         int32_t currentWidth, int32_t currentHeight) override;
84     void SetAvoidArea(const AvoidAreas& areas) override;
85     void UpdateAvoidArea2Ide(const std::string& key, const OHOS::Rosen::Rect& value);
86     OHOS::Rosen::Window* GetWindow() const;
87 
88     void DispatchBackPressedEvent() const;
89     void DispatchKeyEvent(const std::shared_ptr<OHOS::MMI::KeyEvent>& keyEvent) const;
90     void DispatchPointerEvent(const std::shared_ptr<OHOS::MMI::PointerEvent>& pointerEvent) const;
91     void DispatchAxisEvent(const std::shared_ptr<OHOS::MMI::AxisEvent>& axisEvent) const;
92     void DispatchInputMethodEvent(const unsigned int codePoint) const;
93     void InitGlfwEnv();
94     void CalculateAvoidAreaByType(OHOS::Rosen::WindowType type,
95         const OHOS::Rosen::SystemBarProperty& property);
96     void InitAvoidAreas(OHOS::Rosen::Window* window);
97     void InitJsApp() override;
98     void StopAbility();
99     void InitCommandInfo();
100     void InitScreenInfo();
101 
102 protected:
103     void SetJsAppArgs(OHOS::Ace::Platform::AceRunArgs& args);
104     void RunJsApp();
105     void RunNormalAbility();
106     void RunDebugAbility();
107     double watchScreenDensity = 320;  // Watch Screen Density
108     double phoneScreenDensity = 480;  // Phone Screen Density
109     double tvScreenDensity = 320;     // TV Screen Density
110     double tabletScreenDensity = 400; // Tablet Screen Density
111     double carScreenDensity = 320;    // Car Screen Density
112     double twoInOneScreenDensity = 240;    // Car Screen Density
113 
114 private:
115     void SetPkgContextInfo();
116     void SetMockJsonInfo();
117     void SetAssetPath(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
118     void SetProjectModel(OHOS::Ace::Platform::AceRunArgs& args) const;
119     void SetPageProfile(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
120     void SetDeviceWidth(OHOS::Ace::Platform::AceRunArgs& args, const int32_t) const;
121     void SetDeviceHeight(OHOS::Ace::Platform::AceRunArgs& args, const int32_t) const;
122     void SetWindowTitle(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
123     void SetUrl(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
124     void SetConfigChanges(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
125     void SetColorMode(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
126     void SetOrientation(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
127     void SetAceVersionArgs(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
128     void SetLanguage(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
129     void SetRegion(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
130     void SetScript(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
131     void SetSystemResourcesPath(OHOS::Ace::Platform::AceRunArgs& args) const;
132     void SetAppResourcesPath(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
133     void SetFormsEnabled(OHOS::Ace::Platform::AceRunArgs& args, bool formsEnabled) const;
134     void SetContainerSdkPath(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const;
135     void SetOnRouterChange(OHOS::Ace::Platform::AceRunArgs& args) const;
136     void SetOnError(OHOS::Ace::Platform::AceRunArgs& args) const;
137     void SetComponentModeEnabled(OHOS::Ace::Platform::AceRunArgs& args, bool isComponentMode) const;
138     void AssignValueForWidthAndHeight(const int32_t origWidth, const int32_t origHeight,
139                                       const int32_t compWidth, const int32_t compHeight);
140     void AdaptDeviceType(OHOS::Ace::Platform::AceRunArgs& args, const std::string,
141                          const int32_t, double screenDendity = 0) const;
142     void ParseSystemParams(OHOS::Ace::Platform::AceRunArgs& args, const Json2::Value& paramObj);
143     void SetSystemParams(OHOS::Ace::Platform::SystemParams& args, const Json2::Value& paramObj);
144     void SetDeviceScreenDensity(const int32_t screenDensity, const std::string type);
145     std::string GetDeviceTypeName(const OHOS::Ace::DeviceType) const;
146     OHOS::Rosen::FoldStatus ConvertFoldStatus(std::string value) const;
147     const double BASE_SCREEN_DENSITY = 160; // Device Baseline Screen Density
148     std::unique_ptr<OHOS::Ace::Platform::AceAbility> ability;
149     std::atomic<bool> isStop;
150     int32_t width = 0;
151     int32_t height = 0;
152     int32_t orignalWidth = 0;
153     int32_t orignalHeight = 0;
154     AvoidAreas avoidInitialAreas;
155     OHOS::Ace::Platform::AceRunArgs aceRunArgs;
156     std::shared_ptr<OHOS::Rosen::GlfwRenderContext> glfwRenderContext;
157 #if defined(__APPLE__) || defined(_WIN32)
158     std::shared_ptr<OHOS::AbilityRuntime::Simulator> simulator;
159     int64_t debugAbilityId = -1;
160     void SetSimulatorParams(OHOS::AbilityRuntime::Options& options);
161     void SetSimulatorCommonParams(OHOS::AbilityRuntime::Options& options);
162     std::shared_ptr<OHOS::AppExecFwk::Configuration> UpdateConfiguration(OHOS::Ace::Platform::AceRunArgs& args);
163 
164     std::shared_ptr<OHOS::Previewer::PreviewerWindowModel> windowModel;
165     void SetWindowParams() const;
166     template<typename T>
SetDevice(const OHOS::Ace::DeviceType & deviceType)167     T SetDevice(const OHOS::Ace::DeviceType& deviceType) const
168     {
169         static_assert(std::is_enum_v<T>, "T must be an enum type");
170         switch (deviceType) {
171             case OHOS::Ace::DeviceType::WATCH:
172                 return T::WATCH;
173             case OHOS::Ace::DeviceType::TV:
174                 return T::TV;
175             case OHOS::Ace::DeviceType::CAR:
176                 return T::CAR;
177             case OHOS::Ace::DeviceType::TABLET:
178                 return T::TABLET;
179             case OHOS::Ace::DeviceType::PHONE:
180                 return T::PHONE;
181             default:
182                 return T::UNKNOWN;
183         }
184     }
185     template<typename T>
SetColorMode(const OHOS::Ace::ColorMode & colorMode)186     T SetColorMode(const OHOS::Ace::ColorMode& colorMode) const
187     {
188         static_assert(std::is_enum_v<T>, "T must be an enum type");
189         if (colorMode == OHOS::Ace::ColorMode::LIGHT) {
190             return T::LIGHT;
191         } else if (colorMode == OHOS::Ace::ColorMode::DARK) {
192             return T::DARK;
193         } else {
194             return T::COLOR_MODE_UNDEFINED;
195         }
196     }
197     template<typename T>
SetOrientation(const OHOS::Ace::DeviceOrientation & orientation)198     T SetOrientation(const OHOS::Ace::DeviceOrientation& orientation) const
199     {
200         static_assert(std::is_enum_v<T>, "T must be an enum type");
201         if (orientation == OHOS::Ace::DeviceOrientation::PORTRAIT) {
202             return T::PORTRAIT;
203         } else if (orientation == OHOS::Ace::DeviceOrientation::LANDSCAPE) {
204             return T::LANDSCAPE;
205         } else {
206             return T::ORIENTATION_UNDEFINED;
207         }
208     }
209 #endif
210 };
211 
212 #endif // JSAPPIMPL_H
213