• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_adapter.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  * Indicates whether the type of bundle is flags.
173  */
174 const static std::string KEY_BUNDLE_SLOTFLGS_TYPE = "bundleReminderFlagsType";
175 
176 /**
177  * Indicates whether the type of slot is flags.
178  */
179 const static std::string KEY_SLOT_SLOTFLGS_TYPE = "reminderFlagsType";
180 
181 const std::map<std::string,
182     std::function<void(NotificationPreferencesDatabase *, sptr<NotificationSlot> &, std::string &)>>
183     NotificationPreferencesDatabase::slotMap_ = {
184         {
185             KEY_SLOT_DESCRIPTION,
186             std::bind(&NotificationPreferencesDatabase::ParseSlotDescription, std::placeholders::_1,
187                 std::placeholders::_2, std::placeholders::_3),
188         },
189         {
190             KEY_SLOT_LEVEL,
191             std::bind(&NotificationPreferencesDatabase::ParseSlotLevel, std::placeholders::_1, std::placeholders::_2,
192                 std::placeholders::_3),
193         },
194         {
195             KEY_SLOT_SHOW_BADGE,
196             std::bind(&NotificationPreferencesDatabase::ParseSlotShowBadge, std::placeholders::_1,
197                 std::placeholders::_2, std::placeholders::_3),
198         },
199         {
200             KEY_SLOT_ENABLE_LIGHT,
201             std::bind(&NotificationPreferencesDatabase::ParseSlotEnableLight, std::placeholders::_1,
202                 std::placeholders::_2, std::placeholders::_3),
203         },
204         {
205             KEY_SLOT_ENABLE_VRBRATION,
206             std::bind(&NotificationPreferencesDatabase::ParseSlotEnableVrbration, std::placeholders::_1,
207                 std::placeholders::_2, std::placeholders::_3),
208         },
209         {
210             KEY_SLOT_LED_LIGHT_COLOR,
211             std::bind(&NotificationPreferencesDatabase::ParseSlotLedLightColor, std::placeholders::_1,
212                 std::placeholders::_2, std::placeholders::_3),
213         },
214         {
215             KEY_SLOT_LOCKSCREEN_VISIBLENESS,
216             std::bind(&NotificationPreferencesDatabase::ParseSlotLockscreenVisibleness, std::placeholders::_1,
217                 std::placeholders::_2, std::placeholders::_3),
218         },
219         {
220             KEY_SLOT_SOUND,
221             std::bind(&NotificationPreferencesDatabase::ParseSlotSound, std::placeholders::_1, std::placeholders::_2,
222                 std::placeholders::_3),
223         },
224         {
225             KEY_SLOT_VIBRATION_STYLE,
226             std::bind(&NotificationPreferencesDatabase::ParseSlotVibrationSytle, std::placeholders::_1,
227                 std::placeholders::_2, std::placeholders::_3),
228         },
229         {
230             KEY_SLOT_ENABLE_BYPASS_DND,
231             std::bind(&NotificationPreferencesDatabase::ParseSlotEnableBypassDnd, std::placeholders::_1,
232                 std::placeholders::_2, std::placeholders::_3),
233         },
234         {
235             KEY_SLOT_ENABLED,
236             std::bind(&NotificationPreferencesDatabase::ParseSlotEnabled, std::placeholders::_1,
237                 std::placeholders::_2, std::placeholders::_3),
238         },
239         {
240             KEY_SLOT_SLOTFLGS_TYPE,
241             std::bind(&NotificationPreferencesDatabase::ParseSlotFlags, std::placeholders::_1,
242                 std::placeholders::_2, std::placeholders::_3),
243         },
244 };
245 
246 const std::map<std::string,
247     std::function<void(NotificationPreferencesDatabase *, NotificationPreferencesInfo::BundleInfo &, std::string &)>>
248     NotificationPreferencesDatabase::bundleMap_ = {
249         {
250             KEY_BUNDLE_NAME,
251             std::bind(&NotificationPreferencesDatabase::ParseBundleName, std::placeholders::_1, std::placeholders::_2,
252                 std::placeholders::_3),
253         },
254         {
255             KEY_BUNDLE_IMPORTANCE,
256             std::bind(&NotificationPreferencesDatabase::ParseBundleImportance, std::placeholders::_1,
257                 std::placeholders::_2, std::placeholders::_3),
258         },
259         {
260             KEY_BUNDLE_SHOW_BADGE,
261             std::bind(&NotificationPreferencesDatabase::ParseBundleShowBadge, std::placeholders::_1,
262                 std::placeholders::_2, std::placeholders::_3),
263         },
264         {
265             KEY_BUNDLE_BADGE_TOTAL_NUM,
266             std::bind(&NotificationPreferencesDatabase::ParseBundleBadgeNum, std::placeholders::_1,
267                 std::placeholders::_2, std::placeholders::_3),
268         },
269         {
270             KEY_BUNDLE_ENABLE_NOTIFICATION,
271             std::bind(&NotificationPreferencesDatabase::ParseBundleEnableNotification, std::placeholders::_1,
272                 std::placeholders::_2, std::placeholders::_3),
273         },
274         {
275             KEY_BUNDLE_POPPED_DIALOG,
276             std::bind(&NotificationPreferencesDatabase::ParseBundlePoppedDialog, std::placeholders::_1,
277                 std::placeholders::_2, std::placeholders::_3),
278         },
279         {
280             KEY_BUNDLE_UID,
281             std::bind(&NotificationPreferencesDatabase::ParseBundleUid, std::placeholders::_1, std::placeholders::_2,
282                 std::placeholders::_3),
283         },
284         {
285             KEY_BUNDLE_SLOTFLGS_TYPE,
286             std::bind(&NotificationPreferencesDatabase::ParseBundleSlotFlags, std::placeholders::_1,
287                 std::placeholders::_2, std::placeholders::_3),
288         },
289 };
290 
NotificationPreferencesDatabase()291 NotificationPreferencesDatabase::NotificationPreferencesDatabase()
292 {
293     NotificationRdbConfig notificationRdbConfig;
294     rdbDataManager_ = std::make_shared<NotificationDataMgr>(notificationRdbConfig);
295     ANS_LOGD("Notification Rdb is created");
296 }
297 
~NotificationPreferencesDatabase()298 NotificationPreferencesDatabase::~NotificationPreferencesDatabase()
299 {
300     ANS_LOGD("Notification Rdb is deleted");
301 }
302 
CheckRdbStore()303 bool NotificationPreferencesDatabase::CheckRdbStore()
304 {
305     if (rdbDataManager_ != nullptr) {
306         int32_t result = rdbDataManager_->Init();
307         if (result == NativeRdb::E_OK) {
308             return true;
309         }
310     }
311 
312     return false;
313 }
314 
PutSlotsToDisturbeDB(const std::string & bundleName,const int32_t & bundleUid,const std::vector<sptr<NotificationSlot>> & slots)315 bool NotificationPreferencesDatabase::PutSlotsToDisturbeDB(
316     const std::string &bundleName, const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots)
317 {
318     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
319     ANS_LOGD("%{public}s", __FUNCTION__);
320     if (bundleName.empty()) {
321         ANS_LOGE("Bundle name is null.");
322         return false;
323     }
324 
325     if (slots.empty()) {
326         ANS_LOGE("Slot is empty.");
327         return false;
328     }
329 
330     std::unordered_map<std::string, std::string> values;
331     for (auto iter : slots) {
332         bool result = SlotToEntry(bundleName, bundleUid, iter, values);
333         if (!result) {
334             return result;
335         }
336     }
337 
338     if (!CheckRdbStore()) {
339         ANS_LOGE("RdbStore is nullptr.");
340         return false;
341     }
342     int32_t result = rdbDataManager_->InsertBatchData(values);
343     return (result == NativeRdb::E_OK);
344 }
345 
PutBundlePropertyToDisturbeDB(const NotificationPreferencesInfo::BundleInfo & bundleInfo)346 bool NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB(
347     const NotificationPreferencesInfo::BundleInfo &bundleInfo)
348 {
349     if (bundleInfo.GetBundleName().empty()) {
350         ANS_LOGE("Bundle name is null.");
351         return false;
352     }
353 
354     if (!CheckRdbStore()) {
355         ANS_LOGE("RdbStore is nullptr.");
356         return false;
357     }
358     std::string values;
359     std::string bundleKeyStr = KEY_BUNDLE_LABEL + GenerateBundleLablel(bundleInfo);
360     bool result = false;
361     GetValueFromDisturbeDB(bundleKeyStr, [&](const int32_t &status, std::string &value) {
362         switch (status) {
363             case NativeRdb::E_EMPTY_VALUES_BUCKET: {
364                 result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo);
365                 break;
366             }
367             case NativeRdb::E_OK: {
368                 ANS_LOGE("Current bundle has exsited.");
369                 break;
370             }
371             default:
372                 break;
373         }
374     });
375     return result;
376 }
377 
PutShowBadge(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & enable)378 bool NotificationPreferencesDatabase::PutShowBadge(
379     const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable)
380 {
381     if (bundleInfo.GetBundleName().empty()) {
382         ANS_LOGE("Bundle name is nullptr.");
383         return false;
384     }
385 
386     if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
387         return false;
388     }
389 
390     std::string bundleKey = GenerateBundleLablel(bundleInfo);
391     int32_t result =
392         PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable);
393     return (result == NativeRdb::E_OK);
394 }
395 
PutImportance(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const int32_t & importance)396 bool NotificationPreferencesDatabase::PutImportance(
397     const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &importance)
398 {
399     if (bundleInfo.GetBundleName().empty()) {
400         ANS_LOGE("Bundle name is empty.");
401         return false;
402     }
403 
404     if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
405         return false;
406     }
407 
408     std::string bundleKey = GenerateBundleLablel(bundleInfo);
409     int32_t result =
410         PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_IMPORTANCE_TYPE, importance);
411     return (result == NativeRdb::E_OK);
412 }
413 
PutTotalBadgeNums(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const int32_t & totalBadgeNum)414 bool NotificationPreferencesDatabase::PutTotalBadgeNums(
415     const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &totalBadgeNum)
416 {
417     if (bundleInfo.GetBundleName().empty()) {
418         ANS_LOGE("Bundle name is blank.");
419         return false;
420     }
421 
422     if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
423         return false;
424     }
425     std::string bundleKey = GenerateBundleLablel(bundleInfo);
426     int32_t result =
427         PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum);
428     return (result == NativeRdb::E_OK);
429 }
430 
PutNotificationsEnabledForBundle(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & enabled)431 bool NotificationPreferencesDatabase::PutNotificationsEnabledForBundle(
432     const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled)
433 {
434     ANS_LOGD("%{public}s, enabled[%{public}d]", __FUNCTION__, enabled);
435     if (bundleInfo.GetBundleName().empty()) {
436         ANS_LOGE("Bundle name is null.");
437         return false;
438     }
439 
440     if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
441         return false;
442     }
443 
444     std::string bundleKey = GenerateBundleLablel(bundleInfo);
445     int32_t result =
446         PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled);
447     return (result == NativeRdb::E_OK);
448 }
449 
PutNotificationsEnabled(const int32_t & userId,const bool & enabled)450 bool NotificationPreferencesDatabase::PutNotificationsEnabled(const int32_t &userId, const bool &enabled)
451 {
452     if (!CheckRdbStore()) {
453         ANS_LOGE("RdbStore is nullptr.");
454         return false;
455     }
456 
457     std::string typeKey =
458         std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
459     std::string enableValue = std::to_string(enabled);
460     int32_t result = rdbDataManager_->InsertData(typeKey, enableValue);
461     if (result != NativeRdb::E_OK) {
462         ANS_LOGE("Store enable notification failed. %{public}d", result);
463         return false;
464     }
465     return true;
466 }
467 
PutSlotFlags(NotificationPreferencesInfo::BundleInfo & bundleInfo,const int32_t & slotFlags)468 bool NotificationPreferencesDatabase::PutSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo,
469     const int32_t &slotFlags)
470 {
471     if (!CheckRdbStore()) {
472         ANS_LOGE("RdbStore is nullptr.");
473         return false;
474     }
475 
476     std::string bundleKey = GenerateBundleLablel(bundleInfo);
477     int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_SLOTFLGS_TYPE, slotFlags);
478     return (result == NativeRdb::E_OK);
479 }
480 
PutHasPoppedDialog(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & hasPopped)481 bool NotificationPreferencesDatabase::PutHasPoppedDialog(
482     const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped)
483 {
484     if (bundleInfo.GetBundleName().empty()) {
485         ANS_LOGE("Bundle name is null.");
486         return false;
487     }
488 
489     if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
490         return false;
491     }
492 
493     std::string bundleKey = GenerateBundleLablel(bundleInfo);
494     int32_t result =
495         PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
496     return (result == NativeRdb::E_OK);
497 }
498 
PutDoNotDisturbDate(const int32_t & userId,const sptr<NotificationDoNotDisturbDate> & date)499 bool NotificationPreferencesDatabase::PutDoNotDisturbDate(
500     const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date)
501 {
502     if (date == nullptr) {
503         ANS_LOGE("Invalid date.");
504         return false;
505     }
506 
507     if (!CheckRdbStore()) {
508         ANS_LOGE("RdbStore is nullptr.");
509         return false;
510     }
511 
512     std::string typeKey =
513         std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
514     std::string typeValue = std::to_string((int)date->GetDoNotDisturbType());
515 
516     std::string beginDateKey =
517         std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
518     std::string beginDateValue = std::to_string(date->GetBeginDate());
519 
520     std::string endDateKey =
521         std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
522     std::string endDateValue = std::to_string(date->GetEndDate());
523 
524     std::unordered_map<std::string, std::string> values = {
525         {typeKey, typeValue},
526         {beginDateKey, beginDateValue},
527         {endDateKey, endDateValue},
528     };
529 
530     int32_t result = rdbDataManager_->InsertBatchData(values);
531     if (result != NativeRdb::E_OK) {
532         ANS_LOGE("Store DoNotDisturbDate failed. %{public}d", result);
533         return false;
534     }
535 
536     return true;
537 }
538 
GetValueFromDisturbeDB(const std::string & key,std::function<void (std::string &)> callback)539 void NotificationPreferencesDatabase::GetValueFromDisturbeDB(
540     const std::string &key, std::function<void(std::string &)> callback)
541 {
542     if (!CheckRdbStore()) {
543         ANS_LOGE("RdbStore is nullptr.");
544         return;
545     }
546     std::string value;
547     int32_t result = rdbDataManager_->QueryData(key, value);
548     if (result == NativeRdb::E_ERROR) {
549         ANS_LOGE("Get value failed, use default value. error code is %{public}d", result);
550         return;
551     }
552     callback(value);
553 }
554 
GetValueFromDisturbeDB(const std::string & key,std::function<void (int32_t &,std::string &)> callback)555 void NotificationPreferencesDatabase::GetValueFromDisturbeDB(
556     const std::string &key, std::function<void(int32_t &, std::string &)> callback)
557 {
558     if (!CheckRdbStore()) {
559         ANS_LOGE("RdbStore is nullptr.");
560         return;
561     }
562     std::string value;
563     int32_t result = rdbDataManager_->QueryData(key, value);
564     callback(result, value);
565 }
566 
CheckBundle(const std::string & bundleName,const int32_t & bundleUid)567 bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleName, const int32_t &bundleUid)
568 {
569     std::string bundleKeyStr = KEY_BUNDLE_LABEL + bundleName + std::to_string(bundleUid);
570     ANS_LOGD("CheckBundle bundleKeyStr %{public}s", bundleKeyStr.c_str());
571     bool result = true;
572     GetValueFromDisturbeDB(bundleKeyStr, [&](const int32_t &status, std::string &value) {
573         switch (status) {
574             case NativeRdb::E_EMPTY_VALUES_BUCKET: {
575                 NotificationPreferencesInfo::BundleInfo bundleInfo;
576                 bundleInfo.SetBundleName(bundleName);
577                 bundleInfo.SetBundleUid(bundleUid);
578                 result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo);
579                 break;
580             }
581             case NativeRdb::E_OK: {
582                 result = true;
583                 break;
584             }
585             default:
586                 result = false;
587                 break;
588         }
589     });
590     return result;
591 }
592 
PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo & bundleInfo)593 bool NotificationPreferencesDatabase::PutBundlePropertyValueToDisturbeDB(
594     const NotificationPreferencesInfo::BundleInfo &bundleInfo)
595 {
596     std::unordered_map<std::string, std::string> values;
597     std::string bundleKey = bundleInfo.GetBundleName().append(std::to_string(bundleInfo.GetBundleUid()));
598     GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_NAME), bundleInfo.GetBundleName(), values);
599     GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_BADGE_TOTAL_NUM),
600         std::to_string(bundleInfo.GetBadgeTotalNum()),
601         values);
602     GenerateEntry(
603         GenerateBundleKey(bundleKey, KEY_BUNDLE_IMPORTANCE), std::to_string(bundleInfo.GetImportance()), values);
604     GenerateEntry(
605         GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE), std::to_string(bundleInfo.GetIsShowBadge()), values);
606     GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION),
607         std::to_string(bundleInfo.GetEnableNotification()),
608         values);
609     GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_POPPED_DIALOG),
610         std::to_string(bundleInfo.GetHasPoppedDialog()),
611         values);
612     GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_UID), std::to_string(bundleInfo.GetBundleUid()), values);
613     if (!CheckRdbStore()) {
614         ANS_LOGE("RdbStore is nullptr.");
615         return false;
616     }
617     int32_t result = rdbDataManager_->InsertBatchData(values);
618     if (result != NativeRdb::E_OK) {
619         ANS_LOGE("Store bundle failed. %{public}d", result);
620         return false;
621     }
622     return true;
623 }
624 
ParseFromDisturbeDB(NotificationPreferencesInfo & info)625 bool NotificationPreferencesDatabase::ParseFromDisturbeDB(NotificationPreferencesInfo &info)
626 {
627     ANS_LOGD("%{public}s", __FUNCTION__);
628     ParseDoNotDisturbType(info);
629     ParseDoNotDisturbBeginDate(info);
630     ParseDoNotDisturbEndDate(info);
631     ParseEnableAllNotification(info);
632 
633     if (!CheckRdbStore()) {
634         ANS_LOGE("RdbStore is nullptr.");
635         return false;
636     }
637     std::unordered_map<std::string, std::string> values;
638     int32_t result = rdbDataManager_->QueryDataBeginWithKey(KEY_BUNDLE_LABEL, values);
639     if (result == NativeRdb::E_ERROR) {
640         ANS_LOGE("Get Bundle Info failed.");
641         return false;
642     }
643     ParseBundleFromDistureDB(info, values);
644     return true;
645 }
646 
RemoveAllDataFromDisturbeDB()647 bool NotificationPreferencesDatabase::RemoveAllDataFromDisturbeDB()
648 {
649     ANS_LOGD("%{public}s", __FUNCTION__);
650     if (!CheckRdbStore()) {
651         ANS_LOGE("RdbStore is nullptr.");
652         return false;
653     }
654     int32_t result = rdbDataManager_->Destroy();
655     return (result == NativeRdb::E_OK);
656 }
657 
RemoveBundleFromDisturbeDB(const std::string & bundleKey)658 bool NotificationPreferencesDatabase::RemoveBundleFromDisturbeDB(const std::string &bundleKey)
659 {
660     ANS_LOGD("%{public}s", __FUNCTION__);
661     if (!CheckRdbStore()) {
662         ANS_LOGE("RdbStore is nullptr.");
663         return false;
664     }
665 
666     std::unordered_map<std::string, std::string> values;
667     int32_t result = rdbDataManager_->QueryDataBeginWithKey(
668         (KEY_ANS_BUNDLE + KEY_UNDER_LINE + bundleKey + KEY_UNDER_LINE), values);
669 
670     if (result == NativeRdb::E_ERROR) {
671         ANS_LOGE("Get Bundle Info failed.");
672         return false;
673     }
674 
675     std::vector<std::string> keys;
676     for (auto iter : values) {
677         keys.push_back(iter.first);
678     }
679 
680     std::string bundleDBKey = KEY_BUNDLE_LABEL + KEY_BUNDLE_NAME + KEY_UNDER_LINE + bundleKey;
681     keys.push_back(bundleDBKey);
682     result = rdbDataManager_->DeleteBathchData(keys);
683     if (result != NativeRdb::E_OK) {
684         ANS_LOGE("delete bundle Info failed.");
685         return false;
686     }
687     return true;
688 }
689 
RemoveSlotFromDisturbeDB(const std::string & bundleKey,const NotificationConstant::SlotType & type)690 bool NotificationPreferencesDatabase::RemoveSlotFromDisturbeDB(
691     const std::string &bundleKey, const NotificationConstant::SlotType &type)
692 {
693     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
694     ANS_LOGD("%{public}s", __FUNCTION__);
695     if (bundleKey.empty()) {
696         ANS_LOGE("Bundle name is empty.");
697         return false;
698     }
699 
700     if (!CheckRdbStore()) {
701         ANS_LOGE("RdbStore is nullptr.");
702         return false;
703     }
704 
705     std::unordered_map<std::string, std::string> values;
706     std::string slotType = std::to_string(type);
707     int32_t result = rdbDataManager_->QueryDataBeginWithKey(
708         (GenerateSlotKey(bundleKey, slotType) + KEY_UNDER_LINE), values);
709     if (result == NativeRdb::E_ERROR) {
710         return false;
711     }
712     std::vector<std::string> keys;
713     for (auto iter : values) {
714         keys.push_back(iter.first);
715     }
716 
717     result = rdbDataManager_->DeleteBathchData(keys);
718     if (result != NativeRdb::E_OK) {
719         ANS_LOGE("delete bundle Info failed.");
720         return false;
721     }
722 
723     return true;
724 }
725 
RemoveAllSlotsFromDisturbeDB(const std::string & bundleKey)726 bool NotificationPreferencesDatabase::RemoveAllSlotsFromDisturbeDB(const std::string &bundleKey)
727 {
728     ANS_LOGD("%{public}s", __FUNCTION__);
729     if (bundleKey.empty()) {
730         ANS_LOGE("Bundle name is null.");
731         return false;
732     }
733 
734     if (!CheckRdbStore()) {
735         ANS_LOGE("RdbStore is nullptr.");
736         return false;
737     }
738 
739     std::unordered_map<std::string, std::string> values;
740     int32_t result = rdbDataManager_->QueryDataBeginWithKey(
741         (GenerateSlotKey(bundleKey) + KEY_UNDER_LINE), values);
742     if (result == NativeRdb::E_ERROR) {
743         return false;
744     }
745     std::vector<std::string> keys;
746     for (auto iter : values) {
747         keys.push_back(iter.first);
748     }
749 
750     result = rdbDataManager_->DeleteBathchData(keys);
751     return (result == NativeRdb::E_OK);
752 }
753 
754 template <typename T>
PutBundlePropertyToDisturbeDB(const std::string & bundleKey,const BundleType & type,const T & t)755 int32_t NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB(
756     const std::string &bundleKey, const BundleType &type, const T &t)
757 {
758     std::string keyStr;
759     switch (type) {
760         case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
761             keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_BADGE_TOTAL_NUM);
762             break;
763         case BundleType::BUNDLE_IMPORTANCE_TYPE:
764             keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_IMPORTANCE);
765             break;
766         case BundleType::BUNDLE_SHOW_BADGE_TYPE:
767             keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE);
768             break;
769         case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
770             keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION);
771             break;
772         case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
773             ANS_LOGD("Into BUNDLE_POPPED_DIALOG_TYPE:GenerateBundleKey.");
774             keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_POPPED_DIALOG);
775             break;
776         case BundleType::BUNDLE_SLOTFLGS_TYPE:
777             ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:GenerateBundleKey.");
778             keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SLOTFLGS_TYPE);
779             break;
780         default:
781             break;
782     }
783     if (!CheckRdbStore()) {
784         ANS_LOGE("RdbStore is nullptr.");
785         return false;
786     }
787     std::string valueStr = std::to_string(t);
788     int32_t result = rdbDataManager_->InsertData(keyStr, valueStr);
789     return result;
790 }
791 
PutBundleToDisturbeDB(const std::string & bundleKey,const NotificationPreferencesInfo::BundleInfo & bundleInfo)792 bool NotificationPreferencesDatabase::PutBundleToDisturbeDB(
793     const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo)
794 {
795     if (!CheckRdbStore()) {
796         ANS_LOGE("RdbStore is nullptr.");
797         return false;
798     }
799 
800     ANS_LOGD("Key not fund, so create a bundle, bundle key is %{public}s.", bundleKey.c_str());
801     int32_t result = rdbDataManager_->InsertData(bundleKey, GenerateBundleLablel(bundleInfo));
802     if (result != NativeRdb::E_OK) {
803         ANS_LOGE("Store bundle name to db is failed.");
804         return false;
805     }
806 
807     if (!PutBundlePropertyValueToDisturbeDB(bundleInfo)) {
808         return false;
809     }
810     return true;
811 }
812 
GenerateEntry(const std::string & key,const std::string & value,std::unordered_map<std::string,std::string> & values) const813 void NotificationPreferencesDatabase::GenerateEntry(
814     const std::string &key, const std::string &value, std::unordered_map<std::string, std::string> &values) const
815 {
816     values.emplace(key, value);
817 }
818 
SlotToEntry(const std::string & bundleName,const int32_t & bundleUid,const sptr<NotificationSlot> & slot,std::unordered_map<std::string,std::string> & values)819 bool NotificationPreferencesDatabase::SlotToEntry(const std::string &bundleName, const int32_t &bundleUid,
820     const sptr<NotificationSlot> &slot, std::unordered_map<std::string, std::string> &values)
821 {
822     if (slot == nullptr) {
823         ANS_LOGE("Notification slot is nullptr.");
824         return false;
825     }
826 
827     if (!CheckBundle(bundleName, bundleUid)) {
828         return false;
829     }
830 
831     std::string bundleKey = bundleName + std::to_string(bundleUid);
832     GenerateSlotEntry(bundleKey, slot, values);
833     return true;
834 }
835 
GenerateSlotEntry(const std::string & bundleKey,const sptr<NotificationSlot> & slot,std::unordered_map<std::string,std::string> & values) const836 void NotificationPreferencesDatabase::GenerateSlotEntry(const std::string &bundleKey,
837     const sptr<NotificationSlot> &slot, std::unordered_map<std::string, std::string> &values) const
838 {
839     std::string slotType = std::to_string(slot->GetType());
840     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_TYPE), std::to_string(slot->GetType()), values);
841     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ID), slot->GetId(), values);
842     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_NAME), slot->GetName(), values);
843     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_DESCRIPTION), slot->GetDescription(), values);
844     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LEVEL), std::to_string(slot->GetLevel()), values);
845     GenerateEntry(
846         GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SHOW_BADGE), std::to_string(slot->IsShowBadge()), values);
847     GenerateEntry(
848         GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_LIGHT), std::to_string(slot->CanEnableLight()), values);
849     GenerateEntry(
850         GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_VRBRATION), std::to_string(slot->CanVibrate()), values);
851     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LED_LIGHT_COLOR),
852         std::to_string(slot->GetLedLightColor()), values);
853     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LOCKSCREEN_VISIBLENESS),
854         std::to_string(static_cast<int>(slot->GetLockScreenVisibleness())), values);
855     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SOUND), slot->GetSound().ToString(), values);
856     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_BYPASS_DND),
857         std::to_string(slot->IsEnableBypassDnd()), values);
858     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_VIBRATION_STYLE),
859         VectorToString(slot->GetVibrationStyle()), values);
860     GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLED), std::to_string(slot->GetEnable()), values);
861 }
862 
ParseBundleFromDistureDB(NotificationPreferencesInfo & info,const std::unordered_map<std::string,std::string> & values)863 void NotificationPreferencesDatabase::ParseBundleFromDistureDB(
864     NotificationPreferencesInfo &info, const std::unordered_map<std::string, std::string> &values)
865 {
866     if (!CheckRdbStore()) {
867         ANS_LOGE("RdbStore is nullptr.");
868         return;
869     }
870     for (auto item : values) {
871         std::string bundleKey = item.second;
872         ANS_LOGD("Bundle name is %{public}s.", bundleKey.c_str());
873         std::unordered_map<std::string, std::string> bundleEntries;
874         rdbDataManager_->QueryDataBeginWithKey((GenerateBundleKey(bundleKey)), bundleEntries);
875         ANS_LOGD("Bundle key is %{public}s.", GenerateBundleKey(bundleKey).c_str());
876         NotificationPreferencesInfo::BundleInfo bunldeInfo;
877         for (auto bundleEntry : bundleEntries) {
878             if (IsSlotKey(GenerateBundleKey(bundleKey), bundleEntry.first)) {
879                 ParseSlotFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry);
880             } else {
881                 ParseBundlePropertyFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry);
882             }
883         }
884 
885         info.SetBundleInfoFromDb(bunldeInfo, bundleKey);
886     }
887 }
888 
ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & bundleKey,const std::pair<std::string,std::string> & entry)889 void NotificationPreferencesDatabase::ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo,
890     const std::string &bundleKey, const std::pair<std::string, std::string> &entry)
891 {
892     std::string slotKey = entry.first;
893     std::string typeStr = SubUniqueIdentifyFromString(GenerateSlotKey(bundleKey) + KEY_UNDER_LINE, slotKey);
894     NotificationConstant::SlotType slotType = static_cast<NotificationConstant::SlotType>(StringToInt(typeStr));
895     sptr<NotificationSlot> slot = nullptr;
896     if (!bundleInfo.GetSlot(slotType, slot)) {
897         slot = new (std::nothrow) NotificationSlot(slotType);
898         if (slot == nullptr) {
899             ANS_LOGE("Failed to create NotificationSlot instance");
900             return;
901         }
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 
ParseBundleEnableNotification(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1133 void NotificationPreferencesDatabase::ParseBundleEnableNotification(
1134     NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1135 {
1136     ANS_LOGD("SetBundleEnableNotification bundle enable is %{public}s.", value.c_str());
1137     bundleInfo.SetEnableNotification(static_cast<bool>(StringToInt(value)));
1138 }
1139 
ParseBundlePoppedDialog(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1140 void NotificationPreferencesDatabase::ParseBundlePoppedDialog(
1141     NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1142 {
1143     ANS_LOGD("SetBundlePoppedDialog bundle has popped dialog is %{public}s.", value.c_str());
1144     bundleInfo.SetHasPoppedDialog(static_cast<bool>(StringToInt(value)));
1145 }
1146 
ParseBundleUid(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1147 void NotificationPreferencesDatabase::ParseBundleUid(
1148     NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1149 {
1150     ANS_LOGD("SetBundleUid uuid is %{public}s.", value.c_str());
1151     bundleInfo.SetBundleUid(StringToInt(value));
1152 }
1153 
ParseSlotDescription(sptr<NotificationSlot> & slot,const std::string & value) const1154 void NotificationPreferencesDatabase::ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const
1155 {
1156     ANS_LOGD("ParseSlotDescription slot des is %{public}s.", value.c_str());
1157     std::string slotDescription = value;
1158     slot->SetDescription(slotDescription);
1159 }
1160 
ParseSlotLevel(sptr<NotificationSlot> & slot,const std::string & value) const1161 void NotificationPreferencesDatabase::ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const
1162 {
1163     ANS_LOGD("ParseSlotLevel slot level is %{public}s.", value.c_str());
1164     NotificationSlot::NotificationLevel level = static_cast<NotificationSlot::NotificationLevel>(StringToInt(value));
1165     slot->SetLevel(level);
1166 }
1167 
ParseSlotShowBadge(sptr<NotificationSlot> & slot,const std::string & value) const1168 void NotificationPreferencesDatabase::ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const
1169 {
1170     ANS_LOGD("ParseSlotShowBadge slot show badge is %{public}s.", value.c_str());
1171     bool showBadge = static_cast<bool>(StringToInt(value));
1172     slot->EnableBadge(showBadge);
1173 }
1174 
ParseSlotFlags(sptr<NotificationSlot> & slot,const std::string & value) const1175 void NotificationPreferencesDatabase::ParseSlotFlags(sptr<NotificationSlot> &slot, const std::string &value) const
1176 {
1177     ANS_LOGD("ParseSlotFlags slot show flags is %{public}s.", value.c_str());
1178     uint32_t slotFlags = static_cast<uint32_t>(StringToInt(value));
1179     slot->SetSlotFlags(slotFlags);
1180 }
1181 
ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1182 void NotificationPreferencesDatabase::ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo,
1183     const std::string &value) const
1184 {
1185     ANS_LOGD("ParseBundleSlotFlags slot show flags is %{public}s.", value.c_str());
1186     bundleInfo.SetSlotFlags(StringToInt(value));
1187 }
1188 
ParseSlotEnableLight(sptr<NotificationSlot> & slot,const std::string & value) const1189 void NotificationPreferencesDatabase::ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const
1190 {
1191     ANS_LOGD("ParseSlotEnableLight slot enable light is %{public}s.", value.c_str());
1192     bool enableLight = static_cast<bool>(StringToInt(value));
1193     slot->SetEnableLight(enableLight);
1194 }
1195 
ParseSlotEnableVrbration(sptr<NotificationSlot> & slot,const std::string & value) const1196 void NotificationPreferencesDatabase::ParseSlotEnableVrbration(
1197     sptr<NotificationSlot> &slot, const std::string &value) const
1198 {
1199     ANS_LOGD("ParseSlotEnableVrbration slot enable vir is %{public}s.", value.c_str());
1200     bool enableVrbration = static_cast<bool>(StringToInt(value));
1201     slot->SetEnableVibration(enableVrbration);
1202 }
1203 
ParseSlotLedLightColor(sptr<NotificationSlot> & slot,const std::string & value) const1204 void NotificationPreferencesDatabase::ParseSlotLedLightColor(
1205     sptr<NotificationSlot> &slot, const std::string &value) const
1206 {
1207     ANS_LOGD("ParseSlotLedLightColor slot led is %{public}s.", value.c_str());
1208     int32_t ledLightColor = static_cast<int32_t>(StringToInt(value));
1209     slot->SetLedLightColor(ledLightColor);
1210 }
1211 
ParseSlotLockscreenVisibleness(sptr<NotificationSlot> & slot,const std::string & value) const1212 void NotificationPreferencesDatabase::ParseSlotLockscreenVisibleness(
1213     sptr<NotificationSlot> &slot, const std::string &value) const
1214 {
1215 
1216     ANS_LOGD("ParseSlotLockscreenVisibleness slot visible is %{public}s.", value.c_str());
1217     NotificationConstant::VisiblenessType visible =
1218         static_cast<NotificationConstant::VisiblenessType>(StringToInt(value));
1219     slot->SetLockscreenVisibleness(visible);
1220 }
1221 
ParseSlotSound(sptr<NotificationSlot> & slot,const std::string & value) const1222 void NotificationPreferencesDatabase::ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const
1223 {
1224     ANS_LOGD("ParseSlotSound slot sound is %{public}s.", value.c_str());
1225     std::string slotUri = value;
1226     Uri uri(slotUri);
1227     slot->SetSound(uri);
1228 }
1229 
ParseSlotVibrationSytle(sptr<NotificationSlot> & slot,const std::string & value) const1230 void NotificationPreferencesDatabase::ParseSlotVibrationSytle(
1231     sptr<NotificationSlot> &slot, const std::string &value) const
1232 {
1233     ANS_LOGD("ParseSlotVibrationSytle slot vibration style is %{public}s.", value.c_str());
1234     std::vector<int64_t> vibrationStyle;
1235     StringToVector(value, vibrationStyle);
1236     slot->SetVibrationStyle(vibrationStyle);
1237 }
1238 
ParseSlotEnableBypassDnd(sptr<NotificationSlot> & slot,const std::string & value) const1239 void NotificationPreferencesDatabase::ParseSlotEnableBypassDnd(
1240     sptr<NotificationSlot> &slot, const std::string &value) const
1241 {
1242     ANS_LOGD("ParseSlotEnableBypassDnd slot by pass dnd is %{public}s.", value.c_str());
1243     bool enable = static_cast<bool>(StringToInt(value));
1244     slot->EnableBypassDnd(enable);
1245 }
1246 
ParseSlotEnabled(sptr<NotificationSlot> & slot,const std::string & value) const1247 void NotificationPreferencesDatabase::ParseSlotEnabled(
1248     sptr<NotificationSlot> &slot, const std::string &value) const
1249 {
1250     ANS_LOGD("ParseSlotEnabled slot enabled is %{public}s.", value.c_str());
1251     bool enabled = static_cast<bool>(StringToInt(value));
1252     slot->SetEnable(enabled);
1253 }
1254 
GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo & bundleInfo) const1255 std::string NotificationPreferencesDatabase::GenerateBundleLablel(
1256     const NotificationPreferencesInfo::BundleInfo &bundleInfo) const
1257 {
1258     return bundleInfo.GetBundleName().append(std::to_string(bundleInfo.GetBundleUid()));
1259 }
1260 
GetDoNotDisturbType(NotificationPreferencesInfo & info,int32_t userId)1261 void NotificationPreferencesDatabase::GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId)
1262 {
1263     std::string key =
1264         std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1265     GetValueFromDisturbeDB(
1266         key, [&](const int32_t &status, std::string &value) {
1267             sptr<NotificationDoNotDisturbDate> disturbDate = new (std::nothrow)
1268                 NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1269             if (disturbDate == nullptr) {
1270                 ANS_LOGE("Create NotificationDoNotDisturbDate instance fail.");
1271                 return;
1272             }
1273             info.GetDoNotDisturbDate(userId, disturbDate);
1274             if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1275                 PutDoNotDisturbDate(userId, disturbDate);
1276             } else if (status == NativeRdb::E_OK) {
1277                 if (!value.empty()) {
1278                     if (disturbDate != nullptr) {
1279                         disturbDate->SetDoNotDisturbType(
1280                             (NotificationConstant::DoNotDisturbType)StringToInt(value));
1281                     }
1282                 }
1283             } else {
1284                 ANS_LOGW("Parse disturbe mode failed, use default value.");
1285             }
1286             info.SetDoNotDisturbDate(userId, disturbDate);
1287         });
1288 }
1289 
GetDoNotDisturbBeginDate(NotificationPreferencesInfo & info,int32_t userId)1290 void NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId)
1291 {
1292     std::string key =
1293         std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1294     GetValueFromDisturbeDB(
1295         key, [&](const int32_t &status, std::string &value) {
1296             sptr<NotificationDoNotDisturbDate> disturbDate = new (std::nothrow)
1297                 NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1298             if (disturbDate == nullptr) {
1299                 ANS_LOGE("Failed to create NotificationDoNotDisturbDate instance");
1300                 return;
1301             }
1302             info.GetDoNotDisturbDate(userId, disturbDate);
1303             if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1304                 PutDoNotDisturbDate(userId, disturbDate);
1305             } else if (status == NativeRdb::E_OK) {
1306                 if (!value.empty()) {
1307                     if (disturbDate != nullptr) {
1308                         disturbDate->SetBeginDate(StringToInt64(value));
1309                     }
1310                 }
1311             } else {
1312                 ANS_LOGW("Parse disturbe start time failed, use default value.");
1313             }
1314             info.SetDoNotDisturbDate(userId, disturbDate);
1315         });
1316 }
1317 
GetDoNotDisturbEndDate(NotificationPreferencesInfo & info,int32_t userId)1318 void NotificationPreferencesDatabase::GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId)
1319 {
1320     std::string key =
1321         std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1322     GetValueFromDisturbeDB(
1323         key, [&](const int32_t &status, std::string &value) {
1324             sptr<NotificationDoNotDisturbDate> disturbDate = new (std::nothrow)
1325                 NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1326             if (disturbDate == nullptr) {
1327                 ANS_LOGE("Defeat to create NotificationDoNotDisturbDate instance");
1328                 return;
1329             }
1330             info.GetDoNotDisturbDate(userId, disturbDate);
1331             if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1332                 PutDoNotDisturbDate(userId, disturbDate);
1333             } else if (status == NativeRdb::E_OK) {
1334                 if (!value.empty()) {
1335                     if (disturbDate != nullptr) {
1336                         disturbDate->SetEndDate(StringToInt64(value));
1337                     }
1338                 }
1339             } else {
1340                 ANS_LOGW("Parse disturbe end time failed, use default value.");
1341             }
1342             info.SetDoNotDisturbDate(userId, disturbDate);
1343         });
1344 }
1345 
GetEnableAllNotification(NotificationPreferencesInfo & info,int32_t userId)1346 void NotificationPreferencesDatabase::GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId)
1347 {
1348     std::string key =
1349         std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
1350     GetValueFromDisturbeDB(
1351         key, [&](const int32_t &status, std::string &value) {
1352             if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1353                 bool enable = true;
1354                 if (!info.GetEnabledAllNotification(userId, enable)) {
1355                     info.SetEnabledAllNotification(userId, enable);
1356                     ANS_LOGW("Enable setting not found, default true.");
1357                 }
1358                 PutNotificationsEnabled(userId, enable);
1359             } else if (status == NativeRdb::E_OK) {
1360                 if (!value.empty()) {
1361                     info.SetEnabledAllNotification(userId, static_cast<bool>(StringToInt(value)));
1362                 }
1363             } else {
1364                 ANS_LOGW("Parse enable all notification failed, use default value.");
1365             }
1366         });
1367 }
1368 
RemoveNotificationEnable(const int32_t userId)1369 bool NotificationPreferencesDatabase::RemoveNotificationEnable(const int32_t userId)
1370 {
1371     ANS_LOGD("%{public}s", __FUNCTION__);
1372     if (!CheckRdbStore()) {
1373         ANS_LOGE("RdbStore is nullptr.");
1374         return false;
1375     }
1376 
1377     std::string key =
1378         std::string(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
1379     int32_t result = rdbDataManager_->DeleteData(key);
1380     if (result != NativeRdb::E_OK) {
1381         ANS_LOGE("delete bundle Info failed.");
1382         return false;
1383     }
1384 
1385     ANS_LOGD("%{public}s remove notification enable, userId : %{public}d", __FUNCTION__, userId);
1386     return true;
1387 }
1388 
RemoveDoNotDisturbDate(const int32_t userId)1389 bool NotificationPreferencesDatabase::RemoveDoNotDisturbDate(const int32_t userId)
1390 {
1391     ANS_LOGD("%{public}s", __FUNCTION__);
1392     if (!CheckRdbStore()) {
1393         ANS_LOGE("RdbStore is nullptr.");
1394         return false;
1395     }
1396 
1397     std::string typeKey =
1398         std::string(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1399     std::string beginDateKey =
1400         std::string(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1401     std::string endDateKey =
1402         std::string(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1403 
1404     std::vector<std::string> keys = {
1405         typeKey,
1406         beginDateKey,
1407         endDateKey
1408     };
1409 
1410     int32_t result = rdbDataManager_->DeleteBathchData(keys);
1411     if (result != NativeRdb::E_OK) {
1412         ANS_LOGE("delete DoNotDisturb date failed.");
1413         return false;
1414     }
1415 
1416     ANS_LOGD("%{public}s remove DoNotDisturb date, userId : %{public}d", __FUNCTION__, userId);
1417     return true;
1418 }
1419 
RemoveAnsBundleDbInfo(std::string bundleName,int32_t uid)1420 bool NotificationPreferencesDatabase::RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid)
1421 {
1422     if (!CheckRdbStore()) {
1423         ANS_LOGE("RdbStore is nullptr.");
1424         return false;
1425     }
1426 
1427     std::string key = KEY_BUNDLE_LABEL + bundleName + std::to_string(uid);
1428     int32_t result = rdbDataManager_->DeleteData(key);
1429     if (result != NativeRdb::E_OK) {
1430         ANS_LOGE("Delete ans bundle db info failed, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid);
1431         return false;
1432     }
1433 
1434     ANS_LOGE("Remove ans bundle db info, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid);
1435     return true;
1436 }
1437 
SetKvToDb(const std::string & key,const std::string & value)1438 int32_t NotificationPreferencesDatabase::SetKvToDb(
1439     const std::string &key, const std::string &value)
1440 {
1441     if (!CheckRdbStore()) {
1442         ANS_LOGE("RdbStore is nullptr.");
1443         return NativeRdb::E_ERROR;
1444     }
1445     int32_t result = rdbDataManager_->InsertData(key, value);
1446     if (result != NativeRdb::E_OK) {
1447         ANS_LOGE("Set key: %{public}s failed, result %{public}d.", key.c_str(), result);
1448         return NativeRdb::E_ERROR;
1449     }
1450 
1451     ANS_LOGD("Key:%{public}s, value:%{public}s.", key.c_str(), value.c_str());
1452 
1453     return NativeRdb::E_OK;
1454 }
1455 
GetKvFromDb(const std::string & key,std::string & value)1456 int32_t NotificationPreferencesDatabase::GetKvFromDb(
1457     const std::string &key, std::string &value)
1458 {
1459     if (!CheckRdbStore()) {
1460         ANS_LOGE("RdbStore is nullptr.");
1461         return NativeRdb::E_ERROR;
1462     }
1463 
1464     int32_t result = rdbDataManager_->QueryData(key, value);
1465     if (result != NativeRdb::E_OK) {
1466         ANS_LOGE("Get key-value failed, key %{public}s, result %{pubic}d.", key.c_str(), result);
1467         return NativeRdb::E_ERROR;
1468     }
1469 
1470     ANS_LOGD("Key:%{public}s, value:%{public}s.", key.c_str(), value.c_str());
1471 
1472     return NativeRdb::E_OK;
1473 }
1474 
GetBatchKvsFromDb(const std::string & key,std::unordered_map<std::string,std::string> & values)1475 int32_t NotificationPreferencesDatabase::GetBatchKvsFromDb(
1476     const std::string &key, std::unordered_map<std::string, std::string>  &values)
1477 {
1478     if (!CheckRdbStore()) {
1479         ANS_LOGE("RdbStore is nullptr.");
1480         return NativeRdb::E_ERROR;
1481     }
1482 
1483     int32_t result = rdbDataManager_->QueryDataBeginWithKey(key, values);
1484     if (result != NativeRdb::E_OK) {
1485         ANS_LOGE("Get batch notification request failed, key %{public}s, result %{public}d.", key.c_str(), result);
1486         return NativeRdb::E_ERROR;
1487     }
1488     ANS_LOGD("Key:%{public}s.", key.c_str());
1489     return NativeRdb::E_OK;
1490 }
1491 
DeleteKvFromDb(const std::string & key)1492 int32_t NotificationPreferencesDatabase::DeleteKvFromDb(const std::string &key)
1493 {
1494     if (!CheckRdbStore()) {
1495         ANS_LOGE("RdbStore is nullptr.");
1496         return NativeRdb::E_ERROR;
1497     }
1498 
1499     int32_t result = rdbDataManager_->DeleteData(key);
1500     if (result != NativeRdb::E_OK) {
1501         ANS_LOGE("Delete key-value failed, key %{public}s, result %{public}d.", key.c_str(), result);
1502         return NativeRdb::E_ERROR;
1503     }
1504 
1505     ANS_LOGD("Delete key:%{public}s.", key.c_str());
1506 
1507     return NativeRdb::E_OK;
1508 }
1509 }  // namespace Notification
1510 }  // namespace OHOS
1511