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     /**
282      * @brief Remove all proferences info from DB.
283      *
284      * @return Return ERR_OK on success, others on failure.
285      */
286     ErrCode ClearNotificationInRestoreFactorySettings();
287 
288     /**
289      * @brief Query whether there is a agent relationship between the two apps.
290      *
291      * @param agentBundleName The bundleName of the agent app.
292      * @param sourceBundleName The bundleName of the source app.
293      * @return Returns true if There is an agent relationship; returns false otherwise.
294      */
295     bool IsAgentRelationship(const std::string &agentBundleName, const std::string &sourceBundleName);
296 
297     /**
298      * @brief Querying Aggregation Configuration Values
299      *
300      * @return Configured value
301      */
302     std::string GetAdditionalConfig(const std::string &key);
303 
304     /**
305      * @brief Sets whether to allow a specified application to publish notifications cross
306      * device collaboration. The caller must have system permissions to call this method.
307      *
308      * @param bundleOption Indicates the bundle name and uid of the application.
309      * @param deviceType Indicates the type of the device running the application.
310      * @param enabled Specifies whether to allow the given application to publish notifications. The value
311      *                true indicates that notifications are allowed, and the value false indicates that
312      *                notifications are not allowed.
313      * @return Returns set notifications enabled for specified bundle result.
314      */
315     ErrCode SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
316         const std::string &deviceType, const bool enabled);
317 
318     /**
319      * @brief Get Enable smartphone to collaborate with other devices for intelligent reminders
320      *
321      * @param deviceType Indicates the type of the device running the application.
322      * @param enabled Specifies whether to allow the given device to publish notifications.
323      *                The value true indicates that notifications are allowed, and the value
324      *                false indicates that notifications are not allowed.
325      * @return Returns set notifications enabled for specified bundle result.
326      */
327     ErrCode IsSmartReminderEnabled(const std::string &deviceType, bool &enabled);
328 
329     /**
330      * @brief Set Enable smartphone to collaborate with other devices for intelligent reminders
331      *
332      * @param deviceType Indicates the type of the device running the application.
333      * @param enabled Specifies whether to allow the given device to publish notifications.
334      *                The value true indicates that notifications are allowed, and the value
335      *                false indicates that notifications are not allowed.
336      * @return Returns set notifications enabled for specified bundle result.
337      */
338     ErrCode SetSmartReminderEnabled(const std::string &deviceType, const bool enabled);
339 
340     /**
341      * @brief Get whether to allow a specified application to publish notifications cross
342      * device collaboration. The caller must have system permissions to call this method.
343      *
344      * @param bundleOption Indicates the bundle name and uid of the application.
345      * @param deviceType Indicates the type of the device running the application.
346      * @param enabled Specifies whether to allow the given application to publish notifications. The value
347      *                true indicates that notifications are allowed, and the value false indicates that
348      *                notifications are not allowed.
349      * @return Returns set notifications enabled for specified bundle result.
350      */
351     ErrCode IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
352         const std::string &deviceType, bool &enabled);
353 
354     /**
355      * @brief Get the bundle name set for send the sound.
356      *
357      * @param allPackage Specifies whether to allow all bundle to publish notification with sound.
358      * @param bundleNames Indicates bundle name set, allow to publish notification with sound.
359      * @return true if get the permission; returns false otherwise.
360      */
361     bool GetBundleSoundPermission(bool &allPackage, std::set<std::string> &bundleNames);
362 
363     ErrCode UpdateDoNotDisturbProfiles(int32_t userId, int32_t profileId,
364         const std::string& name, const std::vector<NotificationBundleOption>& bundleList);
365 
366     void UpdateProfilesUtil(std::vector<NotificationBundleOption>& trustList,
367         const std::vector<NotificationBundleOption> bundleList);
368 
369     void InitSettingFromDisturbDB(int32_t userId = -1);
370     void RemoveSettings(int32_t userId);
371     void RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> &bundleOption);
372     void RemoveEnabledDbByBundle(const sptr<NotificationBundleOption> &bundleOption);
373     int32_t SetKvToDb(const std::string &key, const std::string &value, const int32_t &userId);
374     int32_t SetByteToDb(const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId);
375     int32_t GetKvFromDb(const std::string &key, std::string &value, const int32_t &userId);
376     int32_t GetByteFromDb(const std::string &key, std::vector<uint8_t> &value, const int32_t &userId);
377     int32_t GetBatchKvsFromDb(
378         const std::string &key, std::unordered_map<std::string, std::string>  &values, const int32_t &userId);
379     int32_t DeleteKvFromDb(const std::string &key, const int &userId);
380     int32_t DeleteBatchKvFromDb(const std::vector<std::string> &keys, const int &userId);
381     ErrCode GetDoNotDisturbProfile(int32_t profileId, int32_t userId, sptr<NotificationDoNotDisturbProfile> &profile);
382     bool CheckDoNotDisturbProfileID(int32_t profileId);
383     void RemoveDoNotDisturbProfileTrustList(int32_t userId, const sptr<NotificationBundleOption> &bundleOption);
384     void GetDoNotDisturbProfileListByUserId(int32_t userId,
385         std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles);
386     void GetAllCLoneBundlesInfo(int32_t userId, std::vector<NotificationCloneBundleInfo> &cloneBundles);
387     void UpdateCloneBundleInfo(int32_t userId, const NotificationCloneBundleInfo& cloneBundleInfo);
388     bool IsNotificationSlotFlagsExists(const sptr<NotificationBundleOption> &bundleOption);
389 
390 private:
391     bool GetBundleInfo(NotificationPreferencesInfo &preferencesInfo,
392         const sptr<NotificationBundleOption> &bundleOption, NotificationPreferencesInfo::BundleInfo &info) const;
393     ErrCode CheckSlotForCreateSlot(const sptr<NotificationBundleOption> &bundleOption,
394         const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const;
395     ErrCode CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> &bundleOption,
396         const NotificationConstant::SlotType &slotType, NotificationPreferencesInfo &preferencesInfo) const;
397     ErrCode CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> &bundleOption,
398         const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const;
399     template <typename T>
400     ErrCode SetBundleProperty(NotificationPreferencesInfo &preferencesInfo,
401         const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value);
402     template <typename T>
403     ErrCode SaveBundleProperty(NotificationPreferencesInfo::BundleInfo &bundleInfo,
404         const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value);
405     template <typename T>
406     ErrCode GetBundleProperty(
407         const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, T &value);
408     std::string GenerateBundleKey(const sptr<NotificationBundleOption> &bundleOption) const;
409     bool CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption) const;
410 
411 private:
412     static std::mutex instanceMutex_;
413     static std::shared_ptr<NotificationPreferences> instance_;
414     NotificationPreferencesInfo preferencesInfo_ {};
415     std::mutex preferenceMutex_;
416     std::shared_ptr<NotificationPreferencesDatabase> preferncesDB_ = nullptr;
417 };
418 }  // namespace Notification
419 }  // namespace OHOS
420 
421 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_H
422