196279301Sopenharmony_ci/*
296279301Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
396279301Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
496279301Sopenharmony_ci * you may not use this file except in compliance with the License.
596279301Sopenharmony_ci * You may obtain a copy of the License at
696279301Sopenharmony_ci *
796279301Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
896279301Sopenharmony_ci *
996279301Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1096279301Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1196279301Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1296279301Sopenharmony_ci * See the License for the specific language governing permissions and
1396279301Sopenharmony_ci * limitations under the License.
1496279301Sopenharmony_ci */
1596279301Sopenharmony_ci
1696279301Sopenharmony_ci#include "notification_preferences.h"
1796279301Sopenharmony_ci
1896279301Sopenharmony_ci#include <fstream>
1996279301Sopenharmony_ci#include <memory>
2096279301Sopenharmony_ci#include <mutex>
2196279301Sopenharmony_ci
2296279301Sopenharmony_ci#include "access_token_helper.h"
2396279301Sopenharmony_ci#include "ans_const_define.h"
2496279301Sopenharmony_ci#include "ans_inner_errors.h"
2596279301Sopenharmony_ci#include "ans_log_wrapper.h"
2696279301Sopenharmony_ci#include "ans_permission_def.h"
2796279301Sopenharmony_ci#include "bundle_manager_helper.h"
2896279301Sopenharmony_ci#include "hitrace_meter_adapter.h"
2996279301Sopenharmony_ci#include "nlohmann/json.hpp"
3096279301Sopenharmony_ci#include "os_account_manager_helper.h"
3196279301Sopenharmony_ci#include "notification_analytics_util.h"
3296279301Sopenharmony_ci#include "notification_config_parse.h"
3396279301Sopenharmony_ci
3496279301Sopenharmony_cinamespace OHOS {
3596279301Sopenharmony_cinamespace Notification {
3696279301Sopenharmony_cinamespace {
3796279301Sopenharmony_ciconst static std::string KEY_BUNDLE_LABEL = "label_ans_bundle_";
3896279301Sopenharmony_ci}
3996279301Sopenharmony_cistd::mutex NotificationPreferences::instanceMutex_;
4096279301Sopenharmony_cistd::shared_ptr<NotificationPreferences> NotificationPreferences::instance_;
4196279301Sopenharmony_ci
4296279301Sopenharmony_ciNotificationPreferences::NotificationPreferences()
4396279301Sopenharmony_ci{
4496279301Sopenharmony_ci    preferncesDB_ = std::make_unique<NotificationPreferencesDatabase>();
4596279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
4696279301Sopenharmony_ci        HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_1)
4796279301Sopenharmony_ci           .Message("preferncesDB is null.");
4896279301Sopenharmony_ci        NotificationAnalyticsUtil::ReportModifyEvent(message);
4996279301Sopenharmony_ci    }
5096279301Sopenharmony_ci    InitSettingFromDisturbDB();
5196279301Sopenharmony_ci}
5296279301Sopenharmony_ci
5396279301Sopenharmony_cistd::shared_ptr<NotificationPreferences> NotificationPreferences::GetInstance()
5496279301Sopenharmony_ci{
5596279301Sopenharmony_ci    if (instance_ == nullptr) {
5696279301Sopenharmony_ci        std::lock_guard<std::mutex> lock(instanceMutex_);
5796279301Sopenharmony_ci        if (instance_ == nullptr) {
5896279301Sopenharmony_ci            auto instance = std::make_shared<NotificationPreferences>();
5996279301Sopenharmony_ci            instance_ = instance;
6096279301Sopenharmony_ci        }
6196279301Sopenharmony_ci    }
6296279301Sopenharmony_ci    return instance_;
6396279301Sopenharmony_ci}
6496279301Sopenharmony_ci
6596279301Sopenharmony_ciErrCode NotificationPreferences::AddNotificationSlots(
6696279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
6796279301Sopenharmony_ci{
6896279301Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
6996279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
7096279301Sopenharmony_ci    HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_1)
7196279301Sopenharmony_ci        .BundleName(bundleOption == nullptr ? "" : bundleOption->GetBundleName());
7296279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty() || slots.empty()) {
7396279301Sopenharmony_ci        message.Message("Invalid param.");
7496279301Sopenharmony_ci        NotificationAnalyticsUtil::ReportModifyEvent(message);
7596279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
7696279301Sopenharmony_ci    }
7796279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
7896279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
7996279301Sopenharmony_ci    ErrCode result = ERR_OK;
8096279301Sopenharmony_ci    for (auto slot : slots) {
8196279301Sopenharmony_ci        result = CheckSlotForCreateSlot(bundleOption, slot, preferencesInfo);
8296279301Sopenharmony_ci        if (result != ERR_OK) {
8396279301Sopenharmony_ci            return result;
8496279301Sopenharmony_ci        }
8596279301Sopenharmony_ci    }
8696279301Sopenharmony_ci
8796279301Sopenharmony_ci    ANS_LOGD("ffrt: add slot to db!");
8896279301Sopenharmony_ci    if (result == ERR_OK &&
8996279301Sopenharmony_ci        (!preferncesDB_->PutSlotsToDisturbeDB(bundleOption->GetBundleName(), bundleOption->GetUid(), slots))) {
9096279301Sopenharmony_ci        message.Message("put slot for to db failed.");
9196279301Sopenharmony_ci        NotificationAnalyticsUtil::ReportModifyEvent(message);
9296279301Sopenharmony_ci        return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
9396279301Sopenharmony_ci    }
9496279301Sopenharmony_ci
9596279301Sopenharmony_ci    if (result == ERR_OK) {
9696279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
9796279301Sopenharmony_ci    }
9896279301Sopenharmony_ci    return result;
9996279301Sopenharmony_ci}
10096279301Sopenharmony_ci
10196279301Sopenharmony_ciErrCode NotificationPreferences::AddNotificationBundleProperty(const sptr<NotificationBundleOption> &bundleOption)
10296279301Sopenharmony_ci{
10396279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
10496279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
10596279301Sopenharmony_ci    }
10696279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
10796279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
10896279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
10996279301Sopenharmony_ci    preferencesInfo.SetBundleInfo(bundleInfo);
11096279301Sopenharmony_ci    ErrCode result = ERR_OK;
11196279301Sopenharmony_ci    if (preferncesDB_->PutBundlePropertyToDisturbeDB(bundleInfo)) {
11296279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
11396279301Sopenharmony_ci    } else {
11496279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
11596279301Sopenharmony_ci    }
11696279301Sopenharmony_ci    ANS_LOGD("AddNotificationBundleProperty.result: %{public}d", result);
11796279301Sopenharmony_ci    return result;
11896279301Sopenharmony_ci}
11996279301Sopenharmony_ci
12096279301Sopenharmony_ciErrCode NotificationPreferences::RemoveNotificationSlot(
12196279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType)
12296279301Sopenharmony_ci{
12396279301Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
12496279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
12596279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
12696279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
12796279301Sopenharmony_ci    }
12896279301Sopenharmony_ci    HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_1)
12996279301Sopenharmony_ci        .BundleName(bundleOption->GetBundleName());
13096279301Sopenharmony_ci    message.SlotType(static_cast<uint32_t>(slotType));
13196279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
13296279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
13396279301Sopenharmony_ci    ErrCode result = ERR_OK;
13496279301Sopenharmony_ci    result = CheckSlotForRemoveSlot(bundleOption, slotType, preferencesInfo);
13596279301Sopenharmony_ci    if (result == ERR_OK &&
13696279301Sopenharmony_ci        (!preferncesDB_->RemoveSlotFromDisturbeDB(GenerateBundleKey(bundleOption), slotType, bundleOption->GetUid()))) {
13796279301Sopenharmony_ci        message.Message("Remove slot failed: " + std::to_string(result));
13896279301Sopenharmony_ci        NotificationAnalyticsUtil::ReportModifyEvent(message);
13996279301Sopenharmony_ci        return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
14096279301Sopenharmony_ci    }
14196279301Sopenharmony_ci
14296279301Sopenharmony_ci    if (result == ERR_OK) {
14396279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
14496279301Sopenharmony_ci    }
14596279301Sopenharmony_ci    message.Message("Remove slot successful");
14696279301Sopenharmony_ci    NotificationAnalyticsUtil::ReportModifyEvent(message);
14796279301Sopenharmony_ci    return result;
14896279301Sopenharmony_ci}
14996279301Sopenharmony_ci
15096279301Sopenharmony_ciErrCode NotificationPreferences::RemoveNotificationAllSlots(const sptr<NotificationBundleOption> &bundleOption)
15196279301Sopenharmony_ci{
15296279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
15396279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
15496279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
15596279301Sopenharmony_ci    }
15696279301Sopenharmony_ci    HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_1)
15796279301Sopenharmony_ci        .BundleName(bundleOption->GetBundleName());
15896279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
15996279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
16096279301Sopenharmony_ci    ErrCode result = ERR_OK;
16196279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
16296279301Sopenharmony_ci    if (preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
16396279301Sopenharmony_ci        bundleInfo.RemoveAllSlots();
16496279301Sopenharmony_ci        preferencesInfo.SetBundleInfo(bundleInfo);
16596279301Sopenharmony_ci        if (!preferncesDB_->RemoveAllSlotsFromDisturbeDB(GenerateBundleKey(bundleOption), bundleOption->GetUid())) {
16696279301Sopenharmony_ci            result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
16796279301Sopenharmony_ci        }
16896279301Sopenharmony_ci    } else {
16996279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
17096279301Sopenharmony_ci    }
17196279301Sopenharmony_ci
17296279301Sopenharmony_ci    if (result == ERR_OK) {
17396279301Sopenharmony_ci        ANS_LOGD("result is ERR_OK");
17496279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
17596279301Sopenharmony_ci    }
17696279301Sopenharmony_ci    message.Message("Remove all slot: " + std::to_string(result));
17796279301Sopenharmony_ci    NotificationAnalyticsUtil::ReportModifyEvent(message);
17896279301Sopenharmony_ci    return result;
17996279301Sopenharmony_ci}
18096279301Sopenharmony_ci
18196279301Sopenharmony_ciErrCode NotificationPreferences::RemoveNotificationForBundle(const sptr<NotificationBundleOption> &bundleOption)
18296279301Sopenharmony_ci{
18396279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
18496279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
18596279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
18696279301Sopenharmony_ci    }
18796279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
18896279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
18996279301Sopenharmony_ci
19096279301Sopenharmony_ci    ErrCode result = ERR_OK;
19196279301Sopenharmony_ci    if (preferencesInfo.IsExsitBundleInfo(bundleOption)) {
19296279301Sopenharmony_ci        preferencesInfo.RemoveBundleInfo(bundleOption);
19396279301Sopenharmony_ci        if (!preferncesDB_->RemoveBundleFromDisturbeDB(GenerateBundleKey(bundleOption), bundleOption->GetUid())) {
19496279301Sopenharmony_ci            result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
19596279301Sopenharmony_ci        }
19696279301Sopenharmony_ci    } else {
19796279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
19896279301Sopenharmony_ci    }
19996279301Sopenharmony_ci
20096279301Sopenharmony_ci    if (result == ERR_OK) {
20196279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
20296279301Sopenharmony_ci    }
20396279301Sopenharmony_ci
20496279301Sopenharmony_ci    return result;
20596279301Sopenharmony_ci}
20696279301Sopenharmony_ci
20796279301Sopenharmony_ciErrCode NotificationPreferences::UpdateNotificationSlots(
20896279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
20996279301Sopenharmony_ci{
21096279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
21196279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty() || slots.empty()) {
21296279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
21396279301Sopenharmony_ci    }
21496279301Sopenharmony_ci    HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_2)
21596279301Sopenharmony_ci        .BundleName(bundleOption->GetBundleName());
21696279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
21796279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
21896279301Sopenharmony_ci    ErrCode result = ERR_OK;
21996279301Sopenharmony_ci    for (auto slotIter : slots) {
22096279301Sopenharmony_ci        result = CheckSlotForUpdateSlot(bundleOption, slotIter, preferencesInfo);
22196279301Sopenharmony_ci        if (result != ERR_OK) {
22296279301Sopenharmony_ci            message.Message("Check slot for update failed." + std::to_string(result));
22396279301Sopenharmony_ci            NotificationAnalyticsUtil::ReportModifyEvent(message);
22496279301Sopenharmony_ci            return result;
22596279301Sopenharmony_ci        }
22696279301Sopenharmony_ci    }
22796279301Sopenharmony_ci
22896279301Sopenharmony_ci    if ((result == ERR_OK) &&
22996279301Sopenharmony_ci        (!preferncesDB_->PutSlotsToDisturbeDB(bundleOption->GetBundleName(), bundleOption->GetUid(), slots))) {
23096279301Sopenharmony_ci        message.Message("Update put slot for to db failed.");
23196279301Sopenharmony_ci        NotificationAnalyticsUtil::ReportModifyEvent(message);
23296279301Sopenharmony_ci        return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
23396279301Sopenharmony_ci    }
23496279301Sopenharmony_ci
23596279301Sopenharmony_ci    if (result == ERR_OK) {
23696279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
23796279301Sopenharmony_ci    }
23896279301Sopenharmony_ci
23996279301Sopenharmony_ci    return result;
24096279301Sopenharmony_ci}
24196279301Sopenharmony_ci
24296279301Sopenharmony_ciErrCode NotificationPreferences::GetNotificationSlot(const sptr<NotificationBundleOption> &bundleOption,
24396279301Sopenharmony_ci    const NotificationConstant::SlotType &type, sptr<NotificationSlot> &slot)
24496279301Sopenharmony_ci{
24596279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
24696279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
24796279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
24896279301Sopenharmony_ci    }
24996279301Sopenharmony_ci
25096279301Sopenharmony_ci    ErrCode result = ERR_OK;
25196279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
25296279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
25396279301Sopenharmony_ci    if (preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) {
25496279301Sopenharmony_ci        if (!bundleInfo.GetSlot(type, slot)) {
25596279301Sopenharmony_ci            result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
25696279301Sopenharmony_ci        }
25796279301Sopenharmony_ci    } else {
25896279301Sopenharmony_ci        ANS_LOGW("bundle not exist");
25996279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
26096279301Sopenharmony_ci    }
26196279301Sopenharmony_ci    ANS_LOGD("%{public}s status  = %{public}d ", __FUNCTION__, result);
26296279301Sopenharmony_ci    return result;
26396279301Sopenharmony_ci}
26496279301Sopenharmony_ci
26596279301Sopenharmony_ciErrCode NotificationPreferences::GetNotificationAllSlots(
26696279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlot>> &slots)
26796279301Sopenharmony_ci{
26896279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
26996279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
27096279301Sopenharmony_ci    }
27196279301Sopenharmony_ci
27296279301Sopenharmony_ci    ErrCode result = ERR_OK;
27396279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
27496279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
27596279301Sopenharmony_ci    if (preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) {
27696279301Sopenharmony_ci        bundleInfo.GetAllSlots(slots);
27796279301Sopenharmony_ci    } else {
27896279301Sopenharmony_ci        ANS_LOGW("Notification bundle does not exsit.");
27996279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
28096279301Sopenharmony_ci    }
28196279301Sopenharmony_ci
28296279301Sopenharmony_ci    return result;
28396279301Sopenharmony_ci}
28496279301Sopenharmony_ci
28596279301Sopenharmony_ciErrCode NotificationPreferences::GetNotificationSlotsNumForBundle(
28696279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, uint64_t &num)
28796279301Sopenharmony_ci{
28896279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
28996279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
29096279301Sopenharmony_ci    }
29196279301Sopenharmony_ci
29296279301Sopenharmony_ci    ErrCode result = ERR_OK;
29396279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
29496279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
29596279301Sopenharmony_ci    if (preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) {
29696279301Sopenharmony_ci        num = static_cast<uint64_t>(bundleInfo.GetAllSlotsSize());
29796279301Sopenharmony_ci    } else {
29896279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
29996279301Sopenharmony_ci    }
30096279301Sopenharmony_ci    return result;
30196279301Sopenharmony_ci}
30296279301Sopenharmony_ci
30396279301Sopenharmony_ciErrCode NotificationPreferences::GetNotificationSlotFlagsForBundle(
30496279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, uint32_t &slotFlags)
30596279301Sopenharmony_ci{
30696279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
30796279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
30896279301Sopenharmony_ci    }
30996279301Sopenharmony_ci
31096279301Sopenharmony_ci    return GetBundleProperty(bundleOption, BundleType::BUNDLE_SLOTFLGS_TYPE, slotFlags);
31196279301Sopenharmony_ci}
31296279301Sopenharmony_ci
31396279301Sopenharmony_ci
31496279301Sopenharmony_ciErrCode NotificationPreferences::SetNotificationSlotFlagsForBundle(
31596279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, uint32_t slotFlags)
31696279301Sopenharmony_ci{
31796279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
31896279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
31996279301Sopenharmony_ci    }
32096279301Sopenharmony_ci
32196279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
32296279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
32396279301Sopenharmony_ci    ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_SLOTFLGS_TYPE, slotFlags);
32496279301Sopenharmony_ci    if (result == ERR_OK) {
32596279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
32696279301Sopenharmony_ci    }
32796279301Sopenharmony_ci    return result;
32896279301Sopenharmony_ci}
32996279301Sopenharmony_ci
33096279301Sopenharmony_ciErrCode NotificationPreferences::IsShowBadge(const sptr<NotificationBundleOption> &bundleOption, bool &enable)
33196279301Sopenharmony_ci{
33296279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
33396279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
33496279301Sopenharmony_ci    }
33596279301Sopenharmony_ci    return GetBundleProperty(bundleOption, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable);
33696279301Sopenharmony_ci}
33796279301Sopenharmony_ci
33896279301Sopenharmony_ciErrCode NotificationPreferences::SetShowBadge(const sptr<NotificationBundleOption> &bundleOption, const bool enable)
33996279301Sopenharmony_ci{
34096279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
34196279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
34296279301Sopenharmony_ci    }
34396279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
34496279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
34596279301Sopenharmony_ci    ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable);
34696279301Sopenharmony_ci    if (result == ERR_OK) {
34796279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
34896279301Sopenharmony_ci    }
34996279301Sopenharmony_ci    return result;
35096279301Sopenharmony_ci}
35196279301Sopenharmony_ci
35296279301Sopenharmony_ciErrCode NotificationPreferences::GetImportance(const sptr<NotificationBundleOption> &bundleOption, int32_t &importance)
35396279301Sopenharmony_ci{
35496279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
35596279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
35696279301Sopenharmony_ci    }
35796279301Sopenharmony_ci
35896279301Sopenharmony_ci    return GetBundleProperty(bundleOption, BundleType::BUNDLE_IMPORTANCE_TYPE, importance);
35996279301Sopenharmony_ci}
36096279301Sopenharmony_ci
36196279301Sopenharmony_ci
36296279301Sopenharmony_ciErrCode NotificationPreferences::SetImportance(
36396279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, const int32_t &importance)
36496279301Sopenharmony_ci{
36596279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
36696279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
36796279301Sopenharmony_ci    }
36896279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
36996279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
37096279301Sopenharmony_ci    ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_IMPORTANCE_TYPE, importance);
37196279301Sopenharmony_ci    if (result == ERR_OK) {
37296279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
37396279301Sopenharmony_ci    }
37496279301Sopenharmony_ci    return result;
37596279301Sopenharmony_ci}
37696279301Sopenharmony_ci
37796279301Sopenharmony_ciErrCode NotificationPreferences::GetTotalBadgeNums(
37896279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, int32_t &totalBadgeNum)
37996279301Sopenharmony_ci{
38096279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
38196279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
38296279301Sopenharmony_ci    }
38396279301Sopenharmony_ci    return GetBundleProperty(bundleOption, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum);
38496279301Sopenharmony_ci}
38596279301Sopenharmony_ci
38696279301Sopenharmony_ciErrCode NotificationPreferences::SetTotalBadgeNums(
38796279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, const int32_t num)
38896279301Sopenharmony_ci{
38996279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
39096279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
39196279301Sopenharmony_ci    }
39296279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
39396279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
39496279301Sopenharmony_ci    ErrCode result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, num);
39596279301Sopenharmony_ci    if (result == ERR_OK) {
39696279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
39796279301Sopenharmony_ci    }
39896279301Sopenharmony_ci    return result;
39996279301Sopenharmony_ci}
40096279301Sopenharmony_ci
40196279301Sopenharmony_ciErrCode NotificationPreferences::GetNotificationsEnabledForBundle(
40296279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, bool &enabled)
40396279301Sopenharmony_ci{
40496279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
40596279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
40696279301Sopenharmony_ci    }
40796279301Sopenharmony_ci    return GetBundleProperty(bundleOption, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled);
40896279301Sopenharmony_ci}
40996279301Sopenharmony_ci
41096279301Sopenharmony_ciErrCode NotificationPreferences::SetNotificationsEnabledForBundle(
41196279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, const bool enabled)
41296279301Sopenharmony_ci{
41396279301Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
41496279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
41596279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
41696279301Sopenharmony_ci    }
41796279301Sopenharmony_ci
41896279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
41996279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
42096279301Sopenharmony_ci    ErrCode result =
42196279301Sopenharmony_ci        SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled);
42296279301Sopenharmony_ci    if (result == ERR_OK) {
42396279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
42496279301Sopenharmony_ci    }
42596279301Sopenharmony_ci    return result;
42696279301Sopenharmony_ci}
42796279301Sopenharmony_ci
42896279301Sopenharmony_ciErrCode NotificationPreferences::GetNotificationsEnabled(const int32_t &userId, bool &enabled)
42996279301Sopenharmony_ci{
43096279301Sopenharmony_ci    if (userId <= SUBSCRIBE_USER_INIT) {
43196279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
43296279301Sopenharmony_ci    }
43396279301Sopenharmony_ci
43496279301Sopenharmony_ci    ErrCode result = ERR_OK;
43596279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
43696279301Sopenharmony_ci    if (!preferencesInfo_.GetEnabledAllNotification(userId, enabled)) {
43796279301Sopenharmony_ci        result = ERR_ANS_INVALID_PARAM;
43896279301Sopenharmony_ci    }
43996279301Sopenharmony_ci    return result;
44096279301Sopenharmony_ci}
44196279301Sopenharmony_ci
44296279301Sopenharmony_ciErrCode NotificationPreferences::SetNotificationsEnabled(const int32_t &userId, const bool &enabled)
44396279301Sopenharmony_ci{
44496279301Sopenharmony_ci    if (userId <= SUBSCRIBE_USER_INIT) {
44596279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
44696279301Sopenharmony_ci    }
44796279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
44896279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
44996279301Sopenharmony_ci    preferencesInfo.SetEnabledAllNotification(userId, enabled);
45096279301Sopenharmony_ci    ErrCode result = ERR_OK;
45196279301Sopenharmony_ci    if (!preferncesDB_->PutNotificationsEnabled(userId, enabled)) {
45296279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
45396279301Sopenharmony_ci    }
45496279301Sopenharmony_ci
45596279301Sopenharmony_ci    if (result == ERR_OK) {
45696279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
45796279301Sopenharmony_ci    }
45896279301Sopenharmony_ci    return result;
45996279301Sopenharmony_ci}
46096279301Sopenharmony_ci
46196279301Sopenharmony_ciErrCode NotificationPreferences::GetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool &hasPopped)
46296279301Sopenharmony_ci{
46396279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
46496279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
46596279301Sopenharmony_ci    }
46696279301Sopenharmony_ci    return GetBundleProperty(bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
46796279301Sopenharmony_ci}
46896279301Sopenharmony_ci
46996279301Sopenharmony_ciErrCode NotificationPreferences::SetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool hasPopped)
47096279301Sopenharmony_ci{
47196279301Sopenharmony_ci    if (bundleOption == nullptr) {
47296279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
47396279301Sopenharmony_ci    }
47496279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
47596279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
47696279301Sopenharmony_ci    ErrCode result = ERR_OK;
47796279301Sopenharmony_ci    result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped);
47896279301Sopenharmony_ci    if (result == ERR_OK) {
47996279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
48096279301Sopenharmony_ci    }
48196279301Sopenharmony_ci    return result;
48296279301Sopenharmony_ci}
48396279301Sopenharmony_ci
48496279301Sopenharmony_ciErrCode NotificationPreferences::GetDoNotDisturbDate(const int32_t &userId,
48596279301Sopenharmony_ci    sptr<NotificationDoNotDisturbDate> &date)
48696279301Sopenharmony_ci{
48796279301Sopenharmony_ci    if (userId <= SUBSCRIBE_USER_INIT) {
48896279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
48996279301Sopenharmony_ci    }
49096279301Sopenharmony_ci
49196279301Sopenharmony_ci    ErrCode result = ERR_OK;
49296279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
49396279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
49496279301Sopenharmony_ci    if (!preferencesInfo.GetDoNotDisturbDate(userId, date)) {
49596279301Sopenharmony_ci        result = ERR_ANS_INVALID_PARAM;
49696279301Sopenharmony_ci    }
49796279301Sopenharmony_ci    return result;
49896279301Sopenharmony_ci}
49996279301Sopenharmony_ci
50096279301Sopenharmony_ciErrCode NotificationPreferences::SetDoNotDisturbDate(const int32_t &userId,
50196279301Sopenharmony_ci    const sptr<NotificationDoNotDisturbDate> date)
50296279301Sopenharmony_ci{
50396279301Sopenharmony_ci    ANS_LOGE("enter.");
50496279301Sopenharmony_ci    if (userId <= SUBSCRIBE_USER_INIT) {
50596279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
50696279301Sopenharmony_ci    }
50796279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
50896279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
50996279301Sopenharmony_ci    preferencesInfo.SetDoNotDisturbDate(userId, date);
51096279301Sopenharmony_ci
51196279301Sopenharmony_ci    ErrCode result = ERR_OK;
51296279301Sopenharmony_ci    if (!preferncesDB_->PutDoNotDisturbDate(userId, date)) {
51396279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
51496279301Sopenharmony_ci    }
51596279301Sopenharmony_ci
51696279301Sopenharmony_ci    if (result == ERR_OK) {
51796279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
51896279301Sopenharmony_ci    }
51996279301Sopenharmony_ci    return result;
52096279301Sopenharmony_ci}
52196279301Sopenharmony_ci
52296279301Sopenharmony_cibool NotificationPreferences::CheckDoNotDisturbProfileID(int32_t profileId)
52396279301Sopenharmony_ci{
52496279301Sopenharmony_ci    if (profileId < DO_NOT_DISTURB_PROFILE_MIN_ID || profileId > DO_NOT_DISTURB_PROFILE_MAX_ID) {
52596279301Sopenharmony_ci        ANS_LOGE("The profile id is out of range.");
52696279301Sopenharmony_ci        return false;
52796279301Sopenharmony_ci    }
52896279301Sopenharmony_ci    return true;
52996279301Sopenharmony_ci}
53096279301Sopenharmony_ci
53196279301Sopenharmony_ciErrCode NotificationPreferences::AddDoNotDisturbProfiles(
53296279301Sopenharmony_ci    int32_t userId, std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)
53396279301Sopenharmony_ci{
53496279301Sopenharmony_ci    ANS_LOGD("Called.");
53596279301Sopenharmony_ci    for (auto profile : profiles) {
53696279301Sopenharmony_ci        if (profile == nullptr) {
53796279301Sopenharmony_ci            ANS_LOGE("The profile is nullptr.");
53896279301Sopenharmony_ci            return ERR_ANS_INVALID_PARAM;
53996279301Sopenharmony_ci        }
54096279301Sopenharmony_ci        if (!CheckDoNotDisturbProfileID(profile->GetProfileId())) {
54196279301Sopenharmony_ci            return ERR_ANS_INVALID_PARAM;
54296279301Sopenharmony_ci        }
54396279301Sopenharmony_ci        auto trustList = profile->GetProfileTrustList();
54496279301Sopenharmony_ci        for (auto& bundleInfo : trustList) {
54596279301Sopenharmony_ci            int32_t index = BundleManagerHelper::GetInstance()->GetAppIndexByUid(bundleInfo.GetUid());
54696279301Sopenharmony_ci            bundleInfo.SetAppIndex(index);
54796279301Sopenharmony_ci            ANS_LOGI("Get app index by uid %{public}d %{public}s %{public}d", bundleInfo.GetUid(),
54896279301Sopenharmony_ci                bundleInfo.GetBundleName().c_str(), index);
54996279301Sopenharmony_ci        }
55096279301Sopenharmony_ci        profile->SetProfileTrustList(trustList);
55196279301Sopenharmony_ci    }
55296279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
55396279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
55496279301Sopenharmony_ci    preferencesInfo.AddDoNotDisturbProfiles(userId, profiles);
55596279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
55696279301Sopenharmony_ci        ANS_LOGE("The prefernces db is nullptr.");
55796279301Sopenharmony_ci        return ERR_ANS_SERVICE_NOT_READY;
55896279301Sopenharmony_ci    }
55996279301Sopenharmony_ci    if (!preferncesDB_->AddDoNotDisturbProfiles(userId, profiles)) {
56096279301Sopenharmony_ci        return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
56196279301Sopenharmony_ci    }
56296279301Sopenharmony_ci    preferencesInfo_ = preferencesInfo;
56396279301Sopenharmony_ci    return ERR_OK;
56496279301Sopenharmony_ci}
56596279301Sopenharmony_ci
56696279301Sopenharmony_ciErrCode NotificationPreferences::RemoveDoNotDisturbProfiles(
56796279301Sopenharmony_ci    int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> profiles)
56896279301Sopenharmony_ci{
56996279301Sopenharmony_ci    ANS_LOGE("Called.");
57096279301Sopenharmony_ci    for (auto profile : profiles) {
57196279301Sopenharmony_ci        if (profile == nullptr) {
57296279301Sopenharmony_ci            ANS_LOGE("The profile is nullptr.");
57396279301Sopenharmony_ci            return ERR_ANS_INVALID_PARAM;
57496279301Sopenharmony_ci        }
57596279301Sopenharmony_ci        if (!CheckDoNotDisturbProfileID(profile->GetProfileId())) {
57696279301Sopenharmony_ci            return ERR_ANS_INVALID_PARAM;
57796279301Sopenharmony_ci        }
57896279301Sopenharmony_ci    }
57996279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
58096279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
58196279301Sopenharmony_ci    preferencesInfo.RemoveDoNotDisturbProfiles(userId, profiles);
58296279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
58396279301Sopenharmony_ci        ANS_LOGE("The prefernces db is nullptr.");
58496279301Sopenharmony_ci        return ERR_ANS_SERVICE_NOT_READY;
58596279301Sopenharmony_ci    }
58696279301Sopenharmony_ci    if (!preferncesDB_->RemoveDoNotDisturbProfiles(userId, profiles)) {
58796279301Sopenharmony_ci        return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
58896279301Sopenharmony_ci    }
58996279301Sopenharmony_ci    preferencesInfo_ = preferencesInfo;
59096279301Sopenharmony_ci    return ERR_OK;
59196279301Sopenharmony_ci}
59296279301Sopenharmony_ci
59396279301Sopenharmony_civoid NotificationPreferences::UpdateProfilesUtil(std::vector<NotificationBundleOption>& trustList,
59496279301Sopenharmony_ci    const std::vector<NotificationBundleOption> bundleList)
59596279301Sopenharmony_ci{
59696279301Sopenharmony_ci    for (auto& item : bundleList) {
59796279301Sopenharmony_ci        bool exit = false;
59896279301Sopenharmony_ci        for (auto& bundle: trustList) {
59996279301Sopenharmony_ci            if (item.GetUid() == bundle.GetUid()) {
60096279301Sopenharmony_ci                exit = true;
60196279301Sopenharmony_ci                break;
60296279301Sopenharmony_ci            }
60396279301Sopenharmony_ci        }
60496279301Sopenharmony_ci        if (!exit) {
60596279301Sopenharmony_ci            trustList.push_back(item);
60696279301Sopenharmony_ci        }
60796279301Sopenharmony_ci    }
60896279301Sopenharmony_ci}
60996279301Sopenharmony_ci
61096279301Sopenharmony_ciErrCode NotificationPreferences::UpdateDoNotDisturbProfiles(int32_t userId, int32_t profileId,
61196279301Sopenharmony_ci    const std::string& name, const std::vector<NotificationBundleOption>& bundleList)
61296279301Sopenharmony_ci{
61396279301Sopenharmony_ci    ANS_LOGI("Called update Profile %{public}d %{public}d %{public}zu.", userId, profileId, bundleList.size());
61496279301Sopenharmony_ci    if (!CheckDoNotDisturbProfileID(profileId) || bundleList.empty()) {
61596279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
61696279301Sopenharmony_ci    }
61796279301Sopenharmony_ci
61896279301Sopenharmony_ci    sptr<NotificationDoNotDisturbProfile> profile = new (std::nothrow) NotificationDoNotDisturbProfile();
61996279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
62096279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
62196279301Sopenharmony_ci    if (preferencesInfo.GetDoNotDisturbProfiles(profileId, userId, profile)) {
62296279301Sopenharmony_ci        auto trustList = profile->GetProfileTrustList();
62396279301Sopenharmony_ci        UpdateProfilesUtil(trustList, bundleList);
62496279301Sopenharmony_ci        profile->SetProfileTrustList(trustList);
62596279301Sopenharmony_ci    } else {
62696279301Sopenharmony_ci        profile->SetProfileId(profileId);
62796279301Sopenharmony_ci        profile->SetProfileName(name);
62896279301Sopenharmony_ci        profile->SetProfileTrustList(bundleList);
62996279301Sopenharmony_ci    }
63096279301Sopenharmony_ci    ANS_LOGI("Update profile %{public}d %{public}d %{public}zu", userId, profile->GetProfileId(),
63196279301Sopenharmony_ci        profile->GetProfileTrustList().size());
63296279301Sopenharmony_ci    preferencesInfo.AddDoNotDisturbProfiles(userId, {profile});
63396279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
63496279301Sopenharmony_ci        ANS_LOGE("The prefernces db is nullptr.");
63596279301Sopenharmony_ci        return ERR_ANS_SERVICE_NOT_READY;
63696279301Sopenharmony_ci    }
63796279301Sopenharmony_ci    if (!preferncesDB_->AddDoNotDisturbProfiles(userId, {profile})) {
63896279301Sopenharmony_ci        return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
63996279301Sopenharmony_ci    }
64096279301Sopenharmony_ci    preferencesInfo_ = preferencesInfo;
64196279301Sopenharmony_ci    return ERR_OK;
64296279301Sopenharmony_ci}
64396279301Sopenharmony_ci
64496279301Sopenharmony_civoid NotificationPreferences::UpdateCloneBundleInfo(int32_t userId,
64596279301Sopenharmony_ci    const NotificationCloneBundleInfo& cloneBundleInfo)
64696279301Sopenharmony_ci{
64796279301Sopenharmony_ci    ANS_LOGI("Event bundle update %{public}s.", cloneBundleInfo.Dump().c_str());
64896279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
64996279301Sopenharmony_ci    sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption();
65096279301Sopenharmony_ci    bundleOption->SetBundleName(cloneBundleInfo.GetBundleName());
65196279301Sopenharmony_ci    bundleOption->SetUid(cloneBundleInfo.GetUid());
65296279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
65396279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
65496279301Sopenharmony_ci    if (!preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
65596279301Sopenharmony_ci        bundleInfo.SetBundleName(cloneBundleInfo.GetBundleName());
65696279301Sopenharmony_ci        bundleInfo.SetBundleUid(cloneBundleInfo.GetUid());
65796279301Sopenharmony_ci    }
65896279301Sopenharmony_ci
65996279301Sopenharmony_ci    /* after clone, override these witch */
66096279301Sopenharmony_ci    bundleInfo.SetSlotFlags(cloneBundleInfo.GetSlotFlags());
66196279301Sopenharmony_ci    bundleInfo.SetIsShowBadge(cloneBundleInfo.GetIsShowBadge());
66296279301Sopenharmony_ci    bundleInfo.SetEnableNotification(cloneBundleInfo.GetEnableNotification());
66396279301Sopenharmony_ci    /* update property to db */
66496279301Sopenharmony_ci    if (!preferncesDB_->UpdateBundlePropertyToDisturbeDB(userId, bundleInfo)) {
66596279301Sopenharmony_ci        ANS_LOGW("Clone bundle info failed %{public}s.", cloneBundleInfo.Dump().c_str());
66696279301Sopenharmony_ci        return;
66796279301Sopenharmony_ci    }
66896279301Sopenharmony_ci    preferencesInfo.SetBundleInfo(bundleInfo);
66996279301Sopenharmony_ci
67096279301Sopenharmony_ci    /* update slot info */
67196279301Sopenharmony_ci    std::vector<sptr<NotificationSlot>> slots;
67296279301Sopenharmony_ci    for (auto& cloneSlot : cloneBundleInfo.GetSlotInfo()) {
67396279301Sopenharmony_ci        sptr<NotificationSlot> slotInfo = new (std::nothrow) NotificationSlot(cloneSlot.slotType_);
67496279301Sopenharmony_ci        uint32_t slotFlags = bundleInfo.GetSlotFlags();
67596279301Sopenharmony_ci        auto configSlotReminderMode = DelayedSingleton<NotificationConfigParse>::GetInstance()->
67696279301Sopenharmony_ci            GetConfigSlotReminderModeByType(slotInfo->GetType());
67796279301Sopenharmony_ci        slotInfo->SetReminderMode(configSlotReminderMode & slotFlags);
67896279301Sopenharmony_ci        slotInfo->SetEnable(cloneSlot.enable_);
67996279301Sopenharmony_ci        slotInfo->SetForceControl(cloneSlot.isForceControl_);
68096279301Sopenharmony_ci        slotInfo->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED);
68196279301Sopenharmony_ci        slots.push_back(slotInfo);
68296279301Sopenharmony_ci        bundleInfo.SetSlot(slotInfo);
68396279301Sopenharmony_ci    }
68496279301Sopenharmony_ci
68596279301Sopenharmony_ci    if (!preferncesDB_->UpdateBundleSlotToDisturbeDB(userId, cloneBundleInfo.GetBundleName(),
68696279301Sopenharmony_ci        cloneBundleInfo.GetUid(), slots)) {
68796279301Sopenharmony_ci        ANS_LOGW("Clone bundle slot failed %{public}s.", cloneBundleInfo.Dump().c_str());
68896279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
68996279301Sopenharmony_ci        return;
69096279301Sopenharmony_ci    }
69196279301Sopenharmony_ci    preferencesInfo.SetBundleInfo(bundleInfo);
69296279301Sopenharmony_ci    preferencesInfo_ = preferencesInfo;
69396279301Sopenharmony_ci}
69496279301Sopenharmony_ci
69596279301Sopenharmony_civoid NotificationPreferences::GetAllCLoneBundlesInfo(int32_t userId,
69696279301Sopenharmony_ci    std::vector<NotificationCloneBundleInfo> &cloneBundles)
69796279301Sopenharmony_ci{
69896279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
69996279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
70096279301Sopenharmony_ci    std::unordered_map<std::string, std::string> bundlesMap;
70196279301Sopenharmony_ci    if (GetBatchKvsFromDb(KEY_BUNDLE_LABEL, bundlesMap, userId) != ERR_OK) {
70296279301Sopenharmony_ci        ANS_LOGE("Get bundle map info failed.");
70396279301Sopenharmony_ci        return;
70496279301Sopenharmony_ci    }
70596279301Sopenharmony_ci    preferencesInfo.GetAllCLoneBundlesInfo(userId, bundlesMap, cloneBundles);
70696279301Sopenharmony_ci    preferencesInfo_ = preferencesInfo;
70796279301Sopenharmony_ci}
70896279301Sopenharmony_ci
70996279301Sopenharmony_civoid NotificationPreferences::GetDoNotDisturbProfileListByUserId(int32_t userId,
71096279301Sopenharmony_ci    std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
71196279301Sopenharmony_ci{
71296279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
71396279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
71496279301Sopenharmony_ci    preferencesInfo.GetAllDoNotDisturbProfiles(userId, profiles);
71596279301Sopenharmony_ci    preferencesInfo_ = preferencesInfo;
71696279301Sopenharmony_ci}
71796279301Sopenharmony_ci
71896279301Sopenharmony_ciErrCode NotificationPreferences::GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> &bundleOption)
71996279301Sopenharmony_ci{
72096279301Sopenharmony_ci    ANS_LOGD("Called.");
72196279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
72296279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
72396279301Sopenharmony_ci    ErrCode result = ERR_OK;
72496279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
72596279301Sopenharmony_ci        return ERR_ANS_SERVICE_NOT_READY;
72696279301Sopenharmony_ci    }
72796279301Sopenharmony_ci    if (preferncesDB_->GetAllNotificationEnabledBundles(bundleOption)) {
72896279301Sopenharmony_ci        preferencesInfo_ = preferencesInfo;
72996279301Sopenharmony_ci    } else {
73096279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
73196279301Sopenharmony_ci    }
73296279301Sopenharmony_ci    return result;
73396279301Sopenharmony_ci}
73496279301Sopenharmony_ci
73596279301Sopenharmony_ciErrCode NotificationPreferences::ClearNotificationInRestoreFactorySettings()
73696279301Sopenharmony_ci{
73796279301Sopenharmony_ci    ErrCode result = ERR_OK;
73896279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
73996279301Sopenharmony_ci    if (!preferncesDB_->RemoveAllDataFromDisturbeDB()) {
74096279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
74196279301Sopenharmony_ci    }
74296279301Sopenharmony_ci
74396279301Sopenharmony_ci    if (result == ERR_OK) {
74496279301Sopenharmony_ci        preferencesInfo_ = NotificationPreferencesInfo();
74596279301Sopenharmony_ci    }
74696279301Sopenharmony_ci    return result;
74796279301Sopenharmony_ci}
74896279301Sopenharmony_ci
74996279301Sopenharmony_ciErrCode NotificationPreferences::GetDoNotDisturbProfile(
75096279301Sopenharmony_ci    int32_t profileId, int32_t userId, sptr<NotificationDoNotDisturbProfile> &profile)
75196279301Sopenharmony_ci{
75296279301Sopenharmony_ci    if (!CheckDoNotDisturbProfileID(profileId)) {
75396279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
75496279301Sopenharmony_ci    }
75596279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
75696279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
75796279301Sopenharmony_ci    if (!preferencesInfo.GetDoNotDisturbProfiles(profileId, userId, profile)) {
75896279301Sopenharmony_ci        return ERR_ANS_NO_PROFILE_TEMPLATE;
75996279301Sopenharmony_ci    }
76096279301Sopenharmony_ci    return ERR_OK;
76196279301Sopenharmony_ci}
76296279301Sopenharmony_ci
76396279301Sopenharmony_civoid NotificationPreferences::RemoveDoNotDisturbProfileTrustList(
76496279301Sopenharmony_ci    int32_t userId, const sptr<NotificationBundleOption> &bundleOption)
76596279301Sopenharmony_ci{
76696279301Sopenharmony_ci    if (bundleOption == nullptr) {
76796279301Sopenharmony_ci        ANS_LOGE("The bundle option is nullptr.");
76896279301Sopenharmony_ci        return;
76996279301Sopenharmony_ci    }
77096279301Sopenharmony_ci    int32_t uid = bundleOption->GetUid();
77196279301Sopenharmony_ci    int32_t appIndex = bundleOption->GetAppIndex();
77296279301Sopenharmony_ci    auto bundleName = bundleOption->GetBundleName();
77396279301Sopenharmony_ci    ANS_LOGI("Remove %{public}s %{public}d %{public}d.", bundleName.c_str(), uid, appIndex);
77496279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
77596279301Sopenharmony_ci    NotificationPreferencesInfo preferencesInfo = preferencesInfo_;
77696279301Sopenharmony_ci
77796279301Sopenharmony_ci    std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
77896279301Sopenharmony_ci    preferencesInfo.GetAllDoNotDisturbProfiles(userId, profiles);
77996279301Sopenharmony_ci    for (auto profile : profiles) {
78096279301Sopenharmony_ci        if (profile == nullptr) {
78196279301Sopenharmony_ci            ANS_LOGE("The profile is nullptr.");
78296279301Sopenharmony_ci            continue;
78396279301Sopenharmony_ci        }
78496279301Sopenharmony_ci        auto trustList = profile->GetProfileTrustList();
78596279301Sopenharmony_ci        for (auto it = trustList.begin(); it != trustList.end(); it++) {
78696279301Sopenharmony_ci            if (it->GetUid() == uid) {
78796279301Sopenharmony_ci                trustList.erase(it);
78896279301Sopenharmony_ci                break;
78996279301Sopenharmony_ci            }
79096279301Sopenharmony_ci        }
79196279301Sopenharmony_ci        profile->SetProfileTrustList(trustList);
79296279301Sopenharmony_ci    }
79396279301Sopenharmony_ci    preferencesInfo.AddDoNotDisturbProfiles(userId, profiles);
79496279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
79596279301Sopenharmony_ci        ANS_LOGE("The prefernces db is nullptr.");
79696279301Sopenharmony_ci        return;
79796279301Sopenharmony_ci    }
79896279301Sopenharmony_ci    if (!preferncesDB_->AddDoNotDisturbProfiles(userId, profiles)) {
79996279301Sopenharmony_ci        return;
80096279301Sopenharmony_ci    }
80196279301Sopenharmony_ci    preferencesInfo_ = preferencesInfo;
80296279301Sopenharmony_ci}
80396279301Sopenharmony_ci
80496279301Sopenharmony_ciErrCode NotificationPreferences::CheckSlotForCreateSlot(const sptr<NotificationBundleOption> &bundleOption,
80596279301Sopenharmony_ci    const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
80696279301Sopenharmony_ci{
80796279301Sopenharmony_ci    if (slot == nullptr) {
80896279301Sopenharmony_ci        ANS_LOGE("Notification slot is nullptr.");
80996279301Sopenharmony_ci        return ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_NOT_EXIST;
81096279301Sopenharmony_ci    }
81196279301Sopenharmony_ci
81296279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
81396279301Sopenharmony_ci    if (!preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
81496279301Sopenharmony_ci        bundleInfo.SetBundleName(bundleOption->GetBundleName());
81596279301Sopenharmony_ci        bundleInfo.SetBundleUid(bundleOption->GetUid());
81696279301Sopenharmony_ci        bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
81796279301Sopenharmony_ci    }
81896279301Sopenharmony_ci    bundleInfo.SetSlot(slot);
81996279301Sopenharmony_ci    preferencesInfo.SetBundleInfo(bundleInfo);
82096279301Sopenharmony_ci
82196279301Sopenharmony_ci    return ERR_OK;
82296279301Sopenharmony_ci}
82396279301Sopenharmony_ci
82496279301Sopenharmony_ciErrCode NotificationPreferences::CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> &bundleOption,
82596279301Sopenharmony_ci    const NotificationConstant::SlotType &slotType, NotificationPreferencesInfo &preferencesInfo) const
82696279301Sopenharmony_ci{
82796279301Sopenharmony_ci    ErrCode result = ERR_OK;
82896279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
82996279301Sopenharmony_ci    if (preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
83096279301Sopenharmony_ci        if (bundleInfo.IsExsitSlot(slotType)) {
83196279301Sopenharmony_ci            bundleInfo.RemoveSlot(slotType);
83296279301Sopenharmony_ci            preferencesInfo.SetBundleInfo(bundleInfo);
83396279301Sopenharmony_ci        } else {
83496279301Sopenharmony_ci            ANS_LOGE("Notification slot type does not exsited.");
83596279301Sopenharmony_ci            result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
83696279301Sopenharmony_ci        }
83796279301Sopenharmony_ci    } else {
83896279301Sopenharmony_ci        ANS_LOGW("Notification bundle does not exsit.");
83996279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
84096279301Sopenharmony_ci    }
84196279301Sopenharmony_ci    return result;
84296279301Sopenharmony_ci}
84396279301Sopenharmony_ci
84496279301Sopenharmony_ciErrCode NotificationPreferences::CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> &bundleOption,
84596279301Sopenharmony_ci    const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const
84696279301Sopenharmony_ci{
84796279301Sopenharmony_ci    if (slot == nullptr) {
84896279301Sopenharmony_ci        ANS_LOGE("Notification slot is nullptr.");
84996279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
85096279301Sopenharmony_ci    }
85196279301Sopenharmony_ci
85296279301Sopenharmony_ci    ErrCode result = ERR_OK;
85396279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
85496279301Sopenharmony_ci    if (preferencesInfo.GetBundleInfo(bundleOption, bundleInfo)) {
85596279301Sopenharmony_ci        if (bundleInfo.IsExsitSlot(slot->GetType())) {
85696279301Sopenharmony_ci            bundleInfo.SetBundleName(bundleOption->GetBundleName());
85796279301Sopenharmony_ci            bundleInfo.SetBundleUid(bundleOption->GetUid());
85896279301Sopenharmony_ci            bundleInfo.SetSlot(slot);
85996279301Sopenharmony_ci            preferencesInfo.SetBundleInfo(bundleInfo);
86096279301Sopenharmony_ci        } else {
86196279301Sopenharmony_ci            ANS_LOGE("Notification slot type does not exist.");
86296279301Sopenharmony_ci            result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
86396279301Sopenharmony_ci        }
86496279301Sopenharmony_ci    } else {
86596279301Sopenharmony_ci        ANS_LOGW("Notification bundle does not exsit.");
86696279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
86796279301Sopenharmony_ci    }
86896279301Sopenharmony_ci
86996279301Sopenharmony_ci    return result;
87096279301Sopenharmony_ci}
87196279301Sopenharmony_ci
87296279301Sopenharmony_citemplate <typename T>
87396279301Sopenharmony_ciErrCode NotificationPreferences::SetBundleProperty(NotificationPreferencesInfo &preferencesInfo,
87496279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
87596279301Sopenharmony_ci{
87696279301Sopenharmony_ci    ErrCode result = ERR_OK;
87796279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
87896279301Sopenharmony_ci    if (!preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) {
87996279301Sopenharmony_ci        bundleInfo.SetBundleName(bundleOption->GetBundleName());
88096279301Sopenharmony_ci        bundleInfo.SetBundleUid(bundleOption->GetUid());
88196279301Sopenharmony_ci        bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
88296279301Sopenharmony_ci    }
88396279301Sopenharmony_ci    result = SaveBundleProperty(bundleInfo, bundleOption, type, value);
88496279301Sopenharmony_ci    if (result == ERR_OK) {
88596279301Sopenharmony_ci        preferencesInfo.SetBundleInfo(bundleInfo);
88696279301Sopenharmony_ci    }
88796279301Sopenharmony_ci
88896279301Sopenharmony_ci    return result;
88996279301Sopenharmony_ci}
89096279301Sopenharmony_ci
89196279301Sopenharmony_citemplate <typename T>
89296279301Sopenharmony_ciErrCode NotificationPreferences::SaveBundleProperty(NotificationPreferencesInfo::BundleInfo &bundleInfo,
89396279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value)
89496279301Sopenharmony_ci{
89596279301Sopenharmony_ci    HaMetaMessage message = HaMetaMessage().BundleName(bundleInfo.GetBundleName());
89696279301Sopenharmony_ci    bool storeDBResult = true;
89796279301Sopenharmony_ci    switch (type) {
89896279301Sopenharmony_ci        case BundleType::BUNDLE_IMPORTANCE_TYPE:
89996279301Sopenharmony_ci            bundleInfo.SetImportance(value);
90096279301Sopenharmony_ci            storeDBResult = preferncesDB_->PutImportance(bundleInfo, value);
90196279301Sopenharmony_ci            break;
90296279301Sopenharmony_ci        case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
90396279301Sopenharmony_ci            bundleInfo.SetBadgeTotalNum(value);
90496279301Sopenharmony_ci            storeDBResult = preferncesDB_->PutTotalBadgeNums(bundleInfo, value);
90596279301Sopenharmony_ci            break;
90696279301Sopenharmony_ci        case BundleType::BUNDLE_SHOW_BADGE_TYPE:
90796279301Sopenharmony_ci            bundleInfo.SetIsShowBadge(value);
90896279301Sopenharmony_ci            storeDBResult = preferncesDB_->PutShowBadge(bundleInfo, value);
90996279301Sopenharmony_ci            break;
91096279301Sopenharmony_ci        case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
91196279301Sopenharmony_ci            bundleInfo.SetEnableNotification(value);
91296279301Sopenharmony_ci            storeDBResult = preferncesDB_->PutNotificationsEnabledForBundle(bundleInfo, value);
91396279301Sopenharmony_ci            break;
91496279301Sopenharmony_ci        case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
91596279301Sopenharmony_ci            ANS_LOGI("Into BUNDLE_POPPED_DIALOG_TYPE:SetHasPoppedDialog.");
91696279301Sopenharmony_ci            bundleInfo.SetHasPoppedDialog(value);
91796279301Sopenharmony_ci            storeDBResult = preferncesDB_->PutHasPoppedDialog(bundleInfo, value);
91896279301Sopenharmony_ci            break;
91996279301Sopenharmony_ci        case BundleType::BUNDLE_SLOTFLGS_TYPE:
92096279301Sopenharmony_ci            ANS_LOGI("Into BUNDLE_SLOTFLGS_TYPE:SetSlotFlags.");
92196279301Sopenharmony_ci            bundleInfo.SetSlotFlags(value);
92296279301Sopenharmony_ci            storeDBResult = preferncesDB_->PutSlotFlags(bundleInfo, value);
92396279301Sopenharmony_ci            break;
92496279301Sopenharmony_ci        default:
92596279301Sopenharmony_ci            break;
92696279301Sopenharmony_ci    }
92796279301Sopenharmony_ci    message.Message("Save:" + std::to_string(static_cast<int32_t>(type)) +
92896279301Sopenharmony_ci        " : " + std::to_string(value) + " : " + std::to_string(storeDBResult));
92996279301Sopenharmony_ci    NotificationAnalyticsUtil::ReportModifyEvent(message);
93096279301Sopenharmony_ci    return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
93196279301Sopenharmony_ci}
93296279301Sopenharmony_ci
93396279301Sopenharmony_citemplate <typename T>
93496279301Sopenharmony_ciErrCode NotificationPreferences::GetBundleProperty(
93596279301Sopenharmony_ci    const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, T &value)
93696279301Sopenharmony_ci{
93796279301Sopenharmony_ci    ErrCode result = ERR_OK;
93896279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
93996279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
94096279301Sopenharmony_ci    if (preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) {
94196279301Sopenharmony_ci        switch (type) {
94296279301Sopenharmony_ci            case BundleType::BUNDLE_IMPORTANCE_TYPE:
94396279301Sopenharmony_ci                value = bundleInfo.GetImportance();
94496279301Sopenharmony_ci                break;
94596279301Sopenharmony_ci            case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE:
94696279301Sopenharmony_ci                value = bundleInfo.GetBadgeTotalNum();
94796279301Sopenharmony_ci                break;
94896279301Sopenharmony_ci            case BundleType::BUNDLE_SHOW_BADGE_TYPE:
94996279301Sopenharmony_ci                value = bundleInfo.GetIsShowBadge();
95096279301Sopenharmony_ci                break;
95196279301Sopenharmony_ci            case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE:
95296279301Sopenharmony_ci                value = bundleInfo.GetEnableNotification();
95396279301Sopenharmony_ci                break;
95496279301Sopenharmony_ci            case BundleType::BUNDLE_POPPED_DIALOG_TYPE:
95596279301Sopenharmony_ci                ANS_LOGD("Into BUNDLE_POPPED_DIALOG_TYPE:GetHasPoppedDialog.");
95696279301Sopenharmony_ci                value = bundleInfo.GetHasPoppedDialog();
95796279301Sopenharmony_ci                break;
95896279301Sopenharmony_ci            case BundleType::BUNDLE_SLOTFLGS_TYPE:
95996279301Sopenharmony_ci                value = bundleInfo.GetSlotFlags();
96096279301Sopenharmony_ci                ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:GetSlotFlags.");
96196279301Sopenharmony_ci                break;
96296279301Sopenharmony_ci            default:
96396279301Sopenharmony_ci                result = ERR_ANS_INVALID_PARAM;
96496279301Sopenharmony_ci                break;
96596279301Sopenharmony_ci        }
96696279301Sopenharmony_ci    } else {
96796279301Sopenharmony_ci        ANS_LOGW("Notification bundle does not exsit.");
96896279301Sopenharmony_ci        result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST;
96996279301Sopenharmony_ci    }
97096279301Sopenharmony_ci    return result;
97196279301Sopenharmony_ci}
97296279301Sopenharmony_ci
97396279301Sopenharmony_cistd::string NotificationPreferences::GenerateBundleKey(const sptr<NotificationBundleOption> &bundleOption) const
97496279301Sopenharmony_ci{
97596279301Sopenharmony_ci    return bundleOption->GetBundleName().append(std::to_string(bundleOption->GetUid()));
97696279301Sopenharmony_ci}
97796279301Sopenharmony_ci
97896279301Sopenharmony_ciErrCode NotificationPreferences::GetTemplateSupported(const std::string& templateName, bool &support)
97996279301Sopenharmony_ci{
98096279301Sopenharmony_ci    if (templateName.length() == 0) {
98196279301Sopenharmony_ci        ANS_LOGE("template name is null.");
98296279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
98396279301Sopenharmony_ci    }
98496279301Sopenharmony_ci
98596279301Sopenharmony_ci    std::ifstream inFile;
98696279301Sopenharmony_ci    inFile.open(DEFAULT_TEMPLATE_PATH.c_str(), std::ios::in);
98796279301Sopenharmony_ci    if (!inFile.is_open()) {
98896279301Sopenharmony_ci        ANS_LOGE("read template config error.");
98996279301Sopenharmony_ci        return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
99096279301Sopenharmony_ci    }
99196279301Sopenharmony_ci
99296279301Sopenharmony_ci    nlohmann::json jsonObj;
99396279301Sopenharmony_ci    inFile >> jsonObj;
99496279301Sopenharmony_ci    if (jsonObj.is_null() || !jsonObj.is_object()) {
99596279301Sopenharmony_ci        ANS_LOGE("Invalid JSON object");
99696279301Sopenharmony_ci        return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
99796279301Sopenharmony_ci    }
99896279301Sopenharmony_ci    if (jsonObj.is_discarded()) {
99996279301Sopenharmony_ci        ANS_LOGE("template json discarded error.");
100096279301Sopenharmony_ci        inFile.close();
100196279301Sopenharmony_ci        return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED;
100296279301Sopenharmony_ci    }
100396279301Sopenharmony_ci
100496279301Sopenharmony_ci    if (jsonObj.contains(templateName)) {
100596279301Sopenharmony_ci        support = true;
100696279301Sopenharmony_ci    }
100796279301Sopenharmony_ci
100896279301Sopenharmony_ci    jsonObj.clear();
100996279301Sopenharmony_ci    inFile.close();
101096279301Sopenharmony_ci    return ERR_OK;
101196279301Sopenharmony_ci}
101296279301Sopenharmony_ci
101396279301Sopenharmony_ciErrCode NotificationPreferences::SetDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
101496279301Sopenharmony_ci    const std::string &deviceType, const bool enabled)
101596279301Sopenharmony_ci{
101696279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
101796279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
101896279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
101996279301Sopenharmony_ci    }
102096279301Sopenharmony_ci
102196279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
102296279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
102396279301Sopenharmony_ci    bundleInfo.SetBundleName(bundleOption->GetBundleName());
102496279301Sopenharmony_ci    bundleInfo.SetBundleUid(bundleOption->GetUid());
102596279301Sopenharmony_ci    bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
102696279301Sopenharmony_ci    bool storeDBResult = true;
102796279301Sopenharmony_ci    storeDBResult = preferncesDB_->PutDistributedEnabledForBundle(deviceType, bundleInfo, enabled);
102896279301Sopenharmony_ci    return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
102996279301Sopenharmony_ci}
103096279301Sopenharmony_ci
103196279301Sopenharmony_ciErrCode NotificationPreferences::IsDistributedEnabledByBundle(const sptr<NotificationBundleOption> &bundleOption,
103296279301Sopenharmony_ci    const std::string &deviceType, bool &enabled)
103396279301Sopenharmony_ci{
103496279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
103596279301Sopenharmony_ci    if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) {
103696279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
103796279301Sopenharmony_ci    }
103896279301Sopenharmony_ci
103996279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
104096279301Sopenharmony_ci    NotificationPreferencesInfo::BundleInfo bundleInfo;
104196279301Sopenharmony_ci    bundleInfo.SetBundleName(bundleOption->GetBundleName());
104296279301Sopenharmony_ci    bundleInfo.SetBundleUid(bundleOption->GetUid());
104396279301Sopenharmony_ci    bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption));
104496279301Sopenharmony_ci    bool storeDBResult = true;
104596279301Sopenharmony_ci    storeDBResult = preferncesDB_->GetDistributedEnabledForBundle(deviceType, bundleInfo, enabled);
104696279301Sopenharmony_ci    return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
104796279301Sopenharmony_ci}
104896279301Sopenharmony_ci
104996279301Sopenharmony_ciErrCode NotificationPreferences::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled)
105096279301Sopenharmony_ci{
105196279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
105296279301Sopenharmony_ci    if (deviceType.empty()) {
105396279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
105496279301Sopenharmony_ci    }
105596279301Sopenharmony_ci
105696279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
105796279301Sopenharmony_ci    bool storeDBResult = true;
105896279301Sopenharmony_ci    storeDBResult = preferncesDB_->SetSmartReminderEnabled(deviceType, enabled);
105996279301Sopenharmony_ci    return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
106096279301Sopenharmony_ci}
106196279301Sopenharmony_ci
106296279301Sopenharmony_ciErrCode NotificationPreferences::IsSmartReminderEnabled(const std::string &deviceType, bool &enabled)
106396279301Sopenharmony_ci{
106496279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
106596279301Sopenharmony_ci    if (deviceType.empty()) {
106696279301Sopenharmony_ci        return ERR_ANS_INVALID_PARAM;
106796279301Sopenharmony_ci    }
106896279301Sopenharmony_ci
106996279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
107096279301Sopenharmony_ci    bool storeDBResult = true;
107196279301Sopenharmony_ci    storeDBResult = preferncesDB_->IsSmartReminderEnabled(deviceType, enabled);
107296279301Sopenharmony_ci    return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED;
107396279301Sopenharmony_ci}
107496279301Sopenharmony_ci
107596279301Sopenharmony_civoid NotificationPreferences::InitSettingFromDisturbDB(int32_t userId)
107696279301Sopenharmony_ci{
107796279301Sopenharmony_ci    ANS_LOGI("%{public}s userId is %{public}d", __FUNCTION__, userId);
107896279301Sopenharmony_ci    std::lock_guard<std::mutex> lock(preferenceMutex_);
107996279301Sopenharmony_ci    if (preferncesDB_ != nullptr) {
108096279301Sopenharmony_ci        preferncesDB_->ParseFromDisturbeDB(preferencesInfo_, userId);
108196279301Sopenharmony_ci    }
108296279301Sopenharmony_ci}
108396279301Sopenharmony_ci
108496279301Sopenharmony_civoid NotificationPreferences::RemoveSettings(int32_t userId)
108596279301Sopenharmony_ci{
108696279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
108796279301Sopenharmony_ci
108896279301Sopenharmony_ci    {
108996279301Sopenharmony_ci        std::lock_guard<std::mutex> lock(preferenceMutex_);
109096279301Sopenharmony_ci        preferencesInfo_.RemoveNotificationEnable(userId);
109196279301Sopenharmony_ci        preferencesInfo_.RemoveDoNotDisturbDate(userId);
109296279301Sopenharmony_ci    }
109396279301Sopenharmony_ci
109496279301Sopenharmony_ci    if (preferncesDB_ != nullptr) {
109596279301Sopenharmony_ci        preferncesDB_->RemoveNotificationEnable(userId);
109696279301Sopenharmony_ci        preferncesDB_->RemoveDoNotDisturbDate(userId);
109796279301Sopenharmony_ci        preferncesDB_->DropUserTable(userId);
109896279301Sopenharmony_ci    }
109996279301Sopenharmony_ci}
110096279301Sopenharmony_ci
110196279301Sopenharmony_cibool NotificationPreferences::CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption) const
110296279301Sopenharmony_ci{
110396279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
110496279301Sopenharmony_ci    std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
110596279301Sopenharmony_ci    if (bundleManager == nullptr) {
110696279301Sopenharmony_ci        return false;
110796279301Sopenharmony_ci    }
110896279301Sopenharmony_ci    return bundleManager->CheckApiCompatibility(bundleOption);
110996279301Sopenharmony_ci}
111096279301Sopenharmony_ci
111196279301Sopenharmony_civoid NotificationPreferences::RemoveAnsBundleDbInfo(const sptr<NotificationBundleOption> &bundleOption)
111296279301Sopenharmony_ci{
111396279301Sopenharmony_ci    ANS_LOGE("%{public}s", __FUNCTION__);
111496279301Sopenharmony_ci    if (preferncesDB_ != nullptr && bundleOption != nullptr) {
111596279301Sopenharmony_ci        preferncesDB_->RemoveAnsBundleDbInfo(bundleOption->GetBundleName(), bundleOption->GetUid());
111696279301Sopenharmony_ci    }
111796279301Sopenharmony_ci}
111896279301Sopenharmony_ci
111996279301Sopenharmony_civoid NotificationPreferences::RemoveEnabledDbByBundle(const sptr<NotificationBundleOption> &bundleOption)
112096279301Sopenharmony_ci{
112196279301Sopenharmony_ci    ANS_LOGE("%{public}s", __FUNCTION__);
112296279301Sopenharmony_ci    if (preferncesDB_ != nullptr && bundleOption != nullptr) {
112396279301Sopenharmony_ci        std::lock_guard<std::mutex> lock(preferenceMutex_);
112496279301Sopenharmony_ci        preferncesDB_->RemoveEnabledDbByBundleName(bundleOption->GetBundleName(), bundleOption->GetUid());
112596279301Sopenharmony_ci    }
112696279301Sopenharmony_ci}
112796279301Sopenharmony_ci
112896279301Sopenharmony_cibool NotificationPreferences::GetBundleSoundPermission(bool &allPackage, std::set<std::string> &bundleNames)
112996279301Sopenharmony_ci{
113096279301Sopenharmony_ci    ANS_LOGD("%{public}s", __FUNCTION__);
113196279301Sopenharmony_ci    std::string value = "";
113296279301Sopenharmony_ci    int32_t userId = -1;
113396279301Sopenharmony_ci    OsAccountManagerHelper::GetInstance().GetCurrentCallingUserId(userId);
113496279301Sopenharmony_ci    if (GetKvFromDb("RING_TRUSTLIST_PKG", value, userId) != ERR_OK) {
113596279301Sopenharmony_ci        ANS_LOGD("Get bundle sound permission failed.");
113696279301Sopenharmony_ci        return false;
113796279301Sopenharmony_ci    }
113896279301Sopenharmony_ci
113996279301Sopenharmony_ci    ANS_LOGD("The bundle permission is :%{public}s.", value.c_str());
114096279301Sopenharmony_ci    nlohmann::json jsonPermission = nlohmann::json::parse(value, nullptr, false);
114196279301Sopenharmony_ci    if (jsonPermission.is_null() || jsonPermission.empty()) {
114296279301Sopenharmony_ci        ANS_LOGE("Invalid JSON object");
114396279301Sopenharmony_ci        return false;
114496279301Sopenharmony_ci    }
114596279301Sopenharmony_ci    if (jsonPermission.is_discarded() || !jsonPermission.is_array()) {
114696279301Sopenharmony_ci        ANS_LOGE("Parse bundle permission failed due to data is discarded or not array");
114796279301Sopenharmony_ci        return false;
114896279301Sopenharmony_ci    }
114996279301Sopenharmony_ci
115096279301Sopenharmony_ci    for (const auto &item : jsonPermission) {
115196279301Sopenharmony_ci        bundleNames.insert(item);
115296279301Sopenharmony_ci        if (item == "ALL_PKG") {
115396279301Sopenharmony_ci            allPackage = true;
115496279301Sopenharmony_ci        }
115596279301Sopenharmony_ci    }
115696279301Sopenharmony_ci    return true;
115796279301Sopenharmony_ci}
115896279301Sopenharmony_ci
115996279301Sopenharmony_ciint32_t NotificationPreferences::SetKvToDb(
116096279301Sopenharmony_ci    const std::string &key, const std::string &value, const int32_t &userId)
116196279301Sopenharmony_ci{
116296279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
116396279301Sopenharmony_ci        return ERR_ANS_SERVICE_NOT_READY;
116496279301Sopenharmony_ci    }
116596279301Sopenharmony_ci    return preferncesDB_->SetKvToDb(key, value, userId);
116696279301Sopenharmony_ci}
116796279301Sopenharmony_ci
116896279301Sopenharmony_ciint32_t NotificationPreferences::SetByteToDb(
116996279301Sopenharmony_ci    const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId)
117096279301Sopenharmony_ci{
117196279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
117296279301Sopenharmony_ci        return ERR_ANS_SERVICE_NOT_READY;
117396279301Sopenharmony_ci    }
117496279301Sopenharmony_ci    return preferncesDB_->SetByteToDb(key, value, userId);
117596279301Sopenharmony_ci}
117696279301Sopenharmony_ci
117796279301Sopenharmony_ciint32_t NotificationPreferences::GetKvFromDb(
117896279301Sopenharmony_ci    const std::string &key, std::string &value, const int32_t &userId)
117996279301Sopenharmony_ci{
118096279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
118196279301Sopenharmony_ci        return ERR_ANS_SERVICE_NOT_READY;
118296279301Sopenharmony_ci    }
118396279301Sopenharmony_ci    return preferncesDB_->GetKvFromDb(key, value, userId);
118496279301Sopenharmony_ci}
118596279301Sopenharmony_ci
118696279301Sopenharmony_ciint32_t NotificationPreferences::GetByteFromDb(
118796279301Sopenharmony_ci    const std::string &key, std::vector<uint8_t> &value, const int32_t &userId)
118896279301Sopenharmony_ci{
118996279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
119096279301Sopenharmony_ci        return ERR_ANS_SERVICE_NOT_READY;
119196279301Sopenharmony_ci    }
119296279301Sopenharmony_ci    return preferncesDB_->GetByteFromDb(key, value, userId);
119396279301Sopenharmony_ci}
119496279301Sopenharmony_ci
119596279301Sopenharmony_ciint32_t NotificationPreferences::GetBatchKvsFromDb(
119696279301Sopenharmony_ci    const std::string &key, std::unordered_map<std::string, std::string> &values, const int32_t &userId)
119796279301Sopenharmony_ci{
119896279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
119996279301Sopenharmony_ci        return ERR_ANS_SERVICE_NOT_READY;
120096279301Sopenharmony_ci    }
120196279301Sopenharmony_ci    return preferncesDB_->GetBatchKvsFromDb(key, values, userId);
120296279301Sopenharmony_ci}
120396279301Sopenharmony_ci
120496279301Sopenharmony_ciint32_t NotificationPreferences::DeleteKvFromDb(const std::string &key, const int32_t &userId)
120596279301Sopenharmony_ci{
120696279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
120796279301Sopenharmony_ci        return ERR_ANS_SERVICE_NOT_READY;
120896279301Sopenharmony_ci    }
120996279301Sopenharmony_ci    return preferncesDB_->DeleteKvFromDb(key, userId);
121096279301Sopenharmony_ci}
121196279301Sopenharmony_ci
121296279301Sopenharmony_cibool NotificationPreferences::IsAgentRelationship(const std::string &agentBundleName,
121396279301Sopenharmony_ci    const std::string &sourceBundleName)
121496279301Sopenharmony_ci{
121596279301Sopenharmony_ci    if (AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) {
121696279301Sopenharmony_ci        ANS_LOGD("Client has agent permission.");
121796279301Sopenharmony_ci        return true;
121896279301Sopenharmony_ci    }
121996279301Sopenharmony_ci
122096279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
122196279301Sopenharmony_ci        ANS_LOGD("perferencdDb is null.");
122296279301Sopenharmony_ci        return false;
122396279301Sopenharmony_ci    }
122496279301Sopenharmony_ci
122596279301Sopenharmony_ci    return preferncesDB_->IsAgentRelationship(agentBundleName, sourceBundleName);
122696279301Sopenharmony_ci}
122796279301Sopenharmony_ci
122896279301Sopenharmony_cistd::string NotificationPreferences::GetAdditionalConfig(const std::string &key)
122996279301Sopenharmony_ci{
123096279301Sopenharmony_ci    if (preferncesDB_ == nullptr) {
123196279301Sopenharmony_ci        return "";
123296279301Sopenharmony_ci    }
123396279301Sopenharmony_ci    return preferncesDB_->GetAdditionalConfig(key);
123496279301Sopenharmony_ci}
123596279301Sopenharmony_ci}  // namespace Notification
123696279301Sopenharmony_ci}  // namespace OHOS
1237