1/*
2 * Copyright (c) 2022-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 FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_ABILITY_MANAGER_HELPER_H
17#define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_ABILITY_MANAGER_HELPER_H
18
19#include "ability_connect_callback_interface.h"
20#include "ability_manager_interface.h"
21#include "ability_manager_death_recipient.h"
22#include "common_event_data.h"
23#include "event_handler.h"
24#include "ffrt.h"
25#include "singleton.h"
26#include "static_subscriber_connection.h"
27
28namespace OHOS {
29namespace EventFwk {
30class AbilityManagerHelper : public DelayedSingleton<AbilityManagerHelper> {
31public:
32    AbilityManagerHelper() {}
33
34    virtual ~AbilityManagerHelper() {}
35
36    /**
37     * @brief SetEventHandler.
38     * @param handler event handler
39     */
40    inline void SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
41    {
42        eventHandler_ = handler;
43    }
44
45    /**
46     * Connects ability.
47     *
48     * @param want Indicates ability inofmation.
49     * @param event Indicates the common event
50     * @param callerToken Indicates the token of caller.
51     * @param userId Indicates the ID of user.
52     * @return Returns result code.
53     */
54    int ConnectAbility(const AAFwk::Want &want, const CommonEventData &event,
55        const sptr<IRemoteObject> &callerToken, const int32_t &userId);
56
57    /**
58     * Clears ability manager service remote object.
59     *
60     */
61    void Clear();
62
63    /**
64     * @brief Disconnect ability delay.
65     * @param connection Indicates the connection want to disconnect.
66     */
67    void DisconnectServiceAbilityDelay(const sptr<StaticSubscriberConnection> &connection);
68private:
69    bool GetAbilityMgrProxy();
70    void DisconnectAbility(const sptr<StaticSubscriberConnection> &connection);
71
72    std::mutex mutex_;
73    sptr<AAFwk::IAbilityManager> abilityMgr_;
74    sptr<AbilityManagerDeathRecipient> deathRecipient_;
75    std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ = nullptr;
76    std::set<sptr<StaticSubscriberConnection>> subscriberConnection_;
77    std::shared_ptr<ffrt::queue> ffrt_ = nullptr;
78};
79}  // namespace EventFwk
80}  // namespace OHOS
81
82#endif  // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_ABILITY_MANAGER_HELPER_H
83