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 Disconnect session with service ability.
64      * @param connect Callback used to notify caller the result of connecting or disconnecting.
65      * @return Returns ERR_OK on success, others on failure.
66      */
67     ErrCode DisconnectServiceAbility(const sptr<AAFwk::IAbilityConnection> &connect);
68     /**
69      * @brief Disconnect ability delay, disconnect session with service ability.
70      * @param connect Callback used to notify caller the result of connecting or disconnecting.
71      * @param delayTime Special time to delay, default is FORM_DISCONNECT_DELAY_TIME.
72      * @return Returns ERR_OK on success, others on failure.
73      */
74     ErrCode DisconnectServiceAbilityDelay(
75         const sptr<AAFwk::IAbilityConnection> &connect, int delayTime = FORM_DISCONNECT_DELAY_TIME);
76     /**
77      * @brief StartAbility with want, send want to ability manager service.
78      * @param want The want of the ability to start.
79      * @param userId Designation User ID.
80      * @return Returns ERR_OK on success, others on failure.
81      */
82     ErrCode StartAbility(const Want &want, int32_t userId);
83     /**
84      * @brief Add the ability manager instance for debug.
85      * @param abilityManager the ability manager ipc object.
86      */
87     void SetAbilityManager(const sptr<AAFwk::IAbilityManager> &abilityManager);
88     /**
89      * @brief StopExtensionAbility with want, send want to ability manager service.
90      * @param want The want of the ability to start.
91      * @return Returns ERR_OK on success, others on failure.
92      */
93     ErrCode StopExtensionAbility(const Want &want);
94     /**
95      * @brief register Configuration Observer.
96      */
97     void RegisterConfigurationObserver();
98     /**
99      * @brief unregister Configuration Observer.
100      */
101     void UnRegisterConfigurationObserver();
102 private:
103     /**
104      * @brief Disconnect ability task, disconnect session with service ability.
105      * @param want Special want for service type's ability.
106      * @param connect Callback used to notify caller the result of connecting or disconnecting.
107      */
108     void DisconnectAbilityTask(const sptr<AAFwk::IAbilityConnection> &connect);
109 private:
110     sptr<AAFwk::IAbilityManager> abilityManager_ = nullptr;
111     std::shared_ptr<FormSerialQueue> serialQueue_ = nullptr;
112     sptr<IConfigurationObserver> configurationObserver = nullptr;
113 };
114 }  // namespace AppExecFwk
115 }  // namespace OHOS
116 
117 #endif // OHOS_FORM_FWK_FORM_AMS_HELPER_H
118