1/*
2 * Copyright (C) 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 TELEPHONY_IMS_SMS_CLIENT_H
17#define TELEPHONY_IMS_SMS_CLIENT_H
18
19#include "event_handler.h"
20#include "event_runner.h"
21#include "ims_core_service_interface.h"
22#include "ims_sms_interface.h"
23#include "iremote_stub.h"
24#include "rwlock.h"
25#include "singleton.h"
26#include "system_ability_status_change_stub.h"
27
28namespace OHOS {
29namespace Telephony {
30class ImsSmsClient {
31    DECLARE_DELAYED_SINGLETON(ImsSmsClient);
32
33public:
34    /**
35     * @brief Get ImsSms Remote Object.
36     *
37     * @return sptr<ImsSmsInterface>
38     */
39    sptr<ImsSmsInterface> GetImsSmsProxy();
40
41    /**
42     * @brief Init the ImsSmsClient.
43     */
44    void Init();
45
46    /**
47     * @brief UnInit the ImsSmsClient.
48     */
49    void UnInit();
50
51    /**
52     * @brief Register the Handler to process the callback events from ImsSms.
53     *
54     * @param slotId Indicates the card slot index number,
55     * ranging from {@code 0} to the maximum card slot index number supported by the device.
56     * @param handler Indicates the EventHandler to process the call back event.
57     * @return Returns TELEPHONY_SUCCESS on success, others on failure.
58     */
59    int32_t RegisterImsSmsCallbackHandler(int32_t slotId, const std::shared_ptr<AppExecFwk::EventHandler> &handler);
60
61    /**
62     * @brief Get Handler which is used to process the callback events from ImsSms.
63     *
64     * @param slotId Indicates the card slot index number,
65     * ranging from {@code 0} to the maximum card slot index number supported by the device.
66     * @return AppExecFwk::EventHandler The EventHandler to process the call back event.
67     */
68    std::shared_ptr<AppExecFwk::EventHandler> GetHandler(int32_t slotId);
69
70    /****************** sms basic ******************/
71    /**
72     * @brief IMS send message interface.
73     *
74     * @param slotId Indicates the card slot index number,
75     * ranging from {@code 0} to the maximum card slot index number supported by the device.
76     * @param imsMessageInfo Indicates the information of IMS message.
77     * @return Returns TELEPHONY_SUCCESS on success, others on failure.
78     */
79    int32_t ImsSendMessage(int32_t slotId, const ImsMessageInfo &imsMessageInfo);
80
81    /**
82     * @brief IMS set ims sms config interface.
83     *
84     * @param slotId Indicates the card slot index number,
85     * ranging from {@code 0} to the maximum card slot index number supported by the device.
86     * @param imsSmsConfig 1:enable ims sms, 0:disable ims sms
87     * @return Returns TELEPHONY_SUCCESS on success, others on failure.
88     */
89    int32_t ImsSetSmsConfig(int32_t slotId, int32_t imsSmsConfig);
90
91    /**
92     * @brief IMS get sms config interface.
93     *
94     * @param slotId Indicates the card slot index number,
95     * ranging from {@code 0} to the maximum card slot index number supported by the device.
96     * @return Returns TELEPHONY_SUCCESS on success, others on failure.
97     */
98    int32_t ImsGetSmsConfig(int32_t slotId);
99
100private:
101    class SystemAbilityListener : public SystemAbilityStatusChangeStub {
102    public:
103        SystemAbilityListener() {}
104        ~SystemAbilityListener() {}
105    public:
106        void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
107        void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
108    };
109
110    /**
111     * @brief Is Connect ImsSms Remote Object.
112     *
113     * @return bool
114     */
115    bool IsConnect() const;
116
117    /**
118     * @brief Register CallBack to listen the response from ImsSms.
119     *
120     * @param sptr<ImsSmsCallbackInterface>
121     * @return Returns TELEPHONY_SUCCESS on success, others on failure.
122     */
123    int32_t RegisterImsSmsCallback();
124
125    /**
126     * @brief Reconnect the ImsSms Remote Object.
127     *
128     * @return int32_t Returns TELEPHONY_SUCCESS on success, others on failure.
129     */
130    int32_t ReConnectService();
131
132    /**
133     * @brief Clean the ImsSms Remote Object.
134     */
135    void Clean();
136
137private:
138    sptr<ImsCoreServiceInterface> imsCoreServiceProxy_ = nullptr;
139    sptr<ImsSmsInterface> imsSmsProxy_ = nullptr;
140    sptr<ImsSmsCallbackInterface> imsSmsCallback_ = nullptr;
141    std::map<int32_t, std::shared_ptr<AppExecFwk::EventHandler>> handlerMap_;
142    Utils::RWLock rwClientLock_;
143    std::mutex mutex_;
144    std::mutex mutexCallback_;
145    sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr;
146};
147} // namespace Telephony
148} // namespace OHOS
149
150#endif // TELEPHONY_IMS_SMS_CLIENT_H
151