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 OHOS_ROSEN_WINDOW_SCENE_SESSION_IMPL_H
17#define OHOS_ROSEN_WINDOW_SCENE_SESSION_IMPL_H
18
19#include "window_session_impl.h"
20
21namespace OHOS {
22namespace Rosen {
23
24class WindowSceneSessionImpl : public WindowSessionImpl {
25public:
26    explicit WindowSceneSessionImpl(const sptr<WindowOption>& option);
27    ~WindowSceneSessionImpl();
28    WMError Create(const std::shared_ptr<AbilityRuntime::Context>& context,
29        const sptr<Rosen::ISession>& iSession, const std::string& identityToken = "") override;
30    WMError Show(uint32_t reason = 0, bool withAnimation = false, bool withFocus = true) override;
31    WMError Hide(uint32_t reason, bool withAnimation, bool isFromInnerkits) override;
32    WMError Destroy(bool needNotifyServer, bool needClearListener = true) override;
33    WMError NotifyDrawingCompleted() override;
34    WMError SetTextFieldAvoidInfo(double textFieldPositionY, double textFieldHeight) override;
35    void PreProcessCreate();
36    void SetDefaultProperty();
37    WMError DisableAppWindowDecor() override;
38    bool IsDecorEnable() const override;
39    WMError Minimize() override;
40    WMError MaximizeFloating() override;
41    WMError Maximize() override;
42    WMError Maximize(MaximizePresentation presentation) override;
43    WMError Recover() override;
44    WMError Recover(uint32_t reason) override;
45    WMError Restore() override;
46    void StartMove() override;
47    bool GetStartMoveFlag() override;
48    WmErrorCode StartMoveSystemWindow() override;
49    WMError Close() override;
50    WindowMode GetMode() const override;
51    WMError MoveTo(int32_t x, int32_t y, bool isMoveToGlobal = false) override;
52    WMError MoveToAsync(int32_t x, int32_t y) override;
53    WMError Resize(uint32_t width, uint32_t height) override;
54    WMError ResizeAsync(uint32_t width, uint32_t height) override;
55    WMError RaiseToAppTop() override;
56    WMError RaiseAboveTarget(int32_t subWindowId) override;
57    void PerformBack() override;
58    WMError SetAspectRatio(float ratio) override;
59    WMError ResetAspectRatio() override;
60    WMError SetGlobalMaximizeMode(MaximizeMode mode) override;
61    MaximizeMode GetGlobalMaximizeMode() const override;
62    WMError GetAvoidAreaByType(AvoidAreaType type, AvoidArea& avoidArea) override;
63    SystemBarProperty GetSystemBarPropertyByType(WindowType type) const override;
64    WMError SetSystemBarProperty(WindowType type, const SystemBarProperty& property) override;
65    WMError SetLayoutFullScreen(bool status) override;
66    WMError SetTitleAndDockHoverShown(bool isTitleHoverShown = true,
67        bool isDockHoverShown = true) override;
68    WMError SetFullScreen(bool status) override;
69    WMError BindDialogTarget(sptr<IRemoteObject> targetToken) override;
70    WMError SetDialogBackGestureEnabled(bool isEnabled) override;
71    WMError GetWindowLimits(WindowLimits& windowLimits) override;
72    WMError SetWindowLimits(WindowLimits& windowLimits) override;
73    static void UpdateConfigurationForAll(const std::shared_ptr<AppExecFwk::Configuration>& configuration);
74    static sptr<Window> GetTopWindowWithContext(const std::shared_ptr<AbilityRuntime::Context>& context = nullptr);
75    static sptr<Window> GetTopWindowWithId(uint32_t mainWinId);
76    static sptr<Window> GetMainWindowWithContext(const std::shared_ptr<AbilityRuntime::Context>& context = nullptr);
77    static sptr<WindowSessionImpl> GetMainWindowWithId(uint32_t mainWinId);
78    static sptr<WindowSessionImpl> GetWindowWithId(uint32_t windId);
79    // only main window, sub window, dialog window can use
80    static int32_t GetParentMainWindowId(int32_t windowId);
81    virtual void UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration) override;
82    WMError NotifyMemoryLevel(int32_t level) override;
83
84    virtual WMError AddWindowFlag(WindowFlag flag) override;
85    virtual WMError RemoveWindowFlag(WindowFlag flag) override;
86    virtual WMError SetWindowFlags(uint32_t flags) override;
87    virtual uint32_t GetWindowFlags() const override;
88
89    // window effect
90    virtual WMError SetCornerRadius(float cornerRadius) override;
91    virtual WMError SetShadowRadius(float radius) override;
92    virtual WMError SetShadowColor(std::string color) override;
93    virtual WMError SetShadowOffsetX(float offsetX) override;
94    virtual WMError SetShadowOffsetY(float offsetY) override;
95    virtual WMError SetBlur(float radius) override;
96    virtual WMError SetBackdropBlur(float radius) override;
97    virtual WMError SetBackdropBlurStyle(WindowBlurStyle blurStyle) override;
98    virtual WMError SetWindowMode(WindowMode mode) override;
99    virtual WMError SetGrayScale(float grayScale) override;
100
101    virtual WMError SetTransparent(bool isTransparent) override;
102    virtual WMError SetTurnScreenOn(bool turnScreenOn) override;
103    virtual WMError SetKeepScreenOn(bool keepScreenOn) override;
104    virtual WMError SetPrivacyMode(bool isPrivacyMode) override;
105    virtual void SetSystemPrivacyMode(bool isSystemPrivacyMode) override;
106    virtual WMError SetSnapshotSkip(bool isSkip) override;
107    virtual std::shared_ptr<Media::PixelMap> Snapshot() override;
108    WMError SetTouchHotAreas(const std::vector<Rect>& rects) override;
109    virtual WmErrorCode KeepKeyboardOnFocus(bool keepKeyboardFlag) override;
110    virtual WMError SetCallingWindow(uint32_t callingSessionId) override;
111
112    virtual bool IsTransparent() const override;
113    virtual bool IsTurnScreenOn() const override;
114    virtual bool IsKeepScreenOn() const override;
115    virtual bool IsPrivacyMode() const override;
116    virtual bool IsLayoutFullScreen() const override;
117    virtual bool IsFullScreen() const override;
118    WMError RegisterAnimationTransitionController(const sptr<IAnimationTransitionController>& listener) override;
119    void SetNeedDefaultAnimation(bool needDefaultAnimation) override;
120    WMError SetTransform(const Transform& trans) override;
121    const Transform& GetTransform() const override;
122    WMError UpdateSurfaceNodeAfterCustomAnimation(bool isAdd) override;
123    WMError SetAlpha(float alpha) override;
124    void DumpSessionElementInfo(const std::vector<std::string>& params) override;
125    WSError UpdateWindowMode(WindowMode mode) override;
126    WSError UpdateMaximizeMode(MaximizeMode mode) override;
127    WSError UpdateTitleInTargetPos(bool isShow, int32_t height) override;
128    void NotifySessionForeground(uint32_t reason, bool withAnimation) override;
129    void NotifySessionBackground(uint32_t reason, bool withAnimation, bool isFromInnerkits) override;
130    WMError NotifyPrepareClosePiPWindow() override;
131    void UpdateSubWindowState(const WindowType& type);
132    WMError SetSystemBarProperties(const std::map<WindowType, SystemBarProperty>& properties,
133        const std::map<WindowType, SystemBarPropertyFlag>& propertyFlags) override;
134    WMError GetSystemBarProperties(std::map<WindowType, SystemBarProperty>& properties) override;
135    WMError SetSpecificBarProperty(WindowType type, const SystemBarProperty& property) override;
136    void ConsumePointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) override;
137    bool PreNotifyKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) override;
138    WSError NotifyDialogStateChange(bool isForeground) override;
139    WMError SetDefaultDensityEnabled(bool enabled) override;
140    bool GetDefaultDensityEnabled() override;
141    WMError HideNonSecureWindows(bool shouldHide) override;
142    virtual WMError SetWindowMask(const std::vector<std::vector<uint32_t>>& windowMask) override;
143    WSError CompatibleFullScreenRecover() override;
144    WSError CompatibleFullScreenMinimize() override;
145    WSError CompatibleFullScreenClose() override;
146    WSError NotifyCompatibleModeEnableInPad(bool enabled) override;
147    void UpdateDensity() override;
148    WSError UpdateOrientation() override;
149    WSError UpdateDisplayId(uint64_t displayId) override;
150    WMError AdjustKeyboardLayout(const KeyboardLayoutParams& params) override;
151
152    /*
153     * PC Window
154     */
155    bool IsPcOrPadCapabilityEnabled() const override;
156    bool IsPcOrPadFreeMultiWindowMode() const override;
157
158    WSError SwitchFreeMultiWindow(bool enable) override;
159    virtual bool GetFreeMultiWindowModeEnabledState() override;
160    void NotifyKeyboardPanelInfoChange(const KeyboardPanelInfo& keyboardPanelInfo) override;
161    virtual WMError SetImmersiveModeEnabledState(bool enable) override;
162    virtual bool GetImmersiveModeEnabledState() const override;
163    uint32_t GetStatusBarHeight() override;
164    void NotifySessionFullScreen(bool fullScreen) override;
165    WMError GetWindowStatus(WindowStatus& windowStatus) override;
166    bool GetIsUIExtFirstSubWindow() const override;
167    bool GetIsUIExtAnySubWindow() const override;
168
169    /*
170     * Gesture Back
171     */
172    WMError SetGestureBackEnabled(bool enable) override;
173    bool GetGestureBackEnabled() const override;
174
175protected:
176    WMError CreateAndConnectSpecificSession();
177    WMError CreateSystemWindow(WindowType type);
178    WMError RecoverAndConnectSpecificSession();
179    WMError RecoverAndReconnectSceneSession();
180    sptr<WindowSessionImpl> FindParentSessionByParentId(uint32_t parentId);
181    bool IsSessionMainWindow(uint32_t parentId);
182    bool VerifySubWindowLevel(uint32_t parentId);
183    void LimitWindowSize(uint32_t& width, uint32_t& height);
184    void LimitCameraFloatWindowMininumSize(uint32_t& width, uint32_t& height, float& vpr);
185    void UpdateFloatingWindowSizeBySizeLimits(uint32_t& width, uint32_t& height) const;
186    WMError NotifyWindowSessionProperty();
187    WMError NotifyWindowNeedAvoid(bool status = false);
188    WMError SetLayoutFullScreenByApiVersion(bool status) override;
189    void UpdateWindowSizeLimits();
190    WindowLimits GetSystemSizeLimits(uint32_t displayWidth, uint32_t displayHeight, float vpr);
191    void GetConfigurationFromAbilityInfo();
192    float GetVirtualPixelRatio(sptr<DisplayInfo> displayInfo) override;
193    WMError NotifySpecificWindowSessionProperty(WindowType type, const SystemBarProperty& property);
194    using SessionMap = std::map<std::string, std::pair<int32_t, sptr<WindowSessionImpl>>>;
195    sptr<WindowSessionImpl> FindParentMainSession(uint32_t parentId, const SessionMap& sessionMap);
196
197private:
198    WMError DestroyInner(bool needNotifyServer);
199    WMError MainWindowCloseInner();
200    WMError SyncDestroyAndDisconnectSpecificSession(int32_t persistentId);
201    bool IsValidSystemWindowType(const WindowType& type);
202    WMError CheckParmAndPermission();
203    static uint32_t maxFloatingWindowSize_;
204    void TransformSurfaceNode(const Transform& trans);
205    void AdjustWindowAnimationFlag(bool withAnimation = false);
206    void RegisterSessionRecoverListener(bool isSpecificSession);
207    WMError UpdateAnimationFlagProperty(bool withAnimation);
208    void UpdateFocusableOnShow(bool withFocus);
209    WMError UpdateWindowModeImmediately(WindowMode mode);
210    uint32_t UpdateConfigVal(uint32_t minVal, uint32_t maxVal, uint32_t configVal, uint32_t defaultVal, float vpr);
211    void UpdateWindowState();
212    void UpdateNewSize();
213    void fillWindowLimits(WindowLimits& windowLimits);
214    void ConsumePointerEventInner(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
215        MMI::PointerEvent::PointerItem& pointerItem);
216    void HandleEventForCompatibleMode(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
217        MMI::PointerEvent::PointerItem& pointerItem);
218    void HandleDownForCompatibleMode(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
219        MMI::PointerEvent::PointerItem& pointerItem);
220    void HandleMoveForCompatibleMode(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
221        MMI::PointerEvent::PointerItem& pointerItem);
222    void HandleUpForCompatibleMode(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
223        MMI::PointerEvent::PointerItem& pointerItem);
224    void ConvertPointForCompatibleMode(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
225        MMI::PointerEvent::PointerItem& pointerItem, int32_t transferX);
226    bool IsInMappingRegionForCompatibleMode(int32_t displayX, int32_t displayY);
227    bool CheckTouchSlop(int32_t pointerId, int32_t x, int32_t y, int32_t threshold);
228    void IgnoreClickEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
229    bool HandlePointDownEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
230        const MMI::PointerEvent::PointerItem& pointerItem, int32_t sourceType, float vpr, const WSRect& rect);
231    std::unique_ptr<Media::PixelMap> HandleWindowMask(const std::vector<std::vector<uint32_t>>& windowMask);
232    void CalculateNewLimitsByLimits(
233        WindowLimits& newLimits, WindowLimits& customizedLimits, float& virtualPixelRatio);
234    void CalculateNewLimitsByRatio(WindowLimits& newLimits, WindowLimits& customizedLimits);
235    void NotifyDisplayInfoChange(const sptr<DisplayInfo>& info = nullptr);
236    void UpdateDensityInner(const sptr<DisplayInfo>& info = nullptr);
237
238    /**
239     * Window Immersive
240     */
241    void UpdateDefaultStatusBarColor();
242    WMError MoveAndResizeKeyboard(const KeyboardLayoutParams& params);
243    bool userLimitsSet_ = false;
244    bool enableDefaultAnimation_ = true;
245    sptr<IAnimationTransitionController> animationTransitionController_;
246    uint32_t setSameSystembarPropertyCnt_ = 0;
247    std::atomic<bool> isDefaultDensityEnabled_ = false;
248    std::atomic<uint32_t> getAvoidAreaCnt_ = 0;
249    bool enableImmersiveMode_ = false;
250    bool titleHoverShowEnabled_ = true;
251    bool dockHoverShowEnabled_ = true;
252    void PreLayoutOnShow(WindowType type, const sptr<DisplayInfo>& info = nullptr);
253
254    /*
255     * Gesture Back
256     */
257    bool gestureBackEnabled_ = true;
258
259    /*
260     * Window Property.
261     */
262    void InitSystemSessionDragEnable();
263
264    /**
265     * Sub Window
266     */
267    void AddSubWindowMapForExtensionWindow();
268    WMError GetParentSessionAndVerify(bool isToast, sptr<WindowSessionImpl>& parentSession);
269
270    /*
271     * system window
272    */
273    WMError SetSystemWindowDisplayId(WindowType type);
274
275    WMError RegisterKeyboardPanelInfoChangeListener(const sptr<IKeyboardPanelInfoChangeListener>& listener) override;
276    WMError UnregisterKeyboardPanelInfoChangeListener(const sptr<IKeyboardPanelInfoChangeListener>& listener) override;
277    static std::mutex keyboardPanelInfoChangeListenerMutex_;
278    sptr<IKeyboardPanelInfoChangeListener> keyboardPanelInfoChangeListeners_ = nullptr;
279    bool isOverTouchSlop_ = false;
280    bool isDown_ = false;
281    std::unordered_map<int32_t, std::vector<bool>> eventMapTriggerByDisplay_;
282    std::unordered_map<int32_t, std::vector<int32_t>> eventMapDeltaXByDisplay_;
283    std::unordered_map<int32_t, std::vector<PointInfo>> downPointerByDisplay_;
284};
285} // namespace Rosen
286} // namespace OHOS
287#endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_IMPL_H
288