• 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 Querying Aggregation Configuration Values
133      *
134      * @return Configured value
135      */
136     std::string GetAdditionalConfig(const std::string &key);
137 
138     /**
139      * @brief Put enable notification into disturbe DB.
140      *
141      * @param userId Indicates user.
142      * @param enabled Indicates to whether to enabled
143      * @return Return true on success, false on failure.
144      */
145     bool PutNotificationsEnabled(const int32_t &userId, const bool &enabled);
146     bool PutSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &slotFlags);
147     bool PutHasPoppedDialog(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped);
148 
149     /**
150      * @brief Put do not disturbe date into disturbe DB.
151      *
152      * @param userId Indicates user.
153      * @param date Indicates to do not disturbe date.
154      * @return Return true on success, false on failure.
155      */
156     bool PutDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date);
157 
158     /**
159      * @brief Parse notification info from disturbe DB.
160      *
161      * @param info Indicates notification info.
162      * @return Return true on success, false on failure.
163      */
164     bool ParseFromDisturbeDB(NotificationPreferencesInfo &info, int32_t userId = -1);
165 
166     /**
167      * @brief Delete all data from disturbe DB.
168      *
169      * @return Return true on success, false on failure.
170      */
171     bool RemoveAllDataFromDisturbeDB();
172 
173     /**
174      * @brief Delete bundle data from disturbe DB.
175      *
176      * @param bundleKey Indicates the bundle key.
177      * @param bundleId Indicates to bundle uid.
178      * @return Return true on success, false on failure.
179      */
180     bool RemoveBundleFromDisturbeDB(const std::string &bundleKey, const int32_t &bundleUid);
181 
182     /**
183      * @brief Delete slot from disturbe DB.
184      *
185      * @param bundleKey Indicates to which a bundle.
186      * @param type Indicates to slot type.
187      * @param bundleId Indicates to bundle uid.
188      * @return Return true on success, false on failure.
189      */
190     bool RemoveSlotFromDisturbeDB(const std::string &bundleKey, const NotificationConstant::SlotType &type,
191         const int32_t &bundleUid);
192 
193     /**
194      * @brief Obtains allow notification application list.
195      *
196      * @param bundleOption Indicates the bundle bundleOption.
197      * @return Returns ERR_OK on success, others on failure.
198      */
199     bool GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> &bundleOption);
200 
201     /**
202      * @brief Delete all slots in the of bundle from disturbe DB.
203      *
204      * @param bundleKey Indicates to which a bundle.
205      * @param bundleUid Indicates to the bundle uid.
206      * @return Return true on success, false on failure.
207      */
208     bool RemoveAllSlotsFromDisturbeDB(const std::string &bundleKey, const int32_t &bundleUid);
209 
210      /**
211      * @brief Get bundleInfo from DB.
212      *
213      * @param bundleOption Indicates the bundle bundleOption.
214      * @param bundleInfo Indicates bundle info.
215      * @return Return true on success, false on failure.
216      */
217     bool GetBundleInfo(const sptr<NotificationBundleOption> &bundleOption,
218         NotificationPreferencesInfo::BundleInfo &bundleInfo);
219 
220     /**
221      * @brief Query whether there is a agent relationship between the two apps.
222      *
223      * @param agentBundleName The bundleName of the agent app.
224      * @param sourceBundleName The bundleName of the source app.
225      * @return Returns true if There is an agent relationship; returns false otherwise.
226      */
227     bool IsAgentRelationship(const std::string &agentBundleName, const std::string &sourceBundleName);
228     bool RemoveNotificationEnable(const int32_t userId);
229     bool RemoveDoNotDisturbDate(const int32_t userId);
230     bool RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid);
231     bool AddDoNotDisturbProfiles(int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles);
232     bool RemoveDoNotDisturbProfiles(
233         int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles);
234     bool GetDoNotDisturbProfiles(
235         const std::string &key, sptr<NotificationDoNotDisturbProfile> &profile, const int32_t &userId);
236     bool RemoveEnabledDbByBundleName(std::string bundleName, const int32_t &bundleUid);
237     int32_t SetKvToDb(const std::string &key, const std::string &value, const int32_t &userId);
238     int32_t SetByteToDb(const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId);
239     int32_t GetKvFromDb(const std::string &key, std::string &value, const int32_t &userId);
240     int32_t GetByteFromDb(const std::string &key, std::vector<uint8_t> &value, const int32_t &userId);
241     int32_t GetBatchKvsFromDb(
242         const std::string &key, std::unordered_map<std::string, std::string> &values, const int32_t &userId);
243     int32_t DeleteKvFromDb(const std::string &key, const int32_t &userId);
244     int32_t DeleteBatchKvFromDb(const std::vector<std::string> &keys, const int &userId);
245     int32_t DropUserTable(const int32_t userId);
246     bool UpdateBundlePropertyToDisturbeDB(int32_t userId, const NotificationPreferencesInfo::BundleInfo &bundleInfo);
247     bool UpdateBundleSlotToDisturbeDB(int32_t userId, const std::string &bundleName,
248         const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots);
249     bool IsNotificationSlotFlagsExists(const sptr<NotificationBundleOption> &bundleOption);
250     bool DelCloneProfileInfo(const int32_t &userId, const sptr<NotificationDoNotDisturbProfile>& info);
251     bool UpdateBatchCloneProfileInfo(const int32_t &userId,
252         const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo);
253     void GetAllCloneProfileInfo(const int32_t &userId,
254         std::vector<sptr<NotificationDoNotDisturbProfile>>& profilesInfo);
255     void GetAllCloneBundleInfo(const int32_t &userId, std::vector<NotificationCloneBundleInfo>& cloneBundleInfo);
256     bool UpdateBatchCloneBundleInfo(const int32_t &userId,
257         const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo);
258     bool DelCloneBundleInfo(const int32_t &userId, const NotificationCloneBundleInfo& cloneBundleInfo);
259     bool DelBatchCloneProfileInfo(const int32_t &userId,
260         const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo);
261     bool DelBatchCloneBundleInfo(const int32_t &userId,
262         const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo);
263 
264     /**
265      * @brief set rule of generate hashCode.
266      *
267      * @param uid uid.
268      * @param type generate hashCode.
269      * @return result true:success.
270      */
271     bool SetHashCodeRule(const int32_t uid, const uint32_t type);
272 
273     /**
274      * @brief set rule of generate hashCode.
275      *
276      * @param uid uid.
277      * @return type generate hashCode.
278      */
279     uint32_t GetHashCodeRule(const int32_t uid);
280 
281 private:
282     bool CheckRdbStore();
283 
284     bool CheckBundle(const std::string &bundleName, const int32_t &bundleUid);
285     bool PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo);
286     template <typename T>
287     int32_t PutBundlePropertyToDisturbeDB(
288         const std::string &bundleKey, const BundleType &type, const T &t, const int32_t &bundleUid);
289     template <typename T>
290     int32_t PutDataToDB(const std::string &key, const T &t, const int32_t &userId);
291     bool PutBundleToDisturbeDB(
292         const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo);
293     bool HandleDataBaseMap(
294         const std::unordered_map<std::string, std::string> &datas, std::vector<NotificationBundleOption> &bundleOption);
295 
296     void GetValueFromDisturbeDB(const std::string &key, const int &userId,
297         std::function<void(std::string &)> function);
298     void GetValueFromDisturbeDB(const std::string &key, const int &userId,
299         std::function<void(int32_t &, std::string &)> function);
300 
301     bool SlotToEntry(const std::string &bundleName, const int32_t &bundleUid, const sptr<NotificationSlot> &slot,
302         std::unordered_map<std::string, std::string> &values);
303     void GenerateSlotEntry(const std::string &bundleKey, const sptr<NotificationSlot> &slot,
304         std::unordered_map<std::string, std::string> &values) const;
305     void GenerateEntry(
306         const std::string &key, const std::string &value, std::unordered_map<std::string, std::string> &values) const;
307 
308     std::string FindLastString(const std::string &findString, const std::string &inputString) const;
309     std::string SubUniqueIdentifyFromString(const std::string &findString, const std::string &keyStr) const;
310     std::string VectorToString(const std::vector<int64_t> &data) const;
311     void StringToVector(const std::string &str, std::vector<int64_t> &data) const;
312     int32_t StringToInt(const std::string &str) const;
313     int64_t StringToInt64(const std::string &str) const;
314     bool IsSlotKey(const std::string &bundleKey, const std::string &key) const;
315     std::string GenerateSlotKey(
316         const std::string &bundleKey, const std::string &type = "", const std::string &subType = "") const;
317     std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const;
318 
319     void ParseBundleFromDistureDB(NotificationPreferencesInfo &info,
320         const std::unordered_map<std::string, std::string> &entries, const int32_t &userId);
321     void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey,
322         const std::pair<std::string, std::string> &entry, const int32_t &userId);
323     void ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo,
324         const std::string &bundleKey, const std::pair<std::string, std::string> &entry);
325     void ParseBundleName(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
326     void ParseBundleImportance(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
327     void ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
328     void ParseBundleShowBadgeEnable(NotificationPreferencesInfo::BundleInfo &bundleInfo,
329         const std::string &value) const;
330     void ParseBundleBadgeNum(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
331     void ParseBundleEnableNotification(
332         NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
333     void ParseBundlePoppedDialog(
334         NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
335     void ParseBundleUid(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
336     void ParseSlot(const std::string &findString, sptr<NotificationSlot> &slot,
337         const std::pair<std::string, std::string> &entry, const int32_t &userId);
338     void ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const;
339     void ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const;
340     void ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const;
341     void ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const;
342     void ParseSlotEnableVrbration(sptr<NotificationSlot> &slot, const std::string &value) const;
343     void ParseSlotLedLightColor(sptr<NotificationSlot> &slot, const std::string &value) const;
344     void ParseSlotLockscreenVisibleness(sptr<NotificationSlot> &slot, const std::string &value) const;
345     void ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const;
346     void ParseSlotVibrationSytle(sptr<NotificationSlot> &slot, const std::string &value) const;
347     void ParseSlotEnableBypassDnd(sptr<NotificationSlot> &slot, const std::string &value) const;
348     void ParseSlotEnabled(sptr<NotificationSlot> &slot, const std::string &value) const;
349     void ParseSlotFlags(sptr<NotificationSlot> &slot, const std::string &value) const;
350     void ParseSlotAuthorizedStatus(sptr<NotificationSlot> &slot, const std::string &value) const;
351     void ParseSlotAuthHitnCnt(sptr<NotificationSlot> &slot, const std::string &value) const;
352     void ParseSlotReminderMode(sptr<NotificationSlot> &slot, const std::string &value) const;
353     bool UpdateCloneToDisturbeDB(const int32_t &userId,
354         const std::unordered_map<std::string, std::string> values);
355 
356     std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo) const;
357     std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo,
358         const std::string &deviceType) const;
359     std::string GenerateBundleLablel(const std::string &deviceType, const int32_t userId) const;
360     void GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId);
361     void GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId);
362     void GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId);
363     void GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId);
364     void GetDoNotDisturbProfile(NotificationPreferencesInfo &info, int32_t userId);
365     void SetSoltProperty(sptr<NotificationSlot> &slot, std::string &typeStr, std::string &valueStr,
366         const std::string &findString, const int32_t &userId);
367     void ExecuteDisturbeDB(sptr<NotificationSlot> &slot, std::string &typeStr, std::string &valueStr,
368         const std::string &findString, const int32_t &userId);
369     bool CheckApiCompatibility(const std::string &bundleName, const int32_t &uid);
370     std::shared_ptr<NotificationDataMgr> rdbDataManager_;
371     std::string GenerateHashCodeGenerate(int32_t uid);
372 };
373 } // namespace Notification
374 } // namespace OHOS
375 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H
376