1 /*
2 * Copyright (c) 2023 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 *e
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 "device_timed_collect_tool.h"
17
18 #include <sys/stat.h>
19 #include <sys/types.h>
20
21 #include "preferences_errno.h"
22 #include "preferences_helper.h"
23 #include "preferences_value.h"
24 #include "sam_log.h"
25 #include "system_ability_manager.h"
26 using namespace std;
27
28 namespace OHOS {
GetInstance()29 shared_ptr<PreferencesUtil> PreferencesUtil::GetInstance()
30 {
31 static std::shared_ptr<PreferencesUtil> instance = make_shared<PreferencesUtil>();
32 return instance;
33 }
34
GetPreference()35 bool PreferencesUtil::GetPreference()
36 {
37 if (ptr_ != nullptr) {
38 return true;
39 }
40 std::string path = "/data/samgr/samgr.xml";
41 int32_t errCode = ERR_INVALID_VALUE;
42 ptr_ = NativePreferences::PreferencesHelper::GetPreferences(path, errCode);
43 if (ptr_ == nullptr) {
44 HILOGE("GetPreference error code is %{public}d", errCode);
45 return false;
46 }
47 return true;
48 }
49
SaveLong(const std::string & key,int64_t value)50 bool PreferencesUtil::SaveLong(const std::string& key, int64_t value)
51 {
52 return Save(key, value);
53 }
54
SaveString(const std::string & key,std::string value)55 bool PreferencesUtil::SaveString(const std::string& key, std::string value)
56 {
57 return Save(key, value);
58 }
59
60 template <typename T>
Save(const std::string & key,const T & value)61 bool PreferencesUtil::Save(const std::string& key, const T& value)
62 {
63 if (!GetPreference()) {
64 return false;
65 }
66 if (!SaveInner(ptr_, key, value)) {
67 HILOGE("SaveInner error");
68 return false;
69 }
70 return RefreshSync();
71 }
72
SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr,const std::string & key,const int64_t & value)73 bool PreferencesUtil::SaveInner(
74 std::shared_ptr<NativePreferences::Preferences> ptr, const std::string& key, const int64_t& value)
75 {
76 if (ptr == nullptr) {
77 HILOGE("ptr is nullptr");
78 return false;
79 }
80 return ptr->PutLong(key, value) == NativePreferences::E_OK;
81 }
82
SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr,const std::string & key,const string & value)83 bool PreferencesUtil::SaveInner(
84 std::shared_ptr<NativePreferences::Preferences> ptr, const std::string& key, const string& value)
85 {
86 if (ptr == nullptr) {
87 HILOGE("ptr is nullptr");
88 return false;
89 }
90 return ptr->PutString(key, value) == NativePreferences::E_OK;
91 }
92
ObtainAll()93 std::map<std::string, NativePreferences::PreferencesValue> PreferencesUtil::ObtainAll()
94 {
95 if (!GetPreference()) {
96 return {};
97 }
98 if (ptr_ == nullptr) {
99 HILOGE("ptr_ is nullptr");
100 return {};
101 }
102 return ptr_->GetAll();
103 }
104
ObtainLong(const std::string & key,int64_t defValue)105 int64_t PreferencesUtil::ObtainLong(const std::string& key, int64_t defValue)
106 {
107 return Obtain(key, defValue);
108 }
109
ObtainString(const std::string & key,std::string defValue)110 string PreferencesUtil::ObtainString(const std::string& key, std::string defValue)
111 {
112 return Obtain(key, defValue);
113 }
114
115 template <typename T>
Obtain(const std::string & key,const T & defValue)116 T PreferencesUtil::Obtain(const std::string& key, const T& defValue)
117 {
118 if (!GetPreference()) {
119 HILOGI("Obtain GetPreference failed");
120 return defValue;
121 }
122 return ObtainInner(ptr_, key, defValue);
123 }
124
ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr,const std::string & key,const int64_t & defValue)125 int64_t PreferencesUtil::ObtainInner(
126 std::shared_ptr<NativePreferences::Preferences> ptr, const std::string& key, const int64_t& defValue)
127 {
128 return ptr->GetLong(key, defValue);
129 }
130
ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr,const std::string & key,const std::string & defValue)131 std::string PreferencesUtil::ObtainInner(
132 std::shared_ptr<NativePreferences::Preferences> ptr, const std::string& key, const std::string& defValue)
133 {
134 return ptr->GetString(key, defValue);
135 }
136
RefreshSync()137 bool PreferencesUtil::RefreshSync()
138 {
139 if (!GetPreference()) {
140 HILOGI("RefreshSync GetPreference failed");
141 return false;
142 }
143 if (ptr_ == nullptr) {
144 HILOGE("ptr_ is nullptr");
145 return false;
146 }
147 if (ptr_->FlushSync() != NativePreferences::E_OK) {
148 HILOGE("RefreshSync error");
149 return false;
150 }
151 return true;
152 }
153
IsExist(const std::string & key)154 bool PreferencesUtil::IsExist(const std::string& key)
155 {
156 if (!GetPreference()) {
157 HILOGI("IsExist GetPreference failed");
158 return false;
159 }
160 if (ptr_ == nullptr) {
161 HILOGE("ptr_ is nullptr");
162 return false;
163 }
164 return ptr_->HasKey(key);
165 }
166
Remove(const std::string & key)167 bool PreferencesUtil::Remove(const std::string &key)
168 {
169 if (!GetPreference()) {
170 HILOGI("Remove GetPreference failed");
171 return false;
172 }
173 if (ptr_ == nullptr) {
174 HILOGE("ptr_ is nullptr");
175 return false;
176 }
177 if (ptr_->Delete(key) != NativePreferences::E_OK) {
178 return false;
179 }
180 return RefreshSync();
181 }
182 }