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 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
80 std::lock_guard<std::mutex> lock(preferenceMutex_);
81 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
82 NotificationPreferencesInfo::BundleInfo bundleInfo;
83 preferencesInfo.SetBundleInfo(bundleInfo);
84 ErrCode result = ERR_OK;
85 if (preferncesDB_->PutBundlePropertyToDisturbeDB(bundleInfo)) {
86 preferencesInfo_ = preferencesInfo;
87 } else {
88 result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
89 }
90
91 return result;
92 }
93
RemoveNotificationSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType)94 ErrCode NotificationPreferences::RemoveNotificationSlot(
95 const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType)
96 {
97 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
98 ANS_LOGD("%{public}s", __FUNCTION__);
99 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
100 return ERR_ANS_INVALID_PARAM;
101 }
102 std::lock_guard<std::mutex> lock(preferenceMutex_);
103 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
104 ErrCode result = ERR_OK;
105 result = CheckSlotForRemoveSlot(bundleOption, slotType, preferencesInfo);
106 if (result == ERR_OK && (!preferncesDB_->RemoveSlotFromDisturbeDB(GenerateBundleKey(bundleOption), slotType))) {
107 return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
108 }
109
110 if (result == ERR_OK) {
111 preferencesInfo_ = preferencesInfo;
112 }
113 return result;
114 }
115
RemoveNotificationAllSlots(const sptr<NotificationBundleOption> & bundleOption)116 ErrCode NotificationPreferences::RemoveNotificationAllSlots(const sptr<NotificationBundleOption> &bundleOption)
117 {
118 ANS_LOGD("%{public}s", __FUNCTION__);
119 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
120 return ERR_ANS_INVALID_PARAM;
121 }
122 std::lock_guard<std::mutex> lock(preferenceMutex_);
123 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
124 ErrCode result = ERR_OK;
125 NotificationPreferencesInfo::BundleInfo bundleInfo;
126 if (preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
127 bundleInfo.RemoveAllSlots();
128 preferencesInfo.SetBundleInfo(bundleInfo);
129 if (!preferncesDB_->RemoveAllSlotsFromDisturbeDB(GenerateBundleKey(bundleOption))) {
130 result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
131 }
132 } else {
133 result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
134 }
135
136 if (result == 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
IsShowBadge(const sptr<NotificationBundleOption> & bundleOption,bool & enable)257 ErrCode NotificationPreferences::IsShowBadge(const sptr<NotificationBundleOption> &bundleOption, bool &enable)
258 {
259 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
260 return ERR_ANS_INVALID_PARAM;
261 }
262 return GetBundleProperty(bundleOption, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable);
263 }
264
SetShowBadge(const sptr<NotificationBundleOption> & bundleOption,const bool enable)265 ErrCode NotificationPreferences::SetShowBadge(const sptr<NotificationBundleOption> &bundleOption, const bool enable)
266 {
267 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
268 return ERR_ANS_INVALID_PARAM;
269 }
270 std::lock_guard<std::mutex> lock(preferenceMutex_);
271 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
272 ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable);
273 if (result == ERR_OK) {
274 preferencesInfo_ = preferencesInfo;
275 }
276 return result;
277 }
278
GetImportance(const sptr<NotificationBundleOption> & bundleOption,int32_t & importance)279 ErrCode NotificationPreferences::GetImportance(const sptr<NotificationBundleOption> &bundleOption, int32_t &importance)
280 {
281 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
282 return ERR_ANS_INVALID_PARAM;
283 }
284
285 return GetBundleProperty(bundleOption, BundleType::BUNDLE_IMPORTANCE_TYPE, importance);
286 }
287
SetImportance(const sptr<NotificationBundleOption> & bundleOption,const int32_t & importance)288 ErrCode NotificationPreferences::SetImportance(
289 const sptr<NotificationBundleOption> &bundleOption, const int32_t &importance)
290 {
291 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
292 return ERR_ANS_INVALID_PARAM;
293 }
294 std::lock_guard<std::mutex> lock(preferenceMutex_);
295 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
296 ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_IMPORTANCE_TYPE, importance);
297 if (result == ERR_OK) {
298 preferencesInfo_ = preferencesInfo;
299 }
300 return result;
301 }
302
GetTotalBadgeNums(const sptr<NotificationBundleOption> & bundleOption,int32_t & totalBadgeNum)303 ErrCode NotificationPreferences::GetTotalBadgeNums(
304 const sptr<NotificationBundleOption> &bundleOption, int32_t &totalBadgeNum)
305 {
306 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
307 return ERR_ANS_INVALID_PARAM;
308 }
309 return GetBundleProperty(bundleOption, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum);
310 }
311
SetTotalBadgeNums(const sptr<NotificationBundleOption> & bundleOption,const int32_t num)312 ErrCode NotificationPreferences::SetTotalBadgeNums(
313 const sptr<NotificationBundleOption> &bundleOption, const int32_t num)
314 {
315 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
316 return ERR_ANS_INVALID_PARAM;
317 }
318 std::lock_guard<std::mutex> lock(preferenceMutex_);
319 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
320 ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, num);
321 if (result == ERR_OK) {
322 preferencesInfo_ = preferencesInfo;
323 }
324 return result;
325 }
326
GetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> & bundleOption,bool & enabled)327 ErrCode NotificationPreferences::GetNotificationsEnabledForBundle(
328 const sptr<NotificationBundleOption> &bundleOption, bool &enabled)
329 {
330 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
331 return ERR_ANS_INVALID_PARAM;
332 }
333 return GetBundleProperty(bundleOption, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled);
334 }
335
SetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> & bundleOption,const bool enabled)336 ErrCode NotificationPreferences::SetNotificationsEnabledForBundle(
337 const sptr<NotificationBundleOption> &bundleOption, const bool enabled)
338 {
339 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
340 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
341 return ERR_ANS_INVALID_PARAM;
342 }
343
344 std::lock_guard<std::mutex> lock(preferenceMutex_);
345 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
346 ErrCode result =
347 SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled);
348 if (result == ERR_OK) {
349 preferencesInfo_ = preferencesInfo;
350 }
351 return result;
352 }
353
GetNotificationsEnabled(const int32_t & userId,bool & enabled)354 ErrCode NotificationPreferences::GetNotificationsEnabled(const int32_t &userId, bool &enabled)
355 {
356 if (userId <= SUBSCRIBE_USER_INIT) {
357 return ERR_ANS_INVALID_PARAM;
358 }
359
360 ErrCode result = ERR_OK;
361 std::lock_guard<std::mutex> lock(preferenceMutex_);
362 if (!preferencesInfo_.GetEnabledAllNotification(userId, enabled)) {
363 result = ERR_ANS_INVALID_PARAM;
364 }
365 return result;
366 }
367
SetNotificationsEnabled(const int32_t & userId,const bool & enabled)368 ErrCode NotificationPreferences::SetNotificationsEnabled(const int32_t &userId, const bool &enabled)
369 {
370 if (userId <= SUBSCRIBE_USER_INIT) {
371 return ERR_ANS_INVALID_PARAM;
372 }
373 std::lock_guard<std::mutex> lock(preferenceMutex_);
374 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
375 preferencesInfo.SetEnabledAllNotification(userId, enabled);
376 ErrCode result = ERR_OK;
377 if (!preferncesDB_->PutNotificationsEnabled(userId, enabled)) {
378 result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
379 }
380
381 if (result == ERR_OK) {
382 preferencesInfo_ = preferencesInfo;
383 }
384 return result;
385 }
386
GetHasPoppedDialog(const sptr<NotificationBundleOption> & bundleOption,bool & hasPopped)387 ErrCode NotificationPreferences::GetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool &hasPopped)
388 {
389 if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
390 return ERR_ANS_INVALID_PARAM;
391 }
392 return GetBundleProperty(bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
393 }
394
SetHasPoppedDialog(const sptr<NotificationBundleOption> & bundleOption,bool hasPopped)395 ErrCode NotificationPreferences::SetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool hasPopped)
396 {
397 if (bundleOption == nullptr) {
398 return ERR_ANS_INVALID_PARAM;
399 }
400 std::lock_guard<std::mutex> lock(preferenceMutex_);
401 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
402 ErrCode result = ERR_OK;
403 result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
404 if (result == ERR_OK) {
405 preferencesInfo_ = preferencesInfo;
406 }
407 return result;
408 }
409
GetDoNotDisturbDate(const int32_t & userId,sptr<NotificationDoNotDisturbDate> & date)410 ErrCode NotificationPreferences::GetDoNotDisturbDate(const int32_t &userId,
411 sptr<NotificationDoNotDisturbDate> &date)
412 {
413 if (userId <= SUBSCRIBE_USER_INIT) {
414 return ERR_ANS_INVALID_PARAM;
415 }
416
417 ErrCode result = ERR_OK;
418 std::lock_guard<std::mutex> lock(preferenceMutex_);
419 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
420 if (!preferencesInfo.GetDoNotDisturbDate(userId, date)) {
421 result = ERR_ANS_INVALID_PARAM;
422 }
423 return result;
424 }
425
SetDoNotDisturbDate(const int32_t & userId,const sptr<NotificationDoNotDisturbDate> date)426 ErrCode NotificationPreferences::SetDoNotDisturbDate(const int32_t &userId,
427 const sptr<NotificationDoNotDisturbDate> date)
428 {
429 ANS_LOGE("enter.");
430 if (userId <= SUBSCRIBE_USER_INIT) {
431 return ERR_ANS_INVALID_PARAM;
432 }
433 std::lock_guard<std::mutex> lock(preferenceMutex_);
434 NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
435 preferencesInfo.SetDoNotDisturbDate(userId, date);
436
437 ErrCode result = ERR_OK;
438 if (!preferncesDB_->PutDoNotDisturbDate(userId, date)) {
439 result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
440 }
441
442 if (result == ERR_OK) {
443 preferencesInfo_ = preferencesInfo;
444 }
445 return result;
446 }
447
ClearNotificationInRestoreFactorySettings()448 ErrCode NotificationPreferences::ClearNotificationInRestoreFactorySettings()
449 {
450 ErrCode result = ERR_OK;
451 std::lock_guard<std::mutex> lock(preferenceMutex_);
452 if (!preferncesDB_->RemoveAllDataFromDisturbeDB()) {
453 result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
454 }
455
456 if (result == ERR_OK) {
457 preferencesInfo_ = NotificationPreferencesInfo();
458 }
459 return result;
460 }
461
CheckSlotForCreateSlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot,NotificationPreferencesInfo & preferencesInfo) const462 ErrCode NotificationPreferences::CheckSlotForCreateSlot(const sptr<NotificationBundleOption> &bundleOption,
463 const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
464 {
465 if (slot == nullptr) {
466 ANS_LOGE("Notification slot is nullptr.");
467 return ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_NOT_EXIST;
468 }
469
470 NotificationPreferencesInfo::BundleInfo bundleInfo;
471 if (!preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
472 bundleInfo.SetBundleName(bundleOption->GetBundleName());
473 bundleInfo.SetBundleUid(bundleOption->GetUid());
474 bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
475 }
476 bundleInfo.SetSlot(slot);
477 preferencesInfo.SetBundleInfo(bundleInfo);
478
479 return ERR_OK;
480 }
481
CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,NotificationPreferencesInfo & preferencesInfo) const482 ErrCode NotificationPreferences::CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> &bundleOption,
483 const NotificationConstant::SlotType &slotType, NotificationPreferencesInfo &preferencesInfo) const
484 {
485 ErrCode result = ERR_OK;
486 NotificationPreferencesInfo::BundleInfo bundleInfo;
487 if (preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
488 if (bundleInfo.IsExsitSlot(slotType)) {
489 bundleInfo.RemoveSlot(slotType);
490 preferencesInfo.SetBundleInfo(bundleInfo);
491 } else {
492 ANS_LOGE("Notification slot type does not exsited.");
493 result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
494 }
495 } else {
496 ANS_LOGW("Notification bundle does not exsit.");
497 result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
498 }
499 return result;
500 }
501
CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot,NotificationPreferencesInfo & preferencesInfo) const502 ErrCode NotificationPreferences::CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> &bundleOption,
503 const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
504 {
505 if (slot == nullptr) {
506 ANS_LOGE("Notification slot is nullptr.");
507 return ERR_ANS_INVALID_PARAM;
508 }
509
510 ErrCode result = ERR_OK;
511 NotificationPreferencesInfo::BundleInfo bundleInfo;
512 if (preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
513 if (bundleInfo.IsExsitSlot(slot->GetType())) {
514 bundleInfo.SetBundleName(bundleOption->GetBundleName());
515 bundleInfo.SetBundleUid(bundleOption->GetUid());
516 bundleInfo.SetSlot(slot);
517 preferencesInfo.SetBundleInfo(bundleInfo);
518 } else {
519 ANS_LOGE("Notification slot type does not exist.");
520 result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
521 }
522 } else {
523 ANS_LOGW("Notification bundle does not exsit.");
524 result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
525 }
526
527 return result;
528 }
529
530 template <typename T>
SetBundleProperty(NotificationPreferencesInfo & preferencesInfo,const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,const T & value)531 ErrCode NotificationPreferences::SetBundleProperty(NotificationPreferencesInfo &preferencesInfo,
532 const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
533 {
534 ErrCode result = ERR_OK;
535 NotificationPreferencesInfo::BundleInfo bundleInfo;
536 if (!preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) {
537 bundleInfo.SetBundleName(bundleOption->GetBundleName());
538 bundleInfo.SetBundleUid(bundleOption->GetUid());
539 bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
540 }
541
542 result = SaveBundleProperty(bundleInfo, bundleOption, type, value);
543 preferencesInfo.SetBundleInfo(bundleInfo);
544
545 return result;
546 }
547
548 template <typename T>
SaveBundleProperty(NotificationPreferencesInfo::BundleInfo & bundleInfo,const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,const T & value)549 ErrCode NotificationPreferences::SaveBundleProperty(NotificationPreferencesInfo::BundleInfo &bundleInfo,
550 const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
551 {
552 bool storeDBResult = true;
553 switch (type) {
554 case BundleType::BUNDLE_IMPORTANCE_TYPE:
555 bundleInfo.SetImportance(value);
556 storeDBResult = preferncesDB_->PutImportance(bundleInfo, value);
557 break;
558 case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
559 bundleInfo.SetBadgeTotalNum(value);
560 storeDBResult = preferncesDB_->PutTotalBadgeNums(bundleInfo, value);
561 break;
562 case BundleType::BUNDLE_SHOW_BADGE_TYPE:
563 bundleInfo.SetIsShowBadge(value);
564 storeDBResult = preferncesDB_->PutShowBadge(bundleInfo, value);
565 break;
566 case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
567 bundleInfo.SetEnableNotification(value);
568 storeDBResult = preferncesDB_->PutNotificationsEnabledForBundle(bundleInfo, value);
569 break;
570 case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
571 bundleInfo.SetHasPoppedDialog(value);
572 storeDBResult = preferncesDB_->PutHasPoppedDialog(bundleInfo, value);
573 break;
574 default:
575 break;
576 }
577 return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
578 }
579
580 template <typename T>
GetBundleProperty(const sptr<NotificationBundleOption> & bundleOption,const BundleType & type,T & value)581 ErrCode NotificationPreferences::GetBundleProperty(
582 const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, T &value)
583 {
584 ErrCode result = ERR_OK;
585 NotificationPreferencesInfo::BundleInfo bundleInfo;
586 std::lock_guard<std::mutex> lock(preferenceMutex_);
587 if (preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) {
588 switch (type) {
589 case BundleType::BUNDLE_IMPORTANCE_TYPE:
590 value = bundleInfo.GetImportance();
591 break;
592 case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
593 value = bundleInfo.GetBadgeTotalNum();
594 break;
595 case BundleType::BUNDLE_SHOW_BADGE_TYPE:
596 value = bundleInfo.GetIsShowBadge();
597 break;
598 case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
599 value = bundleInfo.GetEnableNotification();
600 break;
601 case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
602 value = bundleInfo.GetHasPoppedDialog();
603 break;
604 default:
605 result = ERR_ANS_INVALID_PARAM;
606 break;
607 }
608 } else {
609 ANS_LOGW("Notification bundle does not exsit.");
610 result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
611 }
612 return result;
613 }
614
GenerateBundleKey(const sptr<NotificationBundleOption> & bundleOption) const615 std::string NotificationPreferences::GenerateBundleKey(const sptr<NotificationBundleOption> &bundleOption) const
616 {
617 return bundleOption->GetBundleName().append(std::to_string(bundleOption->GetUid()));
618 }
619
OnDistributedKvStoreDeathRecipient()620 void NotificationPreferences::OnDistributedKvStoreDeathRecipient()
621 {
622 if (preferncesDB_ != nullptr) {
623 if (preferncesDB_->StoreDeathRecipient()) {
624 }
625 }
626 }
627
GetTemplateSupported(const std::string & templateName,bool & support)628 ErrCode NotificationPreferences::GetTemplateSupported(const std::string& templateName, bool &support)
629 {
630 if (templateName.length() == 0) {
631 ANS_LOGE("template name is null.");
632 return ERR_ANS_INVALID_PARAM;
633 }
634
635 std::ifstream inFile;
636 inFile.open(DEFAULT_TEMPLATE_PATH.c_str(), std::ios::in);
637 if (!inFile.is_open()) {
638 ANS_LOGE("read template config error.");
639 return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
640 }
641
642 nlohmann::json jsonObj;
643 inFile >> jsonObj;
644 if (jsonObj.is_discarded()) {
645 ANS_LOGE("template json discarded error.");
646 inFile.close();
647 return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
648 }
649
650 if (jsonObj.contains(templateName)) {
651 support = true;
652 }
653
654 jsonObj.clear();
655 inFile.close();
656 return ERR_OK;
657 }
658
InitSettingFromDisturbDB()659 void NotificationPreferences::InitSettingFromDisturbDB()
660 {
661 ANS_LOGD("%{public}s", __FUNCTION__);
662 std::lock_guard<std::mutex> lock(preferenceMutex_);
663 if (preferncesDB_ != nullptr) {
664 preferncesDB_->ParseFromDisturbeDB(preferencesInfo_);
665 }
666 }
667
RemoveSettings(int32_t userId)668 void NotificationPreferences::RemoveSettings(int32_t userId)
669 {
670 ANS_LOGD("%{public}s", __FUNCTION__);
671
672 {
673 std::lock_guard<std::mutex> lock(preferenceMutex_);
674 preferencesInfo_.RemoveNotificationEnable(userId);
675 preferencesInfo_.RemoveDoNotDisturbDate(userId);
676 }
677
678 if (preferncesDB_ != nullptr) {
679 preferncesDB_->RemoveNotificationEnable(userId);
680 preferncesDB_->RemoveDoNotDisturbDate(userId);
681 }
682 }
683
CheckApiCompatibility(const sptr<NotificationBundleOption> & bundleOption) const684 bool NotificationPreferences::CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption) const
685 {
686 ANS_LOGD("%{public}s", __FUNCTION__);
687 std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
688 if (bundleManager == nullptr) {
689 return false;
690 }
691 return bundleManager->CheckApiCompatibility(bundleOption);
692 }
693
RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> & bundleOption)694 void NotificationPreferences::RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> &bundleOption)
695 {
696 ANS_LOGE("%{public}s", __FUNCTION__);
697 if (preferncesDB_ != nullptr && bundleOption != nullptr) {
698 preferncesDB_->RemoveAnsBundleDbInfo(bundleOption->GetBundleName(), bundleOption->GetUid());
699 }
700 }
701 } // namespace Notification
702 } // namespace OHOS