1 /* 2 * Copyright (c) 2025-2025 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_FLOATING_BALL_CONTROLLER_H 17 #define OHOS_FLOATING_BALL_CONTROLLER_H 18 19 #include <refbase.h> 20 #include <ability_context.h> 21 #include "window.h" 22 #include "wm_common.h" 23 #include "floating_ball_option.h" 24 #include "floating_ball_interface.h" 25 #include "floating_ball_report.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 using namespace Ace; 30 class FloatingBallController : public RefBase { 31 public: 32 FloatingBallController(const sptr<Window>& mainWindow, const uint32_t& mainWindowId, void* context); 33 virtual ~FloatingBallController(); 34 WMError StartFloatingBall(sptr<FbOption>& option); 35 WMError StopFloatingBall(); 36 WMError UpdateFloatingBall(sptr<FbOption>& option); 37 WMError RestoreMainWindow(const std::shared_ptr<AAFwk::Want>& want); 38 WMError DestroyFloatingBallWindow(); 39 sptr<Window> GetFbWindow() const; 40 FbWindowState GetControllerState() const; 41 WMError StopFloatingBallFromClient(); 42 43 WMError RegisterFbLifecycle(const sptr<IFbLifeCycle>& listener); 44 WMError RegisterFbClickObserver(const sptr<IFbClickObserver>& listener); 45 WMError UnRegisterFbLifecycle(const sptr<IFbLifeCycle>& listener); 46 WMError UnRegisterFbClickObserver(const sptr<IFbClickObserver>& listener); 47 48 void OnFloatingBallClick(); 49 50 WMError GetFloatingBallWindowInfo(uint32_t& windowId); 51 52 void UpdateMainWindow(const sptr<Window>& mainWindow); 53 54 private: 55 WMError CreateFloatingBallWindow(const sptr<FbOption>& option); 56 WMError StartFloatingBallInner(const sptr<FbOption>& option); 57 58 template<typename T> WMError RegisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener); 59 template<typename T> WMError UnRegisterListener(std::vector<sptr<T>>& holder, const sptr<T>& listener); 60 61 void OnFloatingBallStart(); 62 void OnFloatingBallStop(); 63 64 wptr<FloatingBallController> weakRef_ = nullptr; 65 sptr<Window> window_ = nullptr; 66 sptr<Window> mainWindow_ = nullptr; 67 uint32_t mainWindowId_ = 0; 68 uint32_t templateType_ = 0; 69 FbWindowState curState_ = FbWindowState::STATE_UNDEFINED; 70 void* contextPtr_ = nullptr; 71 bool stopFromClient_ = false; 72 73 std::vector<sptr<IFbLifeCycle>> fbLifeCycleListeners_; 74 std::vector<sptr<IFbClickObserver>> fbClickObservers_; 75 76 std::mutex controllerMutex_; 77 78 class WindowLifeCycleListener : public IWindowLifeCycle { 79 public: 80 void AfterDestroyed() override; 81 }; 82 sptr<IWindowLifeCycle> mainWindowLifeCycleListener_ = nullptr; 83 std::mutex listenerMutex_; 84 }; 85 } // namespace Rosen 86 } // namespace OHOS 87 #endif // OHOS_FLOATING_BALL_CONTROLLER_H