1 /*
2 * Copyright (C) 2022 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 "utils.h"
17
18 #ifdef OHOS_BUILD_ENABLE_HISYSEVENT
19 #include <hisysevent.h>
20 #endif //OHOS_BUILD_ENABLE_HISYSEVENT
21
22 #include "bundle_mgr_client.h"
23 #include "hilog_wrapper.h"
24 #include "nlohmann/json.hpp"
25 #include "ipc_skeleton.h"
26 #include "parameters.h"
27 #include "bundle_info.h"
28 #include "bundlemgr/bundle_mgr_interface.h"
29 #include "accessibility_resource_bundle_manager.h"
30 #include "app_mgr_client.h"
31 #include "configuration.h"
32
33 namespace OHOS {
34 namespace Accessibility {
35 namespace {
36 const std::string KEY_ACCESSIBILITY_ABILITY_TYPES = "accessibilityAbilityTypes";
37 const std::string KEY_ACCESSIBILITY_CAPABILITIES = "accessibilityCapabilities";
38 const std::string KEY_SETTINGS_ABILITY = "settingsAbility";
39 const std::string KEY_ACCESSIBILITY_CAPABILITIES_RATIONALE = "accessibilityCapabilityRationale";
40 const std::string KEY_IS_IMPORTANT = "isImportant";
41 const std::string KEY_NEED_HIDE = "needHide";
42 const std::string KEY_ACCESSIBILITY_EVENT_CONFIGURE = "accessibilityEventConfigure";
43
44 // The json value of accessibilityAbility type
45 const std::string ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_SPOKEN = "spoken";
46 const std::string ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_HAPIC = "haptic";
47 const std::string ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_AUDIBLE = "audible";
48 const std::string ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_VISUAL = "visual";
49 const std::string ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_GENERIC = "generic";
50 const std::string ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_ALL = "all";
51
52 // The json value of capabilities
53 const std::string CAPABILITIES_JSON_VALUE_RETRIEVE = "retrieve";
54 const std::string CAPABILITIES_JSON_VALUE_TOUCH_GUIDE = "touchGuide";
55 const std::string CAPABILITIES_JSON_VALUE_KEY_EVENT_OBSERVER = "keyEventObserver";
56 const std::string CAPABILITIES_JSON_VALUE_ZOOM = "zoom";
57 const std::string CAPABILITIES_JSON_VALUE_GESTURE = "gesture";
58
59 const std::string FOLD_SCREEN_TYPE = system::GetParameter("const.window.foldscreen.type", "0,0,0,0");
60 const bool IS_WIDE_FOLD = (FOLD_SCREEN_TYPE == "4,2,0,0");
61 const bool IS_BIG_FOLD = (FOLD_SCREEN_TYPE == "1,2,0,0") || (FOLD_SCREEN_TYPE == "6,1,0,0");
62 const int32_t STRING_LEN_MAX = 10240;
63 constexpr int32_t BASE_USER_RANGE = 200000;
64 constexpr int32_t INVALID_ID = -1;
65 constexpr int32_t INVALID_USER_ID = -1;
66 } // namespace
67
68 class JsonUtils {
69 public:
GetStringFromJson(const nlohmann::json & json,const std::string & key,std::string & value)70 static bool GetStringFromJson(const nlohmann::json &json, const std::string &key, std::string &value)
71 {
72 HILOG_DEBUG("start.");
73 if (!json.is_object()) {
74 HILOG_ERROR("json is not object.");
75 return false;
76 }
77 if (json.find(key) != json.end() && json.at(key).is_string()) {
78 HILOG_DEBUG("Find key[%{public}s] successful.", key.c_str());
79 value = json.at(key).get<std::string>();
80 }
81 return true;
82 }
83
GetStringVecFromJson(const nlohmann::json & json,const std::string & key,std::vector<std::string> & value)84 static bool GetStringVecFromJson(const nlohmann::json &json, const std::string &key,
85 std::vector<std::string> &value)
86 {
87 HILOG_DEBUG("start.");
88 if (!json.is_object()) {
89 HILOG_ERROR("json is not object.");
90 return false;
91 }
92 if (json.find(key) != json.end() && json.at(key).is_array()) {
93 HILOG_DEBUG("Find key[%{public}s] successful.", key.c_str());
94 value = json.at(key).get<std::vector<std::string>>();
95 }
96 return true;
97 }
98
GetBoolFromJson(const nlohmann::json & json,const std::string & key,bool & value)99 static bool GetBoolFromJson(const nlohmann::json &json, const std::string &key, bool &value)
100 {
101 HILOG_DEBUG("start.");
102 if (!json.is_object()) {
103 HILOG_ERROR("json is not object.");
104 return false;
105 }
106 if (json.find(key) != json.end() && json.at(key).is_boolean()) {
107 HILOG_DEBUG("Find key[%{public}s] successful.", key.c_str());
108 value = json.at(key).get<bool>();
109 }
110 return true;
111 }
112
GetUInt32VecFromJson(const nlohmann::json & json,const std::string & key,std::vector<uint32_t> & value)113 static bool GetUInt32VecFromJson(const nlohmann::json &json, const std::string &key,
114 std::vector<uint32_t> &value)
115 {
116 HILOG_DEBUG("start.");
117 if (!json.is_object()) {
118 HILOG_ERROR("json is not object.");
119 return false;
120 }
121 if (json.find(key) != json.end() && json.at(key).is_array()) {
122 HILOG_DEBUG("Find key[%{public}s] successful.", key.c_str());
123 value = json.at(key).get<std::vector<uint32_t>>();
124 }
125 return true;
126 }
127 };
128
129 class PraseVecUtils {
130 public:
ParseAbilityTypesFromVec(const std::vector<std::string> & abilities)131 static uint32_t ParseAbilityTypesFromVec(const std::vector<std::string> &abilities)
132 {
133 HILOG_DEBUG("start.");
134 uint32_t abilityTypes = 0;
135
136 for (const auto &ability : abilities) {
137 if (ability == ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_SPOKEN) {
138 abilityTypes |= AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_SPOKEN;
139 }
140
141 if (ability == ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_HAPIC) {
142 abilityTypes |= AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_HAPTIC;
143 }
144
145 if (ability == ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_AUDIBLE) {
146 abilityTypes |= AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_AUDIBLE;
147 }
148
149 if (ability == ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_VISUAL) {
150 abilityTypes |= AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_VISUAL;
151 }
152
153 if (ability == ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_GENERIC) {
154 abilityTypes |= AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_GENERIC;
155 }
156
157 if (ability == ACCESSIBILITY_ABILITY_TYPES_JSON_VALUE_ALL) {
158 abilityTypes |= AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_ALL;
159 }
160 }
161 return abilityTypes;
162 }
163
ParseCapabilitiesFromVec(const std::vector<std::string> & capabilities)164 static uint32_t ParseCapabilitiesFromVec(const std::vector<std::string> &capabilities)
165 {
166 HILOG_DEBUG("start.");
167 uint32_t capabilitiesValue = 0;
168
169 for (const auto &capability : capabilities) {
170 if (capability == CAPABILITIES_JSON_VALUE_RETRIEVE) {
171 capabilitiesValue |= Capability::CAPABILITY_RETRIEVE;
172 }
173
174 if (capability == CAPABILITIES_JSON_VALUE_TOUCH_GUIDE) {
175 capabilitiesValue |= Capability::CAPABILITY_TOUCH_GUIDE;
176 }
177
178 if (capability == CAPABILITIES_JSON_VALUE_KEY_EVENT_OBSERVER) {
179 capabilitiesValue |= Capability::CAPABILITY_KEY_EVENT_OBSERVER;
180 }
181
182 if (capability == CAPABILITIES_JSON_VALUE_ZOOM) {
183 capabilitiesValue |= Capability::CAPABILITY_ZOOM;
184 }
185
186 if (capability == CAPABILITIES_JSON_VALUE_GESTURE) {
187 capabilitiesValue |= Capability::CAPABILITY_GESTURE;
188 }
189 }
190 return capabilitiesValue;
191 }
192 };
193
Parse(const AppExecFwk::ExtensionAbilityInfo & abilityInfo,AccessibilityAbilityInitParams & initParams)194 void Utils::Parse(const AppExecFwk::ExtensionAbilityInfo &abilityInfo, AccessibilityAbilityInitParams &initParams)
195 {
196 HILOG_DEBUG("start.");
197 initParams.name = abilityInfo.name;
198 initParams.bundleName = abilityInfo.bundleName;
199 initParams.moduleName = abilityInfo.moduleName;
200 initParams.description = abilityInfo.description;
201 initParams.label = abilityInfo.label;
202
203 std::vector<std::string> profileInfos;
204 std::string metadataName = "ohos.accessibleability";
205 AppExecFwk::BundleMgrClient bundleMgrClient;
206 bundleMgrClient.GetResConfigFile(abilityInfo, metadataName, profileInfos);
207 if (profileInfos.empty()) {
208 HILOG_ERROR("profileInfos is empty.");
209 return;
210 }
211
212 if (!nlohmann::json::accept(profileInfos[0])) {
213 HILOG_ERROR("profileInfos is not json format.");
214 return;
215 }
216 nlohmann::json sourceJson = nlohmann::json::parse(profileInfos[0]);
217
218 // accessibilityCapabilities
219 std::vector<std::string> capabilities;
220 if (!JsonUtils::GetStringVecFromJson(sourceJson, KEY_ACCESSIBILITY_CAPABILITIES, capabilities)) {
221 HILOG_ERROR("Get accessibilityCapabilities from json failed.");
222 return;
223 }
224 initParams.staticCapabilities = PraseVecUtils::ParseCapabilitiesFromVec(capabilities);
225
226 // accessibilityAbilityTypes
227 std::vector<std::string> abilityTypes;
228 if (!JsonUtils::GetStringVecFromJson(sourceJson, KEY_ACCESSIBILITY_ABILITY_TYPES, abilityTypes)) {
229 HILOG_ERROR("Get accessibilityAbilityTypes from json failed.");
230 return;
231 }
232 initParams.abilityTypes = PraseVecUtils::ParseAbilityTypesFromVec(abilityTypes);
233
234 // accessibilityCapabilityRationale
235 if (!JsonUtils::GetStringFromJson(sourceJson, KEY_ACCESSIBILITY_CAPABILITIES_RATIONALE, initParams.rationale)) {
236 HILOG_ERROR("Get accessibilityCapabilityRationale from json failed.");
237 return;
238 }
239
240 // settingsAbility
241 if (!JsonUtils::GetStringFromJson(sourceJson, KEY_SETTINGS_ABILITY, initParams.settingsAbility)) {
242 HILOG_ERROR("Get settingsAbility from json failed.");
243 return;
244 }
245
246 // isImportant
247 if (!JsonUtils::GetBoolFromJson(sourceJson, KEY_IS_IMPORTANT, initParams.isImportant)) {
248 HILOG_ERROR("Get isImportant from json failed.");
249 return;
250 }
251
252 // needHide
253 if (!JsonUtils::GetBoolFromJson(sourceJson, KEY_NEED_HIDE, initParams.needHide)) {
254 HILOG_ERROR("Get needHide from json failed.");
255 return;
256 }
257
258 //accessibilityEventConfigure
259 if (!JsonUtils::GetUInt32VecFromJson(sourceJson, KEY_ACCESSIBILITY_EVENT_CONFIGURE, initParams.eventConfigure)) {
260 HILOG_ERROR("Get accessibilityCapabilities from json failed.");
261 return;
262 }
263 }
264
GetSystemTime()265 int64_t Utils::GetSystemTime()
266 {
267 HILOG_DEBUG("start.");
268
269 struct timespec times = {0, 0};
270 clock_gettime(CLOCK_MONOTONIC, ×);
271 int64_t millisecond = static_cast<int64_t>(times.tv_sec * 1000 + times.tv_nsec / 1000000);
272
273 return millisecond;
274 }
275
GetUri(const OHOS::AppExecFwk::ElementName & elementName)276 std::string Utils::GetUri(const OHOS::AppExecFwk::ElementName &elementName)
277 {
278 HILOG_DEBUG("bundle name(%{public}s) ability name(%{public}s)",
279 elementName.GetBundleName().c_str(), elementName.GetAbilityName().c_str());
280 return elementName.GetBundleName() + "/" + elementName.GetAbilityName();
281 }
282
GetUri(const std::string & bundleName,const std::string & abilityName)283 std::string Utils::GetUri(const std::string &bundleName, const std::string &abilityName)
284 {
285 HILOG_DEBUG("bundle name(%{public}s) ability name(%{public}s)", bundleName.c_str(), abilityName.c_str());
286 return bundleName + "/" + abilityName;
287 }
288
GetBundleNameFromUri(const std::string & uri)289 std::string Utils::GetBundleNameFromUri(const std::string &uri)
290 {
291 if (uri.empty()) {
292 return "";
293 }
294 size_t pos = uri.find('/');
295 if (pos != std::string::npos) {
296 return uri.substr(0, pos);
297 }
298 return uri;
299 }
300
GetAbilityAutoStartStateKey(const std::string & bundleName,const std::string & abilityName,int32_t accountId)301 std::string Utils::GetAbilityAutoStartStateKey(const std::string &bundleName, const std::string &abilityName,
302 int32_t accountId)
303 {
304 HILOG_DEBUG("bundle name(%{public}s) ability name(%{public}s) accountId(%{public}d)",
305 bundleName.c_str(), abilityName.c_str(), accountId);
306 return bundleName + "/" + abilityName + "/" + std::to_string(accountId);
307 }
308
SelectUsefulFromVecWithSameBundle(std::vector<std::string> & selectVec,std::vector<std::string> & cmpVec,bool & hasDif,const std::string & bundleName)309 void Utils::SelectUsefulFromVecWithSameBundle(std::vector<std::string> &selectVec, std::vector<std::string> &cmpVec,
310 bool &hasDif, const std::string &bundleName)
311 {
312 HILOG_DEBUG();
313 for (auto iter = selectVec.begin(); iter != selectVec.end();) {
314 if (iter->substr(0, iter->find("/")) != bundleName) {
315 ++iter;
316 continue;
317 }
318 auto it = cmpVec.begin();
319 for (; it != cmpVec.end(); ++it) {
320 if ((*it) == (*iter)) {
321 break;
322 }
323 }
324 if (it == cmpVec.end()) {
325 iter = selectVec.erase(iter);
326 hasDif = true;
327 } else {
328 ++iter;
329 }
330 }
331 }
332
RecordUnavailableEvent(A11yUnavailableEvent event,A11yError errCode,const std::string & bundleName,const std::string & abilityName)333 void Utils::RecordUnavailableEvent(A11yUnavailableEvent event, A11yError errCode,
334 const std::string &bundleName, const std::string &abilityName)
335 {
336 if (!(errCode > A11yError::ERROR_NEED_REPORT_BASE && errCode < A11yError::ERROR_NEED_REPORT_END)) {
337 return;
338 }
339 std::ostringstream oss;
340 oss << "accessibility function is unavailable: " << "event: " << TransferUnavailableEventToString(event)
341 << ", errCode: " << static_cast<int32_t>(errCode)
342 << ", bundleName: " << bundleName << ", abilityName: " << abilityName << ";";
343 std::string info = oss.str();
344 HILOG_DEBUG("accessibility function is unavailable: %{public}s", info.c_str());
345 #ifdef OHOS_BUILD_ENABLE_HISYSEVENT
346 int32_t ret = HiSysEventWrite(
347 OHOS::HiviewDFX::HiSysEvent::Domain::ACCESSIBILITY,
348 "UNAVAILABLE",
349 OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
350 "MSG", info);
351 if (ret != 0) {
352 HILOG_ERROR("Write HiSysEvent error, ret:%{public}d", ret);
353 }
354 #endif //OHOS_BUILD_ENABLE_HISYSEVENT
355 }
356
RecordOnRemoveSystemAbility(int32_t systemAbilityId,const std::string & bundleName,const std::string & abilityName)357 void Utils::RecordOnRemoveSystemAbility(int32_t systemAbilityId, const std::string &bundleName,
358 const std::string &abilityName)
359 {
360 std::ostringstream oss;
361 oss << "OnRemoveSystemAbility systemAbilityId is: " << systemAbilityId
362 << ", bundleName: " << bundleName << ", abilityName: " << abilityName << ";";
363 std::string info = oss.str();
364 HILOG_DEBUG("accessibility function is unavailable: %{public}s", info.c_str());
365 #ifdef OHOS_BUILD_ENABLE_HISYSEVENT
366 int32_t ret = HiSysEventWrite(
367 OHOS::HiviewDFX::HiSysEvent::Domain::ACCESSIBILITY,
368 "UNAVAILABLE",
369 OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
370 "MSG", info);
371 if (ret != 0) {
372 HILOG_ERROR("Write OnRemoveSystemAbility error, ret:%{public}d", ret);
373 }
374 #endif //OHOS_BUILD_ENABLE_HISYSEVENT
375 }
376
RecordDatashareInteraction(A11yDatashareValueType type,const std::string & businessName,const std::string & bundleName,const std::string & abilityName)377 void Utils::RecordDatashareInteraction(A11yDatashareValueType type, const std::string &businessName,
378 const std::string &bundleName, const std::string &abilityName)
379 {
380 std::ostringstream oss;
381 oss << "datashare interaction failed, type is: " << static_cast<uint32_t>(type)
382 << ", businessName: " << businessName << ", bundleName: " << bundleName
383 << ", abilityName: " << abilityName << ";";
384 std::string info = oss.str();
385 HILOG_DEBUG("accessibility function is unavailable: %{public}s", info.c_str());
386 #ifdef OHOS_BUILD_ENABLE_HISYSEVENT
387 int32_t ret = HiSysEventWrite(
388 OHOS::HiviewDFX::HiSysEvent::Domain::ACCESSIBILITY,
389 "UNAVAILABLE",
390 OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
391 "MSG", info);
392 if (ret != 0) {
393 HILOG_ERROR("Write RecordDatashareInteraction error, ret:%{public}d", ret);
394 }
395 #endif //OHOS_BUILD_ENABLE_HISYSEVENT
396 }
397
TransferUnavailableEventToString(A11yUnavailableEvent type)398 std::string Utils::TransferUnavailableEventToString(A11yUnavailableEvent type)
399 {
400 std::string event;
401 switch (type) {
402 case A11yUnavailableEvent::READ_EVENT:
403 event = "READ";
404 break;
405 case A11yUnavailableEvent::CONNECT_EVENT:
406 event = "CONNECT";
407 break;
408 case A11yUnavailableEvent::QUERY_EVENT:
409 event = "QUERY";
410 break;
411 default:
412 event = "UNDEFINE";
413 break;
414 }
415 return event;
416 }
417
RecordStartingA11yEvent(uint32_t flag)418 void Utils::RecordStartingA11yEvent(uint32_t flag)
419 {
420 std::ostringstream oss;
421 oss << "starting accessibility: " << "event: " << "system" << ", id: " << flag << ";";
422 HILOG_DEBUG("starting accessibility: %{public}s", oss.str().c_str());
423 #ifdef OHOS_BUILD_ENABLE_HISYSEVENT
424 int32_t ret = HiSysEventWrite(
425 OHOS::HiviewDFX::HiSysEvent::Domain::ACCESSIBILITY,
426 "STARTING_FUNCTION",
427 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
428 "MSG", oss.str());
429 if (ret != 0) {
430 HILOG_ERROR("Write HiSysEvent error, ret:%{public}d", ret);
431 }
432 #endif //OHOS_BUILD_ENABLE_HISYSEVENT
433 }
434
RecordStartingA11yEvent(const std::string & name)435 void Utils::RecordStartingA11yEvent(const std::string &name)
436 {
437 std::ostringstream oss;
438 oss << "starting accessibility: " << "event: " << "extension" << ", name: " << name << ";";
439 HILOG_DEBUG("starting accessibility: %{public}s", oss.str().c_str());
440 #ifdef OHOS_BUILD_ENABLE_HISYSEVENT
441 int32_t ret = HiSysEventWrite(
442 OHOS::HiviewDFX::HiSysEvent::Domain::ACCESSIBILITY,
443 "STARTING_FUNCTION",
444 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
445 "MSG", oss.str());
446 if (ret != 0) {
447 HILOG_ERROR("Write HiSysEvent error, ret:%{public}d", ret);
448 }
449 #endif //OHOS_BUILD_ENABLE_HISYSEVENT
450 }
451
RecordMSDPUnavailableEvent(const std::string & name)452 void Utils::RecordMSDPUnavailableEvent(const std::string &name)
453 {
454 std::ostringstream oss;
455 oss << "msdp run failed" << ", caused by: " << name << ";";
456 HILOG_DEBUG("starting accessibility: %{public}s", oss.str().c_str());
457 int32_t ret = HiSysEventWrite(
458 OHOS::HiviewDFX::HiSysEvent::Domain::ACCESSIBILITY,
459 "UNAVAILABLE",
460 OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
461 "MSG", oss.str());
462 if (ret != 0) {
463 HILOG_ERROR("Write HiSysEvent error, ret:%{public}d", ret);
464 }
465 }
466
RecordEnableShortkeyAbilityEvent(const std::string & name,const bool & enableState)467 void Utils::RecordEnableShortkeyAbilityEvent(const std::string &name, const bool &enableState)
468 {
469 std::string MSG_NAME = "enable single targets";
470 std::ostringstream oss;
471 std::string enableStateValue = enableState ? "on" : "off";
472 oss << "targets name: " << name.c_str() << ", state:" << enableStateValue.c_str() << ";";
473 HILOG_DEBUG("RecordEnableShortkeyAbilityEvent enable single targets: %{public}s, enableState: %{public}s",
474 name.c_str(), enableStateValue.c_str());
475 #ifdef OHOS_BUILD_ENABLE_HISYSEVENT
476 int32_t ret = HiSysEventWrite(
477 OHOS::HiviewDFX::HiSysEvent::Domain::ACCESSIBILITY_UE,
478 "ENABLE_SHORTKEY_ABILITY_SINGLE",
479 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
480 "MSG_NAME", MSG_NAME, "MSG_VALUE", oss.str());
481 if (ret != 0) {
482 HILOG_ERROR("Write HiSysEvent RecordEnableShortkeyAbilityEvent error, ret:%{public}d", ret);
483 }
484 #endif //OHOS_BUILD_ENABLE_HISYSEVENT
485 }
486
RecordOnZoomGestureEvent(const std::string & state,const bool & isFullType)487 void Utils::RecordOnZoomGestureEvent(const std::string &state, const bool &isFullType)
488 {
489 std::string MSG_NAME = "on zoom gesture state";
490 if (!isFullType) {
491 MSG_NAME = "on partial zoom gesture state";
492 }
493 HILOG_DEBUG("starting RecordOnZoomGestureEvent on zoom gesture state: %{public}s", state.c_str());
494 #ifdef OHOS_BUILD_ENABLE_HISYSEVENT
495 int32_t ret = HiSysEventWrite(
496 OHOS::HiviewDFX::HiSysEvent::Domain::ACCESSIBILITY_UE,
497 "ZOOM_GESTURE_ACTION",
498 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
499 "MSG_NAME", MSG_NAME, "MSG_VALUE", state);
500 if (ret != 0) {
501 HILOG_ERROR("Write HiSysEvent RecordOnZoomGestureEvent error, ret:%{public}d", ret);
502 }
503 #endif //OHOS_BUILD_ENABLE_HISYSEVENT
504 }
505
VectorToString(const std::vector<std::string> & vectorVal,std::string & stringOut)506 void Utils::VectorToString(const std::vector<std::string> &vectorVal, std::string &stringOut)
507 {
508 HILOG_DEBUG();
509 int32_t i = 0;
510 for (auto& var : vectorVal) {
511 if (i > 0) {
512 stringOut = stringOut + ',';
513 }
514 stringOut = stringOut + var.c_str();
515 i++;
516 }
517 HILOG_DEBUG("stringOUT = %{public}s .", stringOut.c_str());
518 }
519
StringToVector(const std::string & stringIn,std::vector<std::string> & vectorResult)520 void Utils::StringToVector(const std::string &stringIn, std::vector<std::string> &vectorResult)
521 {
522 HILOG_DEBUG();
523 int32_t strLength = static_cast<int32_t>(stringIn.size());
524 std::vector<int32_t> position;
525
526 if (strLength <= 0 || strLength > STRING_LEN_MAX) {
527 return;
528 }
529
530 for (int32_t j = 0; j < strLength; j++) {
531 if (stringIn[j] == ',') {
532 position.push_back(j);
533 }
534 }
535
536 int32_t wrodCount = static_cast<int32_t>(position.size());
537 if (wrodCount == 0) {
538 vectorResult.push_back(stringIn);
539 } else {
540 int32_t startWrod = 0;
541 int32_t length = 0;
542 for (int32_t i = 0; i <= wrodCount; i++) {
543 if (i == 0) {
544 length = position[i];
545 vectorResult.push_back(stringIn.substr(startWrod, length)); // First string
546 } else if (i < wrodCount) {
547 startWrod = position[i - 1] + 1;
548 length = position[i] - position[i - 1] - 1;
549 vectorResult.push_back(stringIn.substr(startWrod, length)); // Second string to last-1 string
550 } else {
551 startWrod = position[i - 1] + 1;
552 length = strLength - position[i - 1] - 1;
553 vectorResult.push_back(stringIn.substr(startWrod, length)); // Last string
554 }
555 }
556 }
557 HILOG_DEBUG("strLength = %{public}d, wrodCount = %{public}d, stringIn : %{public}s",
558 strLength, wrodCount, stringIn.c_str());
559 for (auto& var : vectorResult) {
560 HILOG_DEBUG("vectorResult = %{public}s ", var.c_str());
561 }
562 }
563
GetUserIdByCallingUid()564 int32_t Utils::GetUserIdByCallingUid()
565 {
566 int32_t uid = IPCSkeleton::GetCallingUid();
567 if (uid <= INVALID_ID) {
568 return INVALID_USER_ID;
569 }
570 return (uid / BASE_USER_RANGE);
571 }
572
UpdateColorModeConfiguration(int32_t & accountId)573 bool Utils::UpdateColorModeConfiguration(int32_t &accountId)
574 {
575 HILOG_DEBUG();
576 auto appMgrClient = std::make_unique<AppExecFwk::AppMgrClient>();
577 if (appMgrClient == nullptr) {
578 HILOG_ERROR("create appMgrClient failed");
579 return false;
580 }
581 AppExecFwk::Configuration configuration;
582 configuration.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE,
583 AppExecFwk::ConfigurationInner::COLOR_MODE_LIGHT);
584 AppExecFwk::AppMgrResultCode status = appMgrClient->UpdateConfiguration(configuration, accountId);
585 if (status != AppExecFwk::AppMgrResultCode::RESULT_OK) {
586 HILOG_ERROR("UpdateConfiguration: Update configuration failed.");
587 return false;
588 }
589 return true;
590 }
591
IsWideFold()592 bool Utils::IsWideFold()
593 {
594 return IS_WIDE_FOLD;
595 }
596
IsBigFold()597 bool Utils::IsBigFold()
598 {
599 return IS_BIG_FOLD;
600 }
601
GetBundleNameByCallingUid(std::string & bundleName)602 bool Utils::GetBundleNameByCallingUid(std::string &bundleName)
603 {
604 int32_t uid = IPCSkeleton::GetCallingUid();
605 if (uid <= INVALID_ID) {
606 return false;
607 }
608 bool ret = Singleton<AccessibilityResourceBundleManager>::GetInstance().GetBundleNameByUid(uid, bundleName);
609 if (ret == false) {
610 HILOG_ERROR("GetBundleNameByCallingUid failed");
611 return ret;
612 }
613 HILOG_DEBUG("GetCallingUid:%{public}d, getBundleNameByUid:%{public}s", uid, bundleName.c_str());
614 return true;
615 }
616
FormatString(const std::string & format,const std::string & value)617 std::string Utils::FormatString(const std::string& format, const std::string& value)
618 {
619 int bufferSize = static_cast<int>(format.size() + value.size() + 1); // +1 for null terminator
620 char* buffer = new char[bufferSize];
621 if (snprintf_s(buffer, bufferSize, bufferSize -1, format.c_str(), value.c_str()) < 0) {
622 delete[] buffer;
623 return "";
624 }
625 std::string result(buffer);
626 delete[] buffer;
627 return result;
628 }
629 } // namespace Accessibility
630 } // namespace OHOS