1 /*
2 * Copyright (c) 2025 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 "system_sound_manager_utils.h"
17
18 #include <fstream>
19
20 #include "ringtone_proxy_uri.h"
21 #include "system_sound_log.h"
22 #include "media_errors.h"
23 #include "os_account_manager.h"
24 #include "system_sound_manager.h"
25 #include "system_tone_player_impl.h"
26 #include "parameter.h"
27 #include "hitrace_meter.h"
28
29 using namespace std;
30 using namespace nlohmann;
31 using namespace OHOS::AbilityRuntime;
32 using namespace OHOS::DataShare;
33
34 namespace {
35 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO_NAPI, "SystemSoundManagerUtils"};
36 }
37
38 namespace OHOS {
39 namespace Media {
40
41 constexpr int32_t RETRY_TIME_S = 5;
42 constexpr int64_t SLEEP_TIME_S = 1;
43 const std::string SANDBOX_PREFIX = "/data/storage/el2/base/files/";
44 const char RINGTONE_PARAMETER_SCANNER_USERID_KEY[] = "ringtone.scanner.userId";
45 const char RINGTONE_PARAMETER_SCANNER_FIRST_FALSE[] = "false";
46 const char RINGTONE_PARAMETER_SCANNER_FIRST_TRUE[] = "true";
47 const int32_t RINGTONEPARA_SIZE = 64;
48
GetCurrentUserId()49 int32_t SystemSoundManagerUtils::GetCurrentUserId()
50 {
51 std::vector<int32_t> ids;
52 int32_t currentuserId = -1;
53 ErrCode result;
54 int32_t retry = RETRY_TIME_S;
55 while (retry--) {
56 result = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
57 if (result == ERR_OK && !ids.empty()) {
58 currentuserId = ids[0];
59 MEDIA_LOGD("current userId is :%{public}d", currentuserId);
60 break;
61 }
62
63 // sleep and wait for 1 millisecond
64 sleep(SLEEP_TIME_S);
65 }
66 if (result != ERR_OK || ids.empty()) {
67 MEDIA_LOGW("current userId is empty");
68 }
69 return currentuserId;
70 }
71
CreateDataShareHelperUri(int32_t systemAbilityId)72 shared_ptr<DataShare::DataShareHelper> SystemSoundManagerUtils::CreateDataShareHelperUri(int32_t systemAbilityId)
73 {
74 MEDIA_LOGI("Enter CreateDataShareHelperUri()");
75 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76 if (saManager == nullptr) {
77 MEDIA_LOGE("Get system ability manager failed.");
78 return nullptr;
79 }
80 auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
81 if (remoteObj == nullptr) {
82 MEDIA_LOGE("Get system ability:[%{public}d] failed.", systemAbilityId);
83 return nullptr;
84 }
85
86 int32_t useId = SystemSoundManagerUtils::GetCurrentUserId();
87 std::string uri = RINGTONE_LIBRARY_PROXY_URI + "?" + "user=" + std::to_string(useId);
88 std::pair<int, std::shared_ptr<DataShare::DataShareHelper>> dataShare =
89 DataShare::DataShareHelper::Create(remoteObj, uri, "");
90 MEDIA_LOGI("CreateDataShareHelperUri with : %{public}s. errcode : %{public}d", uri.c_str(), dataShare.first);
91 return dataShare.second;
92 }
93
CreateDataShareHelper(int32_t systemAbilityId)94 shared_ptr<DataShare::DataShareHelper> SystemSoundManagerUtils::CreateDataShareHelper(int32_t systemAbilityId)
95 {
96 MEDIA_LOGI("Enter CreateDataShareHelper()");
97 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
98 if (saManager == nullptr) {
99 MEDIA_LOGE("Get system ability manager failed.");
100 return nullptr;
101 }
102 auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
103 if (remoteObj == nullptr) {
104 MEDIA_LOGE("Get system ability:[%{public}d] failed.", systemAbilityId);
105 return nullptr;
106 }
107 return DataShare::DataShareHelper::Creator(remoteObj, RINGTONE_URI);
108 }
109
VerifyCustomPath(const std::string & audioUri)110 bool SystemSoundManagerUtils::VerifyCustomPath(const std::string &audioUri)
111 {
112 bool flag = false;
113 if (audioUri.substr(0, SANDBOX_PREFIX.size()) == SANDBOX_PREFIX) {
114 flag = true;
115 }
116 return flag;
117 }
118
IdExists(const std::string & ids,int32_t id)119 bool SystemSoundManagerUtils::IdExists(const std::string &ids, int32_t id)
120 {
121 if (ids.empty()) {
122 return false;
123 }
124
125 size_t pos = 0;
126 std::string idStr = std::to_string(id);
127
128 while ((pos = ids.find(idStr, pos)) != std::string::npos) {
129 bool startPos = (pos == 0) || (ids[pos - 1] == ' ');
130 bool endPos = (pos + idStr.length() == ids.length()) || (ids[pos + idStr.length()] == ' ');
131 if (startPos && endPos) {
132 return true;
133 }
134 pos += idStr.length();
135 }
136 MEDIA_LOGI("IdExists End.");
137 return false;
138 }
139
CheckCurrentUser()140 bool SystemSoundManagerUtils::CheckCurrentUser()
141 {
142 char paramValue[RINGTONEPARA_SIZE] = {0};
143 GetParameter(RINGTONE_PARAMETER_SCANNER_USERID_KEY, "", paramValue, RINGTONEPARA_SIZE);
144 std::string ids(paramValue);
145 int32_t currentUserId = GetCurrentUserId();
146 if (IdExists(ids, currentUserId)) {
147 return true;
148 }
149 if (!ids.empty() && ids.back() != ' ') {
150 ids += " ";
151 }
152 ids += std::to_string(currentUserId);
153 int result = SetParameter(RINGTONE_PARAMETER_SCANNER_USERID_KEY, ids.c_str());
154 MEDIA_LOGI("CheckCurrentUser End. SetParameter result: %{public}d ,CurrentUserIds: %{private}s .",
155 result, ids.c_str());
156 return false;
157 }
158
GetScannerFirstParameter(const char * key,int32_t maxSize)159 bool SystemSoundManagerUtils::GetScannerFirstParameter(const char* key, int32_t maxSize)
160 {
161 char paramValue[RINGTONEPARA_SIZE] = {0};
162 maxSize = RINGTONEPARA_SIZE;
163 GetParameter(key, "", paramValue, maxSize);
164 std::string parameter(paramValue);
165 MEDIA_LOGI("GetParameter end paramValue:%{public}s .", parameter.c_str());
166 if (strcmp(paramValue, RINGTONE_PARAMETER_SCANNER_FIRST_TRUE) == 0) {
167 return true;
168 }
169 if (strcmp(paramValue, RINGTONE_PARAMETER_SCANNER_FIRST_FALSE) == 0) {
170 return false;
171 }
172 return false;
173 }
174
GetTypeForSystemSoundUri(const std::string & audioUri)175 int32_t SystemSoundManagerUtils::GetTypeForSystemSoundUri(const std::string &audioUri)
176 {
177 if (audioUri == NO_SYSTEM_SOUND || audioUri == NO_RING_SOUND) {
178 return SystemToneUriType::NO_RINGTONES;
179 }
180
181 size_t pos = audioUri.find_first_of("sys_prod");
182 if (pos == 0 || pos == 1) {
183 // The audioUri of a preset ringtone starts with "sys prod” or "/sys prod".
184 return SystemToneUriType::PRESET_RINGTONES;
185 }
186 pos = audioUri.find_first_of("data");
187 if (pos == 0 || pos == 1) {
188 // The audioUri of a custom ringtone starts with "data" or "/data".
189 return SystemToneUriType::CUSTOM_RINGTONES;
190 }
191 return UNKNOW_RINGTONES;
192 }
193
GetErrorReason(const int32_t & errorCode)194 std::string SystemSoundManagerUtils::GetErrorReason(const int32_t &errorCode)
195 {
196 std::string errorReason = "";
197 if (errorCode == MSERR_OK) {
198 errorReason = "system tone playback successfully";
199 } else {
200 errorReason = "system tone playback failed";
201 }
202 return errorReason;
203 }
204
MediaTrace(const std::string & funcName)205 MediaTrace::MediaTrace(const std::string &funcName)
206 {
207 StartTrace(HITRACE_TAG_ZMEDIA, funcName);
208 isSync_ = true;
209 }
210
TraceBegin(const std::string & funcName,int32_t taskId)211 void MediaTrace::TraceBegin(const std::string &funcName, int32_t taskId)
212 {
213 StartAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
214 }
215
TraceEnd(const std::string & funcName,int32_t taskId)216 void MediaTrace::TraceEnd(const std::string &funcName, int32_t taskId)
217 {
218 FinishAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
219 }
220
CounterTrace(const std::string & varName,int32_t val)221 void MediaTrace::CounterTrace(const std::string &varName, int32_t val)
222 {
223 CountTrace(HITRACE_TAG_ZMEDIA, varName, val);
224 }
225
~MediaTrace()226 MediaTrace::~MediaTrace()
227 {
228 if (isSync_) {
229 FinishTrace(HITRACE_TAG_ZMEDIA);
230 }
231 }
232 } // namesapce Media
233 } // namespace OHOS