• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
20 #include "ans_const_define.h"
21 #include "ans_inner_errors.h"
22 #include "ans_log_wrapper.h"
23 #include "bundle_manager_helper.h"
24 #include "hitrace_meter_adapter.h"
25 #include "nlohmann/json.hpp"
26 #include "os_account_manager.h"
27 
28 namespace OHOS {
29 namespace Notification {
NotificationPreferences()30 NotificationPreferences::NotificationPreferences()
31 {
32     preferncesDB_ = std::make_unique<NotificationPreferencesDatabase>();
33     InitSettingFromDisturbDB();
34 }
35 
~NotificationPreferences()36 NotificationPreferences::~NotificationPreferences()
37 {}
38 
GetInstance()39 NotificationPreferences &NotificationPreferences::GetInstance()
40 {
41     return DelayedRefSingleton<NotificationPreferences>::GetInstance();
42 }
43 
AddNotificationSlots(const sptr<NotificationBundleOption> & bundleOption,const std::vector<sptr<NotificationSlot>> & slots)44 ErrCode NotificationPreferences::AddNotificationSlots(
45     const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
46 {
47     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
48     ANS_LOGD("%{public}s", __FUNCTION__);
49     if (bundleOption == nullptr || bundleOption->GetBundleName().empty() || slots.empty()) {
50         return ERR_ANS_INVALID_PARAM;
51     }
52     std::lock_guard<std::mutex> lock(preferenceMutex_);
53     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
54     ErrCode result = ERR_OK;
55     for (auto slot : slots) {
56         result = CheckSlotForCreateSlot(bundleOption, slot, preferencesInfo);
57         if (result != ERR_OK) {
58             return result;
59         }
60     }
61 
62     ANS_LOGE("ffrt: add slot to db!");
63     if (result == ERR_OK &&
64         (!preferncesDB_->PutSlotsToDisturbeDB(bundleOption->GetBundleName(), bundleOption->GetUid(), slots))) {
65         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
66     }
67 
68     if (result == ERR_OK) {
69         preferencesInfo_ = preferencesInfo;
70     }
71     return result;
72 }
73 
AddNotificationBundleProperty(const sptr<NotificationBundleOption> & bundleOption)74 ErrCode NotificationPreferences::AddNotificationBundleProperty(const sptr<NotificationBundleOption> &bundleOption)
75 {
76     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
77         return ERR_ANS_INVALID_PARAM;
78     }
79     std::lock_guard<std::mutex> lock(preferenceMutex_);
80     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
81     NotificationPreferencesInfo::BundleInfo bundleInfo;
82     preferencesInfo.SetBundleInfo(bundleInfo);
83     ErrCode result = ERR_OK;
84     if (preferncesDB_->PutBundlePropertyToDisturbeDB(bundleInfo)) {
85         preferencesInfo_ = preferencesInfo;
86     } else {
87         result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
88     }
89     ANS_LOGD("AddNotificationBundleProperty.result: %{public}d", result);
90     return result;
91 }
92 
RemoveNotificationSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType)93 ErrCode NotificationPreferences::RemoveNotificationSlot(
94     const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType)
95 {
96     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
97     ANS_LOGD("%{public}s", __FUNCTION__);
98     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
99         return ERR_ANS_INVALID_PARAM;
100     }
101     std::lock_guard<std::mutex> lock(preferenceMutex_);
102     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
103     ErrCode result = ERR_OK;
104     result = CheckSlotForRemoveSlot(bundleOption, slotType, preferencesInfo);
105     if (result == ERR_OK && (!preferncesDB_->RemoveSlotFromDisturbeDB(GenerateBundleKey(bundleOption), slotType))) {
106         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
107     }
108 
109     if (result == ERR_OK) {
110         preferencesInfo_ = preferencesInfo;
111     }
112     return result;
113 }
114 
RemoveNotificationAllSlots(const sptr<NotificationBundleOption> & bundleOption)115 ErrCode NotificationPreferences::RemoveNotificationAllSlots(const sptr<NotificationBundleOption> &bundleOption)
116 {
117     ANS_LOGD("%{public}s", __FUNCTION__);
118     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
119         return ERR_ANS_INVALID_PARAM;
120     }
121     std::lock_guard<std::mutex> lock(preferenceMutex_);
122     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
123     ErrCode result = ERR_OK;
124     NotificationPreferencesInfo::BundleInfo bundleInfo;
125     if (preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
126         bundleInfo.RemoveAllSlots();
127         preferencesInfo.SetBundleInfo(bundleInfo);
128         if (!preferncesDB_->RemoveAllSlotsFromDisturbeDB(GenerateBundleKey(bundleOption))) {
129             result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
130         }
131     } else {
132         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
133     }
134 
135     if (result == ERR_OK) {
136         ANS_LOGD("result is ERR_OK");
137         preferencesInfo_ = preferencesInfo;
138     }
139     return result;
140 }
141 
RemoveNotificationForBundle(const sptr<NotificationBundleOption> & bundleOption)142 ErrCode NotificationPreferences::RemoveNotificationForBundle(const sptr<NotificationBundleOption> &bundleOption)
143 {
144     ANS_LOGD("%{public}s", __FUNCTION__);
145     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
146         return ERR_ANS_INVALID_PARAM;
147     }
148     std::lock_guard<std::mutex> lock(preferenceMutex_);
149     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
150 
151     ErrCode result = ERR_OK;
152     if (preferencesInfo.IsExsitBundleInfo(bundleOption)) {
153         preferencesInfo.RemoveBundleInfo(bundleOption);
154         if (!preferncesDB_->RemoveBundleFromDisturbeDB(GenerateBundleKey(bundleOption))) {
155             result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
156         }
157     } else {
158         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
159     }
160 
161     if (result == ERR_OK) {
162         preferencesInfo_ = preferencesInfo;
163     }
164 
165     return result;
166 }
167 
UpdateNotificationSlots(const sptr<NotificationBundleOption> & bundleOption,const std::vector<sptr<NotificationSlot>> & slots)168 ErrCode NotificationPreferences::UpdateNotificationSlots(
169     const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
170 {
171     ANS_LOGD("%{public}s", __FUNCTION__);
172     if (bundleOption == nullptr || bundleOption->GetBundleName().empty() || slots.empty()) {
173         return ERR_ANS_INVALID_PARAM;
174     }
175     std::lock_guard<std::mutex> lock(preferenceMutex_);
176     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
177     ErrCode result = ERR_OK;
178     for (auto slotIter : slots) {
179         result = CheckSlotForUpdateSlot(bundleOption, slotIter, preferencesInfo);
180         if (result != ERR_OK) {
181             return result;
182         }
183     }
184 
185     if ((result == ERR_OK) &&
186         (!preferncesDB_->PutSlotsToDisturbeDB(bundleOption->GetBundleName(), bundleOption->GetUid(), slots))) {
187         return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
188     }
189 
190     if (result == ERR_OK) {
191         preferencesInfo_ = preferencesInfo;
192     }
193 
194     return result;
195 }
196 
GetNotificationSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & type,sptr<NotificationSlot> & slot)197 ErrCode NotificationPreferences::GetNotificationSlot(const sptr<NotificationBundleOption> &bundleOption,
198     const NotificationConstant::SlotType &type, sptr<NotificationSlot> &slot)
199 {
200     ANS_LOGD("%{public}s", __FUNCTION__);
201     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
202         return ERR_ANS_INVALID_PARAM;
203     }
204 
205     ErrCode result = ERR_OK;
206     NotificationPreferencesInfo::BundleInfo bundleInfo;
207     std::lock_guard<std::mutex> lock(preferenceMutex_);
208     if (preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) {
209         if (!bundleInfo.GetSlot(type, slot)) {
210             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
211         }
212     } else {
213         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
214     }
215     ANS_LOGD("%{public}s status  = %{public}d ", __FUNCTION__, result);
216     return result;
217 }
218 
GetNotificationAllSlots(const sptr<NotificationBundleOption> & bundleOption,std::vector<sptr<NotificationSlot>> & slots)219 ErrCode NotificationPreferences::GetNotificationAllSlots(
220     const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlot>> &slots)
221 {
222     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
223         return ERR_ANS_INVALID_PARAM;
224     }
225 
226     ErrCode result = ERR_OK;
227     NotificationPreferencesInfo::BundleInfo bundleInfo;
228     std::lock_guard<std::mutex> lock(preferenceMutex_);
229     if (preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) {
230         bundleInfo.GetAllSlots(slots);
231     } else {
232         ANS_LOGW("Notification bundle does not exsit.");
233         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
234     }
235 
236     return result;
237 }
238 
GetNotificationSlotsNumForBundle(const sptr<NotificationBundleOption> & bundleOption,uint64_t & num)239 ErrCode NotificationPreferences::GetNotificationSlotsNumForBundle(
240     const sptr<NotificationBundleOption> &bundleOption, uint64_t &num)
241 {
242     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
243         return ERR_ANS_INVALID_PARAM;
244     }
245 
246     ErrCode result = ERR_OK;
247     NotificationPreferencesInfo::BundleInfo bundleInfo;
248     std::lock_guard<std::mutex> lock(preferenceMutex_);
249     if (preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) {
250         num = static_cast<uint64_t>(bundleInfo.GetAllSlotsSize());
251     } else {
252         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
253     }
254     return result;
255 }
256 
GetNotificationSlotFlagsForBundle(const sptr<NotificationBundleOption> & bundleOption,uint32_t & slotFlags)257 ErrCode NotificationPreferences::GetNotificationSlotFlagsForBundle(
258     const sptr<NotificationBundleOption> &bundleOption, uint32_t &slotFlags)
259 {
260     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
261         return ERR_ANS_INVALID_PARAM;
262     }
263 
264     return GetBundleProperty(bundleOption, BundleType::BUNDLE_SLOTFLGS_TYPE, slotFlags);
265 }
266 
267 
SetNotificationSlotFlagsForBundle(const sptr<NotificationBundleOption> & bundleOption,uint32_t slotFlags)268 ErrCode NotificationPreferences::SetNotificationSlotFlagsForBundle(
269     const sptr<NotificationBundleOption> &bundleOption, uint32_t slotFlags)
270 {
271     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
272         return ERR_ANS_INVALID_PARAM;
273     }
274 
275     std::lock_guard<std::mutex> lock(preferenceMutex_);
276     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
277     ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_SLOTFLGS_TYPE, slotFlags);
278     if (result == ERR_OK) {
279         preferencesInfo_ = preferencesInfo;
280     }
281     return result;
282 }
283 
IsShowBadge(const sptr<NotificationBundleOption> & bundleOption,bool & enable)284 ErrCode NotificationPreferences::IsShowBadge(const sptr<NotificationBundleOption> &bundleOption, bool &enable)
285 {
286     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
287         return ERR_ANS_INVALID_PARAM;
288     }
289     return GetBundleProperty(bundleOption, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable);
290 }
291 
SetShowBadge(const sptr<NotificationBundleOption> & bundleOption,const bool enable)292 ErrCode NotificationPreferences::SetShowBadge(const sptr<NotificationBundleOption> &bundleOption, const bool enable)
293 {
294     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
295         return ERR_ANS_INVALID_PARAM;
296     }
297     std::lock_guard<std::mutex> lock(preferenceMutex_);
298     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
299     ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable);
300     if (result == ERR_OK) {
301         preferencesInfo_ = preferencesInfo;
302     }
303     return result;
304 }
305 
GetImportance(const sptr<NotificationBundleOption> & bundleOption,int32_t & importance)306 ErrCode NotificationPreferences::GetImportance(const sptr<NotificationBundleOption> &bundleOption, int32_t &importance)
307 {
308     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
309         return ERR_ANS_INVALID_PARAM;
310     }
311 
312     return GetBundleProperty(bundleOption, BundleType::BUNDLE_IMPORTANCE_TYPE, importance);
313 }
314 
315 
SetImportance(const sptr<NotificationBundleOption> & bundleOption,const int32_t & importance)316 ErrCode NotificationPreferences::SetImportance(
317     const sptr<NotificationBundleOption> &bundleOption, const int32_t &importance)
318 {
319     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
320         return ERR_ANS_INVALID_PARAM;
321     }
322     std::lock_guard<std::mutex> lock(preferenceMutex_);
323     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
324     ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_IMPORTANCE_TYPE, importance);
325     if (result == ERR_OK) {
326         preferencesInfo_ = preferencesInfo;
327     }
328     return result;
329 }
330 
GetTotalBadgeNums(const sptr<NotificationBundleOption> & bundleOption,int32_t & totalBadgeNum)331 ErrCode NotificationPreferences::GetTotalBadgeNums(
332     const sptr<NotificationBundleOption> &bundleOption, int32_t &totalBadgeNum)
333 {
334     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
335         return ERR_ANS_INVALID_PARAM;
336     }
337     return GetBundleProperty(bundleOption, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum);
338 }
339 
SetTotalBadgeNums(const sptr<NotificationBundleOption> & bundleOption,const int32_t num)340 ErrCode NotificationPreferences::SetTotalBadgeNums(
341     const sptr<NotificationBundleOption> &bundleOption, const int32_t num)
342 {
343     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
344         return ERR_ANS_INVALID_PARAM;
345     }
346     std::lock_guard<std::mutex> lock(preferenceMutex_);
347     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
348     ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, num);
349     if (result == ERR_OK) {
350         preferencesInfo_ = preferencesInfo;
351     }
352     return result;
353 }
354 
GetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> & bundleOption,bool & enabled)355 ErrCode NotificationPreferences::GetNotificationsEnabledForBundle(
356     const sptr<NotificationBundleOption> &bundleOption, bool &enabled)
357 {
358     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
359         return ERR_ANS_INVALID_PARAM;
360     }
361     return GetBundleProperty(bundleOption, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled);
362 }
363 
SetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> & bundleOption,const bool enabled)364 ErrCode NotificationPreferences::SetNotificationsEnabledForBundle(
365     const sptr<NotificationBundleOption> &bundleOption, const bool enabled)
366 {
367     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
368     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
369         return ERR_ANS_INVALID_PARAM;
370     }
371 
372     std::lock_guard<std::mutex> lock(preferenceMutex_);
373     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
374     ErrCode result =
375         SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled);
376     if (result == ERR_OK) {
377         preferencesInfo_ = preferencesInfo;
378     }
379     return result;
380 }
381 
GetNotificationsEnabled(const int32_t & userId,bool & enabled)382 ErrCode NotificationPreferences::GetNotificationsEnabled(const int32_t &userId, bool &enabled)
383 {
384     if (userId <= SUBSCRIBE_USER_INIT) {
385         return ERR_ANS_INVALID_PARAM;
386     }
387 
388     ErrCode result = ERR_OK;
389     std::lock_guard<std::mutex> lock(preferenceMutex_);
390     if (!preferencesInfo_.GetEnabledAllNotification(userId, enabled)) {
391         result = ERR_ANS_INVALID_PARAM;
392     }
393     return result;
394 }
395 
SetNotificationsEnabled(const int32_t & userId,const bool & enabled)396 ErrCode NotificationPreferences::SetNotificationsEnabled(const int32_t &userId, const bool &enabled)
397 {
398     if (userId <= SUBSCRIBE_USER_INIT) {
399         return ERR_ANS_INVALID_PARAM;
400     }
401     std::lock_guard<std::mutex> lock(preferenceMutex_);
402     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
403     preferencesInfo.SetEnabledAllNotification(userId, enabled);
404     ErrCode result = ERR_OK;
405     if (!preferncesDB_->PutNotificationsEnabled(userId, enabled)) {
406         result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
407     }
408 
409     if (result == ERR_OK) {
410         preferencesInfo_ = preferencesInfo;
411     }
412     return result;
413 }
414 
GetHasPoppedDialog(const sptr<NotificationBundleOption> & bundleOption,bool & hasPopped)415 ErrCode NotificationPreferences::GetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool &hasPopped)
416 {
417     if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
418         return ERR_ANS_INVALID_PARAM;
419     }
420     return GetBundleProperty(bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
421 }
422 
SetHasPoppedDialog(const sptr<NotificationBundleOption> & bundleOption,bool hasPopped)423 ErrCode NotificationPreferences::SetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool hasPopped)
424 {
425     if (bundleOption == nullptr) {
426         return ERR_ANS_INVALID_PARAM;
427     }
428     std::lock_guard<std::mutex> lock(preferenceMutex_);
429     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
430     ErrCode result = ERR_OK;
431     result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
432     if (result == ERR_OK) {
433         preferencesInfo_ = preferencesInfo;
434     }
435     return result;
436 }
437 
GetDoNotDisturbDate(const int32_t & userId,sptr<NotificationDoNotDisturbDate> & date)438 ErrCode NotificationPreferences::GetDoNotDisturbDate(const int32_t &userId,
439     sptr<NotificationDoNotDisturbDate> &date)
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     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
448     if (!preferencesInfo.GetDoNotDisturbDate(userId, date)) {
449         result = ERR_ANS_INVALID_PARAM;
450     }
451     return result;
452 }
453 
SetDoNotDisturbDate(const int32_t & userId,const sptr<NotificationDoNotDisturbDate> date)454 ErrCode NotificationPreferences::SetDoNotDisturbDate(const int32_t &userId,
455     const sptr<NotificationDoNotDisturbDate> date)
456 {
457     ANS_LOGE("enter.");
458     if (userId <= SUBSCRIBE_USER_INIT) {
459         return ERR_ANS_INVALID_PARAM;
460     }
461     std::lock_guard<std::mutex> lock(preferenceMutex_);
462     NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
463     preferencesInfo.SetDoNotDisturbDate(userId, date);
464 
465     ErrCode result = ERR_OK;
466     if (!preferncesDB_->PutDoNotDisturbDate(userId, date)) {
467         result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
468     }
469 
470     if (result == ERR_OK) {
471         preferencesInfo_ = preferencesInfo;
472     }
473     return result;
474 }
475 
ClearNotificationInRestoreFactorySettings()476 ErrCode NotificationPreferences::ClearNotificationInRestoreFactorySettings()
477 {
478     ErrCode result = ERR_OK;
479     std::lock_guard<std::mutex> lock(preferenceMutex_);
480     if (!preferncesDB_->RemoveAllDataFromDisturbeDB()) {
481         result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
482     }
483 
484     if (result == ERR_OK) {
485         preferencesInfo_ = NotificationPreferencesInfo();
486     }
487     return result;
488 }
489 
CheckSlotForCreateSlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot,NotificationPreferencesInfo & preferencesInfo) const490 ErrCode NotificationPreferences::CheckSlotForCreateSlot(const sptr<NotificationBundleOption> &bundleOption,
491     const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
492 {
493     if (slot == nullptr) {
494         ANS_LOGE("Notification slot is nullptr.");
495         return ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_NOT_EXIST;
496     }
497 
498     NotificationPreferencesInfo::BundleInfo bundleInfo;
499     if (!preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
500         bundleInfo.SetBundleName(bundleOption->GetBundleName());
501         bundleInfo.SetBundleUid(bundleOption->GetUid());
502         bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
503     }
504     bundleInfo.SetSlot(slot);
505     preferencesInfo.SetBundleInfo(bundleInfo);
506 
507     return ERR_OK;
508 }
509 
CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,NotificationPreferencesInfo & preferencesInfo) const510 ErrCode NotificationPreferences::CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> &bundleOption,
511     const NotificationConstant::SlotType &slotType, NotificationPreferencesInfo &preferencesInfo) const
512 {
513     ErrCode result = ERR_OK;
514     NotificationPreferencesInfo::BundleInfo bundleInfo;
515     if (preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
516         if (bundleInfo.IsExsitSlot(slotType)) {
517             bundleInfo.RemoveSlot(slotType);
518             preferencesInfo.SetBundleInfo(bundleInfo);
519         } else {
520             ANS_LOGE("Notification slot type does not exsited.");
521             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
522         }
523     } else {
524         ANS_LOGW("Notification bundle does not exsit.");
525         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
526     }
527     return result;
528 }
529 
CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot,NotificationPreferencesInfo & preferencesInfo) const530 ErrCode NotificationPreferences::CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> &bundleOption,
531     const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
532 {
533     if (slot == nullptr) {
534         ANS_LOGE("Notification slot is nullptr.");
535         return ERR_ANS_INVALID_PARAM;
536     }
537 
538     ErrCode result = ERR_OK;
539     NotificationPreferencesInfo::BundleInfo bundleInfo;
540     if (preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
541         if (bundleInfo.IsExsitSlot(slot->GetType())) {
542             bundleInfo.SetBundleName(bundleOption->GetBundleName());
543             bundleInfo.SetBundleUid(bundleOption->GetUid());
544             bundleInfo.SetSlot(slot);
545             preferencesInfo.SetBundleInfo(bundleInfo);
546         } else {
547             ANS_LOGE("Notification slot type does not exist.");
548             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
549         }
550     } else {
551         ANS_LOGW("Notification bundle does not exsit.");
552         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
553     }
554 
555     return result;
556 }
557 
558 template <typename T>
SetBundleProperty(NotificationPreferencesInfo & preferencesInfo,const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,const T & value)559 ErrCode NotificationPreferences::SetBundleProperty(NotificationPreferencesInfo &preferencesInfo,
560     const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
561 {
562     ErrCode result = ERR_OK;
563     NotificationPreferencesInfo::BundleInfo bundleInfo;
564     if (!preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) {
565         bundleInfo.SetBundleName(bundleOption->GetBundleName());
566         bundleInfo.SetBundleUid(bundleOption->GetUid());
567         bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
568     }
569     result = SaveBundleProperty(bundleInfo, bundleOption, type, value);
570     preferencesInfo.SetBundleInfo(bundleInfo);
571 
572     return result;
573 }
574 
575 template <typename T>
SaveBundleProperty(NotificationPreferencesInfo::BundleInfo & bundleInfo,const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,const T & value)576 ErrCode NotificationPreferences::SaveBundleProperty(NotificationPreferencesInfo::BundleInfo &bundleInfo,
577     const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
578 {
579     bool storeDBResult = true;
580     switch (type) {
581         case BundleType::BUNDLE_IMPORTANCE_TYPE:
582             bundleInfo.SetImportance(value);
583             storeDBResult = preferncesDB_->PutImportance(bundleInfo, value);
584             break;
585         case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
586             bundleInfo.SetBadgeTotalNum(value);
587             storeDBResult = preferncesDB_->PutTotalBadgeNums(bundleInfo, value);
588             break;
589         case BundleType::BUNDLE_SHOW_BADGE_TYPE:
590             bundleInfo.SetIsShowBadge(value);
591             storeDBResult = preferncesDB_->PutShowBadge(bundleInfo, value);
592             break;
593         case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
594             bundleInfo.SetEnableNotification(value);
595             storeDBResult = preferncesDB_->PutNotificationsEnabledForBundle(bundleInfo, value);
596             break;
597         case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
598             ANS_LOGD("Into BUNDLE_POPPED_DIALOG_TYPE:SetHasPoppedDialog.");
599             bundleInfo.SetHasPoppedDialog(value);
600             storeDBResult = preferncesDB_->PutHasPoppedDialog(bundleInfo, value);
601             break;
602         case BundleType::BUNDLE_SLOTFLGS_TYPE:
603             ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:SetSlotFlags.");
604             bundleInfo.SetSlotFlags(value);
605             storeDBResult = preferncesDB_->PutSlotFlags(bundleInfo, value);
606             break;
607         default:
608             break;
609     }
610     return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
611 }
612 
613 template <typename T>
GetBundleProperty(const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,T & value)614 ErrCode NotificationPreferences::GetBundleProperty(
615     const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, T &value)
616 {
617     ErrCode result = ERR_OK;
618     NotificationPreferencesInfo::BundleInfo bundleInfo;
619     std::lock_guard<std::mutex> lock(preferenceMutex_);
620     if (preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) {
621         switch (type) {
622             case BundleType::BUNDLE_IMPORTANCE_TYPE:
623                 value = bundleInfo.GetImportance();
624                 break;
625             case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
626                 value = bundleInfo.GetBadgeTotalNum();
627                 break;
628             case BundleType::BUNDLE_SHOW_BADGE_TYPE:
629                 value = bundleInfo.GetIsShowBadge();
630                 break;
631             case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
632                 value = bundleInfo.GetEnableNotification();
633                 break;
634             case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
635                 ANS_LOGD("Into BUNDLE_POPPED_DIALOG_TYPE:GetHasPoppedDialog.");
636                 value = bundleInfo.GetHasPoppedDialog();
637                 break;
638             case BundleType::BUNDLE_SLOTFLGS_TYPE:
639                 value = bundleInfo.GetSlotFlags();
640                 ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:GetSlotFlags.");
641                 break;
642             default:
643                 result = ERR_ANS_INVALID_PARAM;
644                 break;
645         }
646     } else {
647         ANS_LOGW("Notification bundle does not exsit.");
648         result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
649     }
650     return result;
651 }
652 
GenerateBundleKey(const sptr<NotificationBundleOption> & bundleOption) const653 std::string NotificationPreferences::GenerateBundleKey(const sptr<NotificationBundleOption> &bundleOption) const
654 {
655     return bundleOption->GetBundleName().append(std::to_string(bundleOption->GetUid()));
656 }
657 
GetTemplateSupported(const std::string & templateName,bool & support)658 ErrCode NotificationPreferences::GetTemplateSupported(const std::string& templateName, bool &support)
659 {
660     if (templateName.length() == 0) {
661         ANS_LOGE("template name is null.");
662         return ERR_ANS_INVALID_PARAM;
663     }
664 
665     std::ifstream inFile;
666     inFile.open(DEFAULT_TEMPLATE_PATH.c_str(), std::ios::in);
667     if (!inFile.is_open()) {
668         ANS_LOGE("read template config error.");
669         return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
670     }
671 
672     nlohmann::json jsonObj;
673     inFile >> jsonObj;
674     if (jsonObj.is_discarded()) {
675         ANS_LOGE("template json discarded error.");
676         inFile.close();
677         return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
678     }
679 
680     if (jsonObj.contains(templateName)) {
681         support = true;
682     }
683 
684     jsonObj.clear();
685     inFile.close();
686     return ERR_OK;
687 }
688 
InitSettingFromDisturbDB()689 void NotificationPreferences::InitSettingFromDisturbDB()
690 {
691     ANS_LOGD("%{public}s", __FUNCTION__);
692     std::lock_guard<std::mutex> lock(preferenceMutex_);
693     if (preferncesDB_ != nullptr) {
694         preferncesDB_->ParseFromDisturbeDB(preferencesInfo_);
695     }
696 }
697 
RemoveSettings(int32_t userId)698 void NotificationPreferences::RemoveSettings(int32_t userId)
699 {
700     ANS_LOGD("%{public}s", __FUNCTION__);
701 
702     {
703         std::lock_guard<std::mutex> lock(preferenceMutex_);
704         preferencesInfo_.RemoveNotificationEnable(userId);
705         preferencesInfo_.RemoveDoNotDisturbDate(userId);
706     }
707 
708     if (preferncesDB_ != nullptr) {
709         preferncesDB_->RemoveNotificationEnable(userId);
710         preferncesDB_->RemoveDoNotDisturbDate(userId);
711     }
712 }
713 
CheckApiCompatibility(const sptr<NotificationBundleOption> & bundleOption) const714 bool NotificationPreferences::CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption) const
715 {
716     ANS_LOGD("%{public}s", __FUNCTION__);
717     std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
718     if (bundleManager == nullptr) {
719         return false;
720     }
721     return bundleManager->CheckApiCompatibility(bundleOption);
722 }
723 
RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> & bundleOption)724 void NotificationPreferences::RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> &bundleOption)
725 {
726     ANS_LOGE("%{public}s", __FUNCTION__);
727     if (preferncesDB_ != nullptr && bundleOption != nullptr) {
728         preferncesDB_->RemoveAnsBundleDbInfo(bundleOption->GetBundleName(), bundleOption->GetUid());
729     }
730 }
731 
SetKvToDb(const std::string & key,const std::string & value)732 int32_t NotificationPreferences::SetKvToDb(
733     const std::string &key, const std::string &value)
734 {
735     if (preferncesDB_ == nullptr) {
736         return ERR_ANS_SERVICE_NOT_READY;
737     }
738     return preferncesDB_->SetKvToDb(key, value);
739 }
740 
GetKvFromDb(const std::string & key,std::string & value)741 int32_t NotificationPreferences::GetKvFromDb(
742     const std::string &key, std::string &value)
743 {
744     if (preferncesDB_ == nullptr) {
745         return ERR_ANS_SERVICE_NOT_READY;
746     }
747     return preferncesDB_->GetKvFromDb(key, value);
748 }
749 
GetBatchKvsFromDb(const std::string & key,std::unordered_map<std::string,std::string> & values)750 int32_t NotificationPreferences::GetBatchKvsFromDb(
751     const std::string &key, std::unordered_map<std::string, std::string> &values)
752 {
753     if (preferncesDB_ == nullptr) {
754         return ERR_ANS_SERVICE_NOT_READY;
755     }
756     return preferncesDB_->GetBatchKvsFromDb(key, values);
757 }
758 
DeleteKvFromDb(const std::string & key)759 int32_t NotificationPreferences::DeleteKvFromDb(const std::string &key)
760 {
761     if (preferncesDB_ == nullptr) {
762         return ERR_ANS_SERVICE_NOT_READY;
763     }
764     return preferncesDB_->DeleteKvFromDb(key);
765 }
766 }  // namespace Notification
767 }  // namespace OHOS
768