• 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 PutHasPoppedDialog(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped);
99 
100     /**
101      * @brief Put do not disturbe date into disturbe DB.
102      *
103      * @param userId Indicates user.
104      * @param date Indicates to do not disturbe date.
105      * @return Return true on success, false on failure.
106      */
107     bool PutDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date);
108 
109     /**
110      * @brief Parse notification info from disturbe DB.
111      *
112      * @param info Indicates notification info.
113      * @return Return true on success, false on failure.
114      */
115     bool ParseFromDisturbeDB(NotificationPreferencesInfo &info);
116 
117     /**
118      * @brief Delete all data from disturbe DB.
119      *
120      * @return Return true on success, false on failure.
121      */
122     bool RemoveAllDataFromDisturbeDB();
123 
124     /**
125      * @brief Delete bundle data from disturbe DB.
126      *
127      * @param bundleKey Indicates the bundle key.
128      * @return Return true on success, false on failure.
129      */
130     bool RemoveBundleFromDisturbeDB(const std::string &bundleKey);
131 
132     /**
133      * @brief Delete slot from disturbe DB.
134      *
135      * @param bundleKey Indicates to which a bundle.
136      * @param type Indicates to slot type.
137      * @return Return true on success, false on failure.
138      */
139     bool RemoveSlotFromDisturbeDB(const std::string &bundleKey, const NotificationConstant::SlotType &type);
140 
141     /**
142      * @brief Delete all slots in the of bundle from disturbe DB.
143      *
144      * @param bundleKey Indicates to which a bundle.
145      * @return Return true on success, false on failure.
146      */
147     bool RemoveAllSlotsFromDisturbeDB(const std::string &bundleKey);
148 
149     /**
150      * @brief Deal death recipient.
151      *
152      * @return Return true on success, false on failure.
153      */
154     bool StoreDeathRecipient();
155 
156     bool RemoveNotificationEnable(const int32_t userId);
157     bool RemoveDoNotDisturbDate(const int32_t userId);
158     bool RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid);
159 
160 private:
161     bool CheckRdbStore();
162 
163     bool CheckBundle(const std::string &bundleName, const int32_t &bundleUid);
164     bool PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo);
165     template <typename T>
166     int32_t PutBundlePropertyToDisturbeDB(
167         const std::string &bundleKey, const BundleType &type, const T &t);
168     bool PutBundleToDisturbeDB(
169         const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo);
170 
171     void GetValueFromDisturbeDB(const std::string &key, std::function<void(std::string &)> function);
172     void GetValueFromDisturbeDB(const std::string &key,
173         std::function<void(int32_t &, std::string &)> function);
174 
175     bool SlotToEntry(const std::string &bundleName, const int32_t &bundleUid, const sptr<NotificationSlot> &slot,
176         std::unordered_map<std::string, std::string> &values);
177     void GenerateSlotEntry(const std::string &bundleKey, const sptr<NotificationSlot> &slot,
178         std::unordered_map<std::string, std::string> &values) const;
179     void GenerateEntry(
180         const std::string &key, const std::string &value, std::unordered_map<std::string, std::string> &values) const;
181 
182     std::string FindLastString(const std::string &findString, const std::string &inputString) const;
183     std::string SubUniqueIdentifyFromString(const std::string &findString, const std::string &keyStr) const;
184     std::string VectorToString(const std::vector<int64_t> &data) const;
185     void StringToVector(const std::string &str, std::vector<int64_t> &data) const;
186     int32_t StringToInt(const std::string &str) const;
187     int64_t StringToInt64(const std::string &str) const;
188     bool IsSlotKey(const std::string &bundleKey, const std::string &key) const;
189     std::string GenerateSlotKey(
190         const std::string &bundleKey, const std::string &type = "", const std::string &subType = "") const;
191     std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const;
192 
193     void ParseBundleFromDistureDB(
194         NotificationPreferencesInfo &info, const std::unordered_map<std::string, std::string> &entries);
195     void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey,
196         const std::pair<std::string, std::string> &entry);
197     void ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo,
198         const std::string &bundleKey, const std::pair<std::string, std::string> &entry);
199     void ParseDoNotDisturbType(NotificationPreferencesInfo &info);
200     void ParseDoNotDisturbBeginDate(NotificationPreferencesInfo &info);
201     void ParseDoNotDisturbEndDate(NotificationPreferencesInfo &info);
202     void ParseEnableAllNotification(NotificationPreferencesInfo &info);
203     void ParseBundleName(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
204     void ParseBundleImportance(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
205     void ParseBundleShowBadge(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
206     void ParseBundleBadgeNum(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
207     void ParseBundleEnableNotification(
208         NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
209     void ParseBundlePoppedDialog(
210         NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
211     void ParseBundleUid(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const;
212     void ParseSlot(
213         const std::string &findString, sptr<NotificationSlot> &slot, const std::pair<std::string, std::string> &entry);
214     void ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const;
215     void ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const;
216     void ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const;
217     void ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const;
218     void ParseSlotEnableVrbration(sptr<NotificationSlot> &slot, const std::string &value) const;
219     void ParseSlotLedLightColor(sptr<NotificationSlot> &slot, const std::string &value) const;
220     void ParseSlotLockscreenVisibleness(sptr<NotificationSlot> &slot, const std::string &value) const;
221     void ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const;
222     void ParseSlotVibrationSytle(sptr<NotificationSlot> &slot, const std::string &value) const;
223     void ParseSlotEnableBypassDnd(sptr<NotificationSlot> &slot, const std::string &value) const;
224     void ParseSlotEnabled(sptr<NotificationSlot> &slot, const std::string &value) const;
225 
226     std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo) const;
227     void GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId);
228     void GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId);
229     void GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId);
230     void GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId);
231 
232     static const std::map<std::string,
233         std::function<void(NotificationPreferencesDatabase *, sptr<NotificationSlot> &, std::string &)>>
234         slotMap_;
235     static const std::map<std::string, std::function<void(NotificationPreferencesDatabase *,
236                                            NotificationPreferencesInfo::BundleInfo &, std::string &)>>
237         bundleMap_;
238 
239     std::shared_ptr<NotificationDataMgr> rdbDataManager_;
240 };
241 } // namespace Notification
242 } // namespace OHOS
243 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H
244