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
16 #include "ringtone_utils.h"
17
18 #include <securec.h>
19 #include <sys/statvfs.h>
20
21 #include "parameter.h"
22 #include "directory_ex.h"
23 #include "dfx_const.h"
24 #include "rdb_sql_utils.h"
25 #include "ringtone_errno.h"
26 #include "ringtone_log.h"
27 #include "ringtone_type.h"
28 #include "ringtone_db_const.h"
29 #include "ringtone_rdb_callbacks.h"
30 #include "preferences_helper.h"
31 #include "rdb_store_config.h"
32
33 namespace OHOS {
34 namespace Media {
35 using namespace std;
36 const char RINGTONE_PARAMETER_SCANNER_COMPLETED_KEY[] = "ringtone.scanner.completed";
37 const int RINGTONE_PARAMETER_SCANNER_COMPLETED_TRUE = 1;
38 const int RINGTONE_PARAMETER_SCANNER_COMPLETED_FALSE = 0;
39 static const int32_t SYSPARA_SIZE = 128;
40 static const int32_t NO_NEED_SCANNER = 1;
41 static const int32_t RDB_AREA_EL2 = 2;
42 constexpr int32_t MAX_SIZE = 9;
43 const std::string DATA_PATH = "/data/storage/el2/base";
44 const int32_t VIDEO_FREE_SIZE_LIMIT_10 = 10;
45 const int64_t UNIT = 1000;
46 const int64_t STD_UNIT = 1024;
47 const int64_t THRESHOLD = 512;
48 const int64_t PERCENTAGE = 100;
49
ReplaceAll(std::string str,const std::string & oldValue,const std::string & newValue)50 std::string RingtoneUtils::ReplaceAll(std::string str, const std::string &oldValue, const std::string &newValue)
51 {
52 for (std::string::size_type pos(0); pos != std::string::npos; pos += newValue.length()) {
53 if ((pos = str.find(oldValue, pos)) != std::string::npos) {
54 str.replace(pos, oldValue.length(), newValue);
55 } else {
56 break;
57 }
58 }
59 return str;
60 }
61
GetDefaultSystemtoneInfo()62 std::map<int, std::string> RingtoneUtils::GetDefaultSystemtoneInfo()
63 {
64 map<int, string> defaultSystemtoneInfo;
65 char paramValue[SYSPARA_SIZE] = {0};
66 GetParameter(PARAM_RINGTONE_SETTING_RINGTONE, "", paramValue, SYSPARA_SIZE);
67 if (strlen(paramValue) > 0) {
68 defaultSystemtoneInfo.insert(make_pair(DEFAULT_RING_TYPE_SIM_CARD_1, string(paramValue)));
69 }
70
71 if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) {
72 GetParameter(PARAM_RINGTONE_SETTING_RINGTONE2, "", paramValue, SYSPARA_SIZE);
73 if (strlen(paramValue) > 0) {
74 defaultSystemtoneInfo.insert(make_pair(DEFAULT_RING_TYPE_SIM_CARD_2, string(paramValue)));
75 }
76 }
77
78 if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) {
79 GetParameter(PARAM_RINGTONE_SETTING_SHOT, "", paramValue, SYSPARA_SIZE);
80 if (strlen(paramValue) > 0) {
81 defaultSystemtoneInfo.insert(make_pair(DEFAULT_SHOT_TYPE_SIM_CARD_1, string(paramValue)));
82 }
83 }
84
85 if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) {
86 GetParameter(PARAM_RINGTONE_SETTING_SHOT2, "", paramValue, SYSPARA_SIZE);
87 if (strlen(paramValue) > 0) {
88 defaultSystemtoneInfo.insert(make_pair(DEFAULT_SHOT_TYPE_SIM_CARD_2, string(paramValue)));
89 }
90 }
91
92 if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) {
93 GetParameter(PARAM_RINGTONE_SETTING_NOTIFICATIONTONE, "", paramValue, SYSPARA_SIZE);
94 if (strlen(paramValue) > 0) {
95 defaultSystemtoneInfo.insert(make_pair(DEFAULT_NOTIFICATION_TYPE, string(paramValue)));
96 }
97 }
98
99 if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) {
100 GetParameter(PARAM_RINGTONE_SETTING_ALARM, "", paramValue, SYSPARA_SIZE);
101 if (strlen(paramValue) > 0) {
102 defaultSystemtoneInfo.insert(make_pair(DEFAULT_ALARM_TYPE, string(paramValue)));
103 }
104 }
105
106 return defaultSystemtoneInfo;
107 }
108
ChecMoveDb()109 int32_t RingtoneUtils::ChecMoveDb()
110 {
111 auto ret = RingtoneUtils::CheckNeedScanner(COMMON_XML_EL1);
112 if (ret == NO_NEED_SCANNER) {
113 RINGTONE_INFO_LOG("no need to scanner el1");
114 return NO_NEED_SCANNER;
115 } else if (ret == E_ERR) {
116 RINGTONE_INFO_LOG("open el1 xml error");
117 return E_ERR;
118 }
119
120 ret = RingtoneUtils::CheckNeedScanner(DFX_COMMON_XML);
121 if (ret == E_ERR) {
122 RINGTONE_INFO_LOG("open el2 xml error");
123 return E_ERR;
124 } else if (ret == E_OK) {
125 RINGTONE_INFO_LOG("need to scanner el2");
126 return E_OK;
127 }
128 if (!RingtoneUtils::MoveEL2DBToEL1DB()) {
129 RINGTONE_INFO_LOG("move error");
130 return E_ERR;
131 }
132 RingtoneUtils::SetMoveEL2DBToEL1();
133 return E_OK;
134 }
135
CheckNeedScanner(const std::string & xmlFilePath)136 int32_t RingtoneUtils::CheckNeedScanner(const std::string &xmlFilePath)
137 {
138 int32_t errCode;
139 shared_ptr<NativePreferences::Preferences> prefs =
140 NativePreferences::PreferencesHelper::GetPreferences(xmlFilePath, errCode);
141 if (!prefs) {
142 RINGTONE_ERR_LOG("get el1 preferences error: %{public}d", errCode);
143 return E_ERR;
144 }
145 int isCompleted = prefs->GetInt(RINGTONE_PARAMETER_SCANNER_COMPLETED_KEY,
146 RINGTONE_PARAMETER_SCANNER_COMPLETED_FALSE);
147 if (!isCompleted) {
148 return E_OK;
149 }
150 return NO_NEED_SCANNER;
151 }
152
MoveEL2DBToEL1DB()153 bool RingtoneUtils::MoveEL2DBToEL1DB()
154 {
155 int32_t errCode = 0;
156 RingtoneDataCallBack rdbDataCallBack;
157 NativeRdb::RdbStoreConfig config {""};
158 config.SetName(RINGTONE_LIBRARY_DB_NAME);
159 string realPath = NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(RINGTONE_LIBRARY_DB_PATH,
160 RINGTONE_LIBRARY_DB_NAME, errCode);
161 config.SetPath(move(realPath));
162 config.SetBundleName(RINGTONE_BUNDLE_NAME);
163 config.SetArea(RDB_AREA_EL2);
164 config.SetSecurityLevel(NativeRdb::SecurityLevel::S3);
165 auto rdbStore = NativeRdb::RdbHelper::GetRdbStore(config, RINGTONE_RDB_VERSION,
166 rdbDataCallBack, errCode);
167 if (rdbStore == nullptr) {
168 RINGTONE_ERR_LOG("GetRdbStore is failed , errCode=%{public}d", errCode);
169 return false;
170 }
171 //el2 rdb success
172 realPath = NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(RINGTONE_LIBRARY_DB_PATH_EL1,
173 RINGTONE_LIBRARY_DB_NAME, errCode);
174 RINGTONE_ERR_LOG("rdb Backup, realPath = %{public}s", realPath.c_str());
175 auto ret = rdbStore->Backup(realPath);
176 RINGTONE_ERR_LOG("rdb Backup, ret = %{public}d", ret);
177 return ret != E_OK ? false : true;
178 }
179
SetMoveEL2DBToEL1()180 bool RingtoneUtils::SetMoveEL2DBToEL1()
181 {
182 int32_t errCode;
183 shared_ptr<NativePreferences::Preferences> prefs =
184 NativePreferences::PreferencesHelper::GetPreferences(COMMON_XML_EL1, errCode);
185 if (!prefs) {
186 RINGTONE_ERR_LOG("get el1 preferences error: %{public}d", errCode);
187 return false;
188 }
189 prefs->PutInt(RINGTONE_PARAMETER_SCANNER_COMPLETED_KEY, RINGTONE_PARAMETER_SCANNER_COMPLETED_TRUE);
190 prefs->FlushSync();
191 return true;
192 }
193
IsNumber(const std::string & str)194 bool RingtoneUtils::IsNumber(const std::string &str)
195 {
196 if (str.empty()) {
197 RINGTONE_ERR_LOG("Input is empty");
198 return false;
199 }
200
201 if (str.size() > MAX_SIZE) {
202 RINGTONE_ERR_LOG("input is too long");
203 return false;
204 }
205
206 for (char const &c : str) {
207 if (isdigit(c) == 0) {
208 return false;
209 }
210 }
211 return true;
212 }
213
GetRoundSize(int64_t size)214 static int64_t GetRoundSize(int64_t size)
215 {
216 uint64_t val = 1;
217 int64_t multple = UNIT;
218 int64_t stdMultiple = STD_UNIT;
219 while (static_cast<int64_t>(val) * stdMultiple < size) {
220 val <<= 1;
221 if (val > THRESHOLD) {
222 val = 1;
223 multple *= UNIT;
224 stdMultiple *= STD_UNIT;
225 }
226 }
227 return static_cast<int64_t>(val) * multple;
228 }
229
GetTotalSize()230 int64_t RingtoneUtils::GetTotalSize()
231 {
232 struct statvfs diskInfo;
233 int ret = statvfs(DATA_PATH.c_str(), &diskInfo);
234 CHECK_AND_RETURN_RET_LOG(ret == 0, E_ERR, "Get total size failed, errno:%{public}d", errno);
235 int64_t totalSize = static_cast<long long>(diskInfo.f_bsize) * static_cast<long long>(diskInfo.f_blocks);
236 CHECK_AND_RETURN_RET_LOG(totalSize > 0, E_ERR, "Get total size failed, totalSize:%{public}" PRId64, totalSize);
237 totalSize = GetRoundSize(totalSize);
238 return totalSize;
239 }
240
GetFreeSize()241 int64_t RingtoneUtils::GetFreeSize()
242 {
243 struct statvfs diskInfo;
244 int ret = statvfs(DATA_PATH.c_str(), &diskInfo);
245 CHECK_AND_RETURN_RET_LOG(ret == 0, E_ERR, "Get free size failed, errno:%{public}d", errno);
246 int64_t freeSize = static_cast<int64_t>(diskInfo.f_bsize) * static_cast<int64_t>(diskInfo.f_bfree);
247 return freeSize;
248 }
249
CheckRemainSpaceMeetCondition()250 bool RingtoneUtils::CheckRemainSpaceMeetCondition()
251 {
252 static int64_t totalSize = RingtoneUtils::GetTotalSize();
253 if (totalSize <= 0) {
254 totalSize = RingtoneUtils::GetTotalSize();
255 }
256 CHECK_AND_RETURN_RET_LOG(totalSize > 0, false, "Get total size failed, totalSize:%{public}" PRId64, totalSize);
257 int64_t freeSize = RingtoneUtils::GetFreeSize();
258 CHECK_AND_RETURN_RET_LOG(freeSize > 0, false, "Get free size failed, freeSize:%{public}" PRId64, freeSize);
259 int32_t freeSizePercent = 0;
260 if (totalSize != 0) {
261 freeSizePercent = static_cast<int32_t>(freeSize * PERCENTAGE / totalSize);
262 }
263 CHECK_AND_RETURN_RET_LOG(freeSizePercent > VIDEO_FREE_SIZE_LIMIT_10, false,
264 "Check free size failed, totalSize:%{public}" PRId64 ", freeSize:%{public}" PRId64, totalSize, freeSize);
265 return true;
266 }
267 } // namespace Media
268 } // namespace OHOS
269