• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
23 #include "notification_rdb_data_mgr.h"
24 #include "notification_preferences_info.h"
25 
26 namespace OHOS {
27 namespace Notification {
28 class NotificationPreferencesDatabase final {
29 public:
30     NotificationPreferencesDatabase();
31     ~NotificationPreferencesDatabase();
32 
33     /**
34      * @brief Put notification slots into disturbe DB.
35      *
36      * @param bundleName Indicates bundle name.
37      * @param bundleUid Indicates bundle uid.
38      * @param slots Indicates notification slots.
39      * @return Return true on success, false on failure.
40      */
41     bool PutSlotsToDisturbeDB(
42         const std::string &bundleName, const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots);
43 
44     /**
45      * @brief Put notification bundle into disturbe DB.
46      *
47      * @param bundleInfo Indicates bundle info.
48      * @return Return true on success, false on failure.
49      */
50     bool PutBundlePropertyToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo);
51 
52     /**
53      * @brief Put show badge in the of bundle into disturbe DB.
54      *
55      * @param bundleInfo Indicates bundle info.
56      * @param enable Indicates to whether show badge.
57      * @return Return true on success, false on failure.
58      */
59     bool PutShowBadge(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable);
60 
61     /**
62      * @brief Put importance in the of bundle into disturbe DB.
63      *
64      * @param bundleInfo Indicates bundle info.
65      * @param importance Indicates to importance level  which can be LEVEL_NONE,
66                LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, LEVEL_HIGH, or LEVEL_UNDEFINED.
67      * @return Return true on success, false on failure.
68      */
69     bool PutImportance(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &importance);
70 
71     /**
72      * @brief Put badge total nums in the of  bundle into disturbe DB.
73      *
74      * @param bundleInfo Indicates bundle info.
75      * @param totalBadgeNum Indicates to total badge num.
76      * @return Return true on success, false on failure.
77      */
78     bool PutTotalBadgeNums(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &totalBadgeNum);
79 
80     /**
81      * @brief Put enable notification in the of  bundle into disturbe DB.
82      *
83      * @param bundleInfo Indicates bundle info.
84      * @param enabled Indicates to whether to enabled
85      * @return Return true on success, false on failure.
86      */
87     bool PutNotificationsEnabledForBundle(
88         const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled);
89 
90     /**
91      * @brief Put enable notification into disturbe DB.
92      *
93      * @param userId Indicates user.
94      * @param enabled Indicates to whether to enabled
95      * @return Return true on success, false on failure.
96      */
97     bool PutNotificationsEnabled(const int32_t &userId, const bool &enabled);
98     bool PutSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &slotFlags);
99     bool PutHasPoppedDialog(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped);
100 
101     /**
102      * @brief Put do not disturbe date into disturbe DB.
103      *
104      * @param userId Indicates user.
105      * @param date Indicates to do not disturbe date.
106      * @return Return true on success, false on failure.
107      */
108     bool PutDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date);
109 
110     /**
111      * @brief Parse notification info from disturbe DB.
112      *
113      * @param info Indicates notification info.
114      * @return Return true on success, false on failure.
115      */
116     bool ParseFromDisturbeDB(NotificationPreferencesInfo &info);
117 
118     /**
119      * @brief Delete all data from disturbe DB.
120      *
121      * @return Return true on success, false on failure.
122      */
123     bool RemoveAllDataFromDisturbeDB();
124 
125     /**
126      * @brief Delete bundle data from disturbe DB.
127      *
128      * @param bundleKey Indicates the bundle key.
129      * @return Return true on success, false on failure.
130      */
131     bool RemoveBundleFromDisturbeDB(const std::string &bundleKey);
132 
133     /**
134      * @brief Delete slot from disturbe DB.
135      *
136      * @param bundleKey Indicates to which a bundle.
137      * @param type Indicates to slot type.
138      * @return Return true on success, false on failure.
139      */
140     bool RemoveSlotFromDisturbeDB(const std::string &bundleKey, const NotificationConstant::SlotType &type);
141 
142     /**
143      * @brief Delete all slots in the of bundle from disturbe DB.
144      *
145      * @param bundleKey Indicates to which a bundle.
146      * @return Return true on success, false on failure.
147      */
148     bool RemoveAllSlotsFromDisturbeDB(const std::string &bundleKey);
149     bool RemoveNotificationEnable(const int32_t userId);
150     bool RemoveDoNotDisturbDate(const int32_t userId);
151     bool RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid);
152     int32_t SetKvToDb(const std::string &key, const std::string &value);
153     int32_t GetKvFromDb(const std::string &key, std::string &value);
154     int32_t GetBatchKvsFromDb(const std::string &key, std::unordered_map<std::string, std::string> &values);
155     int32_t DeleteKvFromDb(const std::string &key);
156 private:
157     bool CheckRdbStore();
158 
159     bool CheckBundle(const std::string &bundleName, const int32_t &bundleUid);
160     bool PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo);
161     template <typename T>
162     int32_t PutBundlePropertyToDisturbeDB(
163         const std::string &bundleKey, const BundleType &type, const T &t);
164     bool PutBundleToDisturbeDB(
165         const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo);
166 
167     void GetValueFromDisturbeDB(const std::string &key, std::function<void(std::string &)> function);
168     void GetValueFromDisturbeDB(const std::string &key,
169         std::function<void(int32_t &, std::string &)> function);
170 
171     bool SlotToEntry(const std::string &bundleName, const int32_t &bundleUid, const sptr<NotificationSlot> &slot,
172         std::unordered_map<std::string, std::string> &values);
173     void GenerateSlotEntry(const std::string &bundleKey, const sptr<NotificationSlot> &slot,
174         std::unordered_map<std::string, std::string> &values) const;
175     void GenerateEntry(
176         const std::string &key, const std::string &value, std::unordered_map<std::string, std::string> &values) const;
177 
178     std::string FindLastString(const std::string &findString, const std::string &inputString) const;
179     std::string SubUniqueIdentifyFromString(const std::string &findString, const std::string &keyStr) const;
180     std::string VectorToString(const std::vector<int64_t> &data) const;
181     void StringToVector(const std::string &str, std::vector<int64_t> &data) const;
182     int32_t StringToInt(const std::string &str) const;
183     int64_t StringToInt64(const std::string &str) const;
184     bool IsSlotKey(const std::string &bundleKey, const std::string &key) const;
185     std::string GenerateSlotKey(
186         const std::string &bundleKey, const std::string &type = "", const std::string &subType = "") const;
187     std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const;
188 
189     void ParseBundleFromDistureDB(
190         NotificationPreferencesInfo &info, const std::unordered_map<std::string, std::string> &entries);
191     void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey,
192         const std::pair<std::string, std::string> &entry);
193     void ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo,
194         const std::string &bundleKey, const std::pair<std::string, std::string> &entry);
195     void ParseDoNotDisturbType(NotificationPreferencesInfo &info);
196     void ParseDoNotDisturbBeginDate(NotificationPreferencesInfo &info);
197     void ParseDoNotDisturbEndDate(NotificationPreferencesInfo &info);
198     void ParseEnableAllNotification(NotificationPreferencesInfo &info);
199     void ParseBundleName(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
200     void ParseBundleImportance(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
201     void ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
202     void ParseBundleShowBadge(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
203     void ParseBundleBadgeNum(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
204     void ParseBundleEnableNotification(
205         NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
206     void ParseBundlePoppedDialog(
207         NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
208     void ParseBundleUid(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
209     void ParseSlot(
210         const std::string &findString, sptr<NotificationSlot> &slot, const std::pair<std::string, std::string> &entry);
211     void ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const;
212     void ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const;
213     void ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const;
214     void ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const;
215     void ParseSlotEnableVrbration(sptr<NotificationSlot> &slot, const std::string &value) const;
216     void ParseSlotLedLightColor(sptr<NotificationSlot> &slot, const std::string &value) const;
217     void ParseSlotLockscreenVisibleness(sptr<NotificationSlot> &slot, const std::string &value) const;
218     void ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const;
219     void ParseSlotVibrationSytle(sptr<NotificationSlot> &slot, const std::string &value) const;
220     void ParseSlotEnableBypassDnd(sptr<NotificationSlot> &slot, const std::string &value) const;
221     void ParseSlotEnabled(sptr<NotificationSlot> &slot, const std::string &value) const;
222     void ParseSlotFlags(sptr<NotificationSlot> &slot, const std::string &value) const;
223 
224     std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo) const;
225     void GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId);
226     void GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId);
227     void GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId);
228     void GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId);
229 
230     static const std::map<std::string,
231         std::function<void(NotificationPreferencesDatabase *, sptr<NotificationSlot> &, std::string &)>>
232         slotMap_;
233     static const std::map<std::string, std::function<void(NotificationPreferencesDatabase *,
234                                            NotificationPreferencesInfo::BundleInfo &, std::string &)>>
235         bundleMap_;
236 
237     std::shared_ptr<NotificationDataMgr> rdbDataManager_;
238 };
239 } // namespace Notification
240 } // namespace OHOS
241 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H
242