1 /*os_account_manager
2 * Copyright (c) 2021-2024 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 <regex>
19 #include <string>
20
21 #include "ans_const_define.h"
22 #include "ans_inner_errors.h"
23 #include "os_account_manager_helper.h"
24 #include "ans_log_wrapper.h"
25 #include "hitrace_meter_adapter.h"
26 #include "os_account_manager.h"
27 #include "ipc_skeleton.h"
28 #include "bundle_manager_helper.h"
29 #include "notification_analytics_util.h"
30 #include "uri.h"
31 namespace OHOS {
32 namespace Notification {
33 /**
34 * Indicates that disturbe key which do not disturbe type.
35 */
36 const static std::string KEY_DO_NOT_DISTURB_TYPE = "ans_doNotDisturbType";
37
38 /**
39 * Indicates that disturbe key which do not disturbe begin date.
40 */
41 const static std::string KEY_DO_NOT_DISTURB_BEGIN_DATE = "ans_doNotDisturbBeginDate";
42
43 /**
44 * Indicates that disturbe key which do not disturbe end date.
45 */
46 const static std::string KEY_DO_NOT_DISTURB_END_DATE = "ans_doNotDisturbEndDate";
47
48 /**
49 * Indicates that disturbe key which do not disturbe id.
50 */
51 const static std::string KEY_DO_NOT_DISTURB_ID = "ans_doNotDisturbId";
52
53 /**
54 * Indicates that disturbe key which enable all notification.
55 */
56 const static std::string KEY_ENABLE_ALL_NOTIFICATION = "ans_notificationAll";
57
58 /**
59 * Indicates that disturbe key which bundle label.
60 */
61 const static std::string KEY_BUNDLE_LABEL = "label_ans_bundle_";
62
63 /**
64 * Indicates that disturbe key which under line.
65 */
66 const static std::string KEY_UNDER_LINE = "_";
67
68 /**
69 * Indicates that disturbe key which middle line.
70 */
71 const static std::string KEY_MIDDLE_LINE = "-";
72
73 /**
74 * Indicates that disturbe key which bundle begin key.
75 */
76 const static std::string KEY_ANS_BUNDLE = "ans_bundle";
77
78 /**
79 * Indicates that disturbe key which bundle name.
80 */
81 const static std::string KEY_BUNDLE_NAME = "name";
82
83 /**
84 * Indicates that disturbe key which bundle imortance.
85 */
86 const static std::string KEY_BUNDLE_IMPORTANCE = "importance";
87
88 /**
89 * Indicates that disturbe key which bundle show badge.
90 */
91 const static std::string KEY_BUNDLE_SHOW_BADGE = "showBadgeEnable";
92
93 /**
94 * Indicates that disturbe key which bundle total badge num.
95 */
96 const static std::string KEY_BUNDLE_BADGE_TOTAL_NUM = "badgeTotalNum";
97
98 /**
99 * Indicates that disturbe key which bundle enable notification.
100 */
101 const static std::string KEY_BUNDLE_ENABLE_NOTIFICATION = "enabledNotification";
102
103 /**
104 * Indicates that disturbe key which bundle enable notification.
105 */
106 const static std::string KEY_ENABLE_BUNDLE_DISTRIBUTED_NOTIFICATION = "enabledDistributedNotification";
107
108 /**
109 * Indicates that disturbe key which bundle enable notification.
110 */
111 const static std::string KEY_SMART_REMINDER_ENABLE_NOTIFICATION = "enabledSmartReminder";
112
113 /**
114 * Indicates that disturbe key which bundle popped dialog.
115 */
116 const static std::string KEY_BUNDLE_POPPED_DIALOG = "poppedDialog";
117
118 /**
119 * Indicates that disturbe key which bundle uid.
120 */
121 const static std::string KEY_BUNDLE_UID = "uid";
122
123 /**
124 * Indicates that disturbe key which slot.
125 */
126 const static std::string KEY_SLOT = "slot";
127
128 /**
129 * Indicates that disturbe key which slot type.
130 */
131 const static std::string KEY_SLOT_TYPE = "type";
132
133 /**
134 * Indicates that disturbe key which slot id.
135 */
136 const static std::string KEY_SLOT_ID = "id";
137
138 /**
139 * Indicates that disturbe key which slot name.
140 */
141 const static std::string KEY_SLOT_NAME = "name";
142
143 /**
144 * Indicates that disturbe key which slot description.
145 */
146 const static std::string KEY_SLOT_DESCRIPTION = "description";
147
148 /**
149 * Indicates that disturbe key which slot level.
150 */
151 const static std::string KEY_SLOT_LEVEL = "level";
152
153 /**
154 * Indicates that disturbe key which slot show badge.
155 */
156 const static std::string KEY_SLOT_SHOW_BADGE = "showBadge";
157
158 /**
159 * Indicates that disturbe key which slot enable light.
160 */
161 const static std::string KEY_SLOT_ENABLE_LIGHT = "enableLight";
162
163 /**
164 * Indicates that disturbe key which slot enable vibration.
165 */
166 const static std::string KEY_SLOT_ENABLE_VRBRATION = "enableVibration";
167
168 /**
169 * Indicates that disturbe key which slot led light color.
170 */
171 const static std::string KEY_SLOT_LED_LIGHT_COLOR = "ledLightColor";
172
173 /**
174 * Indicates that disturbe key which slot lockscreen visibleness.
175 */
176 const static std::string KEY_SLOT_LOCKSCREEN_VISIBLENESS = "lockscreenVisibleness";
177
178 /**
179 * Indicates that disturbe key which slot sound.
180 */
181 const static std::string KEY_SLOT_SOUND = "sound";
182
183 /**
184 * Indicates that disturbe key which slot vibration style.
185 */
186 const static std::string KEY_SLOT_VIBRATION_STYLE = "vibrationSytle";
187
188 /**
189 * Indicates that disturbe key which slot enable bypass end.
190 */
191 const static std::string KEY_SLOT_ENABLE_BYPASS_DND = "enableBypassDnd";
192
193 /**
194 * Indicates whether the type of slot is enabled.
195 */
196 const static std::string KEY_SLOT_ENABLED = "enabled";
197
198 /**
199 * Indicates whether the type of bundle is flags.
200 */
201 const static std::string KEY_BUNDLE_SLOTFLGS_TYPE = "bundleReminderFlagsType";
202
203 /**
204 * Indicates whether the type of slot is flags.
205 */
206 const static std::string KEY_SLOT_SLOTFLGS_TYPE = "reminderFlagsType";
207
208 /**
209 * Indicates that disturbe key which slot authorized status.
210 */
211 const static std::string KEY_SLOT_AUTHORIZED_STATUS = "authorizedStatus";
212
213 /**
214 * Indicates that disturbe key which slot authorized hint count.
215 */
216 const static std::string KEY_SLOT_AUTH_HINT_CNT = "authHintCnt";
217
218 /**
219 * Indicates that reminder mode of slot.
220 */
221 const static std::string KEY_REMINDER_MODE = "reminderMode";
222
223 constexpr char RELATIONSHIP_JSON_KEY_SERVICE[] = "service";
224 constexpr char RELATIONSHIP_JSON_KEY_APP[] = "app";
225
226 const static std::string KEY_CLONE_LABEL = "label_ans_clone_";
227
228 /**
229 * Indicates hashCode rule.
230 */
231 const static std::string KEY_HASH_CODE_RULE = "hashCodeRule";
232
233 const static std::string CLONE_BUNDLE = "bundle_";
234 const static std::string CLONE_PROFILE = "profile_";
235
NotificationPreferencesDatabase()236 NotificationPreferencesDatabase::NotificationPreferencesDatabase()
237 {
238 NotificationRdbConfig notificationRdbConfig;
239 rdbDataManager_ = std::make_shared<NotificationDataMgr>(notificationRdbConfig);
240 ANS_LOGD("Notification Rdb is created");
241 }
242
~NotificationPreferencesDatabase()243 NotificationPreferencesDatabase::~NotificationPreferencesDatabase()
244 {
245 ANS_LOGD("Notification Rdb is deleted");
246 }
247
CheckRdbStore()248 bool NotificationPreferencesDatabase::CheckRdbStore()
249 {
250 if (rdbDataManager_ != nullptr) {
251 int32_t result = rdbDataManager_->Init();
252 if (result == NativeRdb::E_OK) {
253 return true;
254 }
255 }
256
257 return false;
258 }
259
PutSlotsToDisturbeDB(const std::string & bundleName,const int32_t & bundleUid,const std::vector<sptr<NotificationSlot>> & slots)260 bool NotificationPreferencesDatabase::PutSlotsToDisturbeDB(
261 const std::string &bundleName, const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots)
262 {
263 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
264 ANS_LOGD("%{public}s", __FUNCTION__);
265 if (bundleName.empty()) {
266 ANS_LOGE("Bundle name is null.");
267 return false;
268 }
269
270 if (slots.empty()) {
271 ANS_LOGE("Slot is empty.");
272 return false;
273 }
274
275 std::unordered_map<std::string, std::string> values;
276 for (auto iter : slots) {
277 bool result = SlotToEntry(bundleName, bundleUid, iter, values);
278 if (!result) {
279 return result;
280 }
281 }
282 int32_t userId = -1;
283 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
284 if (!CheckRdbStore()) {
285 ANS_LOGE("RdbStore is nullptr.");
286 return false;
287 }
288 int32_t result = rdbDataManager_->InsertBatchData(values, userId);
289 return (result == NativeRdb::E_OK);
290 }
291
PutBundlePropertyToDisturbeDB(const NotificationPreferencesInfo::BundleInfo & bundleInfo)292 bool NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB(
293 const NotificationPreferencesInfo::BundleInfo &bundleInfo)
294 {
295 if (bundleInfo.GetBundleName().empty()) {
296 ANS_LOGE("Bundle name is null.");
297 return false;
298 }
299
300 if (!CheckRdbStore()) {
301 ANS_LOGE("RdbStore is nullptr.");
302 return false;
303 }
304 std::string values;
305 std::string bundleKeyStr = KEY_BUNDLE_LABEL + GenerateBundleLablel(bundleInfo);
306 int32_t userId = -1;
307 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
308 bool result = false;
309 GetValueFromDisturbeDB(bundleKeyStr, userId, [&](const int32_t &status, std::string &value) {
310 switch (status) {
311 case NativeRdb::E_EMPTY_VALUES_BUCKET: {
312 result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo);
313 break;
314 }
315 case NativeRdb::E_OK: {
316 ANS_LOGE("Current bundle has exsited.");
317 break;
318 }
319 default:
320 break;
321 }
322 });
323 return result;
324 }
325
PutShowBadge(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & enable)326 bool NotificationPreferencesDatabase::PutShowBadge(
327 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable)
328 {
329 if (bundleInfo.GetBundleName().empty()) {
330 ANS_LOGE("Bundle name is nullptr.");
331 return false;
332 }
333
334 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
335 return false;
336 }
337
338 std::string bundleKey = GenerateBundleLablel(bundleInfo);
339 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable,
340 bundleInfo.GetBundleUid());
341 return (result == NativeRdb::E_OK);
342 }
343
PutImportance(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const int32_t & importance)344 bool NotificationPreferencesDatabase::PutImportance(
345 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &importance)
346 {
347 if (bundleInfo.GetBundleName().empty()) {
348 ANS_LOGE("Bundle name is empty.");
349 return false;
350 }
351
352 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
353 return false;
354 }
355
356 std::string bundleKey = GenerateBundleLablel(bundleInfo);
357 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_IMPORTANCE_TYPE, importance,
358 bundleInfo.GetBundleUid());
359 return (result == NativeRdb::E_OK);
360 }
361
PutTotalBadgeNums(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const int32_t & totalBadgeNum)362 bool NotificationPreferencesDatabase::PutTotalBadgeNums(
363 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &totalBadgeNum)
364 {
365 if (bundleInfo.GetBundleName().empty()) {
366 ANS_LOGE("Bundle name is blank.");
367 return false;
368 }
369
370 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
371 return false;
372 }
373 std::string bundleKey = GenerateBundleLablel(bundleInfo);
374 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum,
375 bundleInfo.GetBundleUid());
376 return (result == NativeRdb::E_OK);
377 }
378
PutNotificationsEnabledForBundle(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & enabled)379 bool NotificationPreferencesDatabase::PutNotificationsEnabledForBundle(
380 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled)
381 {
382 ANS_LOGD("%{public}s, enabled[%{public}d]", __FUNCTION__, enabled);
383 if (bundleInfo.GetBundleName().empty()) {
384 ANS_LOGE("Bundle name is null.");
385 return false;
386 }
387
388 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
389 return false;
390 }
391
392 std::string bundleKey = GenerateBundleLablel(bundleInfo);
393 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled,
394 bundleInfo.GetBundleUid());
395 return (result == NativeRdb::E_OK);
396 }
397
PutNotificationsEnabled(const int32_t & userId,const bool & enabled)398 bool NotificationPreferencesDatabase::PutNotificationsEnabled(const int32_t &userId, const bool &enabled)
399 {
400 if (!CheckRdbStore()) {
401 ANS_LOGE("RdbStore is nullptr.");
402 return false;
403 }
404
405 std::string typeKey =
406 std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
407 std::string enableValue = std::to_string(enabled);
408 int32_t result = rdbDataManager_->InsertData(typeKey, enableValue, userId);
409 if (result != NativeRdb::E_OK) {
410 ANS_LOGE("Store enable notification failed. %{public}d", result);
411 return false;
412 }
413 return true;
414 }
415
PutSlotFlags(NotificationPreferencesInfo::BundleInfo & bundleInfo,const int32_t & slotFlags)416 bool NotificationPreferencesDatabase::PutSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo,
417 const int32_t &slotFlags)
418 {
419 if (!CheckRdbStore()) {
420 ANS_LOGE("RdbStore is nullptr.");
421 return false;
422 }
423
424 std::string bundleKey = GenerateBundleLablel(bundleInfo);
425 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_SLOTFLGS_TYPE, slotFlags,
426 bundleInfo.GetBundleUid());
427 return (result == NativeRdb::E_OK);
428 }
429
PutHasPoppedDialog(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & hasPopped)430 bool NotificationPreferencesDatabase::PutHasPoppedDialog(
431 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped)
432 {
433 if (bundleInfo.GetBundleName().empty()) {
434 ANS_LOGE("Bundle name is null.");
435 return false;
436 }
437
438 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
439 return false;
440 }
441
442 std::string bundleKey = GenerateBundleLablel(bundleInfo);
443 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped,
444 bundleInfo.GetBundleUid());
445 return (result == NativeRdb::E_OK);
446 }
447
IsNotificationSlotFlagsExists(const sptr<NotificationBundleOption> & bundleOption)448 bool NotificationPreferencesDatabase::IsNotificationSlotFlagsExists(const sptr<NotificationBundleOption> &bundleOption)
449 {
450 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
451 return false;
452 }
453 std::string bundleKey = bundleOption->GetBundleName().append(std::to_string(bundleOption->GetUid()));
454 std::string key = GenerateBundleKey(bundleKey, KEY_BUNDLE_SLOTFLGS_TYPE);
455 std::string value;
456 int32_t userId = -1;
457 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleOption->GetUid(), userId);
458 int32_t result = rdbDataManager_->QueryData(key, value, userId);
459 return (result == NativeRdb::E_OK) || (!value.empty());
460 }
461
PutDoNotDisturbDate(const int32_t & userId,const sptr<NotificationDoNotDisturbDate> & date)462 bool NotificationPreferencesDatabase::PutDoNotDisturbDate(
463 const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date)
464 {
465 if (date == nullptr) {
466 ANS_LOGE("Invalid date.");
467 return false;
468 }
469
470 if (!CheckRdbStore()) {
471 ANS_LOGE("RdbStore is nullptr.");
472 return false;
473 }
474
475 std::string typeKey =
476 std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
477 std::string typeValue = std::to_string((int)date->GetDoNotDisturbType());
478
479 std::string beginDateKey =
480 std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
481 std::string beginDateValue = std::to_string(date->GetBeginDate());
482
483 std::string endDateKey =
484 std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
485 std::string endDateValue = std::to_string(date->GetEndDate());
486
487 std::unordered_map<std::string, std::string> values = {
488 {typeKey, typeValue},
489 {beginDateKey, beginDateValue},
490 {endDateKey, endDateValue},
491 };
492
493 int32_t result = rdbDataManager_->InsertBatchData(values, userId);
494 if (result != NativeRdb::E_OK) {
495 ANS_LOGE("Store DoNotDisturbDate failed. %{public}d", result);
496 return false;
497 }
498
499 return true;
500 }
501
AddDoNotDisturbProfiles(int32_t userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)502 bool NotificationPreferencesDatabase::AddDoNotDisturbProfiles(
503 int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
504 {
505 if (profiles.empty()) {
506 ANS_LOGE("Invalid dates.");
507 return false;
508 }
509 if (!CheckRdbStore()) {
510 ANS_LOGE("RdbStore is nullptr.");
511 return false;
512 }
513 std::unordered_map<std::string, std::string> values;
514 for (auto profile : profiles) {
515 if (profile == nullptr) {
516 ANS_LOGE("The profile is null.");
517 return false;
518 }
519 std::string key = std::string().append(KEY_DO_NOT_DISTURB_ID).append(KEY_UNDER_LINE).append(
520 std::to_string(userId)).append(KEY_UNDER_LINE).append(std::to_string((int32_t)profile->GetProfileId()));
521 values[key] = profile->ToJson();
522 }
523 int32_t result = rdbDataManager_->InsertBatchData(values, userId);
524 if (result != NativeRdb::E_OK) {
525 ANS_LOGE("Add do not disturb profiles failed.");
526 return false;
527 }
528 return true;
529 }
530
RemoveDoNotDisturbProfiles(int32_t userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)531 bool NotificationPreferencesDatabase::RemoveDoNotDisturbProfiles(
532 int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
533 {
534 if (profiles.empty()) {
535 ANS_LOGW("Invalid dates.");
536 return false;
537 }
538 if (!CheckRdbStore()) {
539 ANS_LOGE("RdbStore is nullptr.");
540 return false;
541 }
542 std::vector<std::string> keys;
543 for (auto profile : profiles) {
544 if (profile == nullptr) {
545 ANS_LOGE("The profile is null.");
546 return false;
547 }
548 std::string key = std::string().append(KEY_DO_NOT_DISTURB_ID).append(KEY_UNDER_LINE).append(
549 std::to_string(userId)).append(KEY_UNDER_LINE).append(std::to_string((int32_t)profile->GetProfileId()));
550 keys.push_back(key);
551 }
552 int32_t result = rdbDataManager_->DeleteBathchData(keys, userId);
553 if (result != NativeRdb::E_OK) {
554 ANS_LOGE("Delete do not disturb profiles failed.");
555 return false;
556 }
557 return true;
558 }
559
GetDoNotDisturbProfiles(const std::string & key,sptr<NotificationDoNotDisturbProfile> & profile,const int32_t & userId)560 bool NotificationPreferencesDatabase::GetDoNotDisturbProfiles(
561 const std::string &key, sptr<NotificationDoNotDisturbProfile> &profile, const int32_t &userId)
562 {
563 if (!CheckRdbStore()) {
564 ANS_LOGE("RdbStore is nullptr.");
565 return false;
566 }
567 std::string values;
568 int32_t result = rdbDataManager_->QueryData(key, values, userId);
569 if (result != NativeRdb::E_OK) {
570 ANS_LOGE("Use default value. error code is %{public}d", result);
571 return false;
572 }
573 profile = new (std::nothrow) NotificationDoNotDisturbProfile();
574 if (profile == nullptr) {
575 ANS_LOGE("The profile is null.");
576 return false;
577 }
578 profile->FromJson(values);
579 return true;
580 }
581
GetValueFromDisturbeDB(const std::string & key,const int32_t & userId,std::function<void (std::string &)> callback)582 void NotificationPreferencesDatabase::GetValueFromDisturbeDB(
583 const std::string &key, const int32_t &userId, std::function<void(std::string &)> callback)
584 {
585 if (!CheckRdbStore()) {
586 ANS_LOGE("RdbStore is nullptr.");
587 return;
588 }
589 std::string value;
590 int32_t result = rdbDataManager_->QueryData(key, value, userId);
591 if (result == NativeRdb::E_ERROR) {
592 ANS_LOGE("Get value failed, use default value. error code is %{public}d", result);
593 return;
594 }
595 callback(value);
596 }
597
GetValueFromDisturbeDB(const std::string & key,const int32_t & userId,std::function<void (int32_t &,std::string &)> callback)598 void NotificationPreferencesDatabase::GetValueFromDisturbeDB(
599 const std::string &key, const int32_t &userId, std::function<void(int32_t &, std::string &)> callback)
600 {
601 if (!CheckRdbStore()) {
602 ANS_LOGE("RdbStore is nullptr.");
603 return;
604 }
605 std::string value;
606 int32_t result = rdbDataManager_->QueryData(key, value, userId);
607 callback(result, value);
608 }
609
CheckBundle(const std::string & bundleName,const int32_t & bundleUid)610 bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleName, const int32_t &bundleUid)
611 {
612 std::string bundleKeyStr = KEY_BUNDLE_LABEL + bundleName + std::to_string(bundleUid);
613 ANS_LOGD("CheckBundle bundleKeyStr %{public}s", bundleKeyStr.c_str());
614 bool result = true;
615 int32_t userId = -1;
616 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
617 GetValueFromDisturbeDB(bundleKeyStr, userId, [&](const int32_t &status, std::string &value) {
618 switch (status) {
619 case NativeRdb::E_EMPTY_VALUES_BUCKET: {
620 NotificationPreferencesInfo::BundleInfo bundleInfo;
621 bundleInfo.SetBundleName(bundleName);
622 bundleInfo.SetBundleUid(bundleUid);
623 bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleName, bundleUid));
624 result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo);
625 break;
626 }
627 case NativeRdb::E_OK: {
628 result = true;
629 break;
630 }
631 default:
632 result = false;
633 break;
634 }
635 });
636 return result;
637 }
638
PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo & bundleInfo)639 bool NotificationPreferencesDatabase::PutBundlePropertyValueToDisturbeDB(
640 const NotificationPreferencesInfo::BundleInfo &bundleInfo)
641 {
642 std::unordered_map<std::string, std::string> values;
643 std::string bundleKey = bundleInfo.GetBundleName().append(std::to_string(bundleInfo.GetBundleUid()));
644 GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_NAME), bundleInfo.GetBundleName(), values);
645 GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_BADGE_TOTAL_NUM),
646 std::to_string(bundleInfo.GetBadgeTotalNum()),
647 values);
648 GenerateEntry(
649 GenerateBundleKey(bundleKey, KEY_BUNDLE_IMPORTANCE), std::to_string(bundleInfo.GetImportance()), values);
650 GenerateEntry(
651 GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE), std::to_string(bundleInfo.GetIsShowBadge()), values);
652 GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION),
653 std::to_string(bundleInfo.GetEnableNotification()),
654 values);
655 GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_POPPED_DIALOG),
656 std::to_string(bundleInfo.GetHasPoppedDialog()),
657 values);
658 GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_UID), std::to_string(bundleInfo.GetBundleUid()), values);
659 if (!CheckRdbStore()) {
660 ANS_LOGE("RdbStore is nullptr.");
661 return false;
662 }
663 int32_t userId = -1;
664 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
665 int32_t result = rdbDataManager_->InsertBatchData(values, userId);
666 if (result != NativeRdb::E_OK) {
667 ANS_LOGE("Store bundle failed. %{public}d", result);
668 return false;
669 }
670 return true;
671 }
672
ParseFromDisturbeDB(NotificationPreferencesInfo & info,int32_t userId)673 bool NotificationPreferencesDatabase::ParseFromDisturbeDB(NotificationPreferencesInfo &info, int32_t userId)
674 {
675 ANS_LOGD("%{public}s", __FUNCTION__);
676 if (!CheckRdbStore()) {
677 ANS_LOGE("RdbStore is nullptr.");
678 return false;
679 }
680 std::vector<int> activeUserId;
681 if (userId == -1) {
682 OsAccountManagerHelper::GetInstance().GetAllActiveOsAccount(activeUserId);
683 } else {
684 activeUserId.push_back(userId);
685 }
686
687 for (auto iter : activeUserId) {
688 GetDoNotDisturbType(info, iter);
689 GetDoNotDisturbBeginDate(info, iter);
690 GetDoNotDisturbEndDate(info, iter);
691 GetEnableAllNotification(info, iter);
692 GetDoNotDisturbProfile(info, iter);
693 }
694
695 return true;
696 }
697
698
GetBundleInfo(const sptr<NotificationBundleOption> & bundleOption,NotificationPreferencesInfo::BundleInfo & bundleInfo)699 bool NotificationPreferencesDatabase::GetBundleInfo(const sptr<NotificationBundleOption> &bundleOption,
700 NotificationPreferencesInfo::BundleInfo &bundleInfo)
701 {
702 std::string bundleDBKey = KEY_BUNDLE_LABEL + bundleOption->GetBundleName() +
703 std::to_string(bundleOption->GetUid());
704 int32_t userId = -1;
705 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleOption->GetUid(), userId);
706 std::string bundleKey;
707 int32_t result = rdbDataManager_->QueryData(bundleDBKey, bundleKey, userId);
708 if (result != NativeRdb::E_OK) {
709 ANS_LOGE("Get Bundle Info failed.");
710 return false;
711 }
712 ANS_LOGD("Bundle name is %{public}s.", bundleKey.c_str());
713 std::unordered_map<std::string, std::string> bundleEntries;
714 rdbDataManager_->QueryDataBeginWithKey((GenerateBundleKey(bundleKey)), bundleEntries, userId);
715 ANS_LOGD("Bundle key is %{public}s.", GenerateBundleKey(bundleKey).c_str());
716 std::string keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE);
717 bool badgeEnableExist = false;
718 for (auto bundleEntry : bundleEntries) {
719 if (IsSlotKey(GenerateBundleKey(bundleKey), bundleEntry.first)) {
720 ParseSlotFromDisturbeDB(bundleInfo, bundleKey, bundleEntry, userId);
721 } else {
722 ParseBundlePropertyFromDisturbeDB(bundleInfo, bundleKey, bundleEntry);
723 }
724
725 if (keyStr.compare(bundleEntry.first) == 0) {
726 badgeEnableExist = true;
727 }
728 }
729
730 if (!badgeEnableExist) {
731 bundleInfo.SetIsShowBadge(static_cast<bool>(true));
732 }
733 return true;
734 }
735
RemoveAllDataFromDisturbeDB()736 bool NotificationPreferencesDatabase::RemoveAllDataFromDisturbeDB()
737 {
738 ANS_LOGD("%{public}s", __FUNCTION__);
739 if (!CheckRdbStore()) {
740 ANS_LOGE("RdbStore is nullptr.");
741 return false;
742 }
743 int32_t result = rdbDataManager_->Destroy();
744 return (result == NativeRdb::E_OK);
745 }
746
RemoveBundleFromDisturbeDB(const std::string & bundleKey,const int32_t & bundleUid)747 bool NotificationPreferencesDatabase::RemoveBundleFromDisturbeDB(
748 const std::string &bundleKey, const int32_t &bundleUid)
749 {
750 ANS_LOGD("%{public}s", __FUNCTION__);
751 if (!CheckRdbStore()) {
752 ANS_LOGE("RdbStore is nullptr.");
753 return false;
754 }
755 int32_t userId = -1;
756 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
757
758 std::unordered_map<std::string, std::string> values;
759 int32_t result = rdbDataManager_->QueryDataBeginWithKey(
760 (KEY_ANS_BUNDLE + KEY_UNDER_LINE + bundleKey + KEY_UNDER_LINE), values, userId);
761
762 if (result == NativeRdb::E_ERROR) {
763 ANS_LOGE("Get Bundle Info failed.");
764 return false;
765 }
766
767 std::vector<std::string> keys;
768 for (auto iter : values) {
769 keys.push_back(iter.first);
770 }
771
772 std::string bundleDBKey = KEY_BUNDLE_LABEL + KEY_BUNDLE_NAME + KEY_UNDER_LINE + bundleKey;
773 keys.push_back(bundleDBKey);
774 result = rdbDataManager_->DeleteBathchData(keys, userId);
775 if (result != NativeRdb::E_OK) {
776 ANS_LOGE("delete bundle Info failed.");
777 return false;
778 }
779 return true;
780 }
781
RemoveSlotFromDisturbeDB(const std::string & bundleKey,const NotificationConstant::SlotType & type,const int32_t & bundleUid)782 bool NotificationPreferencesDatabase::RemoveSlotFromDisturbeDB(
783 const std::string &bundleKey, const NotificationConstant::SlotType &type, const int32_t &bundleUid)
784 {
785 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
786 ANS_LOGD("%{public}s", __FUNCTION__);
787 int32_t userId = -1;
788 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
789 if (bundleKey.empty()) {
790 ANS_LOGE("Bundle name is empty.");
791 return false;
792 }
793
794 if (!CheckRdbStore()) {
795 ANS_LOGE("RdbStore is nullptr.");
796 return false;
797 }
798
799 std::unordered_map<std::string, std::string> values;
800 std::string slotType = std::to_string(type);
801 int32_t result = rdbDataManager_->QueryDataBeginWithKey(
802 (GenerateSlotKey(bundleKey, slotType) + KEY_UNDER_LINE), values, userId);
803 if (result == NativeRdb::E_ERROR) {
804 return false;
805 }
806 std::vector<std::string> keys;
807 for (auto iter : values) {
808 keys.push_back(iter.first);
809 }
810
811 result = rdbDataManager_->DeleteBathchData(keys, userId);
812 if (result != NativeRdb::E_OK) {
813 ANS_LOGE("delete bundle Info failed.");
814 return false;
815 }
816
817 return true;
818 }
819
GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> & bundleOption)820 bool NotificationPreferencesDatabase::GetAllNotificationEnabledBundles(
821 std::vector<NotificationBundleOption> &bundleOption)
822 {
823 ANS_LOGD("Called.");
824 if (!CheckRdbStore()) {
825 ANS_LOGE("RdbStore is nullptr.");
826 return false;
827 }
828 std::unordered_map<std::string, std::string> datas;
829 const std::string ANS_BUNDLE_BEGIN = "ans_bundle_";
830 int32_t userId = -1;
831 OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId);
832 int32_t errCode = rdbDataManager_->QueryDataBeginWithKey(ANS_BUNDLE_BEGIN, datas, userId);
833 if (errCode != NativeRdb::E_OK) {
834 ANS_LOGE("Query data begin with ans_bundle_ from db error");
835 return false;
836 }
837 return HandleDataBaseMap(datas, bundleOption);
838 }
839
HandleDataBaseMap(const std::unordered_map<std::string,std::string> & datas,std::vector<NotificationBundleOption> & bundleOption)840 bool NotificationPreferencesDatabase::HandleDataBaseMap(
841 const std::unordered_map<std::string, std::string> &datas, std::vector<NotificationBundleOption> &bundleOption)
842 {
843 std::regex matchBundlenamePattern("^ans_bundle_(.*)_name$");
844 std::smatch match;
845 int32_t currentUserId = SUBSCRIBE_USER_INIT;
846 ErrCode result = ERR_OK;
847 result = OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(currentUserId);
848 if (result != ERR_OK) {
849 ANS_LOGE("Get account id fail");
850 return false;
851 }
852 constexpr int MIDDLE_KEY = 1;
853 for (const auto &dataMapItem : datas) {
854 const std::string &key = dataMapItem.first;
855 const std::string &value = dataMapItem.second;
856 if (!std::regex_match(key, match, matchBundlenamePattern)) {
857 continue;
858 }
859 std::string matchKey = match[MIDDLE_KEY].str();
860 std::string matchUid = "ans_bundle_" + matchKey + "_uid";
861 std::string matchEnableNotification = "ans_bundle_" + matchKey + "_enabledNotification";
862 auto enableNotificationItem = datas.find(matchEnableNotification);
863 if (enableNotificationItem == datas.end()) {
864 continue;
865 }
866 if (static_cast<bool>(StringToInt(enableNotificationItem->second))) {
867 auto uidItem = datas.find(matchUid);
868 if (uidItem == datas.end()) {
869 continue;
870 }
871 int userid = -1;
872 result =
873 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(StringToInt(uidItem->second), userid);
874 if (result != ERR_OK) {
875 return false;
876 }
877 if (userid != currentUserId) {
878 continue;
879 }
880 NotificationBundleOption obj(value, StringToInt(uidItem->second));
881 bundleOption.emplace_back(obj);
882 }
883 }
884 return true;
885 }
886
RemoveAllSlotsFromDisturbeDB(const std::string & bundleKey,const int32_t & bundleUid)887 bool NotificationPreferencesDatabase::RemoveAllSlotsFromDisturbeDB(
888 const std::string &bundleKey, const int32_t &bundleUid)
889 {
890 ANS_LOGD("%{public}s", __FUNCTION__);
891 int32_t userId = -1;
892 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
893 if (bundleKey.empty()) {
894 ANS_LOGE("Bundle name is null.");
895 return false;
896 }
897
898 if (!CheckRdbStore()) {
899 ANS_LOGE("RdbStore is nullptr.");
900 return false;
901 }
902
903 std::unordered_map<std::string, std::string> values;
904 int32_t result = rdbDataManager_->QueryDataBeginWithKey(
905 (GenerateSlotKey(bundleKey) + KEY_UNDER_LINE), values, userId);
906 if (result == NativeRdb::E_ERROR) {
907 return false;
908 }
909 std::vector<std::string> keys;
910 for (auto iter : values) {
911 keys.push_back(iter.first);
912 }
913
914 result = rdbDataManager_->DeleteBathchData(keys, userId);
915 return (result == NativeRdb::E_OK);
916 }
917
918 template <typename T>
PutBundlePropertyToDisturbeDB(const std::string & bundleKey,const BundleType & type,const T & t,const int32_t & bundleUid)919 int32_t NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB(
920 const std::string &bundleKey, const BundleType &type, const T &t, const int32_t &bundleUid)
921 {
922 std::string keyStr;
923 switch (type) {
924 case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
925 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_BADGE_TOTAL_NUM);
926 break;
927 case BundleType::BUNDLE_IMPORTANCE_TYPE:
928 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_IMPORTANCE);
929 break;
930 case BundleType::BUNDLE_SHOW_BADGE_TYPE:
931 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE);
932 break;
933 case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
934 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION);
935 break;
936 case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
937 ANS_LOGD("Into BUNDLE_POPPED_DIALOG_TYPE:GenerateBundleKey.");
938 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_POPPED_DIALOG);
939 break;
940 case BundleType::BUNDLE_SLOTFLGS_TYPE:
941 ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:GenerateBundleKey.");
942 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SLOTFLGS_TYPE);
943 break;
944 default:
945 break;
946 }
947 if (!CheckRdbStore()) {
948 ANS_LOGE("RdbStore is nullptr.");
949 return false;
950 }
951 int32_t userId = -1;
952 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
953 std::string valueStr = std::to_string(t);
954 int32_t result = rdbDataManager_->InsertData(keyStr, valueStr, userId);
955 return result;
956 }
957
PutBundleToDisturbeDB(const std::string & bundleKey,const NotificationPreferencesInfo::BundleInfo & bundleInfo)958 bool NotificationPreferencesDatabase::PutBundleToDisturbeDB(
959 const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo)
960 {
961 if (!CheckRdbStore()) {
962 ANS_LOGE("RdbStore is nullptr.");
963 return false;
964 }
965 int32_t userId = -1;
966 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
967
968 ANS_LOGD("Key not fund, so create a bundle, bundle key is %{public}s.", bundleKey.c_str());
969 int32_t result = rdbDataManager_->InsertData(bundleKey, GenerateBundleLablel(bundleInfo), userId);
970 if (result != NativeRdb::E_OK) {
971 ANS_LOGE("Store bundle name to db is failed.");
972 return false;
973 }
974
975 if (!PutBundlePropertyValueToDisturbeDB(bundleInfo)) {
976 return false;
977 }
978 return true;
979 }
980
GenerateEntry(const std::string & key,const std::string & value,std::unordered_map<std::string,std::string> & values) const981 void NotificationPreferencesDatabase::GenerateEntry(
982 const std::string &key, const std::string &value, std::unordered_map<std::string, std::string> &values) const
983 {
984 values.emplace(key, value);
985 }
986
SlotToEntry(const std::string & bundleName,const int32_t & bundleUid,const sptr<NotificationSlot> & slot,std::unordered_map<std::string,std::string> & values)987 bool NotificationPreferencesDatabase::SlotToEntry(const std::string &bundleName, const int32_t &bundleUid,
988 const sptr<NotificationSlot> &slot, std::unordered_map<std::string, std::string> &values)
989 {
990 if (slot == nullptr) {
991 ANS_LOGE("Notification slot is nullptr.");
992 return false;
993 }
994
995 if (!CheckBundle(bundleName, bundleUid)) {
996 return false;
997 }
998
999 std::string bundleKey = bundleName + std::to_string(bundleUid);
1000 GenerateSlotEntry(bundleKey, slot, values);
1001 return true;
1002 }
1003
GenerateSlotEntry(const std::string & bundleKey,const sptr<NotificationSlot> & slot,std::unordered_map<std::string,std::string> & values) const1004 void NotificationPreferencesDatabase::GenerateSlotEntry(const std::string &bundleKey,
1005 const sptr<NotificationSlot> &slot, std::unordered_map<std::string, std::string> &values) const
1006 {
1007 std::string slotType = std::to_string(slot->GetType());
1008 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_TYPE), std::to_string(slot->GetType()), values);
1009 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ID), slot->GetId(), values);
1010 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_NAME), slot->GetName(), values);
1011 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_DESCRIPTION), slot->GetDescription(), values);
1012 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LEVEL), std::to_string(slot->GetLevel()), values);
1013 GenerateEntry(
1014 GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SHOW_BADGE), std::to_string(slot->IsShowBadge()), values);
1015 GenerateEntry(
1016 GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_LIGHT), std::to_string(slot->CanEnableLight()), values);
1017 GenerateEntry(
1018 GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_VRBRATION), std::to_string(slot->CanVibrate()), values);
1019 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LED_LIGHT_COLOR),
1020 std::to_string(slot->GetLedLightColor()), values);
1021 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LOCKSCREEN_VISIBLENESS),
1022 std::to_string(static_cast<int>(slot->GetLockScreenVisibleness())), values);
1023 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SOUND), slot->GetSound().ToString(), values);
1024 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_BYPASS_DND),
1025 std::to_string(slot->IsEnableBypassDnd()), values);
1026 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_VIBRATION_STYLE),
1027 VectorToString(slot->GetVibrationStyle()), values);
1028 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLED), std::to_string(slot->GetEnable()), values);
1029 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_AUTHORIZED_STATUS),
1030 std::to_string(slot->GetAuthorizedStatus()), values);
1031 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_AUTH_HINT_CNT),
1032 std::to_string(slot->GetAuthHintCnt()), values);
1033 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_REMINDER_MODE),
1034 std::to_string(slot->GetReminderMode()), values);
1035 }
1036
ParseBundleFromDistureDB(NotificationPreferencesInfo & info,const std::unordered_map<std::string,std::string> & values,const int32_t & userId)1037 void NotificationPreferencesDatabase::ParseBundleFromDistureDB(NotificationPreferencesInfo &info,
1038 const std::unordered_map<std::string, std::string> &values, const int32_t &userId)
1039 {
1040 if (!CheckRdbStore()) {
1041 ANS_LOGE("RdbStore is nullptr.");
1042 return;
1043 }
1044 for (auto item : values) {
1045 std::string bundleKey = item.second;
1046 ANS_LOGD("Bundle name is %{public}s.", bundleKey.c_str());
1047 std::unordered_map<std::string, std::string> bundleEntries;
1048 rdbDataManager_->QueryDataBeginWithKey((GenerateBundleKey(bundleKey)), bundleEntries, userId);
1049 ANS_LOGD("Bundle key is %{public}s.", GenerateBundleKey(bundleKey).c_str());
1050 NotificationPreferencesInfo::BundleInfo bunldeInfo;
1051 std::string keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE);
1052 bool badgeEnableExist = false;
1053 for (auto bundleEntry : bundleEntries) {
1054 if (IsSlotKey(GenerateBundleKey(bundleKey), bundleEntry.first)) {
1055 ParseSlotFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry, userId);
1056 } else {
1057 ParseBundlePropertyFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry);
1058 }
1059
1060 if (keyStr.compare(bundleEntry.first) == 0) {
1061 badgeEnableExist = true;
1062 }
1063 }
1064
1065 if (!badgeEnableExist) {
1066 bunldeInfo.SetIsShowBadge(static_cast<bool>(true));
1067 }
1068
1069 info.SetBundleInfoFromDb(bunldeInfo, bundleKey);
1070 }
1071 }
1072
ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & bundleKey,const std::pair<std::string,std::string> & entry,const int32_t & userId)1073 void NotificationPreferencesDatabase::ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo,
1074 const std::string &bundleKey, const std::pair<std::string, std::string> &entry, const int32_t &userId)
1075 {
1076 std::string slotKey = entry.first;
1077 std::string typeStr = SubUniqueIdentifyFromString(GenerateSlotKey(bundleKey) + KEY_UNDER_LINE, slotKey);
1078 NotificationConstant::SlotType slotType = static_cast<NotificationConstant::SlotType>(StringToInt(typeStr));
1079 sptr<NotificationSlot> slot = nullptr;
1080 if (!bundleInfo.GetSlot(slotType, slot)) {
1081 slot = new (std::nothrow) NotificationSlot(slotType);
1082 if (slot == nullptr) {
1083 ANS_LOGE("Failed to create NotificationSlot instance");
1084 return;
1085 }
1086 }
1087 std::string findString = GenerateSlotKey(bundleKey, typeStr) + KEY_UNDER_LINE;
1088 ParseSlot(findString, slot, entry, userId);
1089 bundleInfo.SetSlot(slot);
1090 }
1091
ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & bundleKey,const std::pair<std::string,std::string> & entry)1092 void NotificationPreferencesDatabase::ParseBundlePropertyFromDisturbeDB(
1093 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey,
1094 const std::pair<std::string, std::string> &entry)
1095 {
1096 std::string typeStr = FindLastString(GenerateBundleKey(bundleKey), entry.first);
1097 std::string valueStr = entry.second;
1098
1099 if (typeStr.compare(KEY_BUNDLE_NAME) == 0) {
1100 return ParseBundleName(bundleInfo, valueStr);
1101 }
1102 if (typeStr.compare(KEY_BUNDLE_IMPORTANCE) == 0) {
1103 return ParseBundleImportance(bundleInfo, valueStr);
1104 }
1105 if (typeStr.compare(KEY_BUNDLE_SHOW_BADGE) == 0) {
1106 return ParseBundleShowBadgeEnable(bundleInfo, valueStr);
1107 }
1108 if (typeStr.compare(KEY_BUNDLE_BADGE_TOTAL_NUM) == 0) {
1109 return ParseBundleBadgeNum(bundleInfo, valueStr);
1110 }
1111 if (typeStr.compare(KEY_BUNDLE_ENABLE_NOTIFICATION) == 0) {
1112 return ParseBundleEnableNotification(bundleInfo, valueStr);
1113 }
1114 if (typeStr.compare(KEY_BUNDLE_POPPED_DIALOG) == 0) {
1115 return ParseBundlePoppedDialog(bundleInfo, valueStr);
1116 }
1117 if (typeStr.compare(KEY_BUNDLE_UID) == 0) {
1118 return ParseBundleUid(bundleInfo, valueStr);
1119 }
1120 if (typeStr.compare(KEY_BUNDLE_SLOTFLGS_TYPE) == 0) {
1121 return ParseBundleSlotFlags(bundleInfo, valueStr);
1122 }
1123 }
1124
ParseSlot(const std::string & findString,sptr<NotificationSlot> & slot,const std::pair<std::string,std::string> & entry,const int32_t & userId)1125 void NotificationPreferencesDatabase::ParseSlot(const std::string &findString, sptr<NotificationSlot> &slot,
1126 const std::pair<std::string, std::string> &entry, const int32_t &userId)
1127 {
1128 std::string typeStr = FindLastString(findString, entry.first);
1129 std::string valueStr = entry.second;
1130 ANS_LOGD("db key = %{public}s , %{public}s : %{public}s ",
1131 entry.first.c_str(),
1132 typeStr.c_str(),
1133 entry.second.c_str());
1134 SetSoltProperty(slot, typeStr, valueStr, findString, userId);
1135 }
1136
SetSoltProperty(sptr<NotificationSlot> & slot,std::string & typeStr,std::string & valueStr,const std::string & findString,const int32_t & userId)1137 void NotificationPreferencesDatabase::SetSoltProperty(sptr<NotificationSlot> &slot, std::string &typeStr,
1138 std::string &valueStr, const std::string &findString, const int32_t &userId)
1139 {
1140 if (typeStr.compare(KEY_SLOT_DESCRIPTION) == 0) {
1141 return ParseSlotDescription(slot, valueStr);
1142 }
1143 if (typeStr.compare(KEY_SLOT_LEVEL) == 0) {
1144 return ParseSlotLevel(slot, valueStr);
1145 }
1146 if (typeStr.compare(KEY_SLOT_SHOW_BADGE) == 0) {
1147 return ParseSlotShowBadge(slot, valueStr);
1148 }
1149 if (typeStr.compare(KEY_SLOT_ENABLE_LIGHT) == 0) {
1150 return ParseSlotEnableLight(slot, valueStr);
1151 }
1152 if (typeStr.compare(KEY_SLOT_ENABLE_VRBRATION) == 0) {
1153 return ParseSlotEnableVrbration(slot, valueStr);
1154 }
1155 if (typeStr.compare(KEY_SLOT_LED_LIGHT_COLOR) == 0) {
1156 return ParseSlotLedLightColor(slot, valueStr);
1157 }
1158 if (typeStr.compare(KEY_SLOT_LOCKSCREEN_VISIBLENESS) == 0) {
1159 return ParseSlotLockscreenVisibleness(slot, valueStr);
1160 }
1161 if (typeStr.compare(KEY_SLOT_SOUND) == 0) {
1162 return ParseSlotSound(slot, valueStr);
1163 }
1164 if (typeStr.compare(KEY_SLOT_VIBRATION_STYLE) == 0) {
1165 return ParseSlotVibrationSytle(slot, valueStr);
1166 }
1167 if (typeStr.compare(KEY_SLOT_ENABLE_BYPASS_DND) == 0) {
1168 return ParseSlotEnableBypassDnd(slot, valueStr);
1169 }
1170 if (typeStr.compare(KEY_SLOT_ENABLED) == 0) {
1171 return ParseSlotEnabled(slot, valueStr);
1172 }
1173 if (typeStr.compare(KEY_SLOT_SLOTFLGS_TYPE) == 0) {
1174 return ParseSlotFlags(slot, valueStr);
1175 }
1176 if (typeStr.compare(KEY_SLOT_AUTHORIZED_STATUS) == 0) {
1177 return ParseSlotAuthorizedStatus(slot, valueStr);
1178 }
1179 if (typeStr.compare(KEY_SLOT_AUTH_HINT_CNT) == 0) {
1180 return ParseSlotAuthHitnCnt(slot, valueStr);
1181 }
1182 ExecuteDisturbeDB(slot, typeStr, valueStr, findString, userId);
1183 }
1184
ExecuteDisturbeDB(sptr<NotificationSlot> & slot,std::string & typeStr,std::string & valueStr,const std::string & findString,const int32_t & userId)1185 void NotificationPreferencesDatabase::ExecuteDisturbeDB(sptr<NotificationSlot> &slot, std::string &typeStr,
1186 std::string &valueStr, const std::string &findString, const int32_t &userId)
1187 {
1188 if (typeStr.compare(KEY_REMINDER_MODE) == 0) {
1189 return ParseSlotReminderMode(slot, valueStr);
1190 }
1191 if (!typeStr.compare(KEY_SLOT_VIBRATION_STYLE)) {
1192 GetValueFromDisturbeDB(findString + KEY_SLOT_ENABLE_VRBRATION, userId,
1193 [&](std::string &value) { ParseSlotEnableVrbration(slot, value); });
1194 }
1195 }
1196
FindLastString(const std::string & findString,const std::string & inputString) const1197 std::string NotificationPreferencesDatabase::FindLastString(
1198 const std::string &findString, const std::string &inputString) const
1199 {
1200 std::string keyStr;
1201 size_t pos = findString.size();
1202 if (pos != std::string::npos) {
1203 keyStr = inputString.substr(pos);
1204 }
1205 return keyStr;
1206 }
1207
VectorToString(const std::vector<int64_t> & data) const1208 std::string NotificationPreferencesDatabase::VectorToString(const std::vector<int64_t> &data) const
1209 {
1210 std::stringstream streamStr;
1211 std::copy(data.begin(), data.end(), std::ostream_iterator<int>(streamStr, KEY_UNDER_LINE.c_str()));
1212 return streamStr.str();
1213 }
1214
StringToVector(const std::string & str,std::vector<int64_t> & data) const1215 void NotificationPreferencesDatabase::StringToVector(const std::string &str, std::vector<int64_t> &data) const
1216 {
1217 if (str.empty()) {
1218 return;
1219 }
1220
1221 if (str.find_first_of(KEY_UNDER_LINE) != std::string::npos) {
1222 std::string str1 = str.substr(0, str.find_first_of(KEY_UNDER_LINE));
1223 std::string afterStr = str.substr(str.find_first_of(KEY_UNDER_LINE) + 1);
1224 data.push_back(StringToInt(str1));
1225 StringToVector(afterStr, data);
1226 }
1227 }
1228
StringToInt(const std::string & str) const1229 int32_t NotificationPreferencesDatabase::StringToInt(const std::string &str) const
1230 {
1231 int32_t value = 0;
1232 if (!str.empty()) {
1233 value = atoi(str.c_str());
1234 }
1235 return value;
1236 }
1237
StringToInt64(const std::string & str) const1238 int64_t NotificationPreferencesDatabase::StringToInt64(const std::string &str) const
1239 {
1240 int64_t value = 0;
1241 if (!str.empty()) {
1242 value = atoll(str.c_str());
1243 }
1244 return value;
1245 }
1246
IsSlotKey(const std::string & bundleKey,const std::string & key) const1247 bool NotificationPreferencesDatabase::IsSlotKey(const std::string &bundleKey, const std::string &key) const
1248 {
1249 std::string tempStr = FindLastString(bundleKey, key);
1250 size_t pos = tempStr.find_first_of(KEY_UNDER_LINE);
1251 std::string slotStr;
1252 if (pos != std::string::npos) {
1253 slotStr = tempStr.substr(0, pos);
1254 }
1255 if (!slotStr.compare(KEY_SLOT)) {
1256 return true;
1257 }
1258 return false;
1259 }
1260
GenerateSlotKey(const std::string & bundleKey,const std::string & type,const std::string & subType) const1261 std::string NotificationPreferencesDatabase::GenerateSlotKey(
1262 const std::string &bundleKey, const std::string &type, const std::string &subType) const
1263 {
1264 /* slot key
1265 *
1266 * KEY_ANS_BUNDLE_bundlename_slot_type_0_id
1267 * KEY_ANS_BUNDLE_bundlename_slot_type_0_des
1268 * KEY_ANS_BUNDLE_bundlename_slot_type_1_id
1269 * KEY_ANS_BUNDLE_bundlename_slot_type_1_des
1270 *
1271 */
1272 std::string key = GenerateBundleKey(bundleKey).append(KEY_SLOT).append(KEY_UNDER_LINE).append(KEY_SLOT_TYPE);
1273 if (!type.empty()) {
1274 key.append(KEY_UNDER_LINE).append(type);
1275 }
1276 if (!subType.empty()) {
1277 key.append(KEY_UNDER_LINE).append(subType);
1278 }
1279 ANS_LOGD("Slot key is : %{public}s.", key.c_str());
1280 return key;
1281 }
1282
GenerateBundleKey(const std::string & bundleKey,const std::string & type) const1283 std::string NotificationPreferencesDatabase::GenerateBundleKey(
1284 const std::string &bundleKey, const std::string &type) const
1285 {
1286 /* bundle key
1287 *
1288 * label_KEY_ANS_KEY_BUNDLE_NAME = ""
1289 * KEY_ANS_BUNDLE_bundlename_
1290 * KEY_ANS_BUNDLE_bundlename_
1291 * KEY_ANS_BUNDLE_bundlename_
1292 * KEY_ANS_BUNDLE_bundlename_
1293 *
1294 */
1295 ANS_LOGD("%{public}s, bundleKey[%{public}s] type[%{public}s]", __FUNCTION__, bundleKey.c_str(), type.c_str());
1296 std::string key =
1297 std::string().append(KEY_ANS_BUNDLE).append(KEY_UNDER_LINE).append(bundleKey).append(KEY_UNDER_LINE);
1298 if (!type.empty()) {
1299 key.append(type);
1300 }
1301 ANS_LOGD("Bundle key : %{public}s.", key.c_str());
1302 return key;
1303 }
1304
SubUniqueIdentifyFromString(const std::string & findString,const std::string & keyStr) const1305 std::string NotificationPreferencesDatabase::SubUniqueIdentifyFromString(
1306 const std::string &findString, const std::string &keyStr) const
1307 {
1308 std::string slotType;
1309 std::string tempStr = FindLastString(findString, keyStr);
1310 size_t pos = tempStr.find_last_of(KEY_UNDER_LINE);
1311 if (pos != std::string::npos) {
1312 slotType = tempStr.substr(0, pos);
1313 }
1314
1315 return slotType;
1316 }
1317
ParseBundleName(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1318 void NotificationPreferencesDatabase::ParseBundleName(
1319 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1320 {
1321 ANS_LOGD("SetBundleName bundle name is %{public}s.", value.c_str());
1322 bundleInfo.SetBundleName(value);
1323 }
1324
ParseBundleImportance(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1325 void NotificationPreferencesDatabase::ParseBundleImportance(
1326 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1327 {
1328 ANS_LOGD("SetBundleImportance bundle importance is %{public}s.", value.c_str());
1329 bundleInfo.SetImportance(static_cast<NotificationSlot::NotificationLevel>(StringToInt(value)));
1330 }
1331
ParseBundleShowBadgeEnable(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1332 void NotificationPreferencesDatabase::ParseBundleShowBadgeEnable(
1333 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1334 {
1335 ANS_LOGD("SetBundleShowBadge bundle show badge is %{public}s.", value.c_str());
1336 bundleInfo.SetIsShowBadge(static_cast<bool>(StringToInt(value)));
1337 }
1338
ParseBundleBadgeNum(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1339 void NotificationPreferencesDatabase::ParseBundleBadgeNum(
1340 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1341 {
1342 ANS_LOGD("SetBundleBadgeNum bundle badge num is %{public}s.", value.c_str());
1343 bundleInfo.SetBadgeTotalNum(StringToInt(value));
1344 }
1345
ParseBundleEnableNotification(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1346 void NotificationPreferencesDatabase::ParseBundleEnableNotification(
1347 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1348 {
1349 ANS_LOGD("SetBundleEnableNotification bundle enable is %{public}s.", value.c_str());
1350 bundleInfo.SetEnableNotification(static_cast<bool>(StringToInt(value)));
1351 }
1352
ParseBundlePoppedDialog(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1353 void NotificationPreferencesDatabase::ParseBundlePoppedDialog(
1354 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1355 {
1356 ANS_LOGD("SetBundlePoppedDialog bundle has popped dialog is %{public}s.", value.c_str());
1357 bundleInfo.SetHasPoppedDialog(static_cast<bool>(StringToInt(value)));
1358 }
1359
ParseBundleUid(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1360 void NotificationPreferencesDatabase::ParseBundleUid(
1361 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1362 {
1363 ANS_LOGD("SetBundleUid uuid is %{public}s.", value.c_str());
1364 bundleInfo.SetBundleUid(StringToInt(value));
1365 }
1366
ParseSlotDescription(sptr<NotificationSlot> & slot,const std::string & value) const1367 void NotificationPreferencesDatabase::ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const
1368 {
1369 ANS_LOGD("ParseSlotDescription slot des is %{public}s.", value.c_str());
1370 std::string slotDescription = value;
1371 slot->SetDescription(slotDescription);
1372 }
1373
ParseSlotLevel(sptr<NotificationSlot> & slot,const std::string & value) const1374 void NotificationPreferencesDatabase::ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const
1375 {
1376 ANS_LOGD("ParseSlotLevel slot level is %{public}s.", value.c_str());
1377 NotificationSlot::NotificationLevel level = static_cast<NotificationSlot::NotificationLevel>(StringToInt(value));
1378 slot->SetLevel(level);
1379 }
1380
ParseSlotShowBadge(sptr<NotificationSlot> & slot,const std::string & value) const1381 void NotificationPreferencesDatabase::ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const
1382 {
1383 ANS_LOGD("ParseSlotShowBadge slot show badge is %{public}s.", value.c_str());
1384 bool showBadge = static_cast<bool>(StringToInt(value));
1385 slot->EnableBadge(showBadge);
1386 }
1387
ParseSlotFlags(sptr<NotificationSlot> & slot,const std::string & value) const1388 void NotificationPreferencesDatabase::ParseSlotFlags(sptr<NotificationSlot> &slot, const std::string &value) const
1389 {
1390 ANS_LOGD("ParseSlotFlags slot show flags is %{public}s.", value.c_str());
1391 uint32_t slotFlags = static_cast<uint32_t>(StringToInt(value));
1392 slot->SetSlotFlags(slotFlags);
1393 }
1394
ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1395 void NotificationPreferencesDatabase::ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo,
1396 const std::string &value) const
1397 {
1398 ANS_LOGD("ParseBundleSlotFlags slot show flags is %{public}s.", value.c_str());
1399 bundleInfo.SetSlotFlags(StringToInt(value));
1400 }
1401
ParseSlotEnableLight(sptr<NotificationSlot> & slot,const std::string & value) const1402 void NotificationPreferencesDatabase::ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const
1403 {
1404 ANS_LOGD("ParseSlotEnableLight slot enable light is %{public}s.", value.c_str());
1405 bool enableLight = static_cast<bool>(StringToInt(value));
1406 slot->SetEnableLight(enableLight);
1407 }
1408
ParseSlotEnableVrbration(sptr<NotificationSlot> & slot,const std::string & value) const1409 void NotificationPreferencesDatabase::ParseSlotEnableVrbration(
1410 sptr<NotificationSlot> &slot, const std::string &value) const
1411 {
1412 ANS_LOGD("ParseSlotEnableVrbration slot enable vir is %{public}s.", value.c_str());
1413 bool enableVrbration = static_cast<bool>(StringToInt(value));
1414 slot->SetEnableVibration(enableVrbration);
1415 }
1416
ParseSlotLedLightColor(sptr<NotificationSlot> & slot,const std::string & value) const1417 void NotificationPreferencesDatabase::ParseSlotLedLightColor(
1418 sptr<NotificationSlot> &slot, const std::string &value) const
1419 {
1420 ANS_LOGD("ParseSlotLedLightColor slot led is %{public}s.", value.c_str());
1421 int32_t ledLightColor = static_cast<int32_t>(StringToInt(value));
1422 slot->SetLedLightColor(ledLightColor);
1423 }
1424
ParseSlotLockscreenVisibleness(sptr<NotificationSlot> & slot,const std::string & value) const1425 void NotificationPreferencesDatabase::ParseSlotLockscreenVisibleness(
1426 sptr<NotificationSlot> &slot, const std::string &value) const
1427 {
1428
1429 ANS_LOGD("ParseSlotLockscreenVisibleness slot visible is %{public}s.", value.c_str());
1430 NotificationConstant::VisiblenessType visible =
1431 static_cast<NotificationConstant::VisiblenessType>(StringToInt(value));
1432 slot->SetLockscreenVisibleness(visible);
1433 }
1434
ParseSlotSound(sptr<NotificationSlot> & slot,const std::string & value) const1435 void NotificationPreferencesDatabase::ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const
1436 {
1437 ANS_LOGD("ParseSlotSound slot sound is %{public}s.", value.c_str());
1438 std::string slotUri = value;
1439 Uri uri(slotUri);
1440 slot->SetSound(uri);
1441 }
1442
ParseSlotVibrationSytle(sptr<NotificationSlot> & slot,const std::string & value) const1443 void NotificationPreferencesDatabase::ParseSlotVibrationSytle(
1444 sptr<NotificationSlot> &slot, const std::string &value) const
1445 {
1446 ANS_LOGD("ParseSlotVibrationSytle slot vibration style is %{public}s.", value.c_str());
1447 std::vector<int64_t> vibrationStyle;
1448 StringToVector(value, vibrationStyle);
1449 slot->SetVibrationStyle(vibrationStyle);
1450 }
1451
ParseSlotEnableBypassDnd(sptr<NotificationSlot> & slot,const std::string & value) const1452 void NotificationPreferencesDatabase::ParseSlotEnableBypassDnd(
1453 sptr<NotificationSlot> &slot, const std::string &value) const
1454 {
1455 ANS_LOGD("ParseSlotEnableBypassDnd slot by pass dnd is %{public}s.", value.c_str());
1456 bool enable = static_cast<bool>(StringToInt(value));
1457 slot->EnableBypassDnd(enable);
1458 }
1459
ParseSlotEnabled(sptr<NotificationSlot> & slot,const std::string & value) const1460 void NotificationPreferencesDatabase::ParseSlotEnabled(
1461 sptr<NotificationSlot> &slot, const std::string &value) const
1462 {
1463 ANS_LOGD("ParseSlotEnabled slot enabled is %{public}s.", value.c_str());
1464 bool enabled = static_cast<bool>(StringToInt(value));
1465 slot->SetEnable(enabled);
1466 }
1467
ParseSlotAuthorizedStatus(sptr<NotificationSlot> & slot,const std::string & value) const1468 void NotificationPreferencesDatabase::ParseSlotAuthorizedStatus(
1469 sptr<NotificationSlot> &slot, const std::string &value) const
1470 {
1471 ANS_LOGD("ParseSlotAuthorizedStatus slot status is %{public}s.", value.c_str());
1472 int32_t status = static_cast<int32_t>(StringToInt(value));
1473 slot->SetAuthorizedStatus(status);
1474 }
1475
ParseSlotAuthHitnCnt(sptr<NotificationSlot> & slot,const std::string & value) const1476 void NotificationPreferencesDatabase::ParseSlotAuthHitnCnt(
1477 sptr<NotificationSlot> &slot, const std::string &value) const
1478 {
1479 ANS_LOGD("ParseSlotAuthHitnCnt slot count is %{public}s.", value.c_str());
1480 int32_t count = static_cast<int32_t>(StringToInt(value));
1481 slot->SetAuthHintCnt(count);
1482 }
1483
ParseSlotReminderMode(sptr<NotificationSlot> & slot,const std::string & value) const1484 void NotificationPreferencesDatabase::ParseSlotReminderMode(
1485 sptr<NotificationSlot> &slot, const std::string &value) const
1486 {
1487 ANS_LOGD("ParseSlotReminderMode slot reminder mode is %{public}s.", value.c_str());
1488 int32_t reminderMode = static_cast<int32_t>(StringToInt(value));
1489 slot->SetReminderMode(reminderMode);
1490 }
1491
GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo & bundleInfo) const1492 std::string NotificationPreferencesDatabase::GenerateBundleLablel(
1493 const NotificationPreferencesInfo::BundleInfo &bundleInfo) const
1494 {
1495 return bundleInfo.GetBundleName().append(std::to_string(bundleInfo.GetBundleUid()));
1496 }
1497
GetDoNotDisturbType(NotificationPreferencesInfo & info,int32_t userId)1498 void NotificationPreferencesDatabase::GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId)
1499 {
1500 std::string key =
1501 std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1502 GetValueFromDisturbeDB(
1503 key, userId, [&](const int32_t &status, std::string &value) {
1504 sptr<NotificationDoNotDisturbDate> disturbDate = new (std::nothrow)
1505 NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1506 if (disturbDate == nullptr) {
1507 ANS_LOGE("Create NotificationDoNotDisturbDate instance fail.");
1508 return;
1509 }
1510 info.GetDoNotDisturbDate(userId, disturbDate);
1511 if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1512 PutDoNotDisturbDate(userId, disturbDate);
1513 } else if (status == NativeRdb::E_OK) {
1514 if (!value.empty()) {
1515 if (disturbDate != nullptr) {
1516 disturbDate->SetDoNotDisturbType(
1517 (NotificationConstant::DoNotDisturbType)StringToInt(value));
1518 }
1519 }
1520 } else {
1521 ANS_LOGW("Parse disturbe mode failed, use default value.");
1522 }
1523 info.SetDoNotDisturbDate(userId, disturbDate);
1524 });
1525 }
1526
GetDoNotDisturbBeginDate(NotificationPreferencesInfo & info,int32_t userId)1527 void NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId)
1528 {
1529 std::string key =
1530 std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1531 GetValueFromDisturbeDB(
1532 key, userId, [&](const int32_t &status, std::string &value) {
1533 sptr<NotificationDoNotDisturbDate> disturbDate = new (std::nothrow)
1534 NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1535 if (disturbDate == nullptr) {
1536 ANS_LOGE("Failed to create NotificationDoNotDisturbDate instance");
1537 return;
1538 }
1539 info.GetDoNotDisturbDate(userId, disturbDate);
1540 if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1541 PutDoNotDisturbDate(userId, disturbDate);
1542 } else if (status == NativeRdb::E_OK) {
1543 if (!value.empty()) {
1544 if (disturbDate != nullptr) {
1545 disturbDate->SetBeginDate(StringToInt64(value));
1546 }
1547 }
1548 } else {
1549 ANS_LOGW("Parse disturbe start time failed, use default value.");
1550 }
1551 info.SetDoNotDisturbDate(userId, disturbDate);
1552 });
1553 }
1554
GetDoNotDisturbEndDate(NotificationPreferencesInfo & info,int32_t userId)1555 void NotificationPreferencesDatabase::GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId)
1556 {
1557 std::string key =
1558 std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1559 GetValueFromDisturbeDB(
1560 key, userId, [&](const int32_t &status, std::string &value) {
1561 sptr<NotificationDoNotDisturbDate> disturbDate = new (std::nothrow)
1562 NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1563 if (disturbDate == nullptr) {
1564 ANS_LOGE("Defeat to create NotificationDoNotDisturbDate instance");
1565 return;
1566 }
1567 info.GetDoNotDisturbDate(userId, disturbDate);
1568 if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1569 PutDoNotDisturbDate(userId, disturbDate);
1570 } else if (status == NativeRdb::E_OK) {
1571 if (!value.empty()) {
1572 if (disturbDate != nullptr) {
1573 disturbDate->SetEndDate(StringToInt64(value));
1574 }
1575 }
1576 } else {
1577 ANS_LOGW("Parse disturbe end time failed, use default value.");
1578 }
1579 info.SetDoNotDisturbDate(userId, disturbDate);
1580 });
1581 }
1582
GetEnableAllNotification(NotificationPreferencesInfo & info,int32_t userId)1583 void NotificationPreferencesDatabase::GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId)
1584 {
1585 std::string key =
1586 std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
1587 GetValueFromDisturbeDB(
1588 key, userId, [&](const int32_t &status, std::string &value) {
1589 if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1590 bool enable = true;
1591 if (!info.GetEnabledAllNotification(userId, enable)) {
1592 info.SetEnabledAllNotification(userId, enable);
1593 ANS_LOGW("Enable setting not found, default true.");
1594 }
1595 PutNotificationsEnabled(userId, enable);
1596 } else if (status == NativeRdb::E_OK) {
1597 if (!value.empty()) {
1598 info.SetEnabledAllNotification(userId, static_cast<bool>(StringToInt(value)));
1599 }
1600 } else {
1601 ANS_LOGW("Parse enable all notification failed, use default value.");
1602 }
1603 });
1604 }
1605
GetDoNotDisturbProfile(NotificationPreferencesInfo & info,int32_t userId)1606 void NotificationPreferencesDatabase::GetDoNotDisturbProfile(NotificationPreferencesInfo &info, int32_t userId)
1607 {
1608 if (!CheckRdbStore()) {
1609 ANS_LOGE("RdbStore is nullptr.");
1610 return;
1611 }
1612 std::unordered_map<std::string, std::string> datas;
1613 int32_t result = rdbDataManager_->QueryAllData(datas, userId);
1614 if (result != NativeRdb::E_OK) {
1615 ANS_LOGE("Query all data failed.");
1616 return;
1617 }
1618 std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
1619 for (const auto &data : datas) {
1620 std::string key = data.first;
1621 auto result = key.find(KEY_DO_NOT_DISTURB_ID);
1622 if (result != std::string::npos) {
1623 sptr<NotificationDoNotDisturbProfile> profile;
1624 GetDoNotDisturbProfiles(data.first, profile, userId);
1625 profiles.emplace_back(profile);
1626 }
1627 }
1628 info.AddDoNotDisturbProfiles(userId, profiles);
1629 }
1630
RemoveNotificationEnable(const int32_t userId)1631 bool NotificationPreferencesDatabase::RemoveNotificationEnable(const int32_t userId)
1632 {
1633 ANS_LOGD("%{public}s", __FUNCTION__);
1634 if (!CheckRdbStore()) {
1635 ANS_LOGE("RdbStore is nullptr.");
1636 return false;
1637 }
1638
1639 std::string key =
1640 std::string(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
1641 int32_t result = rdbDataManager_->DeleteData(key, userId);
1642 if (result != NativeRdb::E_OK) {
1643 ANS_LOGE("delete bundle Info failed.");
1644 return false;
1645 }
1646
1647 ANS_LOGD("%{public}s remove notification enable, userId : %{public}d", __FUNCTION__, userId);
1648 return true;
1649 }
1650
RemoveDoNotDisturbDate(const int32_t userId)1651 bool NotificationPreferencesDatabase::RemoveDoNotDisturbDate(const int32_t userId)
1652 {
1653 ANS_LOGD("%{public}s", __FUNCTION__);
1654 if (!CheckRdbStore()) {
1655 ANS_LOGE("RdbStore is nullptr.");
1656 return false;
1657 }
1658
1659 std::string typeKey =
1660 std::string(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1661 std::string beginDateKey =
1662 std::string(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1663 std::string endDateKey =
1664 std::string(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1665
1666 std::vector<std::string> keys = {
1667 typeKey,
1668 beginDateKey,
1669 endDateKey
1670 };
1671
1672 int32_t result = rdbDataManager_->DeleteBathchData(keys, userId);
1673 if (result != NativeRdb::E_OK) {
1674 ANS_LOGE("delete DoNotDisturb date failed.");
1675 return false;
1676 }
1677
1678 ANS_LOGD("%{public}s remove DoNotDisturb date, userId : %{public}d", __FUNCTION__, userId);
1679 return true;
1680 }
1681
RemoveAnsBundleDbInfo(std::string bundleName,int32_t uid)1682 bool NotificationPreferencesDatabase::RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid)
1683 {
1684 if (!CheckRdbStore()) {
1685 ANS_LOGE("RdbStore is nullptr.");
1686 return false;
1687 }
1688
1689 std::string key = KEY_BUNDLE_LABEL + bundleName + std::to_string(uid);
1690 int32_t userId = -1;
1691 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(uid, userId);
1692 int32_t result = rdbDataManager_->DeleteData(key, userId);
1693 if (result != NativeRdb::E_OK) {
1694 ANS_LOGE("Delete ans bundle db info failed, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid);
1695 return false;
1696 }
1697
1698 ANS_LOGE("Remove ans bundle db info, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid);
1699 return true;
1700 }
1701
RemoveEnabledDbByBundleName(std::string bundleName,const int32_t & bundleUid)1702 bool NotificationPreferencesDatabase::RemoveEnabledDbByBundleName(std::string bundleName, const int32_t &bundleUid)
1703 {
1704 if (!CheckRdbStore()) {
1705 ANS_LOGE("RdbStore is nullptr.");
1706 return false;
1707 }
1708 int32_t userId = -1;
1709 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
1710 std::string key = std::string(KEY_ENABLE_BUNDLE_DISTRIBUTED_NOTIFICATION).append(
1711 KEY_MIDDLE_LINE).append(std::string(bundleName).append(KEY_MIDDLE_LINE));
1712 ANS_LOGD("key is %{public}s", key.c_str());
1713 int32_t result = NativeRdb::E_OK;
1714 std::unordered_map<std::string, std::string> values;
1715 result = rdbDataManager_->QueryDataBeginWithKey(key, values, userId);
1716 if (result == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1717 return true;
1718 } else if (result != NativeRdb::E_OK) {
1719 ANS_LOGE("Get failed, key %{public}s,result %{public}d.", key.c_str(), result);
1720 return NativeRdb::E_ERROR;
1721 }
1722
1723 std::vector<std::string> keys;
1724 for (auto iter : values) {
1725 ANS_LOGD("Get failed, key %{public}s", iter.first.c_str());
1726 keys.push_back(iter.first);
1727 }
1728
1729 result = rdbDataManager_->DeleteBathchData(keys, userId);
1730 if (result != NativeRdb::E_OK) {
1731 ANS_LOGE("delete bundle Info failed.");
1732 return false;
1733 }
1734
1735 return true;
1736 }
1737
SetKvToDb(const std::string & key,const std::string & value,const int32_t & userId)1738 int32_t NotificationPreferencesDatabase::SetKvToDb(
1739 const std::string &key, const std::string &value, const int32_t &userId)
1740 {
1741 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_2);
1742 if (!CheckRdbStore()) {
1743 ANS_LOGE("RdbStore is nullptr.");
1744 message.Message("RdbStore is nullptr.");
1745 NotificationAnalyticsUtil::ReportModifyEvent(message);
1746 return NativeRdb::E_ERROR;
1747 }
1748 int32_t result = rdbDataManager_->InsertData(key, value, userId);
1749 if (result != NativeRdb::E_OK) {
1750 message.Message("Set key failed: " + key);
1751 NotificationAnalyticsUtil::ReportModifyEvent(message);
1752 ANS_LOGE("Set key: %{public}s failed, result %{public}d.", key.c_str(), result);
1753 return NativeRdb::E_ERROR;
1754 }
1755
1756 ANS_LOGD("Key:%{public}s, value:%{public}s.", key.c_str(), value.c_str());
1757
1758 return NativeRdb::E_OK;
1759 }
1760
SetByteToDb(const std::string & key,const std::vector<uint8_t> & value,const int32_t & userId)1761 int32_t NotificationPreferencesDatabase::SetByteToDb(
1762 const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId)
1763 {
1764 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_2);
1765 if (!CheckRdbStore()) {
1766 message.Message("RdbStore is nullptr.");
1767 NotificationAnalyticsUtil::ReportModifyEvent(message);
1768 ANS_LOGE("RdbStore is nullptr.");
1769 return NativeRdb::E_ERROR;
1770 }
1771 int32_t result = rdbDataManager_->InsertData(key, value, userId);
1772 if (result != NativeRdb::E_OK) {
1773 message.Message("Set key failed: " + key);
1774 NotificationAnalyticsUtil::ReportModifyEvent(message);
1775 ANS_LOGE("Set key: %{public}s failed, result %{public}d.", key.c_str(), result);
1776 return NativeRdb::E_ERROR;
1777 }
1778
1779 return NativeRdb::E_OK;
1780 }
1781
GetKvFromDb(const std::string & key,std::string & value,const int32_t & userId)1782 int32_t NotificationPreferencesDatabase::GetKvFromDb(
1783 const std::string &key, std::string &value, const int32_t &userId)
1784 {
1785 if (!CheckRdbStore()) {
1786 ANS_LOGE("RdbStore is nullptr.");
1787 return NativeRdb::E_ERROR;
1788 }
1789
1790 int32_t result = rdbDataManager_->QueryData(key, value, userId);
1791 if (result != NativeRdb::E_OK) {
1792 ANS_LOGE("Get key-value failed, key %{public}s, result %{pubic}d.", key.c_str(), result);
1793 return NativeRdb::E_ERROR;
1794 }
1795
1796 ANS_LOGD("Key:%{public}s, value:%{public}s.", key.c_str(), value.c_str());
1797
1798 return NativeRdb::E_OK;
1799 }
1800
GetByteFromDb(const std::string & key,std::vector<uint8_t> & value,const int32_t & userId)1801 int32_t NotificationPreferencesDatabase::GetByteFromDb(
1802 const std::string &key, std::vector<uint8_t> &value, const int32_t &userId)
1803 {
1804 if (!CheckRdbStore()) {
1805 ANS_LOGE("RdbStore is nullptr.");
1806 return NativeRdb::E_ERROR;
1807 }
1808
1809 int32_t result = rdbDataManager_->QueryData(key, value, userId);
1810 if (result != NativeRdb::E_OK) {
1811 ANS_LOGE("Get byte failed, key %{public}s, result %{pubic}d.", key.c_str(), result);
1812 return NativeRdb::E_ERROR;
1813 }
1814
1815 return NativeRdb::E_OK;
1816 }
1817
GetBatchKvsFromDb(const std::string & key,std::unordered_map<std::string,std::string> & values,const int32_t & userId)1818 int32_t NotificationPreferencesDatabase::GetBatchKvsFromDb(
1819 const std::string &key, std::unordered_map<std::string, std::string> &values, const int32_t &userId)
1820 {
1821 if (!CheckRdbStore()) {
1822 ANS_LOGE("RdbStore is nullptr.");
1823 return NativeRdb::E_ERROR;
1824 }
1825
1826 int32_t result = rdbDataManager_->QueryDataBeginWithKey(key, values, userId);
1827 if (result != NativeRdb::E_OK) {
1828 ANS_LOGE("Get batch notification request failed, key %{public}s, result %{public}d.", key.c_str(), result);
1829 return NativeRdb::E_ERROR;
1830 }
1831 ANS_LOGD("Key:%{public}s.", key.c_str());
1832 return NativeRdb::E_OK;
1833 }
1834
DeleteKvFromDb(const std::string & key,const int32_t & userId)1835 int32_t NotificationPreferencesDatabase::DeleteKvFromDb(const std::string &key, const int32_t &userId)
1836 {
1837 if (!CheckRdbStore()) {
1838 ANS_LOGE("RdbStore is nullptr.");
1839 return NativeRdb::E_ERROR;
1840 }
1841
1842 int32_t result = rdbDataManager_->DeleteData(key, userId);
1843 if (result != NativeRdb::E_OK) {
1844 ANS_LOGE("Delete key-value failed, key %{public}s, result %{public}d.", key.c_str(), result);
1845 return NativeRdb::E_ERROR;
1846 }
1847
1848 ANS_LOGD("Delete key:%{public}s.", key.c_str());
1849
1850 return NativeRdb::E_OK;
1851 }
1852
DeleteBatchKvFromDb(const std::vector<std::string> & keys,const int32_t & userId)1853 int32_t NotificationPreferencesDatabase::DeleteBatchKvFromDb(const std::vector<std::string> &keys,
1854 const int32_t &userId)
1855 {
1856 if (!CheckRdbStore()) {
1857 ANS_LOGE("RdbStore is nullptr.");
1858 return NativeRdb::E_ERROR;
1859 }
1860
1861 int32_t result = rdbDataManager_->DeleteBathchData(keys, userId);
1862 if (result != NativeRdb::E_OK) {
1863 ANS_LOGE("Delete key-value failed, result %{public}d.", result);
1864 return NativeRdb::E_ERROR;
1865 }
1866
1867 return NativeRdb::E_OK;
1868 }
1869
DropUserTable(const int32_t userId)1870 int32_t NotificationPreferencesDatabase::DropUserTable(const int32_t userId)
1871 {
1872 if (!CheckRdbStore()) {
1873 ANS_LOGE("RdbStore is nullptr.");
1874 return NativeRdb::E_ERROR;
1875 }
1876
1877 int32_t result = rdbDataManager_->DropUserTable(userId);
1878 if (result != NativeRdb::E_OK) {
1879 ANS_LOGE("Delete table failed, result %{public}d.", result);
1880 return NativeRdb::E_ERROR;
1881 }
1882 return NativeRdb::E_OK;
1883 }
1884
IsAgentRelationship(const std::string & agentBundleName,const std::string & sourceBundleName)1885 bool NotificationPreferencesDatabase::IsAgentRelationship(const std::string &agentBundleName,
1886 const std::string &sourceBundleName)
1887 {
1888 if (!CheckRdbStore()) {
1889 ANS_LOGE("RdbStore is nullptr.");
1890 return false;
1891 }
1892 std::string agentShip = "";
1893 int32_t result = rdbDataManager_->QueryData("PROXY_PKG", agentShip);
1894 if (result != NativeRdb::E_OK) {
1895 ANS_LOGE("Query agent relationships failed.");
1896 return false;
1897 }
1898 ANS_LOGD("The agent relationship is :%{public}s.", agentShip.c_str());
1899 nlohmann::json jsonAgentShip = nlohmann::json::parse(agentShip, nullptr, false);
1900 if (jsonAgentShip.is_null() || jsonAgentShip.empty()) {
1901 ANS_LOGE("Invalid JSON object");
1902 return false;
1903 }
1904 if (jsonAgentShip.is_discarded() || !jsonAgentShip.is_array()) {
1905 ANS_LOGE("Parse agent ship failed due to data is discarded or not array");
1906 return false;
1907 }
1908
1909 nlohmann::json jsonTarget;
1910 jsonTarget[RELATIONSHIP_JSON_KEY_SERVICE] = agentBundleName;
1911 jsonTarget[RELATIONSHIP_JSON_KEY_APP] = sourceBundleName;
1912 bool isAgentRelationship = false;
1913 for (const auto &item : jsonAgentShip) {
1914 if (jsonTarget == item) {
1915 isAgentRelationship = true;
1916 break;
1917 }
1918 }
1919
1920 return isAgentRelationship;
1921 }
1922
PutDistributedEnabledForBundle(const std::string deviceType,const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & enabled)1923 bool NotificationPreferencesDatabase::PutDistributedEnabledForBundle(const std::string deviceType,
1924 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled)
1925 {
1926 ANS_LOGD("%{public}s, deviceType:%{public}s,enabled[%{public}d]", __FUNCTION__, deviceType.c_str(), enabled);
1927 if (bundleInfo.GetBundleName().empty()) {
1928 ANS_LOGE("Bundle name is null.");
1929 return false;
1930 }
1931 int32_t userId = -1;
1932 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
1933
1934 std::string key = GenerateBundleLablel(bundleInfo, deviceType);
1935 int32_t result = PutDataToDB(key, enabled, userId);
1936 ANS_LOGD("result[%{public}d]", result);
1937 return (result == NativeRdb::E_OK);
1938 }
1939
GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & deviceType) const1940 std::string NotificationPreferencesDatabase::GenerateBundleLablel(
1941 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &deviceType) const
1942 {
1943 return std::string(KEY_ENABLE_BUNDLE_DISTRIBUTED_NOTIFICATION).append(KEY_MIDDLE_LINE).append(
1944 std::string(bundleInfo.GetBundleName()).append(KEY_MIDDLE_LINE).append(std::to_string(
1945 bundleInfo.GetBundleUid())).append(KEY_MIDDLE_LINE).append(deviceType));
1946 }
1947
1948 template <typename T>
PutDataToDB(const std::string & key,const T & value,const int32_t & userId)1949 int32_t NotificationPreferencesDatabase::PutDataToDB(const std::string &key, const T &value, const int32_t &userId)
1950 {
1951 if (!CheckRdbStore()) {
1952 ANS_LOGE("RdbStore is nullptr.");
1953 return false;
1954 }
1955 std::string valueStr = std::to_string(value);
1956 int32_t result = rdbDataManager_->InsertData(key, valueStr, userId);
1957 return result;
1958 }
1959
GetDistributedEnabledForBundle(const std::string deviceType,const NotificationPreferencesInfo::BundleInfo & bundleInfo,bool & enabled)1960 bool NotificationPreferencesDatabase::GetDistributedEnabledForBundle(const std::string deviceType,
1961 const NotificationPreferencesInfo::BundleInfo &bundleInfo, bool &enabled)
1962 {
1963 ANS_LOGD("%{public}s, deviceType:%{public}s,enabled[%{public}d]", __FUNCTION__, deviceType.c_str(), enabled);
1964 if (bundleInfo.GetBundleName().empty()) {
1965 ANS_LOGE("Bundle name is null.");
1966 return false;
1967 }
1968
1969 std::string key = GenerateBundleLablel(bundleInfo, deviceType);
1970 bool result = false;
1971 enabled = false;
1972 int32_t userId = -1;
1973 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
1974 GetValueFromDisturbeDB(key, userId, [&](const int32_t &status, std::string &value) {
1975 switch (status) {
1976 case NativeRdb::E_EMPTY_VALUES_BUCKET: {
1977 result = true;
1978 enabled = false;
1979 break;
1980 }
1981 case NativeRdb::E_OK: {
1982 result = true;
1983 enabled = static_cast<bool>(StringToInt(value));
1984 break;
1985 }
1986 default:
1987 result = false;
1988 break;
1989 }
1990 });
1991 ANS_LOGD("GetDistributedEnabledForBundle:enabled:[%{public}d]KEY:%{public}s", enabled, key.c_str());
1992 return result;
1993 }
1994
GenerateBundleLablel(const std::string & deviceType,const int32_t userId) const1995 std::string NotificationPreferencesDatabase::GenerateBundleLablel(const std::string &deviceType,
1996 const int32_t userId) const
1997 {
1998 return std::string(KEY_SMART_REMINDER_ENABLE_NOTIFICATION).append(KEY_MIDDLE_LINE).append(
1999 deviceType).append(KEY_MIDDLE_LINE).append(std::to_string(userId));
2000 }
2001
2002
SetSmartReminderEnabled(const std::string deviceType,const bool & enabled)2003 bool NotificationPreferencesDatabase::SetSmartReminderEnabled(const std::string deviceType, const bool &enabled)
2004 {
2005 ANS_LOGD("%{public}s, deviceType:%{public}s,enabled[%{public}d]", __FUNCTION__, deviceType.c_str(), enabled);
2006 int32_t userId = SUBSCRIBE_USER_INIT;
2007 OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
2008 if (userId == SUBSCRIBE_USER_INIT) {
2009 ANS_LOGE("Current user acquisition failed");
2010 return false;
2011 }
2012
2013 std::string key = GenerateBundleLablel(deviceType, userId);
2014 ANS_LOGD("%{public}s, key:%{public}s,enabled[%{public}d]", __FUNCTION__, key.c_str(), enabled);
2015 int32_t result = PutDataToDB(key, enabled, userId);
2016 return (result == NativeRdb::E_OK);
2017 }
2018
IsSmartReminderEnabled(const std::string deviceType,bool & enabled)2019 bool NotificationPreferencesDatabase::IsSmartReminderEnabled(const std::string deviceType, bool &enabled)
2020 {
2021 ANS_LOGD("%{public}s, deviceType:%{public}s,enabled[%{public}d]", __FUNCTION__, deviceType.c_str(), enabled);
2022 int32_t userId = SUBSCRIBE_USER_INIT;
2023 OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
2024 if (userId == SUBSCRIBE_USER_INIT) {
2025 ANS_LOGE("Current user acquisition failed");
2026 return false;
2027 }
2028
2029 std::string key = GenerateBundleLablel(deviceType, userId);
2030 bool result = false;
2031 enabled = false;
2032 GetValueFromDisturbeDB(key, userId, [&](const int32_t &status, std::string &value) {
2033 switch (status) {
2034 case NativeRdb::E_EMPTY_VALUES_BUCKET: {
2035 result = true;
2036 enabled = false;
2037 break;
2038 }
2039 case NativeRdb::E_OK: {
2040 result = true;
2041 enabled = static_cast<bool>(StringToInt(value));
2042 break;
2043 }
2044 default:
2045 result = false;
2046 break;
2047 }
2048 });
2049 return result;
2050 }
2051
GetAdditionalConfig(const std::string & key)2052 std::string NotificationPreferencesDatabase::GetAdditionalConfig(const std::string &key)
2053 {
2054 if (!CheckRdbStore()) {
2055 ANS_LOGE("RdbStore is nullptr.");
2056 return "";
2057 }
2058 std::string configValue = "";
2059 int32_t result = rdbDataManager_->QueryData(key, configValue);
2060 if (result != NativeRdb::E_OK) {
2061 ANS_LOGE("Query additional config failed.");
2062 return "";
2063 }
2064 ANS_LOGD("The additional config key is :%{public}s, value is :%{public}s.", key.c_str(), configValue.c_str());
2065 return configValue;
2066 }
2067
CheckApiCompatibility(const std::string & bundleName,const int32_t & uid)2068 bool NotificationPreferencesDatabase::CheckApiCompatibility(const std::string &bundleName, const int32_t &uid)
2069 {
2070 ANS_LOGD("%{public}s", __FUNCTION__);
2071 std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
2072 if (bundleManager == nullptr) {
2073 return false;
2074 }
2075 return bundleManager->CheckApiCompatibility(bundleName, uid);
2076 }
2077
UpdateBundlePropertyToDisturbeDB(int32_t userId,const NotificationPreferencesInfo::BundleInfo & bundleInfo)2078 bool NotificationPreferencesDatabase::UpdateBundlePropertyToDisturbeDB(int32_t userId,
2079 const NotificationPreferencesInfo::BundleInfo &bundleInfo)
2080 {
2081 if (bundleInfo.GetBundleName().empty()) {
2082 ANS_LOGE("Bundle name is null.");
2083 return false;
2084 }
2085
2086 if (!CheckRdbStore()) {
2087 ANS_LOGE("RdbStore is nullptr.");
2088 return false;
2089 }
2090 std::string value;
2091 std::string bundleLabelKey = KEY_BUNDLE_LABEL + GenerateBundleLablel(bundleInfo);
2092 int32_t result = rdbDataManager_->QueryData(bundleLabelKey, value, userId);
2093 if (result == NativeRdb::E_EMPTY_VALUES_BUCKET) {
2094 if (rdbDataManager_->InsertData(bundleLabelKey, GenerateBundleLablel(bundleInfo), userId)
2095 != NativeRdb::E_OK) {
2096 ANS_LOGE("Store bundle name %{public}s to db is failed.", bundleLabelKey.c_str());
2097 return false;
2098 }
2099 }
2100 if (result == NativeRdb::E_EMPTY_VALUES_BUCKET || result == NativeRdb::E_OK) {
2101 return PutBundlePropertyValueToDisturbeDB(bundleInfo);
2102 }
2103 ANS_LOGW("Query bundle name %{public}s failed %{public}d.", bundleLabelKey.c_str(), result);
2104 return false;
2105 }
2106
UpdateBundleSlotToDisturbeDB(int32_t userId,const std::string & bundleName,const int32_t & bundleUid,const std::vector<sptr<NotificationSlot>> & slots)2107 bool NotificationPreferencesDatabase::UpdateBundleSlotToDisturbeDB(int32_t userId, const std::string &bundleName,
2108 const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots)
2109 {
2110 if (bundleName.empty()) {
2111 ANS_LOGE("Bundle name is null.");
2112 return false;
2113 }
2114 if (slots.empty()) {
2115 ANS_LOGI("Slot is empty.");
2116 return true;
2117 }
2118
2119 std::string bundleKey = bundleName + std::to_string(bundleUid);
2120 std::unordered_map<std::string, std::string> values;
2121 for (auto& slot : slots) {
2122 GenerateSlotEntry(bundleKey, slot, values);
2123 }
2124 if (!CheckRdbStore()) {
2125 ANS_LOGE("RdbStore is nullptr.");
2126 return false;
2127 }
2128 int32_t result = rdbDataManager_->InsertBatchData(values, userId);
2129 return (result == NativeRdb::E_OK);
2130 }
2131
DelCloneProfileInfo(const int32_t & userId,const sptr<NotificationDoNotDisturbProfile> & info)2132 bool NotificationPreferencesDatabase::DelCloneProfileInfo(const int32_t &userId,
2133 const sptr<NotificationDoNotDisturbProfile>& info)
2134 {
2135 if (!CheckRdbStore()) {
2136 ANS_LOGE("RdbStore is nullptr.");
2137 return false;
2138 }
2139
2140 std::string key = KEY_CLONE_LABEL + CLONE_PROFILE + std::to_string(info->GetProfileId());
2141 int32_t result = rdbDataManager_->DeleteData(key, userId);
2142 if (result != NativeRdb::E_OK) {
2143 ANS_LOGE("delete clone profile Info failed.");
2144 return false;
2145 }
2146 return true;
2147 }
2148
DelBatchCloneProfileInfo(const int32_t & userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> & profileInfo)2149 bool NotificationPreferencesDatabase::DelBatchCloneProfileInfo(const int32_t &userId,
2150 const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo)
2151 {
2152 std::string cloneProfile = KEY_CLONE_LABEL + CLONE_PROFILE;
2153 if (!CheckRdbStore()) {
2154 ANS_LOGE("RdbStore is nullptr.");
2155 return false;
2156 }
2157
2158 std::vector<std::string> keys;
2159 for (auto info : profileInfo) {
2160 std::string key = cloneProfile + std::to_string(info->GetProfileId());
2161 keys.emplace_back(key);
2162 }
2163
2164 int32_t result = rdbDataManager_->DeleteBathchData(keys, userId);
2165 if (result != NativeRdb::E_OK) {
2166 ANS_LOGE("delete clone bundle Info failed.");
2167 return false;
2168 }
2169 return true;
2170 }
2171
UpdateBatchCloneProfileInfo(const int32_t & userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> & profileInfo)2172 bool NotificationPreferencesDatabase::UpdateBatchCloneProfileInfo(const int32_t &userId,
2173 const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo)
2174 {
2175 std::string cloneProfile = KEY_CLONE_LABEL + CLONE_PROFILE;
2176 std::unordered_map<std::string, std::string> values;
2177 for (auto& info : profileInfo) {
2178 std::string key = cloneProfile + std::to_string(info->GetProfileId());
2179 std::string jsonString = info->ToJson();
2180 values.emplace(key, jsonString);
2181 }
2182 return UpdateCloneToDisturbeDB(userId, values);
2183 }
2184
GetAllCloneProfileInfo(const int32_t & userId,std::vector<sptr<NotificationDoNotDisturbProfile>> & profilesInfo)2185 void NotificationPreferencesDatabase::GetAllCloneProfileInfo(const int32_t &userId,
2186 std::vector<sptr<NotificationDoNotDisturbProfile>>& profilesInfo)
2187 {
2188 std::string cloneProfile = KEY_CLONE_LABEL + CLONE_PROFILE;
2189 std::unordered_map<std::string, std::string> values;
2190 if (GetBatchKvsFromDb(cloneProfile, values, userId) != ERR_OK) {
2191 ANS_LOGW("Get clone bundle map info failed %{public}d.", userId);
2192 return;
2193 }
2194
2195 for (auto item : values) {
2196 sptr<NotificationDoNotDisturbProfile> profile = new (std::nothrow) NotificationDoNotDisturbProfile();
2197 profile->FromJson(item.second);
2198 profilesInfo.push_back(profile);
2199 }
2200 }
2201
GetAllCloneBundleInfo(const int32_t & userId,std::vector<NotificationCloneBundleInfo> & cloneBundleInfo)2202 void NotificationPreferencesDatabase::GetAllCloneBundleInfo(const int32_t &userId,
2203 std::vector<NotificationCloneBundleInfo>& cloneBundleInfo)
2204 {
2205 std::unordered_map<std::string, std::string> values;
2206 if (GetBatchKvsFromDb(KEY_CLONE_LABEL + CLONE_BUNDLE, values, userId) != ERR_OK) {
2207 ANS_LOGW("Get clone bundle map info failed %{public}d.", userId);
2208 return;
2209 }
2210
2211 for (auto item : values) {
2212 NotificationCloneBundleInfo bundleInfo;
2213 nlohmann::json jsonObject = nlohmann::json::parse(item.second, nullptr, false);
2214 if (jsonObject.is_null() || !jsonObject.is_object()) {
2215 ANS_LOGE("Invalid JSON object");
2216 continue;
2217 }
2218 bundleInfo.FromJson(jsonObject);
2219 cloneBundleInfo.emplace_back(bundleInfo);
2220 }
2221 }
2222
DelBatchCloneBundleInfo(const int32_t & userId,const std::vector<NotificationCloneBundleInfo> & cloneBundleInfo)2223 bool NotificationPreferencesDatabase::DelBatchCloneBundleInfo(const int32_t &userId,
2224 const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo)
2225 {
2226 std::string cloneBundle = KEY_CLONE_LABEL + CLONE_BUNDLE;
2227 if (!CheckRdbStore()) {
2228 ANS_LOGE("RdbStore is nullptr.");
2229 return false;
2230 }
2231
2232 std::vector<std::string> keys;
2233 for (auto bundleInfo : cloneBundleInfo) {
2234 std::string key = cloneBundle + bundleInfo.GetBundleName() +
2235 std::to_string(bundleInfo.GetAppIndex());
2236 keys.emplace_back(key);
2237 }
2238 int32_t result = rdbDataManager_->DeleteBathchData(keys, userId);
2239 if (result != NativeRdb::E_OK) {
2240 ANS_LOGE("delete clone bundle Info failed.");
2241 return false;
2242 }
2243 return true;
2244 }
2245
UpdateBatchCloneBundleInfo(const int32_t & userId,const std::vector<NotificationCloneBundleInfo> & cloneBundleInfo)2246 bool NotificationPreferencesDatabase::UpdateBatchCloneBundleInfo(const int32_t &userId,
2247 const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo)
2248 {
2249 std::string cloneBundle = KEY_CLONE_LABEL + CLONE_BUNDLE;
2250 std::unordered_map<std::string, std::string> values;
2251 for (auto& info : cloneBundleInfo) {
2252 nlohmann::json jsonNode;
2253 std::string key = cloneBundle + info.GetBundleName() + std::to_string(info.GetAppIndex());
2254 info.ToJson(jsonNode);
2255 values.emplace(key, jsonNode.dump());
2256 }
2257 return UpdateCloneToDisturbeDB(userId, values);
2258 }
2259
DelCloneBundleInfo(const int32_t & userId,const NotificationCloneBundleInfo & cloneBundleInfo)2260 bool NotificationPreferencesDatabase::DelCloneBundleInfo(const int32_t &userId,
2261 const NotificationCloneBundleInfo& cloneBundleInfo)
2262 {
2263 std::string cloneBundle = KEY_CLONE_LABEL + CLONE_BUNDLE;
2264 std::string key = cloneBundle + cloneBundleInfo.GetBundleName() +
2265 std::to_string(cloneBundleInfo.GetAppIndex());
2266 if (!CheckRdbStore()) {
2267 ANS_LOGE("RdbStore is nullptr.");
2268 return false;
2269 }
2270
2271 int32_t result = rdbDataManager_->DeleteData(key, userId);
2272 if (result != NativeRdb::E_OK) {
2273 ANS_LOGE("delete clone bundle Info failed.");
2274 return false;
2275 }
2276 return true;
2277 }
2278
UpdateCloneToDisturbeDB(const int32_t & userId,const std::unordered_map<std::string,std::string> values)2279 bool NotificationPreferencesDatabase::UpdateCloneToDisturbeDB(const int32_t &userId,
2280 const std::unordered_map<std::string, std::string> values)
2281 {
2282 if (values.empty() || !CheckRdbStore()) {
2283 ANS_LOGE("RdbStore is nullptr.");
2284 return false;
2285 }
2286
2287 int32_t result = rdbDataManager_->InsertBatchData(values, userId);
2288 return (result == NativeRdb::E_OK);
2289 }
2290
SetHashCodeRule(const int32_t uid,const uint32_t type)2291 bool NotificationPreferencesDatabase::SetHashCodeRule(const int32_t uid, const uint32_t type)
2292 {
2293 ANS_LOGD("%{public}s, %{public}d,", __FUNCTION__, type);
2294 int32_t userId = SUBSCRIBE_USER_INIT;
2295 OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
2296 ANS_LOGI("SetHashCodeRule userId = %{public}d", userId);
2297 if (userId == SUBSCRIBE_USER_INIT) {
2298 ANS_LOGE("Current user acquisition failed");
2299 return false;
2300 }
2301
2302 std::string key = GenerateHashCodeGenerate(uid);
2303 ANS_LOGD("%{public}s, key:%{public}s,type = %{public}d", __FUNCTION__, key.c_str(), type);
2304 int32_t result = PutDataToDB(key, type, userId);
2305 return (result == NativeRdb::E_OK);
2306 }
2307
GetHashCodeRule(const int32_t uid)2308 uint32_t NotificationPreferencesDatabase::GetHashCodeRule(const int32_t uid)
2309 {
2310 ANS_LOGD("%{public}s, %{public}d,", __FUNCTION__, uid);
2311 int32_t userId = SUBSCRIBE_USER_INIT;
2312 OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
2313 if (userId == SUBSCRIBE_USER_INIT) {
2314 ANS_LOGE("Current user acquisition failed");
2315 return 0;
2316 }
2317
2318 std::string key = GenerateHashCodeGenerate(uid);
2319 ANS_LOGD("%{public}s, key:%{public}s", __FUNCTION__, key.c_str());
2320 uint32_t result = 0;
2321 GetValueFromDisturbeDB(key, userId, [&](const int32_t &status, std::string &value) {
2322 switch (status) {
2323 case NativeRdb::E_EMPTY_VALUES_BUCKET: {
2324 break;
2325 }
2326 case NativeRdb::E_OK: {
2327 result = StringToInt(value);
2328 break;
2329 }
2330 default:
2331 break;
2332 }
2333 });
2334 return result;
2335 }
2336
GenerateHashCodeGenerate(const int32_t uid)2337 std::string NotificationPreferencesDatabase::GenerateHashCodeGenerate(const int32_t uid)
2338 {
2339 return std::string(KEY_HASH_CODE_RULE).append(KEY_MIDDLE_LINE).append(std::to_string(uid));
2340 }
2341 } // namespace Notification
2342 } // namespace OHOS
2343