1 /*
2  * Copyright (c) 2021-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_ACE_ABILITY_H
17 #define FOUNDATION_ACE_ADAPTER_PREVIEW_ACE_ABILITY_H
18 
19 #include <atomic>
20 #include <map>
21 #include <memory>
22 #include <refbase.h>
23 #include <string>
24 #include <thread>
25 
26 #include "adapter/preview/entrance/ace_run_args.h"
27 #include "adapter/preview/entrance/clipboard/clipboard_impl.h"
28 #include "adapter/preview/entrance/clipboard/clipboard_proxy_impl.h"
29 #include "adapter/preview/external/ability/context.h"
30 #include "adapter/preview/external/ability/fa/fa_context.h"
31 #include "adapter/preview/external/ability/stage/stage_context.h"
32 #include "adapter/preview/external/multimodalinput/axis_event.h"
33 #include "adapter/preview/external/multimodalinput/key_event.h"
34 #include "adapter/preview/external/multimodalinput/pointer_event.h"
35 #include "frameworks/base/utils/macros.h"
36 #include "frameworks/core/common/window_animation_config.h"
37 
38 namespace OHOS::Rosen {
39 class Window;
40 class IAvoidAreaChangedListener;
41 class IIgnoreViewSafeAreaListener;
42 }
43 
44 namespace OHOS::Ace::Platform {
45 
46 struct ConfigChanges {
47     bool watchLocale = false;
48     bool watchLayout = false;
49     bool watchFontSize = false;
50     bool watchOrientation = false;
51     bool watchDensity = false;
52 };
53 
54 struct SystemParams {
55     int32_t deviceWidth { 0 };
56     int32_t deviceHeight { 0 };
57     bool isRound = false;
58     double density { 1.0 };
59     std::string language = "zh";
60     std::string region = "CN";
61     std::string script = "";
62     OHOS::Ace::DeviceType deviceType { DeviceType::PHONE };
63     OHOS::Ace::ColorMode colorMode { ColorMode::LIGHT };
64     OHOS::Ace::DeviceOrientation orientation { DeviceOrientation::PORTRAIT };
65 };
66 
67 class ACE_FORCE_EXPORT AceAbility {
68 public:
69     explicit AceAbility(const AceRunArgs& runArgs);
70     ~AceAbility();
71 
72     // Be called in Previewer frontend thread, which is not ACE platform thread.
73     static std::unique_ptr<AceAbility> CreateInstance(AceRunArgs& runArgs);
74     void SetMockModuleList(const std::map<std::string, std::string>& mockJsonInfo);
75     void InitEnv();
76     void InitializeClipboard() const;
77     void OnBackPressed() const;
78     bool OnInputEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const;
79     bool OnInputEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const;
80     bool OnInputEvent(const std::shared_ptr<MMI::AxisEvent>& axisEvent) const;
81     bool OnInputMethodEvent(const unsigned int codePoint) const;
82 
83     void OnConfigurationChanged(const DeviceConfig& newConfig);
84     void SurfaceChanged(
85         const DeviceOrientation& orientation, const double& resolution, int32_t& width, int32_t& height,
86         WindowSizeChangeReason type = WindowSizeChangeReason::UNDEFINED);
87     void ReplacePage(const std::string& url, const std::string& params);
88     void LoadDocument(const std::string& url, const std::string& componentName, SystemParams& systemParams);
89 
90     std::string GetJSONTree();
91     std::string GetDefaultJSONTree();
92     bool OperateComponent(const std::string& attrsJson);
93     void SetWindow(sptr<OHOS::Rosen::Window> rsWindow);
94     sptr<OHOS::Rosen::Window> GetWindow();
95 
GetBundleName()96     std::string GetBundleName()
97     {
98         return bundleName_;
99     }
100 
GetModuleName()101     std::string GetModuleName()
102     {
103         return moduleName_;
104     }
105 
106 private:
107     void InitializeAppInfo();
108     void SetConfigChanges(const std::string& configChanges);
109 
110     AceRunArgs runArgs_;
111     ConfigChanges configChanges_;
112     sptr<OHOS::Rosen::Window> rsWindow_;
113     sptr<OHOS::Rosen::IAvoidAreaChangedListener> avoidAreaChangedListener_;
114     sptr<OHOS::Rosen::IIgnoreViewSafeAreaListener> ignoreViewSafeAreaListener_;
115 
116     std::string bundleName_;
117     std::string moduleName_;
118     std::string compileMode_;
119     int32_t compatibleVersion_ = 0;
120     bool installationFree_ = false;
121     uint32_t labelId_ = 0;
122     bool useNewPipeline_ = true;
123 };
124 
125 } // namespace OHOS::Ace::Platform
126 
127 #endif // FOUNDATION_ACE_ADAPTER_PREVIEW_ACE_ABILITY_H
128