1 /*
2 * Copyright (C) 2024 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 #include "distributed_preference.h"
16
17 #include "ans_log_wrapper.h"
18
19 namespace OHOS {
20 namespace Notification {
21 namespace {
22 const static std::string BUNDL_ICON_KEY_PREFIX = "dans_icon_";
23 }
24
25
DistributedPreferences()26 DistributedPreferences::DistributedPreferences()
27 {
28 DistributedRdbConfig rdbConfig;
29 preferncesDB_ = std::make_shared<DistributedRdbHelper>(rdbConfig);
30 preferncesDB_->Init();
31 ANS_LOGD("Distributed Rdb is created");
32 }
33
GetInstance()34 DistributedPreferences& DistributedPreferences::GetInstance()
35 {
36 static DistributedPreferences distributedPreferences;
37 return distributedPreferences;
38 }
39
InsertBundleIcon(const std::string & bundleName,const std::string & icon)40 int32_t DistributedPreferences::InsertBundleIcon(const std::string &bundleName, const std::string &icon)
41 {
42 std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
43 if (preferncesDB_ == nullptr) {
44 ANS_LOGW("Prefernces handler is nullptr.");
45 return -1;
46 }
47 if (preferncesDB_->InsertData(BUNDL_ICON_KEY_PREFIX + bundleName, icon) != NativeRdb::E_OK) {
48 ANS_LOGW("Prefernces Insert data failed %{public}s.", bundleName.c_str());
49 return -1;
50 }
51 return 0;
52 }
53
DeleteBundleIcon(const std::string & bundleName)54 int32_t DistributedPreferences::DeleteBundleIcon(const std::string &bundleName)
55 {
56 std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
57 if (preferncesDB_ == nullptr) {
58 ANS_LOGW("Prefernces handler is nullptr.");
59 return -1;
60 }
61 if (preferncesDB_->DeleteData(BUNDL_ICON_KEY_PREFIX + bundleName) != NativeRdb::E_OK) {
62 ANS_LOGW("Prefernces delet data failed %{public}s.", bundleName.c_str());
63 return -1;
64 }
65 return 0;
66 }
67
InsertBatchBundleIcons(std::unordered_map<std::string,std::string> & values)68 int32_t DistributedPreferences::InsertBatchBundleIcons(std::unordered_map<std::string, std::string> &values)
69 {
70 std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
71 if (preferncesDB_ == nullptr) {
72 ANS_LOGW("Prefernces handler is nullptr.");
73 return -1;
74 }
75 std::unordered_map<std::string, std::string> bundlesIcon;
76 for (auto& item : values) {
77 bundlesIcon.insert(std::make_pair(BUNDL_ICON_KEY_PREFIX + item.first, item.second));
78 }
79 if (preferncesDB_->InsertBatchData(bundlesIcon) != NativeRdb::E_OK) {
80 ANS_LOGW("Prefernces Insert batch data failed.");
81 return -1;
82 }
83 return 0;
84 }
85
GetIconByBundleName(const std::string & bundleName,std::string & icon)86 int32_t DistributedPreferences::GetIconByBundleName(const std::string& bundleName, std::string &icon)
87 {
88 std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
89 if (preferncesDB_ == nullptr) {
90 ANS_LOGW("Prefernces handler is nullptr.");
91 return -1;
92 }
93 if (preferncesDB_->QueryData(BUNDL_ICON_KEY_PREFIX + bundleName, icon) != NativeRdb::E_OK) {
94 ANS_LOGW("Prefernces query data failed %{public}s.", bundleName.c_str());
95 return -1;
96 }
97 return 0;
98 }
99
GetSavedBundlesIcon(std::vector<std::string> & bundleNames)100 int32_t DistributedPreferences::GetSavedBundlesIcon(std::vector<std::string>& bundleNames)
101 {
102 std::lock_guard<ffrt::mutex> lock(preferenceMutex_);
103 if (preferncesDB_ == nullptr) {
104 ANS_LOGW("Prefernces handler is nullptr.");
105 return -1;
106 }
107 std::unordered_map<std::string, std::string> values;
108 if (preferncesDB_->QueryDataBeginWithKey(BUNDL_ICON_KEY_PREFIX, values) != NativeRdb::E_OK) {
109 ANS_LOGW("Prefernces saved data failed.");
110 return -1;
111 }
112 int32_t prefixLength = BUNDL_ICON_KEY_PREFIX.size();
113 for (auto item : values) {
114 std::string bundleName = item.first.substr(prefixLength, item.first.size() - prefixLength);
115 bundleNames.push_back(bundleName);
116 ANS_LOGI("Prefernces saved data %{public}s %{public}d.", bundleName.c_str(), (int32_t)(item.second.size()));
117 }
118 return 0;
119 }
120 }
121 }
122