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_BUNDLE_DISTRIBUTED_ENABLE_NOTIFICATION = "enabledNotificationDistributed";
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
NotificationPreferencesDatabase()226 NotificationPreferencesDatabase::NotificationPreferencesDatabase()
227 {
228 NotificationRdbConfig notificationRdbConfig;
229 rdbDataManager_ = std::make_shared<NotificationDataMgr>(notificationRdbConfig);
230 ANS_LOGD("Notification Rdb is created");
231 }
232
~NotificationPreferencesDatabase()233 NotificationPreferencesDatabase::~NotificationPreferencesDatabase()
234 {
235 ANS_LOGD("Notification Rdb is deleted");
236 }
237
CheckRdbStore()238 bool NotificationPreferencesDatabase::CheckRdbStore()
239 {
240 if (rdbDataManager_ != nullptr) {
241 int32_t result = rdbDataManager_->Init();
242 if (result == NativeRdb::E_OK) {
243 return true;
244 }
245 }
246
247 return false;
248 }
249
PutSlotsToDisturbeDB( const std::string &bundleName, const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots)250 bool NotificationPreferencesDatabase::PutSlotsToDisturbeDB(
251 const std::string &bundleName, const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots)
252 {
253 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
254 ANS_LOGD("%{public}s", __FUNCTION__);
255 if (bundleName.empty()) {
256 ANS_LOGE("Bundle name is null.");
257 return false;
258 }
259
260 if (slots.empty()) {
261 ANS_LOGE("Slot is empty.");
262 return false;
263 }
264
265 std::unordered_map<std::string, std::string> values;
266 for (auto iter : slots) {
267 bool result = SlotToEntry(bundleName, bundleUid, iter, values);
268 if (!result) {
269 return result;
270 }
271 }
272 int32_t userId = -1;
273 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
274 if (!CheckRdbStore()) {
275 ANS_LOGE("RdbStore is nullptr.");
276 return false;
277 }
278 int32_t result = rdbDataManager_->InsertBatchData(values, userId);
279 return (result == NativeRdb::E_OK);
280 }
281
PutBundlePropertyToDisturbeDB( const NotificationPreferencesInfo::BundleInfo &bundleInfo)282 bool NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB(
283 const NotificationPreferencesInfo::BundleInfo &bundleInfo)
284 {
285 if (bundleInfo.GetBundleName().empty()) {
286 ANS_LOGE("Bundle name is null.");
287 return false;
288 }
289
290 if (!CheckRdbStore()) {
291 ANS_LOGE("RdbStore is nullptr.");
292 return false;
293 }
294 std::string values;
295 std::string bundleKeyStr = KEY_BUNDLE_LABEL + GenerateBundleLablel(bundleInfo);
296 int32_t userId = -1;
297 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
298 bool result = false;
299 GetValueFromDisturbeDB(bundleKeyStr, userId, [&](const int32_t &status, std::string &value) {
300 switch (status) {
301 case NativeRdb::E_EMPTY_VALUES_BUCKET: {
302 result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo);
303 break;
304 }
305 case NativeRdb::E_OK: {
306 ANS_LOGE("Current bundle has exsited.");
307 break;
308 }
309 default:
310 break;
311 }
312 });
313 return result;
314 }
315
PutShowBadge( const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable)316 bool NotificationPreferencesDatabase::PutShowBadge(
317 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable)
318 {
319 if (bundleInfo.GetBundleName().empty()) {
320 ANS_LOGE("Bundle name is nullptr.");
321 return false;
322 }
323
324 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
325 return false;
326 }
327
328 std::string bundleKey = GenerateBundleLablel(bundleInfo);
329 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable,
330 bundleInfo.GetBundleUid());
331 return (result == NativeRdb::E_OK);
332 }
333
PutImportance( const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &importance)334 bool NotificationPreferencesDatabase::PutImportance(
335 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &importance)
336 {
337 if (bundleInfo.GetBundleName().empty()) {
338 ANS_LOGE("Bundle name is empty.");
339 return false;
340 }
341
342 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
343 return false;
344 }
345
346 std::string bundleKey = GenerateBundleLablel(bundleInfo);
347 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_IMPORTANCE_TYPE, importance,
348 bundleInfo.GetBundleUid());
349 return (result == NativeRdb::E_OK);
350 }
351
PutTotalBadgeNums( const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &totalBadgeNum)352 bool NotificationPreferencesDatabase::PutTotalBadgeNums(
353 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &totalBadgeNum)
354 {
355 if (bundleInfo.GetBundleName().empty()) {
356 ANS_LOGE("Bundle name is blank.");
357 return false;
358 }
359
360 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
361 return false;
362 }
363 std::string bundleKey = GenerateBundleLablel(bundleInfo);
364 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum,
365 bundleInfo.GetBundleUid());
366 return (result == NativeRdb::E_OK);
367 }
368
PutNotificationsEnabledForBundle( const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled)369 bool NotificationPreferencesDatabase::PutNotificationsEnabledForBundle(
370 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled)
371 {
372 ANS_LOGD("%{public}s, enabled[%{public}d]", __FUNCTION__, enabled);
373 if (bundleInfo.GetBundleName().empty()) {
374 ANS_LOGE("Bundle name is null.");
375 return false;
376 }
377
378 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
379 return false;
380 }
381
382 std::string bundleKey = GenerateBundleLablel(bundleInfo);
383 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled,
384 bundleInfo.GetBundleUid());
385 return (result == NativeRdb::E_OK);
386 }
387
PutNotificationsEnabled(const int32_t &userId, const bool &enabled)388 bool NotificationPreferencesDatabase::PutNotificationsEnabled(const int32_t &userId, const bool &enabled)
389 {
390 if (!CheckRdbStore()) {
391 ANS_LOGE("RdbStore is nullptr.");
392 return false;
393 }
394
395 std::string typeKey =
396 std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
397 std::string enableValue = std::to_string(enabled);
398 int32_t result = rdbDataManager_->InsertData(typeKey, enableValue, userId);
399 if (result != NativeRdb::E_OK) {
400 ANS_LOGE("Store enable notification failed. %{public}d", result);
401 return false;
402 }
403 return true;
404 }
405
PutSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &slotFlags)406 bool NotificationPreferencesDatabase::PutSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo,
407 const int32_t &slotFlags)
408 {
409 if (!CheckRdbStore()) {
410 ANS_LOGE("RdbStore is nullptr.");
411 return false;
412 }
413
414 std::string bundleKey = GenerateBundleLablel(bundleInfo);
415 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_SLOTFLGS_TYPE, slotFlags,
416 bundleInfo.GetBundleUid());
417 return (result == NativeRdb::E_OK);
418 }
419
PutHasPoppedDialog( const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped)420 bool NotificationPreferencesDatabase::PutHasPoppedDialog(
421 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped)
422 {
423 if (bundleInfo.GetBundleName().empty()) {
424 ANS_LOGE("Bundle name is null.");
425 return false;
426 }
427
428 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
429 return false;
430 }
431
432 std::string bundleKey = GenerateBundleLablel(bundleInfo);
433 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped,
434 bundleInfo.GetBundleUid());
435 return (result == NativeRdb::E_OK);
436 }
437
PutDoNotDisturbDate( const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date)438 bool NotificationPreferencesDatabase::PutDoNotDisturbDate(
439 const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date)
440 {
441 if (date == nullptr) {
442 ANS_LOGE("Invalid date.");
443 return false;
444 }
445
446 if (!CheckRdbStore()) {
447 ANS_LOGE("RdbStore is nullptr.");
448 return false;
449 }
450
451 std::string typeKey =
452 std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
453 std::string typeValue = std::to_string((int)date->GetDoNotDisturbType());
454
455 std::string beginDateKey =
456 std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
457 std::string beginDateValue = std::to_string(date->GetBeginDate());
458
459 std::string endDateKey =
460 std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
461 std::string endDateValue = std::to_string(date->GetEndDate());
462
463 std::unordered_map<std::string, std::string> values = {
464 {typeKey, typeValue},
465 {beginDateKey, beginDateValue},
466 {endDateKey, endDateValue},
467 };
468
469 int32_t result = rdbDataManager_->InsertBatchData(values, userId);
470 if (result != NativeRdb::E_OK) {
471 ANS_LOGE("Store DoNotDisturbDate failed. %{public}d", result);
472 return false;
473 }
474
475 return true;
476 }
477
AddDoNotDisturbProfiles( int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)478 bool NotificationPreferencesDatabase::AddDoNotDisturbProfiles(
479 int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
480 {
481 if (profiles.empty()) {
482 ANS_LOGE("Invalid dates.");
483 return false;
484 }
485 if (!CheckRdbStore()) {
486 ANS_LOGE("RdbStore is nullptr.");
487 return false;
488 }
489 std::unordered_map<std::string, std::string> values;
490 for (auto profile : profiles) {
491 if (profile == nullptr) {
492 ANS_LOGE("The profile is null.");
493 return false;
494 }
495 std::string key = std::string().append(KEY_DO_NOT_DISTURB_ID).append(KEY_UNDER_LINE).append(
496 std::to_string(userId)).append(KEY_UNDER_LINE).append(std::to_string((int32_t)profile->GetProfileId()));
497 values[key] = profile->ToJson();
498 }
499 int32_t result = rdbDataManager_->InsertBatchData(values, userId);
500 if (result != NativeRdb::E_OK) {
501 ANS_LOGE("Add do not disturb profiles failed.");
502 return false;
503 }
504 return true;
505 }
506
RemoveDoNotDisturbProfiles( int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)507 bool NotificationPreferencesDatabase::RemoveDoNotDisturbProfiles(
508 int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
509 {
510 if (profiles.empty()) {
511 ANS_LOGW("Invalid dates.");
512 return false;
513 }
514 if (!CheckRdbStore()) {
515 ANS_LOGE("RdbStore is nullptr.");
516 return false;
517 }
518 std::vector<std::string> keys;
519 for (auto profile : profiles) {
520 if (profile == nullptr) {
521 ANS_LOGE("The profile is null.");
522 return false;
523 }
524 std::string key = std::string().append(KEY_DO_NOT_DISTURB_ID).append(KEY_UNDER_LINE).append(
525 std::to_string(userId)).append(KEY_UNDER_LINE).append(std::to_string((int32_t)profile->GetProfileId()));
526 keys.push_back(key);
527 }
528 int32_t result = rdbDataManager_->DeleteBathchData(keys, userId);
529 if (result != NativeRdb::E_OK) {
530 ANS_LOGE("Delete do not disturb profiles failed.");
531 return false;
532 }
533 return true;
534 }
535
GetDoNotDisturbProfiles( const std::string &key, sptr<NotificationDoNotDisturbProfile> &profile, const int32_t &userId)536 bool NotificationPreferencesDatabase::GetDoNotDisturbProfiles(
537 const std::string &key, sptr<NotificationDoNotDisturbProfile> &profile, const int32_t &userId)
538 {
539 if (!CheckRdbStore()) {
540 ANS_LOGE("RdbStore is nullptr.");
541 return false;
542 }
543 std::string values;
544 int32_t result = rdbDataManager_->QueryData(key, values, userId);
545 if (result != NativeRdb::E_OK) {
546 ANS_LOGE("Use default value. error code is %{public}d", result);
547 return false;
548 }
549 profile = new (std::nothrow) NotificationDoNotDisturbProfile();
550 if (profile == nullptr) {
551 ANS_LOGE("The profile is null.");
552 return false;
553 }
554 profile->FromJson(values);
555 return true;
556 }
557
GetValueFromDisturbeDB( const std::string &key, const int32_t &userId, std::function<void(std::string &)> callback)558 void NotificationPreferencesDatabase::GetValueFromDisturbeDB(
559 const std::string &key, const int32_t &userId, std::function<void(std::string &)> callback)
560 {
561 if (!CheckRdbStore()) {
562 ANS_LOGE("RdbStore is nullptr.");
563 return;
564 }
565 std::string value;
566 int32_t result = rdbDataManager_->QueryData(key, value, userId);
567 if (result == NativeRdb::E_ERROR) {
568 ANS_LOGE("Get value failed, use default value. error code is %{public}d", result);
569 return;
570 }
571 callback(value);
572 }
573
GetValueFromDisturbeDB( const std::string &key, const int32_t &userId, std::function<void(int32_t &, std::string &)> callback)574 void NotificationPreferencesDatabase::GetValueFromDisturbeDB(
575 const std::string &key, const int32_t &userId, std::function<void(int32_t &, std::string &)> callback)
576 {
577 if (!CheckRdbStore()) {
578 ANS_LOGE("RdbStore is nullptr.");
579 return;
580 }
581 std::string value;
582 int32_t result = rdbDataManager_->QueryData(key, value, userId);
583 callback(result, value);
584 }
585
CheckBundle(const std::string &bundleName, const int32_t &bundleUid)586 bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleName, const int32_t &bundleUid)
587 {
588 std::string bundleKeyStr = KEY_BUNDLE_LABEL + bundleName + std::to_string(bundleUid);
589 ANS_LOGD("CheckBundle bundleKeyStr %{public}s", bundleKeyStr.c_str());
590 bool result = true;
591 int32_t userId = -1;
592 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
593 GetValueFromDisturbeDB(bundleKeyStr, userId, [&](const int32_t &status, std::string &value) {
594 switch (status) {
595 case NativeRdb::E_EMPTY_VALUES_BUCKET: {
596 NotificationPreferencesInfo::BundleInfo bundleInfo;
597 bundleInfo.SetBundleName(bundleName);
598 bundleInfo.SetBundleUid(bundleUid);
599 bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleName, bundleUid));
600 result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo);
601 break;
602 }
603 case NativeRdb::E_OK: {
604 result = true;
605 break;
606 }
607 default:
608 result = false;
609 break;
610 }
611 });
612 return result;
613 }
614
PutBundlePropertyValueToDisturbeDB( const NotificationPreferencesInfo::BundleInfo &bundleInfo)615 bool NotificationPreferencesDatabase::PutBundlePropertyValueToDisturbeDB(
616 const NotificationPreferencesInfo::BundleInfo &bundleInfo)
617 {
618 std::unordered_map<std::string, std::string> values;
619 std::string bundleKey = bundleInfo.GetBundleName().append(std::to_string(bundleInfo.GetBundleUid()));
620 GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_NAME), bundleInfo.GetBundleName(), values);
621 GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_BADGE_TOTAL_NUM),
622 std::to_string(bundleInfo.GetBadgeTotalNum()),
623 values);
624 GenerateEntry(
625 GenerateBundleKey(bundleKey, KEY_BUNDLE_IMPORTANCE), std::to_string(bundleInfo.GetImportance()), values);
626 GenerateEntry(
627 GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE), std::to_string(bundleInfo.GetIsShowBadge()), values);
628 GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION),
629 std::to_string(bundleInfo.GetEnableNotification()),
630 values);
631 GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_POPPED_DIALOG),
632 std::to_string(bundleInfo.GetHasPoppedDialog()),
633 values);
634 GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_UID), std::to_string(bundleInfo.GetBundleUid()), values);
635 if (!CheckRdbStore()) {
636 ANS_LOGE("RdbStore is nullptr.");
637 return false;
638 }
639 int32_t userId = -1;
640 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
641 int32_t result = rdbDataManager_->InsertBatchData(values, userId);
642 if (result != NativeRdb::E_OK) {
643 ANS_LOGE("Store bundle failed. %{public}d", result);
644 return false;
645 }
646 return true;
647 }
648
ParseFromDisturbeDB(NotificationPreferencesInfo &info, int32_t userId)649 bool NotificationPreferencesDatabase::ParseFromDisturbeDB(NotificationPreferencesInfo &info, int32_t userId)
650 {
651 ANS_LOGD("%{public}s", __FUNCTION__);
652 if (!CheckRdbStore()) {
653 ANS_LOGE("RdbStore is nullptr.");
654 return false;
655 }
656 std::vector<int> activeUserId;
657 if (userId == -1) {
658 OsAccountManagerHelper::GetInstance().GetAllActiveOsAccount(activeUserId);
659 } else {
660 activeUserId.push_back(userId);
661 }
662
663 for (auto iter : activeUserId) {
664 GetDoNotDisturbType(info, iter);
665 GetDoNotDisturbBeginDate(info, iter);
666 GetDoNotDisturbEndDate(info, iter);
667 GetEnableAllNotification(info, iter);
668 GetDoNotDisturbProfile(info, iter);
669
670 std::unordered_map<std::string, std::string> values;
671 int32_t result = rdbDataManager_->QueryDataBeginWithKey(KEY_BUNDLE_LABEL, values, iter);
672 if (result == NativeRdb::E_ERROR) {
673 ANS_LOGE("Get Bundle Info failed.");
674 continue;
675 }
676 ParseBundleFromDistureDB(info, values, iter);
677 }
678
679 return true;
680 }
681
RemoveAllDataFromDisturbeDB()682 bool NotificationPreferencesDatabase::RemoveAllDataFromDisturbeDB()
683 {
684 ANS_LOGD("%{public}s", __FUNCTION__);
685 if (!CheckRdbStore()) {
686 ANS_LOGE("RdbStore is nullptr.");
687 return false;
688 }
689 int32_t result = rdbDataManager_->Destroy();
690 return (result == NativeRdb::E_OK);
691 }
692
RemoveBundleFromDisturbeDB( const std::string &bundleKey, const int32_t &bundleUid)693 bool NotificationPreferencesDatabase::RemoveBundleFromDisturbeDB(
694 const std::string &bundleKey, const int32_t &bundleUid)
695 {
696 ANS_LOGD("%{public}s", __FUNCTION__);
697 if (!CheckRdbStore()) {
698 ANS_LOGE("RdbStore is nullptr.");
699 return false;
700 }
701 int32_t userId = -1;
702 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
703
704 std::unordered_map<std::string, std::string> values;
705 int32_t result = rdbDataManager_->QueryDataBeginWithKey(
706 (KEY_ANS_BUNDLE + KEY_UNDER_LINE + bundleKey + KEY_UNDER_LINE), values, userId);
707
708 if (result == NativeRdb::E_ERROR) {
709 ANS_LOGE("Get Bundle Info failed.");
710 return false;
711 }
712
713 std::vector<std::string> keys;
714 for (auto iter : values) {
715 keys.push_back(iter.first);
716 }
717
718 std::string bundleDBKey = KEY_BUNDLE_LABEL + KEY_BUNDLE_NAME + KEY_UNDER_LINE + bundleKey;
719 keys.push_back(bundleDBKey);
720 result = rdbDataManager_->DeleteBathchData(keys, userId);
721 if (result != NativeRdb::E_OK) {
722 ANS_LOGE("delete bundle Info failed.");
723 return false;
724 }
725 return true;
726 }
727
RemoveSlotFromDisturbeDB( const std::string &bundleKey, const NotificationConstant::SlotType &type, const int32_t &bundleUid)728 bool NotificationPreferencesDatabase::RemoveSlotFromDisturbeDB(
729 const std::string &bundleKey, const NotificationConstant::SlotType &type, const int32_t &bundleUid)
730 {
731 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
732 ANS_LOGD("%{public}s", __FUNCTION__);
733 int32_t userId = -1;
734 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
735 if (bundleKey.empty()) {
736 ANS_LOGE("Bundle name is empty.");
737 return false;
738 }
739
740 if (!CheckRdbStore()) {
741 ANS_LOGE("RdbStore is nullptr.");
742 return false;
743 }
744
745 std::unordered_map<std::string, std::string> values;
746 std::string slotType = std::to_string(type);
747 int32_t result = rdbDataManager_->QueryDataBeginWithKey(
748 (GenerateSlotKey(bundleKey, slotType) + KEY_UNDER_LINE), values, userId);
749 if (result == NativeRdb::E_ERROR) {
750 return false;
751 }
752 std::vector<std::string> keys;
753 for (auto iter : values) {
754 keys.push_back(iter.first);
755 }
756
757 result = rdbDataManager_->DeleteBathchData(keys, userId);
758 if (result != NativeRdb::E_OK) {
759 ANS_LOGE("delete bundle Info failed.");
760 return false;
761 }
762
763 return true;
764 }
765
GetAllNotificationEnabledBundles( std::vector<NotificationBundleOption> &bundleOption)766 bool NotificationPreferencesDatabase::GetAllNotificationEnabledBundles(
767 std::vector<NotificationBundleOption> &bundleOption)
768 {
769 ANS_LOGD("Called.");
770 if (!CheckRdbStore()) {
771 ANS_LOGE("RdbStore is nullptr.");
772 return false;
773 }
774 std::unordered_map<std::string, std::string> datas;
775 const std::string ANS_BUNDLE_BEGIN = "ans_bundle_";
776 int32_t userId = -1;
777 OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId);
778 int32_t errCode = rdbDataManager_->QueryDataBeginWithKey(ANS_BUNDLE_BEGIN, datas, userId);
779 if (errCode != NativeRdb::E_OK) {
780 ANS_LOGE("Query data begin with ans_bundle_ from db error");
781 return false;
782 }
783 return HandleDataBaseMap(datas, bundleOption);
784 }
785
HandleDataBaseMap( const std::unordered_map<std::string, std::string> &datas, std::vector<NotificationBundleOption> &bundleOption)786 bool NotificationPreferencesDatabase::HandleDataBaseMap(
787 const std::unordered_map<std::string, std::string> &datas, std::vector<NotificationBundleOption> &bundleOption)
788 {
789 std::regex matchBundlenamePattern("^ans_bundle_(.*)_name$");
790 std::smatch match;
791 int32_t currentUserId = SUBSCRIBE_USER_INIT;
792 ErrCode result = ERR_OK;
793 result = OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(currentUserId);
794 if (result != ERR_OK) {
795 ANS_LOGE("Get account id fail");
796 return false;
797 }
798 constexpr int MIDDLE_KEY = 1;
799 for (const auto &dataMapItem : datas) {
800 const std::string &key = dataMapItem.first;
801 const std::string &value = dataMapItem.second;
802 if (!std::regex_match(key, match, matchBundlenamePattern)) {
803 continue;
804 }
805 std::string matchKey = match[MIDDLE_KEY].str();
806 std::string matchUid = "ans_bundle_" + matchKey + "_uid";
807 std::string matchEnableNotification = "ans_bundle_" + matchKey + "_enabledNotification";
808 auto enableNotificationItem = datas.find(matchEnableNotification);
809 if (enableNotificationItem == datas.end()) {
810 continue;
811 }
812 if (static_cast<bool>(StringToInt(enableNotificationItem->second))) {
813 auto uidItem = datas.find(matchUid);
814 if (uidItem == datas.end()) {
815 continue;
816 }
817 int userid = -1;
818 result =
819 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(StringToInt(uidItem->second), userid);
820 if (result != ERR_OK) {
821 return false;
822 }
823 if (userid != currentUserId) {
824 continue;
825 }
826 NotificationBundleOption obj(value, StringToInt(uidItem->second));
827 bundleOption.emplace_back(obj);
828 }
829 }
830 return true;
831 }
832
RemoveAllSlotsFromDisturbeDB( const std::string &bundleKey, const int32_t &bundleUid)833 bool NotificationPreferencesDatabase::RemoveAllSlotsFromDisturbeDB(
834 const std::string &bundleKey, const int32_t &bundleUid)
835 {
836 ANS_LOGD("%{public}s", __FUNCTION__);
837 int32_t userId = -1;
838 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
839 if (bundleKey.empty()) {
840 ANS_LOGE("Bundle name is null.");
841 return false;
842 }
843
844 if (!CheckRdbStore()) {
845 ANS_LOGE("RdbStore is nullptr.");
846 return false;
847 }
848
849 std::unordered_map<std::string, std::string> values;
850 int32_t result = rdbDataManager_->QueryDataBeginWithKey(
851 (GenerateSlotKey(bundleKey) + KEY_UNDER_LINE), values, userId);
852 if (result == NativeRdb::E_ERROR) {
853 return false;
854 }
855 std::vector<std::string> keys;
856 for (auto iter : values) {
857 keys.push_back(iter.first);
858 }
859
860 result = rdbDataManager_->DeleteBathchData(keys, userId);
861 return (result == NativeRdb::E_OK);
862 }
863
864 template <typename T>
PutBundlePropertyToDisturbeDB( const std::string &bundleKey, const BundleType &type, const T &t, const int32_t &bundleUid)865 int32_t NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB(
866 const std::string &bundleKey, const BundleType &type, const T &t, const int32_t &bundleUid)
867 {
868 std::string keyStr;
869 switch (type) {
870 case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
871 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_BADGE_TOTAL_NUM);
872 break;
873 case BundleType::BUNDLE_IMPORTANCE_TYPE:
874 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_IMPORTANCE);
875 break;
876 case BundleType::BUNDLE_SHOW_BADGE_TYPE:
877 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE);
878 break;
879 case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
880 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION);
881 break;
882 case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
883 ANS_LOGD("Into BUNDLE_POPPED_DIALOG_TYPE:GenerateBundleKey.");
884 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_POPPED_DIALOG);
885 break;
886 case BundleType::BUNDLE_SLOTFLGS_TYPE:
887 ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:GenerateBundleKey.");
888 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SLOTFLGS_TYPE);
889 break;
890 default:
891 break;
892 }
893 if (!CheckRdbStore()) {
894 ANS_LOGE("RdbStore is nullptr.");
895 return false;
896 }
897 int32_t userId = -1;
898 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
899 std::string valueStr = std::to_string(t);
900 int32_t result = rdbDataManager_->InsertData(keyStr, valueStr, userId);
901 return result;
902 }
903
PutBundleToDisturbeDB( const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo)904 bool NotificationPreferencesDatabase::PutBundleToDisturbeDB(
905 const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo)
906 {
907 if (!CheckRdbStore()) {
908 ANS_LOGE("RdbStore is nullptr.");
909 return false;
910 }
911 int32_t userId = -1;
912 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
913
914 ANS_LOGD("Key not fund, so create a bundle, bundle key is %{public}s.", bundleKey.c_str());
915 int32_t result = rdbDataManager_->InsertData(bundleKey, GenerateBundleLablel(bundleInfo), userId);
916 if (result != NativeRdb::E_OK) {
917 ANS_LOGE("Store bundle name to db is failed.");
918 return false;
919 }
920
921 if (!PutBundlePropertyValueToDisturbeDB(bundleInfo)) {
922 return false;
923 }
924 return true;
925 }
926
GenerateEntry( const std::string &key, const std::string &value, std::unordered_map<std::string, std::string> &values) const927 void NotificationPreferencesDatabase::GenerateEntry(
928 const std::string &key, const std::string &value, std::unordered_map<std::string, std::string> &values) const
929 {
930 values.emplace(key, value);
931 }
932
SlotToEntry(const std::string &bundleName, const int32_t &bundleUid, const sptr<NotificationSlot> &slot, std::unordered_map<std::string, std::string> &values)933 bool NotificationPreferencesDatabase::SlotToEntry(const std::string &bundleName, const int32_t &bundleUid,
934 const sptr<NotificationSlot> &slot, std::unordered_map<std::string, std::string> &values)
935 {
936 if (slot == nullptr) {
937 ANS_LOGE("Notification slot is nullptr.");
938 return false;
939 }
940
941 if (!CheckBundle(bundleName, bundleUid)) {
942 return false;
943 }
944
945 std::string bundleKey = bundleName + std::to_string(bundleUid);
946 GenerateSlotEntry(bundleKey, slot, values);
947 return true;
948 }
949
GenerateSlotEntry(const std::string &bundleKey, const sptr<NotificationSlot> &slot, std::unordered_map<std::string, std::string> &values) const950 void NotificationPreferencesDatabase::GenerateSlotEntry(const std::string &bundleKey,
951 const sptr<NotificationSlot> &slot, std::unordered_map<std::string, std::string> &values) const
952 {
953 std::string slotType = std::to_string(slot->GetType());
954 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_TYPE), std::to_string(slot->GetType()), values);
955 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ID), slot->GetId(), values);
956 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_NAME), slot->GetName(), values);
957 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_DESCRIPTION), slot->GetDescription(), values);
958 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LEVEL), std::to_string(slot->GetLevel()), values);
959 GenerateEntry(
960 GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SHOW_BADGE), std::to_string(slot->IsShowBadge()), values);
961 GenerateEntry(
962 GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_LIGHT), std::to_string(slot->CanEnableLight()), values);
963 GenerateEntry(
964 GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_VRBRATION), std::to_string(slot->CanVibrate()), values);
965 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LED_LIGHT_COLOR),
966 std::to_string(slot->GetLedLightColor()), values);
967 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LOCKSCREEN_VISIBLENESS),
968 std::to_string(static_cast<int>(slot->GetLockScreenVisibleness())), values);
969 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SOUND), slot->GetSound().ToString(), values);
970 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_BYPASS_DND),
971 std::to_string(slot->IsEnableBypassDnd()), values);
972 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_VIBRATION_STYLE),
973 VectorToString(slot->GetVibrationStyle()), values);
974 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLED), std::to_string(slot->GetEnable()), values);
975 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_AUTHORIZED_STATUS),
976 std::to_string(slot->GetAuthorizedStatus()), values);
977 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_AUTH_HINT_CNT),
978 std::to_string(slot->GetAuthHintCnt()), values);
979 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_REMINDER_MODE),
980 std::to_string(slot->GetReminderMode()), values);
981 }
982
ParseBundleFromDistureDB(NotificationPreferencesInfo &info, const std::unordered_map<std::string, std::string> &values, const int32_t &userId)983 void NotificationPreferencesDatabase::ParseBundleFromDistureDB(NotificationPreferencesInfo &info,
984 const std::unordered_map<std::string, std::string> &values, const int32_t &userId)
985 {
986 if (!CheckRdbStore()) {
987 ANS_LOGE("RdbStore is nullptr.");
988 return;
989 }
990 for (auto item : values) {
991 std::string bundleKey = item.second;
992 ANS_LOGD("Bundle name is %{public}s.", bundleKey.c_str());
993 std::unordered_map<std::string, std::string> bundleEntries;
994 rdbDataManager_->QueryDataBeginWithKey((GenerateBundleKey(bundleKey)), bundleEntries, userId);
995 ANS_LOGD("Bundle key is %{public}s.", GenerateBundleKey(bundleKey).c_str());
996 NotificationPreferencesInfo::BundleInfo bunldeInfo;
997 std::string keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE);
998 bool badgeEnableExist = false;
999 for (auto bundleEntry : bundleEntries) {
1000 if (IsSlotKey(GenerateBundleKey(bundleKey), bundleEntry.first)) {
1001 ParseSlotFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry, userId);
1002 } else {
1003 ParseBundlePropertyFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry);
1004 }
1005
1006 if (keyStr.compare(bundleEntry.first) == 0) {
1007 badgeEnableExist = true;
1008 }
1009 }
1010
1011 if (!badgeEnableExist) {
1012 bunldeInfo.SetIsShowBadge(static_cast<bool>(true));
1013 }
1014
1015 info.SetBundleInfoFromDb(bunldeInfo, bundleKey);
1016 }
1017 }
1018
ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, const std::pair<std::string, std::string> &entry, const int32_t &userId)1019 void NotificationPreferencesDatabase::ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo,
1020 const std::string &bundleKey, const std::pair<std::string, std::string> &entry, const int32_t &userId)
1021 {
1022 std::string slotKey = entry.first;
1023 std::string typeStr = SubUniqueIdentifyFromString(GenerateSlotKey(bundleKey) + KEY_UNDER_LINE, slotKey);
1024 NotificationConstant::SlotType slotType = static_cast<NotificationConstant::SlotType>(StringToInt(typeStr));
1025 sptr<NotificationSlot> slot = nullptr;
1026 if (!bundleInfo.GetSlot(slotType, slot)) {
1027 slot = new (std::nothrow) NotificationSlot(slotType);
1028 if (slot == nullptr) {
1029 ANS_LOGE("Failed to create NotificationSlot instance");
1030 return;
1031 }
1032 }
1033 std::string findString = GenerateSlotKey(bundleKey, typeStr) + KEY_UNDER_LINE;
1034 ParseSlot(findString, slot, entry, userId);
1035 bundleInfo.SetSlot(slot);
1036 }
1037
ParseBundlePropertyFromDisturbeDB( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, const std::pair<std::string, std::string> &entry)1038 void NotificationPreferencesDatabase::ParseBundlePropertyFromDisturbeDB(
1039 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey,
1040 const std::pair<std::string, std::string> &entry)
1041 {
1042 std::string typeStr = FindLastString(GenerateBundleKey(bundleKey), entry.first);
1043 std::string valueStr = entry.second;
1044
1045 if (typeStr.compare(KEY_BUNDLE_NAME) == 0) {
1046 return ParseBundleName(bundleInfo, valueStr);
1047 }
1048 if (typeStr.compare(KEY_BUNDLE_IMPORTANCE) == 0) {
1049 return ParseBundleImportance(bundleInfo, valueStr);
1050 }
1051 if (typeStr.compare(KEY_BUNDLE_SHOW_BADGE) == 0) {
1052 return ParseBundleShowBadgeEnable(bundleInfo, valueStr);
1053 }
1054 if (typeStr.compare(KEY_BUNDLE_BADGE_TOTAL_NUM) == 0) {
1055 return ParseBundleBadgeNum(bundleInfo, valueStr);
1056 }
1057 if (typeStr.compare(KEY_BUNDLE_ENABLE_NOTIFICATION) == 0) {
1058 return ParseBundleEnableNotification(bundleInfo, valueStr);
1059 }
1060 if (typeStr.compare(KEY_BUNDLE_POPPED_DIALOG) == 0) {
1061 return ParseBundlePoppedDialog(bundleInfo, valueStr);
1062 }
1063 if (typeStr.compare(KEY_BUNDLE_UID) == 0) {
1064 return ParseBundleUid(bundleInfo, valueStr);
1065 }
1066 if (typeStr.compare(KEY_BUNDLE_SLOTFLGS_TYPE) == 0) {
1067 return ParseBundleSlotFlags(bundleInfo, valueStr);
1068 }
1069 }
1070
ParseSlot(const std::string &findString, sptr<NotificationSlot> &slot, const std::pair<std::string, std::string> &entry, const int32_t &userId)1071 void NotificationPreferencesDatabase::ParseSlot(const std::string &findString, sptr<NotificationSlot> &slot,
1072 const std::pair<std::string, std::string> &entry, const int32_t &userId)
1073 {
1074 std::string typeStr = FindLastString(findString, entry.first);
1075 std::string valueStr = entry.second;
1076 ANS_LOGD("db key = %{public}s , %{public}s : %{public}s ",
1077 entry.first.c_str(),
1078 typeStr.c_str(),
1079 entry.second.c_str());
1080 SetSoltProperty(slot, typeStr, valueStr, findString, userId);
1081 }
1082
SetSoltProperty(sptr<NotificationSlot> &slot, std::string &typeStr, std::string &valueStr, const std::string &findString, const int32_t &userId)1083 void NotificationPreferencesDatabase::SetSoltProperty(sptr<NotificationSlot> &slot, std::string &typeStr,
1084 std::string &valueStr, const std::string &findString, const int32_t &userId)
1085 {
1086 if (typeStr.compare(KEY_SLOT_DESCRIPTION) == 0) {
1087 return ParseSlotDescription(slot, valueStr);
1088 }
1089 if (typeStr.compare(KEY_SLOT_LEVEL) == 0) {
1090 return ParseSlotLevel(slot, valueStr);
1091 }
1092 if (typeStr.compare(KEY_SLOT_SHOW_BADGE) == 0) {
1093 return ParseSlotShowBadge(slot, valueStr);
1094 }
1095 if (typeStr.compare(KEY_SLOT_ENABLE_LIGHT) == 0) {
1096 return ParseSlotEnableLight(slot, valueStr);
1097 }
1098 if (typeStr.compare(KEY_SLOT_ENABLE_VRBRATION) == 0) {
1099 return ParseSlotEnableVrbration(slot, valueStr);
1100 }
1101 if (typeStr.compare(KEY_SLOT_LED_LIGHT_COLOR) == 0) {
1102 return ParseSlotLedLightColor(slot, valueStr);
1103 }
1104 if (typeStr.compare(KEY_SLOT_LOCKSCREEN_VISIBLENESS) == 0) {
1105 return ParseSlotLockscreenVisibleness(slot, valueStr);
1106 }
1107 if (typeStr.compare(KEY_SLOT_SOUND) == 0) {
1108 return ParseSlotSound(slot, valueStr);
1109 }
1110 if (typeStr.compare(KEY_SLOT_VIBRATION_STYLE) == 0) {
1111 return ParseSlotVibrationSytle(slot, valueStr);
1112 }
1113 if (typeStr.compare(KEY_SLOT_ENABLE_BYPASS_DND) == 0) {
1114 return ParseSlotEnableBypassDnd(slot, valueStr);
1115 }
1116 if (typeStr.compare(KEY_SLOT_ENABLED) == 0) {
1117 return ParseSlotEnabled(slot, valueStr);
1118 }
1119 if (typeStr.compare(KEY_SLOT_SLOTFLGS_TYPE) == 0) {
1120 return ParseSlotFlags(slot, valueStr);
1121 }
1122 if (typeStr.compare(KEY_SLOT_AUTHORIZED_STATUS) == 0) {
1123 return ParseSlotAuthorizedStatus(slot, valueStr);
1124 }
1125 if (typeStr.compare(KEY_SLOT_AUTH_HINT_CNT) == 0) {
1126 return ParseSlotAuthHitnCnt(slot, valueStr);
1127 }
1128 ExecuteDisturbeDB(slot, typeStr, valueStr, findString, userId);
1129 }
1130
ExecuteDisturbeDB(sptr<NotificationSlot> &slot, std::string &typeStr, std::string &valueStr, const std::string &findString, const int32_t &userId)1131 void NotificationPreferencesDatabase::ExecuteDisturbeDB(sptr<NotificationSlot> &slot, std::string &typeStr,
1132 std::string &valueStr, const std::string &findString, const int32_t &userId)
1133 {
1134 if (typeStr.compare(KEY_REMINDER_MODE) == 0) {
1135 return ParseSlotReminderMode(slot, valueStr);
1136 }
1137 if (!typeStr.compare(KEY_SLOT_VIBRATION_STYLE)) {
1138 GetValueFromDisturbeDB(findString + KEY_SLOT_ENABLE_VRBRATION, userId,
1139 [&](std::string &value) { ParseSlotEnableVrbration(slot, value); });
1140 }
1141 }
1142
FindLastString( const std::string &findString, const std::string &inputString) const1143 std::string NotificationPreferencesDatabase::FindLastString(
1144 const std::string &findString, const std::string &inputString) const
1145 {
1146 std::string keyStr;
1147 size_t pos = findString.size();
1148 if (pos != std::string::npos) {
1149 keyStr = inputString.substr(pos);
1150 }
1151 return keyStr;
1152 }
1153
VectorToString(const std::vector<int64_t> &data) const1154 std::string NotificationPreferencesDatabase::VectorToString(const std::vector<int64_t> &data) const
1155 {
1156 std::stringstream streamStr;
1157 std::copy(data.begin(), data.end(), std::ostream_iterator<int>(streamStr, KEY_UNDER_LINE.c_str()));
1158 return streamStr.str();
1159 }
1160
StringToVector(const std::string &str, std::vector<int64_t> &data) const1161 void NotificationPreferencesDatabase::StringToVector(const std::string &str, std::vector<int64_t> &data) const
1162 {
1163 if (str.empty()) {
1164 return;
1165 }
1166
1167 if (str.find_first_of(KEY_UNDER_LINE) != std::string::npos) {
1168 std::string str1 = str.substr(0, str.find_first_of(KEY_UNDER_LINE));
1169 std::string afterStr = str.substr(str.find_first_of(KEY_UNDER_LINE) + 1);
1170 data.push_back(StringToInt(str1));
1171 StringToVector(afterStr, data);
1172 }
1173 }
1174
StringToInt(const std::string &str) const1175 int32_t NotificationPreferencesDatabase::StringToInt(const std::string &str) const
1176 {
1177 int32_t value = 0;
1178 if (!str.empty()) {
1179 value = atoi(str.c_str());
1180 }
1181 return value;
1182 }
1183
StringToInt64(const std::string &str) const1184 int64_t NotificationPreferencesDatabase::StringToInt64(const std::string &str) const
1185 {
1186 int64_t value = 0;
1187 if (!str.empty()) {
1188 value = atoll(str.c_str());
1189 }
1190 return value;
1191 }
1192
IsSlotKey(const std::string &bundleKey, const std::string &key) const1193 bool NotificationPreferencesDatabase::IsSlotKey(const std::string &bundleKey, const std::string &key) const
1194 {
1195 std::string tempStr = FindLastString(bundleKey, key);
1196 size_t pos = tempStr.find_first_of(KEY_UNDER_LINE);
1197 std::string slotStr;
1198 if (pos != std::string::npos) {
1199 slotStr = tempStr.substr(0, pos);
1200 }
1201 if (!slotStr.compare(KEY_SLOT)) {
1202 return true;
1203 }
1204 return false;
1205 }
1206
GenerateSlotKey( const std::string &bundleKey, const std::string &type, const std::string &subType) const1207 std::string NotificationPreferencesDatabase::GenerateSlotKey(
1208 const std::string &bundleKey, const std::string &type, const std::string &subType) const
1209 {
1210 /* slot key
1211 *
1212 * KEY_ANS_BUNDLE_bundlename_slot_type_0_id
1213 * KEY_ANS_BUNDLE_bundlename_slot_type_0_des
1214 * KEY_ANS_BUNDLE_bundlename_slot_type_1_id
1215 * KEY_ANS_BUNDLE_bundlename_slot_type_1_des
1216 *
1217 */
1218 std::string key = GenerateBundleKey(bundleKey).append(KEY_SLOT).append(KEY_UNDER_LINE).append(KEY_SLOT_TYPE);
1219 if (!type.empty()) {
1220 key.append(KEY_UNDER_LINE).append(type);
1221 }
1222 if (!subType.empty()) {
1223 key.append(KEY_UNDER_LINE).append(subType);
1224 }
1225 ANS_LOGD("Slot key is : %{public}s.", key.c_str());
1226 return key;
1227 }
1228
GenerateBundleKey( const std::string &bundleKey, const std::string &type) const1229 std::string NotificationPreferencesDatabase::GenerateBundleKey(
1230 const std::string &bundleKey, const std::string &type) const
1231 {
1232 /* bundle key
1233 *
1234 * label_KEY_ANS_KEY_BUNDLE_NAME = ""
1235 * KEY_ANS_BUNDLE_bundlename_
1236 * KEY_ANS_BUNDLE_bundlename_
1237 * KEY_ANS_BUNDLE_bundlename_
1238 * KEY_ANS_BUNDLE_bundlename_
1239 *
1240 */
1241 ANS_LOGD("%{public}s, bundleKey[%{public}s] type[%{public}s]", __FUNCTION__, bundleKey.c_str(), type.c_str());
1242 std::string key =
1243 std::string().append(KEY_ANS_BUNDLE).append(KEY_UNDER_LINE).append(bundleKey).append(KEY_UNDER_LINE);
1244 if (!type.empty()) {
1245 key.append(type);
1246 }
1247 ANS_LOGD("Bundle key : %{public}s.", key.c_str());
1248 return key;
1249 }
1250
SubUniqueIdentifyFromString( const std::string &findString, const std::string &keyStr) const1251 std::string NotificationPreferencesDatabase::SubUniqueIdentifyFromString(
1252 const std::string &findString, const std::string &keyStr) const
1253 {
1254 std::string slotType;
1255 std::string tempStr = FindLastString(findString, keyStr);
1256 size_t pos = tempStr.find_last_of(KEY_UNDER_LINE);
1257 if (pos != std::string::npos) {
1258 slotType = tempStr.substr(0, pos);
1259 }
1260
1261 return slotType;
1262 }
1263
ParseBundleName( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const1264 void NotificationPreferencesDatabase::ParseBundleName(
1265 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1266 {
1267 ANS_LOGD("SetBundleName bundle name is %{public}s.", value.c_str());
1268 bundleInfo.SetBundleName(value);
1269 }
1270
ParseBundleImportance( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const1271 void NotificationPreferencesDatabase::ParseBundleImportance(
1272 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1273 {
1274 ANS_LOGD("SetBundleImportance bundle importance is %{public}s.", value.c_str());
1275 bundleInfo.SetImportance(static_cast<NotificationSlot::NotificationLevel>(StringToInt(value)));
1276 }
1277
ParseBundleShowBadgeEnable( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const1278 void NotificationPreferencesDatabase::ParseBundleShowBadgeEnable(
1279 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1280 {
1281 ANS_LOGD("SetBundleShowBadge bundle show badge is %{public}s.", value.c_str());
1282 bundleInfo.SetIsShowBadge(static_cast<bool>(StringToInt(value)));
1283 }
1284
ParseBundleBadgeNum( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const1285 void NotificationPreferencesDatabase::ParseBundleBadgeNum(
1286 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1287 {
1288 ANS_LOGD("SetBundleBadgeNum bundle badge num is %{public}s.", value.c_str());
1289 bundleInfo.SetBadgeTotalNum(StringToInt(value));
1290 }
1291
ParseBundleEnableNotification( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const1292 void NotificationPreferencesDatabase::ParseBundleEnableNotification(
1293 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1294 {
1295 ANS_LOGD("SetBundleEnableNotification bundle enable is %{public}s.", value.c_str());
1296 bundleInfo.SetEnableNotification(static_cast<bool>(StringToInt(value)));
1297 }
1298
ParseBundlePoppedDialog( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const1299 void NotificationPreferencesDatabase::ParseBundlePoppedDialog(
1300 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1301 {
1302 ANS_LOGD("SetBundlePoppedDialog bundle has popped dialog is %{public}s.", value.c_str());
1303 bundleInfo.SetHasPoppedDialog(static_cast<bool>(StringToInt(value)));
1304 }
1305
ParseBundleUid( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const1306 void NotificationPreferencesDatabase::ParseBundleUid(
1307 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1308 {
1309 ANS_LOGD("SetBundleUid uuid is %{public}s.", value.c_str());
1310 bundleInfo.SetBundleUid(StringToInt(value));
1311 }
1312
ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const1313 void NotificationPreferencesDatabase::ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const
1314 {
1315 ANS_LOGD("ParseSlotDescription slot des is %{public}s.", value.c_str());
1316 std::string slotDescription = value;
1317 slot->SetDescription(slotDescription);
1318 }
1319
ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const1320 void NotificationPreferencesDatabase::ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const
1321 {
1322 ANS_LOGD("ParseSlotLevel slot level is %{public}s.", value.c_str());
1323 NotificationSlot::NotificationLevel level = static_cast<NotificationSlot::NotificationLevel>(StringToInt(value));
1324 slot->SetLevel(level);
1325 }
1326
ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const1327 void NotificationPreferencesDatabase::ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const
1328 {
1329 ANS_LOGD("ParseSlotShowBadge slot show badge is %{public}s.", value.c_str());
1330 bool showBadge = static_cast<bool>(StringToInt(value));
1331 slot->EnableBadge(showBadge);
1332 }
1333
ParseSlotFlags(sptr<NotificationSlot> &slot, const std::string &value) const1334 void NotificationPreferencesDatabase::ParseSlotFlags(sptr<NotificationSlot> &slot, const std::string &value) const
1335 {
1336 ANS_LOGD("ParseSlotFlags slot show flags is %{public}s.", value.c_str());
1337 uint32_t slotFlags = static_cast<uint32_t>(StringToInt(value));
1338 slot->SetSlotFlags(slotFlags);
1339 }
1340
ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const1341 void NotificationPreferencesDatabase::ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo,
1342 const std::string &value) const
1343 {
1344 ANS_LOGD("ParseBundleSlotFlags slot show flags is %{public}s.", value.c_str());
1345 bundleInfo.SetSlotFlags(StringToInt(value));
1346 }
1347
ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const1348 void NotificationPreferencesDatabase::ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const
1349 {
1350 ANS_LOGD("ParseSlotEnableLight slot enable light is %{public}s.", value.c_str());
1351 bool enableLight = static_cast<bool>(StringToInt(value));
1352 slot->SetEnableLight(enableLight);
1353 }
1354
ParseSlotEnableVrbration( sptr<NotificationSlot> &slot, const std::string &value) const1355 void NotificationPreferencesDatabase::ParseSlotEnableVrbration(
1356 sptr<NotificationSlot> &slot, const std::string &value) const
1357 {
1358 ANS_LOGD("ParseSlotEnableVrbration slot enable vir is %{public}s.", value.c_str());
1359 bool enableVrbration = static_cast<bool>(StringToInt(value));
1360 slot->SetEnableVibration(enableVrbration);
1361 }
1362
ParseSlotLedLightColor( sptr<NotificationSlot> &slot, const std::string &value) const1363 void NotificationPreferencesDatabase::ParseSlotLedLightColor(
1364 sptr<NotificationSlot> &slot, const std::string &value) const
1365 {
1366 ANS_LOGD("ParseSlotLedLightColor slot led is %{public}s.", value.c_str());
1367 int32_t ledLightColor = static_cast<int32_t>(StringToInt(value));
1368 slot->SetLedLightColor(ledLightColor);
1369 }
1370
ParseSlotLockscreenVisibleness( sptr<NotificationSlot> &slot, const std::string &value) const1371 void NotificationPreferencesDatabase::ParseSlotLockscreenVisibleness(
1372 sptr<NotificationSlot> &slot, const std::string &value) const
1373 {
1374
1375 ANS_LOGD("ParseSlotLockscreenVisibleness slot visible is %{public}s.", value.c_str());
1376 NotificationConstant::VisiblenessType visible =
1377 static_cast<NotificationConstant::VisiblenessType>(StringToInt(value));
1378 slot->SetLockscreenVisibleness(visible);
1379 }
1380
ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const1381 void NotificationPreferencesDatabase::ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const
1382 {
1383 ANS_LOGD("ParseSlotSound slot sound is %{public}s.", value.c_str());
1384 std::string slotUri = value;
1385 Uri uri(slotUri);
1386 slot->SetSound(uri);
1387 }
1388
ParseSlotVibrationSytle( sptr<NotificationSlot> &slot, const std::string &value) const1389 void NotificationPreferencesDatabase::ParseSlotVibrationSytle(
1390 sptr<NotificationSlot> &slot, const std::string &value) const
1391 {
1392 ANS_LOGD("ParseSlotVibrationSytle slot vibration style is %{public}s.", value.c_str());
1393 std::vector<int64_t> vibrationStyle;
1394 StringToVector(value, vibrationStyle);
1395 slot->SetVibrationStyle(vibrationStyle);
1396 }
1397
ParseSlotEnableBypassDnd( sptr<NotificationSlot> &slot, const std::string &value) const1398 void NotificationPreferencesDatabase::ParseSlotEnableBypassDnd(
1399 sptr<NotificationSlot> &slot, const std::string &value) const
1400 {
1401 ANS_LOGD("ParseSlotEnableBypassDnd slot by pass dnd is %{public}s.", value.c_str());
1402 bool enable = static_cast<bool>(StringToInt(value));
1403 slot->EnableBypassDnd(enable);
1404 }
1405
ParseSlotEnabled( sptr<NotificationSlot> &slot, const std::string &value) const1406 void NotificationPreferencesDatabase::ParseSlotEnabled(
1407 sptr<NotificationSlot> &slot, const std::string &value) const
1408 {
1409 ANS_LOGD("ParseSlotEnabled slot enabled is %{public}s.", value.c_str());
1410 bool enabled = static_cast<bool>(StringToInt(value));
1411 slot->SetEnable(enabled);
1412 }
1413
ParseSlotAuthorizedStatus( sptr<NotificationSlot> &slot, const std::string &value) const1414 void NotificationPreferencesDatabase::ParseSlotAuthorizedStatus(
1415 sptr<NotificationSlot> &slot, const std::string &value) const
1416 {
1417 ANS_LOGD("ParseSlotAuthorizedStatus slot status is %{public}s.", value.c_str());
1418 int32_t status = static_cast<int32_t>(StringToInt(value));
1419 slot->SetAuthorizedStatus(status);
1420 }
1421
ParseSlotAuthHitnCnt( sptr<NotificationSlot> &slot, const std::string &value) const1422 void NotificationPreferencesDatabase::ParseSlotAuthHitnCnt(
1423 sptr<NotificationSlot> &slot, const std::string &value) const
1424 {
1425 ANS_LOGD("ParseSlotAuthHitnCnt slot count is %{public}s.", value.c_str());
1426 int32_t count = static_cast<int32_t>(StringToInt(value));
1427 slot->SetAuthHintCnt(count);
1428 }
1429
ParseSlotReminderMode( sptr<NotificationSlot> &slot, const std::string &value) const1430 void NotificationPreferencesDatabase::ParseSlotReminderMode(
1431 sptr<NotificationSlot> &slot, const std::string &value) const
1432 {
1433 ANS_LOGD("ParseSlotReminderMode slot reminder mode is %{public}s.", value.c_str());
1434 int32_t reminderMode = static_cast<int32_t>(StringToInt(value));
1435 slot->SetReminderMode(reminderMode);
1436 }
1437
GenerateBundleLablel( const NotificationPreferencesInfo::BundleInfo &bundleInfo) const1438 std::string NotificationPreferencesDatabase::GenerateBundleLablel(
1439 const NotificationPreferencesInfo::BundleInfo &bundleInfo) const
1440 {
1441 return bundleInfo.GetBundleName().append(std::to_string(bundleInfo.GetBundleUid()));
1442 }
1443
GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId)1444 void NotificationPreferencesDatabase::GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId)
1445 {
1446 std::string key =
1447 std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1448 GetValueFromDisturbeDB(
1449 key, userId, [&](const int32_t &status, std::string &value) {
1450 sptr<NotificationDoNotDisturbDate> disturbDate = new (std::nothrow)
1451 NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1452 if (disturbDate == nullptr) {
1453 ANS_LOGE("Create NotificationDoNotDisturbDate instance fail.");
1454 return;
1455 }
1456 info.GetDoNotDisturbDate(userId, disturbDate);
1457 if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1458 PutDoNotDisturbDate(userId, disturbDate);
1459 } else if (status == NativeRdb::E_OK) {
1460 if (!value.empty()) {
1461 if (disturbDate != nullptr) {
1462 disturbDate->SetDoNotDisturbType(
1463 (NotificationConstant::DoNotDisturbType)StringToInt(value));
1464 }
1465 }
1466 } else {
1467 ANS_LOGW("Parse disturbe mode failed, use default value.");
1468 }
1469 info.SetDoNotDisturbDate(userId, disturbDate);
1470 });
1471 }
1472
GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId)1473 void NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId)
1474 {
1475 std::string key =
1476 std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1477 GetValueFromDisturbeDB(
1478 key, userId, [&](const int32_t &status, std::string &value) {
1479 sptr<NotificationDoNotDisturbDate> disturbDate = new (std::nothrow)
1480 NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1481 if (disturbDate == nullptr) {
1482 ANS_LOGE("Failed to create NotificationDoNotDisturbDate instance");
1483 return;
1484 }
1485 info.GetDoNotDisturbDate(userId, disturbDate);
1486 if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1487 PutDoNotDisturbDate(userId, disturbDate);
1488 } else if (status == NativeRdb::E_OK) {
1489 if (!value.empty()) {
1490 if (disturbDate != nullptr) {
1491 disturbDate->SetBeginDate(StringToInt64(value));
1492 }
1493 }
1494 } else {
1495 ANS_LOGW("Parse disturbe start time failed, use default value.");
1496 }
1497 info.SetDoNotDisturbDate(userId, disturbDate);
1498 });
1499 }
1500
GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId)1501 void NotificationPreferencesDatabase::GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId)
1502 {
1503 std::string key =
1504 std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1505 GetValueFromDisturbeDB(
1506 key, userId, [&](const int32_t &status, std::string &value) {
1507 sptr<NotificationDoNotDisturbDate> disturbDate = new (std::nothrow)
1508 NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1509 if (disturbDate == nullptr) {
1510 ANS_LOGE("Defeat to create NotificationDoNotDisturbDate instance");
1511 return;
1512 }
1513 info.GetDoNotDisturbDate(userId, disturbDate);
1514 if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1515 PutDoNotDisturbDate(userId, disturbDate);
1516 } else if (status == NativeRdb::E_OK) {
1517 if (!value.empty()) {
1518 if (disturbDate != nullptr) {
1519 disturbDate->SetEndDate(StringToInt64(value));
1520 }
1521 }
1522 } else {
1523 ANS_LOGW("Parse disturbe end time failed, use default value.");
1524 }
1525 info.SetDoNotDisturbDate(userId, disturbDate);
1526 });
1527 }
1528
GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId)1529 void NotificationPreferencesDatabase::GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId)
1530 {
1531 std::string key =
1532 std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
1533 GetValueFromDisturbeDB(
1534 key, userId, [&](const int32_t &status, std::string &value) {
1535 if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1536 bool enable = true;
1537 if (!info.GetEnabledAllNotification(userId, enable)) {
1538 info.SetEnabledAllNotification(userId, enable);
1539 ANS_LOGW("Enable setting not found, default true.");
1540 }
1541 PutNotificationsEnabled(userId, enable);
1542 } else if (status == NativeRdb::E_OK) {
1543 if (!value.empty()) {
1544 info.SetEnabledAllNotification(userId, static_cast<bool>(StringToInt(value)));
1545 }
1546 } else {
1547 ANS_LOGW("Parse enable all notification failed, use default value.");
1548 }
1549 });
1550 }
1551
GetDoNotDisturbProfile(NotificationPreferencesInfo &info, int32_t userId)1552 void NotificationPreferencesDatabase::GetDoNotDisturbProfile(NotificationPreferencesInfo &info, int32_t userId)
1553 {
1554 if (!CheckRdbStore()) {
1555 ANS_LOGE("RdbStore is nullptr.");
1556 return;
1557 }
1558 std::unordered_map<std::string, std::string> datas;
1559 int32_t result = rdbDataManager_->QueryAllData(datas, userId);
1560 if (result != NativeRdb::E_OK) {
1561 ANS_LOGE("Query all data failed.");
1562 return;
1563 }
1564 std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
1565 for (const auto &data : datas) {
1566 std::string key = data.first;
1567 auto result = key.find(KEY_DO_NOT_DISTURB_ID);
1568 if (result != std::string::npos) {
1569 sptr<NotificationDoNotDisturbProfile> profile;
1570 GetDoNotDisturbProfiles(data.first, profile, userId);
1571 profiles.emplace_back(profile);
1572 }
1573 }
1574 info.AddDoNotDisturbProfiles(userId, profiles);
1575 }
1576
RemoveNotificationEnable(const int32_t userId)1577 bool NotificationPreferencesDatabase::RemoveNotificationEnable(const int32_t userId)
1578 {
1579 ANS_LOGD("%{public}s", __FUNCTION__);
1580 if (!CheckRdbStore()) {
1581 ANS_LOGE("RdbStore is nullptr.");
1582 return false;
1583 }
1584
1585 std::string key =
1586 std::string(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
1587 int32_t result = rdbDataManager_->DeleteData(key, userId);
1588 if (result != NativeRdb::E_OK) {
1589 ANS_LOGE("delete bundle Info failed.");
1590 return false;
1591 }
1592
1593 ANS_LOGD("%{public}s remove notification enable, userId : %{public}d", __FUNCTION__, userId);
1594 return true;
1595 }
1596
RemoveDoNotDisturbDate(const int32_t userId)1597 bool NotificationPreferencesDatabase::RemoveDoNotDisturbDate(const int32_t userId)
1598 {
1599 ANS_LOGD("%{public}s", __FUNCTION__);
1600 if (!CheckRdbStore()) {
1601 ANS_LOGE("RdbStore is nullptr.");
1602 return false;
1603 }
1604
1605 std::string typeKey =
1606 std::string(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1607 std::string beginDateKey =
1608 std::string(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1609 std::string endDateKey =
1610 std::string(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1611
1612 std::vector<std::string> keys = {
1613 typeKey,
1614 beginDateKey,
1615 endDateKey
1616 };
1617
1618 int32_t result = rdbDataManager_->DeleteBathchData(keys, userId);
1619 if (result != NativeRdb::E_OK) {
1620 ANS_LOGE("delete DoNotDisturb date failed.");
1621 return false;
1622 }
1623
1624 ANS_LOGD("%{public}s remove DoNotDisturb date, userId : %{public}d", __FUNCTION__, userId);
1625 return true;
1626 }
1627
RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid)1628 bool NotificationPreferencesDatabase::RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid)
1629 {
1630 if (!CheckRdbStore()) {
1631 ANS_LOGE("RdbStore is nullptr.");
1632 return false;
1633 }
1634
1635 std::string key = KEY_BUNDLE_LABEL + bundleName + std::to_string(uid);
1636 int32_t userId = -1;
1637 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(uid, userId);
1638 int32_t result = rdbDataManager_->DeleteData(key, userId);
1639 if (result != NativeRdb::E_OK) {
1640 ANS_LOGE("Delete ans bundle db info failed, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid);
1641 return false;
1642 }
1643
1644 ANS_LOGE("Remove ans bundle db info, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid);
1645 return true;
1646 }
1647
RemoveEnabledDbByBundleName(std::string bundleName, const int32_t &bundleUid)1648 bool NotificationPreferencesDatabase::RemoveEnabledDbByBundleName(std::string bundleName, const int32_t &bundleUid)
1649 {
1650 if (!CheckRdbStore()) {
1651 ANS_LOGE("RdbStore is nullptr.");
1652 return false;
1653 }
1654 int32_t userId = -1;
1655 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
1656 std::string key = std::string(KEY_BUNDLE_DISTRIBUTED_ENABLE_NOTIFICATION).append(
1657 KEY_MIDDLE_LINE).append(std::string(bundleName).append(KEY_MIDDLE_LINE));
1658 ANS_LOGD("key is %{public}s", key.c_str());
1659 int32_t result = NativeRdb::E_OK;
1660 std::unordered_map<std::string, std::string> values;
1661 result = rdbDataManager_->QueryDataBeginWithKey(key, values, userId);
1662 if (result == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1663 return true;
1664 } else if (result != NativeRdb::E_OK) {
1665 ANS_LOGE("Get failed, key %{public}s,result %{public}d.", key.c_str(), result);
1666 return NativeRdb::E_ERROR;
1667 }
1668
1669 std::vector<std::string> keys;
1670 for (auto iter : values) {
1671 ANS_LOGD("Get failed, key %{public}s", iter.first.c_str());
1672 keys.push_back(iter.first);
1673 }
1674
1675 result = rdbDataManager_->DeleteBathchData(keys, userId);
1676 if (result != NativeRdb::E_OK) {
1677 ANS_LOGE("delete bundle Info failed.");
1678 return false;
1679 }
1680
1681 return true;
1682 }
1683
SetKvToDb( const std::string &key, const std::string &value, const int32_t &userId)1684 int32_t NotificationPreferencesDatabase::SetKvToDb(
1685 const std::string &key, const std::string &value, const int32_t &userId)
1686 {
1687 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_2);
1688 if (!CheckRdbStore()) {
1689 ANS_LOGE("RdbStore is nullptr.");
1690 message.Message("RdbStore is nullptr.");
1691 NotificationAnalyticsUtil::ReportModifyEvent(message);
1692 return NativeRdb::E_ERROR;
1693 }
1694 int32_t result = rdbDataManager_->InsertData(key, value, userId);
1695 if (result != NativeRdb::E_OK) {
1696 message.Message("Set key failed: " + key);
1697 NotificationAnalyticsUtil::ReportModifyEvent(message);
1698 ANS_LOGE("Set key: %{public}s failed, result %{public}d.", key.c_str(), result);
1699 return NativeRdb::E_ERROR;
1700 }
1701
1702 ANS_LOGD("Key:%{public}s, value:%{public}s.", key.c_str(), value.c_str());
1703
1704 return NativeRdb::E_OK;
1705 }
1706
SetByteToDb( const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId)1707 int32_t NotificationPreferencesDatabase::SetByteToDb(
1708 const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId)
1709 {
1710 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_2);
1711 if (!CheckRdbStore()) {
1712 message.Message("RdbStore is nullptr.");
1713 NotificationAnalyticsUtil::ReportModifyEvent(message);
1714 ANS_LOGE("RdbStore is nullptr.");
1715 return NativeRdb::E_ERROR;
1716 }
1717 int32_t result = rdbDataManager_->InsertData(key, value, userId);
1718 if (result != NativeRdb::E_OK) {
1719 message.Message("Set key failed: " + key);
1720 NotificationAnalyticsUtil::ReportModifyEvent(message);
1721 ANS_LOGE("Set key: %{public}s failed, result %{public}d.", key.c_str(), result);
1722 return NativeRdb::E_ERROR;
1723 }
1724
1725 return NativeRdb::E_OK;
1726 }
1727
GetKvFromDb( const std::string &key, std::string &value, const int32_t &userId)1728 int32_t NotificationPreferencesDatabase::GetKvFromDb(
1729 const std::string &key, std::string &value, const int32_t &userId)
1730 {
1731 if (!CheckRdbStore()) {
1732 ANS_LOGE("RdbStore is nullptr.");
1733 return NativeRdb::E_ERROR;
1734 }
1735
1736 int32_t result = rdbDataManager_->QueryData(key, value, userId);
1737 if (result != NativeRdb::E_OK) {
1738 ANS_LOGE("Get key-value failed, key %{public}s, result %{pubic}d.", key.c_str(), result);
1739 return NativeRdb::E_ERROR;
1740 }
1741
1742 ANS_LOGD("Key:%{public}s, value:%{public}s.", key.c_str(), value.c_str());
1743
1744 return NativeRdb::E_OK;
1745 }
1746
GetByteFromDb( const std::string &key, std::vector<uint8_t> &value, const int32_t &userId)1747 int32_t NotificationPreferencesDatabase::GetByteFromDb(
1748 const std::string &key, std::vector<uint8_t> &value, const int32_t &userId)
1749 {
1750 if (!CheckRdbStore()) {
1751 ANS_LOGE("RdbStore is nullptr.");
1752 return NativeRdb::E_ERROR;
1753 }
1754
1755 int32_t result = rdbDataManager_->QueryData(key, value, userId);
1756 if (result != NativeRdb::E_OK) {
1757 ANS_LOGE("Get byte failed, key %{public}s, result %{pubic}d.", key.c_str(), result);
1758 return NativeRdb::E_ERROR;
1759 }
1760
1761 return NativeRdb::E_OK;
1762 }
1763
GetBatchKvsFromDb( const std::string &key, std::unordered_map<std::string, std::string> &values, const int32_t &userId)1764 int32_t NotificationPreferencesDatabase::GetBatchKvsFromDb(
1765 const std::string &key, std::unordered_map<std::string, std::string> &values, const int32_t &userId)
1766 {
1767 if (!CheckRdbStore()) {
1768 ANS_LOGE("RdbStore is nullptr.");
1769 return NativeRdb::E_ERROR;
1770 }
1771
1772 int32_t result = rdbDataManager_->QueryDataBeginWithKey(key, values, userId);
1773 if (result != NativeRdb::E_OK) {
1774 ANS_LOGE("Get batch notification request failed, key %{public}s, result %{public}d.", key.c_str(), result);
1775 return NativeRdb::E_ERROR;
1776 }
1777 ANS_LOGD("Key:%{public}s.", key.c_str());
1778 return NativeRdb::E_OK;
1779 }
1780
DeleteKvFromDb(const std::string &key, const int32_t &userId)1781 int32_t NotificationPreferencesDatabase::DeleteKvFromDb(const std::string &key, const int32_t &userId)
1782 {
1783 if (!CheckRdbStore()) {
1784 ANS_LOGE("RdbStore is nullptr.");
1785 return NativeRdb::E_ERROR;
1786 }
1787
1788 int32_t result = rdbDataManager_->DeleteData(key, userId);
1789 if (result != NativeRdb::E_OK) {
1790 ANS_LOGE("Delete key-value failed, key %{public}s, result %{public}d.", key.c_str(), result);
1791 return NativeRdb::E_ERROR;
1792 }
1793
1794 ANS_LOGD("Delete key:%{public}s.", key.c_str());
1795
1796 return NativeRdb::E_OK;
1797 }
1798
DropUserTable(const int32_t userId)1799 int32_t NotificationPreferencesDatabase::DropUserTable(const int32_t userId)
1800 {
1801 if (!CheckRdbStore()) {
1802 ANS_LOGE("RdbStore is nullptr.");
1803 return NativeRdb::E_ERROR;
1804 }
1805
1806 int32_t result = rdbDataManager_->DropUserTable(userId);
1807 if (result != NativeRdb::E_OK) {
1808 ANS_LOGE("Delete table failed, result %{public}d.", result);
1809 return NativeRdb::E_ERROR;
1810 }
1811 return NativeRdb::E_OK;
1812 }
1813
IsAgentRelationship(const std::string &agentBundleName, const std::string &sourceBundleName)1814 bool NotificationPreferencesDatabase::IsAgentRelationship(const std::string &agentBundleName,
1815 const std::string &sourceBundleName)
1816 {
1817 if (!CheckRdbStore()) {
1818 ANS_LOGE("RdbStore is nullptr.");
1819 return false;
1820 }
1821 std::string agentShip = "";
1822 int32_t result = rdbDataManager_->QueryData("PROXY_PKG", agentShip);
1823 if (result != NativeRdb::E_OK) {
1824 ANS_LOGE("Query agent relationships failed.");
1825 return false;
1826 }
1827 ANS_LOGD("The agent relationship is :%{public}s.", agentShip.c_str());
1828 nlohmann::json jsonAgentShip = nlohmann::json::parse(agentShip, nullptr, false);
1829 if (jsonAgentShip.is_null() || jsonAgentShip.empty()) {
1830 ANS_LOGE("Invalid JSON object");
1831 return false;
1832 }
1833 if (jsonAgentShip.is_discarded() || !jsonAgentShip.is_array()) {
1834 ANS_LOGE("Parse agent ship failed due to data is discarded or not array");
1835 return false;
1836 }
1837
1838 nlohmann::json jsonTarget;
1839 jsonTarget[RELATIONSHIP_JSON_KEY_SERVICE] = agentBundleName;
1840 jsonTarget[RELATIONSHIP_JSON_KEY_APP] = sourceBundleName;
1841 bool isAgentRelationship = false;
1842 for (const auto &item : jsonAgentShip) {
1843 if (jsonTarget == item) {
1844 isAgentRelationship = true;
1845 break;
1846 }
1847 }
1848
1849 return isAgentRelationship;
1850 }
1851
PutDistributedEnabledForBundle(const std::string deviceType, const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled)1852 bool NotificationPreferencesDatabase::PutDistributedEnabledForBundle(const std::string deviceType,
1853 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled)
1854 {
1855 ANS_LOGD("%{public}s, deviceType:%{public}s,enabled[%{public}d]", __FUNCTION__, deviceType.c_str(), enabled);
1856 if (bundleInfo.GetBundleName().empty()) {
1857 ANS_LOGE("Bundle name is null.");
1858 return false;
1859 }
1860 int32_t userId = -1;
1861 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
1862
1863 std::string key = GenerateBundleLablel(bundleInfo, deviceType);
1864 int32_t result = PutDataToDB(key, enabled, userId);
1865 ANS_LOGD("result[%{public}d]", result);
1866 return (result == NativeRdb::E_OK);
1867 }
1868
GenerateBundleLablel( const NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &deviceType) const1869 std::string NotificationPreferencesDatabase::GenerateBundleLablel(
1870 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &deviceType) const
1871 {
1872 return std::string(KEY_BUNDLE_DISTRIBUTED_ENABLE_NOTIFICATION).append(KEY_MIDDLE_LINE).append(
1873 std::string(bundleInfo.GetBundleName()).append(KEY_MIDDLE_LINE).append(std::to_string(
1874 bundleInfo.GetBundleUid())).append(KEY_MIDDLE_LINE).append(deviceType));
1875 }
1876
1877 template <typename T>
PutDataToDB(const std::string &key, const T &value, const int32_t &userId)1878 int32_t NotificationPreferencesDatabase::PutDataToDB(const std::string &key, const T &value, const int32_t &userId)
1879 {
1880 if (!CheckRdbStore()) {
1881 ANS_LOGE("RdbStore is nullptr.");
1882 return false;
1883 }
1884 std::string valueStr = std::to_string(value);
1885 int32_t result = rdbDataManager_->InsertData(key, valueStr, userId);
1886 return result;
1887 }
1888
GetDistributedEnabledForBundle(const std::string deviceType, const NotificationPreferencesInfo::BundleInfo &bundleInfo, bool &enabled)1889 bool NotificationPreferencesDatabase::GetDistributedEnabledForBundle(const std::string deviceType,
1890 const NotificationPreferencesInfo::BundleInfo &bundleInfo, bool &enabled)
1891 {
1892 ANS_LOGD("%{public}s, deviceType:%{public}s,enabled[%{public}d]", __FUNCTION__, deviceType.c_str(), enabled);
1893 if (bundleInfo.GetBundleName().empty()) {
1894 ANS_LOGE("Bundle name is null.");
1895 return false;
1896 }
1897
1898 std::string key = GenerateBundleLablel(bundleInfo, deviceType);
1899 bool result = false;
1900 enabled = false;
1901 int32_t userId = -1;
1902 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
1903 GetValueFromDisturbeDB(key, userId, [&](const int32_t &status, std::string &value) {
1904 switch (status) {
1905 case NativeRdb::E_EMPTY_VALUES_BUCKET: {
1906 result = true;
1907 enabled = false;
1908 break;
1909 }
1910 case NativeRdb::E_OK: {
1911 result = true;
1912 enabled = static_cast<bool>(StringToInt(value));
1913 break;
1914 }
1915 default:
1916 result = false;
1917 break;
1918 }
1919 });
1920 ANS_LOGD("GetDistributedEnabledForBundle:enabled:[%{public}d]KEY:%{public}s", enabled, key.c_str());
1921 return result;
1922 }
1923
GenerateBundleLablel(const std::string &deviceType, const int32_t userId) const1924 std::string NotificationPreferencesDatabase::GenerateBundleLablel(const std::string &deviceType,
1925 const int32_t userId) const
1926 {
1927 return std::string(KEY_SMART_REMINDER_ENABLE_NOTIFICATION).append(KEY_MIDDLE_LINE).append(
1928 deviceType).append(KEY_MIDDLE_LINE).append(std::to_string(userId));
1929 }
1930
1931
SetSmartReminderEnabled(const std::string deviceType, const bool &enabled)1932 bool NotificationPreferencesDatabase::SetSmartReminderEnabled(const std::string deviceType, const bool &enabled)
1933 {
1934 ANS_LOGD("%{public}s, deviceType:%{public}s,enabled[%{public}d]", __FUNCTION__, deviceType.c_str(), enabled);
1935 int32_t userId = SUBSCRIBE_USER_INIT;
1936 OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
1937 if (userId == SUBSCRIBE_USER_INIT) {
1938 ANS_LOGE("Current user acquisition failed");
1939 return false;
1940 }
1941
1942 std::string key = GenerateBundleLablel(deviceType, userId);
1943 ANS_LOGD("%{public}s, key:%{public}s,enabled[%{public}d]", __FUNCTION__, key.c_str(), enabled);
1944 int32_t result = PutDataToDB(key, enabled, userId);
1945 return (result == NativeRdb::E_OK);
1946 }
1947
IsSmartReminderEnabled(const std::string deviceType, bool &enabled)1948 bool NotificationPreferencesDatabase::IsSmartReminderEnabled(const std::string deviceType, bool &enabled)
1949 {
1950 ANS_LOGD("%{public}s, deviceType:%{public}s,enabled[%{public}d]", __FUNCTION__, deviceType.c_str(), enabled);
1951 int32_t userId = SUBSCRIBE_USER_INIT;
1952 OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
1953 if (userId == SUBSCRIBE_USER_INIT) {
1954 ANS_LOGE("Current user acquisition failed");
1955 return false;
1956 }
1957
1958 std::string key = GenerateBundleLablel(deviceType, userId);
1959 bool result = false;
1960 enabled = false;
1961 GetValueFromDisturbeDB(key, userId, [&](const int32_t &status, std::string &value) {
1962 switch (status) {
1963 case NativeRdb::E_EMPTY_VALUES_BUCKET: {
1964 result = true;
1965 enabled = false;
1966 break;
1967 }
1968 case NativeRdb::E_OK: {
1969 result = true;
1970 enabled = static_cast<bool>(StringToInt(value));
1971 break;
1972 }
1973 default:
1974 result = false;
1975 break;
1976 }
1977 });
1978 return result;
1979 }
1980
GetAdditionalConfig(const std::string &key)1981 std::string NotificationPreferencesDatabase::GetAdditionalConfig(const std::string &key)
1982 {
1983 if (!CheckRdbStore()) {
1984 ANS_LOGE("RdbStore is nullptr.");
1985 return "";
1986 }
1987 std::string configValue = "";
1988 int32_t result = rdbDataManager_->QueryData(key, configValue);
1989 if (result != NativeRdb::E_OK) {
1990 ANS_LOGE("Query additional config failed.");
1991 return "";
1992 }
1993 ANS_LOGD("The additional config key is :%{public}s, value is :%{public}s.", key.c_str(), configValue.c_str());
1994 return configValue;
1995 }
1996
CheckApiCompatibility(const std::string &bundleName, const int32_t &uid)1997 bool NotificationPreferencesDatabase::CheckApiCompatibility(const std::string &bundleName, const int32_t &uid)
1998 {
1999 ANS_LOGD("%{public}s", __FUNCTION__);
2000 std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
2001 if (bundleManager == nullptr) {
2002 return false;
2003 }
2004 return bundleManager->CheckApiCompatibility(bundleName, uid);
2005 }
2006
UpdateBundlePropertyToDisturbeDB(int32_t userId, const NotificationPreferencesInfo::BundleInfo &bundleInfo)2007 bool NotificationPreferencesDatabase::UpdateBundlePropertyToDisturbeDB(int32_t userId,
2008 const NotificationPreferencesInfo::BundleInfo &bundleInfo)
2009 {
2010 if (bundleInfo.GetBundleName().empty()) {
2011 ANS_LOGE("Bundle name is null.");
2012 return false;
2013 }
2014
2015 if (!CheckRdbStore()) {
2016 ANS_LOGE("RdbStore is nullptr.");
2017 return false;
2018 }
2019 std::string value;
2020 std::string bundleLabelKey = KEY_BUNDLE_LABEL + GenerateBundleLablel(bundleInfo);
2021 int32_t result = rdbDataManager_->QueryData(bundleLabelKey, value, userId);
2022 if (result == NativeRdb::E_EMPTY_VALUES_BUCKET) {
2023 if (rdbDataManager_->InsertData(bundleLabelKey, GenerateBundleLablel(bundleInfo), userId)
2024 != NativeRdb::E_OK) {
2025 ANS_LOGE("Store bundle name %{public}s to db is failed.", bundleLabelKey.c_str());
2026 return false;
2027 }
2028 }
2029 if (result == NativeRdb::E_EMPTY_VALUES_BUCKET || result == NativeRdb::E_OK) {
2030 return PutBundlePropertyValueToDisturbeDB(bundleInfo);
2031 }
2032 ANS_LOGW("Query bundle name %{public}s failed %{public}d.", bundleLabelKey.c_str(), result);
2033 return false;
2034 }
2035
UpdateBundleSlotToDisturbeDB(int32_t userId, const std::string &bundleName, const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots)2036 bool NotificationPreferencesDatabase::UpdateBundleSlotToDisturbeDB(int32_t userId, const std::string &bundleName,
2037 const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots)
2038 {
2039 if (bundleName.empty()) {
2040 ANS_LOGE("Bundle name is null.");
2041 return false;
2042 }
2043 if (slots.empty()) {
2044 ANS_LOGI("Slot is empty.");
2045 return true;
2046 }
2047
2048 std::string bundleKey = bundleName + std::to_string(bundleUid);
2049 std::unordered_map<std::string, std::string> values;
2050 for (auto& slot : slots) {
2051 GenerateSlotEntry(bundleKey, slot, values);
2052 }
2053 if (!CheckRdbStore()) {
2054 ANS_LOGE("RdbStore is nullptr.");
2055 return false;
2056 }
2057 int32_t result = rdbDataManager_->InsertBatchData(values, userId);
2058 return (result == NativeRdb::E_OK);
2059 }
2060 } // namespace Notification
2061 } // namespace OHOS
2062