• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <event_handler.h>
22 #include <refbase.h>
23 #include <mutex>
24 #include <ability_context.h>
25 #include "picture_in_picture_option.h"
26 #include "window.h"
27 #include "wm_common.h"
28 #include "picture_in_picture_interface.h"
29 #include "xcomponent_controller.h"
30 #include "pip_report.h"
31 #include "navigation_controller.h"
32 #include "display_manager.h"
33 
34 namespace OHOS {
35 namespace Rosen {
36 
37 enum class StartPipType : uint32_t {
38     NULL_START = 0,
39     USER_START = 1,
40     AUTO_START = 2,
41     ONE_STEP_START = 3,
42 };
43 
44 enum class StopPipType : uint32_t {
45     NULL_STOP = 0,
46     USER_STOP = 1,
47     OTHER_PACKAGE_STOP = 2,
48     PACKAGE_STOP = 3,
49 };
50 
51 using namespace Ace;
52 class PictureInPictureController : virtual public RefBase {
53 public:
54     constexpr static int32_t DEFAULT_TIME_DELAY = 400;
55     PictureInPictureController(sptr<PipOption> pipOption, sptr<Window> mainWindow, uint32_t mainWindowId, napi_env env);
56     ~PictureInPictureController();
57     WMError StartPictureInPicture(StartPipType startType);
58     WMError StopPictureInPicture(bool destroyWindow, bool needAnim, StopPipType stopPipType);
59     sptr<Window> GetPipWindow();
60     uint32_t GetMainWindowId();
61     void SetPipWindow(sptr<Window> window);
62     void SetAutoStartEnabled(bool enable);
63     void IsAutoStartEnabled(bool& enable) const;
64     void UpdateContentSize(int32_t width, int32_t height);
65     void StartMove();
66     void DoScale();
67     void DoActionEvent(std::string& actionName);
68     void RestorePictureInPictureWindow();
69     void SetPictureInPictureLifecycle(sptr<IPiPLifeCycle> listener);
70     void SetPictureInPictureActionObserver(sptr<IPiPActionObserver> listener);
71     sptr<IPiPLifeCycle> GetPictureInPictureLifecycle() const;
72     sptr<IPiPActionObserver> GetPictureInPictureActionObserver() const;
73     WMError SetXComponentController(std::shared_ptr<XComponentController> xComponentController);
74     PipWindowState GetControllerState();
75     std::string GetPiPNavigationId();
76 
77     class PipMainWindowLifeCycleImpl : public Rosen::IWindowLifeCycle {
78     public:
PipMainWindowLifeCycleImpl(const std::string & navigationId)79         PipMainWindowLifeCycleImpl(const std::string& navigationId)
80         {
81             navigationId_ = navigationId;
82         };
~PipMainWindowLifeCycleImpl()83         ~PipMainWindowLifeCycleImpl() {};
84         void AfterBackground() override;
85         void BackgroundFailed(int32_t type) override;
86     private:
87         std::string navigationId_ = "";
88     };
89 
90     class PipDisplayListener : public OHOS::Rosen::DisplayManager::IDisplayListener {
91     public:
PipDisplayListener(wptr<PictureInPictureController> pipController)92         PipDisplayListener(wptr<PictureInPictureController> pipController)
93         {
94             pipController_ = pipController;
95             preRotation_ = Rosen::DisplayManager::GetInstance().GetDefaultDisplay()->GetRotation();
96         }
97         void OnCreate(DisplayId displayId) override;
98         void OnDestroy(DisplayId displayId) override;
99         void OnChange(DisplayId displayId) override;
100     private:
101         wptr<PictureInPictureController> pipController_;
102         Rotation preRotation_;
103     };
104 
105 private:
106     WMError CreatePictureInPictureWindow();
107     WMError ShowPictureInPictureWindow(StartPipType startType);
108     WMError StartPictureInPictureInner(StartPipType startType);
109     WMError StopPictureInPictureInner(bool needAnim, StopPipType stopType);
110     void UpdateXComponentPositionAndSize();
111     void ResetExtController();
112     bool IsPullPiPAndHandleNavigation();
113     wptr<PictureInPictureController> weakRef_ = nullptr;
114     sptr<PipOption> pipOption_;
115     sptr<IPiPLifeCycle> pipLifeCycleListener_;
116     sptr<IPiPActionObserver> pipActionObserver_;
117     sptr<Window> window_;
118     sptr<Window> mainWindow_;
119     uint32_t mainWindowId_;
120     Rect windowRect_ = {0, 0, 0, 0};
121     bool isAutoStartEnabled_ = false;
122     PipWindowState curState_ = PipWindowState::STATE_UNDEFINED;
123     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
124     std::shared_ptr<XComponentController> pipXComponentController_;
125     std::shared_ptr<XComponentController> mainWindowXComponentController_;
126     napi_env env_;
127     std::mutex mutex_;
128     int32_t handleId_ = -1;
129     sptr<PictureInPictureController::PipDisplayListener> pipDisplayListener_;
130 };
131 } // namespace Rosen
132 } // namespace OHOS
133 #endif // OHOS_PICTURE_IN_PICTURE_CONTROLLER_H
134