• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_NOTIFICATION_PREFERENCES_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_H
18 
19 #include "refbase.h"
20 #include "singleton.h"
21 
22 #include "notification_do_not_disturb_date.h"
23 #include "notification_preferences_database.h"
24 
25 namespace OHOS {
26 namespace Notification {
27 class NotificationPreferences final {
28 public:
29     DISALLOW_COPY_AND_MOVE(NotificationPreferences);
30 
31     /**
32      * @brief Get NotificationPreferences instance object.
33      */
34     static NotificationPreferences &GetInstance();
35 
36     /**
37      * @brief Add notification slots into DB.
38      *
39      * @param bundleOption Indicates bunlde info label.
40      * @param slots Indicates add notification slots.
41      * @return Return ERR_OK on success, others on failure.
42      */
43     ErrCode AddNotificationSlots(
44         const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots);
45 
46     /**
47      * @brief Add notification bunle info into DB.
48      *
49      * @param bundleOption Indicates bunlde info.
50      * @return Return ERR_OK on success, others on failure.
51      */
52     ErrCode AddNotificationBundleProperty(const sptr<NotificationBundleOption> &bundleOption);
53 
54     /**
55      * @brief Remove notification a slot in the of bundle from DB.
56      *
57      * @param bundleOption Indicates bunlde info label.
58      * @param slotType Indicates slot type.
59      * @return Return ERR_OK on success, others on failure.
60      */
61     ErrCode RemoveNotificationSlot(
62         const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType);
63 
64     /**
65      * @brief Remove notification all slot in the of bundle from DB.
66      *
67      * @param bundleOption Indicates bunlde info label.
68      * @return Return ERR_OK on success, others on failure.
69      */
70     ErrCode RemoveNotificationAllSlots(const sptr<NotificationBundleOption> &bundleOption);
71 
72     /**
73      * @brief Remove notification bundle from DB.
74      *
75      * @param bundleOption Indicates bunlde info label.
76      * @return Return ERR_OK on success, others on failure.
77      */
78     ErrCode RemoveNotificationForBundle(const sptr<NotificationBundleOption> &bundleOption);
79 
80     /**
81      * @brief Update notification slot into DB.
82      *
83      * @param bundleOption Indicates bunlde info label.
84      * @param slot Indicates need to upadte slot.
85      * @return Return ERR_OK on success, others on failure.
86      */
87     ErrCode UpdateNotificationSlots(
88         const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slot);
89 
90     /**
91      * @brief Get notification slot from DB.
92      *
93      * @param bundleOption Indicates bunlde info label.
94      * @param type Indicates to get slot type.
95      * @param slot Indicates to get slot.
96      * @return Return ERR_OK on success, others on failure.
97      */
98     ErrCode GetNotificationSlot(const sptr<NotificationBundleOption> &bundleOption,
99         const NotificationConstant::SlotType &type, sptr<NotificationSlot> &slot);
100 
101     /**
102      * @brief Get notification all slots in a bundle from DB.
103      *
104      * @param bundleOption Indicates bunlde info label.
105      * @param slots Indicates to get slots.
106      * @return Return ERR_OK on success, others on failure.
107      */
108     ErrCode GetNotificationAllSlots(
109         const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlot>> &slots);
110 
111     /**
112      * @brief Get notification slot num in a bundle from DB.
113      *
114      * @param bundleOption Indicates bunlde info label.
115      * @param num Indicates to get slot num.
116      * @return Return ERR_OK on success, others on failure.
117      */
118     ErrCode GetNotificationSlotsNumForBundle(const sptr<NotificationBundleOption> &bundleOption, uint64_t &num);
119 
120     /**
121      * @brief Get show badge in the of bunlde from DB.
122      *
123      * @param bundleOption Indicates bunlde info label.
124      * @param enable Indicates to whether to show badge
125      * @return Return ERR_OK on success, others on failure.
126      */
127     ErrCode IsShowBadge(const sptr<NotificationBundleOption> &bundleOption, bool &enable);
128 
129     /**
130      * @brief Set show badge in the of bunlde from DB.
131      *
132      * @param bundleOption Indicates bunlde info label.
133      * @param enable Indicates to set show badge
134      * @return Return ERR_OK on success, others on failure.
135      */
136     ErrCode SetShowBadge(const sptr<NotificationBundleOption> &bundleOption, const bool enable);
137 
138     /**
139     * @brief Get importance in the of bunlde from DB.
140     *
141     * @param bundleOption Indicates bunlde info label.
142     * @param importance Indicates to importance label which can be LEVEL_NONE,
143                LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, LEVEL_HIGH, or LEVEL_UNDEFINED.
144     * @return Return ERR_OK on success, others on failure.
145     */
146     ErrCode GetImportance(const sptr<NotificationBundleOption> &bundleOption, int32_t &importance);
147 
148     /**
149     * @brief Set importance in the of bunlde from DB.
150     *
151     * @param bundleOption Indicates bunlde info label.
152     * @param importance Indicates to set a importance label which can be LEVEL_NONE,
153                LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, LEVEL_HIGH, or LEVEL_UNDEFINED.
154     * @return Return ERR_OK on success, others on failure.
155     */
156     ErrCode SetImportance(const sptr<NotificationBundleOption> &bundleOption, const int32_t &importance);
157 
158     /**
159      * @brief Get total badge nums in the of bunlde from DB.
160      *
161      * @param bundleOption Indicates bunlde info label.
162      * @param totalBadgeNum Indicates to get badge num.
163      * @return Return ERR_OK on success, others on failure.
164      */
165     ErrCode GetTotalBadgeNums(const sptr<NotificationBundleOption> &bundleOption, int32_t &totalBadgeNum);
166 
167     /**
168      * @brief Set total badge nums in the of bunlde from DB.
169      *
170      * @param bundleOption Indicates bunlde info label.
171      * @param totalBadgeNum Indicates to set badge num.
172      * @return Return ERR_OK on success, others on failure.
173      */
174     ErrCode SetTotalBadgeNums(const sptr<NotificationBundleOption> &bundleOption, const int32_t num);
175 
176     /**
177      * @brief Get private notification allowed in the of bunlde from DB.
178      *
179      * @param bundleOption Indicates bunlde info label.
180      * @param allow Indicates to whether to allow.
181      * @return Return ERR_OK on success, others on failure.
182      */
183     ErrCode GetPrivateNotificationsAllowed(const sptr<NotificationBundleOption> &bundleOption, bool &allow);
184 
185     /**
186      * @brief Set private notification allowed in the of bunlde from DB.
187      *
188      * @param bundleOption Indicates bunlde info label.
189      * @param allow Indicates to set allow.
190      * @return Return ERR_OK on success, others on failure.
191      */
192     ErrCode SetPrivateNotificationsAllowed(const sptr<NotificationBundleOption> &bundleOption, const bool allow);
193 
194     /**
195      * @brief Get private notification enable in the of bunlde from DB.
196      *
197      * @param bundleOption Indicates bunlde info label.
198      * @param enabled Indicates to whether to enable.
199      * @return Return ERR_OK on success, others on failure.
200      */
201     ErrCode GetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, bool &enabled);
202 
203     /**
204      * @brief Set private notification enable in the of bunlde from DB.
205      *
206      * @param bundleOption Indicates bunlde info label.
207      * @param enabled Indicates to set enable.
208      * @return Return ERR_OK on success, others on failure.
209      */
210     ErrCode SetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, const bool enabled);
211 
212     /**
213      * @brief Get notification enable from DB.
214      *
215      * @param userId Indicates user.
216      * @param enabled Indicates to whether to enable.
217      * @return Return ERR_OK on success, others on failure.
218      */
219     ErrCode GetNotificationsEnabled(const int32_t &userId, bool &enabled);
220 
221     /**
222      * @brief Set notification enable from DB.
223      *
224      * @param userId Indicates user.
225      * @param enabled Indicates to set enable.
226      * @return Return ERR_OK on success, others on failure.
227      */
228     ErrCode SetNotificationsEnabled(const int32_t &userId, const bool &enabled);
229     ErrCode GetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool &hasPopped);
230     ErrCode SetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool hasPopped);
231 
232     /**
233      * @brief Get do not disturb date from DB.
234      *
235      * @param userId Indicates user.
236      * @param date Indicates to get do not disturb date.
237      * @return Return ERR_OK on success, others on failure.
238      */
239     ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr<NotificationDoNotDisturbDate> &date);
240 
241     /**
242      * @brief Set do not disturb date from DB.
243      *
244      * @param userId Indicates user.
245      * @param date Indicates to set do not disturb date.
246      * @return Return ERR_OK on success, others on failure.
247      */
248     ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> date);
249     ErrCode GetTemplateSupported(const std::string &templateName, bool &support);
250 
251     /**
252      * @brief Remove all proferences info from DB.
253      *
254      * @return Return ERR_OK on success, others on failure.
255      */
256     ErrCode ClearNotificationInRestoreFactorySettings();
257 
258     /**
259      * @brief Death recipient.
260      */
261     void OnDistributedKvStoreDeathRecipient();
262     void InitSettingFromDisturbDB();
263     void RemoveSettings(int32_t userId);
264     void RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> &bundleOption);
265 
266 private:
267     ErrCode CheckSlotForCreateSlot(const sptr<NotificationBundleOption> &bundleOption,
268         const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const;
269     ErrCode CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> &bundleOption,
270         const NotificationConstant::SlotType &slotType, NotificationPreferencesInfo &preferencesInfo) const;
271     ErrCode CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> &bundleOption,
272         const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const;
273     template <typename T>
274     ErrCode SetBundleProperty(NotificationPreferencesInfo &preferencesInfo,
275         const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value);
276     template <typename T>
277     ErrCode SaveBundleProperty(NotificationPreferencesInfo::BundleInfo &bundleInfo,
278         const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value);
279     template <typename T>
280     ErrCode GetBundleProperty(
281         const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, T &value) const;
282     std::string GenerateBundleKey(const sptr<NotificationBundleOption> &bundleOption) const;
283     bool CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption) const;
284 
285 private:
286     NotificationPreferencesInfo preferencesInfo_ {};
287     std::unique_ptr<NotificationPreferencesDatabase> preferncesDB_ = nullptr;
288     DECLARE_DELAYED_REF_SINGLETON(NotificationPreferences);
289 };
290 }  // namespace Notification
291 }  // namespace OHOS
292 
293 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_H
294