1 /*
2  * Copyright (c) 2023-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_PICTURE_IN_PICTURE_CONTROLLER_H
17 #define OHOS_PICTURE_IN_PICTURE_CONTROLLER_H
18 
19 #define PIP_WINDOW_NAME "pip_window"
20 
21 #include <refbase.h>
22 #include <ability_context.h>
23 #include "picture_in_picture_option.h"
24 #include "window.h"
25 #include "wm_common.h"
26 #include "picture_in_picture_interface.h"
27 #include "xcomponent_controller.h"
28 #include "pip_report.h"
29 #include "navigation_controller.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 
34 enum class StartPipType : uint32_t {
35     NULL_START = 0,
36     USER_START = 1,
37     AUTO_START = 2,
38     ONE_STEP_START = 3,
39 };
40 
41 enum class StopPipType : uint32_t {
42     NULL_STOP = 0,
43     USER_STOP = 1,
44     OTHER_PACKAGE_STOP = 2,
45     PACKAGE_STOP = 3,
46 };
47 
48 static std::map<std::string, PiPControlType> CONTROL_TYPE_MAP = {
49     {"playbackStateChanged", PiPControlType::VIDEO_PLAY_PAUSE},
50     {"nextVideo", PiPControlType::VIDEO_NEXT},
51     {"previousVideo", PiPControlType::VIDEO_PREVIOUS},
52     {"hangUp", PiPControlType::HANG_UP_BUTTON},
53     {"micStateChanged", PiPControlType::MICROPHONE_SWITCH},
54     {"videoStateChanged", PiPControlType::CAMERA_SWITCH},
55     {"voiceStateChanged", PiPControlType::MUTE_SWITCH},
56     {"fastForward", PiPControlType::FAST_FORWARD},
57     {"fastBackward", PiPControlType::FAST_BACKWARD}
58 };
59 
60 using namespace Ace;
61 class PictureInPictureController : virtual public RefBase {
62 public:
63     PictureInPictureController(sptr<PipOption> pipOption, sptr<Window> mainWindow, uint32_t mainWindowId, napi_env env);
64     ~PictureInPictureController();
65     WMError StartPictureInPicture(StartPipType startType);
66     WMError StopPictureInPicture(bool destroyWindow, StopPipType stopPipType, bool withAnim = true);
67     WMError StopPictureInPictureFromClient();
68     WMError DestroyPictureInPictureWindow();
69     sptr<Window> GetPipWindow() const;
70     uint32_t GetMainWindowId();
71     void SetPipWindow(sptr<Window> window);
72     void SetAutoStartEnabled(bool enable);
73     void IsAutoStartEnabled(bool& enable) const;
74     void UpdateContentSize(int32_t width, int32_t height);
75     void UpdatePiPControlStatus(PiPControlType controlType, PiPControlStatus status);
76     bool IsContentSizeChanged(float width, float height, float posX, float posY);
77     void DoActionEvent(const std::string& actionName, int32_t status);
78     void DoControlEvent(PiPControlType controlType, PiPControlStatus status);
79     void PreRestorePictureInPicture();
80     void RestorePictureInPictureWindow();
81     void PrepareSource();
82     void LocateSource();
83     WMError RegisterPiPLifecycle(const sptr<IPiPLifeCycle>& listener);
84     WMError RegisterPiPActionObserver(const sptr<IPiPActionObserver>& listener);
85     WMError RegisterPiPControlObserver(const sptr<IPiPControlObserver>& listener);
86     WMError UnregisterPiPLifecycle(const sptr<IPiPLifeCycle>& listener);
87     WMError UnregisterPiPActionObserver(const sptr<IPiPActionObserver>& listener);
88     WMError UnregisterPiPControlObserver(const sptr<IPiPControlObserver>& listener);
89     sptr<IPiPLifeCycle> GetPictureInPictureLifecycle() const;
90     sptr<IPiPActionObserver> GetPictureInPictureActionObserver() const;
91     sptr<IPiPControlObserver> GetPictureInPictureControlObserver() const;
92     WMError SetXComponentController(std::shared_ptr<XComponentController> xComponentController);
93     PiPWindowState GetControllerState();
94     std::string GetPiPNavigationId();
95     napi_ref GetCustomNodeController();
96     napi_ref GetTypeNode() const;
97     void OnPictureInPictureStart();
98     bool IsTypeNodeEnabled() const;
99 
100 private:
101     class WindowLifeCycleListener : public IWindowLifeCycle {
102     public:
103         void AfterDestroyed() override;
104     };
105 
106 private:
107     uint32_t GetPipPriority(uint32_t pipTemplateType);
108     WMError CreatePictureInPictureWindow(StartPipType startType);
109     WMError ShowPictureInPictureWindow(StartPipType startType);
110     WMError StartPictureInPictureInner(StartPipType startType);
111     WMError StopPictureInPictureInner(StopPipType stopType, bool withAnim);
112     void UpdateWinRectByComponent();
113     void UpdatePiPSourceRect() const;
114     void ResetExtController();
115     bool IsPullPiPAndHandleNavigation();
116     template<typename T> WMError RegisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener);
117     template<typename T> WMError UnregisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener);
118     wptr<PictureInPictureController> weakRef_ = nullptr;
119     sptr<PipOption> pipOption_ = nullptr;
120     std::vector<sptr<IPiPLifeCycle>> pipLifeCycleListeners_;
121     std::vector<sptr<IPiPActionObserver>> pipActionObservers_;
122     std::vector<sptr<IPiPControlObserver>> pipControlObservers_;
123     sptr<Window> window_ = nullptr;
124     sptr<Window> mainWindow_ = nullptr;
125     sptr<IWindowLifeCycle> mainWindowLifeCycleListener_ = nullptr;
126     uint32_t mainWindowId_ = 0;
127     Rect windowRect_ = {0, 0, 0, 0};
128     bool isAutoStartEnabled_ = false;
129     PiPWindowState curState_ = PiPWindowState::STATE_UNDEFINED;
130     std::shared_ptr<XComponentController> pipXComponentController_ = nullptr;
131     std::shared_ptr<XComponentController> mainWindowXComponentController_ = nullptr;
132     napi_env env_ = nullptr;
133     int32_t handleId_ = -1;
134     bool isStoppedFromClient_ = false;
135 };
136 } // namespace Rosen
137 } // namespace OHOS
138 #endif // OHOS_PICTURE_IN_PICTURE_CONTROLLER_H
139