1 /*
2  * Copyright (c) 2021-2024 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 BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_PROXY_H
17 #define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_PROXY_H
18 
19 #include "ans_subscriber_interface.h"
20 #include "distributed_notification_service_ipc_interface_code.h"
21 #include "iremote_proxy.h"
22 
23 namespace OHOS {
24 namespace Notification {
25 class AnsSubscriberProxy : public IRemoteProxy<AnsSubscriberInterface> {
26 public:
27     AnsSubscriberProxy() = delete;
28     explicit AnsSubscriberProxy(const sptr<IRemoteObject> &impl);
29     ~AnsSubscriberProxy() override;
30     DISALLOW_COPY_AND_MOVE(AnsSubscriberProxy);
31 
32     /**
33      * @brief The callback function for the subscriber to establish a connection.
34      */
35     void OnConnected() override;
36 
37     /**
38      * @brief The callback function for subscriber disconnected.
39      */
40     void OnDisconnected() override;
41 
42     /**
43      * @brief The callback function on a notification published.
44      *
45      * @param notification Indicates the consumed notification.
46      * @param notificationMap Indicates the NotificationSortingMap object.
47      */
48     void OnConsumed(
49         const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap) override;
50 
51     void OnConsumedList(const std::vector<sptr<Notification>> &notifications,
52         const sptr<NotificationSortingMap> &notificationMap) override;
53 
54     /**
55      * @brief The callback function on a notification canceled.
56      *
57      * @param notification Indicates the canceled notification.
58      * @param deleteReason Indicates the delete reason.
59      */
60     void OnCanceled(const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap,
61         int32_t deleteReason) override;
62 
63     void OnCanceledList(const std::vector<sptr<Notification>> &notifications,
64         const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason) override;
65 
66     /**
67      * @brief The callback function on the notifications updated.
68      *
69      * @param notificationMap Indicates the NotificationSortingMap object.
70      */
71     void OnUpdated(const sptr<NotificationSortingMap> &notificationMap) override;
72 
73     /**
74      * @brief The callback function on the do not disturb date changed.
75      *
76      * @param date Indicates the NotificationDoNotDisturbDate object.
77      */
78     void OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date) override;
79 
80     /**
81      * @brief The callback function on the notification enabled flag changed.
82      *
83      * @param callbackData Indicates the EnabledNotificationCallbackData object.
84      */
85     void OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override;
86 
87     /**
88      * @brief The callback function on the badge number changed.
89      *
90      * @param badgeData Indicates the BadgeNumberCallbackData object.
91      */
92     void OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData) override;
93 
94     /**
95      * @brief The callback function on the badge enabled state changed.
96      *
97      * @param callbackData Indicates the EnabledNotificationCallbackData object.
98      */
99     void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override;
100 
101 private:
102     ErrCode InnerTransact(NotificationInterfaceCode code, MessageOption &flags,
103         MessageParcel &data, MessageParcel &reply);
104     static inline BrokerDelegator<AnsSubscriberProxy> delegator_;
105 
106     template<typename T>
WriteParcelableVector(const std::vector<sptr<T>> & parcelableVector,MessageParcel & data)107     bool WriteParcelableVector(const std::vector<sptr<T>> &parcelableVector, MessageParcel &data)
108     {
109         if (!data.WriteInt32(parcelableVector.size())) {
110             ANS_LOGE("write ParcelableVector size failed");
111             return false;
112         }
113 
114         for (auto &parcelable : parcelableVector) {
115             if (!data.WriteStrongParcelable(parcelable)) {
116                 ANS_LOGE("write ParcelableVector failed");
117                 return false;
118             }
119         }
120         return true;
121     }
122 };
123 }  // namespace Notification
124 }  // namespace OHOS
125 
126 #endif  // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_PROXY_H
127