1 /* 2 * Copyright (c) 2021-2022 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 "uri.h" 25 26 namespace OHOS { 27 namespace AppExecFwk { 28 using IAbilityConnection = OHOS::AAFwk::IAbilityConnection; 29 using Want = OHOS::AAFwk::Want; 30 /** 31 * @class FormAmsHelper 32 * Ams helpler. 33 */ 34 class FormAmsHelper final : public DelayedRefSingleton<FormAmsHelper> { 35 DECLARE_DELAYED_REF_SINGLETON(FormAmsHelper) 36 public: 37 DISALLOW_COPY_AND_MOVE(FormAmsHelper); 38 /** 39 * @brief SetEventHandler. 40 * @param handler event handler 41 */ SetEventHandler(const std::shared_ptr<FormEventHandler> & handler)42 inline void SetEventHandler(const std::shared_ptr<FormEventHandler> &handler) 43 { 44 eventHandler_ = handler; 45 } 46 /** 47 * @brief acquire a form ability manager if it not existed, 48 * @return returns the ability manager ipc object or nullptr for failed. 49 */ 50 sptr<AAFwk::IAbilityManager> GetAbilityManager(); 51 /** 52 * @brief Connect session with service ability. 53 * @param want Special want for service type's ability. 54 * @param connect Callback used to notify caller the result of connecting or disconnecting. 55 * @return Returns ERR_OK on success, others on failure. 56 */ 57 ErrCode ConnectServiceAbility( 58 const Want &want, const sptr<AAFwk::IAbilityConnection> &connect); 59 /** 60 * @brief Disconnect session with service ability. 61 * @param want Special want for service type's ability. 62 * @param connect Callback used to notify caller the result of connecting or disconnecting. 63 * @return Returns ERR_OK on success, others on failure. 64 */ 65 ErrCode DisconnectServiceAbility(const sptr<AAFwk::IAbilityConnection> &connect); 66 /** 67 * @brief Disconnect ability delay, disconnect session with service ability. 68 * @param want Special want for service type's ability. 69 * @param connect Callback used to notify caller the result of connecting or disconnecting. 70 * @return Returns ERR_OK on success, others on failure. 71 */ 72 ErrCode DisconnectServiceAbilityDelay(const sptr<AAFwk::IAbilityConnection> &connect); 73 /** 74 * @brief StartAbility with want, send want to ability manager service. 75 * @param want The want of the ability to start. 76 * @param userId Designation User ID. 77 * @return Returns ERR_OK on success, others on failure. 78 */ 79 ErrCode StartAbility(const Want &want, int32_t userId); 80 /** 81 * @brief Add the ability manager instance for debug. 82 * @param abilityManager the ability manager ipc object. 83 */ 84 void SetAbilityManager(const sptr<AAFwk::IAbilityManager> &abilityManager); 85 private: 86 /** 87 * @brief Disconnect ability task, disconnect session with service ability. 88 * @param want Special want for service type's ability. 89 * @param connect Callback used to notify caller the result of connecting or disconnecting. 90 */ 91 void DisconnectAbilityTask(const sptr<AAFwk::IAbilityConnection> &connect); 92 private: 93 sptr<AAFwk::IAbilityManager> abilityManager_ = nullptr; 94 std::shared_ptr<FormEventHandler> eventHandler_ = nullptr; 95 }; 96 } // namespace AppExecFwk 97 } // namespace OHOS 98 99 #endif // OHOS_FORM_FWK_FORM_AMS_HELPER_H 100