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