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