1 /*
2  * Copyright (c) 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 "distributed_preferences_info.h"
17 
18 #include "ans_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace Notification {
DistributedPreferencesInfo()22 DistributedPreferencesInfo::DistributedPreferencesInfo()
23 {}
24 
~DistributedPreferencesInfo()25 DistributedPreferencesInfo::~DistributedPreferencesInfo()
26 {}
27 
SetDistributedEnable(bool enable)28 void DistributedPreferencesInfo::SetDistributedEnable(bool enable)
29 {
30     distributedEnable_ = enable;
31 }
32 
GetDistributedEnable()33 bool DistributedPreferencesInfo::GetDistributedEnable()
34 {
35     return distributedEnable_;
36 }
37 
SetDistributedBundleEnable(const std::string & bundleName,int32_t uid,bool enable)38 void DistributedPreferencesInfo::SetDistributedBundleEnable(const std::string &bundleName, int32_t uid, bool enable)
39 {
40     bundleEnable_[std::make_pair(bundleName, uid)] = enable;
41 }
42 
GetDistributedBundleEnable(const std::string & bundleName,int32_t uid)43 bool DistributedPreferencesInfo::GetDistributedBundleEnable(const std::string &bundleName, int32_t uid)
44 {
45     auto iter = bundleEnable_.find(std::make_pair(bundleName, uid));
46     if (iter == bundleEnable_.end()) {
47         ANS_LOGW("bundle %{public}s(%{public}d) not found.", bundleName.c_str(), uid);
48         return true;
49     }
50 
51     return iter->second;
52 }
53 
DeleteDistributedBundleInfo(const std::string & bundleName,int32_t uid)54 void DistributedPreferencesInfo::DeleteDistributedBundleInfo(const std::string &bundleName, int32_t uid)
55 {
56     bundleEnable_.erase(std::make_pair(bundleName, uid));
57 }
58 
SetSyncEnabledWithoutApp(const int32_t userId,const bool enabled)59 void DistributedPreferencesInfo::SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled)
60 {
61     enabledWithoutApp_[userId] = enabled;
62 }
63 
GetSyncEnabledWithoutApp(const int32_t userId,bool & enabled)64 ErrCode DistributedPreferencesInfo::GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled)
65 {
66     auto iter = enabledWithoutApp_.find(userId);
67     if (iter == enabledWithoutApp_.end()) {
68         enabled = false;
69         ANS_LOGW("userId(%{public}d) not found. enabled default false", userId);
70     } else {
71         enabled = iter->second;
72         ANS_LOGI("userId(%{public}d) enabled = %{public}d", userId, enabled);
73     }
74     return ERR_OK;
75 }
76 }  // namespace Notification
77 }  // namespace OHOS