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_ACE_ENGINE_ADAPTER_OHOS_CPP_ACE_ABILITY_H
17 #define FOUNDATION_ACE_ACE_ENGINE_ADAPTER_OHOS_CPP_ACE_ABILITY_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "ability.h"
23 #include "ability_loader.h"
24 #include "want.h"
25 #include "window.h"
26 
27 #include "core/common/window_animation_config.h"
28 #include "core/event/touch_event.h"
29 
30 namespace OHOS::Ace {
31 class AceAbility;
32 class AceWindowListener : public OHOS::Rosen::IWindowDragListener,
33                           public OHOS::Rosen::IWindowChangeListener,
34                           public OHOS::Rosen::IOccupiedAreaChangeListener,
35                           public OHOS::Rosen::IAceAbilityHandler,
36                           public OHOS::Rosen::IInputEventConsumer,
37                           public OHOS::Rosen::IAvoidAreaChangedListener {
38 public:
AceWindowListener(std::shared_ptr<AceAbility> owner)39     explicit AceWindowListener(std::shared_ptr<AceAbility> owner) : callbackOwner_(owner) {}
40     ~AceWindowListener() = default;
41     // override Rosen::IWindowDragListener virtual callback function
42     void OnDrag(int32_t x, int32_t y, OHOS::Rosen::DragEvent event) override;
43 
44     // override Rosen::IOccupiedAreaChangeListener virtual callback function
45     void OnSizeChange(const sptr<OHOS::Rosen::OccupiedAreaChangeInfo>& info,
46         const std::shared_ptr<OHOS::Rosen::RSTransaction>& rsTransaction = nullptr) override;
47 
48     // override Rosen::IAceAbilityHandler virtual callback function
49     void SetBackgroundColor(uint32_t color) override;
50     uint32_t GetBackgroundColor() override;
51 
52     // override Rosen::IWindowChangeListener virtual callback function
53     void OnSizeChange(OHOS::Rosen::Rect rect, OHOS::Rosen::WindowSizeChangeReason reason,
54         const std::shared_ptr<OHOS::Rosen::RSTransaction>& rsTransaction = nullptr) override;
55     void OnModeChange(OHOS::Rosen::WindowMode mode, bool hasDeco = true) override;
56 
57     // override Rosen::IInputEventConsumer virtual callback function
58     bool OnInputEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const override;
59     bool OnInputEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const override;
60     bool OnInputEvent(const std::shared_ptr<MMI::AxisEvent>& axisEvent) const override;
61 
62     // override Rosen::IAvoidAreaChangedListener virtual callback function
63     void OnAvoidAreaChanged(const OHOS::Rosen::AvoidArea avoidArea, OHOS::Rosen::AvoidAreaType type) override;
64 
65 private:
66     std::shared_ptr<AceAbility> callbackOwner_;
67 };
68 
69 class AceAbility final : public OHOS::AppExecFwk::Ability {
70 public:
71     AceAbility();
72     ~AceAbility() override = default;
73 
74     void OnStart(const OHOS::AAFwk::Want& want, sptr<AAFwk::SessionInfo> sessionInfo = nullptr) override;
75     void OnStop() override;
76     void OnActive() override;
77     void OnInactive() override;
78     void OnForeground(const OHOS::AAFwk::Want& want) override;
79     void OnBackground() override;
80     void OnBackPressed() override;
81     void OnNewWant(const OHOS::AAFwk::Want& want) override;
82     void OnRestoreAbilityState(const OHOS::AppExecFwk::PacMap& inState) override;
83     void OnSaveAbilityState(OHOS::AppExecFwk::PacMap& outState) override;
84     void OnConfigurationUpdated(const OHOS::AppExecFwk::Configuration& configuration) override;
85     void OnAbilityResult(int requestCode, int resultCode, const OHOS::AAFwk::Want& resultData) override;
86 
87     bool OnStartContinuation() override;
88     bool OnSaveData(OHOS::AAFwk::WantParams& saveData) override;
89     bool OnRestoreData(OHOS::AAFwk::WantParams& restoreData) override;
90     void OnCompleteContinuation(int result) override;
91     void OnRemoteTerminated() override;
92 
93     // handle window Rosen::IWindowDragListener
94     void OnDrag(int32_t x, int32_t y, OHOS::Rosen::DragEvent event);
95 
96     // handle window Rosen::IWindowChangeListener
97     void OnSizeChange(const OHOS::Rosen::Rect& rect, OHOS::Rosen::WindowSizeChangeReason reason,
98         const std::shared_ptr<OHOS::Rosen::RSTransaction>& rsTransaction = nullptr);
99     void OnModeChange(OHOS::Rosen::WindowMode mode, bool hasDeco = true);
100 
101     // handle window Rosen::IOccupiedAreaChangeListener
102     void OnSizeChange(const sptr<OHOS::Rosen::OccupiedAreaChangeInfo>& info,
103         const std::shared_ptr<OHOS::Rosen::RSTransaction>& rsTransaction = nullptr);
104 
105     // handle window Rosen::IInputEventConsumer
106     bool OnInputEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const;
107     bool OnInputEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const;
108     bool OnInputEvent(const std::shared_ptr<MMI::AxisEvent>& axisEvent) const;
109 
110     // handle window Rosen::IAceAbilityHandler
111     void SetBackgroundColor(uint32_t color);
112     uint32_t GetBackgroundColor();
113 
114     // handle window Rosen::IAvoidAreaChangedListener
115     void OnAvoidAreaChanged(const OHOS::Rosen::AvoidArea& avoidArea, OHOS::Rosen::AvoidAreaType type);
116 
117     void Dump(const std::vector<std::string>& params, std::vector<std::string>& info) override;
118 
119 private:
120     static const std::string START_PARAMS_KEY;
121     static const std::string PAGE_URI;
122     static const std::string CONTINUE_PARAMS_KEY;
123 
124     int32_t abilityId_ = -1;
125     float density_ = 1.0f;
126     std::string remotePageUrl_;
127     std::string remoteData_;
128     std::string pageUrl_;
129     bool isFirstActive_ = true;
130 };
131 
132 } // namespace OHOS::Ace
133 
134 #endif // FOUNDATION_ACE_ACE_ENGINE_ADAPTER_OHOS_CPP_ACE_ABILITY_H
135