• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "ability_manager_client.h"
23 #include "access_token_helper.h"
24 #include "ans_const_define.h"
25 #include "ans_inner_errors.h"
26 #include "ans_log_wrapper.h"
27 #include "ans_trace_wrapper.h"
28 #include "ans_permission_def.h"
29 #include "bundle_manager_helper.h"
30 #include "in_process_call_wrapper.h"
31 #include "nlohmann/json.hpp"
32 #include "os_account_manager_helper.h"
33 #include "notification_analytics_util.h"
34 #include "notification_config_parse.h"
35 
36 namespace OHOS {
37 namespace Notification {
38 namespace {
39 const static std::string KEY_BUNDLE_LABEL = "label_ans_bundle_";
40 }
41 ffrt::mutex NotificationPreferences::instanceMutex_;
42 std::shared_ptr<NotificationPreferences> NotificationPreferences::instance_;
43 
NotificationPreferences()44 NotificationPreferences::NotificationPreferences()
45 {
46     preferncesDB_ = std::make_unique<NotificationPreferencesDatabase>();
47     if (preferncesDB_ == nullptr) {
48         HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_1)
49            .Message("preferncesDB is null.");
50         NotificationAnalyticsUtil::ReportModifyEvent(message);
51     }
52     InitSettingFromDisturbDB();
53 }
54 
GetInstance()55 std::shared_ptr<NotificationPreferences> NotificationPreferences::GetInstance()
56 {
57     if (instance_ == nullptr) {
58         std::lock_guard<ffrt::mutex> lock(instanceMutex_);
59         if (instance_ == nullptr) {
60             auto instance = std::make_shared<NotificationPreferences>();
61             instance_ = instance;
62         }
63     }
64     return instance_;
65 }
66 
AddNotificationSlots(const sptr<NotificationBundleOption> & bundleOption,const std::vector<sptr<NotificationSlot>> & slots)67 ErrCode NotificationPreferences::AddNotificationSlots(
68     const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
69 {
70     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
71     ANS_LOGD("%{public}s", __FUNCTION__);
72     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_6)
73         .BundleName(bundleOption == nullptr ? "" : bundleOption->GetBundleName());
74     if (bundleOption == nullptr || bundleOption->GetBundleName().empty() || slots.empty()) {
75         message.Message("Invalid param.");
76         NotificationAnalyticsUtil::ReportModifyEvent(message);
77         return ERR_ANS_INVALID_PARAM;
78     }
79     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
80     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
81     ErrCode result = ERR_OK;
82     for (auto slot : slots) {
83         result = CheckSlotForCreateSlot(bundleOption, slot, preferencesInfo);
84         if (result != ERR_OK) {
85             return result;
86         }
87     }
88 
89     ANS_LOGD("ffrt: add slot to db!");
90     if (result == ERR_OK &&
91         (!preferncesDB_->PutSlotsToDisturbeDB(bundleOption->GetBundleName(), bundleOption->GetUid(), slots))) {
92         message.Message("put slot for to db failed.");
93         NotificationAnalyticsUtil::ReportModifyEvent(message);
94         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
95     }
96 
97     if (result == ERR_OK) {
98         preferencesInfo_ = preferencesInfo;
99     }
100     return result;
101 }
102 
AddNotificationBundleProperty(const sptr<NotificationBundleOption> & bundleOption)103 ErrCode NotificationPreferences::AddNotificationBundleProperty(const sptr<NotificationBundleOption> &bundleOption)
104 {
105     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
106         return ERR_ANS_INVALID_PARAM;
107     }
108     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
109     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
110     NotificationPreferencesInfo::BundleInfo bundleInfo;
111     preferencesInfo.SetBundleInfo(bundleInfo);
112     ErrCode result = ERR_OK;
113     if (preferncesDB_->PutBundlePropertyToDisturbeDB(bundleInfo)) {
114         preferencesInfo_ = preferencesInfo;
115     } else {
116         result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
117     }
118     ANS_LOGD("AddNotificationBundleProperty.result: %{public}d", result);
119     return result;
120 }
121 
RemoveNotificationSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType)122 ErrCode NotificationPreferences::RemoveNotificationSlot(
123     const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType)
124 {
125     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
126     ANS_LOGD("%{public}s", __FUNCTION__);
127     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
128         return ERR_ANS_INVALID_PARAM;
129     }
130     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_5);
131     message.Message(bundleOption->GetBundleName() + "_" +std::to_string(bundleOption->GetUid()) +
132         " slotType: " + std::to_string(static_cast<uint32_t>(slotType)));
133     message.SlotType(static_cast<uint32_t>(slotType));
134     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
135     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
136     ErrCode result = ERR_OK;
137     result = CheckSlotForRemoveSlot(bundleOption, slotType, preferencesInfo);
138     if (result == ERR_OK &&
139         (!preferncesDB_->RemoveSlotFromDisturbeDB(GenerateBundleKey(bundleOption), slotType, bundleOption->GetUid()))) {
140         message.ErrorCode(result).Append(" Remove slot failed.");
141         NotificationAnalyticsUtil::ReportModifyEvent(message);
142         ANS_LOGE("%{public}s_%{public}d, remove slot failed.",
143             bundleOption->GetBundleName().c_str(), bundleOption->GetUid());
144         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
145     }
146 
147     if (result == ERR_OK) {
148         preferencesInfo_ = preferencesInfo;
149     }
150     ANS_LOGI("%{public}s_%{public}d, Remove slot successful.",
151         bundleOption->GetBundleName().c_str(), bundleOption->GetUid());
152     return result;
153 }
154 
RemoveNotificationAllSlots(const sptr<NotificationBundleOption> & bundleOption)155 ErrCode NotificationPreferences::RemoveNotificationAllSlots(const sptr<NotificationBundleOption> &bundleOption)
156 {
157     ANS_LOGD("%{public}s", __FUNCTION__);
158     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
159         return ERR_ANS_INVALID_PARAM;
160     }
161     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_3);
162     message.Message(bundleOption->GetBundleName() + "_" +std::to_string(bundleOption->GetUid()));
163     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
164     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
165     ErrCode result = ERR_OK;
166     NotificationPreferencesInfo::BundleInfo bundleInfo;
167     if (GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
168         bundleInfo.RemoveAllSlots();
169         preferencesInfo.SetBundleInfo(bundleInfo);
170         if (!preferncesDB_->RemoveAllSlotsFromDisturbeDB(GenerateBundleKey(bundleOption), bundleOption->GetUid())) {
171             result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
172             message.ErrorCode(result).Append(" Db operation failed.");
173             ANS_LOGE("%{public}s_%{public}d, Db operation failed.",
174                 bundleOption->GetBundleName().c_str(), bundleOption->GetUid());
175         }
176     } else {
177         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
178         message.ErrorCode(result).Append(" Notification bundle not exist.");
179         ANS_LOGE("%{public}s_%{public}d, Notification bundle not exist.",
180             bundleOption->GetBundleName().c_str(), bundleOption->GetUid());
181     }
182 
183     if (result == ERR_OK) {
184         preferencesInfo_ = preferencesInfo;
185         message.ErrorCode(result).Append(" Remove all slot successful.");
186         ANS_LOGI("%{public}s_%{public}d, Remove successful.",
187             bundleOption->GetBundleName().c_str(), bundleOption->GetUid());
188     }
189     NotificationAnalyticsUtil::ReportModifyEvent(message);
190     return result;
191 }
192 
RemoveNotificationForBundle(const sptr<NotificationBundleOption> & bundleOption)193 ErrCode NotificationPreferences::RemoveNotificationForBundle(const sptr<NotificationBundleOption> &bundleOption)
194 {
195     ANS_LOGD("%{public}s", __FUNCTION__);
196     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
197         return ERR_ANS_INVALID_PARAM;
198     }
199     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
200     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
201 
202     ErrCode result = ERR_OK;
203     NotificationPreferencesInfo::BundleInfo bundleInfo;
204     if (GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
205         preferencesInfo.RemoveBundleInfo(bundleOption);
206         if (!preferncesDB_->RemoveBundleFromDisturbeDB(GenerateBundleKey(bundleOption), bundleOption->GetUid())) {
207             result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
208         }
209     } else {
210         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
211     }
212 
213     if (result == ERR_OK) {
214         preferencesInfo_ = preferencesInfo;
215     }
216 
217     return result;
218 }
219 
UpdateNotificationSlots(const sptr<NotificationBundleOption> & bundleOption,const std::vector<sptr<NotificationSlot>> & slots)220 ErrCode NotificationPreferences::UpdateNotificationSlots(
221     const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
222 {
223     ANS_LOGD("%{public}s", __FUNCTION__);
224     if (bundleOption == nullptr || bundleOption->GetBundleName().empty() || slots.empty()) {
225         return ERR_ANS_INVALID_PARAM;
226     }
227     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_2)
228         .BundleName(bundleOption->GetBundleName());
229     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
230     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
231     ErrCode result = ERR_OK;
232     for (auto slotIter : slots) {
233         result = CheckSlotForUpdateSlot(bundleOption, slotIter, preferencesInfo);
234         if (result != ERR_OK) {
235             message.Message("Check slot for update failed." + std::to_string(result));
236             NotificationAnalyticsUtil::ReportModifyEvent(message);
237             return result;
238         }
239     }
240 
241     if ((result == ERR_OK) &&
242         (!preferncesDB_->PutSlotsToDisturbeDB(bundleOption->GetBundleName(), bundleOption->GetUid(), slots))) {
243         message.Message("Update put slot for to db failed.");
244         NotificationAnalyticsUtil::ReportModifyEvent(message);
245         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
246     }
247 
248     if (result == ERR_OK) {
249         preferencesInfo_ = preferencesInfo;
250     }
251 
252     return result;
253 }
254 
GetNotificationSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & type,sptr<NotificationSlot> & slot)255 ErrCode NotificationPreferences::GetNotificationSlot(const sptr<NotificationBundleOption> &bundleOption,
256     const NotificationConstant::SlotType &type, sptr<NotificationSlot> &slot)
257 {
258     ANS_LOGD("%{public}s", __FUNCTION__);
259     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
260         return ERR_ANS_INVALID_PARAM;
261     }
262 
263     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_7);
264     ErrCode result = ERR_OK;
265     NotificationPreferencesInfo::BundleInfo bundleInfo;
266     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
267     if (GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
268         if (!bundleInfo.GetSlot(type, slot)) {
269             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
270             message.ErrorCode(ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST).Message("Slot type not exist.");
271             NotificationAnalyticsUtil::ReportModifyEvent(message);
272             ANS_LOGE("Slot type not exist.");
273         }
274     } else {
275         ANS_LOGW("bundle not exist");
276         result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
277         message.ErrorCode(ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST).Message("Slot type not exist.");
278         NotificationAnalyticsUtil::ReportModifyEvent(message);
279     }
280     ANS_LOGD("%{public}s status  = %{public}d ", __FUNCTION__, result);
281     return result;
282 }
283 
GetNotificationAllSlots(const sptr<NotificationBundleOption> & bundleOption,std::vector<sptr<NotificationSlot>> & slots)284 ErrCode NotificationPreferences::GetNotificationAllSlots(
285     const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlot>> &slots)
286 {
287     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
288         return ERR_ANS_INVALID_PARAM;
289     }
290 
291     ErrCode result = ERR_OK;
292     NotificationPreferencesInfo::BundleInfo bundleInfo;
293     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
294     if (GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
295         bundleInfo.GetAllSlots(slots);
296     } else {
297         ANS_LOGW("Notification bundle does not exsit.");
298         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
299     }
300 
301     return result;
302 }
303 
GetNotificationSlotsNumForBundle(const sptr<NotificationBundleOption> & bundleOption,uint64_t & num)304 ErrCode NotificationPreferences::GetNotificationSlotsNumForBundle(
305     const sptr<NotificationBundleOption> &bundleOption, uint64_t &num)
306 {
307     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
308         return ERR_ANS_INVALID_PARAM;
309     }
310 
311     ErrCode result = ERR_OK;
312     NotificationPreferencesInfo::BundleInfo bundleInfo;
313     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
314     if (GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
315         num = static_cast<uint64_t>(bundleInfo.GetAllSlotsSize());
316     } else {
317         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
318     }
319     return result;
320 }
321 
GetNotificationSlotFlagsForBundle(const sptr<NotificationBundleOption> & bundleOption,uint32_t & slotFlags)322 ErrCode NotificationPreferences::GetNotificationSlotFlagsForBundle(
323     const sptr<NotificationBundleOption> &bundleOption, uint32_t &slotFlags)
324 {
325     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
326         return ERR_ANS_INVALID_PARAM;
327     }
328 
329     return GetBundleProperty(bundleOption, BundleType::BUNDLE_SLOTFLGS_TYPE, slotFlags);
330 }
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<ffrt::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<ffrt::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<ffrt::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<ffrt::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,NotificationConstant::SWITCH_STATE & state)420 ErrCode NotificationPreferences::GetNotificationsEnabledForBundle(
421     const sptr<NotificationBundleOption> &bundleOption, NotificationConstant::SWITCH_STATE &state)
422 {
423     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
424         return ERR_ANS_INVALID_PARAM;
425     }
426     int32_t val = static_cast<int32_t>(NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF);
427     ErrCode result = GetBundleProperty(bundleOption, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, val);
428     if (result != ERR_OK) {
429         return result;
430     }
431     state = static_cast<NotificationConstant::SWITCH_STATE>(val);
432     return ERR_OK;
433 }
434 
SetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SWITCH_STATE state)435 ErrCode NotificationPreferences::SetNotificationsEnabledForBundle(
436     const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SWITCH_STATE state)
437 {
438     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
439     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
440         return ERR_ANS_INVALID_PARAM;
441     }
442 
443     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
444     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
445     ErrCode result = SetBundleProperty(preferencesInfo, bundleOption,
446         BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, static_cast<int32_t>(state));
447     if (result == ERR_OK) {
448         preferencesInfo_ = preferencesInfo;
449     }
450     return result;
451 }
452 
GetNotificationsEnabled(const int32_t & userId,bool & enabled)453 ErrCode NotificationPreferences::GetNotificationsEnabled(const int32_t &userId, bool &enabled)
454 {
455     if (userId <= SUBSCRIBE_USER_INIT) {
456         return ERR_ANS_INVALID_PARAM;
457     }
458 
459     ErrCode result = ERR_OK;
460     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
461     if (!preferencesInfo_.GetEnabledAllNotification(userId, enabled)) {
462         result = ERR_ANS_INVALID_PARAM;
463     }
464     return result;
465 }
466 
SetNotificationsEnabled(const int32_t & userId,const bool & enabled)467 ErrCode NotificationPreferences::SetNotificationsEnabled(const int32_t &userId, const bool &enabled)
468 {
469     if (userId <= SUBSCRIBE_USER_INIT) {
470         return ERR_ANS_INVALID_PARAM;
471     }
472     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
473     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
474     preferencesInfo.SetEnabledAllNotification(userId, enabled);
475     ErrCode result = ERR_OK;
476     if (!preferncesDB_->PutNotificationsEnabled(userId, enabled)) {
477         result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
478     }
479 
480     if (result == ERR_OK) {
481         preferencesInfo_ = preferencesInfo;
482     }
483     return result;
484 }
485 
GetHasPoppedDialog(const sptr<NotificationBundleOption> & bundleOption,bool & hasPopped)486 ErrCode NotificationPreferences::GetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool &hasPopped)
487 {
488     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
489         return ERR_ANS_INVALID_PARAM;
490     }
491     return GetBundleProperty(bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
492 }
493 
SetHasPoppedDialog(const sptr<NotificationBundleOption> & bundleOption,bool hasPopped)494 ErrCode NotificationPreferences::SetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool hasPopped)
495 {
496     if (bundleOption == nullptr) {
497         return ERR_ANS_INVALID_PARAM;
498     }
499     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
500     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
501     ErrCode result = ERR_OK;
502     result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
503     if (result == ERR_OK) {
504         preferencesInfo_ = preferencesInfo;
505     }
506     return result;
507 }
508 
GetDoNotDisturbDate(const int32_t & userId,sptr<NotificationDoNotDisturbDate> & date)509 ErrCode NotificationPreferences::GetDoNotDisturbDate(const int32_t &userId,
510     sptr<NotificationDoNotDisturbDate> &date)
511 {
512     if (userId <= SUBSCRIBE_USER_INIT) {
513         return ERR_ANS_INVALID_PARAM;
514     }
515 
516     ErrCode result = ERR_OK;
517     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
518     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
519     if (!preferencesInfo.GetDoNotDisturbDate(userId, date)) {
520         result = ERR_ANS_INVALID_PARAM;
521     }
522     return result;
523 }
524 
SetDoNotDisturbDate(const int32_t & userId,const sptr<NotificationDoNotDisturbDate> date)525 ErrCode NotificationPreferences::SetDoNotDisturbDate(const int32_t &userId,
526     const sptr<NotificationDoNotDisturbDate> date)
527 {
528     ANS_LOGE("called");
529     if (userId <= SUBSCRIBE_USER_INIT) {
530         return ERR_ANS_INVALID_PARAM;
531     }
532     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
533     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
534     preferencesInfo.SetDoNotDisturbDate(userId, date);
535 
536     ErrCode result = ERR_OK;
537     if (!preferncesDB_->PutDoNotDisturbDate(userId, date)) {
538         result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
539     }
540 
541     if (result == ERR_OK) {
542         preferencesInfo_ = preferencesInfo;
543     }
544     return result;
545 }
546 
AddDoNotDisturbProfiles(int32_t userId,std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)547 ErrCode NotificationPreferences::AddDoNotDisturbProfiles(
548     int32_t userId, std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)
549 {
550     ANS_LOGD("called");
551     for (auto profile : profiles) {
552         if (profile == nullptr) {
553             ANS_LOGE("The profile is nullptr.");
554             return ERR_ANS_INVALID_PARAM;
555         }
556         auto trustList = profile->GetProfileTrustList();
557         for (auto& bundleInfo : trustList) {
558             int32_t index = BundleManagerHelper::GetInstance()->GetAppIndexByUid(bundleInfo.GetUid());
559             bundleInfo.SetAppIndex(index);
560             ANS_LOGI("Get app index by uid %{public}d %{public}s %{public}d", bundleInfo.GetUid(),
561                 bundleInfo.GetBundleName().c_str(), index);
562         }
563         profile->SetProfileTrustList(trustList);
564     }
565     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
566     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
567     preferencesInfo.AddDoNotDisturbProfiles(userId, profiles);
568     if (preferncesDB_ == nullptr) {
569         ANS_LOGE("The prefernces db is nullptr.");
570         return ERR_ANS_SERVICE_NOT_READY;
571     }
572     if (!preferncesDB_->AddDoNotDisturbProfiles(userId, profiles)) {
573         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
574     }
575     preferencesInfo_ = preferencesInfo;
576     return ERR_OK;
577 }
578 
IsNotificationSlotFlagsExists(const sptr<NotificationBundleOption> & bundleOption)579 bool NotificationPreferences::IsNotificationSlotFlagsExists(
580     const sptr<NotificationBundleOption> &bundleOption)
581 {
582     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
583         return false;
584     }
585     return preferncesDB_->IsNotificationSlotFlagsExists(bundleOption);
586 }
587 
GetBundleInfo(NotificationPreferencesInfo & preferencesInfo,const sptr<NotificationBundleOption> & bundleOption,NotificationPreferencesInfo::BundleInfo & info) const588 bool NotificationPreferences::GetBundleInfo(NotificationPreferencesInfo &preferencesInfo,
589     const sptr<NotificationBundleOption> &bundleOption, NotificationPreferencesInfo::BundleInfo &info) const
590 {
591     if (preferencesInfo.GetBundleInfo(bundleOption, info)) {
592         return true;
593     } else if (preferncesDB_->GetBundleInfo(bundleOption, info)) {
594         preferencesInfo.SetBundleInfo(info);
595         return true;
596     }
597     return false;
598 }
599 
RemoveDoNotDisturbProfiles(int32_t userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)600 ErrCode NotificationPreferences::RemoveDoNotDisturbProfiles(
601     int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)
602 {
603     ANS_LOGD("called");
604     for (auto profile : profiles) {
605         if (profile == nullptr) {
606             ANS_LOGE("The profile is nullptr.");
607             return ERR_ANS_INVALID_PARAM;
608         }
609     }
610     std::lock_guard<ffrt::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,int64_t profileId,const std::string & name,const std::vector<NotificationBundleOption> & bundleList)641 ErrCode NotificationPreferences::UpdateDoNotDisturbProfiles(int32_t userId, int64_t profileId,
642     const std::string& name, const std::vector<NotificationBundleOption>& bundleList)
643 {
644     ANS_LOGD("called, update Profile %{public}d %{public}s %{public}zu",
645         userId, std::to_string(profileId).c_str(), bundleList.size());
646     if (bundleList.empty()) {
647         return ERR_ANS_INVALID_PARAM;
648     }
649 
650     sptr<NotificationDoNotDisturbProfile> profile = new (std::nothrow) NotificationDoNotDisturbProfile();
651     if (profile == nullptr) {
652         ANS_LOGE("profile is nullptr");
653         return ERR_ANS_INVALID_PARAM;
654     }
655     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
656     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
657     if (preferencesInfo.GetDoNotDisturbProfiles(profileId, userId, profile)) {
658         auto trustList = profile->GetProfileTrustList();
659         UpdateProfilesUtil(trustList, bundleList);
660         profile->SetProfileTrustList(trustList);
661     } else {
662         profile->SetProfileId(profileId);
663         profile->SetProfileName(name);
664         profile->SetProfileTrustList(bundleList);
665     }
666     ANS_LOGI("Update profile %{public}d %{public}s %{public}zu",
667         userId, std::to_string(profile->GetProfileId()).c_str(),
668         profile->GetProfileTrustList().size());
669     preferencesInfo.AddDoNotDisturbProfiles(userId, {profile});
670     if (preferncesDB_ == nullptr) {
671         ANS_LOGE("The prefernces db is nullptr.");
672         return ERR_ANS_SERVICE_NOT_READY;
673     }
674     if (!preferncesDB_->AddDoNotDisturbProfiles(userId, {profile})) {
675         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
676     }
677     preferencesInfo_ = preferencesInfo;
678     return ERR_OK;
679 }
680 
UpdateCloneBundleInfo(int32_t userId,const NotificationCloneBundleInfo & cloneBundleInfo)681 void NotificationPreferences::UpdateCloneBundleInfo(int32_t userId,
682     const NotificationCloneBundleInfo& cloneBundleInfo)
683 {
684     ANS_LOGI("Event bundle update %{public}s.", cloneBundleInfo.Dump().c_str());
685     NotificationPreferencesInfo::BundleInfo bundleInfo;
686     sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption();
687     if (bundleOption == nullptr) {
688         return;
689     }
690     bundleOption->SetBundleName(cloneBundleInfo.GetBundleName());
691     bundleOption->SetUid(cloneBundleInfo.GetUid());
692     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
693     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
694     if (!GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
695         bundleInfo.SetBundleName(cloneBundleInfo.GetBundleName());
696         bundleInfo.SetBundleUid(cloneBundleInfo.GetUid());
697     }
698 
699     /* after clone, override these witch */
700     bundleInfo.SetIsShowBadge(cloneBundleInfo.GetIsShowBadge());
701     bundleInfo.SetEnableNotification(cloneBundleInfo.GetEnableNotification());
702     /* update property to db */
703     if (!preferncesDB_->UpdateBundlePropertyToDisturbeDB(userId, bundleInfo)) {
704         ANS_LOGW("Clone bundle info failed %{public}s.", cloneBundleInfo.Dump().c_str());
705         return;
706     }
707 
708     if (SaveBundleProperty(bundleInfo, bundleOption,
709         BundleType::BUNDLE_SLOTFLGS_TYPE, cloneBundleInfo.GetSlotFlags()) != ERR_OK) {
710         ANS_LOGW("Clone bundle slot info %{public}s.", cloneBundleInfo.Dump().c_str());
711         return;
712     }
713     preferencesInfo.SetBundleInfo(bundleInfo);
714 
715     /* update slot info */
716     std::vector<sptr<NotificationSlot>> slots;
717     for (auto& cloneSlot : cloneBundleInfo.GetSlotInfo()) {
718         sptr<NotificationSlot> slotInfo = new (std::nothrow) NotificationSlot(cloneSlot.slotType_);
719         if (slotInfo == nullptr) {
720             return;
721         }
722         uint32_t slotFlags = bundleInfo.GetSlotFlags();
723         auto configSlotReminderMode = DelayedSingleton<NotificationConfigParse>::GetInstance()->
724             GetConfigSlotReminderModeByType(slotInfo->GetType());
725         slotInfo->SetReminderMode(configSlotReminderMode & slotFlags);
726         slotInfo->SetEnable(cloneSlot.enable_);
727         slotInfo->SetForceControl(cloneSlot.isForceControl_);
728         slotInfo->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED);
729         slots.push_back(slotInfo);
730         bundleInfo.SetSlot(slotInfo);
731     }
732 
733     if (!preferncesDB_->UpdateBundleSlotToDisturbeDB(userId, cloneBundleInfo.GetBundleName(),
734         cloneBundleInfo.GetUid(), slots)) {
735         ANS_LOGW("Clone bundle slot failed %{public}s.", cloneBundleInfo.Dump().c_str());
736         preferencesInfo_ = preferencesInfo;
737         return;
738     }
739     preferencesInfo.SetBundleInfo(bundleInfo);
740     preferencesInfo_ = preferencesInfo;
741 }
742 
GetAllCLoneBundlesInfo(int32_t userId,std::vector<NotificationCloneBundleInfo> & cloneBundles)743 void NotificationPreferences::GetAllCLoneBundlesInfo(int32_t userId,
744     std::vector<NotificationCloneBundleInfo> &cloneBundles)
745 {
746     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
747     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
748     std::unordered_map<std::string, std::string> bundlesMap;
749     if (GetBatchKvsFromDb(KEY_BUNDLE_LABEL, bundlesMap, userId) != ERR_OK) {
750         ANS_LOGE("Get bundle map info failed.");
751         return;
752     }
753     preferncesDB_->ParseBundleFromDistureDB(preferencesInfo, bundlesMap, userId);
754     preferencesInfo.GetAllCLoneBundlesInfo(userId, bundlesMap, cloneBundles);
755     preferencesInfo_ = preferencesInfo;
756 }
757 
GetDoNotDisturbProfileListByUserId(int32_t userId,std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)758 void NotificationPreferences::GetDoNotDisturbProfileListByUserId(int32_t userId,
759     std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
760 {
761     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
762     preferencesInfo_.GetAllDoNotDisturbProfiles(userId, profiles);
763 }
764 
GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> & bundleOption)765 ErrCode NotificationPreferences::GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> &bundleOption)
766 {
767     ANS_LOGD("called");
768     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
769     if (preferncesDB_ == nullptr) {
770         return ERR_ANS_SERVICE_NOT_READY;
771     }
772     if (!preferncesDB_->GetAllNotificationEnabledBundles(bundleOption)) {
773         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
774     }
775     return ERR_OK;
776 }
777 
GetAllLiveViewEnabledBundles(const int32_t userId,std::vector<NotificationBundleOption> & bundleOption)778 ErrCode NotificationPreferences::GetAllLiveViewEnabledBundles(const int32_t userId,
779     std::vector<NotificationBundleOption> &bundleOption)
780 {
781     ANS_LOGD("called");
782     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
783     return preferencesInfo_.GetAllLiveViewEnabledBundles(userId, bundleOption);
784 }
785 
GetAllDistribuedEnabledBundles(int32_t userId,const std::string & deviceType,std::vector<NotificationBundleOption> & bundleOption)786 ErrCode NotificationPreferences::GetAllDistribuedEnabledBundles(int32_t userId,
787     const std::string &deviceType, std::vector<NotificationBundleOption> &bundleOption)
788 {
789     ANS_LOGD("called");
790     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
791     if (preferncesDB_ == nullptr) {
792         return ERR_ANS_SERVICE_NOT_READY;
793     }
794     if (!preferncesDB_->GetAllDistribuedEnabledBundles(userId, deviceType, bundleOption)) {
795         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
796     }
797     return ERR_OK;
798 }
799 
ClearNotificationInRestoreFactorySettings()800 ErrCode NotificationPreferences::ClearNotificationInRestoreFactorySettings()
801 {
802     ErrCode result = ERR_OK;
803     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
804     if (!preferncesDB_->RemoveAllDataFromDisturbeDB()) {
805         result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
806     }
807 
808     if (result == ERR_OK) {
809         preferencesInfo_ = NotificationPreferencesInfo();
810     }
811     return result;
812 }
813 
GetDoNotDisturbProfile(int64_t profileId,int32_t userId,sptr<NotificationDoNotDisturbProfile> & profile)814 ErrCode NotificationPreferences::GetDoNotDisturbProfile(
815     int64_t profileId, int32_t userId, sptr<NotificationDoNotDisturbProfile> &profile)
816 {
817     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
818     if (!preferencesInfo_.GetDoNotDisturbProfiles(profileId, userId, profile)) {
819         return ERR_ANS_NO_PROFILE_TEMPLATE;
820     }
821     return ERR_OK;
822 }
823 
RemoveDoNotDisturbProfileTrustList(int32_t userId,const sptr<NotificationBundleOption> & bundleOption)824 void NotificationPreferences::RemoveDoNotDisturbProfileTrustList(
825     int32_t userId, const sptr<NotificationBundleOption> &bundleOption)
826 {
827     if (bundleOption == nullptr) {
828         ANS_LOGE("The bundle option is nullptr.");
829         return;
830     }
831     int32_t uid = bundleOption->GetUid();
832     int32_t appIndex = bundleOption->GetAppIndex();
833     auto bundleName = bundleOption->GetBundleName();
834     ANS_LOGI("Remove %{public}s %{public}d %{public}d.", bundleName.c_str(), uid, appIndex);
835     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
836     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
837 
838     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
839     preferencesInfo.GetAllDoNotDisturbProfiles(userId, profiles);
840     for (auto profile : profiles) {
841         if (profile == nullptr) {
842             ANS_LOGE("The profile is nullptr.");
843             continue;
844         }
845         auto trustList = profile->GetProfileTrustList();
846         for (auto it = trustList.begin(); it != trustList.end(); it++) {
847             if (it->GetUid() == uid) {
848                 trustList.erase(it);
849                 break;
850             }
851         }
852         profile->SetProfileTrustList(trustList);
853     }
854     preferencesInfo.AddDoNotDisturbProfiles(userId, profiles);
855     if (preferncesDB_ == nullptr) {
856         ANS_LOGE("The prefernces db is nullptr.");
857         return;
858     }
859     if (!preferncesDB_->AddDoNotDisturbProfiles(userId, profiles)) {
860         return;
861     }
862     preferencesInfo_ = preferencesInfo;
863 }
864 
CheckSlotForCreateSlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot,NotificationPreferencesInfo & preferencesInfo) const865 ErrCode NotificationPreferences::CheckSlotForCreateSlot(const sptr<NotificationBundleOption> &bundleOption,
866     const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
867 {
868     if (slot == nullptr) {
869         ANS_LOGE("Notification slot is nullptr.");
870         return ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_NOT_EXIST;
871     }
872 
873     NotificationPreferencesInfo::BundleInfo bundleInfo;
874     if (!GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
875         bundleInfo.SetBundleName(bundleOption->GetBundleName());
876         bundleInfo.SetBundleUid(bundleOption->GetUid());
877         NotificationConstant::SWITCH_STATE defaultState = CheckApiCompatibility(bundleOption) ?
878             NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON :
879             NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF;
880         bundleInfo.SetEnableNotification(defaultState);
881     }
882     bundleInfo.SetSlot(slot);
883     preferencesInfo.SetBundleInfo(bundleInfo);
884 
885     return ERR_OK;
886 }
887 
CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,NotificationPreferencesInfo & preferencesInfo) const888 ErrCode NotificationPreferences::CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> &bundleOption,
889     const NotificationConstant::SlotType &slotType, NotificationPreferencesInfo &preferencesInfo) const
890 {
891     ErrCode result = ERR_OK;
892     NotificationPreferencesInfo::BundleInfo bundleInfo;
893     if (GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
894         if (bundleInfo.IsExsitSlot(slotType)) {
895             bundleInfo.RemoveSlot(slotType);
896             preferencesInfo.SetBundleInfo(bundleInfo);
897         } else {
898             ANS_LOGE("Notification slot type does not exsited.");
899             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
900         }
901     } else {
902         ANS_LOGW("Notification bundle does not exsit.");
903         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
904     }
905     return result;
906 }
907 
CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot,NotificationPreferencesInfo & preferencesInfo) const908 ErrCode NotificationPreferences::CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> &bundleOption,
909     const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
910 {
911     if (slot == nullptr) {
912         ANS_LOGE("Notification slot is nullptr.");
913         return ERR_ANS_INVALID_PARAM;
914     }
915 
916     ErrCode result = ERR_OK;
917     NotificationPreferencesInfo::BundleInfo bundleInfo;
918     if (GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
919         if (bundleInfo.IsExsitSlot(slot->GetType())) {
920             bundleInfo.SetBundleName(bundleOption->GetBundleName());
921             bundleInfo.SetBundleUid(bundleOption->GetUid());
922             bundleInfo.SetSlot(slot);
923             preferencesInfo.SetBundleInfo(bundleInfo);
924         } else {
925             ANS_LOGE("Notification slot type does not exist.");
926             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
927         }
928     } else {
929         ANS_LOGW("Notification bundle does not exsit.");
930         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
931     }
932 
933     return result;
934 }
935 
936 template <typename T>
SetBundleProperty(NotificationPreferencesInfo & preferencesInfo,const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,const T & value)937 ErrCode NotificationPreferences::SetBundleProperty(NotificationPreferencesInfo &preferencesInfo,
938     const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
939 {
940     ErrCode result = ERR_OK;
941     NotificationPreferencesInfo::BundleInfo bundleInfo;
942     if (!GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
943         bundleInfo.SetBundleName(bundleOption->GetBundleName());
944         bundleInfo.SetBundleUid(bundleOption->GetUid());
945         NotificationConstant::SWITCH_STATE defaultState = CheckApiCompatibility(bundleOption) ?
946             NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON :
947             NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF;
948         bundleInfo.SetEnableNotification(defaultState);
949     }
950     result = SaveBundleProperty(bundleInfo, bundleOption, type, value);
951     if (result == ERR_OK) {
952         preferencesInfo.SetBundleInfo(bundleInfo);
953     }
954 
955     return result;
956 }
957 
958 template <typename T>
SaveBundleProperty(NotificationPreferencesInfo::BundleInfo & bundleInfo,const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,const T & value)959 ErrCode NotificationPreferences::SaveBundleProperty(NotificationPreferencesInfo::BundleInfo &bundleInfo,
960     const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
961 {
962     bool storeDBResult = true;
963     NotificationConstant::SWITCH_STATE state = NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF;
964     switch (type) {
965         case BundleType::BUNDLE_IMPORTANCE_TYPE:
966             bundleInfo.SetImportance(value);
967             storeDBResult = preferncesDB_->PutImportance(bundleInfo, value);
968             break;
969         case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
970             bundleInfo.SetBadgeTotalNum(value);
971             storeDBResult = preferncesDB_->PutTotalBadgeNums(bundleInfo, value);
972             break;
973         case BundleType::BUNDLE_SHOW_BADGE_TYPE:
974             bundleInfo.SetIsShowBadge(value);
975             storeDBResult = preferncesDB_->PutShowBadge(bundleInfo, value);
976             break;
977         case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
978             state = static_cast<NotificationConstant::SWITCH_STATE>(value);
979             bundleInfo.SetEnableNotification(state);
980             storeDBResult = preferncesDB_->PutNotificationsEnabledForBundle(bundleInfo, state);
981             if (storeDBResult) {
982                 if (state == NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON ||
983                     state == NotificationConstant::SWITCH_STATE::USER_MODIFIED_ON) {
984                     SetDistributedEnabledForBundle(bundleInfo);
985                 }
986             }
987             break;
988         case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
989             ANS_LOGI("Into BUNDLE_POPPED_DIALOG_TYPE:SetHasPoppedDialog.");
990             bundleInfo.SetHasPoppedDialog(value);
991             storeDBResult = preferncesDB_->PutHasPoppedDialog(bundleInfo, value);
992             break;
993         case BundleType::BUNDLE_SLOTFLGS_TYPE:
994             ANS_LOGI("Into BUNDLE_SLOTFLGS_TYPE:SetSlotFlags.");
995             bundleInfo.SetSlotFlags(value);
996             storeDBResult = preferncesDB_->PutSlotFlags(bundleInfo, value);
997             break;
998         default:
999             break;
1000     }
1001     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1002 }
1003 
1004 template <typename T>
GetBundleProperty(const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,T & value)1005 ErrCode NotificationPreferences::GetBundleProperty(
1006     const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, T &value)
1007 {
1008     ErrCode result = ERR_OK;
1009     NotificationPreferencesInfo::BundleInfo bundleInfo;
1010     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1011     if (GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
1012         switch (type) {
1013             case BundleType::BUNDLE_IMPORTANCE_TYPE:
1014                 value = bundleInfo.GetImportance();
1015                 break;
1016             case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
1017                 value = bundleInfo.GetBadgeTotalNum();
1018                 break;
1019             case BundleType::BUNDLE_SHOW_BADGE_TYPE:
1020                 value = bundleInfo.GetIsShowBadge();
1021                 break;
1022             case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
1023                 value = static_cast<int32_t>(bundleInfo.GetEnableNotification());
1024                 break;
1025             case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
1026                 ANS_LOGD("Into BUNDLE_POPPED_DIALOG_TYPE:GetHasPoppedDialog.");
1027                 value = bundleInfo.GetHasPoppedDialog();
1028                 break;
1029             case BundleType::BUNDLE_SLOTFLGS_TYPE:
1030                 value = bundleInfo.GetSlotFlags();
1031                 ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:GetSlotFlags.");
1032                 break;
1033             default:
1034                 result = ERR_ANS_INVALID_PARAM;
1035                 break;
1036         }
1037     } else {
1038         ANS_LOGW("Notification bundle does not exsit.");
1039         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
1040     }
1041     return result;
1042 }
1043 
GenerateBundleKey(const sptr<NotificationBundleOption> & bundleOption) const1044 std::string NotificationPreferences::GenerateBundleKey(const sptr<NotificationBundleOption> &bundleOption) const
1045 {
1046     return bundleOption->GetBundleName().append(std::to_string(bundleOption->GetUid()));
1047 }
1048 
GetTemplateSupported(const std::string & templateName,bool & support)1049 ErrCode NotificationPreferences::GetTemplateSupported(const std::string& templateName, bool &support)
1050 {
1051     if (templateName.length() == 0) {
1052         ANS_LOGE("template name is null.");
1053         return ERR_ANS_INVALID_PARAM;
1054     }
1055 
1056     std::ifstream inFile;
1057     inFile.open(DEFAULT_TEMPLATE_PATH.c_str(), std::ios::in);
1058     if (!inFile.is_open()) {
1059         ANS_LOGE("read template config error.");
1060         return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
1061     }
1062 
1063     nlohmann::json jsonObj;
1064     inFile >> jsonObj;
1065     if (jsonObj.is_null() || !jsonObj.is_object()) {
1066         ANS_LOGE("Invalid JSON object");
1067         return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
1068     }
1069     if (jsonObj.is_discarded()) {
1070         ANS_LOGE("template json discarded error.");
1071         inFile.close();
1072         return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
1073     }
1074 
1075     if (jsonObj.contains(templateName)) {
1076         support = true;
1077     }
1078 
1079     jsonObj.clear();
1080     inFile.close();
1081     return ERR_OK;
1082 }
1083 
SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,const bool enabled)1084 ErrCode NotificationPreferences::SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
1085     const std::string &deviceType, const bool enabled)
1086 {
1087     ANS_LOGD("%{public}s", __FUNCTION__);
1088     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
1089         return ERR_ANS_INVALID_PARAM;
1090     }
1091 
1092     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1093     NotificationPreferencesInfo::BundleInfo bundleInfo;
1094     bundleInfo.SetBundleName(bundleOption->GetBundleName());
1095     bundleInfo.SetBundleUid(bundleOption->GetUid());
1096     NotificationConstant::SWITCH_STATE defaultState = CheckApiCompatibility(bundleOption) ?
1097         NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON :
1098         NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF;
1099     bundleInfo.SetEnableNotification(defaultState);
1100     bool storeDBResult = true;
1101     storeDBResult = preferncesDB_->PutDistributedEnabledForBundle(deviceType, bundleInfo, enabled);
1102     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1103 }
1104 
SetDistributedBundleOption(const std::vector<sptr<DistributedBundleOption>> & bundles,const std::string & deviceType)1105 ErrCode NotificationPreferences::SetDistributedBundleOption(
1106     const std::vector<sptr<DistributedBundleOption>> &bundles,
1107     const std::string &deviceType)
1108 {
1109     ANS_LOGD("%{public}s", __FUNCTION__);
1110 
1111     int32_t userId = -1;
1112     OsAccountManagerHelper::GetInstance().GetCurrentCallingUserId(userId);
1113 
1114     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1115     bool storeDBResult = true;
1116     storeDBResult = preferncesDB_->PutDistributedBundleOption(bundles, deviceType, userId);
1117     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1118 }
1119 
IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,bool & enabled)1120 ErrCode NotificationPreferences::IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
1121     const std::string &deviceType, bool &enabled)
1122 {
1123     ANS_LOGD("%{public}s", __FUNCTION__);
1124     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
1125         return ERR_ANS_INVALID_PARAM;
1126     }
1127 
1128     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1129     NotificationPreferencesInfo::BundleInfo bundleInfo;
1130     bundleInfo.SetBundleName(bundleOption->GetBundleName());
1131     bundleInfo.SetBundleUid(bundleOption->GetUid());
1132     NotificationConstant::SWITCH_STATE defaultState = CheckApiCompatibility(bundleOption) ?
1133         NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON :
1134         NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF;
1135     bundleInfo.SetEnableNotification(defaultState);
1136     bool storeDBResult = true;
1137     storeDBResult = preferncesDB_->GetDistributedEnabledForBundle(deviceType, bundleInfo, enabled);
1138     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1139 }
1140 
SetSilentReminderEnabled(const sptr<NotificationBundleOption> & bundleOption,const bool enabled)1141 ErrCode NotificationPreferences::SetSilentReminderEnabled(const sptr<NotificationBundleOption> &bundleOption,
1142     const bool enabled)
1143 {
1144     ANS_LOGD("%{public}s", __FUNCTION__);
1145     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
1146         return ERR_ANS_INVALID_PARAM;
1147     }
1148 
1149     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1150     NotificationPreferencesInfo::SilentReminderInfo silentReminderInfo;
1151     silentReminderInfo.bundleName = bundleOption->GetBundleName();
1152     silentReminderInfo.uid = bundleOption->GetUid();
1153     silentReminderInfo.enableStatus =
1154         enabled ? NotificationConstant::SWITCH_STATE::USER_MODIFIED_ON
1155         : NotificationConstant::SWITCH_STATE::USER_MODIFIED_OFF;
1156     bool storeDBResult = true;
1157     storeDBResult = preferncesDB_->SetSilentReminderEnabled(silentReminderInfo);
1158     if (storeDBResult) {
1159         preferencesInfo_.SetSilentReminderInfo(silentReminderInfo);
1160     }
1161     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1162 }
1163 
IsSilentReminderEnabled(const sptr<NotificationBundleOption> & bundleOption,NotificationConstant::SWITCH_STATE & enableStatus)1164 ErrCode NotificationPreferences::IsSilentReminderEnabled(const sptr<NotificationBundleOption> &bundleOption,
1165     NotificationConstant::SWITCH_STATE &enableStatus)
1166 {
1167     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
1168         return ERR_ANS_INVALID_PARAM;
1169     }
1170 
1171     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1172     NotificationPreferencesInfo::SilentReminderInfo silentReminderInfo;
1173     if (preferencesInfo_.GetSilentReminderInfo(bundleOption, silentReminderInfo)) {
1174         enableStatus = silentReminderInfo.enableStatus;
1175         return ERR_OK;
1176     }
1177     silentReminderInfo.bundleName = bundleOption->GetBundleName();
1178     silentReminderInfo.uid = bundleOption->GetUid();
1179     bool storeDBResult = true;
1180     storeDBResult = preferncesDB_->IsSilentReminderEnabled(silentReminderInfo);
1181     if (storeDBResult) {
1182         enableStatus = silentReminderInfo.enableStatus;
1183         preferencesInfo_.SetSilentReminderInfo(silentReminderInfo);
1184     }
1185     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1186 }
1187 
RemoveSilentEnabledDbByBundle(const sptr<NotificationBundleOption> & bundleOption)1188 void NotificationPreferences::RemoveSilentEnabledDbByBundle(const sptr<NotificationBundleOption> &bundleOption)
1189 {
1190     ANS_LOGE("%{public}s", __FUNCTION__);
1191     if (preferncesDB_ != nullptr && bundleOption != nullptr) {
1192         std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1193         preferncesDB_->RemoveSilentEnabledDbByBundle(bundleOption->GetBundleName(), bundleOption->GetUid());
1194         preferencesInfo_.RemoveSilentReminderInfo(bundleOption);
1195     }
1196 }
1197 
SetDistributedEnabled(const std::string & deviceType,const NotificationConstant::SWITCH_STATE & enableStatus)1198 ErrCode NotificationPreferences::SetDistributedEnabled(
1199     const std::string &deviceType, const NotificationConstant::SWITCH_STATE &enableStatus)
1200 {
1201     ANS_LOGD("%{public}s", __FUNCTION__);
1202     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1203     bool storeDBResult = true;
1204     storeDBResult = preferncesDB_->PutDistributedEnabled(deviceType, enableStatus);
1205     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1206 }
1207 
IsDistributedEnabled(const std::string & deviceType,NotificationConstant::SWITCH_STATE & enableStatus)1208 ErrCode NotificationPreferences::IsDistributedEnabled(
1209     const std::string &deviceType, NotificationConstant::SWITCH_STATE &enableStatus)
1210 {
1211     ANS_LOGD("%{public}s", __FUNCTION__);
1212     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1213     bool storeDBResult = true;
1214     enableStatus = NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF;
1215     storeDBResult = preferncesDB_->GetDistributedEnabled(deviceType, enableStatus);
1216     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1217 }
1218 
GetDistributedAuthStatus(const std::string & deviceType,const std::string & deviceId,int32_t targetUserId,bool & isAuth)1219 ErrCode NotificationPreferences::GetDistributedAuthStatus(
1220     const std::string &deviceType, const std::string &deviceId, int32_t targetUserId, bool &isAuth)
1221 {
1222     ANS_LOGD("%{public}s", __FUNCTION__);
1223     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1224     bool storeDBResult = true;
1225     storeDBResult = preferncesDB_->GetDistributedAuthStatus(deviceType, deviceId, targetUserId, isAuth);
1226     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1227 }
1228 
SetDistributedAuthStatus(const std::string & deviceType,const std::string & deviceId,int32_t targetUserId,bool isAuth)1229 ErrCode NotificationPreferences::SetDistributedAuthStatus(
1230     const std::string &deviceType, const std::string &deviceId, int32_t targetUserId, bool isAuth)
1231 {
1232     ANS_LOGD("%{public}s", __FUNCTION__);
1233     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1234     bool storeDBResult = true;
1235     storeDBResult = preferncesDB_->SetDistributedAuthStatus(deviceType, deviceId, targetUserId, isAuth);
1236     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1237 }
1238 
SetSmartReminderEnabled(const std::string & deviceType,const bool enabled)1239 ErrCode NotificationPreferences::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled)
1240 {
1241     ANS_LOGD("%{public}s", __FUNCTION__);
1242     if (deviceType.empty()) {
1243         return ERR_ANS_INVALID_PARAM;
1244     }
1245 
1246     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1247     bool storeDBResult = true;
1248     storeDBResult = preferncesDB_->SetSmartReminderEnabled(deviceType, enabled);
1249     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1250 }
1251 
IsSmartReminderEnabled(const std::string & deviceType,bool & enabled)1252 ErrCode NotificationPreferences::IsSmartReminderEnabled(const std::string &deviceType, bool &enabled)
1253 {
1254     ANS_LOGD("%{public}s", __FUNCTION__);
1255     if (deviceType.empty()) {
1256         return ERR_ANS_INVALID_PARAM;
1257     }
1258 
1259     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1260     bool storeDBResult = true;
1261     storeDBResult = preferncesDB_->IsSmartReminderEnabled(deviceType, enabled);
1262     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1263 }
1264 
SetDistributedEnabledBySlot(const NotificationConstant::SlotType & slotType,const std::string & deviceType,const bool enabled)1265 ErrCode NotificationPreferences::SetDistributedEnabledBySlot(
1266     const NotificationConstant::SlotType &slotType, const std::string &deviceType, const bool enabled)
1267 {
1268     ANS_LOGD("%{public}s", __FUNCTION__);
1269     if (deviceType.empty()) {
1270         return ERR_ANS_INVALID_PARAM;
1271     }
1272 
1273     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1274     bool storeDBResult = true;
1275     storeDBResult = preferncesDB_->SetDistributedEnabledBySlot(slotType, deviceType, enabled);
1276     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1277 }
1278 
IsDistributedEnabledBySlot(const NotificationConstant::SlotType & slotType,const std::string & deviceType,bool & enabled)1279 ErrCode NotificationPreferences::IsDistributedEnabledBySlot(
1280     const NotificationConstant::SlotType &slotType, const std::string &deviceType, bool &enabled)
1281 {
1282     ANS_LOGD("%{public}s", __FUNCTION__);
1283     if (deviceType.empty()) {
1284         return ERR_ANS_INVALID_PARAM;
1285     }
1286 
1287     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1288     bool storeDBResult = true;
1289     storeDBResult = preferncesDB_->IsDistributedEnabledBySlot(slotType, deviceType, enabled);
1290     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1291 }
1292 
InitSettingFromDisturbDB(int32_t userId)1293 void NotificationPreferences::InitSettingFromDisturbDB(int32_t userId)
1294 {
1295     ANS_LOGI("%{public}s userId is %{public}d", __FUNCTION__, userId);
1296     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1297     if (preferncesDB_ != nullptr) {
1298         preferncesDB_->ParseFromDisturbeDB(preferencesInfo_, userId);
1299     }
1300 }
1301 
RemoveSettings(int32_t userId)1302 void NotificationPreferences::RemoveSettings(int32_t userId)
1303 {
1304     ANS_LOGD("%{public}s", __FUNCTION__);
1305     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1306     preferencesInfo_.RemoveNotificationEnable(userId);
1307     preferencesInfo_.RemoveDoNotDisturbDate(userId);
1308     if (preferncesDB_ != nullptr) {
1309         preferncesDB_->RemoveNotificationEnable(userId);
1310         preferncesDB_->RemoveDoNotDisturbDate(userId);
1311         preferncesDB_->DropUserTable(userId);
1312     }
1313 }
1314 
CheckApiCompatibility(const sptr<NotificationBundleOption> & bundleOption) const1315 bool NotificationPreferences::CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption) const
1316 {
1317     ANS_LOGD("%{public}s", __FUNCTION__);
1318     std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
1319     if (bundleManager == nullptr) {
1320         return false;
1321     }
1322     return bundleManager->CheckApiCompatibility(bundleOption);
1323 }
1324 
RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> & bundleOption)1325 void NotificationPreferences::RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> &bundleOption)
1326 {
1327     ANS_LOGE("%{public}s", __FUNCTION__);
1328     if (preferncesDB_ != nullptr && bundleOption != nullptr) {
1329         preferncesDB_->RemoveAnsBundleDbInfo(bundleOption->GetBundleName(), bundleOption->GetUid());
1330     }
1331 }
1332 
RemoveEnabledDbByBundle(const sptr<NotificationBundleOption> & bundleOption)1333 void NotificationPreferences::RemoveEnabledDbByBundle(const sptr<NotificationBundleOption> &bundleOption)
1334 {
1335     ANS_LOGE("%{public}s", __FUNCTION__);
1336     if (preferncesDB_ != nullptr && bundleOption != nullptr) {
1337         std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1338         preferncesDB_->RemoveEnabledDbByBundleName(bundleOption->GetBundleName(), bundleOption->GetUid());
1339     }
1340 }
1341 
GetBundleSoundPermission(bool & allPackage,std::set<std::string> & bundleNames)1342 bool NotificationPreferences::GetBundleSoundPermission(bool &allPackage, std::set<std::string> &bundleNames)
1343 {
1344     ANS_LOGD("%{public}s", __FUNCTION__);
1345     std::string value = "";
1346     int32_t userId = -1;
1347     OsAccountManagerHelper::GetInstance().GetCurrentCallingUserId(userId);
1348     if (GetKvFromDb("RING_TRUSTLIST_PKG", value, userId) != ERR_OK) {
1349         ANS_LOGD("Get bundle sound permission failed.");
1350         return false;
1351     }
1352 
1353     ANS_LOGD("The bundle permission is :%{public}s.", value.c_str());
1354     nlohmann::json jsonPermission = nlohmann::json::parse(value, nullptr, false);
1355     if (jsonPermission.is_null() || jsonPermission.empty()) {
1356         ANS_LOGE("Invalid JSON object");
1357         return false;
1358     }
1359     if (jsonPermission.is_discarded() || !jsonPermission.is_array()) {
1360         ANS_LOGE("Parse bundle permission failed due to data is discarded or not array");
1361         return false;
1362     }
1363 
1364     for (const auto &item : jsonPermission) {
1365         bundleNames.insert(item);
1366         if (item == "ALL_PKG") {
1367             allPackage = true;
1368         }
1369     }
1370     return true;
1371 }
1372 
SetKvToDb(const std::string & key,const std::string & value,const int32_t & userId)1373 int32_t NotificationPreferences::SetKvToDb(
1374     const std::string &key, const std::string &value, const int32_t &userId)
1375 {
1376     if (preferncesDB_ == nullptr) {
1377         return ERR_ANS_SERVICE_NOT_READY;
1378     }
1379     if (key == "kiosk_app_trust_list") {
1380         isKioskTrustListUpdate_ = true;
1381     }
1382     return preferncesDB_->SetKvToDb(key, value, userId);
1383 }
1384 
SetByteToDb(const std::string & key,const std::vector<uint8_t> & value,const int32_t & userId)1385 int32_t NotificationPreferences::SetByteToDb(
1386     const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId)
1387 {
1388     if (preferncesDB_ == nullptr) {
1389         return ERR_ANS_SERVICE_NOT_READY;
1390     }
1391     return preferncesDB_->SetByteToDb(key, value, userId);
1392 }
1393 
GetKvFromDb(const std::string & key,std::string & value,const int32_t & userId)1394 int32_t NotificationPreferences::GetKvFromDb(
1395     const std::string &key, std::string &value, const int32_t &userId)
1396 {
1397     if (preferncesDB_ == nullptr) {
1398         return ERR_ANS_SERVICE_NOT_READY;
1399     }
1400     return preferncesDB_->GetKvFromDb(key, value, userId);
1401 }
1402 
GetByteFromDb(const std::string & key,std::vector<uint8_t> & value,const int32_t & userId)1403 int32_t NotificationPreferences::GetByteFromDb(
1404     const std::string &key, std::vector<uint8_t> &value, const int32_t &userId)
1405 {
1406     if (preferncesDB_ == nullptr) {
1407         return ERR_ANS_SERVICE_NOT_READY;
1408     }
1409     return preferncesDB_->GetByteFromDb(key, value, userId);
1410 }
1411 
GetBatchKvsFromDbContainsKey(const std::string & key,std::unordered_map<std::string,std::string> & values,const int32_t & userId)1412 int32_t NotificationPreferences::GetBatchKvsFromDbContainsKey(
1413     const std::string &key, std::unordered_map<std::string, std::string> &values, const int32_t &userId)
1414 {
1415     if (preferncesDB_ == nullptr) {
1416         return ERR_ANS_SERVICE_NOT_READY;
1417     }
1418     return preferncesDB_->GetBatchKvsFromDbContainsKey(key, values, userId);
1419 }
1420 
GetBatchKvsFromDb(const std::string & key,std::unordered_map<std::string,std::string> & values,const int32_t & userId)1421 int32_t NotificationPreferences::GetBatchKvsFromDb(
1422     const std::string &key, std::unordered_map<std::string, std::string> &values, const int32_t &userId)
1423 {
1424     if (preferncesDB_ == nullptr) {
1425         return ERR_ANS_SERVICE_NOT_READY;
1426     }
1427     return preferncesDB_->GetBatchKvsFromDb(key, values, userId);
1428 }
1429 
DeleteKvFromDb(const std::string & key,const int32_t & userId)1430 int32_t NotificationPreferences::DeleteKvFromDb(const std::string &key, const int32_t &userId)
1431 {
1432     if (preferncesDB_ == nullptr) {
1433         return ERR_ANS_SERVICE_NOT_READY;
1434     }
1435     return preferncesDB_->DeleteKvFromDb(key, userId);
1436 }
1437 
DeleteBatchKvFromDb(const std::vector<std::string> & keys,const int32_t & userId)1438 int32_t NotificationPreferences::DeleteBatchKvFromDb(const std::vector<std::string> &keys,  const int32_t &userId)
1439 {
1440     if (preferncesDB_ == nullptr) {
1441         return ERR_ANS_SERVICE_NOT_READY;
1442     }
1443     return preferncesDB_->DeleteBatchKvFromDb(keys, userId);
1444 }
1445 
IsAgentRelationship(const std::string & agentBundleName,const std::string & sourceBundleName)1446 bool NotificationPreferences::IsAgentRelationship(const std::string &agentBundleName,
1447     const std::string &sourceBundleName)
1448 {
1449     if (AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) {
1450         ANS_LOGD("Client has agent permission.");
1451         return true;
1452     }
1453 
1454     if (preferncesDB_ == nullptr) {
1455         ANS_LOGD("perferencdDb is null.");
1456         return false;
1457     }
1458 
1459     return preferncesDB_->IsAgentRelationship(agentBundleName, sourceBundleName);
1460 }
1461 
GetAdditionalConfig(const std::string & key)1462 std::string NotificationPreferences::GetAdditionalConfig(const std::string &key)
1463 {
1464     if (preferncesDB_ == nullptr) {
1465         return "";
1466     }
1467     return preferncesDB_->GetAdditionalConfig(key);
1468 }
1469 
DelCloneProfileInfo(const int32_t & userId,const sptr<NotificationDoNotDisturbProfile> & info)1470 bool NotificationPreferences::DelCloneProfileInfo(const int32_t &userId,
1471     const sptr<NotificationDoNotDisturbProfile>& info)
1472 {
1473     if (preferncesDB_ == nullptr) {
1474         return false;
1475     }
1476     return preferncesDB_->DelCloneProfileInfo(userId, info);
1477 }
1478 
UpdateBatchCloneProfileInfo(const int32_t & userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> & profileInfo)1479 bool NotificationPreferences::UpdateBatchCloneProfileInfo(const int32_t &userId,
1480     const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo)
1481 {
1482     if (preferncesDB_ == nullptr) {
1483         return false;
1484     }
1485     return preferncesDB_->UpdateBatchCloneProfileInfo(userId, profileInfo);
1486 }
1487 
GetAllCloneProfileInfo(const int32_t & userId,std::vector<sptr<NotificationDoNotDisturbProfile>> & profilesInfo)1488 void NotificationPreferences::GetAllCloneProfileInfo(const int32_t &userId,
1489     std::vector<sptr<NotificationDoNotDisturbProfile>>& profilesInfo)
1490 {
1491     if (preferncesDB_ == nullptr) {
1492         return;
1493     }
1494     return preferncesDB_->GetAllCloneProfileInfo(userId, profilesInfo);
1495 }
1496 
GetAllCloneBundleInfo(const int32_t & userId,std::vector<NotificationCloneBundleInfo> & cloneBundleInfo)1497 void NotificationPreferences::GetAllCloneBundleInfo(const int32_t &userId,
1498     std::vector<NotificationCloneBundleInfo>& cloneBundleInfo)
1499 {
1500     if (preferncesDB_ == nullptr) {
1501         return;
1502     }
1503     return preferncesDB_->GetAllCloneBundleInfo(userId, cloneBundleInfo);
1504 }
1505 
UpdateBatchCloneBundleInfo(const int32_t & userId,const std::vector<NotificationCloneBundleInfo> & cloneBundleInfo)1506 bool NotificationPreferences::UpdateBatchCloneBundleInfo(const int32_t &userId,
1507     const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo)
1508 {
1509     if (preferncesDB_ == nullptr) {
1510         return false;
1511     }
1512     return preferncesDB_->UpdateBatchCloneBundleInfo(userId, cloneBundleInfo);
1513 }
1514 
DelCloneBundleInfo(const int32_t & userId,const NotificationCloneBundleInfo & cloneBundleInfo)1515 bool NotificationPreferences::DelCloneBundleInfo(const int32_t &userId,
1516     const NotificationCloneBundleInfo& cloneBundleInfo)
1517 {
1518     if (preferncesDB_ == nullptr) {
1519         return false;
1520     }
1521     return preferncesDB_->DelCloneBundleInfo(userId, cloneBundleInfo);
1522 }
1523 
DelBatchCloneProfileInfo(const int32_t & userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> & profileInfo)1524 bool NotificationPreferences::DelBatchCloneProfileInfo(const int32_t &userId,
1525     const std::vector<sptr<NotificationDoNotDisturbProfile>>& profileInfo)
1526 {
1527     if (preferncesDB_ == nullptr) {
1528         return false;
1529     }
1530     return preferncesDB_->DelBatchCloneProfileInfo(userId, profileInfo);
1531 }
1532 
DelBatchCloneBundleInfo(const int32_t & userId,const std::vector<NotificationCloneBundleInfo> & cloneBundleInfo)1533 bool NotificationPreferences::DelBatchCloneBundleInfo(const int32_t &userId,
1534     const std::vector<NotificationCloneBundleInfo>& cloneBundleInfo)
1535 {
1536     if (preferncesDB_ == nullptr) {
1537         return false;
1538     }
1539     return preferncesDB_->DelBatchCloneBundleInfo(userId, cloneBundleInfo);
1540 }
1541 
SetDisableNotificationInfo(const sptr<NotificationDisable> & notificationDisable)1542 ErrCode NotificationPreferences::SetDisableNotificationInfo(const sptr<NotificationDisable> &notificationDisable)
1543 {
1544     ANS_LOGD("called");
1545     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1546     preferencesInfo_.SetDisableNotificationInfo(notificationDisable);
1547     if (preferncesDB_ == nullptr) {
1548         ANS_LOGE("the prefernces db is nullptr");
1549         return ERR_ANS_SERVICE_NOT_READY;
1550     }
1551     if (!preferncesDB_->SetDisableNotificationInfo(notificationDisable)) {
1552         ANS_LOGE("db set disable notification info fail");
1553         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1554     }
1555 
1556     return ERR_OK;
1557 }
1558 
GetDisableNotificationInfo(NotificationDisable & notificationDisable)1559 bool NotificationPreferences::GetDisableNotificationInfo(NotificationDisable &notificationDisable)
1560 {
1561     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1562     if (preferencesInfo_.GetDisableNotificationInfo(notificationDisable)) {
1563         ANS_LOGD("info get disable notification success");
1564         return true;
1565     }
1566     if (preferncesDB_ == nullptr) {
1567         ANS_LOGE("the prefernces db is nullptr");
1568         return false;
1569     }
1570     if (preferncesDB_->GetDisableNotificationInfo(notificationDisable)) {
1571         ANS_LOGD("db get disable notification success");
1572         sptr<NotificationDisable> notificationDisablePtr = new (std::nothrow) NotificationDisable(notificationDisable);
1573         preferencesInfo_.SetDisableNotificationInfo(notificationDisablePtr);
1574     } else {
1575         ANS_LOGD("db get disable notification fail");
1576         return false;
1577     }
1578     return true;
1579 }
1580 
GetUserDisableNotificationInfo(int32_t userId,NotificationDisable & notificationDisable)1581 bool NotificationPreferences::GetUserDisableNotificationInfo(int32_t userId, NotificationDisable &notificationDisable)
1582 {
1583     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1584     if (preferencesInfo_.GetUserDisableNotificationInfo(userId, notificationDisable)) {
1585         ANS_LOGD("info get disable notification success");
1586         return true;
1587     }
1588     if (preferncesDB_ == nullptr) {
1589         ANS_LOGE("the prefernces db is nullptr");
1590         return false;
1591     }
1592     if (preferncesDB_->GetUserDisableNotificationInfo(userId, notificationDisable)) {
1593         ANS_LOGD("db get disable notification success");
1594         sptr<NotificationDisable> notificationDisablePtr = new (std::nothrow) NotificationDisable(notificationDisable);
1595         preferencesInfo_.SetDisableNotificationInfo(notificationDisablePtr);
1596     } else {
1597         ANS_LOGD("db get disable notification fail");
1598         return false;
1599     }
1600     return true;
1601 }
1602 
GetkioskAppTrustList(std::vector<std::string> & kioskAppTrustList)1603 bool NotificationPreferences::GetkioskAppTrustList(std::vector<std::string> &kioskAppTrustList)
1604 {
1605     ANS_LOGD("%{public}s", __FUNCTION__);
1606     if (preferencesInfo_.GetkioskAppTrustList(kioskAppTrustList) && !isKioskTrustListUpdate_) {
1607         ANS_LOGD("info get disable notification success");
1608         return true;
1609     }
1610     std::string value = "";
1611     int32_t userId = -1;
1612     if (GetKvFromDb("kiosk_app_trust_list", value, userId) != ERR_OK) {
1613         ANS_LOGD("Get kiosk app trust list failed.");
1614         return false;
1615     }
1616     if (value.empty() || !nlohmann::json::accept(value)) {
1617         ANS_LOGE("Invalid json string");
1618         return false;
1619     }
1620     nlohmann::json jsonObject = nlohmann::json::parse(value, nullptr, false);
1621     if (jsonObject.is_null() || jsonObject.empty()) {
1622         ANS_LOGE("Invalid JSON object");
1623         return false;
1624     }
1625     if (jsonObject.is_discarded() || !jsonObject.is_array()) {
1626         ANS_LOGE("Parse kiosk app trust list failed due to data is discarded or not array");
1627         return false;
1628     }
1629     kioskAppTrustList = jsonObject.get<std::vector<std::string>>();
1630     preferencesInfo_.SetkioskAppTrustList(kioskAppTrustList);
1631     isKioskTrustListUpdate_ = false;
1632     return true;
1633 }
1634 
SetDistributedDevicelist(std::vector<std::string> & deviceTypes,const int32_t & userId)1635 ErrCode NotificationPreferences::SetDistributedDevicelist(std::vector<std::string> &deviceTypes, const int32_t &userId)
1636 {
1637     ANS_LOGD("%{public}s", __FUNCTION__);
1638     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1639     bool storeDBResult = true;
1640     nlohmann::json deviceTypesJson = deviceTypes;
1641     std::string deviceTypesjsonString = deviceTypesJson.dump();
1642     storeDBResult = preferncesDB_->PutDistributedDevicelist(deviceTypesjsonString, userId);
1643     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1644 }
1645 
GetDistributedDevicelist(std::vector<std::string> & deviceTypes)1646 ErrCode NotificationPreferences::GetDistributedDevicelist(std::vector<std::string> &deviceTypes)
1647 {
1648     ANS_LOGD("%{public}s", __FUNCTION__);
1649     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1650     std::string value = "";
1651     auto storeDBResult = preferncesDB_->GetDistributedDevicelist(value);
1652     if (!storeDBResult) {
1653         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1654     }
1655     if (value.empty()) {
1656         ANS_LOGE("Empty json");
1657         return ERR_OK;
1658     }
1659 
1660     if (!nlohmann::json::accept(value)) {
1661         ANS_LOGE("Invalid json string");
1662         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1663     }
1664     nlohmann::json jsonObject = nlohmann::json::parse(value, nullptr, false);
1665     if (jsonObject.is_null() || jsonObject.empty()) {
1666         ANS_LOGE("Invalid JSON object");
1667         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1668     }
1669     if (jsonObject.is_discarded() || !jsonObject.is_array()) {
1670         ANS_LOGE("Parse device type list failed due to data is discarded or not array");
1671         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1672     }
1673     deviceTypes = jsonObject.get<std::vector<std::string>>();
1674     return ERR_OK;
1675 }
1676 
SetSubscriberExistFlag(const std::string & deviceType,bool existFlag)1677 ErrCode NotificationPreferences::SetSubscriberExistFlag(const std::string& deviceType, bool existFlag)
1678 {
1679     ANS_LOGD("%{public}s", __FUNCTION__);
1680     if (deviceType.empty()) {
1681         return ERR_ANS_INVALID_PARAM;
1682     }
1683 
1684     if (preferncesDB_ == nullptr) {
1685         return ERR_ANS_SERVICE_NOT_READY;
1686     }
1687 
1688     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1689     bool storeDBResult = preferncesDB_->SetSubscriberExistFlag(deviceType, existFlag);
1690     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1691 }
1692 
GetSubscriberExistFlag(const std::string & deviceType,bool & existFlag)1693 ErrCode NotificationPreferences::GetSubscriberExistFlag(const std::string& deviceType, bool& existFlag)
1694 {
1695     ANS_LOGD("%{public}s", __FUNCTION__);
1696     if (deviceType.empty()) {
1697         return ERR_ANS_INVALID_PARAM;
1698     }
1699 
1700     if (preferncesDB_ == nullptr) {
1701         return ERR_ANS_SERVICE_NOT_READY;
1702     }
1703 
1704     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1705     bool storeDBResult = preferncesDB_->GetSubscriberExistFlag(deviceType, existFlag);
1706     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1707 }
1708 
SetDistributedEnabledForBundle(const NotificationPreferencesInfo::BundleInfo & bundleInfo)1709 void NotificationPreferences::SetDistributedEnabledForBundle(const NotificationPreferencesInfo::BundleInfo& bundleInfo)
1710 {
1711     ANS_LOGD("%{public}s", __FUNCTION__);
1712     if (!isCachedMirrorNotificationEnabledStatus_) {
1713         if (!DelayedSingleton<NotificationConfigParse>::GetInstance()->GetMirrorNotificationEnabledStatus(
1714             mirrorNotificationEnabledStatus_)) {
1715             return;
1716         }
1717         isCachedMirrorNotificationEnabledStatus_ = true;
1718     }
1719     if (mirrorNotificationEnabledStatus_.empty()) {
1720         ANS_LOGD("mirrorNotificationEnabledStatus_ is empty");
1721         return;
1722     }
1723     if (preferncesDB_ == nullptr) {
1724         ANS_LOGD("preferncesDB_ is nullptr");
1725         return;
1726     }
1727     for (const auto& deviceType : mirrorNotificationEnabledStatus_) {
1728         bool ret = preferncesDB_->IsDistributedEnabledEmptyForBundle(deviceType, bundleInfo);
1729         if (!ret) {
1730             ANS_LOGD("get %{public}s distributedEnabled is empty", deviceType.c_str());
1731             preferncesDB_->PutDistributedEnabledForBundle(deviceType, bundleInfo, true);
1732         }
1733     }
1734 }
1735 
SetHashCodeRule(const int32_t uid,const uint32_t type)1736 ErrCode NotificationPreferences::SetHashCodeRule(const int32_t uid, const uint32_t type)
1737 {
1738     ANS_LOGD("%{public}s", __FUNCTION__);
1739 
1740     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1741     bool storeDBResult = true;
1742     storeDBResult = preferncesDB_->SetHashCodeRule(uid, type);
1743     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1744 }
1745 
GetHashCodeRule(const int32_t uid)1746 uint32_t NotificationPreferences::GetHashCodeRule(const int32_t uid)
1747 {
1748     ANS_LOGD("%{public}s", __FUNCTION__);
1749     std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
1750     uint32_t result = 0;
1751     result = preferncesDB_->GetHashCodeRule(uid);
1752     ANS_LOGI("uid = %{public}d result = %{public}d", uid, result);
1753     return result;
1754 }
1755 
GetBundleRemoveFlag(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,int32_t sourceType)1756 bool NotificationPreferences::GetBundleRemoveFlag(const sptr<NotificationBundleOption> &bundleOption,
1757     const NotificationConstant::SlotType &slotType, int32_t sourceType)
1758 {
1759     if (preferncesDB_ == nullptr) {
1760         return true;
1761     }
1762     return preferncesDB_->GetBundleRemoveFlag(bundleOption, slotType, sourceType);
1763 }
1764 
SetBundleRemoveFlag(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,int32_t sourceType)1765 bool NotificationPreferences::SetBundleRemoveFlag(const sptr<NotificationBundleOption> &bundleOption,
1766     const NotificationConstant::SlotType &slotType, int32_t sourceType)
1767 {
1768     if (preferncesDB_ == nullptr) {
1769         return false;
1770     }
1771     return preferncesDB_->SetBundleRemoveFlag(bundleOption, slotType, sourceType);
1772 }
1773 
SetKioskModeStatus(bool isKioskMode)1774 void NotificationPreferences::SetKioskModeStatus(bool isKioskMode)
1775 {
1776     isKioskMode_ = isKioskMode;
1777 }
1778 
IsKioskMode()1779 bool NotificationPreferences::IsKioskMode()
1780 {
1781     AAFwk::KioskStatus kioskStatus;
1782     auto ret = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->GetKioskStatus(kioskStatus));
1783     if (ret != ERR_OK) {
1784         ANS_LOGE("Get KioskStatus failed");
1785         return isKioskMode_;
1786     }
1787     isKioskMode_ = kioskStatus.isKioskMode_;
1788     return isKioskMode_;
1789 }
1790 
1791 #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER
GetKvFromDb(const std::string & key,std::string & value,const int32_t & userId,int32_t & retCode)1792 int32_t NotificationPreferences::GetKvFromDb(
1793     const std::string &key, std::string &value, const int32_t &userId, int32_t &retCode)
1794 {
1795     if (preferncesDB_ == nullptr) {
1796         return ERR_ANS_SERVICE_NOT_READY;
1797     }
1798     return preferncesDB_->GetKvFromDb(key, value, userId, retCode);
1799 }
1800 #endif
1801 }  // namespace Notification
1802 }  // namespace OHOS
1803