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_FORM_FWK_FORM_AMS_HELPER_H 17 #define OHOS_FORM_FWK_FORM_AMS_HELPER_H 18 19 #include <singleton.h> 20 21 #include "ability_connect_callback_interface.h" 22 #include "ability_manager_interface.h" 23 #include "form_event_handler.h" 24 #include "form_serial_queue.h" 25 #include "iconfiguration_observer.h" 26 #include "uri.h" 27 28 namespace OHOS { 29 namespace AppExecFwk { 30 using IAbilityConnection = OHOS::AAFwk::IAbilityConnection; 31 using Want = OHOS::AAFwk::Want; 32 const int FORM_DISCONNECT_DELAY_TIME = 10000; // ms 33 /** 34 * @class FormAmsHelper 35 * Ams helpler. 36 */ 37 class FormAmsHelper final : public DelayedRefSingleton<FormAmsHelper> { 38 DECLARE_DELAYED_REF_SINGLETON(FormAmsHelper) 39 public: 40 DISALLOW_COPY_AND_MOVE(FormAmsHelper); 41 /** 42 * @brief SetEventHandler. 43 * @param handler event handler 44 */ SetSerialQueue(const std::shared_ptr<FormSerialQueue> & serialQueue)45 inline void SetSerialQueue(const std::shared_ptr<FormSerialQueue> &serialQueue) 46 { 47 serialQueue_ = serialQueue; 48 } 49 /** 50 * @brief acquire a form ability manager if it not existed, 51 * @return returns the ability manager ipc object or nullptr for failed. 52 */ 53 sptr<AAFwk::IAbilityManager> GetAbilityManager(); 54 /** 55 * @brief Connect session with service ability. 56 * @param want Special want for service type's ability. 57 * @param connect Callback used to notify caller the result of connecting or disconnecting. 58 * @return Returns ERR_OK on success, others on failure. 59 */ 60 ErrCode ConnectServiceAbility( 61 const Want &want, const sptr<AAFwk::IAbilityConnection> &connect); 62 /** 63 * @brief Connect session with service ability. 64 * @param want Special want for service type's ability. 65 * @param connect Callback used to notify caller the result of connecting or disconnecting. 66 * @param userId Designation User ID. 67 * @return Returns ERR_OK on success, others on failure. 68 */ 69 ErrCode ConnectServiceAbilityWithUserId( 70 const Want &want, const sptr<AAFwk::IAbilityConnection> &connect, int32_t userId); 71 /** 72 * @brief Disconnect session with service ability. 73 * @param connect Callback used to notify caller the result of connecting or disconnecting. 74 * @return Returns ERR_OK on success, others on failure. 75 */ 76 ErrCode DisconnectServiceAbility(const sptr<AAFwk::IAbilityConnection> &connect); 77 /** 78 * @brief Disconnect ability delay, disconnect session with service ability. 79 * @param connect Callback used to notify caller the result of connecting or disconnecting. 80 * @param delayTime Special time to delay, default is FORM_DISCONNECT_DELAY_TIME. 81 * @return Returns ERR_OK on success, others on failure. 82 */ 83 ErrCode DisconnectServiceAbilityDelay( 84 const sptr<AAFwk::IAbilityConnection> &connect, int delayTime = FORM_DISCONNECT_DELAY_TIME); 85 /** 86 * @brief StartAbility with want, send want to ability manager service. 87 * @param want The want of the ability to start. 88 * @param userId Designation User ID. 89 * @return Returns ERR_OK on success, others on failure. 90 */ 91 ErrCode StartAbility(const Want &want, int32_t userId); 92 /** 93 * @brief Add the ability manager instance for debug. 94 * @param abilityManager the ability manager ipc object. 95 */ 96 void SetAbilityManager(const sptr<AAFwk::IAbilityManager> &abilityManager); 97 /** 98 * @brief StopExtensionAbility with want, send want to ability manager service. 99 * @param want The want of the ability to start. 100 * @return Returns ERR_OK on success, others on failure. 101 */ 102 ErrCode StopExtensionAbility(const Want &want); 103 /** 104 * @brief register Configuration Observer. 105 */ 106 void RegisterConfigurationObserver(); 107 /** 108 * @brief unregister Configuration Observer. 109 */ 110 void UnRegisterConfigurationObserver(); 111 private: 112 /** 113 * @brief Disconnect ability task, disconnect session with service ability. 114 * @param want Special want for service type's ability. 115 * @param connect Callback used to notify caller the result of connecting or disconnecting. 116 */ 117 void DisconnectAbilityTask(const sptr<AAFwk::IAbilityConnection> &connect); 118 private: 119 sptr<AAFwk::IAbilityManager> abilityManager_ = nullptr; 120 std::shared_ptr<FormSerialQueue> serialQueue_ = nullptr; 121 sptr<IConfigurationObserver> configurationObserver = nullptr; 122 }; 123 } // namespace AppExecFwk 124 } // namespace OHOS 125 126 #endif // OHOS_FORM_FWK_FORM_AMS_HELPER_H 127