• 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_DATABASE_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H
18 
19 #include <functional>
20 #include <memory>
21 #include <sstream>
22 #include <string>
23 
24 #include "notification_rdb_data_mgr.h"
25 #include "notification_preferences_info.h"
26 
27 namespace OHOS {
28 namespace Notification {
29 class NotificationPreferencesDatabase final {
30 public:
31     NotificationPreferencesDatabase();
32     ~NotificationPreferencesDatabase();
33 
34     /**
35      * @brief Put notification slots into disturbe DB.
36      *
37      * @param bundleName Indicates bundle name.
38      * @param bundleUid Indicates bundle uid.
39      * @param slots Indicates notification slots.
40      * @return Return true on success, false on failure.
41      */
42     bool PutSlotsToDisturbeDB(
43         const std::string &bundleName, const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots);
44 
45     /**
46      * @brief Put notification bundle into disturbe DB.
47      *
48      * @param bundleInfo Indicates bundle info.
49      * @return Return true on success, false on failure.
50      */
51     bool PutBundlePropertyToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo);
52 
53     /**
54      * @brief Put show badge in the of bundle into disturbe DB.
55      *
56      * @param bundleInfo Indicates bundle info.
57      * @param enable Indicates to whether show badge.
58      * @return Return true on success, false on failure.
59      */
60     bool PutShowBadge(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable);
61 
62     /**
63      * @brief Put importance in the of bundle into disturbe DB.
64      *
65      * @param bundleInfo Indicates bundle info.
66      * @param importance Indicates to importance level  which can be LEVEL_NONE,
67                LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, LEVEL_HIGH, or LEVEL_UNDEFINED.
68      * @return Return true on success, false on failure.
69      */
70     bool PutImportance(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &importance);
71 
72     /**
73      * @brief Put badge total nums in the of  bundle into disturbe DB.
74      *
75      * @param bundleInfo Indicates bundle info.
76      * @param totalBadgeNum Indicates to total badge num.
77      * @return Return true on success, false on failure.
78      */
79     bool PutTotalBadgeNums(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &totalBadgeNum);
80 
81     /**
82      * @brief Put enable notification in the of  bundle into disturbe DB.
83      *
84      * @param bundleInfo Indicates bundle info.
85      * @param enabled Indicates to whether to enabled
86      * @return Return true on success, false on failure.
87      */
88     bool PutNotificationsEnabledForBundle(
89         const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled);
90 
91     /**
92      * @brief Put distributed enable notification in the of  bundle into disturbe DB.
93      *
94      * @param deviceType Indicates device type.
95      * @param bundleInfo Indicates bundle info.
96      * @param enabled Indicates to whether to enabled
97      * @return Return true on success, false on failure.
98      */
99     bool PutDistributedEnabledForBundle(const std::string deviceType,
100         const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled);
101 
102     /**
103      * @brief Get distributed enable notification in the of  bundle into disturbe DB.
104      *
105      * @param deviceType Indicates device type.
106      * @param bundleInfo Indicates bundle info.
107      * @param enabled Indicates to whether to enabled
108      * @return Return true on success, false on failure.
109      */
110     bool GetDistributedEnabledForBundle(const std::string deviceType,
111         const NotificationPreferencesInfo::BundleInfo &bundleInfo, bool &enabled);
112 
113     /**
114      * @brief Put smart reminder enable notification in the of  bundle into disturbe DB.
115      *
116      * @param deviceType Indicates device type.
117      * @param enabled Indicates to whether to enabled
118      * @return Return true on success, false on failure.
119      */
120     bool SetSmartReminderEnabled(const std::string deviceType, const bool &enabled);
121 
122     /**
123      * @brief Get smart reminder enable notification in the of  bundle into disturbe DB.
124      *
125      * @param deviceType Indicates device type.
126      * @param enabled Indicates to whether to enabled
127      * @return Return true on success, false on failure.
128      */
129     bool IsSmartReminderEnabled(const std::string deviceType, bool &enabled);
130 
131     /**
132      * @brief Set the channel switch for collaborative reminders.
133        The caller must have system permissions to call this method.
134      *
135      * @param slotType Indicates the slot type of the application.
136      * @param deviceType Indicates the type of the device running the application.
137      * @param enabled Indicates slot switch status.
138      * @return Returns set channel switch result.
139      */
140     bool SetDistributedEnabledBySlot(
141         const NotificationConstant::SlotType &slotType, const std::string &deviceType, const bool enabled);
142 
143     /**
144      * @brief Query the channel switch for collaborative reminders.
145        The caller must have system permissions to call this method.
146      *
147      * @param slotType Indicates the slot type of the application.
148      * @param deviceType Indicates the type of the device running the application.
149      * @param enabled Indicates slot switch status.
150      * @return Returns channel switch result.
151      */
152     bool IsDistributedEnabledBySlot(
153         const NotificationConstant::SlotType &slotType, const std::string &deviceType, bool &enabled);
154 
155     /**
156      * @brief Querying Aggregation Configuration Values
157      *
158      * @return Configured value
159      */
160     std::string GetAdditionalConfig(const std::string &key);
161 
162     /**
163      * @brief Put enable notification into disturbe DB.
164      *
165      * @param userId Indicates user.
166      * @param enabled Indicates to whether to enabled
167      * @return Return true on success, false on failure.
168      */
169     bool PutNotificationsEnabled(const int32_t &userId, const bool &enabled);
170     bool PutSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &slotFlags);
171     bool PutHasPoppedDialog(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped);
172 
173     /**
174      * @brief Put do not disturbe date into disturbe DB.
175      *
176      * @param userId Indicates user.
177      * @param date Indicates to do not disturbe date.
178      * @return Return true on success, false on failure.
179      */
180     bool PutDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date);
181 
182     /**
183      * @brief Parse notification info from disturbe DB.
184      *
185      * @param info Indicates notification info.
186      * @return Return true on success, false on failure.
187      */
188     bool ParseFromDisturbeDB(NotificationPreferencesInfo &info, int32_t userId = -1);
189 
190     /**
191      * @brief Delete all data from disturbe DB.
192      *
193      * @return Return true on success, false on failure.
194      */
195     bool RemoveAllDataFromDisturbeDB();
196 
197     /**
198      * @brief Delete bundle data from disturbe DB.
199      *
200      * @param bundleKey Indicates the bundle key.
201      * @param bundleId Indicates to bundle uid.
202      * @return Return true on success, false on failure.
203      */
204     bool RemoveBundleFromDisturbeDB(const std::string &bundleKey, const int32_t &bundleUid);
205 
206     /**
207      * @brief Delete slot from disturbe DB.
208      *
209      * @param bundleKey Indicates to which a bundle.
210      * @param type Indicates to slot type.
211      * @param bundleId Indicates to bundle uid.
212      * @return Return true on success, false on failure.
213      */
214     bool RemoveSlotFromDisturbeDB(const std::string &bundleKey, const NotificationConstant::SlotType &type,
215         const int32_t &bundleUid);
216 
217     /**
218      * @brief Obtains allow notification application list.
219      *
220      * @param bundleOption Indicates the bundle bundleOption.
221      * @return Returns ERR_OK on success, others on failure.
222      */
223     bool GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> &bundleOption);
224 
225     /**
226      * @brief Delete all slots in the of bundle from disturbe DB.
227      *
228      * @param bundleKey Indicates to which a bundle.
229      * @param bundleUid Indicates to the bundle uid.
230      * @return Return true on success, false on failure.
231      */
232     bool RemoveAllSlotsFromDisturbeDB(const std::string &bundleKey, const int32_t &bundleUid);
233 
234      /**
235      * @brief Get bundleInfo from DB.
236      *
237      * @param bundleOption Indicates the bundle bundleOption.
238      * @param bundleInfo Indicates bundle info.
239      * @return Return true on success, false on failure.
240      */
241     bool GetBundleInfo(const sptr<NotificationBundleOption> &bundleOption,
242         NotificationPreferencesInfo::BundleInfo &bundleInfo);
243 
244     /**
245      * @brief Query whether there is a agent relationship between the two apps.
246      *
247      * @param agentBundleName The bundleName of the agent app.
248      * @param sourceBundleName The bundleName of the source app.
249      * @return Returns true if There is an agent relationship; returns false otherwise.
250      */
251     bool IsAgentRelationship(const std::string &agentBundleName, const std::string &sourceBundleName);
252     bool RemoveNotificationEnable(const int32_t userId);
253     bool RemoveDoNotDisturbDate(const int32_t userId);
254     bool RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid);
255     bool AddDoNotDisturbProfiles(int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles);
256     bool RemoveDoNotDisturbProfiles(
257         int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles);
258     bool GetDoNotDisturbProfiles(
259         const std::string &key, sptr<NotificationDoNotDisturbProfile> &profile, const int32_t &userId);
260     bool RemoveEnabledDbByBundleName(std::string bundleName, const int32_t &bundleUid);
261     int32_t SetKvToDb(const std::string &key, const std::string &value, const int32_t &userId);
262     int32_t SetByteToDb(const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId);
263     int32_t GetKvFromDb(const std::string &key, std::string &value, const int32_t &userId);
264     int32_t GetByteFromDb(const std::string &key, std::vector<uint8_t> &value, const int32_t &userId);
265     int32_t GetBatchKvsFromDb(
266         const std::string &key, std::unordered_map<std::string, std::string> &values, const int32_t &userId);
267     int32_t DeleteKvFromDb(const std::string &key, const int32_t &userId);
268     int32_t DeleteBatchKvFromDb(const std::vector<std::string> &keys, const int &userId);
269     int32_t DropUserTable(const int32_t userId);
270     bool UpdateBundlePropertyToDisturbeDB(int32_t userId, const NotificationPreferencesInfo::BundleInfo &bundleInfo);
271     bool UpdateBundleSlotToDisturbeDB(int32_t userId, const std::string &bundleName,
272         const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots);
273     bool IsNotificationSlotFlagsExists(const sptr<NotificationBundleOption> &bundleOption);
274     bool DelCloneProfileInfo(const int32_t &userId, const sptr<NotificationDoNotDisturbProfile>& info);
275     bool UpdateBatchCloneProfileInfo(const int32_t &userId,
276         const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo);
277     void GetAllCloneProfileInfo(const int32_t &userId,
278         std::vector<sptr<NotificationDoNotDisturbProfile>>& profilesInfo);
279     void GetAllCloneBundleInfo(const int32_t &userId, std::vector<NotificationCloneBundleInfo>& cloneBundleInfo);
280     bool UpdateBatchCloneBundleInfo(const int32_t &userId,
281         const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo);
282     bool DelCloneBundleInfo(const int32_t &userId, const NotificationCloneBundleInfo& cloneBundleInfo);
283     bool DelBatchCloneProfileInfo(const int32_t &userId,
284         const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo);
285     bool DelBatchCloneBundleInfo(const int32_t &userId,
286         const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo);
287     bool SetDisableNotificationInfo(const sptr<NotificationDisable> &notificationDisable);
288     bool GetDisableNotificationInfo(NotificationDisable &notificationDisable);
289     bool SetSubscriberExistFlag(const std::string& deviceType, bool existFlag);
290     bool GetSubscriberExistFlag(const std::string& deviceType, bool& existFlag);
291     bool IsDistributedEnabledEmptyForBundle(
292         const std::string& deviceType, const NotificationPreferencesInfo::BundleInfo& bundleInfo);
293     bool GetAllDistribuedEnabledBundles(int32_t userId,
294         const std::string &deviceType, std::vector<NotificationBundleOption> &bundleOption);
295     /**
296      * @brief set rule of generate hashCode.
297      *
298      * @param uid uid.
299      * @param type generate hashCode.
300      * @return result true:success.
301      */
302     bool SetHashCodeRule(const int32_t uid, const uint32_t type);
303 
304     /**
305      * @brief set rule of generate hashCode.
306      *
307      * @param uid uid.
308      * @return type generate hashCode.
309      */
310     uint32_t GetHashCodeRule(const int32_t uid);
311 
312     bool SetBundleRemoveFlag(const sptr<NotificationBundleOption> &bundleOption,
313         const NotificationConstant::SlotType &slotType, int32_t sourceType);
314 
315     bool GetBundleRemoveFlag(const sptr<NotificationBundleOption> &bundleOption,
316         const NotificationConstant::SlotType &slotType, int32_t sourceType);
317 
318 private:
319     bool CheckRdbStore();
320 
321     bool CheckBundle(const std::string &bundleName, const int32_t &bundleUid);
322     bool PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo);
323     template <typename T>
324     int32_t PutBundlePropertyToDisturbeDB(
325         const std::string &bundleKey, const BundleType &type, const T &t, const int32_t &bundleUid);
326     template <typename T>
327     int32_t PutDataToDB(const std::string &key, const T &t, const int32_t &userId);
328     bool PutBundleToDisturbeDB(
329         const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo);
330     bool HandleDataBaseMap(
331         const std::unordered_map<std::string, std::string> &datas, std::vector<NotificationBundleOption> &bundleOption);
332 
333     void GetValueFromDisturbeDB(const std::string &key, const int &userId,
334         std::function<void(std::string &)> function);
335     void GetValueFromDisturbeDB(const std::string &key, const int &userId,
336         std::function<void(int32_t &, std::string &)> function);
337 
338     bool SlotToEntry(const std::string &bundleName, const int32_t &bundleUid, const sptr<NotificationSlot> &slot,
339         std::unordered_map<std::string, std::string> &values);
340     void GenerateSlotEntry(const std::string &bundleKey, const sptr<NotificationSlot> &slot,
341         std::unordered_map<std::string, std::string> &values) const;
342     void GenerateEntry(
343         const std::string &key, const std::string &value, std::unordered_map<std::string, std::string> &values) const;
344 
345     std::string FindLastString(const std::string &findString, const std::string &inputString) const;
346     std::string SubUniqueIdentifyFromString(const std::string &findString, const std::string &keyStr) const;
347     std::string VectorToString(const std::vector<int64_t> &data) const;
348     void StringToVector(const std::string &str, std::vector<int64_t> &data) const;
349     int32_t StringToInt(const std::string &str) const;
350     int64_t StringToInt64(const std::string &str) const;
351     void StringSplit(const std::string content, char delim, std::vector<std::string>& result) const;
352     bool IsSlotKey(const std::string &bundleKey, const std::string &key) const;
353     std::string GenerateSlotKey(
354         const std::string &bundleKey, const std::string &type = "", const std::string &subType = "") const;
355     std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const;
356 
357     void ParseBundleFromDistureDB(NotificationPreferencesInfo &info,
358         const std::unordered_map<std::string, std::string> &entries, const int32_t &userId);
359     void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey,
360         const std::pair<std::string, std::string> &entry, const int32_t &userId);
361     void ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo,
362         const std::string &bundleKey, const std::pair<std::string, std::string> &entry);
363     void ParseBundleName(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
364     void ParseBundleImportance(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
365     void ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
366     void ParseBundleShowBadgeEnable(NotificationPreferencesInfo::BundleInfo &bundleInfo,
367         const std::string &value) const;
368     void ParseBundleBadgeNum(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
369     void ParseBundleEnableNotification(
370         NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
371     void ParseBundlePoppedDialog(
372         NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
373     void ParseBundleUid(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
374     void ParseSlot(const std::string &findString, sptr<NotificationSlot> &slot,
375         const std::pair<std::string, std::string> &entry, const int32_t &userId);
376     void ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const;
377     void ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const;
378     void ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const;
379     void ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const;
380     void ParseSlotEnableVrbration(sptr<NotificationSlot> &slot, const std::string &value) const;
381     void ParseSlotLedLightColor(sptr<NotificationSlot> &slot, const std::string &value) const;
382     void ParseSlotLockscreenVisibleness(sptr<NotificationSlot> &slot, const std::string &value) const;
383     void ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const;
384     void ParseSlotVibrationSytle(sptr<NotificationSlot> &slot, const std::string &value) const;
385     void ParseSlotEnableBypassDnd(sptr<NotificationSlot> &slot, const std::string &value) const;
386     void ParseSlotEnabled(sptr<NotificationSlot> &slot, const std::string &value) const;
387     void ParseSlotFlags(sptr<NotificationSlot> &slot, const std::string &value) const;
388     void ParseSlotAuthorizedStatus(sptr<NotificationSlot> &slot, const std::string &value) const;
389     void ParseSlotAuthHitnCnt(sptr<NotificationSlot> &slot, const std::string &value) const;
390     void ParseSlotReminderMode(sptr<NotificationSlot> &slot, const std::string &value) const;
391     bool UpdateCloneToDisturbeDB(const int32_t &userId,
392         const std::unordered_map<std::string, std::string> values);
393 
394     std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo) const;
395     std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo,
396         const std::string &deviceType) const;
397     std::string GenerateBundleLablel(const std::string &deviceType, const int32_t userId) const;
398     std::string GenerateBundleLablel(const NotificationConstant::SlotType &slotType,
399         const std::string &deviceType, const int32_t userId) const;
400     void GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId);
401     void GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId);
402     void GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId);
403     void GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId);
404     void GetDoNotDisturbProfile(NotificationPreferencesInfo &info, int32_t userId);
405     void GetDisableNotificationInfo(NotificationPreferencesInfo &info);
406     void SetSoltProperty(sptr<NotificationSlot> &slot, std::string &typeStr, std::string &valueStr,
407         const std::string &findString, const int32_t &userId);
408     void ExecuteDisturbeDB(sptr<NotificationSlot> &slot, std::string &typeStr, std::string &valueStr,
409         const std::string &findString, const int32_t &userId);
410     bool CheckApiCompatibility(const std::string &bundleName, const int32_t &uid);
411     std::shared_ptr<NotificationDataMgr> rdbDataManager_;
412     std::string GenerateSubscriberExistFlagKey(const std::string& deviceType, const int32_t userId) const;
413     void GetSmartReminderEnableFromCCM(const std::string& deviceType, bool& enabled);
414     bool isCachedSmartReminderEnableList_ = false;
415     std::vector<std::string> smartReminderEnableList_ = {};
416     std::string GenerateHashCodeGenerate(int32_t uid);
417 };
418 } // namespace Notification
419 } // namespace OHOS
420 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H
421