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
IsNotificationSlotFlagsExists(const sptr<NotificationBundleOption> & bundleOption)316 bool NotificationPreferencesDatabase::IsNotificationSlotFlagsExists(const sptr<NotificationBundleOption> &bundleOption)
317 {
318 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
319 return false;
320 }
321 std::string bundleKey = bundleOption->GetBundleName().append(std::to_string(bundleOption->GetUid()));
322 std::string key = GenerateBundleKey(bundleKey, KEY_BUNDLE_SLOTFLGS_TYPE);
323 std::string value;
324 int32_t userId = -1;
325 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleOption->GetUid(), userId);
326 int32_t result = rdbDataManager_->QueryData(key, value, userId);
327 return (result == NativeRdb::E_OK) || (!value.empty());
328 }
329
PutShowBadge(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & enable)330 bool NotificationPreferencesDatabase::PutShowBadge(
331 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable)
332 {
333 if (bundleInfo.GetBundleName().empty()) {
334 ANS_LOGE("Bundle name is nullptr.");
335 return false;
336 }
337
338 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
339 return false;
340 }
341
342 std::string bundleKey = GenerateBundleLablel(bundleInfo);
343 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable,
344 bundleInfo.GetBundleUid());
345 return (result == NativeRdb::E_OK);
346 }
347
PutImportance(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const int32_t & importance)348 bool NotificationPreferencesDatabase::PutImportance(
349 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &importance)
350 {
351 if (bundleInfo.GetBundleName().empty()) {
352 ANS_LOGE("Bundle name is empty.");
353 return false;
354 }
355
356 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
357 return false;
358 }
359
360 std::string bundleKey = GenerateBundleLablel(bundleInfo);
361 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_IMPORTANCE_TYPE, importance,
362 bundleInfo.GetBundleUid());
363 return (result == NativeRdb::E_OK);
364 }
365
PutTotalBadgeNums(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const int32_t & totalBadgeNum)366 bool NotificationPreferencesDatabase::PutTotalBadgeNums(
367 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &totalBadgeNum)
368 {
369 if (bundleInfo.GetBundleName().empty()) {
370 ANS_LOGE("Bundle name is blank.");
371 return false;
372 }
373
374 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
375 return false;
376 }
377 std::string bundleKey = GenerateBundleLablel(bundleInfo);
378 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum,
379 bundleInfo.GetBundleUid());
380 return (result == NativeRdb::E_OK);
381 }
382
PutNotificationsEnabledForBundle(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & enabled)383 bool NotificationPreferencesDatabase::PutNotificationsEnabledForBundle(
384 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled)
385 {
386 ANS_LOGD("%{public}s, enabled[%{public}d]", __FUNCTION__, enabled);
387 if (bundleInfo.GetBundleName().empty()) {
388 ANS_LOGE("Bundle name is null.");
389 return false;
390 }
391
392 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
393 return false;
394 }
395
396 std::string bundleKey = GenerateBundleLablel(bundleInfo);
397 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled,
398 bundleInfo.GetBundleUid());
399 return (result == NativeRdb::E_OK);
400 }
401
PutNotificationsEnabled(const int32_t & userId,const bool & enabled)402 bool NotificationPreferencesDatabase::PutNotificationsEnabled(const int32_t &userId, const bool &enabled)
403 {
404 if (!CheckRdbStore()) {
405 ANS_LOGE("RdbStore is nullptr.");
406 return false;
407 }
408
409 std::string typeKey =
410 std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
411 std::string enableValue = std::to_string(enabled);
412 int32_t result = rdbDataManager_->InsertData(typeKey, enableValue, userId);
413 if (result != NativeRdb::E_OK) {
414 ANS_LOGE("Store enable notification failed. %{public}d", result);
415 return false;
416 }
417 return true;
418 }
419
PutSlotFlags(NotificationPreferencesInfo::BundleInfo & bundleInfo,const int32_t & slotFlags)420 bool NotificationPreferencesDatabase::PutSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo,
421 const int32_t &slotFlags)
422 {
423 if (!CheckRdbStore()) {
424 ANS_LOGE("RdbStore is nullptr.");
425 return false;
426 }
427
428 std::string bundleKey = GenerateBundleLablel(bundleInfo);
429 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_SLOTFLGS_TYPE, slotFlags,
430 bundleInfo.GetBundleUid());
431 return (result == NativeRdb::E_OK);
432 }
433
PutHasPoppedDialog(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & hasPopped)434 bool NotificationPreferencesDatabase::PutHasPoppedDialog(
435 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped)
436 {
437 if (bundleInfo.GetBundleName().empty()) {
438 ANS_LOGE("Bundle name is null.");
439 return false;
440 }
441
442 if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) {
443 return false;
444 }
445
446 std::string bundleKey = GenerateBundleLablel(bundleInfo);
447 int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped,
448 bundleInfo.GetBundleUid());
449 return (result == NativeRdb::E_OK);
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 std::vector<int32_t> ids;
836 ErrCode result = ERR_OK;
837 result = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
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 constexpr int FIRST_USERID = 0;
863 result =
864 OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(StringToInt(uidItem->second), userid);
865 if (result != ERR_OK) {
866 return false;
867 }
868 if (userid != ids[FIRST_USERID]) {
869 continue;
870 }
871 NotificationBundleOption obj(value, StringToInt(uidItem->second));
872 bundleOption.emplace_back(obj);
873 }
874 }
875 return true;
876 }
877
RemoveAllSlotsFromDisturbeDB(const std::string & bundleKey,const int32_t & bundleUid)878 bool NotificationPreferencesDatabase::RemoveAllSlotsFromDisturbeDB(
879 const std::string &bundleKey, const int32_t &bundleUid)
880 {
881 ANS_LOGD("%{public}s", __FUNCTION__);
882 int32_t userId = -1;
883 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
884 if (bundleKey.empty()) {
885 ANS_LOGE("Bundle name is null.");
886 return false;
887 }
888
889 if (!CheckRdbStore()) {
890 ANS_LOGE("RdbStore is nullptr.");
891 return false;
892 }
893
894 std::unordered_map<std::string, std::string> values;
895 int32_t result = rdbDataManager_->QueryDataBeginWithKey(
896 (GenerateSlotKey(bundleKey) + KEY_UNDER_LINE), values, userId);
897 if (result == NativeRdb::E_ERROR) {
898 return false;
899 }
900 std::vector<std::string> keys;
901 for (auto iter : values) {
902 keys.push_back(iter.first);
903 }
904
905 result = rdbDataManager_->DeleteBathchData(keys, userId);
906 return (result == NativeRdb::E_OK);
907 }
908
909 template <typename T>
PutBundlePropertyToDisturbeDB(const std::string & bundleKey,const BundleType & type,const T & t,const int32_t & bundleUid)910 int32_t NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB(
911 const std::string &bundleKey, const BundleType &type, const T &t, const int32_t &bundleUid)
912 {
913 std::string keyStr;
914 switch (type) {
915 case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
916 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_BADGE_TOTAL_NUM);
917 break;
918 case BundleType::BUNDLE_IMPORTANCE_TYPE:
919 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_IMPORTANCE);
920 break;
921 case BundleType::BUNDLE_SHOW_BADGE_TYPE:
922 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE);
923 break;
924 case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
925 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION);
926 break;
927 case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
928 ANS_LOGD("Into BUNDLE_POPPED_DIALOG_TYPE:GenerateBundleKey.");
929 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_POPPED_DIALOG);
930 break;
931 case BundleType::BUNDLE_SLOTFLGS_TYPE:
932 ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:GenerateBundleKey.");
933 keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SLOTFLGS_TYPE);
934 break;
935 default:
936 break;
937 }
938 if (!CheckRdbStore()) {
939 ANS_LOGE("RdbStore is nullptr.");
940 return false;
941 }
942 int32_t userId = -1;
943 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
944 std::string valueStr = std::to_string(t);
945 int32_t result = rdbDataManager_->InsertData(keyStr, valueStr, userId);
946 return result;
947 }
948
PutBundleToDisturbeDB(const std::string & bundleKey,const NotificationPreferencesInfo::BundleInfo & bundleInfo)949 bool NotificationPreferencesDatabase::PutBundleToDisturbeDB(
950 const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo)
951 {
952 if (!CheckRdbStore()) {
953 ANS_LOGE("RdbStore is nullptr.");
954 return false;
955 }
956 int32_t userId = -1;
957 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
958
959 ANS_LOGD("Key not fund, so create a bundle, bundle key is %{public}s.", bundleKey.c_str());
960 int32_t result = rdbDataManager_->InsertData(bundleKey, GenerateBundleLablel(bundleInfo), userId);
961 if (result != NativeRdb::E_OK) {
962 ANS_LOGE("Store bundle name to db is failed.");
963 return false;
964 }
965
966 if (!PutBundlePropertyValueToDisturbeDB(bundleInfo)) {
967 return false;
968 }
969 return true;
970 }
971
GenerateEntry(const std::string & key,const std::string & value,std::unordered_map<std::string,std::string> & values) const972 void NotificationPreferencesDatabase::GenerateEntry(
973 const std::string &key, const std::string &value, std::unordered_map<std::string, std::string> &values) const
974 {
975 values.emplace(key, value);
976 }
977
SlotToEntry(const std::string & bundleName,const int32_t & bundleUid,const sptr<NotificationSlot> & slot,std::unordered_map<std::string,std::string> & values)978 bool NotificationPreferencesDatabase::SlotToEntry(const std::string &bundleName, const int32_t &bundleUid,
979 const sptr<NotificationSlot> &slot, std::unordered_map<std::string, std::string> &values)
980 {
981 if (slot == nullptr) {
982 ANS_LOGE("Notification slot is nullptr.");
983 return false;
984 }
985
986 if (!CheckBundle(bundleName, bundleUid)) {
987 return false;
988 }
989
990 std::string bundleKey = bundleName + std::to_string(bundleUid);
991 GenerateSlotEntry(bundleKey, slot, values);
992 return true;
993 }
994
GenerateSlotEntry(const std::string & bundleKey,const sptr<NotificationSlot> & slot,std::unordered_map<std::string,std::string> & values) const995 void NotificationPreferencesDatabase::GenerateSlotEntry(const std::string &bundleKey,
996 const sptr<NotificationSlot> &slot, std::unordered_map<std::string, std::string> &values) const
997 {
998 std::string slotType = std::to_string(slot->GetType());
999 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_TYPE), std::to_string(slot->GetType()), values);
1000 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ID), slot->GetId(), values);
1001 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_NAME), slot->GetName(), values);
1002 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_DESCRIPTION), slot->GetDescription(), values);
1003 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LEVEL), std::to_string(slot->GetLevel()), values);
1004 GenerateEntry(
1005 GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SHOW_BADGE), std::to_string(slot->IsShowBadge()), values);
1006 GenerateEntry(
1007 GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_LIGHT), std::to_string(slot->CanEnableLight()), values);
1008 GenerateEntry(
1009 GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_VRBRATION), std::to_string(slot->CanVibrate()), values);
1010 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LED_LIGHT_COLOR),
1011 std::to_string(slot->GetLedLightColor()), values);
1012 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LOCKSCREEN_VISIBLENESS),
1013 std::to_string(static_cast<int>(slot->GetLockScreenVisibleness())), values);
1014 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SOUND), slot->GetSound().ToString(), values);
1015 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_BYPASS_DND),
1016 std::to_string(slot->IsEnableBypassDnd()), values);
1017 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_VIBRATION_STYLE),
1018 VectorToString(slot->GetVibrationStyle()), values);
1019 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLED), std::to_string(slot->GetEnable()), values);
1020 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_AUTHORIZED_STATUS),
1021 std::to_string(slot->GetAuthorizedStatus()), values);
1022 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_AUTH_HINT_CNT),
1023 std::to_string(slot->GetAuthHintCnt()), values);
1024 GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_REMINDER_MODE),
1025 std::to_string(slot->GetReminderMode()), values);
1026 }
1027
ParseBundleFromDistureDB(NotificationPreferencesInfo & info,const std::unordered_map<std::string,std::string> & values,const int32_t & userId)1028 void NotificationPreferencesDatabase::ParseBundleFromDistureDB(NotificationPreferencesInfo &info,
1029 const std::unordered_map<std::string, std::string> &values, const int32_t &userId)
1030 {
1031 if (!CheckRdbStore()) {
1032 ANS_LOGE("RdbStore is nullptr.");
1033 return;
1034 }
1035 for (auto item : values) {
1036 std::string bundleKey = item.second;
1037 ANS_LOGD("Bundle name is %{public}s.", bundleKey.c_str());
1038 std::unordered_map<std::string, std::string> bundleEntries;
1039 rdbDataManager_->QueryDataBeginWithKey((GenerateBundleKey(bundleKey)), bundleEntries, userId);
1040 ANS_LOGD("Bundle key is %{public}s.", GenerateBundleKey(bundleKey).c_str());
1041 NotificationPreferencesInfo::BundleInfo bunldeInfo;
1042 std::string keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE);
1043 bool badgeEnableExist = false;
1044 for (auto bundleEntry : bundleEntries) {
1045 if (IsSlotKey(GenerateBundleKey(bundleKey), bundleEntry.first)) {
1046 ParseSlotFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry, userId);
1047 } else {
1048 ParseBundlePropertyFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry);
1049 }
1050
1051 if (keyStr.compare(bundleEntry.first) == 0) {
1052 badgeEnableExist = true;
1053 }
1054 }
1055
1056 if (!badgeEnableExist) {
1057 bunldeInfo.SetIsShowBadge(static_cast<bool>(true));
1058 }
1059
1060 info.SetBundleInfoFromDb(bunldeInfo, bundleKey);
1061 }
1062 }
1063
ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & bundleKey,const std::pair<std::string,std::string> & entry,const int32_t & userId)1064 void NotificationPreferencesDatabase::ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo,
1065 const std::string &bundleKey, const std::pair<std::string, std::string> &entry, const int32_t &userId)
1066 {
1067 std::string slotKey = entry.first;
1068 std::string typeStr = SubUniqueIdentifyFromString(GenerateSlotKey(bundleKey) + KEY_UNDER_LINE, slotKey);
1069 NotificationConstant::SlotType slotType = static_cast<NotificationConstant::SlotType>(StringToInt(typeStr));
1070 sptr<NotificationSlot> slot = nullptr;
1071 if (!bundleInfo.GetSlot(slotType, slot)) {
1072 slot = new (std::nothrow) NotificationSlot(slotType);
1073 if (slot == nullptr) {
1074 ANS_LOGE("Failed to create NotificationSlot instance");
1075 return;
1076 }
1077 }
1078 std::string findString = GenerateSlotKey(bundleKey, typeStr) + KEY_UNDER_LINE;
1079 ParseSlot(findString, slot, entry, userId);
1080 bundleInfo.SetSlot(slot);
1081 }
1082
ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & bundleKey,const std::pair<std::string,std::string> & entry)1083 void NotificationPreferencesDatabase::ParseBundlePropertyFromDisturbeDB(
1084 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey,
1085 const std::pair<std::string, std::string> &entry)
1086 {
1087 std::string typeStr = FindLastString(GenerateBundleKey(bundleKey), entry.first);
1088 std::string valueStr = entry.second;
1089
1090 if (typeStr.compare(KEY_BUNDLE_NAME) == 0) {
1091 return ParseBundleName(bundleInfo, valueStr);
1092 }
1093 if (typeStr.compare(KEY_BUNDLE_IMPORTANCE) == 0) {
1094 return ParseBundleImportance(bundleInfo, valueStr);
1095 }
1096 if (typeStr.compare(KEY_BUNDLE_SHOW_BADGE) == 0) {
1097 return ParseBundleShowBadgeEnable(bundleInfo, valueStr);
1098 }
1099 if (typeStr.compare(KEY_BUNDLE_BADGE_TOTAL_NUM) == 0) {
1100 return ParseBundleBadgeNum(bundleInfo, valueStr);
1101 }
1102 if (typeStr.compare(KEY_BUNDLE_ENABLE_NOTIFICATION) == 0) {
1103 return ParseBundleEnableNotification(bundleInfo, valueStr);
1104 }
1105 if (typeStr.compare(KEY_BUNDLE_POPPED_DIALOG) == 0) {
1106 return ParseBundlePoppedDialog(bundleInfo, valueStr);
1107 }
1108 if (typeStr.compare(KEY_BUNDLE_UID) == 0) {
1109 return ParseBundleUid(bundleInfo, valueStr);
1110 }
1111 if (typeStr.compare(KEY_BUNDLE_SLOTFLGS_TYPE) == 0) {
1112 return ParseBundleSlotFlags(bundleInfo, valueStr);
1113 }
1114 }
1115
ParseSlot(const std::string & findString,sptr<NotificationSlot> & slot,const std::pair<std::string,std::string> & entry,const int32_t & userId)1116 void NotificationPreferencesDatabase::ParseSlot(const std::string &findString, sptr<NotificationSlot> &slot,
1117 const std::pair<std::string, std::string> &entry, const int32_t &userId)
1118 {
1119 std::string typeStr = FindLastString(findString, entry.first);
1120 std::string valueStr = entry.second;
1121 ANS_LOGD("db key = %{public}s , %{public}s : %{public}s ",
1122 entry.first.c_str(),
1123 typeStr.c_str(),
1124 entry.second.c_str());
1125 SetSoltProperty(slot, typeStr, valueStr, findString, userId);
1126 }
1127
SetSoltProperty(sptr<NotificationSlot> & slot,std::string & typeStr,std::string & valueStr,const std::string & findString,const int32_t & userId)1128 void NotificationPreferencesDatabase::SetSoltProperty(sptr<NotificationSlot> &slot, std::string &typeStr,
1129 std::string &valueStr, const std::string &findString, const int32_t &userId)
1130 {
1131 if (typeStr.compare(KEY_SLOT_DESCRIPTION) == 0) {
1132 return ParseSlotDescription(slot, valueStr);
1133 }
1134 if (typeStr.compare(KEY_SLOT_LEVEL) == 0) {
1135 return ParseSlotLevel(slot, valueStr);
1136 }
1137 if (typeStr.compare(KEY_SLOT_SHOW_BADGE) == 0) {
1138 return ParseSlotShowBadge(slot, valueStr);
1139 }
1140 if (typeStr.compare(KEY_SLOT_ENABLE_LIGHT) == 0) {
1141 return ParseSlotEnableLight(slot, valueStr);
1142 }
1143 if (typeStr.compare(KEY_SLOT_ENABLE_VRBRATION) == 0) {
1144 return ParseSlotEnableVrbration(slot, valueStr);
1145 }
1146 if (typeStr.compare(KEY_SLOT_LED_LIGHT_COLOR) == 0) {
1147 return ParseSlotLedLightColor(slot, valueStr);
1148 }
1149 if (typeStr.compare(KEY_SLOT_LOCKSCREEN_VISIBLENESS) == 0) {
1150 return ParseSlotLockscreenVisibleness(slot, valueStr);
1151 }
1152 if (typeStr.compare(KEY_SLOT_SOUND) == 0) {
1153 return ParseSlotSound(slot, valueStr);
1154 }
1155 if (typeStr.compare(KEY_SLOT_VIBRATION_STYLE) == 0) {
1156 return ParseSlotVibrationSytle(slot, valueStr);
1157 }
1158 if (typeStr.compare(KEY_SLOT_ENABLE_BYPASS_DND) == 0) {
1159 return ParseSlotEnableBypassDnd(slot, valueStr);
1160 }
1161 if (typeStr.compare(KEY_SLOT_ENABLED) == 0) {
1162 return ParseSlotEnabled(slot, valueStr);
1163 }
1164 if (typeStr.compare(KEY_SLOT_SLOTFLGS_TYPE) == 0) {
1165 return ParseSlotFlags(slot, valueStr);
1166 }
1167 if (typeStr.compare(KEY_SLOT_AUTHORIZED_STATUS) == 0) {
1168 return ParseSlotAuthorizedStatus(slot, valueStr);
1169 }
1170 if (typeStr.compare(KEY_SLOT_AUTH_HINT_CNT) == 0) {
1171 return ParseSlotAuthHitnCnt(slot, valueStr);
1172 }
1173 ExecuteDisturbeDB(slot, typeStr, valueStr, findString, userId);
1174 }
1175
ExecuteDisturbeDB(sptr<NotificationSlot> & slot,std::string & typeStr,std::string & valueStr,const std::string & findString,const int32_t & userId)1176 void NotificationPreferencesDatabase::ExecuteDisturbeDB(sptr<NotificationSlot> &slot, std::string &typeStr,
1177 std::string &valueStr, const std::string &findString, const int32_t &userId)
1178 {
1179 if (typeStr.compare(KEY_REMINDER_MODE) == 0) {
1180 return ParseSlotReminderMode(slot, valueStr);
1181 }
1182 if (!typeStr.compare(KEY_SLOT_VIBRATION_STYLE)) {
1183 GetValueFromDisturbeDB(findString + KEY_SLOT_ENABLE_VRBRATION, userId,
1184 [&](std::string &value) { ParseSlotEnableVrbration(slot, value); });
1185 }
1186 }
1187
FindLastString(const std::string & findString,const std::string & inputString) const1188 std::string NotificationPreferencesDatabase::FindLastString(
1189 const std::string &findString, const std::string &inputString) const
1190 {
1191 std::string keyStr;
1192 size_t pos = findString.size();
1193 if (pos != std::string::npos) {
1194 keyStr = inputString.substr(pos);
1195 }
1196 return keyStr;
1197 }
1198
VectorToString(const std::vector<int64_t> & data) const1199 std::string NotificationPreferencesDatabase::VectorToString(const std::vector<int64_t> &data) const
1200 {
1201 std::stringstream streamStr;
1202 std::copy(data.begin(), data.end(), std::ostream_iterator<int>(streamStr, KEY_UNDER_LINE.c_str()));
1203 return streamStr.str();
1204 }
1205
StringToVector(const std::string & str,std::vector<int64_t> & data) const1206 void NotificationPreferencesDatabase::StringToVector(const std::string &str, std::vector<int64_t> &data) const
1207 {
1208 if (str.empty()) {
1209 return;
1210 }
1211
1212 if (str.find_first_of(KEY_UNDER_LINE) != std::string::npos) {
1213 std::string str1 = str.substr(0, str.find_first_of(KEY_UNDER_LINE));
1214 std::string afterStr = str.substr(str.find_first_of(KEY_UNDER_LINE) + 1);
1215 data.push_back(StringToInt(str1));
1216 StringToVector(afterStr, data);
1217 }
1218 }
1219
StringToInt(const std::string & str) const1220 int32_t NotificationPreferencesDatabase::StringToInt(const std::string &str) const
1221 {
1222 int32_t value = 0;
1223 if (!str.empty()) {
1224 value = stoi(str, nullptr);
1225 }
1226 return value;
1227 }
1228
StringToInt64(const std::string & str) const1229 int64_t NotificationPreferencesDatabase::StringToInt64(const std::string &str) const
1230 {
1231 int64_t value = 0;
1232 if (!str.empty()) {
1233 value = stoll(str, nullptr);
1234 }
1235 return value;
1236 }
1237
IsSlotKey(const std::string & bundleKey,const std::string & key) const1238 bool NotificationPreferencesDatabase::IsSlotKey(const std::string &bundleKey, const std::string &key) const
1239 {
1240 std::string tempStr = FindLastString(bundleKey, key);
1241 size_t pos = tempStr.find_first_of(KEY_UNDER_LINE);
1242 std::string slotStr;
1243 if (pos != std::string::npos) {
1244 slotStr = tempStr.substr(0, pos);
1245 }
1246 if (!slotStr.compare(KEY_SLOT)) {
1247 return true;
1248 }
1249 return false;
1250 }
1251
GenerateSlotKey(const std::string & bundleKey,const std::string & type,const std::string & subType) const1252 std::string NotificationPreferencesDatabase::GenerateSlotKey(
1253 const std::string &bundleKey, const std::string &type, const std::string &subType) const
1254 {
1255 /* slot key
1256 *
1257 * KEY_ANS_BUNDLE_bundlename_slot_type_0_id
1258 * KEY_ANS_BUNDLE_bundlename_slot_type_0_des
1259 * KEY_ANS_BUNDLE_bundlename_slot_type_1_id
1260 * KEY_ANS_BUNDLE_bundlename_slot_type_1_des
1261 *
1262 */
1263 std::string key = GenerateBundleKey(bundleKey).append(KEY_SLOT).append(KEY_UNDER_LINE).append(KEY_SLOT_TYPE);
1264 if (!type.empty()) {
1265 key.append(KEY_UNDER_LINE).append(type);
1266 }
1267 if (!subType.empty()) {
1268 key.append(KEY_UNDER_LINE).append(subType);
1269 }
1270 ANS_LOGD("Slot key is : %{public}s.", key.c_str());
1271 return key;
1272 }
1273
GenerateBundleKey(const std::string & bundleKey,const std::string & type) const1274 std::string NotificationPreferencesDatabase::GenerateBundleKey(
1275 const std::string &bundleKey, const std::string &type) const
1276 {
1277 /* bundle key
1278 *
1279 * label_KEY_ANS_KEY_BUNDLE_NAME = ""
1280 * KEY_ANS_BUNDLE_bundlename_
1281 * KEY_ANS_BUNDLE_bundlename_
1282 * KEY_ANS_BUNDLE_bundlename_
1283 * KEY_ANS_BUNDLE_bundlename_
1284 *
1285 */
1286 ANS_LOGD("%{public}s, bundleKey[%{public}s] type[%{public}s]", __FUNCTION__, bundleKey.c_str(), type.c_str());
1287 std::string key =
1288 std::string().append(KEY_ANS_BUNDLE).append(KEY_UNDER_LINE).append(bundleKey).append(KEY_UNDER_LINE);
1289 if (!type.empty()) {
1290 key.append(type);
1291 }
1292 ANS_LOGD("Bundle key : %{public}s.", key.c_str());
1293 return key;
1294 }
1295
SubUniqueIdentifyFromString(const std::string & findString,const std::string & keyStr) const1296 std::string NotificationPreferencesDatabase::SubUniqueIdentifyFromString(
1297 const std::string &findString, const std::string &keyStr) const
1298 {
1299 std::string slotType;
1300 std::string tempStr = FindLastString(findString, keyStr);
1301 size_t pos = tempStr.find_last_of(KEY_UNDER_LINE);
1302 if (pos != std::string::npos) {
1303 slotType = tempStr.substr(0, pos);
1304 }
1305
1306 return slotType;
1307 }
1308
ParseBundleName(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1309 void NotificationPreferencesDatabase::ParseBundleName(
1310 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1311 {
1312 ANS_LOGD("SetBundleName bundle name is %{public}s.", value.c_str());
1313 bundleInfo.SetBundleName(value);
1314 }
1315
ParseBundleImportance(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1316 void NotificationPreferencesDatabase::ParseBundleImportance(
1317 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1318 {
1319 ANS_LOGD("SetBundleImportance bundle importance is %{public}s.", value.c_str());
1320 bundleInfo.SetImportance(static_cast<NotificationSlot::NotificationLevel>(StringToInt(value)));
1321 }
1322
ParseBundleShowBadgeEnable(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1323 void NotificationPreferencesDatabase::ParseBundleShowBadgeEnable(
1324 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1325 {
1326 ANS_LOGD("SetBundleShowBadge bundle show badge is %{public}s.", value.c_str());
1327 bundleInfo.SetIsShowBadge(static_cast<bool>(StringToInt(value)));
1328 }
1329
ParseBundleBadgeNum(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1330 void NotificationPreferencesDatabase::ParseBundleBadgeNum(
1331 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1332 {
1333 ANS_LOGD("SetBundleBadgeNum bundle badge num is %{public}s.", value.c_str());
1334 bundleInfo.SetBadgeTotalNum(StringToInt(value));
1335 }
1336
ParseBundleEnableNotification(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1337 void NotificationPreferencesDatabase::ParseBundleEnableNotification(
1338 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1339 {
1340 ANS_LOGD("SetBundleEnableNotification bundle enable is %{public}s.", value.c_str());
1341 bundleInfo.SetEnableNotification(static_cast<bool>(StringToInt(value)));
1342 }
1343
ParseBundlePoppedDialog(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1344 void NotificationPreferencesDatabase::ParseBundlePoppedDialog(
1345 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1346 {
1347 ANS_LOGD("SetBundlePoppedDialog bundle has popped dialog is %{public}s.", value.c_str());
1348 bundleInfo.SetHasPoppedDialog(static_cast<bool>(StringToInt(value)));
1349 }
1350
ParseBundleUid(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1351 void NotificationPreferencesDatabase::ParseBundleUid(
1352 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const
1353 {
1354 ANS_LOGD("SetBundleUid uuid is %{public}s.", value.c_str());
1355 bundleInfo.SetBundleUid(StringToInt(value));
1356 }
1357
ParseSlotDescription(sptr<NotificationSlot> & slot,const std::string & value) const1358 void NotificationPreferencesDatabase::ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const
1359 {
1360 ANS_LOGD("ParseSlotDescription slot des is %{public}s.", value.c_str());
1361 std::string slotDescription = value;
1362 slot->SetDescription(slotDescription);
1363 }
1364
ParseSlotLevel(sptr<NotificationSlot> & slot,const std::string & value) const1365 void NotificationPreferencesDatabase::ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const
1366 {
1367 ANS_LOGD("ParseSlotLevel slot level is %{public}s.", value.c_str());
1368 NotificationSlot::NotificationLevel level = static_cast<NotificationSlot::NotificationLevel>(StringToInt(value));
1369 slot->SetLevel(level);
1370 }
1371
ParseSlotShowBadge(sptr<NotificationSlot> & slot,const std::string & value) const1372 void NotificationPreferencesDatabase::ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const
1373 {
1374 ANS_LOGD("ParseSlotShowBadge slot show badge is %{public}s.", value.c_str());
1375 bool showBadge = static_cast<bool>(StringToInt(value));
1376 slot->EnableBadge(showBadge);
1377 }
1378
ParseSlotFlags(sptr<NotificationSlot> & slot,const std::string & value) const1379 void NotificationPreferencesDatabase::ParseSlotFlags(sptr<NotificationSlot> &slot, const std::string &value) const
1380 {
1381 ANS_LOGD("ParseSlotFlags slot show flags is %{public}s.", value.c_str());
1382 uint32_t slotFlags = static_cast<uint32_t>(StringToInt(value));
1383 slot->SetSlotFlags(slotFlags);
1384 }
1385
ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & value) const1386 void NotificationPreferencesDatabase::ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo,
1387 const std::string &value) const
1388 {
1389 ANS_LOGD("ParseBundleSlotFlags slot show flags is %{public}s.", value.c_str());
1390 bundleInfo.SetSlotFlags(StringToInt(value));
1391 }
1392
ParseSlotEnableLight(sptr<NotificationSlot> & slot,const std::string & value) const1393 void NotificationPreferencesDatabase::ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const
1394 {
1395 ANS_LOGD("ParseSlotEnableLight slot enable light is %{public}s.", value.c_str());
1396 bool enableLight = static_cast<bool>(StringToInt(value));
1397 slot->SetEnableLight(enableLight);
1398 }
1399
ParseSlotEnableVrbration(sptr<NotificationSlot> & slot,const std::string & value) const1400 void NotificationPreferencesDatabase::ParseSlotEnableVrbration(
1401 sptr<NotificationSlot> &slot, const std::string &value) const
1402 {
1403 ANS_LOGD("ParseSlotEnableVrbration slot enable vir is %{public}s.", value.c_str());
1404 bool enableVrbration = static_cast<bool>(StringToInt(value));
1405 slot->SetEnableVibration(enableVrbration);
1406 }
1407
ParseSlotLedLightColor(sptr<NotificationSlot> & slot,const std::string & value) const1408 void NotificationPreferencesDatabase::ParseSlotLedLightColor(
1409 sptr<NotificationSlot> &slot, const std::string &value) const
1410 {
1411 ANS_LOGD("ParseSlotLedLightColor slot led is %{public}s.", value.c_str());
1412 int32_t ledLightColor = static_cast<int32_t>(StringToInt(value));
1413 slot->SetLedLightColor(ledLightColor);
1414 }
1415
ParseSlotLockscreenVisibleness(sptr<NotificationSlot> & slot,const std::string & value) const1416 void NotificationPreferencesDatabase::ParseSlotLockscreenVisibleness(
1417 sptr<NotificationSlot> &slot, const std::string &value) const
1418 {
1419
1420 ANS_LOGD("ParseSlotLockscreenVisibleness slot visible is %{public}s.", value.c_str());
1421 NotificationConstant::VisiblenessType visible =
1422 static_cast<NotificationConstant::VisiblenessType>(StringToInt(value));
1423 slot->SetLockscreenVisibleness(visible);
1424 }
1425
ParseSlotSound(sptr<NotificationSlot> & slot,const std::string & value) const1426 void NotificationPreferencesDatabase::ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const
1427 {
1428 ANS_LOGD("ParseSlotSound slot sound is %{public}s.", value.c_str());
1429 std::string slotUri = value;
1430 Uri uri(slotUri);
1431 slot->SetSound(uri);
1432 }
1433
ParseSlotVibrationSytle(sptr<NotificationSlot> & slot,const std::string & value) const1434 void NotificationPreferencesDatabase::ParseSlotVibrationSytle(
1435 sptr<NotificationSlot> &slot, const std::string &value) const
1436 {
1437 ANS_LOGD("ParseSlotVibrationSytle slot vibration style is %{public}s.", value.c_str());
1438 std::vector<int64_t> vibrationStyle;
1439 StringToVector(value, vibrationStyle);
1440 slot->SetVibrationStyle(vibrationStyle);
1441 }
1442
ParseSlotEnableBypassDnd(sptr<NotificationSlot> & slot,const std::string & value) const1443 void NotificationPreferencesDatabase::ParseSlotEnableBypassDnd(
1444 sptr<NotificationSlot> &slot, const std::string &value) const
1445 {
1446 ANS_LOGD("ParseSlotEnableBypassDnd slot by pass dnd is %{public}s.", value.c_str());
1447 bool enable = static_cast<bool>(StringToInt(value));
1448 slot->EnableBypassDnd(enable);
1449 }
1450
ParseSlotEnabled(sptr<NotificationSlot> & slot,const std::string & value) const1451 void NotificationPreferencesDatabase::ParseSlotEnabled(
1452 sptr<NotificationSlot> &slot, const std::string &value) const
1453 {
1454 ANS_LOGD("ParseSlotEnabled slot enabled is %{public}s.", value.c_str());
1455 bool enabled = static_cast<bool>(StringToInt(value));
1456 slot->SetEnable(enabled);
1457 }
1458
ParseSlotAuthorizedStatus(sptr<NotificationSlot> & slot,const std::string & value) const1459 void NotificationPreferencesDatabase::ParseSlotAuthorizedStatus(
1460 sptr<NotificationSlot> &slot, const std::string &value) const
1461 {
1462 ANS_LOGD("ParseSlotAuthorizedStatus slot status is %{public}s.", value.c_str());
1463 int32_t status = static_cast<int32_t>(StringToInt(value));
1464 slot->SetAuthorizedStatus(status);
1465 }
1466
ParseSlotAuthHitnCnt(sptr<NotificationSlot> & slot,const std::string & value) const1467 void NotificationPreferencesDatabase::ParseSlotAuthHitnCnt(
1468 sptr<NotificationSlot> &slot, const std::string &value) const
1469 {
1470 ANS_LOGD("ParseSlotAuthHitnCnt slot count is %{public}s.", value.c_str());
1471 int32_t count = static_cast<int32_t>(StringToInt(value));
1472 slot->SetAuthHintCnt(count);
1473 }
1474
ParseSlotReminderMode(sptr<NotificationSlot> & slot,const std::string & value) const1475 void NotificationPreferencesDatabase::ParseSlotReminderMode(
1476 sptr<NotificationSlot> &slot, const std::string &value) const
1477 {
1478 ANS_LOGD("ParseSlotReminderMode slot reminder mode is %{public}s.", value.c_str());
1479 int32_t reminderMode = static_cast<int32_t>(StringToInt(value));
1480 slot->SetReminderMode(reminderMode);
1481 }
1482
GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo & bundleInfo) const1483 std::string NotificationPreferencesDatabase::GenerateBundleLablel(
1484 const NotificationPreferencesInfo::BundleInfo &bundleInfo) const
1485 {
1486 return bundleInfo.GetBundleName().append(std::to_string(bundleInfo.GetBundleUid()));
1487 }
1488
GetDoNotDisturbType(NotificationPreferencesInfo & info,int32_t userId)1489 void NotificationPreferencesDatabase::GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId)
1490 {
1491 std::string key =
1492 std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1493 GetValueFromDisturbeDB(
1494 key, userId, [&](const int32_t &status, std::string &value) {
1495 sptr<NotificationDoNotDisturbDate> disturbDate = new (std::nothrow)
1496 NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1497 if (disturbDate == nullptr) {
1498 ANS_LOGE("Create NotificationDoNotDisturbDate instance fail.");
1499 return;
1500 }
1501 info.GetDoNotDisturbDate(userId, disturbDate);
1502 if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1503 PutDoNotDisturbDate(userId, disturbDate);
1504 } else if (status == NativeRdb::E_OK) {
1505 if (!value.empty()) {
1506 if (disturbDate != nullptr) {
1507 disturbDate->SetDoNotDisturbType(
1508 (NotificationConstant::DoNotDisturbType)StringToInt(value));
1509 }
1510 }
1511 } else {
1512 ANS_LOGW("Parse disturbe mode failed, use default value.");
1513 }
1514 info.SetDoNotDisturbDate(userId, disturbDate);
1515 });
1516 }
1517
GetDoNotDisturbBeginDate(NotificationPreferencesInfo & info,int32_t userId)1518 void NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId)
1519 {
1520 std::string key =
1521 std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1522 GetValueFromDisturbeDB(
1523 key, userId, [&](const int32_t &status, std::string &value) {
1524 sptr<NotificationDoNotDisturbDate> disturbDate = new (std::nothrow)
1525 NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1526 if (disturbDate == nullptr) {
1527 ANS_LOGE("Failed to create NotificationDoNotDisturbDate instance");
1528 return;
1529 }
1530 info.GetDoNotDisturbDate(userId, disturbDate);
1531 if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1532 PutDoNotDisturbDate(userId, disturbDate);
1533 } else if (status == NativeRdb::E_OK) {
1534 if (!value.empty()) {
1535 if (disturbDate != nullptr) {
1536 disturbDate->SetBeginDate(StringToInt64(value));
1537 }
1538 }
1539 } else {
1540 ANS_LOGW("Parse disturbe start time failed, use default value.");
1541 }
1542 info.SetDoNotDisturbDate(userId, disturbDate);
1543 });
1544 }
1545
GetDoNotDisturbEndDate(NotificationPreferencesInfo & info,int32_t userId)1546 void NotificationPreferencesDatabase::GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId)
1547 {
1548 std::string key =
1549 std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1550 GetValueFromDisturbeDB(
1551 key, userId, [&](const int32_t &status, std::string &value) {
1552 sptr<NotificationDoNotDisturbDate> disturbDate = new (std::nothrow)
1553 NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
1554 if (disturbDate == nullptr) {
1555 ANS_LOGE("Defeat to create NotificationDoNotDisturbDate instance");
1556 return;
1557 }
1558 info.GetDoNotDisturbDate(userId, disturbDate);
1559 if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1560 PutDoNotDisturbDate(userId, disturbDate);
1561 } else if (status == NativeRdb::E_OK) {
1562 if (!value.empty()) {
1563 if (disturbDate != nullptr) {
1564 disturbDate->SetEndDate(StringToInt64(value));
1565 }
1566 }
1567 } else {
1568 ANS_LOGW("Parse disturbe end time failed, use default value.");
1569 }
1570 info.SetDoNotDisturbDate(userId, disturbDate);
1571 });
1572 }
1573
GetEnableAllNotification(NotificationPreferencesInfo & info,int32_t userId)1574 void NotificationPreferencesDatabase::GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId)
1575 {
1576 std::string key =
1577 std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
1578 GetValueFromDisturbeDB(
1579 key, userId, [&](const int32_t &status, std::string &value) {
1580 if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1581 bool enable = true;
1582 if (!info.GetEnabledAllNotification(userId, enable)) {
1583 info.SetEnabledAllNotification(userId, enable);
1584 ANS_LOGW("Enable setting not found, default true.");
1585 }
1586 PutNotificationsEnabled(userId, enable);
1587 } else if (status == NativeRdb::E_OK) {
1588 if (!value.empty()) {
1589 info.SetEnabledAllNotification(userId, static_cast<bool>(StringToInt(value)));
1590 }
1591 } else {
1592 ANS_LOGW("Parse enable all notification failed, use default value.");
1593 }
1594 });
1595 }
1596
GetDoNotDisturbProfile(NotificationPreferencesInfo & info,int32_t userId)1597 void NotificationPreferencesDatabase::GetDoNotDisturbProfile(NotificationPreferencesInfo &info, int32_t userId)
1598 {
1599 if (!CheckRdbStore()) {
1600 ANS_LOGE("RdbStore is nullptr.");
1601 return;
1602 }
1603 std::unordered_map<std::string, std::string> datas;
1604 int32_t result = rdbDataManager_->QueryAllData(datas, userId);
1605 if (result != NativeRdb::E_OK) {
1606 ANS_LOGE("Query all data failed.");
1607 return;
1608 }
1609 std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
1610 for (const auto &data : datas) {
1611 std::string key = data.first;
1612 auto result = key.find(KEY_DO_NOT_DISTURB_ID);
1613 if (result != std::string::npos) {
1614 sptr<NotificationDoNotDisturbProfile> profile;
1615 GetDoNotDisturbProfiles(data.first, profile, userId);
1616 profiles.emplace_back(profile);
1617 }
1618 }
1619 info.AddDoNotDisturbProfiles(userId, profiles);
1620 }
1621
RemoveNotificationEnable(const int32_t userId)1622 bool NotificationPreferencesDatabase::RemoveNotificationEnable(const int32_t userId)
1623 {
1624 ANS_LOGD("%{public}s", __FUNCTION__);
1625 if (!CheckRdbStore()) {
1626 ANS_LOGE("RdbStore is nullptr.");
1627 return false;
1628 }
1629
1630 std::string key =
1631 std::string(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId));
1632 int32_t result = rdbDataManager_->DeleteData(key, userId);
1633 if (result != NativeRdb::E_OK) {
1634 ANS_LOGE("delete bundle Info failed.");
1635 return false;
1636 }
1637
1638 ANS_LOGD("%{public}s remove notification enable, userId : %{public}d", __FUNCTION__, userId);
1639 return true;
1640 }
1641
RemoveDoNotDisturbDate(const int32_t userId)1642 bool NotificationPreferencesDatabase::RemoveDoNotDisturbDate(const int32_t userId)
1643 {
1644 ANS_LOGD("%{public}s", __FUNCTION__);
1645 if (!CheckRdbStore()) {
1646 ANS_LOGE("RdbStore is nullptr.");
1647 return false;
1648 }
1649
1650 std::string typeKey =
1651 std::string(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1652 std::string beginDateKey =
1653 std::string(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1654 std::string endDateKey =
1655 std::string(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId));
1656
1657 std::vector<std::string> keys = {
1658 typeKey,
1659 beginDateKey,
1660 endDateKey
1661 };
1662
1663 int32_t result = rdbDataManager_->DeleteBathchData(keys, userId);
1664 if (result != NativeRdb::E_OK) {
1665 ANS_LOGE("delete DoNotDisturb date failed.");
1666 return false;
1667 }
1668
1669 ANS_LOGD("%{public}s remove DoNotDisturb date, userId : %{public}d", __FUNCTION__, userId);
1670 return true;
1671 }
1672
RemoveAnsBundleDbInfo(std::string bundleName,int32_t uid)1673 bool NotificationPreferencesDatabase::RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid)
1674 {
1675 if (!CheckRdbStore()) {
1676 ANS_LOGE("RdbStore is nullptr.");
1677 return false;
1678 }
1679
1680 std::string key = KEY_BUNDLE_LABEL + bundleName + std::to_string(uid);
1681 int32_t userId = -1;
1682 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(uid, userId);
1683 int32_t result = rdbDataManager_->DeleteData(key, userId);
1684 if (result != NativeRdb::E_OK) {
1685 ANS_LOGE("Delete ans bundle db info failed, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid);
1686 return false;
1687 }
1688
1689 ANS_LOGE("Remove ans bundle db info, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid);
1690 return true;
1691 }
1692
RemoveEnabledDbByBundleName(std::string bundleName,const int32_t & bundleUid)1693 bool NotificationPreferencesDatabase::RemoveEnabledDbByBundleName(std::string bundleName, const int32_t &bundleUid)
1694 {
1695 if (!CheckRdbStore()) {
1696 ANS_LOGE("RdbStore is nullptr.");
1697 return false;
1698 }
1699 int32_t userId = -1;
1700 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId);
1701 std::string key = std::string(KEY_ENABLE_BUNDLE_DISTRIBUTED_NOTIFICATION).append(
1702 KEY_MIDDLE_LINE).append(std::string(bundleName).append(KEY_MIDDLE_LINE));
1703 ANS_LOGD("key is %{public}s", key.c_str());
1704 int32_t result = NativeRdb::E_OK;
1705 std::unordered_map<std::string, std::string> values;
1706 result = rdbDataManager_->QueryDataBeginWithKey(key, values, userId);
1707 if (result == NativeRdb::E_EMPTY_VALUES_BUCKET) {
1708 return true;
1709 } else if (result != NativeRdb::E_OK) {
1710 ANS_LOGE("Get failed, key %{public}s,result %{public}d.", key.c_str(), result);
1711 return NativeRdb::E_ERROR;
1712 }
1713
1714 std::vector<std::string> keys;
1715 for (auto iter : values) {
1716 ANS_LOGD("Get failed, key %{public}s", iter.first.c_str());
1717 keys.push_back(iter.first);
1718 }
1719
1720 result = rdbDataManager_->DeleteBathchData(keys, userId);
1721 if (result != NativeRdb::E_OK) {
1722 ANS_LOGE("delete bundle Info failed.");
1723 return false;
1724 }
1725
1726 return true;
1727 }
1728
SetKvToDb(const std::string & key,const std::string & value,const int32_t & userId)1729 int32_t NotificationPreferencesDatabase::SetKvToDb(
1730 const std::string &key, const std::string &value, const int32_t &userId)
1731 {
1732 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_2);
1733 if (!CheckRdbStore()) {
1734 ANS_LOGE("RdbStore is nullptr.");
1735 message.Message("RdbStore is nullptr.");
1736 NotificationAnalyticsUtil::ReportModifyEvent(message);
1737 return NativeRdb::E_ERROR;
1738 }
1739 int32_t result = rdbDataManager_->InsertData(key, value, userId);
1740 if (result != NativeRdb::E_OK) {
1741 message.Message("Set key failed: " + key);
1742 NotificationAnalyticsUtil::ReportModifyEvent(message);
1743 ANS_LOGE("Set key: %{public}s failed, result %{public}d.", key.c_str(), result);
1744 return NativeRdb::E_ERROR;
1745 }
1746
1747 ANS_LOGD("Key:%{public}s, value:%{public}s.", key.c_str(), value.c_str());
1748
1749 return NativeRdb::E_OK;
1750 }
1751
SetByteToDb(const std::string & key,const std::vector<uint8_t> & value,const int32_t & userId)1752 int32_t NotificationPreferencesDatabase::SetByteToDb(
1753 const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId)
1754 {
1755 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_2);
1756 if (!CheckRdbStore()) {
1757 message.Message("RdbStore is nullptr.");
1758 NotificationAnalyticsUtil::ReportModifyEvent(message);
1759 ANS_LOGE("RdbStore is nullptr.");
1760 return NativeRdb::E_ERROR;
1761 }
1762 int32_t result = rdbDataManager_->InsertData(key, value, userId);
1763 if (result != NativeRdb::E_OK) {
1764 message.Message("Set key failed: " + key);
1765 NotificationAnalyticsUtil::ReportModifyEvent(message);
1766 ANS_LOGE("Set key: %{public}s failed, result %{public}d.", key.c_str(), result);
1767 return NativeRdb::E_ERROR;
1768 }
1769
1770 return NativeRdb::E_OK;
1771 }
1772
GetKvFromDb(const std::string & key,std::string & value,const int32_t & userId)1773 int32_t NotificationPreferencesDatabase::GetKvFromDb(
1774 const std::string &key, std::string &value, const int32_t &userId)
1775 {
1776 if (!CheckRdbStore()) {
1777 ANS_LOGE("RdbStore is nullptr.");
1778 return NativeRdb::E_ERROR;
1779 }
1780
1781 int32_t result = rdbDataManager_->QueryData(key, value, userId);
1782 if (result != NativeRdb::E_OK) {
1783 ANS_LOGE("Get key-value failed, key %{public}s, result %{pubic}d.", key.c_str(), result);
1784 return NativeRdb::E_ERROR;
1785 }
1786
1787 ANS_LOGD("Key:%{public}s, value:%{public}s.", key.c_str(), value.c_str());
1788
1789 return NativeRdb::E_OK;
1790 }
1791
GetByteFromDb(const std::string & key,std::vector<uint8_t> & value,const int32_t & userId)1792 int32_t NotificationPreferencesDatabase::GetByteFromDb(
1793 const std::string &key, std::vector<uint8_t> &value, const int32_t &userId)
1794 {
1795 if (!CheckRdbStore()) {
1796 ANS_LOGE("RdbStore is nullptr.");
1797 return NativeRdb::E_ERROR;
1798 }
1799
1800 int32_t result = rdbDataManager_->QueryData(key, value, userId);
1801 if (result != NativeRdb::E_OK) {
1802 ANS_LOGE("Get byte failed, key %{public}s, result %{pubic}d.", key.c_str(), result);
1803 return NativeRdb::E_ERROR;
1804 }
1805
1806 return NativeRdb::E_OK;
1807 }
1808
GetBatchKvsFromDb(const std::string & key,std::unordered_map<std::string,std::string> & values,const int32_t & userId)1809 int32_t NotificationPreferencesDatabase::GetBatchKvsFromDb(
1810 const std::string &key, std::unordered_map<std::string, std::string> &values, const int32_t &userId)
1811 {
1812 if (!CheckRdbStore()) {
1813 ANS_LOGE("RdbStore is nullptr.");
1814 return NativeRdb::E_ERROR;
1815 }
1816
1817 int32_t result = rdbDataManager_->QueryDataBeginWithKey(key, values, userId);
1818 if (result != NativeRdb::E_OK) {
1819 ANS_LOGE("Get batch notification request failed, key %{public}s, result %{public}d.", key.c_str(), result);
1820 return NativeRdb::E_ERROR;
1821 }
1822 ANS_LOGD("Key:%{public}s.", key.c_str());
1823 return NativeRdb::E_OK;
1824 }
1825
DeleteKvFromDb(const std::string & key,const int32_t & userId)1826 int32_t NotificationPreferencesDatabase::DeleteKvFromDb(const std::string &key, const int32_t &userId)
1827 {
1828 if (!CheckRdbStore()) {
1829 ANS_LOGE("RdbStore is nullptr.");
1830 return NativeRdb::E_ERROR;
1831 }
1832
1833 int32_t result = rdbDataManager_->DeleteData(key, userId);
1834 if (result != NativeRdb::E_OK) {
1835 ANS_LOGE("Delete key-value failed, key %{public}s, result %{public}d.", key.c_str(), result);
1836 return NativeRdb::E_ERROR;
1837 }
1838
1839 ANS_LOGD("Delete key:%{public}s.", key.c_str());
1840
1841 return NativeRdb::E_OK;
1842 }
1843
DeleteBatchKvFromDb(const std::vector<std::string> & keys,const int32_t & userId)1844 int32_t NotificationPreferencesDatabase::DeleteBatchKvFromDb(const std::vector<std::string> &keys,
1845 const int32_t &userId)
1846 {
1847 if (!CheckRdbStore()) {
1848 ANS_LOGE("RdbStore is nullptr.");
1849 return NativeRdb::E_ERROR;
1850 }
1851
1852 int32_t result = rdbDataManager_->DeleteBathchData(keys, userId);
1853 if (result != NativeRdb::E_OK) {
1854 ANS_LOGE("Delete key-value failed, result %{public}d.", result);
1855 return NativeRdb::E_ERROR;
1856 }
1857
1858 return NativeRdb::E_OK;
1859 }
1860
DropUserTable(const int32_t userId)1861 int32_t NotificationPreferencesDatabase::DropUserTable(const int32_t userId)
1862 {
1863 if (!CheckRdbStore()) {
1864 ANS_LOGE("RdbStore is nullptr.");
1865 return NativeRdb::E_ERROR;
1866 }
1867
1868 int32_t result = rdbDataManager_->DropUserTable(userId);
1869 if (result != NativeRdb::E_OK) {
1870 ANS_LOGE("Delete table failed, result %{public}d.", result);
1871 return NativeRdb::E_ERROR;
1872 }
1873 return NativeRdb::E_OK;
1874 }
1875
IsAgentRelationship(const std::string & agentBundleName,const std::string & sourceBundleName)1876 bool NotificationPreferencesDatabase::IsAgentRelationship(const std::string &agentBundleName,
1877 const std::string &sourceBundleName)
1878 {
1879 if (!CheckRdbStore()) {
1880 ANS_LOGE("RdbStore is nullptr.");
1881 return false;
1882 }
1883 std::string agentShip = "";
1884 int32_t result = rdbDataManager_->QueryData("PROXY_PKG", agentShip);
1885 if (result != NativeRdb::E_OK) {
1886 ANS_LOGE("Query agent relationships failed.");
1887 return false;
1888 }
1889 ANS_LOGD("The agent relationship is :%{public}s.", agentShip.c_str());
1890 nlohmann::json jsonAgentShip = nlohmann::json::parse(agentShip, nullptr, false);
1891 if (jsonAgentShip.is_null() || jsonAgentShip.empty()) {
1892 ANS_LOGE("Invalid JSON object");
1893 return false;
1894 }
1895 if (jsonAgentShip.is_discarded() || !jsonAgentShip.is_array()) {
1896 ANS_LOGE("Parse agent ship failed due to data is discarded or not array");
1897 return false;
1898 }
1899
1900 nlohmann::json jsonTarget;
1901 jsonTarget[RELATIONSHIP_JSON_KEY_SERVICE] = agentBundleName;
1902 jsonTarget[RELATIONSHIP_JSON_KEY_APP] = sourceBundleName;
1903 bool isAgentRelationship = false;
1904 for (const auto &item : jsonAgentShip) {
1905 if (jsonTarget == item) {
1906 isAgentRelationship = true;
1907 break;
1908 }
1909 }
1910
1911 return isAgentRelationship;
1912 }
1913
PutDistributedEnabledForBundle(const std::string deviceType,const NotificationPreferencesInfo::BundleInfo & bundleInfo,const bool & enabled)1914 bool NotificationPreferencesDatabase::PutDistributedEnabledForBundle(const std::string deviceType,
1915 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled)
1916 {
1917 ANS_LOGD("%{public}s, deviceType:%{public}s,enabled[%{public}d]", __FUNCTION__, deviceType.c_str(), enabled);
1918 if (bundleInfo.GetBundleName().empty()) {
1919 ANS_LOGE("Bundle name is null.");
1920 return false;
1921 }
1922 int32_t userId = -1;
1923 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
1924
1925 std::string key = GenerateBundleLablel(bundleInfo, deviceType);
1926 int32_t result = PutDataToDB(key, enabled, userId);
1927 ANS_LOGD("result[%{public}d]", result);
1928 return (result == NativeRdb::E_OK);
1929 }
1930
GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo & bundleInfo,const std::string & deviceType) const1931 std::string NotificationPreferencesDatabase::GenerateBundleLablel(
1932 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &deviceType) const
1933 {
1934 return std::string(KEY_ENABLE_BUNDLE_DISTRIBUTED_NOTIFICATION).append(KEY_MIDDLE_LINE).append(
1935 std::string(bundleInfo.GetBundleName()).append(KEY_MIDDLE_LINE).append(std::to_string(
1936 bundleInfo.GetBundleUid())).append(KEY_MIDDLE_LINE).append(deviceType));
1937 }
1938
1939 template <typename T>
PutDataToDB(const std::string & key,const T & value,const int32_t & userId)1940 int32_t NotificationPreferencesDatabase::PutDataToDB(const std::string &key, const T &value, const int32_t &userId)
1941 {
1942 if (!CheckRdbStore()) {
1943 ANS_LOGE("RdbStore is nullptr.");
1944 return false;
1945 }
1946 std::string valueStr = std::to_string(value);
1947 int32_t result = rdbDataManager_->InsertData(key, valueStr, userId);
1948 return result;
1949 }
1950
GetDistributedEnabledForBundle(const std::string deviceType,const NotificationPreferencesInfo::BundleInfo & bundleInfo,bool & enabled)1951 bool NotificationPreferencesDatabase::GetDistributedEnabledForBundle(const std::string deviceType,
1952 const NotificationPreferencesInfo::BundleInfo &bundleInfo, bool &enabled)
1953 {
1954 ANS_LOGD("%{public}s, deviceType:%{public}s,enabled[%{public}d]", __FUNCTION__, deviceType.c_str(), enabled);
1955 if (bundleInfo.GetBundleName().empty()) {
1956 ANS_LOGE("Bundle name is null.");
1957 return false;
1958 }
1959
1960 std::string key = GenerateBundleLablel(bundleInfo, deviceType);
1961 bool result = false;
1962 enabled = false;
1963 int32_t userId = -1;
1964 OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId);
1965 GetValueFromDisturbeDB(key, userId, [&](const int32_t &status, std::string &value) {
1966 switch (status) {
1967 case NativeRdb::E_EMPTY_VALUES_BUCKET: {
1968 result = true;
1969 enabled = false;
1970 break;
1971 }
1972 case NativeRdb::E_OK: {
1973 result = true;
1974 enabled = static_cast<bool>(StringToInt(value));
1975 break;
1976 }
1977 default:
1978 result = false;
1979 break;
1980 }
1981 });
1982 ANS_LOGD("GetDistributedEnabledForBundle:enabled:[%{public}d]KEY:%{public}s", enabled, key.c_str());
1983 return result;
1984 }
1985
GenerateBundleLablel(const std::string & deviceType,const int32_t userId) const1986 std::string NotificationPreferencesDatabase::GenerateBundleLablel(const std::string &deviceType,
1987 const int32_t userId) const
1988 {
1989 return std::string(KEY_SMART_REMINDER_ENABLE_NOTIFICATION).append(KEY_MIDDLE_LINE).append(
1990 deviceType).append(KEY_MIDDLE_LINE).append(std::to_string(userId));
1991 }
1992
1993
SetSmartReminderEnabled(const std::string deviceType,const bool & enabled)1994 bool NotificationPreferencesDatabase::SetSmartReminderEnabled(const std::string deviceType, const bool &enabled)
1995 {
1996 ANS_LOGD("%{public}s, deviceType:%{public}s,enabled[%{public}d]", __FUNCTION__, deviceType.c_str(), enabled);
1997 int32_t userId = SUBSCRIBE_USER_INIT;
1998 OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
1999 if (userId == SUBSCRIBE_USER_INIT) {
2000 ANS_LOGE("Current user acquisition failed");
2001 return false;
2002 }
2003
2004 std::string key = GenerateBundleLablel(deviceType, userId);
2005 ANS_LOGD("%{public}s, key:%{public}s,enabled[%{public}d]", __FUNCTION__, key.c_str(), enabled);
2006 int32_t result = PutDataToDB(key, enabled, userId);
2007 return (result == NativeRdb::E_OK);
2008 }
2009
IsSmartReminderEnabled(const std::string deviceType,bool & enabled)2010 bool NotificationPreferencesDatabase::IsSmartReminderEnabled(const std::string deviceType, bool &enabled)
2011 {
2012 ANS_LOGD("%{public}s, deviceType:%{public}s,enabled[%{public}d]", __FUNCTION__, deviceType.c_str(), enabled);
2013 int32_t userId = SUBSCRIBE_USER_INIT;
2014 OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
2015 if (userId == SUBSCRIBE_USER_INIT) {
2016 ANS_LOGE("Current user acquisition failed");
2017 return false;
2018 }
2019
2020 std::string key = GenerateBundleLablel(deviceType, userId);
2021 bool result = false;
2022 enabled = false;
2023 GetValueFromDisturbeDB(key, userId, [&](const int32_t &status, std::string &value) {
2024 switch (status) {
2025 case NativeRdb::E_EMPTY_VALUES_BUCKET: {
2026 result = true;
2027 enabled = false;
2028 break;
2029 }
2030 case NativeRdb::E_OK: {
2031 result = true;
2032 enabled = static_cast<bool>(StringToInt(value));
2033 break;
2034 }
2035 default:
2036 result = false;
2037 break;
2038 }
2039 });
2040 return result;
2041 }
2042
GetAdditionalConfig(const std::string & key)2043 std::string NotificationPreferencesDatabase::GetAdditionalConfig(const std::string &key)
2044 {
2045 if (!CheckRdbStore()) {
2046 ANS_LOGE("RdbStore is nullptr.");
2047 return "";
2048 }
2049 std::string configValue = "";
2050 int32_t result = rdbDataManager_->QueryData(key, configValue);
2051 if (result != NativeRdb::E_OK) {
2052 ANS_LOGE("Query additional config failed.");
2053 return "";
2054 }
2055 ANS_LOGD("The additional config key is :%{public}s, value is :%{public}s.", key.c_str(), configValue.c_str());
2056 return configValue;
2057 }
2058
CheckApiCompatibility(const std::string & bundleName,const int32_t & uid)2059 bool NotificationPreferencesDatabase::CheckApiCompatibility(const std::string &bundleName, const int32_t &uid)
2060 {
2061 ANS_LOGD("%{public}s", __FUNCTION__);
2062 std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
2063 if (bundleManager == nullptr) {
2064 return false;
2065 }
2066 return bundleManager->CheckApiCompatibility(bundleName, uid);
2067 }
2068
UpdateBundlePropertyToDisturbeDB(int32_t userId,const NotificationPreferencesInfo::BundleInfo & bundleInfo)2069 bool NotificationPreferencesDatabase::UpdateBundlePropertyToDisturbeDB(int32_t userId,
2070 const NotificationPreferencesInfo::BundleInfo &bundleInfo)
2071 {
2072 if (bundleInfo.GetBundleName().empty()) {
2073 ANS_LOGE("Bundle name is null.");
2074 return false;
2075 }
2076
2077 if (!CheckRdbStore()) {
2078 ANS_LOGE("RdbStore is nullptr.");
2079 return false;
2080 }
2081 std::string value;
2082 std::string bundleLabelKey = KEY_BUNDLE_LABEL + GenerateBundleLablel(bundleInfo);
2083 int32_t result = rdbDataManager_->QueryData(bundleLabelKey, value, userId);
2084 if (result == NativeRdb::E_EMPTY_VALUES_BUCKET) {
2085 if (rdbDataManager_->InsertData(bundleLabelKey, GenerateBundleLablel(bundleInfo), userId)
2086 != NativeRdb::E_OK) {
2087 ANS_LOGE("Store bundle name %{public}s to db is failed.", bundleLabelKey.c_str());
2088 return false;
2089 }
2090 }
2091 if (result == NativeRdb::E_EMPTY_VALUES_BUCKET || result == NativeRdb::E_OK) {
2092 return PutBundlePropertyValueToDisturbeDB(bundleInfo);
2093 }
2094 ANS_LOGW("Query bundle name %{public}s failed %{public}d.", bundleLabelKey.c_str(), result);
2095 return false;
2096 }
2097
UpdateBundleSlotToDisturbeDB(int32_t userId,const std::string & bundleName,const int32_t & bundleUid,const std::vector<sptr<NotificationSlot>> & slots)2098 bool NotificationPreferencesDatabase::UpdateBundleSlotToDisturbeDB(int32_t userId, const std::string &bundleName,
2099 const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots)
2100 {
2101 if (bundleName.empty()) {
2102 ANS_LOGE("Bundle name is null.");
2103 return false;
2104 }
2105 if (slots.empty()) {
2106 ANS_LOGI("Slot is empty.");
2107 return true;
2108 }
2109
2110 std::string bundleKey = bundleName + std::to_string(bundleUid);
2111 std::unordered_map<std::string, std::string> values;
2112 for (auto& slot : slots) {
2113 GenerateSlotEntry(bundleKey, slot, values);
2114 }
2115 if (!CheckRdbStore()) {
2116 ANS_LOGE("RdbStore is nullptr.");
2117 return false;
2118 }
2119 int32_t result = rdbDataManager_->InsertBatchData(values, userId);
2120 return (result == NativeRdb::E_OK);
2121 }
2122 } // namespace Notification
2123 } // namespace OHOS
2124