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 
251 private:
252     bool CheckRdbStore();
253 
254     bool CheckBundle(const std::string &bundleName, const int32_t &bundleUid);
255     bool PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo);
256     template <typename T>
257     int32_t PutBundlePropertyToDisturbeDB(
258         const std::string &bundleKey, const BundleType &type, const T &t, const int32_t &bundleUid);
259     template <typename T>
260     int32_t PutDataToDB(const std::string &key, const T &t, const int32_t &userId);
261     bool PutBundleToDisturbeDB(
262         const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo);
263     bool HandleDataBaseMap(
264         const std::unordered_map<std::string, std::string> &datas, std::vector<NotificationBundleOption> &bundleOption);
265 
266     void GetValueFromDisturbeDB(const std::string &key, const int &userId,
267         std::function<void(std::string &)> function);
268     void GetValueFromDisturbeDB(const std::string &key, const int &userId,
269         std::function<void(int32_t &, std::string &)> function);
270 
271     bool SlotToEntry(const std::string &bundleName, const int32_t &bundleUid, const sptr<NotificationSlot> &slot,
272         std::unordered_map<std::string, std::string> &values);
273     void GenerateSlotEntry(const std::string &bundleKey, const sptr<NotificationSlot> &slot,
274         std::unordered_map<std::string, std::string> &values) const;
275     void GenerateEntry(
276         const std::string &key, const std::string &value, std::unordered_map<std::string, std::string> &values) const;
277 
278     std::string FindLastString(const std::string &findString, const std::string &inputString) const;
279     std::string SubUniqueIdentifyFromString(const std::string &findString, const std::string &keyStr) const;
280     std::string VectorToString(const std::vector<int64_t> &data) const;
281     void StringToVector(const std::string &str, std::vector<int64_t> &data) const;
282     int32_t StringToInt(const std::string &str) const;
283     int64_t StringToInt64(const std::string &str) const;
284     bool IsSlotKey(const std::string &bundleKey, const std::string &key) const;
285     std::string GenerateSlotKey(
286         const std::string &bundleKey, const std::string &type = "", const std::string &subType = "") const;
287     std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const;
288 
289     void ParseBundleFromDistureDB(NotificationPreferencesInfo &info,
290         const std::unordered_map<std::string, std::string> &entries, const int32_t &userId);
291     void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey,
292         const std::pair<std::string, std::string> &entry, const int32_t &userId);
293     void ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo,
294         const std::string &bundleKey, const std::pair<std::string, std::string> &entry);
295     void ParseBundleName(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
296     void ParseBundleImportance(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
297     void ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
298     void ParseBundleShowBadgeEnable(NotificationPreferencesInfo::BundleInfo &bundleInfo,
299         const std::string &value) const;
300     void ParseBundleBadgeNum(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
301     void ParseBundleEnableNotification(
302         NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
303     void ParseBundlePoppedDialog(
304         NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
305     void ParseBundleUid(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
306     void ParseSlot(const std::string &findString, sptr<NotificationSlot> &slot,
307         const std::pair<std::string, std::string> &entry, const int32_t &userId);
308     void ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const;
309     void ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const;
310     void ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const;
311     void ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const;
312     void ParseSlotEnableVrbration(sptr<NotificationSlot> &slot, const std::string &value) const;
313     void ParseSlotLedLightColor(sptr<NotificationSlot> &slot, const std::string &value) const;
314     void ParseSlotLockscreenVisibleness(sptr<NotificationSlot> &slot, const std::string &value) const;
315     void ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const;
316     void ParseSlotVibrationSytle(sptr<NotificationSlot> &slot, const std::string &value) const;
317     void ParseSlotEnableBypassDnd(sptr<NotificationSlot> &slot, const std::string &value) const;
318     void ParseSlotEnabled(sptr<NotificationSlot> &slot, const std::string &value) const;
319     void ParseSlotFlags(sptr<NotificationSlot> &slot, const std::string &value) const;
320     void ParseSlotAuthorizedStatus(sptr<NotificationSlot> &slot, const std::string &value) const;
321     void ParseSlotAuthHitnCnt(sptr<NotificationSlot> &slot, const std::string &value) const;
322     void ParseSlotReminderMode(sptr<NotificationSlot> &slot, const std::string &value) const;
323 
324     std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo) const;
325     std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo,
326         const std::string &deviceType) const;
327     std::string GenerateBundleLablel(const std::string &deviceType, const int32_t userId) const;
328     void GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId);
329     void GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId);
330     void GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId);
331     void GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId);
332     void GetDoNotDisturbProfile(NotificationPreferencesInfo &info, int32_t userId);
333     void SetSoltProperty(sptr<NotificationSlot> &slot, std::string &typeStr, std::string &valueStr,
334         const std::string &findString, const int32_t &userId);
335     void ExecuteDisturbeDB(sptr<NotificationSlot> &slot, std::string &typeStr, std::string &valueStr,
336         const std::string &findString, const int32_t &userId);
337     bool CheckApiCompatibility(const std::string &bundleName, const int32_t &uid);
338     std::shared_ptr<NotificationDataMgr> rdbDataManager_;
339 };
340 } // namespace Notification
341 } // namespace OHOS
342 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H
343