• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include "parameter.h"
21 #include "directory_ex.h"
22 #include "dfx_const.h"
23 #include "rdb_sql_utils.h"
24 #include "ringtone_errno.h"
25 #include "ringtone_log.h"
26 #include "ringtone_type.h"
27 #include "ringtone_db_const.h"
28 #include "ringtone_rdb_callbacks.h"
29 #include "preferences_helper.h"
30 #include "rdb_store_config.h"
31 
32 namespace OHOS {
33 namespace Media {
34 using namespace std;
35 const char RINGTONE_PARAMETER_SCANNER_COMPLETED_KEY[] = "ringtone.scanner.completed";
36 const int RINGTONE_PARAMETER_SCANNER_COMPLETED_TRUE = 1;
37 const int RINGTONE_PARAMETER_SCANNER_COMPLETED_FALSE = 0;
38 static const int32_t SYSPARA_SIZE = 128;
39 static const int32_t NO_NEED_SCANNER = 1;
40 static const int32_t RDB_AREA_EL2 = 2;
41 constexpr int32_t MAX_SIZE = 9;
42 
ReplaceAll(std::string str,const std::string & oldValue,const std::string & newValue)43 std::string RingtoneUtils::ReplaceAll(std::string str, const std::string &oldValue, const std::string &newValue)
44 {
45     for (std::string::size_type pos(0); pos != std::string::npos; pos += newValue.length()) {
46         if ((pos = str.find(oldValue, pos)) != std::string::npos) {
47             str.replace(pos, oldValue.length(), newValue);
48         } else {
49             break;
50         }
51     }
52     return str;
53 }
54 
GetDefaultSystemtoneInfo()55 std::map<int, std::string> RingtoneUtils::GetDefaultSystemtoneInfo()
56 {
57     map<int, string> defaultSystemtoneInfo;
58     char paramValue[SYSPARA_SIZE] = {0};
59     GetParameter(PARAM_RINGTONE_SETTING_RINGTONE, "", paramValue, SYSPARA_SIZE);
60     if (strlen(paramValue) > 0) {
61         defaultSystemtoneInfo.insert(make_pair(DEFAULT_RING_TYPE_SIM_CARD_1, string(paramValue)));
62     }
63 
64     if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) {
65         GetParameter(PARAM_RINGTONE_SETTING_RINGTONE2, "", paramValue, SYSPARA_SIZE);
66         if (strlen(paramValue) > 0) {
67             defaultSystemtoneInfo.insert(make_pair(DEFAULT_RING_TYPE_SIM_CARD_2, string(paramValue)));
68         }
69     }
70 
71     if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) {
72         GetParameter(PARAM_RINGTONE_SETTING_SHOT, "", paramValue, SYSPARA_SIZE);
73         if (strlen(paramValue) > 0) {
74             defaultSystemtoneInfo.insert(make_pair(DEFAULT_SHOT_TYPE_SIM_CARD_1, string(paramValue)));
75         }
76     }
77 
78     if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) {
79         GetParameter(PARAM_RINGTONE_SETTING_SHOT2, "", paramValue, SYSPARA_SIZE);
80         if (strlen(paramValue) > 0) {
81             defaultSystemtoneInfo.insert(make_pair(DEFAULT_SHOT_TYPE_SIM_CARD_2, string(paramValue)));
82         }
83     }
84 
85     if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) {
86         GetParameter(PARAM_RINGTONE_SETTING_NOTIFICATIONTONE, "", paramValue, SYSPARA_SIZE);
87         if (strlen(paramValue) > 0) {
88             defaultSystemtoneInfo.insert(make_pair(DEFAULT_NOTIFICATION_TYPE, string(paramValue)));
89         }
90     }
91 
92     if (memset_s(paramValue, sizeof(paramValue), 0, sizeof(paramValue)) == 0) {
93         GetParameter(PARAM_RINGTONE_SETTING_ALARM, "", paramValue, SYSPARA_SIZE);
94         if (strlen(paramValue) > 0) {
95             defaultSystemtoneInfo.insert(make_pair(DEFAULT_ALARM_TYPE, string(paramValue)));
96         }
97     }
98 
99     return defaultSystemtoneInfo;
100 }
101 
ChecMoveDb()102 int32_t RingtoneUtils::ChecMoveDb()
103 {
104     auto ret = RingtoneUtils::CheckNeedScanner(COMMON_XML_EL1);
105     if (ret == NO_NEED_SCANNER) {
106         RINGTONE_INFO_LOG("no need to scanner el1");
107         return NO_NEED_SCANNER;
108     } else if (ret == E_ERR) {
109         RINGTONE_INFO_LOG("open el1 xml error");
110         return E_ERR;
111     }
112 
113     ret = RingtoneUtils::CheckNeedScanner(DFX_COMMON_XML);
114     if (ret == E_ERR) {
115         RINGTONE_INFO_LOG("open el2 xml error");
116         return E_ERR;
117     } else if (ret == E_OK) {
118         RINGTONE_INFO_LOG("need to scanner el2");
119         return E_OK;
120     }
121     if (!RingtoneUtils::MoveEL2DBToEL1DB()) {
122         RINGTONE_INFO_LOG("move error");
123         return E_ERR;
124     }
125     RingtoneUtils::SetMoveEL2DBToEL1();
126     return E_OK;
127 }
128 
CheckNeedScanner(const std::string & xmlFilePath)129 int32_t RingtoneUtils::CheckNeedScanner(const std::string &xmlFilePath)
130 {
131     int32_t errCode;
132     shared_ptr<NativePreferences::Preferences> prefs =
133         NativePreferences::PreferencesHelper::GetPreferences(xmlFilePath, errCode);
134     if (!prefs) {
135         RINGTONE_ERR_LOG("get el1 preferences error: %{public}d", errCode);
136         return E_ERR;
137     }
138     int isCompleted = prefs->GetInt(RINGTONE_PARAMETER_SCANNER_COMPLETED_KEY,
139         RINGTONE_PARAMETER_SCANNER_COMPLETED_FALSE);
140     if (!isCompleted) {
141         return E_OK;
142     }
143     return NO_NEED_SCANNER;
144 }
145 
MoveEL2DBToEL1DB()146 bool RingtoneUtils::MoveEL2DBToEL1DB()
147 {
148     int32_t errCode = 0;
149     RingtoneDataCallBack rdbDataCallBack;
150     NativeRdb::RdbStoreConfig config {""};
151     config.SetName(RINGTONE_LIBRARY_DB_NAME);
152     string realPath = NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(RINGTONE_LIBRARY_DB_PATH,
153         RINGTONE_LIBRARY_DB_NAME, errCode);
154     config.SetPath(move(realPath));
155     config.SetBundleName(RINGTONE_BUNDLE_NAME);
156     config.SetArea(RDB_AREA_EL2);
157     config.SetSecurityLevel(NativeRdb::SecurityLevel::S3);
158     auto rdbStore = NativeRdb::RdbHelper::GetRdbStore(config, RINGTONE_RDB_VERSION,
159         rdbDataCallBack, errCode);
160     if (rdbStore == nullptr) {
161         RINGTONE_ERR_LOG("GetRdbStore is failed , errCode=%{public}d", errCode);
162         return false;
163     }
164     //el2 rdb success
165     realPath = NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(RINGTONE_LIBRARY_DB_PATH_EL1,
166         RINGTONE_LIBRARY_DB_NAME, errCode);
167     RINGTONE_ERR_LOG("rdb Backup, realPath = %{public}s", realPath.c_str());
168     auto ret = rdbStore->Backup(realPath);
169     RINGTONE_ERR_LOG("rdb Backup, ret = %{public}d", ret);
170     return ret != E_OK ? false : true;
171 }
172 
SetMoveEL2DBToEL1()173 bool RingtoneUtils::SetMoveEL2DBToEL1()
174 {
175     int32_t errCode;
176     shared_ptr<NativePreferences::Preferences> prefs =
177         NativePreferences::PreferencesHelper::GetPreferences(COMMON_XML_EL1, errCode);
178     if (!prefs) {
179         RINGTONE_ERR_LOG("get el1 preferences error: %{public}d", errCode);
180         return false;
181     }
182     prefs->PutInt(RINGTONE_PARAMETER_SCANNER_COMPLETED_KEY, RINGTONE_PARAMETER_SCANNER_COMPLETED_TRUE);
183     prefs->FlushSync();
184     return true;
185 }
186 
IsNumber(const std::string & str)187 bool RingtoneUtils::IsNumber(const std::string &str)
188 {
189     if (str.empty()) {
190         RINGTONE_ERR_LOG("Input is empty");
191         return false;
192     }
193 
194     if (str.size() > MAX_SIZE) {
195         RINGTONE_ERR_LOG("input is too long");
196         return false;
197     }
198 
199     for (char const &c : str) {
200         if (isdigit(c) == 0) {
201             return false;
202         }
203     }
204     return true;
205 }
206 } // namespace Media
207 } // namespace OHOS
208