• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_MANAGER_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_MANAGER_H
18 
19 #include <string>
20 
21 #include "singleton.h"
22 
23 #include "event_handler.h"
24 #include "event_runner.h"
25 
26 #include "distributed_database.h"
27 #include "distributed_database_callback.h"
28 #include "distributed_device_callback.h"
29 #include "notification_request.h"
30 
31 namespace OHOS {
32 namespace Notification {
33 class DistributedNotificationManager : public DelayedSingleton<DistributedNotificationManager> {
34 public:
35     /**
36      * @brief Distributed notification callback function for remote device.
37      */
38     struct IDistributedCallback {
39         std::function<void(
40             const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request)>
41             OnPublish;
42         std::function<void(
43             const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request)>
44             OnUpdate;
45         std::function<void(
46             const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id)>
47             OnDelete;
48     };
49 
50     /**
51      * @brief Publishes a local notification to remote device.
52      *
53      * @param bundleName Indicates the bundle name of the application whose notifications are to be publish.
54      * @param label Indicates the label of the notifications.
55      * @param id Indicates the bundle uid of the application whose notifications are to be publish.
56      * @param request Indicates the NotificationRequest object for setting the notification content.
57      * @return ErrCode Returns the publish result.
58      */
59     ErrCode Publish(
60         const std::string &bundleName, const std::string &label, int32_t id, const sptr<NotificationRequest> &request);
61 
62     /**
63      * @brief Updates infomation of local notification to remote device.
64      *
65      * @param bundleName Indicates the bundle name of the application whose notifications are to be update.
66      * @param label Indicates the label of the notifications.
67      * @param id Indicates the bundle uid of the application whose notifications are to be update.
68      * @param request Indicates the NotificationRequest object for setting the notification content.
69      * @return ErrCode Returns the update result.
70      */
71     ErrCode Update(
72         const std::string &bundleName, const std::string &label, int32_t id, const sptr<NotificationRequest> &request);
73 
74     /**
75      * @brief Removes a local notification.
76      *
77      * @param bundleName Indicates the bundle name of the application whose notifications are to be remove.
78      * @param label Indicates the label of the notifications.
79      * @param id Indicates the bundle uid of the application whose notifications are to be remove.
80      * @return ErrCode Returns the remove result.
81      */
82     ErrCode Delete(const std::string &bundleName, const std::string &label, int32_t id);
83 
84     /**
85      * @brief Removes a remote notification.
86      *
87      * @param deviceId Indicates the ID of the device.
88      * @param bundleName Indicates the bundle name of the application whose notifications are to be remove.
89      * @param label Indicates the label of the notifications.
90      * @param id Indicates the bundle uid of the application whose notifications are to be remove.
91      * @return ErrCode Returns the remove result.
92      */
93     ErrCode DeleteRemoteNotification(
94         const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id);
95 
96     /**
97      * @brief Register callback of distributed notification changed.
98      *
99      * @param callback Indicates the callback structure
100      * @return ErrCode Returns the register result.
101      */
102     ErrCode RegisterCallback(const IDistributedCallback &callback);
103 
104     /**
105      * @brief Unregister Callback of Distributed notification changed.
106      *
107      * @return ErrCode Returns the unregister result.
108      */
109     ErrCode UngegisterCallback(void);
110 
111     /**
112      * @brief Get all distributed notification in database.
113      *
114      * @param requestList Indicates the list of NotificationRequest object for setting the notification content.
115      * @return ErrCode Returns Get all distributed notification result.
116      */
117     ErrCode GetCurrentDistributedNotification(std::vector<sptr<NotificationRequest>> &requestList);
118 
119     /**
120      * @brief Get local device info.
121      *
122      * @param deviceInfo Indicates the infomation of local device.
123      * @return ErrCode Returns get device infomation result.
124      */
125     ErrCode GetLocalDeviceInfo(DistributedDatabase::DeviceInfo &deviceInfo);
126 
127     /**
128      * @brief Obtains the death event of the Distributed KvStore service.
129      */
130     ErrCode OnDistributedKvStoreDeathRecipient();
131 
132 private:
133     void OnDatabaseInsert(const std::string &deviceId, const std::string &key, const std::string &value);
134     void OnDatabaseUpdate(const std::string &deviceId, const std::string &key, const std::string &value);
135     void OnDatabaseDelete(const std::string &deviceId, const std::string &key, const std::string &value);
136     void OnDeviceConnected(const std::string &deviceId);
137     void OnDeviceDisconnected(const std::string &deviceId);
138 
139 private:
140     struct ResolveKey {
141         std::string deviceId;
142         std::string bundleName;
143         std::string label;
144         int32_t id = 0;
145     };
146 
147     void GenerateDistributedKey(const std::string &deviceId, const std::string &bundleName, const std::string &label,
148         int32_t id, std::string &key);
149     bool GenerateLocalDistributedKey(
150         const std::string &bundleName, const std::string &label, int32_t id, std::string &key);
151     bool ResolveDistributedKey(const std::string &key, ResolveKey &resolveKey);
152     bool GetDeviceIdFromKey(const std::string &key, std::string &deviceId);
153     bool CheckDeviceId(const std::string &deviceId, const std::string &key);
154 
155     bool PublishCallback(
156         const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request);
157     bool UpdateCallback(const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request);
158     bool DeleteCallback(
159         const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id);
160 
161 private:
162     std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ = nullptr;
163     std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ = nullptr;
164     std::shared_ptr<DistributedDatabase> database_ = nullptr;
165 
166     std::shared_ptr<DistributedDatabaseCallback> databaseCb_;
167     std::shared_ptr<DistributedDeviceCallback> deviceCb_;
168     IDistributedCallback callback_ = {0};
169 
170     DECLARE_DELAYED_SINGLETON(DistributedNotificationManager);
171     DISALLOW_COPY_AND_MOVE(DistributedNotificationManager);
172 };
173 }  // namespace Notification
174 }  // namespace OHOS
175 
176 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_MANAGER_H