1 /*
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.h"
17
18 #include <fstream>
19 #include <memory>
20 #include <mutex>
21
22 #include "access_token_helper.h"
23 #include "ans_const_define.h"
24 #include "ans_inner_errors.h"
25 #include "ans_log_wrapper.h"
26 #include "ans_permission_def.h"
27 #include "bundle_manager_helper.h"
28 #include "hitrace_meter_adapter.h"
29 #include "nlohmann/json.hpp"
30 #include "os_account_manager_helper.h"
31 #include "notification_analytics_util.h"
32 #include "notification_config_parse.h"
33
34 namespace OHOS {
35 namespace Notification {
36 namespace {
37 const static std::string KEY_BUNDLE_LABEL = "label_ans_bundle_";
38 }
39 std::mutex NotificationPreferences::instanceMutex_;
40 std::shared_ptr<NotificationPreferences> NotificationPreferences::instance_;
41
NotificationPreferences()42 NotificationPreferences::NotificationPreferences()
43 {
44 preferncesDB_ = std::make_unique<NotificationPreferencesDatabase>();
45 if (preferncesDB_ == nullptr) {
46 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_1)
47 .Message("preferncesDB is null.");
48 NotificationAnalyticsUtil::ReportModifyEvent(message);
49 }
50 InitSettingFromDisturbDB();
51 }
52
GetInstance()53 std::shared_ptr<NotificationPreferences> NotificationPreferences::GetInstance()
54 {
55 if (instance_ == nullptr) {
56 std::lock_guard<std::mutex> lock(instanceMutex_);
57 if (instance_ == nullptr) {
58 auto instance = std::make_shared<NotificationPreferences>();
59 instance_ = instance;
60 }
61 }
62 return instance_;
63 }
64
AddNotificationSlots(const sptr<NotificationBundleOption> & bundleOption,const std::vector<sptr<NotificationSlot>> & slots)65 ErrCode NotificationPreferences::AddNotificationSlots(
66 const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
67 {
68 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
69 ANS_LOGD("%{public}s", __FUNCTION__);
70 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_6)
71 .BundleName(bundleOption == nullptr ? "" : bundleOption->GetBundleName());
72 if (bundleOption == nullptr || bundleOption->GetBundleName().empty() || slots.empty()) {
73 message.Message("Invalid param.");
74 NotificationAnalyticsUtil::ReportModifyEvent(message);
75 return ERR_ANS_INVALID_PARAM;
76 }
77 std::lock_guard<std::mutex> lock(preferenceMutex_);
78 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
79 ErrCode result = ERR_OK;
80 for (auto slot : slots) {
81 result = CheckSlotForCreateSlot(bundleOption, slot, preferencesInfo);
82 if (result != ERR_OK) {
83 return result;
84 }
85 }
86
87 ANS_LOGD("ffrt: add slot to db!");
88 if (result == ERR_OK &&
89 (!preferncesDB_->PutSlotsToDisturbeDB(bundleOption->GetBundleName(), bundleOption->GetUid(), slots))) {
90 message.Message("put slot for to db failed.");
91 NotificationAnalyticsUtil::ReportModifyEvent(message);
92 return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
93 }
94
95 if (result == ERR_OK) {
96 preferencesInfo_ = preferencesInfo;
97 }
98 return result;
99 }
100
AddNotificationBundleProperty(const sptr<NotificationBundleOption> & bundleOption)101 ErrCode NotificationPreferences::AddNotificationBundleProperty(const sptr<NotificationBundleOption> &bundleOption)
102 {
103 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
104 return ERR_ANS_INVALID_PARAM;
105 }
106 std::lock_guard<std::mutex> lock(preferenceMutex_);
107 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
108 NotificationPreferencesInfo::BundleInfo bundleInfo;
109 preferencesInfo.SetBundleInfo(bundleInfo);
110 ErrCode result = ERR_OK;
111 if (preferncesDB_->PutBundlePropertyToDisturbeDB(bundleInfo)) {
112 preferencesInfo_ = preferencesInfo;
113 } else {
114 result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
115 }
116 ANS_LOGD("AddNotificationBundleProperty.result: %{public}d", result);
117 return result;
118 }
119
RemoveNotificationSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType)120 ErrCode NotificationPreferences::RemoveNotificationSlot(
121 const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType)
122 {
123 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
124 ANS_LOGD("%{public}s", __FUNCTION__);
125 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
126 return ERR_ANS_INVALID_PARAM;
127 }
128 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_5);
129 message.Message(bundleOption->GetBundleName() + "_" +std::to_string(bundleOption->GetUid()) +
130 " slotType: " + std::to_string(static_cast<uint32_t>(slotType)));
131 message.SlotType(static_cast<uint32_t>(slotType));
132 std::lock_guard<std::mutex> lock(preferenceMutex_);
133 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
134 ErrCode result = ERR_OK;
135 result = CheckSlotForRemoveSlot(bundleOption, slotType, preferencesInfo);
136 if (result == ERR_OK &&
137 (!preferncesDB_->RemoveSlotFromDisturbeDB(GenerateBundleKey(bundleOption), slotType, bundleOption->GetUid()))) {
138 message.ErrorCode(result).Append(" Remove slot failed.");
139 NotificationAnalyticsUtil::ReportModifyEvent(message);
140 ANS_LOGE("%{public}s_%{public}d, remove slot failed.",
141 bundleOption->GetBundleName().c_str(), bundleOption->GetUid());
142 return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
143 }
144
145 if (result == ERR_OK) {
146 preferencesInfo_ = preferencesInfo;
147 }
148 ANS_LOGI("%{public}s_%{public}d, Remove slot successful.",
149 bundleOption->GetBundleName().c_str(), bundleOption->GetUid());
150 return result;
151 }
152
RemoveNotificationAllSlots(const sptr<NotificationBundleOption> & bundleOption)153 ErrCode NotificationPreferences::RemoveNotificationAllSlots(const sptr<NotificationBundleOption> &bundleOption)
154 {
155 ANS_LOGD("%{public}s", __FUNCTION__);
156 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
157 return ERR_ANS_INVALID_PARAM;
158 }
159 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_3);
160 message.Message(bundleOption->GetBundleName() + "_" +std::to_string(bundleOption->GetUid()));
161 std::lock_guard<std::mutex> lock(preferenceMutex_);
162 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
163 ErrCode result = ERR_OK;
164 NotificationPreferencesInfo::BundleInfo bundleInfo;
165 if (GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
166 bundleInfo.RemoveAllSlots();
167 preferencesInfo.SetBundleInfo(bundleInfo);
168 if (!preferncesDB_->RemoveAllSlotsFromDisturbeDB(GenerateBundleKey(bundleOption), bundleOption->GetUid())) {
169 result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
170 message.ErrorCode(result).Append(" Db operation failed.");
171 ANS_LOGE("%{public}s_%{public}d, Db operation failed.",
172 bundleOption->GetBundleName().c_str(), bundleOption->GetUid());
173 }
174 } else {
175 result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
176 message.ErrorCode(result).Append(" Notification bundle not exist.");
177 ANS_LOGE("%{public}s_%{public}d, Notification bundle not exist.",
178 bundleOption->GetBundleName().c_str(), bundleOption->GetUid());
179 }
180
181 if (result == ERR_OK) {
182 preferencesInfo_ = preferencesInfo;
183 message.ErrorCode(result).Append(" Remove all slot successful.");
184 ANS_LOGI("%{public}s_%{public}d, Remove all slot successful.",
185 bundleOption->GetBundleName().c_str(), bundleOption->GetUid());
186 }
187 NotificationAnalyticsUtil::ReportModifyEvent(message);
188 return result;
189 }
190
RemoveNotificationForBundle(const sptr<NotificationBundleOption> & bundleOption)191 ErrCode NotificationPreferences::RemoveNotificationForBundle(const sptr<NotificationBundleOption> &bundleOption)
192 {
193 ANS_LOGD("%{public}s", __FUNCTION__);
194 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
195 return ERR_ANS_INVALID_PARAM;
196 }
197 std::lock_guard<std::mutex> lock(preferenceMutex_);
198 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
199
200 ErrCode result = ERR_OK;
201 NotificationPreferencesInfo::BundleInfo bundleInfo;
202 if (GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
203 preferencesInfo.RemoveBundleInfo(bundleOption);
204 if (!preferncesDB_->RemoveBundleFromDisturbeDB(GenerateBundleKey(bundleOption), bundleOption->GetUid())) {
205 result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
206 }
207 } else {
208 result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
209 }
210
211 if (result == ERR_OK) {
212 preferencesInfo_ = preferencesInfo;
213 }
214
215 return result;
216 }
217
UpdateNotificationSlots(const sptr<NotificationBundleOption> & bundleOption,const std::vector<sptr<NotificationSlot>> & slots)218 ErrCode NotificationPreferences::UpdateNotificationSlots(
219 const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
220 {
221 ANS_LOGD("%{public}s", __FUNCTION__);
222 if (bundleOption == nullptr || bundleOption->GetBundleName().empty() || slots.empty()) {
223 return ERR_ANS_INVALID_PARAM;
224 }
225 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_2)
226 .BundleName(bundleOption->GetBundleName());
227 std::lock_guard<std::mutex> lock(preferenceMutex_);
228 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
229 ErrCode result = ERR_OK;
230 for (auto slotIter : slots) {
231 result = CheckSlotForUpdateSlot(bundleOption, slotIter, preferencesInfo);
232 if (result != ERR_OK) {
233 message.Message("Check slot for update failed." + std::to_string(result));
234 NotificationAnalyticsUtil::ReportModifyEvent(message);
235 return result;
236 }
237 }
238
239 if ((result == ERR_OK) &&
240 (!preferncesDB_->PutSlotsToDisturbeDB(bundleOption->GetBundleName(), bundleOption->GetUid(), slots))) {
241 message.Message("Update put slot for to db failed.");
242 NotificationAnalyticsUtil::ReportModifyEvent(message);
243 return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
244 }
245
246 if (result == ERR_OK) {
247 preferencesInfo_ = preferencesInfo;
248 }
249
250 return result;
251 }
252
GetNotificationSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & type,sptr<NotificationSlot> & slot)253 ErrCode NotificationPreferences::GetNotificationSlot(const sptr<NotificationBundleOption> &bundleOption,
254 const NotificationConstant::SlotType &type, sptr<NotificationSlot> &slot)
255 {
256 ANS_LOGD("%{public}s", __FUNCTION__);
257 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
258 return ERR_ANS_INVALID_PARAM;
259 }
260
261 ErrCode result = ERR_OK;
262 NotificationPreferencesInfo::BundleInfo bundleInfo;
263 std::lock_guard<std::mutex> lock(preferenceMutex_);
264 if (GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
265 if (!bundleInfo.GetSlot(type, slot)) {
266 result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
267 }
268 } else {
269 ANS_LOGW("bundle not exist");
270 result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
271 }
272 ANS_LOGD("%{public}s status = %{public}d ", __FUNCTION__, result);
273 return result;
274 }
275
GetNotificationAllSlots(const sptr<NotificationBundleOption> & bundleOption,std::vector<sptr<NotificationSlot>> & slots)276 ErrCode NotificationPreferences::GetNotificationAllSlots(
277 const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlot>> &slots)
278 {
279 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
280 return ERR_ANS_INVALID_PARAM;
281 }
282
283 ErrCode result = ERR_OK;
284 NotificationPreferencesInfo::BundleInfo bundleInfo;
285 std::lock_guard<std::mutex> lock(preferenceMutex_);
286 if (GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
287 bundleInfo.GetAllSlots(slots);
288 } else {
289 ANS_LOGW("Notification bundle does not exsit.");
290 result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
291 }
292
293 return result;
294 }
295
GetNotificationSlotsNumForBundle(const sptr<NotificationBundleOption> & bundleOption,uint64_t & num)296 ErrCode NotificationPreferences::GetNotificationSlotsNumForBundle(
297 const sptr<NotificationBundleOption> &bundleOption, uint64_t &num)
298 {
299 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
300 return ERR_ANS_INVALID_PARAM;
301 }
302
303 ErrCode result = ERR_OK;
304 NotificationPreferencesInfo::BundleInfo bundleInfo;
305 std::lock_guard<std::mutex> lock(preferenceMutex_);
306 if (GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
307 num = static_cast<uint64_t>(bundleInfo.GetAllSlotsSize());
308 } else {
309 result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
310 }
311 return result;
312 }
313
GetNotificationSlotFlagsForBundle(const sptr<NotificationBundleOption> & bundleOption,uint32_t & slotFlags)314 ErrCode NotificationPreferences::GetNotificationSlotFlagsForBundle(
315 const sptr<NotificationBundleOption> &bundleOption, uint32_t &slotFlags)
316 {
317 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
318 return ERR_ANS_INVALID_PARAM;
319 }
320
321 return GetBundleProperty(bundleOption, BundleType::BUNDLE_SLOTFLGS_TYPE, slotFlags);
322 }
323
IsNotificationSlotFlagsExists(const sptr<NotificationBundleOption> & bundleOption)324 bool NotificationPreferences::IsNotificationSlotFlagsExists(
325 const sptr<NotificationBundleOption> &bundleOption)
326 {
327 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
328 return false;
329 }
330 return preferncesDB_->IsNotificationSlotFlagsExists(bundleOption);
331 }
332
SetNotificationSlotFlagsForBundle(const sptr<NotificationBundleOption> & bundleOption,uint32_t slotFlags)333 ErrCode NotificationPreferences::SetNotificationSlotFlagsForBundle(
334 const sptr<NotificationBundleOption> &bundleOption, uint32_t slotFlags)
335 {
336 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
337 return ERR_ANS_INVALID_PARAM;
338 }
339
340 std::lock_guard<std::mutex> lock(preferenceMutex_);
341 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
342 ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_SLOTFLGS_TYPE, slotFlags);
343 if (result == ERR_OK) {
344 preferencesInfo_ = preferencesInfo;
345 }
346 return result;
347 }
348
IsShowBadge(const sptr<NotificationBundleOption> & bundleOption,bool & enable)349 ErrCode NotificationPreferences::IsShowBadge(const sptr<NotificationBundleOption> &bundleOption, bool &enable)
350 {
351 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
352 return ERR_ANS_INVALID_PARAM;
353 }
354 return GetBundleProperty(bundleOption, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable);
355 }
356
SetShowBadge(const sptr<NotificationBundleOption> & bundleOption,const bool enable)357 ErrCode NotificationPreferences::SetShowBadge(const sptr<NotificationBundleOption> &bundleOption, const bool enable)
358 {
359 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
360 return ERR_ANS_INVALID_PARAM;
361 }
362 std::lock_guard<std::mutex> lock(preferenceMutex_);
363 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
364 ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable);
365 if (result == ERR_OK) {
366 preferencesInfo_ = preferencesInfo;
367 }
368 return result;
369 }
370
GetImportance(const sptr<NotificationBundleOption> & bundleOption,int32_t & importance)371 ErrCode NotificationPreferences::GetImportance(const sptr<NotificationBundleOption> &bundleOption, int32_t &importance)
372 {
373 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
374 return ERR_ANS_INVALID_PARAM;
375 }
376
377 return GetBundleProperty(bundleOption, BundleType::BUNDLE_IMPORTANCE_TYPE, importance);
378 }
379
380
SetImportance(const sptr<NotificationBundleOption> & bundleOption,const int32_t & importance)381 ErrCode NotificationPreferences::SetImportance(
382 const sptr<NotificationBundleOption> &bundleOption, const int32_t &importance)
383 {
384 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
385 return ERR_ANS_INVALID_PARAM;
386 }
387 std::lock_guard<std::mutex> lock(preferenceMutex_);
388 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
389 ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_IMPORTANCE_TYPE, importance);
390 if (result == ERR_OK) {
391 preferencesInfo_ = preferencesInfo;
392 }
393 return result;
394 }
395
GetTotalBadgeNums(const sptr<NotificationBundleOption> & bundleOption,int32_t & totalBadgeNum)396 ErrCode NotificationPreferences::GetTotalBadgeNums(
397 const sptr<NotificationBundleOption> &bundleOption, int32_t &totalBadgeNum)
398 {
399 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
400 return ERR_ANS_INVALID_PARAM;
401 }
402 return GetBundleProperty(bundleOption, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum);
403 }
404
SetTotalBadgeNums(const sptr<NotificationBundleOption> & bundleOption,const int32_t num)405 ErrCode NotificationPreferences::SetTotalBadgeNums(
406 const sptr<NotificationBundleOption> &bundleOption, const int32_t num)
407 {
408 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
409 return ERR_ANS_INVALID_PARAM;
410 }
411 std::lock_guard<std::mutex> lock(preferenceMutex_);
412 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
413 ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, num);
414 if (result == ERR_OK) {
415 preferencesInfo_ = preferencesInfo;
416 }
417 return result;
418 }
419
GetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> & bundleOption,bool & enabled)420 ErrCode NotificationPreferences::GetNotificationsEnabledForBundle(
421 const sptr<NotificationBundleOption> &bundleOption, bool &enabled)
422 {
423 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
424 return ERR_ANS_INVALID_PARAM;
425 }
426 return GetBundleProperty(bundleOption, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled);
427 }
428
SetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> & bundleOption,const bool enabled)429 ErrCode NotificationPreferences::SetNotificationsEnabledForBundle(
430 const sptr<NotificationBundleOption> &bundleOption, const bool enabled)
431 {
432 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
433 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
434 return ERR_ANS_INVALID_PARAM;
435 }
436
437 std::lock_guard<std::mutex> lock(preferenceMutex_);
438 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
439 ErrCode result =
440 SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled);
441 if (result == ERR_OK) {
442 preferencesInfo_ = preferencesInfo;
443 }
444 return result;
445 }
446
GetNotificationsEnabled(const int32_t & userId,bool & enabled)447 ErrCode NotificationPreferences::GetNotificationsEnabled(const int32_t &userId, bool &enabled)
448 {
449 if (userId <= SUBSCRIBE_USER_INIT) {
450 return ERR_ANS_INVALID_PARAM;
451 }
452
453 ErrCode result = ERR_OK;
454 std::lock_guard<std::mutex> lock(preferenceMutex_);
455 if (!preferencesInfo_.GetEnabledAllNotification(userId, enabled)) {
456 result = ERR_ANS_INVALID_PARAM;
457 }
458 return result;
459 }
460
SetNotificationsEnabled(const int32_t & userId,const bool & enabled)461 ErrCode NotificationPreferences::SetNotificationsEnabled(const int32_t &userId, const bool &enabled)
462 {
463 if (userId <= SUBSCRIBE_USER_INIT) {
464 return ERR_ANS_INVALID_PARAM;
465 }
466 std::lock_guard<std::mutex> lock(preferenceMutex_);
467 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
468 preferencesInfo.SetEnabledAllNotification(userId, enabled);
469 ErrCode result = ERR_OK;
470 if (!preferncesDB_->PutNotificationsEnabled(userId, enabled)) {
471 result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
472 }
473
474 if (result == ERR_OK) {
475 preferencesInfo_ = preferencesInfo;
476 }
477 return result;
478 }
479
GetHasPoppedDialog(const sptr<NotificationBundleOption> & bundleOption,bool & hasPopped)480 ErrCode NotificationPreferences::GetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool &hasPopped)
481 {
482 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
483 return ERR_ANS_INVALID_PARAM;
484 }
485 return GetBundleProperty(bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
486 }
487
SetHasPoppedDialog(const sptr<NotificationBundleOption> & bundleOption,bool hasPopped)488 ErrCode NotificationPreferences::SetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool hasPopped)
489 {
490 if (bundleOption == nullptr) {
491 return ERR_ANS_INVALID_PARAM;
492 }
493 std::lock_guard<std::mutex> lock(preferenceMutex_);
494 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
495 ErrCode result = ERR_OK;
496 result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
497 if (result == ERR_OK) {
498 preferencesInfo_ = preferencesInfo;
499 }
500 return result;
501 }
502
GetDoNotDisturbDate(const int32_t & userId,sptr<NotificationDoNotDisturbDate> & date)503 ErrCode NotificationPreferences::GetDoNotDisturbDate(const int32_t &userId,
504 sptr<NotificationDoNotDisturbDate> &date)
505 {
506 if (userId <= SUBSCRIBE_USER_INIT) {
507 return ERR_ANS_INVALID_PARAM;
508 }
509
510 ErrCode result = ERR_OK;
511 std::lock_guard<std::mutex> lock(preferenceMutex_);
512 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
513 if (!preferencesInfo.GetDoNotDisturbDate(userId, date)) {
514 result = ERR_ANS_INVALID_PARAM;
515 }
516 return result;
517 }
518
SetDoNotDisturbDate(const int32_t & userId,const sptr<NotificationDoNotDisturbDate> date)519 ErrCode NotificationPreferences::SetDoNotDisturbDate(const int32_t &userId,
520 const sptr<NotificationDoNotDisturbDate> date)
521 {
522 ANS_LOGE("enter.");
523 if (userId <= SUBSCRIBE_USER_INIT) {
524 return ERR_ANS_INVALID_PARAM;
525 }
526 std::lock_guard<std::mutex> lock(preferenceMutex_);
527 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
528 preferencesInfo.SetDoNotDisturbDate(userId, date);
529
530 ErrCode result = ERR_OK;
531 if (!preferncesDB_->PutDoNotDisturbDate(userId, date)) {
532 result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
533 }
534
535 if (result == ERR_OK) {
536 preferencesInfo_ = preferencesInfo;
537 }
538 return result;
539 }
540
CheckDoNotDisturbProfileID(int32_t profileId)541 bool NotificationPreferences::CheckDoNotDisturbProfileID(int32_t profileId)
542 {
543 if (profileId < DO_NOT_DISTURB_PROFILE_MIN_ID || profileId > DO_NOT_DISTURB_PROFILE_MAX_ID) {
544 ANS_LOGE("The profile id is out of range.");
545 return false;
546 }
547 return true;
548 }
549
AddDoNotDisturbProfiles(int32_t userId,std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)550 ErrCode NotificationPreferences::AddDoNotDisturbProfiles(
551 int32_t userId, std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)
552 {
553 ANS_LOGD("Called.");
554 for (auto profile : profiles) {
555 if (profile == nullptr) {
556 ANS_LOGE("The profile is nullptr.");
557 return ERR_ANS_INVALID_PARAM;
558 }
559 if (!CheckDoNotDisturbProfileID(profile->GetProfileId())) {
560 return ERR_ANS_INVALID_PARAM;
561 }
562 auto trustList = profile->GetProfileTrustList();
563 for (auto& bundleInfo : trustList) {
564 int32_t index = BundleManagerHelper::GetInstance()->GetAppIndexByUid(bundleInfo.GetUid());
565 bundleInfo.SetAppIndex(index);
566 ANS_LOGI("Get app index by uid %{public}d %{public}s %{public}d", bundleInfo.GetUid(),
567 bundleInfo.GetBundleName().c_str(), index);
568 }
569 profile->SetProfileTrustList(trustList);
570 }
571 std::lock_guard<std::mutex> lock(preferenceMutex_);
572 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
573 preferencesInfo.AddDoNotDisturbProfiles(userId, profiles);
574 if (preferncesDB_ == nullptr) {
575 ANS_LOGE("The prefernces db is nullptr.");
576 return ERR_ANS_SERVICE_NOT_READY;
577 }
578 if (!preferncesDB_->AddDoNotDisturbProfiles(userId, profiles)) {
579 return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
580 }
581 preferencesInfo_ = preferencesInfo;
582 return ERR_OK;
583 }
584
GetBundleInfo(NotificationPreferencesInfo & preferencesInfo,const sptr<NotificationBundleOption> & bundleOption,NotificationPreferencesInfo::BundleInfo & info) const585 bool NotificationPreferences::GetBundleInfo(NotificationPreferencesInfo &preferencesInfo,
586 const sptr<NotificationBundleOption> &bundleOption, NotificationPreferencesInfo::BundleInfo &info) const
587 {
588 if (preferencesInfo.GetBundleInfo(bundleOption, info)) {
589 return true;
590 } else if (preferncesDB_->GetBundleInfo(bundleOption, info)) {
591 preferencesInfo.SetBundleInfo(info);
592 return true;
593 }
594 return false;
595 }
596
RemoveDoNotDisturbProfiles(int32_t userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)597 ErrCode NotificationPreferences::RemoveDoNotDisturbProfiles(
598 int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)
599 {
600 ANS_LOGD("Called.");
601 for (auto profile : profiles) {
602 if (profile == nullptr) {
603 ANS_LOGE("The profile is nullptr.");
604 return ERR_ANS_INVALID_PARAM;
605 }
606 if (!CheckDoNotDisturbProfileID(profile->GetProfileId())) {
607 return ERR_ANS_INVALID_PARAM;
608 }
609 }
610 std::lock_guard<std::mutex> lock(preferenceMutex_);
611 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
612 preferencesInfo.RemoveDoNotDisturbProfiles(userId, profiles);
613 if (preferncesDB_ == nullptr) {
614 ANS_LOGE("The prefernces db is nullptr.");
615 return ERR_ANS_SERVICE_NOT_READY;
616 }
617 if (!preferncesDB_->RemoveDoNotDisturbProfiles(userId, profiles)) {
618 return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
619 }
620 preferencesInfo_ = preferencesInfo;
621 return ERR_OK;
622 }
623
UpdateProfilesUtil(std::vector<NotificationBundleOption> & trustList,const std::vector<NotificationBundleOption> bundleList)624 void NotificationPreferences::UpdateProfilesUtil(std::vector<NotificationBundleOption>& trustList,
625 const std::vector<NotificationBundleOption> bundleList)
626 {
627 for (auto& item : bundleList) {
628 bool exit = false;
629 for (auto& bundle: trustList) {
630 if (item.GetUid() == bundle.GetUid()) {
631 exit = true;
632 break;
633 }
634 }
635 if (!exit) {
636 trustList.push_back(item);
637 }
638 }
639 }
640
UpdateDoNotDisturbProfiles(int32_t userId,int32_t profileId,const std::string & name,const std::vector<NotificationBundleOption> & bundleList)641 ErrCode NotificationPreferences::UpdateDoNotDisturbProfiles(int32_t userId, int32_t profileId,
642 const std::string& name, const std::vector<NotificationBundleOption>& bundleList)
643 {
644 ANS_LOGI("Called update Profile %{public}d %{public}d %{public}zu.", userId, profileId, bundleList.size());
645 if (!CheckDoNotDisturbProfileID(profileId) || bundleList.empty()) {
646 return ERR_ANS_INVALID_PARAM;
647 }
648
649 sptr<NotificationDoNotDisturbProfile> profile = new (std::nothrow) NotificationDoNotDisturbProfile();
650 std::lock_guard<std::mutex> lock(preferenceMutex_);
651 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
652 if (preferencesInfo.GetDoNotDisturbProfiles(profileId, userId, profile)) {
653 auto trustList = profile->GetProfileTrustList();
654 UpdateProfilesUtil(trustList, bundleList);
655 profile->SetProfileTrustList(trustList);
656 } else {
657 profile->SetProfileId(profileId);
658 profile->SetProfileName(name);
659 profile->SetProfileTrustList(bundleList);
660 }
661 ANS_LOGI("Update profile %{public}d %{public}d %{public}zu", userId, profile->GetProfileId(),
662 profile->GetProfileTrustList().size());
663 preferencesInfo.AddDoNotDisturbProfiles(userId, {profile});
664 if (preferncesDB_ == nullptr) {
665 ANS_LOGE("The prefernces db is nullptr.");
666 return ERR_ANS_SERVICE_NOT_READY;
667 }
668 if (!preferncesDB_->AddDoNotDisturbProfiles(userId, {profile})) {
669 return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
670 }
671 preferencesInfo_ = preferencesInfo;
672 return ERR_OK;
673 }
674
UpdateCloneBundleInfo(int32_t userId,const NotificationCloneBundleInfo & cloneBundleInfo)675 void NotificationPreferences::UpdateCloneBundleInfo(int32_t userId,
676 const NotificationCloneBundleInfo& cloneBundleInfo)
677 {
678 ANS_LOGI("Event bundle update %{public}s.", cloneBundleInfo.Dump().c_str());
679 NotificationPreferencesInfo::BundleInfo bundleInfo;
680 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption();
681 bundleOption->SetBundleName(cloneBundleInfo.GetBundleName());
682 bundleOption->SetUid(cloneBundleInfo.GetUid());
683 std::lock_guard<std::mutex> lock(preferenceMutex_);
684 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
685 if (!GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
686 bundleInfo.SetBundleName(cloneBundleInfo.GetBundleName());
687 bundleInfo.SetBundleUid(cloneBundleInfo.GetUid());
688 }
689
690 /* after clone, override these witch */
691 bundleInfo.SetIsShowBadge(cloneBundleInfo.GetIsShowBadge());
692 bundleInfo.SetEnableNotification(cloneBundleInfo.GetEnableNotification());
693 /* update property to db */
694 if (!preferncesDB_->UpdateBundlePropertyToDisturbeDB(userId, bundleInfo)) {
695 ANS_LOGW("Clone bundle info failed %{public}s.", cloneBundleInfo.Dump().c_str());
696 return;
697 }
698
699 if (SaveBundleProperty(bundleInfo, bundleOption,
700 BundleType::BUNDLE_SLOTFLGS_TYPE, cloneBundleInfo.GetSlotFlags()) != ERR_OK) {
701 ANS_LOGW("Clone bundle slot info %{public}s.", cloneBundleInfo.Dump().c_str());
702 return;
703 }
704 preferencesInfo.SetBundleInfo(bundleInfo);
705
706 /* update slot info */
707 std::vector<sptr<NotificationSlot>> slots;
708 for (auto& cloneSlot : cloneBundleInfo.GetSlotInfo()) {
709 sptr<NotificationSlot> slotInfo = new (std::nothrow) NotificationSlot(cloneSlot.slotType_);
710 uint32_t slotFlags = bundleInfo.GetSlotFlags();
711 auto configSlotReminderMode = DelayedSingleton<NotificationConfigParse>::GetInstance()->
712 GetConfigSlotReminderModeByType(slotInfo->GetType(), bundleOption);
713 slotInfo->SetReminderMode(configSlotReminderMode & slotFlags);
714 slotInfo->SetEnable(cloneSlot.enable_);
715 slotInfo->SetForceControl(cloneSlot.isForceControl_);
716 slotInfo->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED);
717 slots.push_back(slotInfo);
718 bundleInfo.SetSlot(slotInfo);
719 }
720
721 if (!preferncesDB_->UpdateBundleSlotToDisturbeDB(userId, cloneBundleInfo.GetBundleName(),
722 cloneBundleInfo.GetUid(), slots)) {
723 ANS_LOGW("Clone bundle slot failed %{public}s.", cloneBundleInfo.Dump().c_str());
724 preferencesInfo_ = preferencesInfo;
725 return;
726 }
727 preferencesInfo.SetBundleInfo(bundleInfo);
728 preferencesInfo_ = preferencesInfo;
729 }
730
GetAllCLoneBundlesInfo(int32_t userId,std::vector<NotificationCloneBundleInfo> & cloneBundles)731 void NotificationPreferences::GetAllCLoneBundlesInfo(int32_t userId,
732 std::vector<NotificationCloneBundleInfo> &cloneBundles)
733 {
734 std::lock_guard<std::mutex> lock(preferenceMutex_);
735 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
736 std::unordered_map<std::string, std::string> bundlesMap;
737 if (GetBatchKvsFromDb(KEY_BUNDLE_LABEL, bundlesMap, userId) != ERR_OK) {
738 ANS_LOGE("Get bundle map info failed.");
739 return;
740 }
741 preferencesInfo.GetAllCLoneBundlesInfo(userId, bundlesMap, cloneBundles);
742 preferencesInfo_ = preferencesInfo;
743 }
744
GetDoNotDisturbProfileListByUserId(int32_t userId,std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)745 void NotificationPreferences::GetDoNotDisturbProfileListByUserId(int32_t userId,
746 std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
747 {
748 std::lock_guard<std::mutex> lock(preferenceMutex_);
749 preferencesInfo_.GetAllDoNotDisturbProfiles(userId, profiles);
750 }
751
GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> & bundleOption)752 ErrCode NotificationPreferences::GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> &bundleOption)
753 {
754 ANS_LOGD("Called.");
755 std::lock_guard<std::mutex> lock(preferenceMutex_);
756 if (preferncesDB_ == nullptr) {
757 return ERR_ANS_SERVICE_NOT_READY;
758 }
759 if (!preferncesDB_->GetAllNotificationEnabledBundles(bundleOption)) {
760 return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
761 }
762 return ERR_OK;
763 }
764
ClearNotificationInRestoreFactorySettings()765 ErrCode NotificationPreferences::ClearNotificationInRestoreFactorySettings()
766 {
767 ErrCode result = ERR_OK;
768 std::lock_guard<std::mutex> lock(preferenceMutex_);
769 if (!preferncesDB_->RemoveAllDataFromDisturbeDB()) {
770 result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
771 }
772
773 if (result == ERR_OK) {
774 preferencesInfo_ = NotificationPreferencesInfo();
775 }
776 return result;
777 }
778
GetDoNotDisturbProfile(int32_t profileId,int32_t userId,sptr<NotificationDoNotDisturbProfile> & profile)779 ErrCode NotificationPreferences::GetDoNotDisturbProfile(
780 int32_t profileId, int32_t userId, sptr<NotificationDoNotDisturbProfile> &profile)
781 {
782 if (!CheckDoNotDisturbProfileID(profileId)) {
783 return ERR_ANS_INVALID_PARAM;
784 }
785 std::lock_guard<std::mutex> lock(preferenceMutex_);
786 if (!preferencesInfo_.GetDoNotDisturbProfiles(profileId, userId, profile)) {
787 return ERR_ANS_NO_PROFILE_TEMPLATE;
788 }
789 return ERR_OK;
790 }
791
RemoveDoNotDisturbProfileTrustList(int32_t userId,const sptr<NotificationBundleOption> & bundleOption)792 void NotificationPreferences::RemoveDoNotDisturbProfileTrustList(
793 int32_t userId, const sptr<NotificationBundleOption> &bundleOption)
794 {
795 if (bundleOption == nullptr) {
796 ANS_LOGE("The bundle option is nullptr.");
797 return;
798 }
799 int32_t uid = bundleOption->GetUid();
800 int32_t appIndex = bundleOption->GetAppIndex();
801 auto bundleName = bundleOption->GetBundleName();
802 ANS_LOGI("Remove %{public}s %{public}d %{public}d.", bundleName.c_str(), uid, appIndex);
803 std::lock_guard<std::mutex> lock(preferenceMutex_);
804 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
805
806 std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
807 preferencesInfo.GetAllDoNotDisturbProfiles(userId, profiles);
808 for (auto profile : profiles) {
809 if (profile == nullptr) {
810 ANS_LOGE("The profile is nullptr.");
811 continue;
812 }
813 auto trustList = profile->GetProfileTrustList();
814 for (auto it = trustList.begin(); it != trustList.end(); it++) {
815 if (it->GetUid() == uid) {
816 trustList.erase(it);
817 break;
818 }
819 }
820 profile->SetProfileTrustList(trustList);
821 }
822 preferencesInfo.AddDoNotDisturbProfiles(userId, profiles);
823 if (preferncesDB_ == nullptr) {
824 ANS_LOGE("The prefernces db is nullptr.");
825 return;
826 }
827 if (!preferncesDB_->AddDoNotDisturbProfiles(userId, profiles)) {
828 return;
829 }
830 preferencesInfo_ = preferencesInfo;
831 }
832
CheckSlotForCreateSlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot,NotificationPreferencesInfo & preferencesInfo) const833 ErrCode NotificationPreferences::CheckSlotForCreateSlot(const sptr<NotificationBundleOption> &bundleOption,
834 const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
835 {
836 if (slot == nullptr) {
837 ANS_LOGE("Notification slot is nullptr.");
838 return ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_NOT_EXIST;
839 }
840
841 NotificationPreferencesInfo::BundleInfo bundleInfo;
842 if (!GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
843 bundleInfo.SetBundleName(bundleOption->GetBundleName());
844 bundleInfo.SetBundleUid(bundleOption->GetUid());
845 bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
846 }
847 bundleInfo.SetSlot(slot);
848 preferencesInfo.SetBundleInfo(bundleInfo);
849
850 return ERR_OK;
851 }
852
CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,NotificationPreferencesInfo & preferencesInfo) const853 ErrCode NotificationPreferences::CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> &bundleOption,
854 const NotificationConstant::SlotType &slotType, NotificationPreferencesInfo &preferencesInfo) const
855 {
856 ErrCode result = ERR_OK;
857 NotificationPreferencesInfo::BundleInfo bundleInfo;
858 if (GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
859 if (bundleInfo.IsExsitSlot(slotType)) {
860 bundleInfo.RemoveSlot(slotType);
861 preferencesInfo.SetBundleInfo(bundleInfo);
862 } else {
863 ANS_LOGE("Notification slot type does not exsited.");
864 result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
865 }
866 } else {
867 ANS_LOGW("Notification bundle does not exsit.");
868 result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
869 }
870 return result;
871 }
872
CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot,NotificationPreferencesInfo & preferencesInfo) const873 ErrCode NotificationPreferences::CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> &bundleOption,
874 const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
875 {
876 if (slot == nullptr) {
877 ANS_LOGE("Notification slot is nullptr.");
878 return ERR_ANS_INVALID_PARAM;
879 }
880
881 ErrCode result = ERR_OK;
882 NotificationPreferencesInfo::BundleInfo bundleInfo;
883 if (GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
884 if (bundleInfo.IsExsitSlot(slot->GetType())) {
885 bundleInfo.SetBundleName(bundleOption->GetBundleName());
886 bundleInfo.SetBundleUid(bundleOption->GetUid());
887 bundleInfo.SetSlot(slot);
888 preferencesInfo.SetBundleInfo(bundleInfo);
889 } else {
890 ANS_LOGE("Notification slot type does not exist.");
891 result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
892 }
893 } else {
894 ANS_LOGW("Notification bundle does not exsit.");
895 result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
896 }
897
898 return result;
899 }
900
901 template <typename T>
SetBundleProperty(NotificationPreferencesInfo & preferencesInfo,const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,const T & value)902 ErrCode NotificationPreferences::SetBundleProperty(NotificationPreferencesInfo &preferencesInfo,
903 const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
904 {
905 ErrCode result = ERR_OK;
906 NotificationPreferencesInfo::BundleInfo bundleInfo;
907 if (!GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
908 bundleInfo.SetBundleName(bundleOption->GetBundleName());
909 bundleInfo.SetBundleUid(bundleOption->GetUid());
910 bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
911 }
912 result = SaveBundleProperty(bundleInfo, bundleOption, type, value);
913 if (result == ERR_OK) {
914 preferencesInfo.SetBundleInfo(bundleInfo);
915 }
916
917 return result;
918 }
919
920 template <typename T>
SaveBundleProperty(NotificationPreferencesInfo::BundleInfo & bundleInfo,const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,const T & value)921 ErrCode NotificationPreferences::SaveBundleProperty(NotificationPreferencesInfo::BundleInfo &bundleInfo,
922 const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
923 {
924 bool storeDBResult = true;
925 switch (type) {
926 case BundleType::BUNDLE_IMPORTANCE_TYPE:
927 bundleInfo.SetImportance(value);
928 storeDBResult = preferncesDB_->PutImportance(bundleInfo, value);
929 break;
930 case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
931 bundleInfo.SetBadgeTotalNum(value);
932 storeDBResult = preferncesDB_->PutTotalBadgeNums(bundleInfo, value);
933 break;
934 case BundleType::BUNDLE_SHOW_BADGE_TYPE:
935 bundleInfo.SetIsShowBadge(value);
936 storeDBResult = preferncesDB_->PutShowBadge(bundleInfo, value);
937 break;
938 case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
939 bundleInfo.SetEnableNotification(value);
940 storeDBResult = preferncesDB_->PutNotificationsEnabledForBundle(bundleInfo, value);
941 break;
942 case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
943 ANS_LOGI("Into BUNDLE_POPPED_DIALOG_TYPE:SetHasPoppedDialog.");
944 bundleInfo.SetHasPoppedDialog(value);
945 storeDBResult = preferncesDB_->PutHasPoppedDialog(bundleInfo, value);
946 break;
947 case BundleType::BUNDLE_SLOTFLGS_TYPE:
948 ANS_LOGI("Into BUNDLE_SLOTFLGS_TYPE:SetSlotFlags.");
949 bundleInfo.SetSlotFlags(value);
950 storeDBResult = preferncesDB_->PutSlotFlags(bundleInfo, value);
951 break;
952 default:
953 break;
954 }
955 return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
956 }
957
958 template <typename T>
GetBundleProperty(const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,T & value)959 ErrCode NotificationPreferences::GetBundleProperty(
960 const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, T &value)
961 {
962 ErrCode result = ERR_OK;
963 NotificationPreferencesInfo::BundleInfo bundleInfo;
964 std::lock_guard<std::mutex> lock(preferenceMutex_);
965 if (GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
966 switch (type) {
967 case BundleType::BUNDLE_IMPORTANCE_TYPE:
968 value = bundleInfo.GetImportance();
969 break;
970 case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
971 value = bundleInfo.GetBadgeTotalNum();
972 break;
973 case BundleType::BUNDLE_SHOW_BADGE_TYPE:
974 value = bundleInfo.GetIsShowBadge();
975 break;
976 case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
977 value = bundleInfo.GetEnableNotification();
978 break;
979 case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
980 ANS_LOGD("Into BUNDLE_POPPED_DIALOG_TYPE:GetHasPoppedDialog.");
981 value = bundleInfo.GetHasPoppedDialog();
982 break;
983 case BundleType::BUNDLE_SLOTFLGS_TYPE:
984 value = bundleInfo.GetSlotFlags();
985 ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:GetSlotFlags.");
986 break;
987 default:
988 result = ERR_ANS_INVALID_PARAM;
989 break;
990 }
991 } else {
992 ANS_LOGW("Notification bundle does not exsit.");
993 result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
994 }
995 return result;
996 }
997
GenerateBundleKey(const sptr<NotificationBundleOption> & bundleOption) const998 std::string NotificationPreferences::GenerateBundleKey(const sptr<NotificationBundleOption> &bundleOption) const
999 {
1000 return bundleOption->GetBundleName().append(std::to_string(bundleOption->GetUid()));
1001 }
1002
GetTemplateSupported(const std::string & templateName,bool & support)1003 ErrCode NotificationPreferences::GetTemplateSupported(const std::string& templateName, bool &support)
1004 {
1005 if (templateName.length() == 0) {
1006 ANS_LOGE("template name is null.");
1007 return ERR_ANS_INVALID_PARAM;
1008 }
1009
1010 std::ifstream inFile;
1011 inFile.open(DEFAULT_TEMPLATE_PATH.c_str(), std::ios::in);
1012 if (!inFile.is_open()) {
1013 ANS_LOGE("read template config error.");
1014 return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
1015 }
1016
1017 nlohmann::json jsonObj;
1018 inFile >> jsonObj;
1019 if (jsonObj.is_null() || !jsonObj.is_object()) {
1020 ANS_LOGE("Invalid JSON object");
1021 return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
1022 }
1023 if (jsonObj.is_discarded()) {
1024 ANS_LOGE("template json discarded error.");
1025 inFile.close();
1026 return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
1027 }
1028
1029 if (jsonObj.contains(templateName)) {
1030 support = true;
1031 }
1032
1033 jsonObj.clear();
1034 inFile.close();
1035 return ERR_OK;
1036 }
1037
SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,const bool enabled)1038 ErrCode NotificationPreferences::SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
1039 const std::string &deviceType, const bool enabled)
1040 {
1041 ANS_LOGD("%{public}s", __FUNCTION__);
1042 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
1043 return ERR_ANS_INVALID_PARAM;
1044 }
1045
1046 std::lock_guard<std::mutex> lock(preferenceMutex_);
1047 NotificationPreferencesInfo::BundleInfo bundleInfo;
1048 bundleInfo.SetBundleName(bundleOption->GetBundleName());
1049 bundleInfo.SetBundleUid(bundleOption->GetUid());
1050 bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
1051 bool storeDBResult = true;
1052 storeDBResult = preferncesDB_->PutDistributedEnabledForBundle(deviceType, bundleInfo, enabled);
1053 return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1054 }
1055
IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,bool & enabled)1056 ErrCode NotificationPreferences::IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
1057 const std::string &deviceType, bool &enabled)
1058 {
1059 ANS_LOGD("%{public}s", __FUNCTION__);
1060 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
1061 return ERR_ANS_INVALID_PARAM;
1062 }
1063
1064 std::lock_guard<std::mutex> lock(preferenceMutex_);
1065 NotificationPreferencesInfo::BundleInfo bundleInfo;
1066 bundleInfo.SetBundleName(bundleOption->GetBundleName());
1067 bundleInfo.SetBundleUid(bundleOption->GetUid());
1068 bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
1069 bool storeDBResult = true;
1070 storeDBResult = preferncesDB_->GetDistributedEnabledForBundle(deviceType, bundleInfo, enabled);
1071 return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1072 }
1073
SetSmartReminderEnabled(const std::string & deviceType,const bool enabled)1074 ErrCode NotificationPreferences::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled)
1075 {
1076 ANS_LOGD("%{public}s", __FUNCTION__);
1077 if (deviceType.empty()) {
1078 return ERR_ANS_INVALID_PARAM;
1079 }
1080
1081 std::lock_guard<std::mutex> lock(preferenceMutex_);
1082 bool storeDBResult = true;
1083 storeDBResult = preferncesDB_->SetSmartReminderEnabled(deviceType, enabled);
1084 return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1085 }
1086
IsSmartReminderEnabled(const std::string & deviceType,bool & enabled)1087 ErrCode NotificationPreferences::IsSmartReminderEnabled(const std::string &deviceType, bool &enabled)
1088 {
1089 ANS_LOGD("%{public}s", __FUNCTION__);
1090 if (deviceType.empty()) {
1091 return ERR_ANS_INVALID_PARAM;
1092 }
1093
1094 std::lock_guard<std::mutex> lock(preferenceMutex_);
1095 bool storeDBResult = true;
1096 storeDBResult = preferncesDB_->IsSmartReminderEnabled(deviceType, enabled);
1097 return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1098 }
1099
InitSettingFromDisturbDB(int32_t userId)1100 void NotificationPreferences::InitSettingFromDisturbDB(int32_t userId)
1101 {
1102 ANS_LOGI("%{public}s userId is %{public}d", __FUNCTION__, userId);
1103 std::lock_guard<std::mutex> lock(preferenceMutex_);
1104 if (preferncesDB_ != nullptr) {
1105 preferncesDB_->ParseFromDisturbeDB(preferencesInfo_, userId);
1106 }
1107 }
1108
RemoveSettings(int32_t userId)1109 void NotificationPreferences::RemoveSettings(int32_t userId)
1110 {
1111 ANS_LOGD("%{public}s", __FUNCTION__);
1112 std::lock_guard<std::mutex> lock(preferenceMutex_);
1113 preferencesInfo_.RemoveNotificationEnable(userId);
1114 preferencesInfo_.RemoveDoNotDisturbDate(userId);
1115 if (preferncesDB_ != nullptr) {
1116 preferncesDB_->RemoveNotificationEnable(userId);
1117 preferncesDB_->RemoveDoNotDisturbDate(userId);
1118 preferncesDB_->DropUserTable(userId);
1119 }
1120 }
1121
CheckApiCompatibility(const sptr<NotificationBundleOption> & bundleOption) const1122 bool NotificationPreferences::CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption) const
1123 {
1124 ANS_LOGD("%{public}s", __FUNCTION__);
1125 std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
1126 if (bundleManager == nullptr) {
1127 return false;
1128 }
1129 return bundleManager->CheckApiCompatibility(bundleOption);
1130 }
1131
RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> & bundleOption)1132 void NotificationPreferences::RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> &bundleOption)
1133 {
1134 ANS_LOGE("%{public}s", __FUNCTION__);
1135 if (preferncesDB_ != nullptr && bundleOption != nullptr) {
1136 preferncesDB_->RemoveAnsBundleDbInfo(bundleOption->GetBundleName(), bundleOption->GetUid());
1137 }
1138 }
1139
RemoveEnabledDbByBundle(const sptr<NotificationBundleOption> & bundleOption)1140 void NotificationPreferences::RemoveEnabledDbByBundle(const sptr<NotificationBundleOption> &bundleOption)
1141 {
1142 ANS_LOGE("%{public}s", __FUNCTION__);
1143 if (preferncesDB_ != nullptr && bundleOption != nullptr) {
1144 std::lock_guard<std::mutex> lock(preferenceMutex_);
1145 preferncesDB_->RemoveEnabledDbByBundleName(bundleOption->GetBundleName(), bundleOption->GetUid());
1146 }
1147 }
1148
GetBundleSoundPermission(bool & allPackage,std::set<std::string> & bundleNames)1149 bool NotificationPreferences::GetBundleSoundPermission(bool &allPackage, std::set<std::string> &bundleNames)
1150 {
1151 ANS_LOGD("%{public}s", __FUNCTION__);
1152 std::string value = "";
1153 int32_t userId = -1;
1154 OsAccountManagerHelper::GetInstance().GetCurrentCallingUserId(userId);
1155 if (GetKvFromDb("RING_TRUSTLIST_PKG", value, userId) != ERR_OK) {
1156 ANS_LOGD("Get bundle sound permission failed.");
1157 return false;
1158 }
1159
1160 ANS_LOGD("The bundle permission is :%{public}s.", value.c_str());
1161 nlohmann::json jsonPermission = nlohmann::json::parse(value, nullptr, false);
1162 if (jsonPermission.is_null() || jsonPermission.empty()) {
1163 ANS_LOGE("Invalid JSON object");
1164 return false;
1165 }
1166 if (jsonPermission.is_discarded() || !jsonPermission.is_array()) {
1167 ANS_LOGE("Parse bundle permission failed due to data is discarded or not array");
1168 return false;
1169 }
1170
1171 for (const auto &item : jsonPermission) {
1172 bundleNames.insert(item);
1173 if (item == "ALL_PKG") {
1174 allPackage = true;
1175 }
1176 }
1177 return true;
1178 }
1179
SetKvToDb(const std::string & key,const std::string & value,const int32_t & userId)1180 int32_t NotificationPreferences::SetKvToDb(
1181 const std::string &key, const std::string &value, const int32_t &userId)
1182 {
1183 if (preferncesDB_ == nullptr) {
1184 return ERR_ANS_SERVICE_NOT_READY;
1185 }
1186 return preferncesDB_->SetKvToDb(key, value, userId);
1187 }
1188
SetByteToDb(const std::string & key,const std::vector<uint8_t> & value,const int32_t & userId)1189 int32_t NotificationPreferences::SetByteToDb(
1190 const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId)
1191 {
1192 if (preferncesDB_ == nullptr) {
1193 return ERR_ANS_SERVICE_NOT_READY;
1194 }
1195 return preferncesDB_->SetByteToDb(key, value, userId);
1196 }
1197
GetKvFromDb(const std::string & key,std::string & value,const int32_t & userId)1198 int32_t NotificationPreferences::GetKvFromDb(
1199 const std::string &key, std::string &value, const int32_t &userId)
1200 {
1201 if (preferncesDB_ == nullptr) {
1202 return ERR_ANS_SERVICE_NOT_READY;
1203 }
1204 return preferncesDB_->GetKvFromDb(key, value, userId);
1205 }
1206
GetByteFromDb(const std::string & key,std::vector<uint8_t> & value,const int32_t & userId)1207 int32_t NotificationPreferences::GetByteFromDb(
1208 const std::string &key, std::vector<uint8_t> &value, const int32_t &userId)
1209 {
1210 if (preferncesDB_ == nullptr) {
1211 return ERR_ANS_SERVICE_NOT_READY;
1212 }
1213 return preferncesDB_->GetByteFromDb(key, value, userId);
1214 }
1215
GetBatchKvsFromDb(const std::string & key,std::unordered_map<std::string,std::string> & values,const int32_t & userId)1216 int32_t NotificationPreferences::GetBatchKvsFromDb(
1217 const std::string &key, std::unordered_map<std::string, std::string> &values, const int32_t &userId)
1218 {
1219 if (preferncesDB_ == nullptr) {
1220 return ERR_ANS_SERVICE_NOT_READY;
1221 }
1222 return preferncesDB_->GetBatchKvsFromDb(key, values, userId);
1223 }
1224
DeleteKvFromDb(const std::string & key,const int32_t & userId)1225 int32_t NotificationPreferences::DeleteKvFromDb(const std::string &key, const int32_t &userId)
1226 {
1227 if (preferncesDB_ == nullptr) {
1228 return ERR_ANS_SERVICE_NOT_READY;
1229 }
1230 return preferncesDB_->DeleteKvFromDb(key, userId);
1231 }
1232
DeleteBatchKvFromDb(const std::vector<std::string> & keys,const int32_t & userId)1233 int32_t NotificationPreferences::DeleteBatchKvFromDb(const std::vector<std::string> &keys, const int32_t &userId)
1234 {
1235 if (preferncesDB_ == nullptr) {
1236 return ERR_ANS_SERVICE_NOT_READY;
1237 }
1238 return preferncesDB_->DeleteBatchKvFromDb(keys, userId);
1239 }
1240
IsAgentRelationship(const std::string & agentBundleName,const std::string & sourceBundleName)1241 bool NotificationPreferences::IsAgentRelationship(const std::string &agentBundleName,
1242 const std::string &sourceBundleName)
1243 {
1244 if (AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) {
1245 ANS_LOGD("Client has agent permission.");
1246 return true;
1247 }
1248
1249 if (preferncesDB_ == nullptr) {
1250 ANS_LOGD("perferencdDb is null.");
1251 return false;
1252 }
1253
1254 return preferncesDB_->IsAgentRelationship(agentBundleName, sourceBundleName);
1255 }
1256
GetAdditionalConfig(const std::string & key)1257 std::string NotificationPreferences::GetAdditionalConfig(const std::string &key)
1258 {
1259 if (preferncesDB_ == nullptr) {
1260 return "";
1261 }
1262 return preferncesDB_->GetAdditionalConfig(key);
1263 }
1264
DelCloneProfileInfo(const int32_t & userId,const sptr<NotificationDoNotDisturbProfile> & info)1265 bool NotificationPreferences::DelCloneProfileInfo(const int32_t &userId,
1266 const sptr<NotificationDoNotDisturbProfile>& info)
1267 {
1268 if (preferncesDB_ == nullptr) {
1269 return false;
1270 }
1271 return preferncesDB_->DelCloneProfileInfo(userId, info);
1272 }
1273
UpdateBatchCloneProfileInfo(const int32_t & userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> & profileInfo)1274 bool NotificationPreferences::UpdateBatchCloneProfileInfo(const int32_t &userId,
1275 const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo)
1276 {
1277 if (preferncesDB_ == nullptr) {
1278 return false;
1279 }
1280 return preferncesDB_->UpdateBatchCloneProfileInfo(userId, profileInfo);
1281 }
1282
GetAllCloneProfileInfo(const int32_t & userId,std::vector<sptr<NotificationDoNotDisturbProfile>> & profilesInfo)1283 void NotificationPreferences::GetAllCloneProfileInfo(const int32_t &userId,
1284 std::vector<sptr<NotificationDoNotDisturbProfile>>& profilesInfo)
1285 {
1286 if (preferncesDB_ == nullptr) {
1287 return;
1288 }
1289 return preferncesDB_->GetAllCloneProfileInfo(userId, profilesInfo);
1290 }
1291
GetAllCloneBundleInfo(const int32_t & userId,std::vector<NotificationCloneBundleInfo> & cloneBundleInfo)1292 void NotificationPreferences::GetAllCloneBundleInfo(const int32_t &userId,
1293 std::vector<NotificationCloneBundleInfo>& cloneBundleInfo)
1294 {
1295 if (preferncesDB_ == nullptr) {
1296 return;
1297 }
1298 return preferncesDB_->GetAllCloneBundleInfo(userId, cloneBundleInfo);
1299 }
1300
UpdateBatchCloneBundleInfo(const int32_t & userId,const std::vector<NotificationCloneBundleInfo> & cloneBundleInfo)1301 bool NotificationPreferences::UpdateBatchCloneBundleInfo(const int32_t &userId,
1302 const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo)
1303 {
1304 if (preferncesDB_ == nullptr) {
1305 return false;
1306 }
1307 return preferncesDB_->UpdateBatchCloneBundleInfo(userId, cloneBundleInfo);
1308 }
1309
DelCloneBundleInfo(const int32_t & userId,const NotificationCloneBundleInfo & cloneBundleInfo)1310 bool NotificationPreferences::DelCloneBundleInfo(const int32_t &userId,
1311 const NotificationCloneBundleInfo& cloneBundleInfo)
1312 {
1313 if (preferncesDB_ == nullptr) {
1314 return false;
1315 }
1316 return preferncesDB_->DelCloneBundleInfo(userId, cloneBundleInfo);
1317 }
1318
DelBatchCloneProfileInfo(const int32_t & userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> & profileInfo)1319 bool NotificationPreferences::DelBatchCloneProfileInfo(const int32_t &userId,
1320 const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo)
1321 {
1322 if (preferncesDB_ == nullptr) {
1323 return false;
1324 }
1325 return preferncesDB_->DelBatchCloneProfileInfo(userId, profileInfo);
1326 }
1327
DelBatchCloneBundleInfo(const int32_t & userId,const std::vector<NotificationCloneBundleInfo> & cloneBundleInfo)1328 bool NotificationPreferences::DelBatchCloneBundleInfo(const int32_t &userId,
1329 const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo)
1330 {
1331 if (preferncesDB_ == nullptr) {
1332 return false;
1333 }
1334 return preferncesDB_->DelBatchCloneBundleInfo(userId, cloneBundleInfo);
1335 }
1336
SetHashCodeRule(const int32_t uid,const uint32_t type)1337 ErrCode NotificationPreferences::SetHashCodeRule(const int32_t uid, const uint32_t type)
1338 {
1339 ANS_LOGD("%{public}s", __FUNCTION__);
1340
1341 std::lock_guard<std::mutex> lock(preferenceMutex_);
1342 bool storeDBResult = true;
1343 storeDBResult = preferncesDB_->SetHashCodeRule(uid, type);
1344 return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1345 }
1346
GetHashCodeRule(const int32_t uid)1347 uint32_t NotificationPreferences::GetHashCodeRule(const int32_t uid)
1348 {
1349 ANS_LOGD("%{public}s", __FUNCTION__);
1350 std::lock_guard<std::mutex> lock(preferenceMutex_);
1351 uint32_t result = 0;
1352 result = preferncesDB_->GetHashCodeRule(uid);
1353 ANS_LOGI("GetHashCodeRule uid = %{public}d result = %{public}d", uid, result);
1354 return result;
1355 }
1356 } // namespace Notification
1357 } // namespace OHOS
1358