• 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 
CheckDoNotDisturbProfileID(int32_t profileId)533 bool NotificationPreferences::CheckDoNotDisturbProfileID(int32_t profileId)
534 {
535     if (profileId < DO_NOT_DISTURB_PROFILE_MIN_ID || profileId > DO_NOT_DISTURB_PROFILE_MAX_ID) {
536         ANS_LOGE("The profile id is out of range.");
537         return false;
538     }
539     return true;
540 }
541 
AddDoNotDisturbProfiles(int32_t userId,std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)542 ErrCode NotificationPreferences::AddDoNotDisturbProfiles(
543     int32_t userId, std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)
544 {
545     ANS_LOGD("Called.");
546     for (auto profile : profiles) {
547         if (profile == nullptr) {
548             ANS_LOGE("The profile is nullptr.");
549             return ERR_ANS_INVALID_PARAM;
550         }
551         if (!CheckDoNotDisturbProfileID(profile->GetProfileId())) {
552             return ERR_ANS_INVALID_PARAM;
553         }
554         auto trustList = profile->GetProfileTrustList();
555         for (auto& bundleInfo : trustList) {
556             int32_t index = BundleManagerHelper::GetInstance()->GetAppIndexByUid(bundleInfo.GetUid());
557             bundleInfo.SetAppIndex(index);
558             ANS_LOGI("Get app index by uid %{public}d %{public}s %{public}d", bundleInfo.GetUid(),
559                 bundleInfo.GetBundleName().c_str(), index);
560         }
561         profile->SetProfileTrustList(trustList);
562     }
563     std::lock_guard<std::mutex> lock(preferenceMutex_);
564     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
565     preferencesInfo.AddDoNotDisturbProfiles(userId, profiles);
566     if (preferncesDB_ == nullptr) {
567         ANS_LOGE("The prefernces db is nullptr.");
568         return ERR_ANS_SERVICE_NOT_READY;
569     }
570     if (!preferncesDB_->AddDoNotDisturbProfiles(userId, profiles)) {
571         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
572     }
573     preferencesInfo_ = preferencesInfo;
574     return ERR_OK;
575 }
576 
IsNotificationSlotFlagsExists(const sptr<NotificationBundleOption> & bundleOption)577 bool NotificationPreferences::IsNotificationSlotFlagsExists(
578     const sptr<NotificationBundleOption> &bundleOption)
579 {
580     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
581         return false;
582     }
583     return preferncesDB_->IsNotificationSlotFlagsExists(bundleOption);
584 }
585 
GetBundleInfo(NotificationPreferencesInfo & preferencesInfo,const sptr<NotificationBundleOption> & bundleOption,NotificationPreferencesInfo::BundleInfo & info) const586 bool NotificationPreferences::GetBundleInfo(NotificationPreferencesInfo &preferencesInfo,
587     const sptr<NotificationBundleOption> &bundleOption, NotificationPreferencesInfo::BundleInfo &info) const
588 {
589     if (preferencesInfo.GetBundleInfo(bundleOption, info)) {
590         return true;
591     } else if (preferncesDB_->GetBundleInfo(bundleOption, info)) {
592         preferencesInfo.SetBundleInfo(info);
593         return true;
594     }
595     return false;
596 }
597 
RemoveDoNotDisturbProfiles(int32_t userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)598 ErrCode NotificationPreferences::RemoveDoNotDisturbProfiles(
599     int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)
600 {
601     ANS_LOGD("Called.");
602     for (auto profile : profiles) {
603         if (profile == nullptr) {
604             ANS_LOGE("The profile is nullptr.");
605             return ERR_ANS_INVALID_PARAM;
606         }
607         if (!CheckDoNotDisturbProfileID(profile->GetProfileId())) {
608             return ERR_ANS_INVALID_PARAM;
609         }
610     }
611     std::lock_guard<std::mutex> lock(preferenceMutex_);
612     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
613     preferencesInfo.RemoveDoNotDisturbProfiles(userId, profiles);
614     if (preferncesDB_ == nullptr) {
615         ANS_LOGE("The prefernces db is nullptr.");
616         return ERR_ANS_SERVICE_NOT_READY;
617     }
618     if (!preferncesDB_->RemoveDoNotDisturbProfiles(userId, profiles)) {
619         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
620     }
621     preferencesInfo_ = preferencesInfo;
622     return ERR_OK;
623 }
624 
UpdateProfilesUtil(std::vector<NotificationBundleOption> & trustList,const std::vector<NotificationBundleOption> bundleList)625 void NotificationPreferences::UpdateProfilesUtil(std::vector<NotificationBundleOption>& trustList,
626     const std::vector<NotificationBundleOption> bundleList)
627 {
628     for (auto& item : bundleList) {
629         bool exit = false;
630         for (auto& bundle: trustList) {
631             if (item.GetUid() == bundle.GetUid()) {
632                 exit = true;
633                 break;
634             }
635         }
636         if (!exit) {
637             trustList.push_back(item);
638         }
639     }
640 }
641 
UpdateDoNotDisturbProfiles(int32_t userId,int32_t profileId,const std::string & name,const std::vector<NotificationBundleOption> & bundleList)642 ErrCode NotificationPreferences::UpdateDoNotDisturbProfiles(int32_t userId, int32_t profileId,
643     const std::string& name, const std::vector<NotificationBundleOption>& bundleList)
644 {
645     ANS_LOGI("Called update Profile %{public}d %{public}d %{public}zu.", userId, profileId, bundleList.size());
646     if (!CheckDoNotDisturbProfileID(profileId) || bundleList.empty()) {
647         return ERR_ANS_INVALID_PARAM;
648     }
649 
650     sptr<NotificationDoNotDisturbProfile> profile = new (std::nothrow) NotificationDoNotDisturbProfile();
651     std::lock_guard<std::mutex> lock(preferenceMutex_);
652     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
653     if (preferencesInfo.GetDoNotDisturbProfiles(profileId, userId, profile)) {
654         auto trustList = profile->GetProfileTrustList();
655         UpdateProfilesUtil(trustList, bundleList);
656         profile->SetProfileTrustList(trustList);
657     } else {
658         profile->SetProfileId(profileId);
659         profile->SetProfileName(name);
660         profile->SetProfileTrustList(bundleList);
661     }
662     ANS_LOGI("Update profile %{public}d %{public}d %{public}zu", userId, profile->GetProfileId(),
663         profile->GetProfileTrustList().size());
664     preferencesInfo.AddDoNotDisturbProfiles(userId, {profile});
665     if (preferncesDB_ == nullptr) {
666         ANS_LOGE("The prefernces db is nullptr.");
667         return ERR_ANS_SERVICE_NOT_READY;
668     }
669     if (!preferncesDB_->AddDoNotDisturbProfiles(userId, {profile})) {
670         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
671     }
672     preferencesInfo_ = preferencesInfo;
673     return ERR_OK;
674 }
675 
UpdateCloneBundleInfo(int32_t userId,const NotificationCloneBundleInfo & cloneBundleInfo)676 void NotificationPreferences::UpdateCloneBundleInfo(int32_t userId,
677     const NotificationCloneBundleInfo& cloneBundleInfo)
678 {
679     ANS_LOGI("Event bundle update %{public}s.", cloneBundleInfo.Dump().c_str());
680     NotificationPreferencesInfo::BundleInfo bundleInfo;
681     sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption();
682     bundleOption->SetBundleName(cloneBundleInfo.GetBundleName());
683     bundleOption->SetUid(cloneBundleInfo.GetUid());
684     std::lock_guard<std::mutex> lock(preferenceMutex_);
685     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
686     if (!GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
687         bundleInfo.SetBundleName(cloneBundleInfo.GetBundleName());
688         bundleInfo.SetBundleUid(cloneBundleInfo.GetUid());
689     }
690 
691     /* after clone, override these witch */
692     bundleInfo.SetSlotFlags(cloneBundleInfo.GetSlotFlags());
693     bundleInfo.SetIsShowBadge(cloneBundleInfo.GetIsShowBadge());
694     bundleInfo.SetEnableNotification(cloneBundleInfo.GetEnableNotification());
695     /* update property to db */
696     if (!preferncesDB_->UpdateBundlePropertyToDisturbeDB(userId, bundleInfo)) {
697         ANS_LOGW("Clone bundle info failed %{public}s.", cloneBundleInfo.Dump().c_str());
698         return;
699     }
700     preferencesInfo.SetBundleInfo(bundleInfo);
701 
702     /* update slot info */
703     std::vector<sptr<NotificationSlot>> slots;
704     for (auto& cloneSlot : cloneBundleInfo.GetSlotInfo()) {
705         sptr<NotificationSlot> slotInfo = new (std::nothrow) NotificationSlot(cloneSlot.slotType_);
706         uint32_t slotFlags = bundleInfo.GetSlotFlags();
707         auto configSlotReminderMode = DelayedSingleton<NotificationConfigParse>::GetInstance()->
708             GetConfigSlotReminderModeByType(slotInfo->GetType(), bundleOption);
709         slotInfo->SetReminderMode(configSlotReminderMode & slotFlags);
710         slotInfo->SetEnable(cloneSlot.enable_);
711         slotInfo->SetForceControl(cloneSlot.isForceControl_);
712         slotInfo->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED);
713         slots.push_back(slotInfo);
714         bundleInfo.SetSlot(slotInfo);
715     }
716 
717     if (!preferncesDB_->UpdateBundleSlotToDisturbeDB(userId, cloneBundleInfo.GetBundleName(),
718         cloneBundleInfo.GetUid(), slots)) {
719         ANS_LOGW("Clone bundle slot failed %{public}s.", cloneBundleInfo.Dump().c_str());
720         preferencesInfo_ = preferencesInfo;
721         return;
722     }
723     preferencesInfo.SetBundleInfo(bundleInfo);
724     preferencesInfo_ = preferencesInfo;
725 }
726 
GetAllCLoneBundlesInfo(int32_t userId,std::vector<NotificationCloneBundleInfo> & cloneBundles)727 void NotificationPreferences::GetAllCLoneBundlesInfo(int32_t userId,
728     std::vector<NotificationCloneBundleInfo> &cloneBundles)
729 {
730     std::lock_guard<std::mutex> lock(preferenceMutex_);
731     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
732     std::unordered_map<std::string, std::string> bundlesMap;
733     if (GetBatchKvsFromDb(KEY_BUNDLE_LABEL, bundlesMap, userId) != ERR_OK) {
734         ANS_LOGE("Get bundle map info failed.");
735         return;
736     }
737     preferencesInfo.GetAllCLoneBundlesInfo(userId, bundlesMap, cloneBundles);
738     preferencesInfo_ = preferencesInfo;
739 }
740 
GetDoNotDisturbProfileListByUserId(int32_t userId,std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)741 void NotificationPreferences::GetDoNotDisturbProfileListByUserId(int32_t userId,
742     std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
743 {
744     std::lock_guard<std::mutex> lock(preferenceMutex_);
745     preferencesInfo_.GetAllDoNotDisturbProfiles(userId, profiles);
746 }
747 
GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> & bundleOption)748 ErrCode NotificationPreferences::GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> &bundleOption)
749 {
750     ANS_LOGD("Called.");
751     std::lock_guard<std::mutex> lock(preferenceMutex_);
752     if (preferncesDB_ == nullptr) {
753         return ERR_ANS_SERVICE_NOT_READY;
754     }
755     if (!preferncesDB_->GetAllNotificationEnabledBundles(bundleOption)) {
756         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
757     }
758     return ERR_OK;
759 }
760 
ClearNotificationInRestoreFactorySettings()761 ErrCode NotificationPreferences::ClearNotificationInRestoreFactorySettings()
762 {
763     ErrCode result = ERR_OK;
764     std::lock_guard<std::mutex> lock(preferenceMutex_);
765     if (!preferncesDB_->RemoveAllDataFromDisturbeDB()) {
766         result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
767     }
768 
769     if (result == ERR_OK) {
770         preferencesInfo_ = NotificationPreferencesInfo();
771     }
772     return result;
773 }
774 
GetDoNotDisturbProfile(int32_t profileId,int32_t userId,sptr<NotificationDoNotDisturbProfile> & profile)775 ErrCode NotificationPreferences::GetDoNotDisturbProfile(
776     int32_t profileId, int32_t userId, sptr<NotificationDoNotDisturbProfile> &profile)
777 {
778     if (!CheckDoNotDisturbProfileID(profileId)) {
779         return ERR_ANS_INVALID_PARAM;
780     }
781     std::lock_guard<std::mutex> lock(preferenceMutex_);
782     if (!preferencesInfo_.GetDoNotDisturbProfiles(profileId, userId, profile)) {
783         return ERR_ANS_NO_PROFILE_TEMPLATE;
784     }
785     return ERR_OK;
786 }
787 
RemoveDoNotDisturbProfileTrustList(int32_t userId,const sptr<NotificationBundleOption> & bundleOption)788 void NotificationPreferences::RemoveDoNotDisturbProfileTrustList(
789     int32_t userId, const sptr<NotificationBundleOption> &bundleOption)
790 {
791     if (bundleOption == nullptr) {
792         ANS_LOGE("The bundle option is nullptr.");
793         return;
794     }
795     int32_t uid = bundleOption->GetUid();
796     int32_t appIndex = bundleOption->GetAppIndex();
797     auto bundleName = bundleOption->GetBundleName();
798     ANS_LOGI("Remove %{public}s %{public}d %{public}d.", bundleName.c_str(), uid, appIndex);
799     std::lock_guard<std::mutex> lock(preferenceMutex_);
800     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
801 
802     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
803     preferencesInfo.GetAllDoNotDisturbProfiles(userId, profiles);
804     for (auto profile : profiles) {
805         if (profile == nullptr) {
806             ANS_LOGE("The profile is nullptr.");
807             continue;
808         }
809         auto trustList = profile->GetProfileTrustList();
810         for (auto it = trustList.begin(); it != trustList.end(); it++) {
811             if (it->GetUid() == uid) {
812                 trustList.erase(it);
813                 break;
814             }
815         }
816         profile->SetProfileTrustList(trustList);
817     }
818     preferencesInfo.AddDoNotDisturbProfiles(userId, profiles);
819     if (preferncesDB_ == nullptr) {
820         ANS_LOGE("The prefernces db is nullptr.");
821         return;
822     }
823     if (!preferncesDB_->AddDoNotDisturbProfiles(userId, profiles)) {
824         return;
825     }
826     preferencesInfo_ = preferencesInfo;
827 }
828 
CheckSlotForCreateSlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot,NotificationPreferencesInfo & preferencesInfo) const829 ErrCode NotificationPreferences::CheckSlotForCreateSlot(const sptr<NotificationBundleOption> &bundleOption,
830     const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
831 {
832     if (slot == nullptr) {
833         ANS_LOGE("Notification slot is nullptr.");
834         return ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_NOT_EXIST;
835     }
836 
837     NotificationPreferencesInfo::BundleInfo bundleInfo;
838     if (!GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
839         bundleInfo.SetBundleName(bundleOption->GetBundleName());
840         bundleInfo.SetBundleUid(bundleOption->GetUid());
841         bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
842     }
843     bundleInfo.SetSlot(slot);
844     preferencesInfo.SetBundleInfo(bundleInfo);
845 
846     return ERR_OK;
847 }
848 
CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,NotificationPreferencesInfo & preferencesInfo) const849 ErrCode NotificationPreferences::CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> &bundleOption,
850     const NotificationConstant::SlotType &slotType, NotificationPreferencesInfo &preferencesInfo) const
851 {
852     ErrCode result = ERR_OK;
853     NotificationPreferencesInfo::BundleInfo bundleInfo;
854     if (GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
855         if (bundleInfo.IsExsitSlot(slotType)) {
856             bundleInfo.RemoveSlot(slotType);
857         } else {
858             ANS_LOGE("Notification slot type does not exsited.");
859             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
860         }
861     } else {
862         ANS_LOGW("Notification bundle does not exsit.");
863         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
864     }
865     return result;
866 }
867 
CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot,NotificationPreferencesInfo & preferencesInfo) const868 ErrCode NotificationPreferences::CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> &bundleOption,
869     const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
870 {
871     if (slot == nullptr) {
872         ANS_LOGE("Notification slot is nullptr.");
873         return ERR_ANS_INVALID_PARAM;
874     }
875 
876     ErrCode result = ERR_OK;
877     NotificationPreferencesInfo::BundleInfo bundleInfo;
878     if (GetBundleInfo(preferencesInfo, bundleOption, bundleInfo)) {
879         if (bundleInfo.IsExsitSlot(slot->GetType())) {
880             bundleInfo.SetBundleName(bundleOption->GetBundleName());
881             bundleInfo.SetBundleUid(bundleOption->GetUid());
882             bundleInfo.SetSlot(slot);
883             preferencesInfo.SetBundleInfo(bundleInfo);
884         } else {
885             ANS_LOGE("Notification slot type does not exist.");
886             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
887         }
888     } else {
889         ANS_LOGW("Notification bundle does not exsit.");
890         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
891     }
892 
893     return result;
894 }
895 
896 template <typename T>
SetBundleProperty(NotificationPreferencesInfo & preferencesInfo,const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,const T & value)897 ErrCode NotificationPreferences::SetBundleProperty(NotificationPreferencesInfo &preferencesInfo,
898     const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
899 {
900     ErrCode result = ERR_OK;
901     NotificationPreferencesInfo::BundleInfo bundleInfo;
902     if (!GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
903         bundleInfo.SetBundleName(bundleOption->GetBundleName());
904         bundleInfo.SetBundleUid(bundleOption->GetUid());
905         bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
906     }
907     result = SaveBundleProperty(bundleInfo, bundleOption, type, value);
908     if (result == ERR_OK) {
909         preferencesInfo.SetBundleInfo(bundleInfo);
910     }
911 
912     return result;
913 }
914 
915 template <typename T>
SaveBundleProperty(NotificationPreferencesInfo::BundleInfo & bundleInfo,const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,const T & value)916 ErrCode NotificationPreferences::SaveBundleProperty(NotificationPreferencesInfo::BundleInfo &bundleInfo,
917     const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
918 {
919     bool storeDBResult = true;
920     switch (type) {
921         case BundleType::BUNDLE_IMPORTANCE_TYPE:
922             bundleInfo.SetImportance(value);
923             storeDBResult = preferncesDB_->PutImportance(bundleInfo, value);
924             break;
925         case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
926             bundleInfo.SetBadgeTotalNum(value);
927             storeDBResult = preferncesDB_->PutTotalBadgeNums(bundleInfo, value);
928             break;
929         case BundleType::BUNDLE_SHOW_BADGE_TYPE:
930             bundleInfo.SetIsShowBadge(value);
931             storeDBResult = preferncesDB_->PutShowBadge(bundleInfo, value);
932             break;
933         case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
934             bundleInfo.SetEnableNotification(value);
935             storeDBResult = preferncesDB_->PutNotificationsEnabledForBundle(bundleInfo, value);
936             break;
937         case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
938             ANS_LOGI("Into BUNDLE_POPPED_DIALOG_TYPE:SetHasPoppedDialog.");
939             bundleInfo.SetHasPoppedDialog(value);
940             storeDBResult = preferncesDB_->PutHasPoppedDialog(bundleInfo, value);
941             break;
942         case BundleType::BUNDLE_SLOTFLGS_TYPE:
943             ANS_LOGI("Into BUNDLE_SLOTFLGS_TYPE:SetSlotFlags.");
944             bundleInfo.SetSlotFlags(value);
945             storeDBResult = preferncesDB_->PutSlotFlags(bundleInfo, value);
946             break;
947         default:
948             break;
949     }
950     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
951 }
952 
953 template <typename T>
GetBundleProperty(const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,T & value)954 ErrCode NotificationPreferences::GetBundleProperty(
955     const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, T &value)
956 {
957     ErrCode result = ERR_OK;
958     NotificationPreferencesInfo::BundleInfo bundleInfo;
959     std::lock_guard<std::mutex> lock(preferenceMutex_);
960     if (GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) {
961         switch (type) {
962             case BundleType::BUNDLE_IMPORTANCE_TYPE:
963                 value = bundleInfo.GetImportance();
964                 break;
965             case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
966                 value = bundleInfo.GetBadgeTotalNum();
967                 break;
968             case BundleType::BUNDLE_SHOW_BADGE_TYPE:
969                 value = bundleInfo.GetIsShowBadge();
970                 break;
971             case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
972                 value = bundleInfo.GetEnableNotification();
973                 break;
974             case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
975                 ANS_LOGD("Into BUNDLE_POPPED_DIALOG_TYPE:GetHasPoppedDialog.");
976                 value = bundleInfo.GetHasPoppedDialog();
977                 break;
978             case BundleType::BUNDLE_SLOTFLGS_TYPE:
979                 value = bundleInfo.GetSlotFlags();
980                 ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:GetSlotFlags.");
981                 break;
982             default:
983                 result = ERR_ANS_INVALID_PARAM;
984                 break;
985         }
986     } else {
987         ANS_LOGW("Notification bundle does not exsit.");
988         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
989     }
990     return result;
991 }
992 
GenerateBundleKey(const sptr<NotificationBundleOption> & bundleOption) const993 std::string NotificationPreferences::GenerateBundleKey(const sptr<NotificationBundleOption> &bundleOption) const
994 {
995     return bundleOption->GetBundleName().append(std::to_string(bundleOption->GetUid()));
996 }
997 
GetTemplateSupported(const std::string & templateName,bool & support)998 ErrCode NotificationPreferences::GetTemplateSupported(const std::string& templateName, bool &support)
999 {
1000     if (templateName.length() == 0) {
1001         ANS_LOGE("template name is null.");
1002         return ERR_ANS_INVALID_PARAM;
1003     }
1004 
1005     std::ifstream inFile;
1006     inFile.open(DEFAULT_TEMPLATE_PATH.c_str(), std::ios::in);
1007     if (!inFile.is_open()) {
1008         ANS_LOGE("read template config error.");
1009         return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
1010     }
1011 
1012     nlohmann::json jsonObj;
1013     inFile >> jsonObj;
1014     if (jsonObj.is_null() || !jsonObj.is_object()) {
1015         ANS_LOGE("Invalid JSON object");
1016         return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
1017     }
1018     if (jsonObj.is_discarded()) {
1019         ANS_LOGE("template json discarded error.");
1020         inFile.close();
1021         return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
1022     }
1023 
1024     if (jsonObj.contains(templateName)) {
1025         support = true;
1026     }
1027 
1028     jsonObj.clear();
1029     inFile.close();
1030     return ERR_OK;
1031 }
1032 
SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,const bool enabled)1033 ErrCode NotificationPreferences::SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
1034     const std::string &deviceType, const bool enabled)
1035 {
1036     ANS_LOGD("%{public}s", __FUNCTION__);
1037     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
1038         return ERR_ANS_INVALID_PARAM;
1039     }
1040 
1041     std::lock_guard<std::mutex> lock(preferenceMutex_);
1042     NotificationPreferencesInfo::BundleInfo bundleInfo;
1043     bundleInfo.SetBundleName(bundleOption->GetBundleName());
1044     bundleInfo.SetBundleUid(bundleOption->GetUid());
1045     bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
1046     bool storeDBResult = true;
1047     storeDBResult = preferncesDB_->PutDistributedEnabledForBundle(deviceType, bundleInfo, enabled);
1048     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1049 }
1050 
IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & deviceType,bool & enabled)1051 ErrCode NotificationPreferences::IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
1052     const std::string &deviceType, bool &enabled)
1053 {
1054     ANS_LOGD("%{public}s", __FUNCTION__);
1055     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
1056         return ERR_ANS_INVALID_PARAM;
1057     }
1058 
1059     std::lock_guard<std::mutex> lock(preferenceMutex_);
1060     NotificationPreferencesInfo::BundleInfo bundleInfo;
1061     bundleInfo.SetBundleName(bundleOption->GetBundleName());
1062     bundleInfo.SetBundleUid(bundleOption->GetUid());
1063     bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
1064     bool storeDBResult = true;
1065     storeDBResult = preferncesDB_->GetDistributedEnabledForBundle(deviceType, bundleInfo, enabled);
1066     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1067 }
1068 
SetSmartReminderEnabled(const std::string & deviceType,const bool enabled)1069 ErrCode NotificationPreferences::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled)
1070 {
1071     ANS_LOGD("%{public}s", __FUNCTION__);
1072     if (deviceType.empty()) {
1073         return ERR_ANS_INVALID_PARAM;
1074     }
1075 
1076     std::lock_guard<std::mutex> lock(preferenceMutex_);
1077     bool storeDBResult = true;
1078     storeDBResult = preferncesDB_->SetSmartReminderEnabled(deviceType, enabled);
1079     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1080 }
1081 
IsSmartReminderEnabled(const std::string & deviceType,bool & enabled)1082 ErrCode NotificationPreferences::IsSmartReminderEnabled(const std::string &deviceType, bool &enabled)
1083 {
1084     ANS_LOGD("%{public}s", __FUNCTION__);
1085     if (deviceType.empty()) {
1086         return ERR_ANS_INVALID_PARAM;
1087     }
1088 
1089     std::lock_guard<std::mutex> lock(preferenceMutex_);
1090     bool storeDBResult = true;
1091     storeDBResult = preferncesDB_->IsSmartReminderEnabled(deviceType, enabled);
1092     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
1093 }
1094 
InitSettingFromDisturbDB(int32_t userId)1095 void NotificationPreferences::InitSettingFromDisturbDB(int32_t userId)
1096 {
1097     ANS_LOGI("%{public}s userId is %{public}d", __FUNCTION__, userId);
1098     std::lock_guard<std::mutex> lock(preferenceMutex_);
1099     if (preferncesDB_ != nullptr) {
1100         preferncesDB_->ParseFromDisturbeDB(preferencesInfo_, userId);
1101     }
1102 }
1103 
RemoveSettings(int32_t userId)1104 void NotificationPreferences::RemoveSettings(int32_t userId)
1105 {
1106     ANS_LOGD("%{public}s", __FUNCTION__);
1107     std::lock_guard<std::mutex> lock(preferenceMutex_);
1108     preferencesInfo_.RemoveNotificationEnable(userId);
1109     preferencesInfo_.RemoveDoNotDisturbDate(userId);
1110     if (preferncesDB_ != nullptr) {
1111         preferncesDB_->RemoveNotificationEnable(userId);
1112         preferncesDB_->RemoveDoNotDisturbDate(userId);
1113         preferncesDB_->DropUserTable(userId);
1114     }
1115 }
1116 
CheckApiCompatibility(const sptr<NotificationBundleOption> & bundleOption) const1117 bool NotificationPreferences::CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption) const
1118 {
1119     ANS_LOGD("%{public}s", __FUNCTION__);
1120     std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
1121     if (bundleManager == nullptr) {
1122         return false;
1123     }
1124     return bundleManager->CheckApiCompatibility(bundleOption);
1125 }
1126 
RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> & bundleOption)1127 void NotificationPreferences::RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> &bundleOption)
1128 {
1129     ANS_LOGE("%{public}s", __FUNCTION__);
1130     if (preferncesDB_ != nullptr && bundleOption != nullptr) {
1131         preferncesDB_->RemoveAnsBundleDbInfo(bundleOption->GetBundleName(), bundleOption->GetUid());
1132     }
1133 }
1134 
RemoveEnabledDbByBundle(const sptr<NotificationBundleOption> & bundleOption)1135 void NotificationPreferences::RemoveEnabledDbByBundle(const sptr<NotificationBundleOption> &bundleOption)
1136 {
1137     ANS_LOGE("%{public}s", __FUNCTION__);
1138     if (preferncesDB_ != nullptr && bundleOption != nullptr) {
1139         std::lock_guard<std::mutex> lock(preferenceMutex_);
1140         preferncesDB_->RemoveEnabledDbByBundleName(bundleOption->GetBundleName(), bundleOption->GetUid());
1141     }
1142 }
1143 
GetBundleSoundPermission(bool & allPackage,std::set<std::string> & bundleNames)1144 bool NotificationPreferences::GetBundleSoundPermission(bool &allPackage, std::set<std::string> &bundleNames)
1145 {
1146     ANS_LOGD("%{public}s", __FUNCTION__);
1147     std::string value = "";
1148     int32_t userId = -1;
1149     OsAccountManagerHelper::GetInstance().GetCurrentCallingUserId(userId);
1150     if (GetKvFromDb("RING_TRUSTLIST_PKG", value, userId) != ERR_OK) {
1151         ANS_LOGD("Get bundle sound permission failed.");
1152         return false;
1153     }
1154 
1155     ANS_LOGD("The bundle permission is :%{public}s.", value.c_str());
1156     nlohmann::json jsonPermission = nlohmann::json::parse(value, nullptr, false);
1157     if (jsonPermission.is_null() || jsonPermission.empty()) {
1158         ANS_LOGE("Invalid JSON object");
1159         return false;
1160     }
1161     if (jsonPermission.is_discarded() || !jsonPermission.is_array()) {
1162         ANS_LOGE("Parse bundle permission failed due to data is discarded or not array");
1163         return false;
1164     }
1165 
1166     for (const auto &item : jsonPermission) {
1167         bundleNames.insert(item);
1168         if (item == "ALL_PKG") {
1169             allPackage = true;
1170         }
1171     }
1172     return true;
1173 }
1174 
SetKvToDb(const std::string & key,const std::string & value,const int32_t & userId)1175 int32_t NotificationPreferences::SetKvToDb(
1176     const std::string &key, const std::string &value, const int32_t &userId)
1177 {
1178     if (preferncesDB_ == nullptr) {
1179         return ERR_ANS_SERVICE_NOT_READY;
1180     }
1181     return preferncesDB_->SetKvToDb(key, value, userId);
1182 }
1183 
SetByteToDb(const std::string & key,const std::vector<uint8_t> & value,const int32_t & userId)1184 int32_t NotificationPreferences::SetByteToDb(
1185     const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId)
1186 {
1187     if (preferncesDB_ == nullptr) {
1188         return ERR_ANS_SERVICE_NOT_READY;
1189     }
1190     return preferncesDB_->SetByteToDb(key, value, userId);
1191 }
1192 
GetKvFromDb(const std::string & key,std::string & value,const int32_t & userId)1193 int32_t NotificationPreferences::GetKvFromDb(
1194     const std::string &key, std::string &value, const int32_t &userId)
1195 {
1196     if (preferncesDB_ == nullptr) {
1197         return ERR_ANS_SERVICE_NOT_READY;
1198     }
1199     return preferncesDB_->GetKvFromDb(key, value, userId);
1200 }
1201 
GetByteFromDb(const std::string & key,std::vector<uint8_t> & value,const int32_t & userId)1202 int32_t NotificationPreferences::GetByteFromDb(
1203     const std::string &key, std::vector<uint8_t> &value, const int32_t &userId)
1204 {
1205     if (preferncesDB_ == nullptr) {
1206         return ERR_ANS_SERVICE_NOT_READY;
1207     }
1208     return preferncesDB_->GetByteFromDb(key, value, userId);
1209 }
1210 
GetBatchKvsFromDb(const std::string & key,std::unordered_map<std::string,std::string> & values,const int32_t & userId)1211 int32_t NotificationPreferences::GetBatchKvsFromDb(
1212     const std::string &key, std::unordered_map<std::string, std::string> &values, const int32_t &userId)
1213 {
1214     if (preferncesDB_ == nullptr) {
1215         return ERR_ANS_SERVICE_NOT_READY;
1216     }
1217     return preferncesDB_->GetBatchKvsFromDb(key, values, userId);
1218 }
1219 
DeleteKvFromDb(const std::string & key,const int32_t & userId)1220 int32_t NotificationPreferences::DeleteKvFromDb(const std::string &key, const int32_t &userId)
1221 {
1222     if (preferncesDB_ == nullptr) {
1223         return ERR_ANS_SERVICE_NOT_READY;
1224     }
1225     return preferncesDB_->DeleteKvFromDb(key, userId);
1226 }
1227 
DeleteBatchKvFromDb(const std::vector<std::string> & keys,const int32_t & userId)1228 int32_t NotificationPreferences::DeleteBatchKvFromDb(const std::vector<std::string> &keys,  const int32_t &userId)
1229 {
1230     if (preferncesDB_ == nullptr) {
1231         return ERR_ANS_SERVICE_NOT_READY;
1232     }
1233     return preferncesDB_->DeleteBatchKvFromDb(keys, userId);
1234 }
1235 
IsAgentRelationship(const std::string & agentBundleName,const std::string & sourceBundleName)1236 bool NotificationPreferences::IsAgentRelationship(const std::string &agentBundleName,
1237     const std::string &sourceBundleName)
1238 {
1239     if (AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) {
1240         ANS_LOGD("Client has agent permission.");
1241         return true;
1242     }
1243 
1244     if (preferncesDB_ == nullptr) {
1245         ANS_LOGD("perferencdDb is null.");
1246         return false;
1247     }
1248 
1249     return preferncesDB_->IsAgentRelationship(agentBundleName, sourceBundleName);
1250 }
1251 
GetAdditionalConfig(const std::string & key)1252 std::string NotificationPreferences::GetAdditionalConfig(const std::string &key)
1253 {
1254     if (preferncesDB_ == nullptr) {
1255         return "";
1256     }
1257     return preferncesDB_->GetAdditionalConfig(key);
1258 }
1259 }  // namespace Notification
1260 }  // namespace OHOS
1261