• 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 #include "notification_preferences_database.h"
17 
18 #include "ans_const_define.h"
19 #include "ans_log_wrapper.h"
20 #include "hitrace_meter.h"
21 #include "os_account_manager.h"
22 
23 #include "uri.h"
24 namespace OHOS {
25 namespace Notification {
26 /**
27  * Indicates that disturbe key which do not disturbe type.
28  */
29 const static std::string KEY_DO_NOT_DISTURB_TYPE = "ans_doNotDisturbType";
30 
31 /**
32  * Indicates that disturbe key which do not disturbe begin date.
33  */
34 const static std::string KEY_DO_NOT_DISTURB_BEGIN_DATE = "ans_doNotDisturbBeginDate";
35 
36 /**
37  * Indicates that disturbe key which do not disturbe end date.
38  */
39 const static std::string KEY_DO_NOT_DISTURB_END_DATE = "ans_doNotDisturbEndDate";
40 
41 /**
42  * Indicates that disturbe key which enable all notification.
43  */
44 const static std::string KEY_ENABLE_ALL_NOTIFICATION = "ans_notificationAll";
45 
46 /**
47  * Indicates that disturbe key which bundle label.
48  */
49 const static std::string KEY_BUNDLE_LABEL = "label_ans_bundle_";
50 
51 /**
52  * Indicates that disturbe key which under line.
53  */
54 const static std::string KEY_UNDER_LINE = "_";
55 
56 /**
57  * Indicates that disturbe key which bundle begin key.
58  */
59 const static std::string KEY_ANS_BUNDLE = "ans_bundle";
60 
61 /**
62  * Indicates that disturbe key which bundle name.
63  */
64 const static std::string KEY_BUNDLE_NAME = "name";
65 
66 /**
67  * Indicates that disturbe key which bundle imortance.
68  */
69 const static std::string KEY_BUNDLE_IMPORTANCE = "importance";
70 
71 /**
72  * Indicates that disturbe key which bundle show badge.
73  */
74 const static std::string KEY_BUNDLE_SHOW_BADGE = "showBadge";
75 
76 /**
77  * Indicates that disturbe key which bundle total badge num.
78  */
79 const static std::string KEY_BUNDLE_BADGE_TOTAL_NUM = "badgeTotalNum";
80 
81 /**
82  * Indicates that disturbe key which bundle enable notification.
83  */
84 const static std::string KEY_BUNDLE_ENABLE_NOTIFICATION = "enabledNotification";
85 
86 /**
87  * Indicates that disturbe key which bundle popped dialog.
88  */
89 const static std::string KEY_BUNDLE_POPPED_DIALOG = "poppedDialog";
90 
91 /**
92  * Indicates that disturbe key which bundle uid.
93  */
94 const static std::string KEY_BUNDLE_UID = "uid";
95 
96 /**
97  * Indicates that disturbe key which slot.
98  */
99 const static std::string KEY_SLOT = "slot";
100 
101 /**
102  * Indicates that disturbe key which slot type.
103  */
104 const static std::string KEY_SLOT_TYPE = "type";
105 
106 /**
107  * Indicates that disturbe key which slot id.
108  */
109 const static std::string KEY_SLOT_ID = "id";
110 
111 /**
112  * Indicates that disturbe key which slot name.
113  */
114 const static std::string KEY_SLOT_NAME = "name";
115 
116 /**
117  * Indicates that disturbe key which slot description.
118  */
119 const static std::string KEY_SLOT_DESCRIPTION = "description";
120 
121 /**
122  * Indicates that disturbe key which slot level.
123  */
124 const static std::string KEY_SLOT_LEVEL = "level";
125 
126 /**
127  * Indicates that disturbe key which slot show badge.
128  */
129 const static std::string KEY_SLOT_SHOW_BADGE = "showBadge";
130 
131 /**
132  * Indicates that disturbe key which slot enable light.
133  */
134 const static std::string KEY_SLOT_ENABLE_LIGHT = "enableLight";
135 
136 /**
137  * Indicates that disturbe key which slot enable vibration.
138  */
139 const static std::string KEY_SLOT_ENABLE_VRBRATION = "enableVibration";
140 
141 /**
142  * Indicates that disturbe key which slot led light color.
143  */
144 const static std::string KEY_SLOT_LED_LIGHT_COLOR = "ledLightColor";
145 
146 /**
147  * Indicates that disturbe key which slot lockscreen visibleness.
148  */
149 const static std::string KEY_SLOT_LOCKSCREEN_VISIBLENESS = "lockscreenVisibleness";
150 
151 /**
152  * Indicates that disturbe key which slot sound.
153  */
154 const static std::string KEY_SLOT_SOUND = "sound";
155 
156 /**
157  * Indicates that disturbe key which slot vibration style.
158  */
159 const static std::string KEY_SLOT_VIBRATION_STYLE = "vibrationSytle";
160 
161 /**
162  * Indicates that disturbe key which slot enable bypass end.
163  */
164 const static std::string KEY_SLOT_ENABLE_BYPASS_DND = "enableBypassDnd";
165 
166 /**
167  * Indicates whether the type of slot is enabled.
168  */
169 const static std::string KEY_SLOT_ENABLED = "enabled";
170 
171 
172 const std::map<std::string,
173     std::function<void(NotificationPreferencesDatabase *, sptr<NotificationSlot> &, std::string &)>>
174     NotificationPreferencesDatabase::slotMap_ = {
175         {
176             KEY_SLOT_DESCRIPTION,
177             std::bind(&NotificationPreferencesDatabase::ParseSlotDescription, std::placeholders::_1,
178                 std::placeholders::_2, std::placeholders::_3),
179         },
180         {
181             KEY_SLOT_LEVEL,
182             std::bind(&NotificationPreferencesDatabase::ParseSlotLevel, std::placeholders::_1, std::placeholders::_2,
183                 std::placeholders::_3),
184         },
185         {
186             KEY_SLOT_SHOW_BADGE,
187             std::bind(&NotificationPreferencesDatabase::ParseSlotShowBadge, std::placeholders::_1,
188                 std::placeholders::_2, std::placeholders::_3),
189         },
190         {
191             KEY_SLOT_ENABLE_LIGHT,
192             std::bind(&NotificationPreferencesDatabase::ParseSlotEnableLight, std::placeholders::_1,
193                 std::placeholders::_2, std::placeholders::_3),
194         },
195         {
196             KEY_SLOT_ENABLE_VRBRATION,
197             std::bind(&NotificationPreferencesDatabase::ParseSlotEnableVrbration, std::placeholders::_1,
198                 std::placeholders::_2, std::placeholders::_3),
199         },
200         {
201             KEY_SLOT_LED_LIGHT_COLOR,
202             std::bind(&NotificationPreferencesDatabase::ParseSlotLedLightColor, std::placeholders::_1,
203                 std::placeholders::_2, std::placeholders::_3),
204         },
205         {
206             KEY_SLOT_LOCKSCREEN_VISIBLENESS,
207             std::bind(&NotificationPreferencesDatabase::ParseSlotLockscreenVisibleness, std::placeholders::_1,
208                 std::placeholders::_2, std::placeholders::_3),
209         },
210         {
211             KEY_SLOT_SOUND,
212             std::bind(&NotificationPreferencesDatabase::ParseSlotSound, std::placeholders::_1, std::placeholders::_2,
213                 std::placeholders::_3),
214         },
215         {
216             KEY_SLOT_VIBRATION_STYLE,
217             std::bind(&NotificationPreferencesDatabase::ParseSlotVibrationSytle, std::placeholders::_1,
218                 std::placeholders::_2, std::placeholders::_3),
219         },
220         {
221             KEY_SLOT_ENABLE_BYPASS_DND,
222             std::bind(&NotificationPreferencesDatabase::ParseSlotEnableBypassDnd, std::placeholders::_1,
223                 std::placeholders::_2, std::placeholders::_3),
224         },
225         {
226             KEY_SLOT_ENABLED,
227             std::bind(&NotificationPreferencesDatabase::ParseSlotEnabled, std::placeholders::_1,
228                 std::placeholders::_2, std::placeholders::_3),
229         },
230 };
231 
232 const std::map<std::string,
233     std::function<void(NotificationPreferencesDatabase *, NotificationPreferencesInfo::BundleInfo &, std::string &)>>
234     NotificationPreferencesDatabase::bundleMap_ = {
235         {
236             KEY_BUNDLE_NAME,
237             std::bind(&NotificationPreferencesDatabase::ParseBundleName, std::placeholders::_1, std::placeholders::_2,
238                 std::placeholders::_3),
239         },
240         {
241             KEY_BUNDLE_IMPORTANCE,
242             std::bind(&NotificationPreferencesDatabase::ParseBundleImportance, std::placeholders::_1,
243                 std::placeholders::_2, std::placeholders::_3),
244         },
245         {
246             KEY_BUNDLE_SHOW_BADGE,
247             std::bind(&NotificationPreferencesDatabase::ParseBundleShowBadge, std::placeholders::_1,
248                 std::placeholders::_2, std::placeholders::_3),
249         },
250         {
251             KEY_BUNDLE_BADGE_TOTAL_NUM,
252             std::bind(&NotificationPreferencesDatabase::ParseBundleBadgeNum, std::placeholders::_1,
253                 std::placeholders::_2, std::placeholders::_3),
254         },
255         {
256             KEY_BUNDLE_ENABLE_NOTIFICATION,
257             std::bind(&NotificationPreferencesDatabase::ParseBundleEnableNotification, std::placeholders::_1,
258                 std::placeholders::_2, std::placeholders::_3),
259         },
260         {
261             KEY_BUNDLE_POPPED_DIALOG,
262             std::bind(&NotificationPreferencesDatabase::ParseBundlePoppedDialog, std::placeholders::_1,
263                 std::placeholders::_2, std::placeholders::_3),
264         },
265         {
266             KEY_BUNDLE_UID,
267             std::bind(&NotificationPreferencesDatabase::ParseBundleUid, std::placeholders::_1, std::placeholders::_2,
268                 std::placeholders::_3),
269         },
270 };
271 
NotificationPreferencesDatabase()272 NotificationPreferencesDatabase::NotificationPreferencesDatabase()
273 {
274     NotificationRdbConfig notificationRdbConfig;
275     rdbDataManager_ = std::make_shared<NotificationDataMgr>(notificationRdbConfig);
276     ANS_LOGD("Notification Rdb is created");
277 }
278 
~NotificationPreferencesDatabase()279 NotificationPreferencesDatabase::~NotificationPreferencesDatabase()
280 {
281     ANS_LOGD("Notification Rdb is deleted");
282 }
283 
CheckRdbStore()284 bool NotificationPreferencesDatabase::CheckRdbStore()
285 {
286     if (rdbDataManager_ != nullptr) {
287         int32_t result = rdbDataManager_->Init();
288         if (result == NativeRdb::E_OK) {
289             return true;
290         }
291     }
292 
293     return false;
294 }
295 
PutSlotsToDisturbeDB(const std::string & bundleName,const int32_t & bundleUid,const std::vector<sptr<NotificationSlot>> & slots)296 bool NotificationPreferencesDatabase::PutSlotsToDisturbeDB(
297     const std::string &bundleName, const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots)
298 {
299     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
300     ANS_LOGD("%{public}s", __FUNCTION__);
301     if (bundleName.empty()) {
302         ANS_LOGE("Bundle name is null.");
303         return false;
304     }
305 
306     if (slots.empty()) {
307         ANS_LOGE("Slot is empty.");
308         return false;
309     }
310 
311     std::unordered_map<std::string, std::string> values;
312     for (auto iter : slots) {
313         bool result = SlotToEntry(bundleName, bundleUid, iter, values);
314         if (!result) {
315             return result;
316         }
317     }
318 
319     if (!CheckRdbStore()) {
320         ANS_LOGE("RdbStore is nullptr.");
321         return false;
322     }
323     int32_t result = rdbDataManager_->InsertBatchData(values);
324     return (result == NativeRdb::E_OK);
325 }
326 
PutBundlePropertyToDisturbeDB(const NotificationPreferencesInfo::BundleInfo & bundleInfo)327 bool NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB(
328     const NotificationPreferencesInfo::BundleInfo &bundleInfo)
329 {
330     if (bundleInfo.GetBundleName().empty()) {
331         ANS_LOGE("Bundle name is null.");
332         return false;
333     }
334 
335     if (!CheckRdbStore()) {
336         ANS_LOGE("RdbStore is nullptr.");
337         return false;
338     }
339     std::string values;
340     std::string bundleKeyStr = KEY_BUNDLE_LABEL + GenerateBundleLablel(bundleInfo);
341     bool result = false;
342     GetValueFromDisturbeDB(bundleKeyStr, [&](const int32_t &status, std::string &value) {
343         switch (status) {
344             case NativeRdb::E_EMPTY_VALUES_BUCKET: {
345                 result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo);
346                 break;
347             }
348             case NativeRdb::E_OK: {
349                 ANS_LOGE("Current bundle has exsited.");
350                 break;
351             }
352             default:
353                 break;
354         }
355     });
356     return result;
357 }
358 
PutShowBadge(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & enable)359 bool NotificationPreferencesDatabase::PutShowBadge(
360     const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable)
361 {
362     if (bundleInfo.GetBundleName().empty()) {
363         ANS_LOGE("Bundle name is null.");
364         return false;
365     }
366 
367     if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
368         return false;
369     }
370 
371     std::string bundleKey = GenerateBundleLablel(bundleInfo);
372     int32_t result =
373         PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable);
374     return (result == NativeRdb::E_OK);
375 }
376 
PutImportance(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const int32_t & importance)377 bool NotificationPreferencesDatabase::PutImportance(
378     const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &importance)
379 {
380     if (bundleInfo.GetBundleName().empty()) {
381         ANS_LOGE("Bundle name is null.");
382         return false;
383     }
384 
385     if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
386         return false;
387     }
388 
389     std::string bundleKey = GenerateBundleLablel(bundleInfo);
390     int32_t result =
391         PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_IMPORTANCE_TYPE, importance);
392     return (result == NativeRdb::E_OK);
393 }
394 
PutTotalBadgeNums(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const int32_t & totalBadgeNum)395 bool NotificationPreferencesDatabase::PutTotalBadgeNums(
396     const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &totalBadgeNum)
397 {
398     if (bundleInfo.GetBundleName().empty()) {
399         ANS_LOGE("Bundle name is null.");
400         return false;
401     }
402 
403     if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
404         return false;
405     }
406     std::string bundleKey = GenerateBundleLablel(bundleInfo);
407     int32_t result =
408         PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum);
409     return (result == NativeRdb::E_OK);
410 }
411 
PutNotificationsEnabledForBundle(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & enabled)412 bool NotificationPreferencesDatabase::PutNotificationsEnabledForBundle(
413     const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled)
414 {
415     ANS_LOGD("%{public}s, enabled[%{public}d]", __FUNCTION__, enabled);
416     if (bundleInfo.GetBundleName().empty()) {
417         ANS_LOGE("Bundle name is null.");
418         return false;
419     }
420 
421     if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
422         return false;
423     }
424 
425     std::string bundleKey = GenerateBundleLablel(bundleInfo);
426     int32_t result =
427         PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled);
428     return (result == NativeRdb::E_OK);
429 }
430 
PutNotificationsEnabled(const int32_t & userId,const bool & enabled)431 bool NotificationPreferencesDatabase::PutNotificationsEnabled(const int32_t &userId, const bool &enabled)
432 {
433     if (!CheckRdbStore()) {
434         ANS_LOGE("RdbStore is nullptr.");
435         return false;
436     }
437 
438     std::string typeKey =
439         std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
440     std::string enableValue = std::to_string(enabled);
441     int32_t result = rdbDataManager_->InsertData(typeKey, enableValue);
442     if (result != NativeRdb::E_OK) {
443         ANS_LOGE("Store enable notification failed. %{public}d", result);
444         return false;
445     }
446     return true;
447 }
448 
PutHasPoppedDialog(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & hasPopped)449 bool NotificationPreferencesDatabase::PutHasPoppedDialog(
450     const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped)
451 {
452     if (bundleInfo.GetBundleName().empty()) {
453         ANS_LOGE("Bundle name is null.");
454         return false;
455     }
456 
457     if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
458         return false;
459     }
460 
461     std::string bundleKey = GenerateBundleLablel(bundleInfo);
462     int32_t result =
463         PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
464     return (result == NativeRdb::E_OK);
465 }
466 
PutDoNotDisturbDate(const int32_t & userId,const sptr<NotificationDoNotDisturbDate> & date)467 bool NotificationPreferencesDatabase::PutDoNotDisturbDate(
468     const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date)
469 {
470     if (date == nullptr) {
471         ANS_LOGE("Invalid date.");
472         return false;
473     }
474 
475     if (!CheckRdbStore()) {
476         ANS_LOGE("RdbStore is nullptr.");
477         return false;
478     }
479 
480     std::string typeKey =
481         std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
482     std::string typeValue = std::to_string((int)date->GetDoNotDisturbType());
483 
484     std::string beginDateKey =
485         std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
486     std::string beginDateValue = std::to_string(date->GetBeginDate());
487 
488     std::string endDateKey =
489         std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
490     std::string endDateValue = std::to_string(date->GetEndDate());
491 
492     std::unordered_map<std::string, std::string> values = {
493         {typeKey, typeValue},
494         {beginDateKey, beginDateValue},
495         {endDateKey, endDateValue},
496     };
497 
498     int32_t result = rdbDataManager_->InsertBatchData(values);
499     if (result != NativeRdb::E_OK) {
500         ANS_LOGE("Store DoNotDisturbDate failed. %{public}d", result);
501         return false;
502     }
503 
504     return true;
505 }
506 
GetValueFromDisturbeDB(const std::string & key,std::function<void (std::string &)> callback)507 void NotificationPreferencesDatabase::GetValueFromDisturbeDB(
508     const std::string &key, std::function<void(std::string &)> callback)
509 {
510     if (!CheckRdbStore()) {
511         ANS_LOGE("RdbStore is nullptr.");
512         return;
513     }
514     std::string value;
515     int32_t result = rdbDataManager_->QueryData(key, value);
516     if (result == NativeRdb::E_ERROR) {
517         ANS_LOGE("Get value failed, use default value. error code is %{public}d", result);
518         return;
519     }
520     callback(value);
521 }
522 
GetValueFromDisturbeDB(const std::string & key,std::function<void (int32_t &,std::string &)> callback)523 void NotificationPreferencesDatabase::GetValueFromDisturbeDB(
524     const std::string &key, std::function<void(int32_t &, std::string &)> callback)
525 {
526     if (!CheckRdbStore()) {
527         ANS_LOGE("RdbStore is nullptr.");
528         return;
529     }
530     std::string value;
531     int32_t result = rdbDataManager_->QueryData(key, value);
532     callback(result, value);
533 }
534 
CheckBundle(const std::string & bundleName,const int32_t & bundleUid)535 bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleName, const int32_t &bundleUid)
536 {
537     std::string bundleKeyStr = KEY_BUNDLE_LABEL + bundleName + std::to_string(bundleUid);
538     ANS_LOGD("CheckBundle bundleKeyStr %{public}s", bundleKeyStr.c_str());
539     bool result = true;
540     GetValueFromDisturbeDB(bundleKeyStr, [&](const int32_t &status, std::string &value) {
541         switch (status) {
542             case NativeRdb::E_EMPTY_VALUES_BUCKET: {
543                 NotificationPreferencesInfo::BundleInfo bundleInfo;
544                 bundleInfo.SetBundleName(bundleName);
545                 bundleInfo.SetBundleUid(bundleUid);
546                 result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo);
547                 break;
548             }
549             case NativeRdb::E_OK: {
550                 result = true;
551                 break;
552             }
553             default:
554                 result = false;
555                 break;
556         }
557     });
558     return result;
559 }
560 
PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo & bundleInfo)561 bool NotificationPreferencesDatabase::PutBundlePropertyValueToDisturbeDB(
562     const NotificationPreferencesInfo::BundleInfo &bundleInfo)
563 {
564     std::unordered_map<std::string, std::string> values;
565     std::string bundleKey = bundleInfo.GetBundleName().append(std::to_string(bundleInfo.GetBundleUid()));
566     GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_NAME), bundleInfo.GetBundleName(), values);
567     GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_BADGE_TOTAL_NUM),
568         std::to_string(bundleInfo.GetBadgeTotalNum()),
569         values);
570     GenerateEntry(
571         GenerateBundleKey(bundleKey, KEY_BUNDLE_IMPORTANCE), std::to_string(bundleInfo.GetImportance()), values);
572     GenerateEntry(
573         GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE), std::to_string(bundleInfo.GetIsShowBadge()), values);
574     GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION),
575         std::to_string(bundleInfo.GetEnableNotification()),
576         values);
577     GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_POPPED_DIALOG),
578         std::to_string(bundleInfo.GetHasPoppedDialog()),
579         values);
580     GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_UID), std::to_string(bundleInfo.GetBundleUid()), values);
581     if (!CheckRdbStore()) {
582         ANS_LOGE("RdbStore is nullptr.");
583         return false;
584     }
585     int32_t result = rdbDataManager_->InsertBatchData(values);
586     if (result != NativeRdb::E_OK) {
587         ANS_LOGE("Store bundle failed. %{public}d", result);
588         return false;
589     }
590     return true;
591 }
592 
ParseFromDisturbeDB(NotificationPreferencesInfo & info)593 bool NotificationPreferencesDatabase::ParseFromDisturbeDB(NotificationPreferencesInfo &info)
594 {
595     ANS_LOGD("%{public}s", __FUNCTION__);
596     ParseDoNotDisturbType(info);
597     ParseDoNotDisturbBeginDate(info);
598     ParseDoNotDisturbEndDate(info);
599     ParseEnableAllNotification(info);
600 
601     if (!CheckRdbStore()) {
602         ANS_LOGE("RdbStore is nullptr.");
603         return false;
604     }
605     std::unordered_map<std::string, std::string> values;
606     int32_t result = rdbDataManager_->QueryDataBeginWithKey(KEY_BUNDLE_LABEL, values);
607     if (result == NativeRdb::E_ERROR) {
608         ANS_LOGE("Get Bundle Info failed.");
609         return false;
610     }
611     ParseBundleFromDistureDB(info, values);
612     return true;
613 }
614 
RemoveAllDataFromDisturbeDB()615 bool NotificationPreferencesDatabase::RemoveAllDataFromDisturbeDB()
616 {
617     ANS_LOGD("%{public}s", __FUNCTION__);
618     if (!CheckRdbStore()) {
619         ANS_LOGE("RdbStore is nullptr.");
620         return false;
621     }
622     int32_t result = rdbDataManager_->Destroy();
623     return (result == NativeRdb::E_OK);
624 }
625 
RemoveBundleFromDisturbeDB(const std::string & bundleKey)626 bool NotificationPreferencesDatabase::RemoveBundleFromDisturbeDB(const std::string &bundleKey)
627 {
628     ANS_LOGD("%{public}s", __FUNCTION__);
629     if (!CheckRdbStore()) {
630         ANS_LOGE("RdbStore is nullptr.");
631         return false;
632     }
633 
634     std::unordered_map<std::string, std::string> values;
635     int32_t result = rdbDataManager_->QueryDataBeginWithKey(
636         (KEY_ANS_BUNDLE + KEY_UNDER_LINE + bundleKey + KEY_UNDER_LINE), values);
637 
638     if (result == NativeRdb::E_ERROR) {
639         ANS_LOGE("Get Bundle Info failed.");
640         return false;
641     }
642 
643     std::vector<std::string> keys;
644     for (auto iter : values) {
645         keys.push_back(iter.first);
646     }
647 
648     std::string bundleDBKey = KEY_BUNDLE_LABEL + KEY_BUNDLE_NAME + KEY_UNDER_LINE + bundleKey;
649     keys.push_back(bundleDBKey);
650     result = rdbDataManager_->DeleteBathchData(keys);
651     if (result != NativeRdb::E_OK) {
652         ANS_LOGE("delete bundle Info failed.");
653         return false;
654     }
655     return true;
656 }
657 
RemoveSlotFromDisturbeDB(const std::string & bundleKey,const NotificationConstant::SlotType & type)658 bool NotificationPreferencesDatabase::RemoveSlotFromDisturbeDB(
659     const std::string &bundleKey, const NotificationConstant::SlotType &type)
660 {
661     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
662     ANS_LOGD("%{public}s", __FUNCTION__);
663     if (bundleKey.empty()) {
664         ANS_LOGE("Bundle name is null.");
665         return false;
666     }
667 
668     if (!CheckRdbStore()) {
669         ANS_LOGE("RdbStore is nullptr.");
670         return false;
671     }
672 
673     std::unordered_map<std::string, std::string> values;
674     std::string slotType = std::to_string(type);
675     int32_t result = rdbDataManager_->QueryDataBeginWithKey(
676         (GenerateSlotKey(bundleKey, slotType) + KEY_UNDER_LINE), values);
677     if (result == NativeRdb::E_ERROR) {
678         return false;
679     }
680     std::vector<std::string> keys;
681     for (auto iter : values) {
682         keys.push_back(iter.first);
683     }
684 
685     result = rdbDataManager_->DeleteBathchData(keys);
686     if (result != NativeRdb::E_OK) {
687         ANS_LOGE("delete bundle Info failed.");
688         return false;
689     }
690 
691     return true;
692 }
693 
RemoveAllSlotsFromDisturbeDB(const std::string & bundleKey)694 bool NotificationPreferencesDatabase::RemoveAllSlotsFromDisturbeDB(const std::string &bundleKey)
695 {
696     ANS_LOGD("%{public}s", __FUNCTION__);
697     if (bundleKey.empty()) {
698         ANS_LOGE("Bundle name is null.");
699         return false;
700     }
701 
702     if (!CheckRdbStore()) {
703         ANS_LOGE("RdbStore is nullptr.");
704         return false;
705     }
706 
707     std::unordered_map<std::string, std::string> values;
708     int32_t result = rdbDataManager_->QueryDataBeginWithKey(
709         (GenerateSlotKey(bundleKey) + KEY_UNDER_LINE), values);
710     if (result == NativeRdb::E_ERROR) {
711         return false;
712     }
713     std::vector<std::string> keys;
714     for (auto iter : values) {
715         keys.push_back(iter.first);
716     }
717 
718     result = rdbDataManager_->DeleteBathchData(keys);
719     return (result == NativeRdb::E_OK);
720 }
721 
StoreDeathRecipient()722 bool NotificationPreferencesDatabase::StoreDeathRecipient()
723 {
724     ANS_LOGW("distribute remote died");
725     rdbDataManager_ = nullptr;
726     return true;
727 }
728 
729 template <typename T>
PutBundlePropertyToDisturbeDB(const std::string & bundleKey,const BundleType & type,const T & t)730 int32_t NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB(
731     const std::string &bundleKey, const BundleType &type, const T &t)
732 {
733     std::string keyStr;
734     switch (type) {
735         case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
736             keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_BADGE_TOTAL_NUM);
737             break;
738         case BundleType::BUNDLE_IMPORTANCE_TYPE:
739             keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_IMPORTANCE);
740             break;
741         case BundleType::BUNDLE_SHOW_BADGE_TYPE:
742             keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE);
743             break;
744         case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
745             keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION);
746             break;
747         case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
748             keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_POPPED_DIALOG);
749             break;
750         default:
751             break;
752     }
753     if (!CheckRdbStore()) {
754         ANS_LOGE("RdbStore is nullptr.");
755         return false;
756     }
757     std::string valueStr = std::to_string(t);
758     int32_t result = rdbDataManager_->InsertData(keyStr, valueStr);
759     return result;
760 }
761 
PutBundleToDisturbeDB(const std::string & bundleKey,const NotificationPreferencesInfo::BundleInfo & bundleInfo)762 bool NotificationPreferencesDatabase::PutBundleToDisturbeDB(
763     const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo)
764 {
765     if (!CheckRdbStore()) {
766         ANS_LOGE("RdbStore is nullptr.");
767         return false;
768     }
769 
770     ANS_LOGD("Key not fund, so create a bundle, bundle key is %{public}s.", bundleKey.c_str());
771     int32_t result = rdbDataManager_->InsertData(bundleKey, GenerateBundleLablel(bundleInfo));
772     if (result != NativeRdb::E_OK) {
773         ANS_LOGE("Store bundle name to db is failed.");
774         return false;
775     }
776 
777     if (!PutBundlePropertyValueToDisturbeDB(bundleInfo)) {
778         return false;
779     }
780     return true;
781 }
782 
GenerateEntry(const std::string & key,const std::string & value,std::unordered_map<std::string,std::string> & values) const783 void NotificationPreferencesDatabase::GenerateEntry(
784     const std::string &key, const std::string &value, std::unordered_map<std::string, std::string> &values) const
785 {
786     values.emplace(key, value);
787 }
788 
SlotToEntry(const std::string & bundleName,const int32_t & bundleUid,const sptr<NotificationSlot> & slot,std::unordered_map<std::string,std::string> & values)789 bool NotificationPreferencesDatabase::SlotToEntry(const std::string &bundleName, const int32_t &bundleUid,
790     const sptr<NotificationSlot> &slot, std::unordered_map<std::string, std::string> &values)
791 {
792     if (slot == nullptr) {
793         ANS_LOGE("Notification slot is nullptr.");
794         return false;
795     }
796 
797     if (!CheckBundle(bundleName, bundleUid)) {
798         return false;
799     }
800 
801     std::string bundleKey = bundleName + std::to_string(bundleUid);
802     GenerateSlotEntry(bundleKey, slot, values);
803     return true;
804 }
805 
GenerateSlotEntry(const std::string & bundleKey,const sptr<NotificationSlot> & slot,std::unordered_map<std::string,std::string> & values) const806 void NotificationPreferencesDatabase::GenerateSlotEntry(const std::string &bundleKey,
807     const sptr<NotificationSlot> &slot, std::unordered_map<std::string, std::string> &values) const
808 {
809     std::string slotType = std::to_string(slot->GetType());
810     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_TYPE), std::to_string(slot->GetType()), values);
811     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ID), slot->GetId(), values);
812     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_NAME), slot->GetName(), values);
813     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_DESCRIPTION), slot->GetDescription(), values);
814     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LEVEL), std::to_string(slot->GetLevel()), values);
815     GenerateEntry(
816         GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SHOW_BADGE), std::to_string(slot->IsShowBadge()), values);
817     GenerateEntry(
818         GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_LIGHT), std::to_string(slot->CanEnableLight()), values);
819     GenerateEntry(
820         GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_VRBRATION), std::to_string(slot->CanVibrate()), values);
821     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LED_LIGHT_COLOR),
822         std::to_string(slot->GetLedLightColor()), values);
823     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LOCKSCREEN_VISIBLENESS),
824         std::to_string(static_cast<int>(slot->GetLockScreenVisibleness())), values);
825     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SOUND), slot->GetSound().ToString(), values);
826     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_BYPASS_DND),
827         std::to_string(slot->IsEnableBypassDnd()), values);
828     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_VIBRATION_STYLE),
829         VectorToString(slot->GetVibrationStyle()), values);
830     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLED), std::to_string(slot->GetEnable()), values);
831 }
832 
ParseBundleFromDistureDB(NotificationPreferencesInfo & info,const std::unordered_map<std::string,std::string> & values)833 void NotificationPreferencesDatabase::ParseBundleFromDistureDB(
834     NotificationPreferencesInfo &info, const std::unordered_map<std::string, std::string> &values)
835 {
836     if (!CheckRdbStore()) {
837         ANS_LOGE("RdbStore is nullptr.");
838         return;
839     }
840     for (auto item : values) {
841         std::string bundleKey = item.second;
842         ANS_LOGD("Bundle name is %{public}s.", bundleKey.c_str());
843         std::unordered_map<std::string, std::string> bundleEntries;
844         rdbDataManager_->QueryDataBeginWithKey((GenerateBundleKey(bundleKey)), bundleEntries);
845         ANS_LOGD("Bundle key is %{public}s.", GenerateBundleKey(bundleKey).c_str());
846         NotificationPreferencesInfo::BundleInfo bunldeInfo;
847         for (auto bundleEntry : bundleEntries) {
848             if (IsSlotKey(GenerateBundleKey(bundleKey), bundleEntry.first)) {
849                 ParseSlotFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry);
850             } else {
851                 ParseBundlePropertyFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry);
852             }
853         }
854 
855         info.SetBundleInfoFromDb(bunldeInfo, bundleKey);
856     }
857 }
858 
ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & bundleKey,const std::pair<std::string,std::string> & entry)859 void NotificationPreferencesDatabase::ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo,
860     const std::string &bundleKey, const std::pair<std::string, std::string> &entry)
861 {
862     std::string slotKey = entry.first;
863     std::string typeStr = SubUniqueIdentifyFromString(GenerateSlotKey(bundleKey) + KEY_UNDER_LINE, slotKey);
864     NotificationConstant::SlotType slotType = static_cast<NotificationConstant::SlotType>(StringToInt(typeStr));
865     sptr<NotificationSlot> slot = nullptr;
866     if (!bundleInfo.GetSlot(slotType, slot)) {
867         slot = new NotificationSlot(slotType);
868     }
869     std::string findString = GenerateSlotKey(bundleKey, typeStr) + KEY_UNDER_LINE;
870     ParseSlot(findString, slot, entry);
871     bundleInfo.SetSlot(slot);
872 }
873 
ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & bundleKey,const std::pair<std::string,std::string> & entry)874 void NotificationPreferencesDatabase::ParseBundlePropertyFromDisturbeDB(
875     NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey,
876     const std::pair<std::string, std::string> &entry)
877 {
878     std::string typeStr = FindLastString(GenerateBundleKey(bundleKey), entry.first);
879     std::string valueStr = entry.second;
880 
881     auto iter = bundleMap_.find(typeStr);
882     if (iter != bundleMap_.end()) {
883         auto func = iter->second;
884         func(this, bundleInfo, valueStr);
885     }
886 }
887 
ParseSlot(const std::string & findString,sptr<NotificationSlot> & slot,const std::pair<std::string,std::string> & entry)888 void NotificationPreferencesDatabase::ParseSlot(
889     const std::string &findString, sptr<NotificationSlot> &slot, const std::pair<std::string, std::string> &entry)
890 {
891     std::string typeStr = FindLastString(findString, entry.first);
892     std::string valueStr = entry.second;
893     ANS_LOGD("db key = %{public}s , %{public}s : %{public}s ",
894         entry.first.c_str(),
895         typeStr.c_str(),
896         entry.second.c_str());
897 
898     auto iter = slotMap_.find(typeStr);
899     if (iter != slotMap_.end()) {
900         auto func = iter->second;
901         func(this, slot, valueStr);
902     }
903 
904     if (!typeStr.compare(KEY_SLOT_VIBRATION_STYLE)) {
905         GetValueFromDisturbeDB(findString + KEY_SLOT_ENABLE_VRBRATION,
906             [&](std::string &value) { ParseSlotEnableVrbration(slot, value); });
907     }
908 }
909 
FindLastString(const std::string & findString,const std::string & inputString) const910 std::string NotificationPreferencesDatabase::FindLastString(
911     const std::string &findString, const std::string &inputString) const
912 {
913     std::string keyStr;
914     size_t pos = findString.size();
915     if (pos != std::string::npos) {
916         keyStr = inputString.substr(pos);
917     }
918     return keyStr;
919 }
920 
VectorToString(const std::vector<int64_t> & data) const921 std::string NotificationPreferencesDatabase::VectorToString(const std::vector<int64_t> &data) const
922 {
923     std::stringstream streamStr;
924     std::copy(data.begin(), data.end(), std::ostream_iterator<int>(streamStr, KEY_UNDER_LINE.c_str()));
925     return streamStr.str();
926 }
927 
StringToVector(const std::string & str,std::vector<int64_t> & data) const928 void NotificationPreferencesDatabase::StringToVector(const std::string &str, std::vector<int64_t> &data) const
929 {
930     if (str.empty()) {
931         return;
932     }
933 
934     if (str.find_first_of(KEY_UNDER_LINE) != std::string::npos) {
935         std::string str1 = str.substr(0, str.find_first_of(KEY_UNDER_LINE));
936         std::string afterStr = str.substr(str.find_first_of(KEY_UNDER_LINE) + 1);
937         data.push_back(StringToInt(str1));
938         StringToVector(afterStr, data);
939     }
940 }
941 
StringToInt(const std::string & str) const942 int32_t NotificationPreferencesDatabase::StringToInt(const std::string &str) const
943 {
944     int32_t value = 0;
945     if (!str.empty()) {
946         value = stoi(str, nullptr);
947     }
948     return value;
949 }
950 
StringToInt64(const std::string & str) const951 int64_t NotificationPreferencesDatabase::StringToInt64(const std::string &str) const
952 {
953     int64_t value = 0;
954     if (!str.empty()) {
955         value = stoll(str, nullptr);
956     }
957     return value;
958 }
959 
IsSlotKey(const std::string & bundleKey,const std::string & key) const960 bool NotificationPreferencesDatabase::IsSlotKey(const std::string &bundleKey, const std::string &key) const
961 {
962     std::string tempStr = FindLastString(bundleKey, key);
963     size_t pos = tempStr.find_first_of(KEY_UNDER_LINE);
964     std::string slotStr;
965     if (pos != std::string::npos) {
966         slotStr = tempStr.substr(0, pos);
967     }
968     if (!slotStr.compare(KEY_SLOT)) {
969         return true;
970     }
971     return false;
972 }
973 
GenerateSlotKey(const std::string & bundleKey,const std::string & type,const std::string & subType) const974 std::string NotificationPreferencesDatabase::GenerateSlotKey(
975     const std::string &bundleKey, const std::string &type, const std::string &subType) const
976 {
977     /* slot key
978      *
979      * KEY_ANS_BUNDLE_bundlename_slot_type_0_id
980      * KEY_ANS_BUNDLE_bundlename_slot_type_0_des
981      * KEY_ANS_BUNDLE_bundlename_slot_type_1_id
982      * KEY_ANS_BUNDLE_bundlename_slot_type_1_des
983      *
984      */
985     std::string key = GenerateBundleKey(bundleKey).append(KEY_SLOT).append(KEY_UNDER_LINE).append(KEY_SLOT_TYPE);
986     if (!type.empty()) {
987         key.append(KEY_UNDER_LINE).append(type);
988     }
989     if (!subType.empty()) {
990         key.append(KEY_UNDER_LINE).append(subType);
991     }
992     ANS_LOGD("Slot key is : %{public}s.", key.c_str());
993     return key;
994 }
995 
GenerateBundleKey(const std::string & bundleKey,const std::string & type) const996 std::string NotificationPreferencesDatabase::GenerateBundleKey(
997     const std::string &bundleKey, const std::string &type) const
998 {
999     /* bundle key
1000      *
1001      * label_KEY_ANS_KEY_BUNDLE_NAME = ""
1002      * KEY_ANS_BUNDLE_bundlename_
1003      * KEY_ANS_BUNDLE_bundlename_
1004      * KEY_ANS_BUNDLE_bundlename_
1005      * KEY_ANS_BUNDLE_bundlename_
1006      *
1007      */
1008     ANS_LOGD("%{public}s, bundleKey[%{public}s] type[%{public}s]", __FUNCTION__, bundleKey.c_str(), type.c_str());
1009     std::string key =
1010         std::string().append(KEY_ANS_BUNDLE).append(KEY_UNDER_LINE).append(bundleKey).append(KEY_UNDER_LINE);
1011     if (!type.empty()) {
1012         key.append(type);
1013     }
1014     ANS_LOGD("Bundle key : %{public}s.", key.c_str());
1015     return key;
1016 }
1017 
SubUniqueIdentifyFromString(const std::string & findString,const std::string & keyStr) const1018 std::string NotificationPreferencesDatabase::SubUniqueIdentifyFromString(
1019     const std::string &findString, const std::string &keyStr) const
1020 {
1021     std::string slotType;
1022     std::string tempStr = FindLastString(findString, keyStr);
1023     size_t pos = tempStr.find_last_of(KEY_UNDER_LINE);
1024     if (pos != std::string::npos) {
1025         slotType = tempStr.substr(0, pos);
1026     }
1027 
1028     return slotType;
1029 }
1030 
ParseDoNotDisturbType(NotificationPreferencesInfo & info)1031 void NotificationPreferencesDatabase::ParseDoNotDisturbType(NotificationPreferencesInfo &info)
1032 {
1033     std::vector<int> activeUserId;
1034     OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId);
1035 
1036     for (auto iter : activeUserId) {
1037         NotificationPreferencesDatabase::GetDoNotDisturbType(info, iter);
1038     }
1039 }
1040 
ParseDoNotDisturbBeginDate(NotificationPreferencesInfo & info)1041 void NotificationPreferencesDatabase::ParseDoNotDisturbBeginDate(NotificationPreferencesInfo &info)
1042 {
1043     std::vector<int> activeUserId;
1044     OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId);
1045 
1046     for (auto iter : activeUserId) {
1047         NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(info, iter);
1048     }
1049 }
1050 
ParseDoNotDisturbEndDate(NotificationPreferencesInfo & info)1051 void NotificationPreferencesDatabase::ParseDoNotDisturbEndDate(NotificationPreferencesInfo &info)
1052 {
1053     std::vector<int> activeUserId;
1054     OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId);
1055 
1056     for (auto iter : activeUserId) {
1057         NotificationPreferencesDatabase::GetDoNotDisturbEndDate(info, iter);
1058     }
1059 }
1060 
ParseEnableAllNotification(NotificationPreferencesInfo & info)1061 void NotificationPreferencesDatabase::ParseEnableAllNotification(NotificationPreferencesInfo &info)
1062 {
1063     std::vector<int> activeUserId;
1064     OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId);
1065 
1066     for (auto iter : activeUserId) {
1067         NotificationPreferencesDatabase::GetEnableAllNotification(info, iter);
1068     }
1069 }
1070 
ParseBundleName(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1071 void NotificationPreferencesDatabase::ParseBundleName(
1072     NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1073 {
1074     ANS_LOGD("SetBundleName bundle name is %{public}s.", value.c_str());
1075     bundleInfo.SetBundleName(value);
1076 }
1077 
ParseBundleImportance(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1078 void NotificationPreferencesDatabase::ParseBundleImportance(
1079     NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1080 {
1081     ANS_LOGD("SetBundleImportance bundle importance is %{public}s.", value.c_str());
1082     bundleInfo.SetImportance(static_cast<NotificationSlot::NotificationLevel>(StringToInt(value)));
1083 }
1084 
ParseBundleShowBadge(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1085 void NotificationPreferencesDatabase::ParseBundleShowBadge(
1086     NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1087 {
1088     ANS_LOGD("SetBundleShowBadge bundle show badge is %{public}s.", value.c_str());
1089     bundleInfo.SetIsShowBadge(static_cast<bool>(StringToInt(value)));
1090 }
1091 
ParseBundleBadgeNum(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1092 void NotificationPreferencesDatabase::ParseBundleBadgeNum(
1093     NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1094 {
1095     ANS_LOGD("SetBundleBadgeNum bundle badge num is %{public}s.", value.c_str());
1096     bundleInfo.SetBadgeTotalNum(StringToInt(value));
1097 }
1098 
ParseBundleEnableNotification(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1099 void NotificationPreferencesDatabase::ParseBundleEnableNotification(
1100     NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1101 {
1102     ANS_LOGD("SetBundleEnableNotification bundle enable is %{public}s.", value.c_str());
1103     bundleInfo.SetEnableNotification(static_cast<bool>(StringToInt(value)));
1104 }
1105 
ParseBundlePoppedDialog(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1106 void NotificationPreferencesDatabase::ParseBundlePoppedDialog(
1107     NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1108 {
1109     ANS_LOGD("SetBundlePoppedDialog bundle has popped dialog is %{public}s.", value.c_str());
1110     bundleInfo.SetHasPoppedDialog(static_cast<bool>(StringToInt(value)));
1111 }
1112 
ParseBundleUid(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1113 void NotificationPreferencesDatabase::ParseBundleUid(
1114     NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1115 {
1116     ANS_LOGD("SetBundleUid uuid is %{public}s.", value.c_str());
1117     bundleInfo.SetBundleUid(StringToInt(value));
1118 }
1119 
ParseSlotDescription(sptr<NotificationSlot> & slot,const std::string & value) const1120 void NotificationPreferencesDatabase::ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const
1121 {
1122     ANS_LOGD("ParseSlotDescription slot des is %{public}s.", value.c_str());
1123     std::string slotDescription = value;
1124     slot->SetDescription(slotDescription);
1125 }
1126 
ParseSlotLevel(sptr<NotificationSlot> & slot,const std::string & value) const1127 void NotificationPreferencesDatabase::ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const
1128 {
1129     ANS_LOGD("ParseSlotLevel slot level is %{public}s.", value.c_str());
1130     NotificationSlot::NotificationLevel level = static_cast<NotificationSlot::NotificationLevel>(StringToInt(value));
1131     slot->SetLevel(level);
1132 }
1133 
ParseSlotShowBadge(sptr<NotificationSlot> & slot,const std::string & value) const1134 void NotificationPreferencesDatabase::ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const
1135 {
1136     ANS_LOGD("ParseSlotShowBadge slot show badge is %{public}s.", value.c_str());
1137     bool showBadge = static_cast<bool>(StringToInt(value));
1138     slot->EnableBadge(showBadge);
1139 }
1140 
ParseSlotEnableLight(sptr<NotificationSlot> & slot,const std::string & value) const1141 void NotificationPreferencesDatabase::ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const
1142 {
1143     ANS_LOGD("ParseSlotEnableLight slot enable light is %{public}s.", value.c_str());
1144     bool enableLight = static_cast<bool>(StringToInt(value));
1145     slot->SetEnableLight(enableLight);
1146 }
1147 
ParseSlotEnableVrbration(sptr<NotificationSlot> & slot,const std::string & value) const1148 void NotificationPreferencesDatabase::ParseSlotEnableVrbration(
1149     sptr<NotificationSlot> &slot, const std::string &value) const
1150 {
1151     ANS_LOGD("ParseSlotEnableVrbration slot enable vir is %{public}s.", value.c_str());
1152     bool enableVrbration = static_cast<bool>(StringToInt(value));
1153     slot->SetEnableVibration(enableVrbration);
1154 }
1155 
ParseSlotLedLightColor(sptr<NotificationSlot> & slot,const std::string & value) const1156 void NotificationPreferencesDatabase::ParseSlotLedLightColor(
1157     sptr<NotificationSlot> &slot, const std::string &value) const
1158 {
1159     ANS_LOGD("ParseSlotLedLightColor slot led is %{public}s.", value.c_str());
1160     int32_t ledLightColor = static_cast<int32_t>(StringToInt(value));
1161     slot->SetLedLightColor(ledLightColor);
1162 }
1163 
ParseSlotLockscreenVisibleness(sptr<NotificationSlot> & slot,const std::string & value) const1164 void NotificationPreferencesDatabase::ParseSlotLockscreenVisibleness(
1165     sptr<NotificationSlot> &slot, const std::string &value) const
1166 {
1167 
1168     ANS_LOGD("ParseSlotLockscreenVisibleness slot visible is %{public}s.", value.c_str());
1169     NotificationConstant::VisiblenessType visible =
1170         static_cast<NotificationConstant::VisiblenessType>(StringToInt(value));
1171     slot->SetLockscreenVisibleness(visible);
1172 }
1173 
ParseSlotSound(sptr<NotificationSlot> & slot,const std::string & value) const1174 void NotificationPreferencesDatabase::ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const
1175 {
1176     ANS_LOGD("ParseSlotSound slot sound is %{public}s.", value.c_str());
1177     std::string slotUri = value;
1178     Uri uri(slotUri);
1179     slot->SetSound(uri);
1180 }
1181 
ParseSlotVibrationSytle(sptr<NotificationSlot> & slot,const std::string & value) const1182 void NotificationPreferencesDatabase::ParseSlotVibrationSytle(
1183     sptr<NotificationSlot> &slot, const std::string &value) const
1184 {
1185     ANS_LOGD("ParseSlotVibrationSytle slot vibration style is %{public}s.", value.c_str());
1186     std::vector<int64_t> vibrationStyle;
1187     StringToVector(value, vibrationStyle);
1188     slot->SetVibrationStyle(vibrationStyle);
1189 }
1190 
ParseSlotEnableBypassDnd(sptr<NotificationSlot> & slot,const std::string & value) const1191 void NotificationPreferencesDatabase::ParseSlotEnableBypassDnd(
1192     sptr<NotificationSlot> &slot, const std::string &value) const
1193 {
1194     ANS_LOGD("ParseSlotEnableBypassDnd slot by pass dnd is %{public}s.", value.c_str());
1195     bool enable = static_cast<bool>(StringToInt(value));
1196     slot->EnableBypassDnd(enable);
1197 }
1198 
ParseSlotEnabled(sptr<NotificationSlot> & slot,const std::string & value) const1199 void NotificationPreferencesDatabase::ParseSlotEnabled(
1200     sptr<NotificationSlot> &slot, const std::string &value) const
1201 {
1202     ANS_LOGD("ParseSlotEnabled slot enabled is %{public}s.", value.c_str());
1203     bool enabled = static_cast<bool>(StringToInt(value));
1204     slot->SetEnable(enabled);
1205 }
1206 
GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo & bundleInfo) const1207 std::string NotificationPreferencesDatabase::GenerateBundleLablel(
1208     const NotificationPreferencesInfo::BundleInfo &bundleInfo) const
1209 {
1210     return bundleInfo.GetBundleName().append(std::to_string(bundleInfo.GetBundleUid()));
1211 }
1212 
GetDoNotDisturbType(NotificationPreferencesInfo & info,int32_t userId)1213 void NotificationPreferencesDatabase::GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId)
1214 {
1215     std::string key =
1216         std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1217     GetValueFromDisturbeDB(
1218         key, [&](const int32_t &status, std::string &value) {
1219             sptr<NotificationDoNotDisturbDate> disturbDate =
1220                         new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1221             info.GetDoNotDisturbDate(userId, disturbDate);
1222             if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1223                 PutDoNotDisturbDate(userId, disturbDate);
1224             } else if (status == NativeRdb::E_OK) {
1225                 if (!value.empty()) {
1226                     if (disturbDate != nullptr) {
1227                         disturbDate->SetDoNotDisturbType(
1228                             (NotificationConstant::DoNotDisturbType)StringToInt(value));
1229                     }
1230                 }
1231             } else {
1232                 ANS_LOGW("Parse disturbe mode failed, use default value.");
1233             }
1234             info.SetDoNotDisturbDate(userId, disturbDate);
1235         });
1236 }
1237 
GetDoNotDisturbBeginDate(NotificationPreferencesInfo & info,int32_t userId)1238 void NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId)
1239 {
1240     std::string key =
1241         std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1242     GetValueFromDisturbeDB(
1243         key, [&](const int32_t &status, std::string &value) {
1244             sptr<NotificationDoNotDisturbDate> disturbDate =
1245                         new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1246             info.GetDoNotDisturbDate(userId, disturbDate);
1247             if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1248                 PutDoNotDisturbDate(userId, disturbDate);
1249             } else if (status == NativeRdb::E_OK) {
1250                 if (!value.empty()) {
1251                     if (disturbDate != nullptr) {
1252                         disturbDate->SetBeginDate(StringToInt64(value));
1253                     }
1254                 }
1255             } else {
1256                 ANS_LOGW("Parse disturbe start time failed, use default value.");
1257             }
1258             info.SetDoNotDisturbDate(userId, disturbDate);
1259         });
1260 }
1261 
GetDoNotDisturbEndDate(NotificationPreferencesInfo & info,int32_t userId)1262 void NotificationPreferencesDatabase::GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId)
1263 {
1264     std::string key =
1265         std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1266     GetValueFromDisturbeDB(
1267         key, [&](const int32_t &status, std::string &value) {
1268             sptr<NotificationDoNotDisturbDate> disturbDate =
1269                         new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1270             info.GetDoNotDisturbDate(userId, disturbDate);
1271             if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1272                 PutDoNotDisturbDate(userId, disturbDate);
1273             } else if (status == NativeRdb::E_OK) {
1274                 if (!value.empty()) {
1275                     if (disturbDate != nullptr) {
1276                         disturbDate->SetEndDate(StringToInt64(value));
1277                     }
1278                 }
1279             } else {
1280                 ANS_LOGW("Parse disturbe end time failed, use default value.");
1281             }
1282             info.SetDoNotDisturbDate(userId, disturbDate);
1283         });
1284 }
1285 
GetEnableAllNotification(NotificationPreferencesInfo & info,int32_t userId)1286 void NotificationPreferencesDatabase::GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId)
1287 {
1288     std::string key =
1289         std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
1290     GetValueFromDisturbeDB(
1291         key, [&](const int32_t &status, std::string &value) {
1292             if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1293                 bool enable = true;
1294                 if (!info.GetEnabledAllNotification(userId, enable)) {
1295                     info.SetEnabledAllNotification(userId, enable);
1296                     ANS_LOGW("Enable setting not found, default true.");
1297                 }
1298                 PutNotificationsEnabled(userId, enable);
1299             } else if (status == NativeRdb::E_OK) {
1300                 if (!value.empty()) {
1301                     info.SetEnabledAllNotification(userId, static_cast<bool>(StringToInt(value)));
1302                 }
1303             } else {
1304                 ANS_LOGW("Parse enable all notification failed, use default value.");
1305             }
1306         });
1307 }
1308 
RemoveNotificationEnable(const int32_t userId)1309 bool NotificationPreferencesDatabase::RemoveNotificationEnable(const int32_t userId)
1310 {
1311     ANS_LOGD("%{public}s", __FUNCTION__);
1312     if (!CheckRdbStore()) {
1313         ANS_LOGE("RdbStore is nullptr.");
1314         return false;
1315     }
1316 
1317     std::string key =
1318         std::string(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
1319     int32_t result = rdbDataManager_->DeleteData(key);
1320     if (result != NativeRdb::E_OK) {
1321         ANS_LOGE("delete bundle Info failed.");
1322         return false;
1323     }
1324 
1325     ANS_LOGD("%{public}s remove notification enable, userId : %{public}d", __FUNCTION__, userId);
1326     return true;
1327 }
1328 
RemoveDoNotDisturbDate(const int32_t userId)1329 bool NotificationPreferencesDatabase::RemoveDoNotDisturbDate(const int32_t userId)
1330 {
1331     ANS_LOGD("%{public}s", __FUNCTION__);
1332     if (!CheckRdbStore()) {
1333         ANS_LOGE("RdbStore is nullptr.");
1334         return false;
1335     }
1336 
1337     std::string typeKey =
1338         std::string(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1339     std::string beginDateKey =
1340         std::string(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1341     std::string endDateKey =
1342         std::string(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1343 
1344     std::vector<std::string> keys = {
1345         typeKey,
1346         beginDateKey,
1347         endDateKey
1348     };
1349 
1350     int32_t result = rdbDataManager_->DeleteBathchData(keys);
1351     if (result != NativeRdb::E_OK) {
1352         ANS_LOGE("delete DoNotDisturb date failed.");
1353         return false;
1354     }
1355 
1356     ANS_LOGD("%{public}s remove DoNotDisturb date, userId : %{public}d", __FUNCTION__, userId);
1357     return true;
1358 }
1359 
RemoveAnsBundleDbInfo(std::string bundleName,int32_t uid)1360 bool NotificationPreferencesDatabase::RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid)
1361 {
1362     if (!CheckRdbStore()) {
1363         ANS_LOGE("RdbStore is nullptr.");
1364         return false;
1365     }
1366 
1367     std::string key = KEY_BUNDLE_LABEL + bundleName + std::to_string(uid);
1368     int32_t result = rdbDataManager_->DeleteData(key);
1369     if (result != NativeRdb::E_OK) {
1370         ANS_LOGE("Delete ans bundle db info failed, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid);
1371         return false;
1372     }
1373 
1374     ANS_LOGE("Remove ans bundle db info, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid);
1375     return true;
1376 }
1377 }  // namespace Notification
1378 }  // namespace OHOS