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