• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
324 
SetNotificationSlotFlagsForBundle(const sptr<NotificationBundleOption> & bundleOption,uint32_t slotFlags)325 ErrCode NotificationPreferences::SetNotificationSlotFlagsForBundle(
326     const sptr<NotificationBundleOption> &bundleOption, uint32_t slotFlags)
327 {
328     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
329         return ERR_ANS_INVALID_PARAM;
330     }
331 
332     std::lock_guard<std::mutex> lock(preferenceMutex_);
333     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
334     ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_SLOTFLGS_TYPE, slotFlags);
335     if (result == ERR_OK) {
336         preferencesInfo_ = preferencesInfo;
337     }
338     return result;
339 }
340 
IsShowBadge(const sptr<NotificationBundleOption> & bundleOption,bool & enable)341 ErrCode NotificationPreferences::IsShowBadge(const sptr<NotificationBundleOption> &bundleOption, bool &enable)
342 {
343     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
344         return ERR_ANS_INVALID_PARAM;
345     }
346     return GetBundleProperty(bundleOption, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable);
347 }
348 
SetShowBadge(const sptr<NotificationBundleOption> & bundleOption,const bool enable)349 ErrCode NotificationPreferences::SetShowBadge(const sptr<NotificationBundleOption> &bundleOption, const bool enable)
350 {
351     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
352         return ERR_ANS_INVALID_PARAM;
353     }
354     std::lock_guard<std::mutex> lock(preferenceMutex_);
355     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
356     ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable);
357     if (result == ERR_OK) {
358         preferencesInfo_ = preferencesInfo;
359     }
360     return result;
361 }
362 
GetImportance(const sptr<NotificationBundleOption> & bundleOption,int32_t & importance)363 ErrCode NotificationPreferences::GetImportance(const sptr<NotificationBundleOption> &bundleOption, int32_t &importance)
364 {
365     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
366         return ERR_ANS_INVALID_PARAM;
367     }
368 
369     return GetBundleProperty(bundleOption, BundleType::BUNDLE_IMPORTANCE_TYPE, importance);
370 }
371 
372 
SetImportance(const sptr<NotificationBundleOption> & bundleOption,const int32_t & importance)373 ErrCode NotificationPreferences::SetImportance(
374     const sptr<NotificationBundleOption> &bundleOption, const int32_t &importance)
375 {
376     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
377         return ERR_ANS_INVALID_PARAM;
378     }
379     std::lock_guard<std::mutex> lock(preferenceMutex_);
380     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
381     ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_IMPORTANCE_TYPE, importance);
382     if (result == ERR_OK) {
383         preferencesInfo_ = preferencesInfo;
384     }
385     return result;
386 }
387 
GetTotalBadgeNums(const sptr<NotificationBundleOption> & bundleOption,int32_t & totalBadgeNum)388 ErrCode NotificationPreferences::GetTotalBadgeNums(
389     const sptr<NotificationBundleOption> &bundleOption, int32_t &totalBadgeNum)
390 {
391     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
392         return ERR_ANS_INVALID_PARAM;
393     }
394     return GetBundleProperty(bundleOption, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum);
395 }
396 
SetTotalBadgeNums(const sptr<NotificationBundleOption> & bundleOption,const int32_t num)397 ErrCode NotificationPreferences::SetTotalBadgeNums(
398     const sptr<NotificationBundleOption> &bundleOption, const int32_t num)
399 {
400     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
401         return ERR_ANS_INVALID_PARAM;
402     }
403     std::lock_guard<std::mutex> lock(preferenceMutex_);
404     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
405     ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, num);
406     if (result == ERR_OK) {
407         preferencesInfo_ = preferencesInfo;
408     }
409     return result;
410 }
411 
GetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> & bundleOption,bool & enabled)412 ErrCode NotificationPreferences::GetNotificationsEnabledForBundle(
413     const sptr<NotificationBundleOption> &bundleOption, bool &enabled)
414 {
415     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
416         return ERR_ANS_INVALID_PARAM;
417     }
418     return GetBundleProperty(bundleOption, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled);
419 }
420 
SetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> & bundleOption,const bool enabled)421 ErrCode NotificationPreferences::SetNotificationsEnabledForBundle(
422     const sptr<NotificationBundleOption> &bundleOption, const bool enabled)
423 {
424     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
425     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
426         return ERR_ANS_INVALID_PARAM;
427     }
428 
429     std::lock_guard<std::mutex> lock(preferenceMutex_);
430     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
431     ErrCode result =
432         SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled);
433     if (result == ERR_OK) {
434         preferencesInfo_ = preferencesInfo;
435     }
436     return result;
437 }
438 
GetNotificationsEnabled(const int32_t & userId,bool & enabled)439 ErrCode NotificationPreferences::GetNotificationsEnabled(const int32_t &userId, bool &enabled)
440 {
441     if (userId <= SUBSCRIBE_USER_INIT) {
442         return ERR_ANS_INVALID_PARAM;
443     }
444 
445     ErrCode result = ERR_OK;
446     std::lock_guard<std::mutex> lock(preferenceMutex_);
447     if (!preferencesInfo_.GetEnabledAllNotification(userId, enabled)) {
448         result = ERR_ANS_INVALID_PARAM;
449     }
450     return result;
451 }
452 
SetNotificationsEnabled(const int32_t & userId,const bool & enabled)453 ErrCode NotificationPreferences::SetNotificationsEnabled(const int32_t &userId, const bool &enabled)
454 {
455     if (userId <= SUBSCRIBE_USER_INIT) {
456         return ERR_ANS_INVALID_PARAM;
457     }
458     std::lock_guard<std::mutex> lock(preferenceMutex_);
459     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
460     preferencesInfo.SetEnabledAllNotification(userId, enabled);
461     ErrCode result = ERR_OK;
462     if (!preferncesDB_->PutNotificationsEnabled(userId, enabled)) {
463         result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
464     }
465 
466     if (result == ERR_OK) {
467         preferencesInfo_ = preferencesInfo;
468     }
469     return result;
470 }
471 
GetHasPoppedDialog(const sptr<NotificationBundleOption> & bundleOption,bool & hasPopped)472 ErrCode NotificationPreferences::GetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool &hasPopped)
473 {
474     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
475         return ERR_ANS_INVALID_PARAM;
476     }
477     return GetBundleProperty(bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
478 }
479 
SetHasPoppedDialog(const sptr<NotificationBundleOption> & bundleOption,bool hasPopped)480 ErrCode NotificationPreferences::SetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool hasPopped)
481 {
482     if (bundleOption == nullptr) {
483         return ERR_ANS_INVALID_PARAM;
484     }
485     std::lock_guard<std::mutex> lock(preferenceMutex_);
486     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
487     ErrCode result = ERR_OK;
488     result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
489     if (result == ERR_OK) {
490         preferencesInfo_ = preferencesInfo;
491     }
492     return result;
493 }
494 
GetDoNotDisturbDate(const int32_t & userId,sptr<NotificationDoNotDisturbDate> & date)495 ErrCode NotificationPreferences::GetDoNotDisturbDate(const int32_t &userId,
496     sptr<NotificationDoNotDisturbDate> &date)
497 {
498     if (userId <= SUBSCRIBE_USER_INIT) {
499         return ERR_ANS_INVALID_PARAM;
500     }
501 
502     ErrCode result = ERR_OK;
503     std::lock_guard<std::mutex> lock(preferenceMutex_);
504     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
505     if (!preferencesInfo.GetDoNotDisturbDate(userId, date)) {
506         result = ERR_ANS_INVALID_PARAM;
507     }
508     return result;
509 }
510 
SetDoNotDisturbDate(const int32_t & userId,const sptr<NotificationDoNotDisturbDate> date)511 ErrCode NotificationPreferences::SetDoNotDisturbDate(const int32_t &userId,
512     const sptr<NotificationDoNotDisturbDate> date)
513 {
514     ANS_LOGE("enter.");
515     if (userId <= SUBSCRIBE_USER_INIT) {
516         return ERR_ANS_INVALID_PARAM;
517     }
518     std::lock_guard<std::mutex> lock(preferenceMutex_);
519     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
520     preferencesInfo.SetDoNotDisturbDate(userId, date);
521 
522     ErrCode result = ERR_OK;
523     if (!preferncesDB_->PutDoNotDisturbDate(userId, date)) {
524         result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
525     }
526 
527     if (result == ERR_OK) {
528         preferencesInfo_ = preferencesInfo;
529     }
530     return result;
531 }
532 
AddDoNotDisturbProfiles(int32_t userId,std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)533 ErrCode NotificationPreferences::AddDoNotDisturbProfiles(
534     int32_t userId, std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)
535 {
536     ANS_LOGD("Called.");
537     for (auto profile : profiles) {
538         if (profile == nullptr) {
539             ANS_LOGE("The profile is nullptr.");
540             return ERR_ANS_INVALID_PARAM;
541         }
542         auto trustList = profile->GetProfileTrustList();
543         for (auto& bundleInfo : trustList) {
544             int32_t index = BundleManagerHelper::GetInstance()->GetAppIndexByUid(bundleInfo.GetUid());
545             bundleInfo.SetAppIndex(index);
546             ANS_LOGI("Get app index by uid %{public}d %{public}s %{public}d", bundleInfo.GetUid(),
547                 bundleInfo.GetBundleName().c_str(), index);
548         }
549         profile->SetProfileTrustList(trustList);
550     }
551     std::lock_guard<std::mutex> lock(preferenceMutex_);
552     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
553     preferencesInfo.AddDoNotDisturbProfiles(userId, profiles);
554     if (preferncesDB_ == nullptr) {
555         ANS_LOGE("The prefernces db is nullptr.");
556         return ERR_ANS_SERVICE_NOT_READY;
557     }
558     if (!preferncesDB_->AddDoNotDisturbProfiles(userId, profiles)) {
559         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
560     }
561     preferencesInfo_ = preferencesInfo;
562     return ERR_OK;
563 }
564 
IsNotificationSlotFlagsExists(const sptr<NotificationBundleOption> & bundleOption)565 bool NotificationPreferences::IsNotificationSlotFlagsExists(
566     const sptr<NotificationBundleOption> &bundleOption)
567 {
568     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
569         return false;
570     }
571     return preferncesDB_->IsNotificationSlotFlagsExists(bundleOption);
572 }
573 
GetBundleInfo(NotificationPreferencesInfo & preferencesInfo,const sptr<NotificationBundleOption> & bundleOption,NotificationPreferencesInfo::BundleInfo & info) const574 bool NotificationPreferences::GetBundleInfo(NotificationPreferencesInfo &preferencesInfo,
575     const sptr<NotificationBundleOption> &bundleOption, NotificationPreferencesInfo::BundleInfo &info) const
576 {
577     if (preferencesInfo.GetBundleInfo(bundleOption, info)) {
578         return true;
579     } else if (preferncesDB_->GetBundleInfo(bundleOption, info)) {
580         preferencesInfo.SetBundleInfo(info);
581         return true;
582     }
583     return false;
584 }
585 
RemoveDoNotDisturbProfiles(int32_t userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)586 ErrCode NotificationPreferences::RemoveDoNotDisturbProfiles(
587     int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)
588 {
589     ANS_LOGD("Called.");
590     for (auto profile : profiles) {
591         if (profile == nullptr) {
592             ANS_LOGE("The profile is nullptr.");
593             return ERR_ANS_INVALID_PARAM;
594         }
595     }
596     std::lock_guard<std::mutex> lock(preferenceMutex_);
597     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
598     preferencesInfo.RemoveDoNotDisturbProfiles(userId, profiles);
599     if (preferncesDB_ == nullptr) {
600         ANS_LOGE("The prefernces db is nullptr.");
601         return ERR_ANS_SERVICE_NOT_READY;
602     }
603     if (!preferncesDB_->RemoveDoNotDisturbProfiles(userId, profiles)) {
604         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
605     }
606     preferencesInfo_ = preferencesInfo;
607     return ERR_OK;
608 }
609 
UpdateProfilesUtil(std::vector<NotificationBundleOption> & trustList,const std::vector<NotificationBundleOption> bundleList)610 void NotificationPreferences::UpdateProfilesUtil(std::vector<NotificationBundleOption>& trustList,
611     const std::vector<NotificationBundleOption> bundleList)
612 {
613     for (auto& item : bundleList) {
614         bool exit = false;
615         for (auto& bundle: trustList) {
616             if (item.GetUid() == bundle.GetUid()) {
617                 exit = true;
618                 break;
619             }
620         }
621         if (!exit) {
622             trustList.push_back(item);
623         }
624     }
625 }
626 
UpdateDoNotDisturbProfiles(int32_t userId,int64_t profileId,const std::string & name,const std::vector<NotificationBundleOption> & bundleList)627 ErrCode NotificationPreferences::UpdateDoNotDisturbProfiles(int32_t userId, int64_t profileId,
628     const std::string& name, const std::vector<NotificationBundleOption>& bundleList)
629 {
630     ANS_LOGI("Called update Profile %{public}d %{public}s %{public}zu.",
631         userId, std::to_string(profileId).c_str(), bundleList.size());
632     if (bundleList.empty()) {
633         return ERR_ANS_INVALID_PARAM;
634     }
635 
636     sptr<NotificationDoNotDisturbProfile> profile = new (std::nothrow) NotificationDoNotDisturbProfile();
637     std::lock_guard<std::mutex> lock(preferenceMutex_);
638     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
639     if (preferencesInfo.GetDoNotDisturbProfiles(profileId, userId, profile)) {
640         auto trustList = profile->GetProfileTrustList();
641         UpdateProfilesUtil(trustList, bundleList);
642         profile->SetProfileTrustList(trustList);
643     } else {
644         profile->SetProfileId(profileId);
645         profile->SetProfileName(name);
646         profile->SetProfileTrustList(bundleList);
647     }
648     ANS_LOGI("Update profile %{public}d %{public}s %{public}zu",
649         userId, std::to_string(profile->GetProfileId()).c_str(),
650         profile->GetProfileTrustList().size());
651     preferencesInfo.AddDoNotDisturbProfiles(userId, {profile});
652     if (preferncesDB_ == nullptr) {
653         ANS_LOGE("The prefernces db is nullptr.");
654         return ERR_ANS_SERVICE_NOT_READY;
655     }
656     if (!preferncesDB_->AddDoNotDisturbProfiles(userId, {profile})) {
657         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
658     }
659     preferencesInfo_ = preferencesInfo;
660     return ERR_OK;
661 }
662 
UpdateCloneBundleInfo(int32_t userId,const NotificationCloneBundleInfo & cloneBundleInfo)663 void NotificationPreferences::UpdateCloneBundleInfo(int32_t userId,
664     const NotificationCloneBundleInfo& cloneBundleInfo)
665 {
666     ANS_LOGI("Event bundle update %{public}s.", cloneBundleInfo.Dump().c_str());
667     NotificationPreferencesInfo::BundleInfo bundleInfo;
668     sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption();
669     if (bundleOption == nullptr) {
670         return;
671     }
672     bundleOption->SetBundleName(cloneBundleInfo.GetBundleName());
673     bundleOption->SetUid(cloneBundleInfo.GetUid());
674     std::lock_guard<std::mutex> lock(preferenceMutex_);
675     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
676     if (!GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
677         bundleInfo.SetBundleName(cloneBundleInfo.GetBundleName());
678         bundleInfo.SetBundleUid(cloneBundleInfo.GetUid());
679     }
680 
681     /* after clone, override these witch */
682     bundleInfo.SetIsShowBadge(cloneBundleInfo.GetIsShowBadge());
683     bundleInfo.SetEnableNotification(cloneBundleInfo.GetEnableNotification());
684     /* update property to db */
685     if (!preferncesDB_->UpdateBundlePropertyToDisturbeDB(userId, bundleInfo)) {
686         ANS_LOGW("Clone bundle info failed %{public}s.", cloneBundleInfo.Dump().c_str());
687         return;
688     }
689 
690     if (SaveBundleProperty(bundleInfo, bundleOption,
691         BundleType::BUNDLE_SLOTFLGS_TYPE, cloneBundleInfo.GetSlotFlags()) != ERR_OK) {
692         ANS_LOGW("Clone bundle slot info %{public}s.", cloneBundleInfo.Dump().c_str());
693         return;
694     }
695     preferencesInfo.SetBundleInfo(bundleInfo);
696 
697     /* update slot info */
698     std::vector<sptr<NotificationSlot>> slots;
699     for (auto& cloneSlot : cloneBundleInfo.GetSlotInfo()) {
700         sptr<NotificationSlot> slotInfo = new (std::nothrow) NotificationSlot(cloneSlot.slotType_);
701         if (slotInfo == nullptr) {
702             return;
703         }
704         uint32_t slotFlags = bundleInfo.GetSlotFlags();
705         auto configSlotReminderMode = DelayedSingleton<NotificationConfigParse>::GetInstance()->
706             GetConfigSlotReminderModeByType(slotInfo->GetType());
707         slotInfo->SetReminderMode(configSlotReminderMode & slotFlags);
708         slotInfo->SetEnable(cloneSlot.enable_);
709         slotInfo->SetForceControl(cloneSlot.isForceControl_);
710         slotInfo->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED);
711         slots.push_back(slotInfo);
712         bundleInfo.SetSlot(slotInfo);
713     }
714 
715     if (!preferncesDB_->UpdateBundleSlotToDisturbeDB(userId, cloneBundleInfo.GetBundleName(),
716         cloneBundleInfo.GetUid(), slots)) {
717         ANS_LOGW("Clone bundle slot failed %{public}s.", cloneBundleInfo.Dump().c_str());
718         preferencesInfo_ = preferencesInfo;
719         return;
720     }
721     preferencesInfo.SetBundleInfo(bundleInfo);
722     preferencesInfo_ = preferencesInfo;
723 }
724 
GetAllCLoneBundlesInfo(int32_t userId,std::vector<NotificationCloneBundleInfo> & cloneBundles)725 void NotificationPreferences::GetAllCLoneBundlesInfo(int32_t userId,
726     std::vector<NotificationCloneBundleInfo> &cloneBundles)
727 {
728     std::lock_guard<std::mutex> lock(preferenceMutex_);
729     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
730     std::unordered_map<std::string, std::string> bundlesMap;
731     if (GetBatchKvsFromDb(KEY_BUNDLE_LABEL, bundlesMap, userId) != ERR_OK) {
732         ANS_LOGE("Get bundle map info failed.");
733         return;
734     }
735     preferencesInfo.GetAllCLoneBundlesInfo(userId, bundlesMap, cloneBundles);
736     preferencesInfo_ = preferencesInfo;
737 }
738 
GetDoNotDisturbProfileListByUserId(int32_t userId,std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)739 void NotificationPreferences::GetDoNotDisturbProfileListByUserId(int32_t userId,
740     std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
741 {
742     std::lock_guard<std::mutex> lock(preferenceMutex_);
743     preferencesInfo_.GetAllDoNotDisturbProfiles(userId, profiles);
744 }
745 
GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> & bundleOption)746 ErrCode NotificationPreferences::GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> &bundleOption)
747 {
748     ANS_LOGD("Called.");
749     std::lock_guard<std::mutex> lock(preferenceMutex_);
750     if (preferncesDB_ == nullptr) {
751         return ERR_ANS_SERVICE_NOT_READY;
752     }
753     if (!preferncesDB_->GetAllNotificationEnabledBundles(bundleOption)) {
754         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
755     }
756     return ERR_OK;
757 }
758 
GetAllLiveViewEnabledBundles(const int32_t userId,std::vector<NotificationBundleOption> & bundleOption)759 ErrCode NotificationPreferences::GetAllLiveViewEnabledBundles(const int32_t userId,
760     std::vector<NotificationBundleOption> &bundleOption)
761 {
762     ANS_LOGD("Called.");
763     std::lock_guard<std::mutex> lock(preferenceMutex_);
764     return preferencesInfo_.GetAllLiveViewEnabledBundles(userId, bundleOption);
765 }
766 
GetAllDistribuedEnabledBundles(int32_t userId,const std::string & deviceType,std::vector<NotificationBundleOption> & bundleOption)767 ErrCode NotificationPreferences::GetAllDistribuedEnabledBundles(int32_t userId,
768     const std::string &deviceType, std::vector<NotificationBundleOption> &bundleOption)
769 {
770     ANS_LOGD("Called.");
771     std::lock_guard<std::mutex> lock(preferenceMutex_);
772     if (preferncesDB_ == nullptr) {
773         return ERR_ANS_SERVICE_NOT_READY;
774     }
775     if (!preferncesDB_->GetAllDistribuedEnabledBundles(userId, deviceType, bundleOption)) {
776         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
777     }
778     return ERR_OK;
779 }
780 
ClearNotificationInRestoreFactorySettings()781 ErrCode NotificationPreferences::ClearNotificationInRestoreFactorySettings()
782 {
783     ErrCode result = ERR_OK;
784     std::lock_guard<std::mutex> lock(preferenceMutex_);
785     if (!preferncesDB_->RemoveAllDataFromDisturbeDB()) {
786         result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
787     }
788 
789     if (result == ERR_OK) {
790         preferencesInfo_ = NotificationPreferencesInfo();
791     }
792     return result;
793 }
794 
GetDoNotDisturbProfile(int64_t profileId,int32_t userId,sptr<NotificationDoNotDisturbProfile> & profile)795 ErrCode NotificationPreferences::GetDoNotDisturbProfile(
796     int64_t profileId, int32_t userId, sptr<NotificationDoNotDisturbProfile> &profile)
797 {
798     std::lock_guard<std::mutex> lock(preferenceMutex_);
799     if (!preferencesInfo_.GetDoNotDisturbProfiles(profileId, userId, profile)) {
800         return ERR_ANS_NO_PROFILE_TEMPLATE;
801     }
802     return ERR_OK;
803 }
804 
RemoveDoNotDisturbProfileTrustList(int32_t userId,const sptr<NotificationBundleOption> & bundleOption)805 void NotificationPreferences::RemoveDoNotDisturbProfileTrustList(
806     int32_t userId, const sptr<NotificationBundleOption> &bundleOption)
807 {
808     if (bundleOption == nullptr) {
809         ANS_LOGE("The bundle option is nullptr.");
810         return;
811     }
812     int32_t uid = bundleOption->GetUid();
813     int32_t appIndex = bundleOption->GetAppIndex();
814     auto bundleName = bundleOption->GetBundleName();
815     ANS_LOGI("Remove %{public}s %{public}d %{public}d.", bundleName.c_str(), uid, appIndex);
816     std::lock_guard<std::mutex> lock(preferenceMutex_);
817     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
818 
819     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
820     preferencesInfo.GetAllDoNotDisturbProfiles(userId, profiles);
821     for (auto profile : profiles) {
822         if (profile == nullptr) {
823             ANS_LOGE("The profile is nullptr.");
824             continue;
825         }
826         auto trustList = profile->GetProfileTrustList();
827         for (auto it = trustList.begin(); it != trustList.end(); it++) {
828             if (it->GetUid() == uid) {
829                 trustList.erase(it);
830                 break;
831             }
832         }
833         profile->SetProfileTrustList(trustList);
834     }
835     preferencesInfo.AddDoNotDisturbProfiles(userId, profiles);
836     if (preferncesDB_ == nullptr) {
837         ANS_LOGE("The prefernces db is nullptr.");
838         return;
839     }
840     if (!preferncesDB_->AddDoNotDisturbProfiles(userId, profiles)) {
841         return;
842     }
843     preferencesInfo_ = preferencesInfo;
844 }
845 
CheckSlotForCreateSlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot,NotificationPreferencesInfo & preferencesInfo) const846 ErrCode NotificationPreferences::CheckSlotForCreateSlot(const sptr<NotificationBundleOption> &bundleOption,
847     const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
848 {
849     if (slot == nullptr) {
850         ANS_LOGE("Notification slot is nullptr.");
851         return ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_NOT_EXIST;
852     }
853 
854     NotificationPreferencesInfo::BundleInfo bundleInfo;
855     if (!GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
856         bundleInfo.SetBundleName(bundleOption->GetBundleName());
857         bundleInfo.SetBundleUid(bundleOption->GetUid());
858         bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
859     }
860     bundleInfo.SetSlot(slot);
861     preferencesInfo.SetBundleInfo(bundleInfo);
862 
863     return ERR_OK;
864 }
865 
CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,NotificationPreferencesInfo & preferencesInfo) const866 ErrCode NotificationPreferences::CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> &bundleOption,
867     const NotificationConstant::SlotType &slotType, NotificationPreferencesInfo &preferencesInfo) const
868 {
869     ErrCode result = ERR_OK;
870     NotificationPreferencesInfo::BundleInfo bundleInfo;
871     if (GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
872         if (bundleInfo.IsExsitSlot(slotType)) {
873             bundleInfo.RemoveSlot(slotType);
874             preferencesInfo.SetBundleInfo(bundleInfo);
875         } else {
876             ANS_LOGE("Notification slot type does not exsited.");
877             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
878         }
879     } else {
880         ANS_LOGW("Notification bundle does not exsit.");
881         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
882     }
883     return result;
884 }
885 
CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot,NotificationPreferencesInfo & preferencesInfo) const886 ErrCode NotificationPreferences::CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> &bundleOption,
887     const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
888 {
889     if (slot == nullptr) {
890         ANS_LOGE("Notification slot is nullptr.");
891         return ERR_ANS_INVALID_PARAM;
892     }
893 
894     ErrCode result = ERR_OK;
895     NotificationPreferencesInfo::BundleInfo bundleInfo;
896     if (GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
897         if (bundleInfo.IsExsitSlot(slot->GetType())) {
898             bundleInfo.SetBundleName(bundleOption->GetBundleName());
899             bundleInfo.SetBundleUid(bundleOption->GetUid());
900             bundleInfo.SetSlot(slot);
901             preferencesInfo.SetBundleInfo(bundleInfo);
902         } else {
903             ANS_LOGE("Notification slot type does not exist.");
904             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
905         }
906     } else {
907         ANS_LOGW("Notification bundle does not exsit.");
908         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
909     }
910 
911     return result;
912 }
913 
914 template <typename T>
SetBundleProperty(NotificationPreferencesInfo & preferencesInfo,const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,const T & value)915 ErrCode NotificationPreferences::SetBundleProperty(NotificationPreferencesInfo &preferencesInfo,
916     const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
917 {
918     ErrCode result = ERR_OK;
919     NotificationPreferencesInfo::BundleInfo bundleInfo;
920     if (!GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
921         bundleInfo.SetBundleName(bundleOption->GetBundleName());
922         bundleInfo.SetBundleUid(bundleOption->GetUid());
923         bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
924     }
925     result = SaveBundleProperty(bundleInfo, bundleOption, type, value);
926     if (result == ERR_OK) {
927         preferencesInfo.SetBundleInfo(bundleInfo);
928     }
929 
930     return result;
931 }
932 
933 template <typename T>
SaveBundleProperty(NotificationPreferencesInfo::BundleInfo & bundleInfo,const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,const T & value)934 ErrCode NotificationPreferences::SaveBundleProperty(NotificationPreferencesInfo::BundleInfo &bundleInfo,
935     const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
936 {
937     bool storeDBResult = true;
938     switch (type) {
939         case BundleType::BUNDLE_IMPORTANCE_TYPE:
940             bundleInfo.SetImportance(value);
941             storeDBResult = preferncesDB_->PutImportance(bundleInfo, value);
942             break;
943         case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
944             bundleInfo.SetBadgeTotalNum(value);
945             storeDBResult = preferncesDB_->PutTotalBadgeNums(bundleInfo, value);
946             break;
947         case BundleType::BUNDLE_SHOW_BADGE_TYPE:
948             bundleInfo.SetIsShowBadge(value);
949             storeDBResult = preferncesDB_->PutShowBadge(bundleInfo, value);
950             break;
951         case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
952             bundleInfo.SetEnableNotification(value);
953             storeDBResult = preferncesDB_->PutNotificationsEnabledForBundle(bundleInfo, value);
954             if (value && storeDBResult) {
955                 SetDistributedEnabledForBundle(bundleInfo);
956             }
957             break;
958         case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
959             ANS_LOGI("Into BUNDLE_POPPED_DIALOG_TYPE:SetHasPoppedDialog.");
960             bundleInfo.SetHasPoppedDialog(value);
961             storeDBResult = preferncesDB_->PutHasPoppedDialog(bundleInfo, value);
962             break;
963         case BundleType::BUNDLE_SLOTFLGS_TYPE:
964             ANS_LOGI("Into BUNDLE_SLOTFLGS_TYPE:SetSlotFlags.");
965             bundleInfo.SetSlotFlags(value);
966             storeDBResult = preferncesDB_->PutSlotFlags(bundleInfo, value);
967             break;
968         default:
969             break;
970     }
971     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
972 }
973 
974 template <typename T>
GetBundleProperty(const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,T & value)975 ErrCode NotificationPreferences::GetBundleProperty(
976     const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, T &value)
977 {
978     ErrCode result = ERR_OK;
979     NotificationPreferencesInfo::BundleInfo bundleInfo;
980     std::lock_guard<std::mutex> lock(preferenceMutex_);
981     if (GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
982         switch (type) {
983             case BundleType::BUNDLE_IMPORTANCE_TYPE:
984                 value = bundleInfo.GetImportance();
985                 break;
986             case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
987                 value = bundleInfo.GetBadgeTotalNum();
988                 break;
989             case BundleType::BUNDLE_SHOW_BADGE_TYPE:
990                 value = bundleInfo.GetIsShowBadge();
991                 break;
992             case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
993                 value = bundleInfo.GetEnableNotification();
994                 break;
995             case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
996                 ANS_LOGD("Into BUNDLE_POPPED_DIALOG_TYPE:GetHasPoppedDialog.");
997                 value = bundleInfo.GetHasPoppedDialog();
998                 break;
999             case BundleType::BUNDLE_SLOTFLGS_TYPE:
1000                 value = bundleInfo.GetSlotFlags();
1001                 ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:GetSlotFlags.");
1002                 break;
1003             default:
1004                 result = ERR_ANS_INVALID_PARAM;
1005                 break;
1006         }
1007     } else {
1008         ANS_LOGW("Notification bundle does not exsit.");
1009         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
1010     }
1011     return result;
1012 }
1013 
GenerateBundleKey(const sptr<NotificationBundleOption> & bundleOption) const1014 std::string NotificationPreferences::GenerateBundleKey(const sptr<NotificationBundleOption> &bundleOption) const
1015 {
1016     return bundleOption->GetBundleName().append(std::to_string(bundleOption->GetUid()));
1017 }
1018 
GetTemplateSupported(const std::string & templateName,bool & support)1019 ErrCode NotificationPreferences::GetTemplateSupported(const std::string& templateName, bool &support)
1020 {
1021     if (templateName.length() == 0) {
1022         ANS_LOGE("template name is null.");
1023         return ERR_ANS_INVALID_PARAM;
1024     }
1025 
1026     std::ifstream inFile;
1027     inFile.open(DEFAULT_TEMPLATE_PATH.c_str(), std::ios::in);
1028     if (!inFile.is_open()) {
1029         ANS_LOGE("read template config error.");
1030         return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
1031     }
1032 
1033     nlohmann::json jsonObj;
1034     inFile >> jsonObj;
1035     if (jsonObj.is_null() || !jsonObj.is_object()) {
1036         ANS_LOGE("Invalid JSON object");
1037         return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
1038     }
1039     if (jsonObj.is_discarded()) {
1040         ANS_LOGE("template json discarded error.");
1041         inFile.close();
1042         return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
1043     }
1044 
1045     if (jsonObj.contains(templateName)) {
1046         support = true;
1047     }
1048 
1049     jsonObj.clear();
1050     inFile.close();
1051     return ERR_OK;
1052 }
1053 
SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,const bool enabled)1054 ErrCode NotificationPreferences::SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
1055     const std::string &deviceType, const bool enabled)
1056 {
1057     ANS_LOGD("%{public}s", __FUNCTION__);
1058     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
1059         return ERR_ANS_INVALID_PARAM;
1060     }
1061 
1062     std::lock_guard<std::mutex> lock(preferenceMutex_);
1063     NotificationPreferencesInfo::BundleInfo bundleInfo;
1064     bundleInfo.SetBundleName(bundleOption->GetBundleName());
1065     bundleInfo.SetBundleUid(bundleOption->GetUid());
1066     bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
1067     bool storeDBResult = true;
1068     storeDBResult = preferncesDB_->PutDistributedEnabledForBundle(deviceType, bundleInfo, enabled);
1069     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1070 }
1071 
IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,bool & enabled)1072 ErrCode NotificationPreferences::IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
1073     const std::string &deviceType, bool &enabled)
1074 {
1075     ANS_LOGD("%{public}s", __FUNCTION__);
1076     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
1077         return ERR_ANS_INVALID_PARAM;
1078     }
1079 
1080     std::lock_guard<std::mutex> lock(preferenceMutex_);
1081     NotificationPreferencesInfo::BundleInfo bundleInfo;
1082     bundleInfo.SetBundleName(bundleOption->GetBundleName());
1083     bundleInfo.SetBundleUid(bundleOption->GetUid());
1084     bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
1085     bool storeDBResult = true;
1086     storeDBResult = preferncesDB_->GetDistributedEnabledForBundle(deviceType, bundleInfo, enabled);
1087     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1088 }
1089 
SetSmartReminderEnabled(const std::string & deviceType,const bool enabled)1090 ErrCode NotificationPreferences::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled)
1091 {
1092     ANS_LOGD("%{public}s", __FUNCTION__);
1093     if (deviceType.empty()) {
1094         return ERR_ANS_INVALID_PARAM;
1095     }
1096 
1097     std::lock_guard<std::mutex> lock(preferenceMutex_);
1098     bool storeDBResult = true;
1099     storeDBResult = preferncesDB_->SetSmartReminderEnabled(deviceType, enabled);
1100     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1101 }
1102 
IsSmartReminderEnabled(const std::string & deviceType,bool & enabled)1103 ErrCode NotificationPreferences::IsSmartReminderEnabled(const std::string &deviceType, bool &enabled)
1104 {
1105     ANS_LOGD("%{public}s", __FUNCTION__);
1106     if (deviceType.empty()) {
1107         return ERR_ANS_INVALID_PARAM;
1108     }
1109 
1110     std::lock_guard<std::mutex> lock(preferenceMutex_);
1111     bool storeDBResult = true;
1112     storeDBResult = preferncesDB_->IsSmartReminderEnabled(deviceType, enabled);
1113     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1114 }
1115 
SetDistributedEnabledBySlot(const NotificationConstant::SlotType & slotType,const std::string & deviceType,const bool enabled)1116 ErrCode NotificationPreferences::SetDistributedEnabledBySlot(
1117     const NotificationConstant::SlotType &slotType, const std::string &deviceType, const bool enabled)
1118 {
1119     ANS_LOGD("%{public}s", __FUNCTION__);
1120     if (deviceType.empty()) {
1121         return ERR_ANS_INVALID_PARAM;
1122     }
1123 
1124     std::lock_guard<std::mutex> lock(preferenceMutex_);
1125     bool storeDBResult = true;
1126     storeDBResult = preferncesDB_->SetDistributedEnabledBySlot(slotType, deviceType, enabled);
1127     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1128 }
1129 
IsDistributedEnabledBySlot(const NotificationConstant::SlotType & slotType,const std::string & deviceType,bool & enabled)1130 ErrCode NotificationPreferences::IsDistributedEnabledBySlot(
1131     const NotificationConstant::SlotType &slotType, const std::string &deviceType, bool &enabled)
1132 {
1133     ANS_LOGD("%{public}s", __FUNCTION__);
1134     if (deviceType.empty()) {
1135         return ERR_ANS_INVALID_PARAM;
1136     }
1137 
1138     std::lock_guard<std::mutex> lock(preferenceMutex_);
1139     bool storeDBResult = true;
1140     storeDBResult = preferncesDB_->IsDistributedEnabledBySlot(slotType, deviceType, enabled);
1141     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1142 }
1143 
InitSettingFromDisturbDB(int32_t userId)1144 void NotificationPreferences::InitSettingFromDisturbDB(int32_t userId)
1145 {
1146     ANS_LOGI("%{public}s userId is %{public}d", __FUNCTION__, userId);
1147     std::lock_guard<std::mutex> lock(preferenceMutex_);
1148     if (preferncesDB_ != nullptr) {
1149         preferncesDB_->ParseFromDisturbeDB(preferencesInfo_, userId);
1150     }
1151 }
1152 
RemoveSettings(int32_t userId)1153 void NotificationPreferences::RemoveSettings(int32_t userId)
1154 {
1155     ANS_LOGD("%{public}s", __FUNCTION__);
1156     std::lock_guard<std::mutex> lock(preferenceMutex_);
1157     preferencesInfo_.RemoveNotificationEnable(userId);
1158     preferencesInfo_.RemoveDoNotDisturbDate(userId);
1159     if (preferncesDB_ != nullptr) {
1160         preferncesDB_->RemoveNotificationEnable(userId);
1161         preferncesDB_->RemoveDoNotDisturbDate(userId);
1162         preferncesDB_->DropUserTable(userId);
1163     }
1164 }
1165 
CheckApiCompatibility(const sptr<NotificationBundleOption> & bundleOption) const1166 bool NotificationPreferences::CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption) const
1167 {
1168     ANS_LOGD("%{public}s", __FUNCTION__);
1169     std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
1170     if (bundleManager == nullptr) {
1171         return false;
1172     }
1173     return bundleManager->CheckApiCompatibility(bundleOption);
1174 }
1175 
RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> & bundleOption)1176 void NotificationPreferences::RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> &bundleOption)
1177 {
1178     ANS_LOGE("%{public}s", __FUNCTION__);
1179     if (preferncesDB_ != nullptr && bundleOption != nullptr) {
1180         preferncesDB_->RemoveAnsBundleDbInfo(bundleOption->GetBundleName(), bundleOption->GetUid());
1181     }
1182 }
1183 
RemoveEnabledDbByBundle(const sptr<NotificationBundleOption> & bundleOption)1184 void NotificationPreferences::RemoveEnabledDbByBundle(const sptr<NotificationBundleOption> &bundleOption)
1185 {
1186     ANS_LOGE("%{public}s", __FUNCTION__);
1187     if (preferncesDB_ != nullptr && bundleOption != nullptr) {
1188         std::lock_guard<std::mutex> lock(preferenceMutex_);
1189         preferncesDB_->RemoveEnabledDbByBundleName(bundleOption->GetBundleName(), bundleOption->GetUid());
1190     }
1191 }
1192 
GetBundleSoundPermission(bool & allPackage,std::set<std::string> & bundleNames)1193 bool NotificationPreferences::GetBundleSoundPermission(bool &allPackage, std::set<std::string> &bundleNames)
1194 {
1195     ANS_LOGD("%{public}s", __FUNCTION__);
1196     std::string value = "";
1197     int32_t userId = -1;
1198     OsAccountManagerHelper::GetInstance().GetCurrentCallingUserId(userId);
1199     if (GetKvFromDb("RING_TRUSTLIST_PKG", value, userId) != ERR_OK) {
1200         ANS_LOGD("Get bundle sound permission failed.");
1201         return false;
1202     }
1203 
1204     ANS_LOGD("The bundle permission is :%{public}s.", value.c_str());
1205     nlohmann::json jsonPermission = nlohmann::json::parse(value, nullptr, false);
1206     if (jsonPermission.is_null() || jsonPermission.empty()) {
1207         ANS_LOGE("Invalid JSON object");
1208         return false;
1209     }
1210     if (jsonPermission.is_discarded() || !jsonPermission.is_array()) {
1211         ANS_LOGE("Parse bundle permission failed due to data is discarded or not array");
1212         return false;
1213     }
1214 
1215     for (const auto &item : jsonPermission) {
1216         bundleNames.insert(item);
1217         if (item == "ALL_PKG") {
1218             allPackage = true;
1219         }
1220     }
1221     return true;
1222 }
1223 
SetKvToDb(const std::string & key,const std::string & value,const int32_t & userId)1224 int32_t NotificationPreferences::SetKvToDb(
1225     const std::string &key, const std::string &value, const int32_t &userId)
1226 {
1227     if (preferncesDB_ == nullptr) {
1228         return ERR_ANS_SERVICE_NOT_READY;
1229     }
1230     return preferncesDB_->SetKvToDb(key, value, userId);
1231 }
1232 
SetByteToDb(const std::string & key,const std::vector<uint8_t> & value,const int32_t & userId)1233 int32_t NotificationPreferences::SetByteToDb(
1234     const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId)
1235 {
1236     if (preferncesDB_ == nullptr) {
1237         return ERR_ANS_SERVICE_NOT_READY;
1238     }
1239     return preferncesDB_->SetByteToDb(key, value, userId);
1240 }
1241 
GetKvFromDb(const std::string & key,std::string & value,const int32_t & userId)1242 int32_t NotificationPreferences::GetKvFromDb(
1243     const std::string &key, std::string &value, const int32_t &userId)
1244 {
1245     if (preferncesDB_ == nullptr) {
1246         return ERR_ANS_SERVICE_NOT_READY;
1247     }
1248     return preferncesDB_->GetKvFromDb(key, value, userId);
1249 }
1250 
GetByteFromDb(const std::string & key,std::vector<uint8_t> & value,const int32_t & userId)1251 int32_t NotificationPreferences::GetByteFromDb(
1252     const std::string &key, std::vector<uint8_t> &value, const int32_t &userId)
1253 {
1254     if (preferncesDB_ == nullptr) {
1255         return ERR_ANS_SERVICE_NOT_READY;
1256     }
1257     return preferncesDB_->GetByteFromDb(key, value, userId);
1258 }
1259 
GetBatchKvsFromDb(const std::string & key,std::unordered_map<std::string,std::string> & values,const int32_t & userId)1260 int32_t NotificationPreferences::GetBatchKvsFromDb(
1261     const std::string &key, std::unordered_map<std::string, std::string> &values, const int32_t &userId)
1262 {
1263     if (preferncesDB_ == nullptr) {
1264         return ERR_ANS_SERVICE_NOT_READY;
1265     }
1266     return preferncesDB_->GetBatchKvsFromDb(key, values, userId);
1267 }
1268 
DeleteKvFromDb(const std::string & key,const int32_t & userId)1269 int32_t NotificationPreferences::DeleteKvFromDb(const std::string &key, const int32_t &userId)
1270 {
1271     if (preferncesDB_ == nullptr) {
1272         return ERR_ANS_SERVICE_NOT_READY;
1273     }
1274     return preferncesDB_->DeleteKvFromDb(key, userId);
1275 }
1276 
DeleteBatchKvFromDb(const std::vector<std::string> & keys,const int32_t & userId)1277 int32_t NotificationPreferences::DeleteBatchKvFromDb(const std::vector<std::string> &keys,  const int32_t &userId)
1278 {
1279     if (preferncesDB_ == nullptr) {
1280         return ERR_ANS_SERVICE_NOT_READY;
1281     }
1282     return preferncesDB_->DeleteBatchKvFromDb(keys, userId);
1283 }
1284 
IsAgentRelationship(const std::string & agentBundleName,const std::string & sourceBundleName)1285 bool NotificationPreferences::IsAgentRelationship(const std::string &agentBundleName,
1286     const std::string &sourceBundleName)
1287 {
1288     if (AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) {
1289         ANS_LOGD("Client has agent permission.");
1290         return true;
1291     }
1292 
1293     if (preferncesDB_ == nullptr) {
1294         ANS_LOGD("perferencdDb is null.");
1295         return false;
1296     }
1297 
1298     return preferncesDB_->IsAgentRelationship(agentBundleName, sourceBundleName);
1299 }
1300 
GetAdditionalConfig(const std::string & key)1301 std::string NotificationPreferences::GetAdditionalConfig(const std::string &key)
1302 {
1303     if (preferncesDB_ == nullptr) {
1304         return "";
1305     }
1306     return preferncesDB_->GetAdditionalConfig(key);
1307 }
1308 
DelCloneProfileInfo(const int32_t & userId,const sptr<NotificationDoNotDisturbProfile> & info)1309 bool NotificationPreferences::DelCloneProfileInfo(const int32_t &userId,
1310     const sptr<NotificationDoNotDisturbProfile>& info)
1311 {
1312     if (preferncesDB_ == nullptr) {
1313         return false;
1314     }
1315     return preferncesDB_->DelCloneProfileInfo(userId, info);
1316 }
1317 
UpdateBatchCloneProfileInfo(const int32_t & userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> & profileInfo)1318 bool NotificationPreferences::UpdateBatchCloneProfileInfo(const int32_t &userId,
1319     const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo)
1320 {
1321     if (preferncesDB_ == nullptr) {
1322         return false;
1323     }
1324     return preferncesDB_->UpdateBatchCloneProfileInfo(userId, profileInfo);
1325 }
1326 
GetAllCloneProfileInfo(const int32_t & userId,std::vector<sptr<NotificationDoNotDisturbProfile>> & profilesInfo)1327 void NotificationPreferences::GetAllCloneProfileInfo(const int32_t &userId,
1328     std::vector<sptr<NotificationDoNotDisturbProfile>>& profilesInfo)
1329 {
1330     if (preferncesDB_ == nullptr) {
1331         return;
1332     }
1333     return preferncesDB_->GetAllCloneProfileInfo(userId, profilesInfo);
1334 }
1335 
GetAllCloneBundleInfo(const int32_t & userId,std::vector<NotificationCloneBundleInfo> & cloneBundleInfo)1336 void NotificationPreferences::GetAllCloneBundleInfo(const int32_t &userId,
1337     std::vector<NotificationCloneBundleInfo>& cloneBundleInfo)
1338 {
1339     if (preferncesDB_ == nullptr) {
1340         return;
1341     }
1342     return preferncesDB_->GetAllCloneBundleInfo(userId, cloneBundleInfo);
1343 }
1344 
UpdateBatchCloneBundleInfo(const int32_t & userId,const std::vector<NotificationCloneBundleInfo> & cloneBundleInfo)1345 bool NotificationPreferences::UpdateBatchCloneBundleInfo(const int32_t &userId,
1346     const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo)
1347 {
1348     if (preferncesDB_ == nullptr) {
1349         return false;
1350     }
1351     return preferncesDB_->UpdateBatchCloneBundleInfo(userId, cloneBundleInfo);
1352 }
1353 
DelCloneBundleInfo(const int32_t & userId,const NotificationCloneBundleInfo & cloneBundleInfo)1354 bool NotificationPreferences::DelCloneBundleInfo(const int32_t &userId,
1355     const NotificationCloneBundleInfo& cloneBundleInfo)
1356 {
1357     if (preferncesDB_ == nullptr) {
1358         return false;
1359     }
1360     return preferncesDB_->DelCloneBundleInfo(userId, cloneBundleInfo);
1361 }
1362 
DelBatchCloneProfileInfo(const int32_t & userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> & profileInfo)1363 bool NotificationPreferences::DelBatchCloneProfileInfo(const int32_t &userId,
1364     const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo)
1365 {
1366     if (preferncesDB_ == nullptr) {
1367         return false;
1368     }
1369     return preferncesDB_->DelBatchCloneProfileInfo(userId, profileInfo);
1370 }
1371 
DelBatchCloneBundleInfo(const int32_t & userId,const std::vector<NotificationCloneBundleInfo> & cloneBundleInfo)1372 bool NotificationPreferences::DelBatchCloneBundleInfo(const int32_t &userId,
1373     const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo)
1374 {
1375     if (preferncesDB_ == nullptr) {
1376         return false;
1377     }
1378     return preferncesDB_->DelBatchCloneBundleInfo(userId, cloneBundleInfo);
1379 }
1380 
SetDisableNotificationInfo(const sptr<NotificationDisable> & notificationDisable)1381 ErrCode NotificationPreferences::SetDisableNotificationInfo(const sptr<NotificationDisable> &notificationDisable)
1382 {
1383     ANS_LOGD("called");
1384     std::lock_guard<std::mutex> lock(preferenceMutex_);
1385     preferencesInfo_.SetDisableNotificationInfo(notificationDisable);
1386     if (preferncesDB_ == nullptr) {
1387         ANS_LOGE("the prefernces db is nullptr");
1388         return ERR_ANS_SERVICE_NOT_READY;
1389     }
1390     if (!preferncesDB_->SetDisableNotificationInfo(notificationDisable)) {
1391         ANS_LOGE("db set disable notification info fail");
1392         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1393     }
1394 
1395     return ERR_OK;
1396 }
1397 
GetDisableNotificationInfo(NotificationDisable & notificationDisable)1398 bool NotificationPreferences::GetDisableNotificationInfo(NotificationDisable &notificationDisable)
1399 {
1400     std::lock_guard<std::mutex> lock(preferenceMutex_);
1401     if (preferencesInfo_.GetDisableNotificationInfo(notificationDisable)) {
1402         ANS_LOGD("info get disable notification success");
1403         return true;
1404     }
1405     if (preferncesDB_ == nullptr) {
1406         ANS_LOGE("the prefernces db is nullptr");
1407         return false;
1408     }
1409     if (preferncesDB_->GetDisableNotificationInfo(notificationDisable)) {
1410         ANS_LOGD("db get disable notification success");
1411         sptr<NotificationDisable> notificationDisablePtr = new (std::nothrow) NotificationDisable(notificationDisable);
1412         preferencesInfo_.SetDisableNotificationInfo(notificationDisablePtr);
1413     } else {
1414         ANS_LOGD("db get disable notification fail");
1415         return false;
1416     }
1417     return true;
1418 }
1419 
SetSubscriberExistFlag(const std::string & deviceType,bool existFlag)1420 ErrCode NotificationPreferences::SetSubscriberExistFlag(const std::string& deviceType, bool existFlag)
1421 {
1422     ANS_LOGD("%{public}s", __FUNCTION__);
1423     if (deviceType.empty()) {
1424         return ERR_ANS_INVALID_PARAM;
1425     }
1426 
1427     if (preferncesDB_ == nullptr) {
1428         return ERR_ANS_SERVICE_NOT_READY;
1429     }
1430 
1431     std::lock_guard<std::mutex> lock(preferenceMutex_);
1432     bool storeDBResult = preferncesDB_->SetSubscriberExistFlag(deviceType, existFlag);
1433     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1434 }
1435 
GetSubscriberExistFlag(const std::string & deviceType,bool & existFlag)1436 ErrCode NotificationPreferences::GetSubscriberExistFlag(const std::string& deviceType, bool& existFlag)
1437 {
1438     ANS_LOGD("%{public}s", __FUNCTION__);
1439     if (deviceType.empty()) {
1440         return ERR_ANS_INVALID_PARAM;
1441     }
1442 
1443     if (preferncesDB_ == nullptr) {
1444         return ERR_ANS_SERVICE_NOT_READY;
1445     }
1446 
1447     std::lock_guard<std::mutex> lock(preferenceMutex_);
1448     bool storeDBResult = preferncesDB_->GetSubscriberExistFlag(deviceType, existFlag);
1449     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1450 }
1451 
SetDistributedEnabledForBundle(const NotificationPreferencesInfo::BundleInfo & bundleInfo)1452 void NotificationPreferences::SetDistributedEnabledForBundle(const NotificationPreferencesInfo::BundleInfo& bundleInfo)
1453 {
1454     ANS_LOGD("%{public}s", __FUNCTION__);
1455     if (!isCachedMirrorNotificationEnabledStatus_) {
1456         if (!DelayedSingleton<NotificationConfigParse>::GetInstance()->GetMirrorNotificationEnabledStatus(
1457             mirrorNotificationEnabledStatus_)) {
1458             ANS_LOGE("GetMirrorNotificationEnabledStatus failed from json");
1459             return;
1460         }
1461         isCachedMirrorNotificationEnabledStatus_ = true;
1462     }
1463     if (mirrorNotificationEnabledStatus_.empty()) {
1464         ANS_LOGD("mirrorNotificationEnabledStatus_ is empty");
1465         return;
1466     }
1467     if (preferncesDB_ == nullptr) {
1468         ANS_LOGD("preferncesDB_ is nullptr");
1469         return;
1470     }
1471     for (const auto& deviceType : mirrorNotificationEnabledStatus_) {
1472         bool ret = preferncesDB_->IsDistributedEnabledEmptyForBundle(deviceType, bundleInfo);
1473         if (!ret) {
1474             ANS_LOGD("get %{public}s distributedEnabled is empty", deviceType.c_str());
1475             preferncesDB_->PutDistributedEnabledForBundle(deviceType, bundleInfo, true);
1476         }
1477     }
1478 }
1479 
SetHashCodeRule(const int32_t uid,const uint32_t type)1480 ErrCode NotificationPreferences::SetHashCodeRule(const int32_t uid, const uint32_t type)
1481 {
1482     ANS_LOGD("%{public}s", __FUNCTION__);
1483 
1484     std::lock_guard<std::mutex> lock(preferenceMutex_);
1485     bool storeDBResult = true;
1486     storeDBResult = preferncesDB_->SetHashCodeRule(uid, type);
1487     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1488 }
1489 
GetHashCodeRule(const int32_t uid)1490 uint32_t NotificationPreferences::GetHashCodeRule(const int32_t uid)
1491 {
1492     ANS_LOGD("%{public}s", __FUNCTION__);
1493     std::lock_guard<std::mutex> lock(preferenceMutex_);
1494     uint32_t result = 0;
1495     result = preferncesDB_->GetHashCodeRule(uid);
1496     ANS_LOGI("GetHashCodeRule uid = %{public}d result = %{public}d", uid, result);
1497     return result;
1498 }
1499 
GetBundleRemoveFlag(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,int32_t sourceType)1500 bool NotificationPreferences::GetBundleRemoveFlag(const sptr<NotificationBundleOption> &bundleOption,
1501     const NotificationConstant::SlotType &slotType, int32_t sourceType)
1502 {
1503     if (preferncesDB_ == nullptr) {
1504         return true;
1505     }
1506     return preferncesDB_->GetBundleRemoveFlag(bundleOption, slotType, sourceType);
1507 }
1508 
SetBundleRemoveFlag(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,int32_t sourceType)1509 bool NotificationPreferences::SetBundleRemoveFlag(const sptr<NotificationBundleOption> &bundleOption,
1510     const NotificationConstant::SlotType &slotType, int32_t sourceType)
1511 {
1512     if (preferncesDB_ == nullptr) {
1513         return false;
1514     }
1515     return preferncesDB_->SetBundleRemoveFlag(bundleOption, slotType, sourceType);
1516 }
1517 }  // namespace Notification
1518 }  // namespace OHOS
1519