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 OHOS_ROSEN_WINDOW_NODE_CONTAINER_H
17 #define OHOS_ROSEN_WINDOW_NODE_CONTAINER_H
18 
19 #include "animation_config.h"
20 #include "avoid_area_controller.h"
21 #include "display_info.h"
22 #include "minimize_app.h"
23 #include "display_group_controller.h"
24 #include "display_group_info.h"
25 #include "window_layout_policy.h"
26 #include "window_manager.h"
27 #include "window_node.h"
28 #include "window_zorder_policy.h"
29 #include "wm_common.h"
30 #include "wm_common_inner.h"
31 #include "window_pair.h"
32 #include "window_root.h"
33 
34 namespace OHOS {
35 namespace Rosen {
36 using WindowNodeOperationFunc = std::function<bool(sptr<WindowNode>)>; // return true indicates to stop traverse
37 class WindowNodeContainer : public RefBase {
38 public:
39     WindowNodeContainer(const sptr<DisplayInfo>& displayInfo, ScreenId displayGroupId);
40     ~WindowNodeContainer();
41     WMError ShowStartingWindow(sptr<WindowNode>& node);
42     WMError AddWindowNode(sptr<WindowNode>& node, sptr<WindowNode>& parentNode, bool afterAnimation = false);
43     WMError RemoveWindowNode(sptr<WindowNode>& node, bool fromAnimation = false);
44     WMError HandleRemoveWindow(sptr<WindowNode>& node);
45     WMError UpdateWindowNode(sptr<WindowNode>& node, WindowUpdateReason reason);
46     WMError DestroyWindowNode(sptr<WindowNode>& node, std::vector<uint32_t>& windowIds);
47     const std::vector<uint32_t>& Destroy();
48     void AssignZOrder();
49     WMError SetFocusWindow(uint32_t windowId);
50     uint32_t GetFocusWindow() const;
51     WMError SetActiveWindow(uint32_t windowId, bool byRemoved);
52     uint32_t GetActiveWindow() const;
53     void SetDisplayBrightness(float brightness);
54     float GetDisplayBrightness() const;
55     void SetBrightnessWindow(uint32_t windowId);
56     uint32_t GetBrightnessWindow() const;
57     uint32_t ToOverrideBrightness(float brightness);
58     void UpdateBrightness(uint32_t id, bool byRemoved);
59     void HandleKeepScreenOn(const sptr<WindowNode>& node, bool requireLock);
60     AvoidArea GetAvoidAreaByType(const sptr<WindowNode>& node, AvoidAreaType avoidAreaType) const;
61     WMError MinimizeStructuredAppWindowsExceptSelf(const sptr<WindowNode>& node);
62     void TraverseContainer(std::vector<sptr<WindowNode>>& windowNodes) const;
63     std::unordered_map<WindowType, SystemBarProperty> GetExpectImmersiveProperty(DisplayId id,
64         sptr<WindowNode>& triggerWindow) const;
65     uint32_t GetWindowCountByType(WindowType windowType);
66     uint32_t GetMainFloatingWindowCount();
67     bool IsForbidDockSliceMove(DisplayId displayId) const;
68     bool IsDockSliceInExitSplitModeArea(DisplayId displayId) const;
69     void ExitSplitMode(DisplayId displayId);
70 
71     bool IsVerticalDisplay(DisplayId displayId) const;
72     WMError RaiseZOrderForAppWindow(sptr<WindowNode>& node, sptr<WindowNode>& parentNode);
73     sptr<WindowNode> GetNextFocusableWindow(uint32_t windowId) const;
74     sptr<WindowNode> GetNextRotatableWindow(uint32_t windowId) const;
75     sptr<WindowNode> GetNextActiveWindow(uint32_t windowId) const;
76     void MinimizeAllAppWindows(DisplayId displayId);
77     void MinimizeOldestAppWindow();
78     void MinimizeOldestMainFloatingWindow(uint32_t windowId);
79     WMError ToggleShownStateForAllAppWindows(std::function<bool(uint32_t, WindowMode)> restoreFunc, bool restore);
80     void BackUpAllAppWindows();
81     void RestoreAllAppWindows(std::function<bool(uint32_t, WindowMode)> restoreFunc);
82     bool IsAppWindowsEmpty() const;
83     void SetSurfaceNodeVisible(sptr<WindowNode>& node, int32_t topPriority, bool visible);
84     void SetBelowScreenlockVisible(sptr<WindowNode>& node, bool visible);
85     void ProcessWindowStateChange(WindowState state, WindowStateChangeReason reason);
86     void NotifySystemBarTints(std::vector<DisplayId> displayIdVec);
87     WMError MinimizeAppNodeExceptOptions(MinimizeReason reason, const std::vector<uint32_t> &exceptionalIds = {},
88                                          const std::vector<WindowMode> &exceptionalModes = {});
89     WMError SetWindowMode(sptr<WindowNode>& node, WindowMode dstMode);
90     WMError SwitchLayoutPolicy(WindowLayoutMode mode, DisplayId displayId, bool reorder = false);
91     void RaiseSplitRelatedWindowToTop(sptr<WindowNode>& node);
92     Rect GetDisplayGroupRect() const;
93     void TraverseWindowTree(const WindowNodeOperationFunc& func, bool isFromTopToBottom = true) const;
94     void UpdateSizeChangeReason(sptr<WindowNode>& node, WindowSizeChangeReason reason);
95     void DropShowWhenLockedWindowIfNeeded(const sptr<WindowNode>& node);
96 
97     void SetMinimizedByOther(bool isMinimizedByOther);
98     void GetModeChangeHotZones(DisplayId displayId,
99         ModeChangeHotZones& hotZones, const ModeChangeHotZonesConfig& config);
100 
101     // parentDisplayId is the same as displayId in single-display mode
102     bool AddNodeOnRSTree(sptr<WindowNode>& node, DisplayId displayId, DisplayId parentDisplayId,
103         WindowUpdateType type, bool animationPlayed = false);
104     // parentDisplayId is the same as displayId in single-display mode
105     bool RemoveNodeFromRSTree(sptr<WindowNode>& node, DisplayId displayId, DisplayId parentDisplayId,
106         WindowUpdateType type, bool animationPlayed = false);
107     bool AddAppSurfaceNodeOnRSTree(sptr<WindowNode>& node);
108 
109     sptr<WindowLayoutPolicy> GetLayoutPolicy() const;
110     sptr<AvoidAreaController> GetAvoidController() const;
111     sptr<DisplayGroupController> GetDisplayGroupController() const;
112     sptr<WindowNode> GetRootNode(WindowRootNodeType type) const;
113     void NotifyDockWindowStateChanged(sptr<WindowNode>& node, bool isEnable);
114     void NotifyDockWindowStateChanged(DisplayId displayId);
115     void UpdateCameraFloatWindowStatus(const sptr<WindowNode>& node, bool isShowing);
116     void UpdateAvoidAreaListener(sptr<WindowNode>& windowNode, bool haveAvoidAreaListener);
117     void BeforeProcessWindowAvoidAreaChangeWhenDisplayChange() const;
118     void ProcessWindowAvoidAreaChangeWhenDisplayChange() const;
119     WindowLayoutMode GetCurrentLayoutMode() const;
120     void RemoveSingleUserWindowNodes(int accountId);
121     WMError IsTileRectSatisfiedWithSizeLimits(sptr<WindowNode>& node);
122     bool HasPrivateWindow();
123     sptr<WindowNode> GetDeskTopWindow();
124     static AnimationConfig& GetAnimationConfigRef();
125     bool TakeWindowPairSnapshot(DisplayId displayId);
126     void ClearWindowPairSnapshot(DisplayId displayId);
127     bool IsScreenLocked();
128     void LayoutWhenAddWindowNode(sptr<WindowNode>& node, bool afterAnimation = false);
129     void UpdatePrivateStateAndNotify();
130     static void SetConfigMainFloatingWindowAbove(bool isAbove);
131     static void SetMaxMainFloatingWindowNumber(uint32_t maxNumber);
132     void SetDisplayOrientationFromWindow(sptr<WindowNode>& node, bool withAnimation);
133     bool HasMainFullScreenWindowShown();
134     static bool GetAnimateTransactionEnabled();
135     void SetWindowPairFrameGravity(DisplayId displayId, Gravity gravity);
136     static std::string uiType_;
137 private:
138     friend class WindowRoot;
139     void TraverseWindowNode(sptr<WindowNode>& root, std::vector<sptr<WindowNode>>& windowNodes) const;
140     sptr<WindowNode> FindRoot(WindowType type) const;
141     sptr<WindowNode> FindWindowNodeById(uint32_t id) const;
142     void UpdateFocusStatus(uint32_t id, bool focused);
143     void UpdateActiveStatus(uint32_t id, bool isActive);
144     void NotifyIfAvoidAreaChanged(const sptr<WindowNode>& node, const AvoidControlType avoidType) const;
145     void NotifyIfSystemBarTintChanged(DisplayId displayId) const;
146     void NotifyIfSystemBarRegionChanged(DisplayId displayId) const;
147     void NotifyIfKeyboardRegionChanged(const sptr<WindowNode>& node, const AvoidControlType avoidType) const;
148     void TraverseAndUpdateWindowState(WindowState state, int32_t topPriority);
149     void UpdateWindowTree(sptr<WindowNode>& node);
150     void UpdateWindowState(sptr<WindowNode> node, int32_t topPriority, WindowState state);
151     void HandleKeepScreenOn(const sptr<WindowNode>& node, WindowState state);
152     bool IsTopWindow(uint32_t windowId, sptr<WindowNode>& rootNode) const;
153     sptr<WindowNode> FindDividerNode() const;
154     void RaiseWindowToTop(uint32_t windowId, std::vector<sptr<WindowNode>>& windowNodes);
155     bool IsAboveSystemBarNode(sptr<WindowNode> node) const;
156     bool IsSplitImmersiveNode(sptr<WindowNode> node) const;
157     bool TraverseFromTopToBottom(sptr<WindowNode> node, const WindowNodeOperationFunc& func) const;
158     bool TraverseFromBottomToTop(sptr<WindowNode> node, const WindowNodeOperationFunc& func) const;
159     void RaiseOrderedWindowToTop(std::vector<sptr<WindowNode>>& orderedNodes,
160         std::vector<sptr<WindowNode>>& windowNodes);
161     void DumpScreenWindowTree();
162     void DumpScreenWindowTreeByWinId(uint32_t winid);
163     void RaiseInputMethodWindowPriorityIfNeeded(const sptr<WindowNode>& node) const;
164     void ReZOrderShowWhenLockedWindowIfNeeded(const sptr<WindowNode>& node);
165     void RaiseShowWhenLockedWindowIfNeeded(const sptr<WindowNode>& node);
166     void ReZOrderShowWhenLockedWindows(bool up);
167 
168     WMError AddWindowNodeOnWindowTree(sptr<WindowNode>& node, const sptr<WindowNode>& parentNode);
169     void RemoveWindowNodeFromWindowTree(sptr<WindowNode>& node);
170     void UpdateRSTreeWhenShowingDisplaysChange(sptr<WindowNode>& node,
171         const std::vector<DisplayId>& lastShowingDisplays);
172     bool CheckWindowNodeWhetherInWindowTree(const sptr<WindowNode>& node) const;
173     void UpdateWindowModeSupportTypeWhenKeyguardChange(const sptr<WindowNode>& node, bool up);
174     void RemoveFromRsTreeWhenRemoveWindowNode(sptr<WindowNode>& node, bool fromAnimation);
175     void UpdateSizeChangeReason(sptr<WindowNode>& node, WindowMode srcMode, WindowMode dstMode);
176     void UpdatePrivateWindowCount();
177     uint32_t GetAppWindowNum();
178     void ResetMainFloatingWindowPriorityIfNeeded(sptr<WindowNode>& node);
179     void ResetWindowZOrderPriorityWhenSetMode(sptr<WindowNode>& node,
180         const WindowMode& dstMode, const WindowMode& srcMode);
181     void ResetAllMainFloatingWindowZOrder(sptr<WindowNode>& rootNode);
182     void HandleRemoveWindowDisplayOrientation(sptr<WindowNode>& node, bool fromAnimation);
183     void OpenInputMethodSyncTransaction();
184     void CloseInputMethodSyncTransaction();
185 
186     void JudgeToReportSystemBarInfo(const sptr<WindowNode> window,
187         const std::unordered_map<WindowType, SystemBarProperty>& systemBarPropInfo) const;
188 
189     bool IsWindowFollowParent(WindowType type);
190 
191     float displayBrightness_ = UNDEFINED_BRIGHTNESS;
192     uint32_t brightnessWindow_ = INVALID_WINDOW_ID;
193     uint32_t zOrder_ { 0 };
194     uint32_t focusedWindow_ { INVALID_WINDOW_ID };
195     int32_t focusedPid_ = -1;
196     uint32_t activeWindow_ = INVALID_WINDOW_ID;
197     int32_t activePid_ = -1;
198     bool isScreenLocked_ = false;
199     uint32_t privateWindowCount_ = 0;
200 
201     std::vector<uint32_t> backupWindowIds_;
202     std::map<uint32_t, WindowMode> backupWindowMode_;
203     std::map<DisplayId, Rect> backupDividerWindowRect_;
204     std::map<DisplayId, std::set<WindowMode>> backupDisplaySplitWindowMode_;
205 
206     sptr<WindowZorderPolicy> zorderPolicy_ = new WindowZorderPolicy();
207     std::unordered_map<WindowLayoutMode, sptr<WindowLayoutPolicy>> layoutPolicies_;
208     WindowLayoutMode layoutMode_ = WindowLayoutMode::CASCADE;
209     std::vector<Rect> currentCoveredArea_;
210     std::vector<uint32_t> removedIds_;
211     static AnimationConfig animationConfig_;
212 
213     sptr<WindowNode> belowAppWindowNode_ = new WindowNode();
214     sptr<WindowNode> appWindowNode_ = new WindowNode();
215     sptr<WindowNode> aboveAppWindowNode_ = new WindowNode();
216     sptr<WindowLayoutPolicy> layoutPolicy_;
217     sptr<AvoidAreaController> avoidController_;
218     sptr<DisplayGroupController> displayGroupController_;
219 
220     // if isFloatWindowHigher_ is true, FloatWindow should be above the full screen window.
221     static bool isFloatWindowAboveFullWindow_;
222     static uint32_t maxMainFloatingWindowNumber_;
223     static bool isAnimateTransactionEnabled_;
224 };
225 } // namespace Rosen
226 } // namespace OHOS
227 #endif // OHOS_ROSEN_WINDOW_NODE_CONTAINER_H
228