1/*
2 * Copyright (C) 2021 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_OBSERVER_CLIENT_H
17#define TELEPHONY_OBSERVER_CLIENT_H
18
19#include <cstdint>
20#include <iremote_object.h>
21#include <singleton.h>
22
23#include "i_telephony_state_notify.h"
24
25namespace OHOS {
26namespace Telephony {
27class TelephonyObserverClient : public DelayedRefSingleton<TelephonyObserverClient> {
28    DECLARE_DELAYED_REF_SINGLETON(TelephonyObserverClient);
29
30public:
31    /**
32     * @brief Add state observer.
33     *
34     * @param telephonyObserver Indicates the TelephonyObserverBroker.
35     * @param slotId Indicates the slot identification.
36     * @param mask Indicates the event type mask.
37     * @param isUpdate Whether to update data immediately.
38     * @return Return 0 if add succeed, others if remove failed.
39     */
40    int32_t AddStateObserver(const sptr<TelephonyObserverBroker> &telephonyObserver,
41        int32_t slotId, uint32_t mask, bool isUpdate);
42
43    /**
44     * @brief Remove state observer.
45     *
46     * @param slotId Indicates the slot identification.
47     * @param mask Indicates event type mask.
48     * @return Return 0 if remove successful, others if remove failed.
49     */
50    int32_t RemoveStateObserver(int32_t slotId, uint32_t mask);
51
52    /**
53     * @brief Get the state registry proxy.
54     *
55     * @return Return the ITelephonyStateNotify interface.
56     */
57    sptr<ITelephonyStateNotify> GetProxy();
58
59private:
60    class StateRegistryDeathRecipient : public IRemoteObject::DeathRecipient {
61    public:
62        explicit StateRegistryDeathRecipient(TelephonyObserverClient &client) : client_(client) {}
63        ~StateRegistryDeathRecipient() override = default;
64        void OnRemoteDied(const wptr<IRemoteObject> &remote) override
65        {
66            client_.OnRemoteDied(remote);
67        }
68
69    private:
70        TelephonyObserverClient &client_;
71    };
72
73    void OnRemoteDied(const wptr<IRemoteObject> &remote);
74
75private:
76    std::mutex mutexProxy_;
77    sptr<ITelephonyStateNotify> proxy_ {nullptr};
78    sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr};
79};
80} // namespace Telephony
81} // namespace OHOS
82#endif // TELEPHONY_OBSERVER_CLIENT_H
83