• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 #include <memory>
25 #include <mutex>
26 #include "notification_clone_bundle_info.h"
27 
28 namespace OHOS {
29 namespace Notification {
30 class NotificationPreferences final {
31 public:
32     NotificationPreferences();
33     ~NotificationPreferences() = default;
34     /**
35      * @brief Get NotificationPreferences instance object.
36      */
37     static std::shared_ptr<NotificationPreferences> GetInstance();
38 
39     /**
40      * @brief Add notification slots into DB.
41      *
42      * @param bundleOption Indicates bunlde info label.
43      * @param slots Indicates add notification slots.
44      * @return Return ERR_OK on success, others on failure.
45      */
46     ErrCode AddNotificationSlots(
47         const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots);
48 
49     /**
50      * @brief Add notification bunle info into DB.
51      *
52      * @param bundleOption Indicates bunlde info.
53      * @return Return ERR_OK on success, others on failure.
54      */
55     ErrCode AddNotificationBundleProperty(const sptr<NotificationBundleOption> &bundleOption);
56 
57     /**
58      * @brief Remove notification a slot in the of bundle from DB.
59      *
60      * @param bundleOption Indicates bunlde info label.
61      * @param slotType Indicates slot type.
62      * @return Return ERR_OK on success, others on failure.
63      */
64     ErrCode RemoveNotificationSlot(
65         const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType);
66 
67     /**
68      * @brief Remove notification all slot in the of bundle from DB.
69      *
70      * @param bundleOption Indicates bunlde info label.
71      * @return Return ERR_OK on success, others on failure.
72      */
73     ErrCode RemoveNotificationAllSlots(const sptr<NotificationBundleOption> &bundleOption);
74 
75     /**
76      * @brief Remove notification bundle from DB.
77      *
78      * @param bundleOption Indicates bunlde info label.
79      * @return Return ERR_OK on success, others on failure.
80      */
81     ErrCode RemoveNotificationForBundle(const sptr<NotificationBundleOption> &bundleOption);
82 
83     /**
84      * @brief Update notification slot into DB.
85      *
86      * @param bundleOption Indicates bunlde info label.
87      * @param slot Indicates need to upadte slot.
88      * @return Return ERR_OK on success, others on failure.
89      */
90     ErrCode UpdateNotificationSlots(
91         const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slot);
92 
93     /**
94      * @brief Get notification slot from DB.
95      *
96      * @param bundleOption Indicates bunlde info label.
97      * @param type Indicates to get slot type.
98      * @param slot Indicates to get slot.
99      * @return Return ERR_OK on success, others on failure.
100      */
101     ErrCode GetNotificationSlot(const sptr<NotificationBundleOption> &bundleOption,
102         const NotificationConstant::SlotType &type, sptr<NotificationSlot> &slot);
103 
104     /**
105      * @brief Get notification all slots in a bundle from DB.
106      *
107      * @param bundleOption Indicates bunlde info label.
108      * @param slots Indicates to get slots.
109      * @return Return ERR_OK on success, others on failure.
110      */
111     ErrCode GetNotificationAllSlots(
112         const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlot>> &slots);
113 
114     /**
115      * @brief Get notification slot num in a bundle from DB.
116      *
117      * @param bundleOption Indicates bunlde info label.
118      * @param num Indicates to get slot num.
119      * @return Return ERR_OK on success, others on failure.
120      */
121     ErrCode GetNotificationSlotsNumForBundle(const sptr<NotificationBundleOption> &bundleOption, uint64_t &num);
122 
123     /**
124      * @brief Get show badge in the of bunlde from DB.
125      *
126      * @param bundleOption Indicates bunlde info label.
127      * @param enable Indicates to whether to show badge
128      * @return Return ERR_OK on success, others on failure.
129      */
130     ErrCode IsShowBadge(const sptr<NotificationBundleOption> &bundleOption, bool &enable);
131 
132     /**
133      * @brief Set show badge in the of bunlde from DB.
134      *
135      * @param bundleOption Indicates bunlde info label.
136      * @param enable Indicates to set show badge
137      * @return Return ERR_OK on success, others on failure.
138      */
139     ErrCode SetShowBadge(const sptr<NotificationBundleOption> &bundleOption, const bool enable);
140 
141     /**
142     * @brief Get importance in the of bunlde from DB.
143     *
144     * @param bundleOption Indicates bunlde info label.
145     * @param importance Indicates to importance label which can be LEVEL_NONE,
146                LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, LEVEL_HIGH, or LEVEL_UNDEFINED.
147     * @return Return ERR_OK on success, others on failure.
148     */
149     ErrCode GetImportance(const sptr<NotificationBundleOption> &bundleOption, int32_t &importance);
150 
151     /**
152     * @brief Set importance in the of bunlde from DB.
153     *
154     * @param bundleOption Indicates bunlde info label.
155     * @param importance Indicates to set a importance label which can be LEVEL_NONE,
156                LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, LEVEL_HIGH, or LEVEL_UNDEFINED.
157     * @return Return ERR_OK on success, others on failure.
158     */
159     ErrCode SetImportance(const sptr<NotificationBundleOption> &bundleOption, const int32_t &importance);
160 
161     /**
162      * @brief Get total badge nums in the of bunlde from DB.
163      *
164      * @param bundleOption Indicates bunlde info label.
165      * @param totalBadgeNum Indicates to get badge num.
166      * @return Return ERR_OK on success, others on failure.
167      */
168     ErrCode GetTotalBadgeNums(const sptr<NotificationBundleOption> &bundleOption, int32_t &totalBadgeNum);
169 
170     /**
171      * @brief Set total badge nums in the of bunlde from DB.
172      *
173      * @param bundleOption Indicates bunlde info label.
174      * @param totalBadgeNum Indicates to set badge num.
175      * @return Return ERR_OK on success, others on failure.
176      */
177     ErrCode SetTotalBadgeNums(const sptr<NotificationBundleOption> &bundleOption, const int32_t num);
178 
179     /**
180      * @brief Get slotFlags in the of bunlde from DB.
181      *
182      * @param bundleOption Indicates bunlde info label.
183      * @param slotFlags Indicates to set soltFlags.
184      * @return Return ERR_OK on success, others on failure.
185      */
186     ErrCode GetNotificationSlotFlagsForBundle(const sptr<NotificationBundleOption> &bundleOption, uint32_t &slotFlags);
187 
188     /**
189      * @brief Get slotFlags in the of bunlde from DB.
190      *
191      * @param bundleOption Indicates bunlde info label.
192      * @param slotFlags Indicates to get slotFlags.
193      * @return Return ERR_OK on success, others on failure.
194      */
195     ErrCode SetNotificationSlotFlagsForBundle(const sptr<NotificationBundleOption> &bundleOption, uint32_t slotFlags);
196 
197     /**
198      * @brief Get private notification enable in the of bunlde from DB.
199      *
200      * @param bundleOption Indicates bunlde info label.
201      * @param enabled Indicates to whether to enable.
202      * @return Return ERR_OK on success, others on failure.
203      */
204     ErrCode GetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, bool &enabled);
205 
206     /**
207      * @brief Set private notification enable in the of bunlde from DB.
208      *
209      * @param bundleOption Indicates bunlde info label.
210      * @param enabled Indicates to set enable.
211      * @return Return ERR_OK on success, others on failure.
212      */
213     ErrCode SetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, const bool enabled);
214 
215     /**
216      * @brief Get notification enable from DB.
217      *
218      * @param userId Indicates user.
219      * @param enabled Indicates to whether to enable.
220      * @return Return ERR_OK on success, others on failure.
221      */
222     ErrCode GetNotificationsEnabled(const int32_t &userId, bool &enabled);
223 
224     /**
225      * @brief Set notification enable from DB.
226      *
227      * @param userId Indicates user.
228      * @param enabled Indicates to set enable.
229      * @return Return ERR_OK on success, others on failure.
230      */
231     ErrCode SetNotificationsEnabled(const int32_t &userId, const bool &enabled);
232     ErrCode GetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool &hasPopped);
233     ErrCode SetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool hasPopped);
234 
235     /**
236      * @brief Get do not disturb date from DB.
237      *
238      * @param userId Indicates user.
239      * @param date Indicates to get do not disturb date.
240      * @return Return ERR_OK on success, others on failure.
241      */
242     ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr<NotificationDoNotDisturbDate> &date);
243 
244     /**
245      * @brief Set do not disturb date from DB.
246      *
247      * @param userId Indicates user.
248      * @param date Indicates to set do not disturb date.
249      * @return Return ERR_OK on success, others on failure.
250      */
251     ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> date);
252     ErrCode GetTemplateSupported(const std::string &templateName, bool &support);
253 
254     /**
255      * @brief Add do not disturb profiles from DB.
256      *
257      * @param userId Indicates user.
258      * @param profiles Indicates to add do not disturb profiles.
259      * @return Return ERR_OK on success, others on failure.
260      */
261     ErrCode AddDoNotDisturbProfiles(int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> profiles);
262 
263     /**
264      * @brief Remove do not disturb profiles from DB.
265      *
266      * @param userId Indicates user.
267      * @param profiles Indicates to remove do not disturb profiles.
268      * @return Return ERR_OK on success, others on failure.
269      */
270     ErrCode RemoveDoNotDisturbProfiles(
271         int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> profiles);
272 
273     /**
274      * @brief Obtains allow notification application list.
275      *
276      * @param bundleOption Indicates the bundle bundleOption.
277      * @return Returns ERR_OK on success, others on failure.
278      */
279     ErrCode GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> &bundleOption);
280 
281     ErrCode GetAllLiveViewEnabledBundles(const int32_t userId, std::vector<NotificationBundleOption> &bundleOption);
282 
283     ErrCode GetAllDistribuedEnabledBundles(int32_t userId,
284         const std::string &deviceType, std::vector<NotificationBundleOption> &bundleOption);
285 
286     /**
287      * @brief Remove all proferences info from DB.
288      *
289      * @return Return ERR_OK on success, others on failure.
290      */
291     ErrCode ClearNotificationInRestoreFactorySettings();
292 
293     /**
294      * @brief Query whether there is a agent relationship between the two apps.
295      *
296      * @param agentBundleName The bundleName of the agent app.
297      * @param sourceBundleName The bundleName of the source app.
298      * @return Returns true if There is an agent relationship; returns false otherwise.
299      */
300     bool IsAgentRelationship(const std::string &agentBundleName, const std::string &sourceBundleName);
301 
302     /**
303      * @brief Querying Aggregation Configuration Values
304      *
305      * @return Configured value
306      */
307     std::string GetAdditionalConfig(const std::string &key);
308 
309     /**
310      * @brief Sets whether to allow a specified application to publish notifications cross
311      * device collaboration. The caller must have system permissions to call this method.
312      *
313      * @param bundleOption Indicates the bundle name and uid of the application.
314      * @param deviceType Indicates the type of the device running the application.
315      * @param enabled Specifies whether to allow the given application to publish notifications. The value
316      *                true indicates that notifications are allowed, and the value false indicates that
317      *                notifications are not allowed.
318      * @return Returns set notifications enabled for specified bundle result.
319      */
320     ErrCode SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
321         const std::string &deviceType, const bool enabled);
322 
323     /**
324      * @brief Get Enable smartphone to collaborate with other devices for intelligent reminders
325      *
326      * @param deviceType Indicates the type of the device running the application.
327      * @param enabled Specifies whether to allow the given device to publish notifications.
328      *                The value true indicates that notifications are allowed, and the value
329      *                false indicates that notifications are not allowed.
330      * @return Returns set notifications enabled for specified bundle result.
331      */
332     ErrCode IsSmartReminderEnabled(const std::string &deviceType, bool &enabled);
333 
334     /**
335      * @brief Set Enable smartphone to collaborate with other devices for intelligent reminders
336      *
337      * @param deviceType Indicates the type of the device running the application.
338      * @param enabled Specifies whether to allow the given device to publish notifications.
339      *                The value true indicates that notifications are allowed, and the value
340      *                false indicates that notifications are not allowed.
341      * @return Returns set notifications enabled for specified bundle result.
342      */
343     ErrCode SetSmartReminderEnabled(const std::string &deviceType, const bool enabled);
344 
345     /**
346      * @brief Get whether to allow a specified application to publish notifications cross
347      * device collaboration. The caller must have system permissions to call this method.
348      *
349      * @param bundleOption Indicates the bundle name and uid of the application.
350      * @param deviceType Indicates the type of the device running the application.
351      * @param enabled Specifies whether to allow the given application to publish notifications. The value
352      *                true indicates that notifications are allowed, and the value false indicates that
353      *                notifications are not allowed.
354      * @return Returns set notifications enabled for specified bundle result.
355      */
356     ErrCode IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
357         const std::string &deviceType, bool &enabled);
358 
359     /**
360      * @brief Set the channel switch for collaborative reminders.
361        The caller must have system permissions to call this method.
362      *
363      * @param slotType Indicates the slot type of the application.
364      * @param deviceType Indicates the type of the device running the application.
365      * @param enabled Indicates slot switch status.
366      * @return Returns set channel switch result.
367      */
368     ErrCode SetDistributedEnabledBySlot(
369         const NotificationConstant::SlotType &slotType, const std::string &deviceType, const bool enabled);
370 
371     /**
372      * @brief Query the channel switch for collaborative reminders.
373        The caller must have system permissions to call this method.
374      *
375      * @param slotType Indicates the slot type of the application.
376      * @param deviceType Indicates the type of the device running the application.
377      * @param enabled Indicates slot switch status.
378      * @return Returns channel switch result.
379      */
380     ErrCode IsDistributedEnabledBySlot(
381         const NotificationConstant::SlotType &slotType, const std::string &deviceType, bool &enabled);
382 
383     /**
384      * @brief Get the bundle name set for send the sound.
385      *
386      * @param allPackage Specifies whether to allow all bundle to publish notification with sound.
387      * @param bundleNames Indicates bundle name set, allow to publish notification with sound.
388      * @return true if get the permission; returns false otherwise.
389      */
390     bool GetBundleSoundPermission(bool &allPackage, std::set<std::string> &bundleNames);
391 
392     ErrCode UpdateDoNotDisturbProfiles(int32_t userId, int64_t profileId,
393         const std::string& name, const std::vector<NotificationBundleOption>& bundleList);
394 
395     void UpdateProfilesUtil(std::vector<NotificationBundleOption>& trustList,
396         const std::vector<NotificationBundleOption> bundleList);
397 
398     void InitSettingFromDisturbDB(int32_t userId = -1);
399     void RemoveSettings(int32_t userId);
400     void RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> &bundleOption);
401     void RemoveEnabledDbByBundle(const sptr<NotificationBundleOption> &bundleOption);
402     int32_t SetKvToDb(const std::string &key, const std::string &value, const int32_t &userId);
403     int32_t SetByteToDb(const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId);
404     int32_t GetKvFromDb(const std::string &key, std::string &value, const int32_t &userId);
405     int32_t GetByteFromDb(const std::string &key, std::vector<uint8_t> &value, const int32_t &userId);
406     int32_t GetBatchKvsFromDb(
407         const std::string &key, std::unordered_map<std::string, std::string>  &values, const int32_t &userId);
408     int32_t DeleteKvFromDb(const std::string &key, const int &userId);
409     int32_t DeleteBatchKvFromDb(const std::vector<std::string> &keys, const int &userId);
410     ErrCode GetDoNotDisturbProfile(int64_t profileId, int32_t userId, sptr<NotificationDoNotDisturbProfile> &profile);
411     void RemoveDoNotDisturbProfileTrustList(int32_t userId, const sptr<NotificationBundleOption> &bundleOption);
412     void GetDoNotDisturbProfileListByUserId(int32_t userId,
413         std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles);
414     void GetAllCLoneBundlesInfo(int32_t userId, std::vector<NotificationCloneBundleInfo> &cloneBundles);
415     void UpdateCloneBundleInfo(int32_t userId, const NotificationCloneBundleInfo& cloneBundleInfo);
416     bool IsNotificationSlotFlagsExists(const sptr<NotificationBundleOption> &bundleOption);
417     bool DelCloneProfileInfo(const int32_t &userId, const sptr<NotificationDoNotDisturbProfile>& info);
418     bool UpdateBatchCloneProfileInfo(const int32_t &userId,
419         const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo);
420     void GetAllCloneProfileInfo(const int32_t &userId,
421         std::vector<sptr<NotificationDoNotDisturbProfile>>& profilesInfo);
422     void GetAllCloneBundleInfo(const int32_t &userId, std::vector<NotificationCloneBundleInfo>& cloneBundleInfo);
423     bool UpdateBatchCloneBundleInfo(const int32_t &userId,
424         const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo);
425     bool DelCloneBundleInfo(const int32_t &userId, const NotificationCloneBundleInfo& cloneBundleInfo);
426     bool DelBatchCloneBundleInfo(const int32_t &userId,
427         const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo);
428     bool DelBatchCloneProfileInfo(const int32_t &userId,
429         const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo);
430     ErrCode SetDisableNotificationInfo(const sptr<NotificationDisable> &notificationDisable);
431     bool GetDisableNotificationInfo(NotificationDisable &notificationDisable);
432     ErrCode SetSubscriberExistFlag(const std::string& deviceType, bool existFlag);
433     ErrCode GetSubscriberExistFlag(const std::string& deviceType, bool& existFlag);
434     /**
435      * @brief set rule of generate hashCode.
436      *
437      * @param uid uid.
438      * @param type generate hashCode.
439      * @return Returns ERR_OK on success, others on failure.
440      */
441     ErrCode SetHashCodeRule(const int32_t uid, const uint32_t type);
442 
443     /**
444      * @brief get rule of generate hashCode.
445      *
446      * @param uid uid.
447      * @return  generate hashCode type.
448      */
449     uint32_t GetHashCodeRule(const int32_t uid);
450 
451     bool GetBundleRemoveFlag(const sptr<NotificationBundleOption> &bundleOption,
452         const NotificationConstant::SlotType &slotType, int32_t sourceType);
453 
454     bool SetBundleRemoveFlag(const sptr<NotificationBundleOption> &bundleOption,
455         const NotificationConstant::SlotType &slotType, int32_t sourceType);
456 
457 private:
458     bool GetBundleInfo(NotificationPreferencesInfo &preferencesInfo,
459         const sptr<NotificationBundleOption> &bundleOption, NotificationPreferencesInfo::BundleInfo &info) const;
460     ErrCode CheckSlotForCreateSlot(const sptr<NotificationBundleOption> &bundleOption,
461         const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const;
462     ErrCode CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> &bundleOption,
463         const NotificationConstant::SlotType &slotType, NotificationPreferencesInfo &preferencesInfo) const;
464     ErrCode CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> &bundleOption,
465         const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const;
466     template <typename T>
467     ErrCode SetBundleProperty(NotificationPreferencesInfo &preferencesInfo,
468         const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value);
469     template <typename T>
470     ErrCode SaveBundleProperty(NotificationPreferencesInfo::BundleInfo &bundleInfo,
471         const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value);
472     template <typename T>
473     ErrCode GetBundleProperty(
474         const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, T &value);
475     std::string GenerateBundleKey(const sptr<NotificationBundleOption> &bundleOption) const;
476     bool CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption) const;
477     void SetDistributedEnabledForBundle(const NotificationPreferencesInfo::BundleInfo& bundleInfo);
478 
479 private:
480     static std::mutex instanceMutex_;
481     static std::shared_ptr<NotificationPreferences> instance_;
482     NotificationPreferencesInfo preferencesInfo_ {};
483     std::mutex preferenceMutex_;
484     std::shared_ptr<NotificationPreferencesDatabase> preferncesDB_ = nullptr;
485     bool isCachedMirrorNotificationEnabledStatus_ = false;
486     std::vector<std::string> mirrorNotificationEnabledStatus_ = {};
487 };
488 }  // namespace Notification
489 }  // namespace OHOS
490 
491 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_H
492