13e5483f6Sopenharmony_ci/* 23e5483f6Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd. 33e5483f6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43e5483f6Sopenharmony_ci * you may not use this file except in compliance with the License. 53e5483f6Sopenharmony_ci * You may obtain a copy of the License at 63e5483f6Sopenharmony_ci * 73e5483f6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83e5483f6Sopenharmony_ci * 93e5483f6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103e5483f6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113e5483f6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123e5483f6Sopenharmony_ci * See the License for the specific language governing permissions and 133e5483f6Sopenharmony_ci * limitations under the License. 143e5483f6Sopenharmony_ci */ 153e5483f6Sopenharmony_ci 163e5483f6Sopenharmony_ci#include "ringtone_utils.h" 173e5483f6Sopenharmony_ci 183e5483f6Sopenharmony_ci#include <securec.h> 193e5483f6Sopenharmony_ci 203e5483f6Sopenharmony_ci#include "parameter.h" 213e5483f6Sopenharmony_ci#include "directory_ex.h" 223e5483f6Sopenharmony_ci#include "dfx_const.h" 233e5483f6Sopenharmony_ci#include "rdb_sql_utils.h" 243e5483f6Sopenharmony_ci#include "ringtone_errno.h" 253e5483f6Sopenharmony_ci#include "ringtone_log.h" 263e5483f6Sopenharmony_ci#include "ringtone_type.h" 273e5483f6Sopenharmony_ci#include "ringtone_db_const.h" 283e5483f6Sopenharmony_ci#include "ringtone_rdb_callbacks.h" 293e5483f6Sopenharmony_ci#include "preferences_helper.h" 303e5483f6Sopenharmony_ci#include "rdb_store_config.h" 313e5483f6Sopenharmony_ci 323e5483f6Sopenharmony_cinamespace OHOS { 333e5483f6Sopenharmony_cinamespace Media { 343e5483f6Sopenharmony_ciusing namespace std; 353e5483f6Sopenharmony_ciconst char RINGTONE_PARAMETER_SCANNER_COMPLETED_KEY[] = "ringtone.scanner.completed"; 363e5483f6Sopenharmony_ciconst int RINGTONE_PARAMETER_SCANNER_COMPLETED_TRUE = 1; 373e5483f6Sopenharmony_ciconst int RINGTONE_PARAMETER_SCANNER_COMPLETED_FALSE = 0; 383e5483f6Sopenharmony_cistatic const int32_t SYSPARA_SIZE = 128; 393e5483f6Sopenharmony_cistatic const int32_t NO_NEED_SCANNER = 1; 403e5483f6Sopenharmony_cistatic const int32_t RDB_AREA_EL2 = 2; 413e5483f6Sopenharmony_ci 423e5483f6Sopenharmony_cistd::string RingtoneUtils::ReplaceAll(std::string str, const std::string &oldValue, const std::string &newValue) 433e5483f6Sopenharmony_ci{ 443e5483f6Sopenharmony_ci for (std::string::size_type pos(0); pos != std::string::npos; pos += newValue.length()) { 453e5483f6Sopenharmony_ci if ((pos = str.find(oldValue, pos)) != std::string::npos) { 463e5483f6Sopenharmony_ci str.replace(pos, oldValue.length(), newValue); 473e5483f6Sopenharmony_ci } else { 483e5483f6Sopenharmony_ci break; 493e5483f6Sopenharmony_ci } 503e5483f6Sopenharmony_ci } 513e5483f6Sopenharmony_ci return str; 523e5483f6Sopenharmony_ci} 533e5483f6Sopenharmony_ci 543e5483f6Sopenharmony_cistd::map<int, std::string> RingtoneUtils::GetDefaultSystemtoneInfo() 553e5483f6Sopenharmony_ci{ 563e5483f6Sopenharmony_ci map<int, string> defaultSystemtoneInfo; 573e5483f6Sopenharmony_ci char paramValue[SYSPARA_SIZE] = {0}; 583e5483f6Sopenharmony_ci GetParameter(PARAM_RINGTONE_SETTING_RINGTONE, "", paramValue, SYSPARA_SIZE); 593e5483f6Sopenharmony_ci if (strlen(paramValue) > 0) { 603e5483f6Sopenharmony_ci defaultSystemtoneInfo.insert(make_pair(DEFAULT_RING_TYPE_SIM_CARD_1, string(paramValue))); 613e5483f6Sopenharmony_ci } 623e5483f6Sopenharmony_ci 633e5483f6Sopenharmony_ci if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) { 643e5483f6Sopenharmony_ci GetParameter(PARAM_RINGTONE_SETTING_RINGTONE2, "", paramValue, SYSPARA_SIZE); 653e5483f6Sopenharmony_ci if (strlen(paramValue) > 0) { 663e5483f6Sopenharmony_ci defaultSystemtoneInfo.insert(make_pair(DEFAULT_RING_TYPE_SIM_CARD_2, string(paramValue))); 673e5483f6Sopenharmony_ci } 683e5483f6Sopenharmony_ci } 693e5483f6Sopenharmony_ci 703e5483f6Sopenharmony_ci if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) { 713e5483f6Sopenharmony_ci GetParameter(PARAM_RINGTONE_SETTING_SHOT, "", paramValue, SYSPARA_SIZE); 723e5483f6Sopenharmony_ci if (strlen(paramValue) > 0) { 733e5483f6Sopenharmony_ci defaultSystemtoneInfo.insert(make_pair(DEFAULT_SHOT_TYPE_SIM_CARD_1, string(paramValue))); 743e5483f6Sopenharmony_ci } 753e5483f6Sopenharmony_ci } 763e5483f6Sopenharmony_ci 773e5483f6Sopenharmony_ci if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) { 783e5483f6Sopenharmony_ci GetParameter(PARAM_RINGTONE_SETTING_SHOT2, "", paramValue, SYSPARA_SIZE); 793e5483f6Sopenharmony_ci if (strlen(paramValue) > 0) { 803e5483f6Sopenharmony_ci defaultSystemtoneInfo.insert(make_pair(DEFAULT_SHOT_TYPE_SIM_CARD_2, string(paramValue))); 813e5483f6Sopenharmony_ci } 823e5483f6Sopenharmony_ci } 833e5483f6Sopenharmony_ci 843e5483f6Sopenharmony_ci if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) { 853e5483f6Sopenharmony_ci GetParameter(PARAM_RINGTONE_SETTING_NOTIFICATIONTONE, "", paramValue, SYSPARA_SIZE); 863e5483f6Sopenharmony_ci if (strlen(paramValue) > 0) { 873e5483f6Sopenharmony_ci defaultSystemtoneInfo.insert(make_pair(DEFAULT_NOTIFICATION_TYPE, string(paramValue))); 883e5483f6Sopenharmony_ci } 893e5483f6Sopenharmony_ci } 903e5483f6Sopenharmony_ci 913e5483f6Sopenharmony_ci if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) { 923e5483f6Sopenharmony_ci GetParameter(PARAM_RINGTONE_SETTING_ALARM, "", paramValue, SYSPARA_SIZE); 933e5483f6Sopenharmony_ci if (strlen(paramValue) > 0) { 943e5483f6Sopenharmony_ci defaultSystemtoneInfo.insert(make_pair(DEFAULT_ALARM_TYPE, string(paramValue))); 953e5483f6Sopenharmony_ci } 963e5483f6Sopenharmony_ci } 973e5483f6Sopenharmony_ci 983e5483f6Sopenharmony_ci return defaultSystemtoneInfo; 993e5483f6Sopenharmony_ci} 1003e5483f6Sopenharmony_ci 1013e5483f6Sopenharmony_ciint32_t RingtoneUtils::ChecMoveDb() 1023e5483f6Sopenharmony_ci{ 1033e5483f6Sopenharmony_ci auto ret = RingtoneUtils::CheckNeedScanner(COMMON_XML_EL1); 1043e5483f6Sopenharmony_ci if (ret == NO_NEED_SCANNER) { 1053e5483f6Sopenharmony_ci RINGTONE_INFO_LOG("no need to scanner el1"); 1063e5483f6Sopenharmony_ci return NO_NEED_SCANNER; 1073e5483f6Sopenharmony_ci } else if (ret == E_ERR) { 1083e5483f6Sopenharmony_ci RINGTONE_INFO_LOG("open el1 xml error"); 1093e5483f6Sopenharmony_ci return E_ERR; 1103e5483f6Sopenharmony_ci } 1113e5483f6Sopenharmony_ci 1123e5483f6Sopenharmony_ci ret = RingtoneUtils::CheckNeedScanner(DFX_COMMON_XML); 1133e5483f6Sopenharmony_ci if (ret == E_ERR) { 1143e5483f6Sopenharmony_ci RINGTONE_INFO_LOG("open el2 xml error"); 1153e5483f6Sopenharmony_ci return E_ERR; 1163e5483f6Sopenharmony_ci } else if (ret == E_OK) { 1173e5483f6Sopenharmony_ci RINGTONE_INFO_LOG("need to scanner el2"); 1183e5483f6Sopenharmony_ci return E_OK; 1193e5483f6Sopenharmony_ci } 1203e5483f6Sopenharmony_ci if (!RingtoneUtils::MoveEL2DBToEL1DB()) { 1213e5483f6Sopenharmony_ci RINGTONE_INFO_LOG("move error"); 1223e5483f6Sopenharmony_ci return E_ERR; 1233e5483f6Sopenharmony_ci } 1243e5483f6Sopenharmony_ci RingtoneUtils::SetMoveEL2DBToEL1(); 1253e5483f6Sopenharmony_ci return E_OK; 1263e5483f6Sopenharmony_ci} 1273e5483f6Sopenharmony_ci 1283e5483f6Sopenharmony_ciint32_t RingtoneUtils::CheckNeedScanner(const std::string &xmlFilePath) 1293e5483f6Sopenharmony_ci{ 1303e5483f6Sopenharmony_ci int32_t errCode; 1313e5483f6Sopenharmony_ci shared_ptr<NativePreferences::Preferences> prefs = 1323e5483f6Sopenharmony_ci NativePreferences::PreferencesHelper::GetPreferences(xmlFilePath, errCode); 1333e5483f6Sopenharmony_ci if (!prefs) { 1343e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("get el1 preferences error: %{public}d", errCode); 1353e5483f6Sopenharmony_ci return E_ERR; 1363e5483f6Sopenharmony_ci } 1373e5483f6Sopenharmony_ci int isCompleted = prefs->GetInt(RINGTONE_PARAMETER_SCANNER_COMPLETED_KEY, 1383e5483f6Sopenharmony_ci RINGTONE_PARAMETER_SCANNER_COMPLETED_FALSE); 1393e5483f6Sopenharmony_ci if (!isCompleted) { 1403e5483f6Sopenharmony_ci return E_OK; 1413e5483f6Sopenharmony_ci } 1423e5483f6Sopenharmony_ci return NO_NEED_SCANNER; 1433e5483f6Sopenharmony_ci} 1443e5483f6Sopenharmony_ci 1453e5483f6Sopenharmony_cibool RingtoneUtils::MoveEL2DBToEL1DB() 1463e5483f6Sopenharmony_ci{ 1473e5483f6Sopenharmony_ci int32_t errCode = 0; 1483e5483f6Sopenharmony_ci RingtoneDataCallBack rdbDataCallBack; 1493e5483f6Sopenharmony_ci NativeRdb::RdbStoreConfig config {""}; 1503e5483f6Sopenharmony_ci config.SetName(RINGTONE_LIBRARY_DB_NAME); 1513e5483f6Sopenharmony_ci string realPath = NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(RINGTONE_LIBRARY_DB_PATH, 1523e5483f6Sopenharmony_ci RINGTONE_LIBRARY_DB_NAME, errCode); 1533e5483f6Sopenharmony_ci config.SetPath(move(realPath)); 1543e5483f6Sopenharmony_ci config.SetBundleName(RINGTONE_BUNDLE_NAME); 1553e5483f6Sopenharmony_ci config.SetArea(RDB_AREA_EL2); 1563e5483f6Sopenharmony_ci config.SetSecurityLevel(NativeRdb::SecurityLevel::S3); 1573e5483f6Sopenharmony_ci auto rdbStore = NativeRdb::RdbHelper::GetRdbStore(config, RINGTONE_RDB_VERSION, 1583e5483f6Sopenharmony_ci rdbDataCallBack, errCode); 1593e5483f6Sopenharmony_ci if (rdbStore == nullptr) { 1603e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("GetRdbStore is failed , errCode=%{public}d", errCode); 1613e5483f6Sopenharmony_ci return false; 1623e5483f6Sopenharmony_ci } 1633e5483f6Sopenharmony_ci //el2 rdb success 1643e5483f6Sopenharmony_ci realPath = NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(RINGTONE_LIBRARY_DB_PATH_EL1, 1653e5483f6Sopenharmony_ci RINGTONE_LIBRARY_DB_NAME, errCode); 1663e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("rdb Backup, realPath = %{public}s", realPath.c_str()); 1673e5483f6Sopenharmony_ci auto ret = rdbStore->Backup(realPath); 1683e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("rdb Backup, ret = %{public}d", ret); 1693e5483f6Sopenharmony_ci return true; 1703e5483f6Sopenharmony_ci} 1713e5483f6Sopenharmony_ci 1723e5483f6Sopenharmony_cibool RingtoneUtils::SetMoveEL2DBToEL1() 1733e5483f6Sopenharmony_ci{ 1743e5483f6Sopenharmony_ci int32_t errCode; 1753e5483f6Sopenharmony_ci shared_ptr<NativePreferences::Preferences> prefs = 1763e5483f6Sopenharmony_ci NativePreferences::PreferencesHelper::GetPreferences(COMMON_XML_EL1, errCode); 1773e5483f6Sopenharmony_ci if (!prefs) { 1783e5483f6Sopenharmony_ci RINGTONE_ERR_LOG("get el1 preferences error: %{public}d", errCode); 1793e5483f6Sopenharmony_ci return false; 1803e5483f6Sopenharmony_ci } 1813e5483f6Sopenharmony_ci prefs->PutInt(RINGTONE_PARAMETER_SCANNER_COMPLETED_KEY, RINGTONE_PARAMETER_SCANNER_COMPLETED_TRUE); 1823e5483f6Sopenharmony_ci prefs->FlushSync(); 1833e5483f6Sopenharmony_ci return true; 1843e5483f6Sopenharmony_ci} 1853e5483f6Sopenharmony_ci} // namespace Media 1863e5483f6Sopenharmony_ci} // namespace OHOS 187