1 /*
2 * Copyright (c) 2021-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 "key_command_handler_util.h"
17 #include "json_parser.h"
18
19 #ifdef SHORTCUT_KEY_MANAGER_ENABLED
20 #include "key_shortcut_manager.h"
21 #endif // SHORTCUT_KEY_MANAGER_ENABLED
22
23 namespace OHOS {
24 namespace MMI {
IsSpecialType(int32_t keyCode,SpecialType type)25 bool IsSpecialType(int32_t keyCode, SpecialType type)
26 {
27 auto it = SPECIAL_KEYS.find(keyCode);
28 if (it == SPECIAL_KEYS.end()) {
29 return false;
30 }
31 return (it->second == SpecialType::SPECIAL_ALL || it->second == type);
32 }
33
GetBusinessId(const cJSON * jsonData,std::string & businessIdValue,std::vector<std::string> & businessIds)34 bool GetBusinessId(const cJSON* jsonData, std::string &businessIdValue, std::vector<std::string> &businessIds)
35 {
36 if (!cJSON_IsObject(jsonData)) {
37 MMI_HILOGE("The json data is not object");
38 return false;
39 }
40 cJSON *businessId = cJSON_GetObjectItemCaseSensitive(jsonData, "businessId");
41 if (!cJSON_IsString(businessId)) {
42 MMI_HILOGE("The business id is not string");
43 return false;
44 }
45 businessIdValue = businessId->valuestring;
46 businessIds.push_back(businessIdValue);
47 return true;
48 }
49
GetPreKeys(const cJSON * jsonData,ShortcutKey & shortcutKey)50 bool GetPreKeys(const cJSON* jsonData, ShortcutKey &shortcutKey)
51 {
52 if (!cJSON_IsObject(jsonData)) {
53 MMI_HILOGE("The json data is not object");
54 return false;
55 }
56 cJSON* preKey = cJSON_GetObjectItemCaseSensitive(jsonData, "preKey");
57 if (!cJSON_IsArray(preKey)) {
58 MMI_HILOGE("The pre-key number must be array");
59 return false;
60 }
61 int32_t preKeySize = cJSON_GetArraySize(preKey);
62 if (preKeySize > MAX_PREKEYS_NUM) {
63 MMI_HILOGE("The pre-key size number must less and equal four");
64 return false;
65 }
66 for (int32_t i = 0; i < preKeySize; ++i) {
67 cJSON *preKeyJson = cJSON_GetArrayItem(preKey, i);
68 if (!cJSON_IsNumber(preKeyJson)) {
69 MMI_HILOGE("The pre-key json is not number");
70 return false;
71 }
72 if (preKeyJson->valueint < 0) {
73 MMI_HILOGE("The pre-key json must be number and bigger or equal than 0");
74 return false;
75 }
76 if (!shortcutKey.preKeys.emplace(preKeyJson->valueint).second) {
77 MMI_HILOGE("The pre-key json must be unduplicated");
78 return false;
79 }
80 }
81 return true;
82 }
83
GetTrigger(const cJSON * jsonData,int32_t & triggerType)84 bool GetTrigger(const cJSON* jsonData, int32_t &triggerType)
85 {
86 if (!cJSON_IsObject(jsonData)) {
87 MMI_HILOGE("The json data is not object");
88 return false;
89 }
90 cJSON *trigger = cJSON_GetObjectItemCaseSensitive(jsonData, "trigger");
91 if (!cJSON_IsString(trigger)) {
92 MMI_HILOGE("The trigger is not string");
93 return false;
94 }
95 if (((std::strcmp(trigger->valuestring, "key_up") != 0)
96 && (std::strcmp(trigger->valuestring, "key_down") != 0))) {
97 MMI_HILOGE("The trigger must be one of [key_up, key_down]");
98 return false;
99 }
100 if (std::strcmp(trigger->valuestring, "key_up") == 0) {
101 triggerType = KeyEvent::KEY_ACTION_UP;
102 } else {
103 triggerType = KeyEvent::KEY_ACTION_DOWN;
104 }
105 return true;
106 }
107
GetKeyDownDuration(const cJSON * jsonData,int32_t & keyDownDurationInt)108 bool GetKeyDownDuration(const cJSON* jsonData, int32_t &keyDownDurationInt)
109 {
110 if (!cJSON_IsObject(jsonData)) {
111 MMI_HILOGE("The json data is not object");
112 return false;
113 }
114 cJSON *keyDownDuration = cJSON_GetObjectItemCaseSensitive(jsonData, "keyDownDuration");
115 if (!cJSON_IsNumber(keyDownDuration)) {
116 MMI_HILOGE("The key-down duration is not number");
117 return false;
118 }
119 if (keyDownDuration->valueint < 0 || keyDownDuration->valueint > MAX_KEYDOWNDURATION_TIME) {
120 MMI_HILOGE("The key-down duration must be number and bigger and equal zero");
121 return false;
122 }
123 keyDownDurationInt = keyDownDuration->valueint;
124 return true;
125 }
126
GetKeyFinalKey(const cJSON * jsonData,int32_t & finalKeyInt)127 bool GetKeyFinalKey(const cJSON* jsonData, int32_t &finalKeyInt)
128 {
129 if (!cJSON_IsObject(jsonData)) {
130 MMI_HILOGE("The json data is not object");
131 return false;
132 }
133 cJSON *finalKey = cJSON_GetObjectItemCaseSensitive(jsonData, "finalKey");
134 if (!cJSON_IsNumber(finalKey)) {
135 MMI_HILOGE("The final key must be number");
136 return false;
137 }
138 finalKeyInt = finalKey->valueint;
139 return true;
140 }
141
GetKeyVal(const cJSON * json,const std::string & key,std::string & value)142 void GetKeyVal(const cJSON* json, const std::string &key, std::string &value)
143 {
144 if (!cJSON_IsObject(json)) {
145 MMI_HILOGE("The json is not object");
146 return;
147 }
148 cJSON *valueJson = cJSON_GetObjectItemCaseSensitive(json, key.c_str());
149 if (valueJson == nullptr) {
150 MMI_HILOGE("The value json init failed");
151 return;
152 }
153 if (cJSON_IsString(valueJson)) {
154 value = valueJson->valuestring;
155 }
156 }
157
GetEntities(const cJSON * jsonAbility,Ability & ability)158 bool GetEntities(const cJSON* jsonAbility, Ability &ability)
159 {
160 if (!cJSON_IsObject(jsonAbility)) {
161 MMI_HILOGE("The json ability is not object");
162 return false;
163 }
164 cJSON *entities = cJSON_GetObjectItemCaseSensitive(jsonAbility, "entities");
165 if (entities == nullptr) {
166 return true;
167 }
168 if (!cJSON_IsArray(entities)) {
169 MMI_HILOGE("The entities must be array");
170 return false;
171 }
172 int32_t entitySize = cJSON_GetArraySize(entities);
173 for (int32_t i = 0; i < entitySize; i++) {
174 cJSON* entity = cJSON_GetArrayItem(entities, i);
175 if (entity == nullptr) {
176 MMI_HILOGE("The entity init failed");
177 continue;
178 }
179 if (!cJSON_IsString(entity)) {
180 MMI_HILOGE("The entity is not string");
181 return false;
182 }
183 ability.entities.push_back(entity->valuestring);
184 }
185 return true;
186 }
187
GetParams(const cJSON * jsonAbility,Ability & ability)188 bool GetParams(const cJSON* jsonAbility, Ability &ability)
189 {
190 if (!cJSON_IsObject(jsonAbility)) {
191 MMI_HILOGE("The json ability is not object");
192 return false;
193 }
194 cJSON *params = cJSON_GetObjectItemCaseSensitive(jsonAbility, "params");
195 if (params == nullptr) {
196 return true;
197 }
198 if (!cJSON_IsArray(params)) {
199 MMI_HILOGE("The params must be array");
200 return false;
201 }
202 int32_t paramsSize = cJSON_GetArraySize(params);
203 for (int32_t i = 0; i < paramsSize; ++i) {
204 cJSON* param = cJSON_GetArrayItem(params, i);
205 if (!cJSON_IsObject(param)) {
206 MMI_HILOGE("The param must be object");
207 return false;
208 }
209 cJSON* key = cJSON_GetObjectItemCaseSensitive(param, "key");
210 if (!cJSON_IsString(key)) {
211 MMI_HILOGE("The key is not string");
212 return false;
213 }
214 cJSON* value = cJSON_GetObjectItemCaseSensitive(param, "value");
215 if (!cJSON_IsString(value)) {
216 MMI_HILOGE("The value is not string");
217 return false;
218 }
219 auto ret = ability.params.emplace(key->valuestring, value->valuestring);
220 if (!ret.second) {
221 MMI_HILOGW("The key is duplicated");
222 }
223 }
224 return true;
225 }
226
PackageAbility(const cJSON * jsonAbility,Ability & ability)227 bool PackageAbility(const cJSON* jsonAbility, Ability &ability)
228 {
229 if (!cJSON_IsObject(jsonAbility)) {
230 MMI_HILOGE("The json ability is not object");
231 return false;
232 }
233 GetKeyVal(jsonAbility, "bundleName", ability.bundleName);
234 GetKeyVal(jsonAbility, "abilityName", ability.abilityName);
235 GetKeyVal(jsonAbility, "action", ability.action);
236 GetKeyVal(jsonAbility, "type", ability.type);
237 GetKeyVal(jsonAbility, "deviceId", ability.deviceId);
238 GetKeyVal(jsonAbility, "uri", ability.uri);
239 GetKeyVal(jsonAbility, "abilityType", ability.abilityType);
240 if (!GetEntities(jsonAbility, ability)) {
241 MMI_HILOGE("Get centities failed");
242 return false;
243 }
244 if (!GetParams(jsonAbility, ability)) {
245 MMI_HILOGE("Get params failed");
246 return false;
247 }
248 return true;
249 }
250
ConvertToShortcutKey(const cJSON * jsonData,ShortcutKey & shortcutKey,std::vector<std::string> & businessIds)251 bool ConvertToShortcutKey(const cJSON* jsonData, ShortcutKey &shortcutKey, std::vector<std::string> &businessIds)
252 {
253 if (!cJSON_IsObject(jsonData)) {
254 MMI_HILOGE("The json data is not object");
255 return false;
256 }
257 if (!GetBusinessId(jsonData, shortcutKey.businessId, businessIds)) {
258 MMI_HILOGW("Get abilityKey failed");
259 }
260 if (!GetPreKeys(jsonData, shortcutKey)) {
261 MMI_HILOGE("Get preKeys failed");
262 return false;
263 }
264 if (!GetKeyFinalKey(jsonData, shortcutKey.finalKey)) {
265 MMI_HILOGE("Get finalKey failed");
266 return false;
267 }
268 if (!GetTrigger(jsonData, shortcutKey.triggerType)) {
269 MMI_HILOGE("Get trigger failed");
270 return false;
271 }
272 if (!GetKeyDownDuration(jsonData, shortcutKey.keyDownDuration)) {
273 MMI_HILOGE("Get downDuration failed");
274 return false;
275 }
276
277 GetKeyVal(jsonData, "statusConfig", shortcutKey.statusConfig);
278
279 cJSON *ability = cJSON_GetObjectItemCaseSensitive(jsonData, "ability");
280 if (!cJSON_IsObject(ability)) {
281 MMI_HILOGE("The ability is not object");
282 return false;
283 }
284 if (!PackageAbility(ability, shortcutKey.ability)) {
285 MMI_HILOGE("Package ability failed");
286 return false;
287 }
288 return true;
289 }
290
GetKeyCode(const cJSON * jsonData,int32_t & keyCodeInt)291 bool GetKeyCode(const cJSON* jsonData, int32_t &keyCodeInt)
292 {
293 if (!cJSON_IsObject(jsonData)) {
294 MMI_HILOGE("The json data is not object");
295 return false;
296 }
297 cJSON *keyCode = cJSON_GetObjectItemCaseSensitive(jsonData, "keyCode");
298 if (keyCode == nullptr) {
299 MMI_HILOGE("The keyCode init failed");
300 return false;
301 }
302 if (!cJSON_IsNumber(keyCode)) {
303 MMI_HILOGE("The keyCode is not number");
304 return false;
305 }
306 if (keyCode->valueint < 0) {
307 MMI_HILOGE("The keyCode must be number and bigger and equal zero");
308 return false;
309 }
310 keyCodeInt = keyCode->valueint;
311 return true;
312 }
313
GetKeyAction(const cJSON * jsonData,int32_t & keyActionInt)314 bool GetKeyAction(const cJSON* jsonData, int32_t &keyActionInt)
315 {
316 if (!cJSON_IsObject(jsonData)) {
317 MMI_HILOGE("The json data is not object");
318 return false;
319 }
320 cJSON *keyAction = cJSON_GetObjectItemCaseSensitive(jsonData, "keyAction");
321 if (keyAction == nullptr) {
322 MMI_HILOGE("THe key action Init failed");
323 return false;
324 }
325 if (!cJSON_IsNumber(keyAction)) {
326 MMI_HILOGE("THe key action is not number");
327 return false;
328 }
329 if ((keyAction->valueint != KeyEvent::KEY_ACTION_DOWN) && (keyAction->valueint != KeyEvent::KEY_ACTION_UP)) {
330 MMI_HILOGE("THe key action must be down or up");
331 return false;
332 }
333 keyActionInt = keyAction->valueint;
334 return true;
335 }
336
GetDelay(const cJSON * jsonData,int64_t & delayInt)337 bool GetDelay(const cJSON* jsonData, int64_t &delayInt)
338 {
339 if (!cJSON_IsObject(jsonData)) {
340 MMI_HILOGE("The json data is not object");
341 return false;
342 }
343 cJSON *delay = cJSON_GetObjectItemCaseSensitive(jsonData, "delay");
344 if (delay == nullptr) {
345 MMI_HILOGE("The delay init failed");
346 return false;
347 }
348 if (!cJSON_IsNumber(delay)) {
349 MMI_HILOGE("The delay is not number");
350 return false;
351 }
352 if ((delay->valueint < 0) || (delay->valueint > MAX_DELAY_TIME)) {
353 MMI_HILOGE("The delay must be number and bigger and equal zero and less than max delay");
354 return false;
355 }
356 delayInt = delay->valueint * SECONDS_SYSTEM;
357 return true;
358 }
359
GetRepeatKeyDelay(const cJSON * jsonData,int64_t & delayInt)360 bool GetRepeatKeyDelay(const cJSON* jsonData, int64_t &delayInt)
361 {
362 if (!cJSON_IsObject(jsonData)) {
363 MMI_HILOGE("The json data is not object");
364 return false;
365 }
366 cJSON *delay = cJSON_GetObjectItemCaseSensitive(jsonData, "delay");
367 if (delay == nullptr) {
368 MMI_HILOGE("The delay init failed");
369 return false;
370 }
371 if (!cJSON_IsNumber(delay)) {
372 MMI_HILOGE("The delay is not number");
373 return false;
374 }
375 if ((delay->valueint < 0) || (delay->valueint > MAX_REPEATKEY_DELAY_TIME)) {
376 MMI_HILOGE("The delay must be number and bigger and equal zero and less than max delay");
377 return false;
378 }
379 delayInt = delay->valueint * SECONDS_SYSTEM;
380 return true;
381 }
382
GetRepeatTimes(const cJSON * jsonData,int32_t & repeatTimesInt)383 bool GetRepeatTimes(const cJSON* jsonData, int32_t &repeatTimesInt)
384 {
385 if (!cJSON_IsObject(jsonData)) {
386 MMI_HILOGE("Get repeat times jsonData is not object");
387 return false;
388 }
389 cJSON *repeatTimes = cJSON_GetObjectItemCaseSensitive(jsonData, "times");
390 if (repeatTimes == nullptr) {
391 MMI_HILOGE("The repeat times init failed");
392 return false;
393 }
394 if (!cJSON_IsNumber(repeatTimes)) {
395 MMI_HILOGE("The repeat times is not number");
396 return false;
397 }
398 if (repeatTimes->valueint < 0) {
399 MMI_HILOGE("The repeat times must be number and bigger and equal zero");
400 return false;
401 }
402 repeatTimesInt = repeatTimes->valueint;
403 return true;
404 }
405
GetAbilityStartDelay(const cJSON * jsonData,int64_t & abilityStartDelayInt)406 bool GetAbilityStartDelay(const cJSON* jsonData, int64_t &abilityStartDelayInt)
407 {
408 if (!cJSON_IsObject(jsonData)) {
409 MMI_HILOGE("The json Data is not object");
410 return false;
411 }
412 cJSON *abilityStartDelay = cJSON_GetObjectItemCaseSensitive(jsonData, "abilityStartDelay");
413 if (abilityStartDelay == nullptr) {
414 MMI_HILOGE("The ability start delay init failed");
415 return false;
416 }
417 if (!cJSON_IsNumber(abilityStartDelay)) {
418 MMI_HILOGE("The ability start delay is not number");
419 return false;
420 }
421 if ((abilityStartDelay->valueint < 0) || (abilityStartDelay->valueint > MAX_ABILITYSTARTDELAY_TIME)) {
422 MMI_HILOGE("The ability start delay must be number and bigger and equal zero and less than max delay time");
423 return false;
424 }
425 abilityStartDelayInt = abilityStartDelay->valueint;
426 return true;
427 }
428
PackageSequenceKey(const cJSON * sequenceKeysJson,SequenceKey & sequenceKey)429 bool PackageSequenceKey(const cJSON* sequenceKeysJson, SequenceKey &sequenceKey)
430 {
431 if (!cJSON_IsObject(sequenceKeysJson)) {
432 MMI_HILOGE("The sequence keys json is not object");
433 return false;
434 }
435 if (!GetKeyCode(sequenceKeysJson, sequenceKey.keyCode)) {
436 MMI_HILOGE("Get keyCode failed");
437 return false;
438 }
439 if (!GetKeyAction(sequenceKeysJson, sequenceKey.keyAction)) {
440 MMI_HILOGE("Get keyAction failed");
441 return false;
442 }
443 if (!GetDelay(sequenceKeysJson, sequenceKey.delay)) {
444 MMI_HILOGE("Get delay failed");
445 return false;
446 }
447 return true;
448 }
449
GetSequenceKeys(const cJSON * jsonData,Sequence & sequence)450 bool GetSequenceKeys(const cJSON* jsonData, Sequence &sequence)
451 {
452 if (!cJSON_IsObject(jsonData)) {
453 MMI_HILOGE("The json data is not object");
454 return false;
455 }
456 cJSON* sequenceKeys = cJSON_GetObjectItemCaseSensitive(jsonData, "sequenceKeys");
457 if (!cJSON_IsArray(sequenceKeys)) {
458 MMI_HILOGE("The sequence keys number must be array");
459 return false;
460 }
461 int32_t sequenceKeysSize = cJSON_GetArraySize(sequenceKeys);
462 if (sequenceKeysSize > MAX_SEQUENCEKEYS_NUM) {
463 MMI_HILOGE("The sequence keys size number must less and equal %{public}d", MAX_SEQUENCEKEYS_NUM);
464 return false;
465 }
466 for (int32_t i = 0; i < sequenceKeysSize; ++i) {
467 cJSON *sequenceKeysJson = cJSON_GetArrayItem(sequenceKeys, i);
468 if (!cJSON_IsObject(sequenceKeysJson)) {
469 MMI_HILOGE("The sequence keys json is not object");
470 return false;
471 }
472 SequenceKey sequenceKey;
473 if (!PackageSequenceKey(sequenceKeysJson, sequenceKey)) {
474 MMI_HILOGE("Packege sequence key failed");
475 return false;
476 }
477 sequence.sequenceKeys.push_back(sequenceKey);
478 }
479 return true;
480 }
481
IsSequenceKeysValid(const Sequence & sequence)482 bool IsSequenceKeysValid(const Sequence &sequence)
483 {
484 if (sequence.sequenceKeys.empty()) {
485 MMI_HILOGE("The sequence keys can not be empty");
486 return false;
487 }
488
489 if (sequence.sequenceKeys.size() > MAX_SEQUENCEKEYS_NUM) {
490 MMI_HILOGE("The sequence keys size must less or equal to %{public}d", MAX_SEQUENCEKEYS_NUM);
491 return false;
492 }
493
494 std::map<int32_t, SequenceKey> sequenceKeys;
495 for (const SequenceKey& item : sequence.sequenceKeys) {
496 if (sequenceKeys.find(item.keyCode) == sequenceKeys.end()) {
497 auto it = sequenceKeys.emplace(item.keyCode, item);
498 if (!it.second) {
499 MMI_HILOGE("The key code is duplicated");
500 return false;
501 }
502 } else {
503 if (sequenceKeys[item.keyCode].keyAction == item.keyAction) {
504 MMI_HILOGE("The sequence keys illegal");
505 return false;
506 }
507 sequenceKeys[item.keyCode].keyAction = item.keyAction;
508 sequenceKeys[item.keyCode].delay = item.delay;
509 }
510 }
511 return true;
512 }
513
ConvertToKeySequence(const cJSON * jsonData,Sequence & sequence)514 bool ConvertToKeySequence(const cJSON* jsonData, Sequence &sequence)
515 {
516 if (!cJSON_IsObject(jsonData)) {
517 MMI_HILOGE("The json data is not object");
518 return false;
519 }
520 if (!GetSequenceKeys(jsonData, sequence)) {
521 MMI_HILOGE("Get sequence keys failed");
522 return false;
523 }
524 if (!IsSequenceKeysValid(sequence)) {
525 MMI_HILOGE("Sequence invalid");
526 return false;
527 }
528 if (!GetAbilityStartDelay(jsonData, sequence.abilityStartDelay)) {
529 MMI_HILOGE("Get ability start delay failed");
530 return false;
531 }
532
533 GetKeyVal(jsonData, "statusConfig", sequence.statusConfig);
534
535 cJSON *ability = cJSON_GetObjectItemCaseSensitive(jsonData, "ability");
536 if (!cJSON_IsObject(ability)) {
537 MMI_HILOGE("The ability is not object");
538 return false;
539 }
540 if (!PackageAbility(ability, sequence.ability)) {
541 MMI_HILOGE("Package ability failed");
542 return false;
543 }
544 return true;
545 }
546
ConvertToExcludeKey(const cJSON * jsonData,ExcludeKey & exKey)547 bool ConvertToExcludeKey(const cJSON* jsonData, ExcludeKey &exKey)
548 {
549 cJSON *keyCodeJson = cJSON_GetObjectItemCaseSensitive(jsonData, "keyCode");
550 if (!cJSON_IsNumber(keyCodeJson)) {
551 MMI_HILOGE("The keyCode json is not number");
552 return false;
553 }
554 exKey.keyCode = keyCodeJson->valueint;
555
556 cJSON *keyActionJson = cJSON_GetObjectItemCaseSensitive(jsonData, "keyAction");
557 if (!cJSON_IsNumber(keyActionJson)) {
558 MMI_HILOGE("THe keyAction json is not number");
559 return false;
560 }
561 exKey.keyAction = keyActionJson->valueint;
562
563 cJSON *delayJson = cJSON_GetObjectItemCaseSensitive(jsonData, "delay");
564 if (!cJSON_IsNumber(delayJson)) {
565 MMI_HILOGE("The delay json is not number");
566 return false;
567 }
568 exKey.delay = delayJson->valueint;
569
570 return true;
571 }
572
ConvertToKeyRepeat(const cJSON * jsonData,RepeatKey & repeatKey)573 bool ConvertToKeyRepeat(const cJSON* jsonData, RepeatKey &repeatKey)
574 {
575 if (!cJSON_IsObject(jsonData)) {
576 MMI_HILOGE("The json data is not object");
577 return false;
578 }
579
580 if (!GetKeyCode(jsonData, repeatKey.keyCode)) {
581 MMI_HILOGE("Get keyCode failed");
582 return false;
583 }
584
585 if (!GetRepeatTimes(jsonData, repeatKey.times)) {
586 MMI_HILOGE("Get repeat times failed");
587 return false;
588 }
589
590 if (!GetRepeatKeyDelay(jsonData, repeatKey.delay)) {
591 MMI_HILOGE("Get delay failed");
592 return false;
593 }
594
595 GetKeyVal(jsonData, "statusConfig", repeatKey.statusConfig);
596
597 cJSON *ability = cJSON_GetObjectItemCaseSensitive(jsonData, "ability");
598 if (!cJSON_IsObject(ability)) {
599 MMI_HILOGE("The ability is not object");
600 return false;
601 }
602 if (!PackageAbility(ability, repeatKey.ability)) {
603 MMI_HILOGE("Package ability failed");
604 return false;
605 }
606 return true;
607 }
608
GenerateKey(const ShortcutKey & key)609 std::string GenerateKey(const ShortcutKey& key)
610 {
611 std::set<int32_t> preKeys = key.preKeys;
612 std::stringstream ss;
613 for (const auto& preKey : preKeys) {
614 ss << preKey << ",";
615 }
616 ss << key.finalKey << ",";
617 ss << key.triggerType << ",";
618 ss << key.keyDownDuration;
619 return std::string(ss.str());
620 }
621
622 #ifdef SHORTCUT_KEY_MANAGER_ENABLED
RegisterSystemKey(const ShortcutKey & shortcutKey,std::function<void (std::shared_ptr<KeyEvent>)> callback)623 static int32_t RegisterSystemKey(const ShortcutKey &shortcutKey,
624 std::function<void(std::shared_ptr<KeyEvent>)> callback)
625 {
626 KeyShortcutManager::SystemShortcutKey sysKey {
627 .modifiers = shortcutKey.preKeys,
628 .finalKey = shortcutKey.finalKey,
629 .longPressTime = shortcutKey.keyDownDuration,
630 .triggerType = (shortcutKey.triggerType == KeyEvent::KEY_ACTION_DOWN ?
631 KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN : KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_UP),
632 .callback = callback,
633 };
634 return KEY_SHORTCUT_MGR->RegisterSystemKey(sysKey);
635 }
636 #endif // SHORTCUT_KEY_MANAGER_ENABLED
637
ParseShortcutKeys(const JsonParser & parser,std::map<std::string,ShortcutKey> & shortcutKeyMap,std::vector<std::string> & businessIds)638 bool ParseShortcutKeys(const JsonParser& parser, std::map<std::string, ShortcutKey>& shortcutKeyMap,
639 std::vector<std::string>& businessIds)
640 {
641 cJSON* shortkeys = cJSON_GetObjectItemCaseSensitive(parser.Get(), "Shortkeys");
642 if (!cJSON_IsArray(shortkeys)) {
643 MMI_HILOGE("The short keys is not array");
644 return false;
645 }
646 int32_t shortkeysSize = cJSON_GetArraySize(shortkeys);
647 for (int32_t i = 0; i < shortkeysSize; ++i) {
648 ShortcutKey shortcutKey;
649 cJSON *shortkey = cJSON_GetArrayItem(shortkeys, i);
650 if (!cJSON_IsObject(shortkey)) {
651 continue;
652 }
653 if (!ConvertToShortcutKey(shortkey, shortcutKey, businessIds)) {
654 continue;
655 }
656 #ifdef SHORTCUT_KEY_MANAGER_ENABLED
657 shortcutKey.shortcutId = RegisterSystemKey(shortcutKey,
658 [shortcutKey](std::shared_ptr<KeyEvent> keyEvent) {});
659 if (shortcutKey.shortcutId < 0) {
660 MMI_HILOGE("Register system key fail, error:%{public}d", shortcutKey.shortcutId);
661 continue;
662 }
663 #endif // SHORTCUT_KEY_MANAGER_ENABLED
664 std::string key = GenerateKey(shortcutKey);
665 if (shortcutKeyMap.find(key) == shortcutKeyMap.end()) {
666 if (!shortcutKeyMap.emplace(key, shortcutKey).second) {
667 MMI_HILOGW("Duplicate shortcutKey:%s", key.c_str());
668 }
669 }
670 }
671 return true;
672 }
673
ParseSequences(const JsonParser & parser,std::vector<Sequence> & sequenceVec)674 bool ParseSequences(const JsonParser& parser, std::vector<Sequence>& sequenceVec)
675 {
676 cJSON* sequences = cJSON_GetObjectItemCaseSensitive(parser.Get(), "Sequences");
677 if (!cJSON_IsArray(sequences)) {
678 MMI_HILOGE("The sequences is not array");
679 return false;
680 }
681 int32_t sequencesSize = cJSON_GetArraySize(sequences);
682 for (int32_t i = 0; i < sequencesSize; ++i) {
683 Sequence seq;
684 cJSON *sequence = cJSON_GetArrayItem(sequences, i);
685 if (!cJSON_IsObject(sequence)) {
686 continue;
687 }
688 if (!ConvertToKeySequence(sequence, seq)) {
689 continue;
690 }
691 sequenceVec.push_back(seq);
692 }
693 return true;
694 }
695
ParseExcludeKeys(const JsonParser & parser,std::vector<ExcludeKey> & excludeKeyVec)696 bool ParseExcludeKeys(const JsonParser& parser, std::vector<ExcludeKey>& excludeKeyVec)
697 {
698 cJSON* excludeKeys = cJSON_GetObjectItemCaseSensitive(parser.Get(), "excludeKeys");
699 if (!cJSON_IsArray(excludeKeys)) {
700 MMI_HILOGE("The exclude keys is not array");
701 return false;
702 }
703 int32_t excludeKeysSize = cJSON_GetArraySize(excludeKeys);
704 for (int32_t i = 0; i < excludeKeysSize; ++i) {
705 ExcludeKey exKey;
706 cJSON *keyJson = cJSON_GetArrayItem(excludeKeys, i);
707 if (!cJSON_IsObject(keyJson)) {
708 continue;
709 }
710 if (!ConvertToExcludeKey(keyJson, exKey)) {
711 continue;
712 }
713 excludeKeyVec.push_back(exKey);
714 }
715 return true;
716 }
717
ParseRepeatKeys(const JsonParser & parser,std::vector<RepeatKey> & repeatKeyVec,std::map<int32_t,int32_t> & repeatKeyMaxTimes)718 bool ParseRepeatKeys(const JsonParser& parser, std::vector<RepeatKey>& repeatKeyVec,
719 std::map<int32_t, int32_t>& repeatKeyMaxTimes)
720 {
721 cJSON* repeatKeys = cJSON_GetObjectItemCaseSensitive(parser.Get(), "RepeatKeys");
722 if (!cJSON_IsArray(repeatKeys)) {
723 MMI_HILOGE("The repeat keys is not array");
724 return false;
725 }
726 int32_t repeatKeysSize = cJSON_GetArraySize(repeatKeys);
727 for (int32_t i = 0; i < repeatKeysSize; i++) {
728 cJSON *repeatKey = cJSON_GetArrayItem(repeatKeys, i);
729 if (!cJSON_IsObject(repeatKey)) {
730 continue;
731 }
732 RepeatKey rep;
733 if (!ConvertToKeyRepeat(repeatKey, rep)) {
734 continue;
735 }
736 repeatKeyVec.push_back(rep);
737 if (repeatKeyMaxTimes.find(rep.keyCode) == repeatKeyMaxTimes.end()) {
738 repeatKeyMaxTimes.insert(std::make_pair(rep.keyCode, rep.times));
739 }
740 if (repeatKeyMaxTimes[rep.keyCode] < rep.times) {
741 repeatKeyMaxTimes[rep.keyCode] = rep.times;
742 }
743 }
744
745 return true;
746 }
747
ParseTwoFingerGesture(const JsonParser & parser,TwoFingerGesture & gesture)748 bool ParseTwoFingerGesture(const JsonParser& parser, TwoFingerGesture& gesture)
749 {
750 cJSON *jsonData = cJSON_GetObjectItemCaseSensitive(parser.Get(), "TwoFingerGesture");
751 if (!cJSON_IsObject(jsonData)) {
752 MMI_HILOGE("Two finger gesture is not object");
753 return false;
754 }
755 if (!GetAbilityStartDelay(jsonData, gesture.abilityStartDelay)) {
756 MMI_HILOGE("Get ability start delay failed");
757 return false;
758 }
759 cJSON *ability = cJSON_GetObjectItemCaseSensitive(jsonData, "ability");
760 if (!cJSON_IsObject(ability)) {
761 MMI_HILOGE("The ability is not object");
762 return false;
763 }
764 if (!PackageAbility(ability, gesture.ability)) {
765 MMI_HILOGE("Package ability failed");
766 return false;
767 }
768 gesture.active = true;
769 return true;
770 }
771
IsPackageKnuckleGesture(const cJSON * jsonData,const std::string knuckleGesture,Ability & launchAbility)772 bool IsPackageKnuckleGesture(const cJSON* jsonData, const std::string knuckleGesture, Ability &launchAbility)
773 {
774 cJSON *knuckleGestureData = cJSON_GetObjectItemCaseSensitive(jsonData, knuckleGesture.c_str());
775 if (!cJSON_IsObject(knuckleGestureData)) {
776 MMI_HILOGE("Knuckle gesture data is not object");
777 return false;
778 }
779 cJSON *ability = cJSON_GetObjectItemCaseSensitive(knuckleGestureData, "ability");
780 if (!cJSON_IsObject(ability)) {
781 MMI_HILOGE("Ability is not object");
782 return false;
783 }
784 if (!PackageAbility(ability, launchAbility)) {
785 MMI_HILOGE("Package ability failed");
786 return false;
787 }
788 return true;
789 }
790
IsParseKnuckleGesture(const JsonParser & parser,const std::string ability,KnuckleGesture & knuckleGesture)791 bool IsParseKnuckleGesture(const JsonParser &parser, const std::string ability, KnuckleGesture &knuckleGesture)
792 {
793 cJSON *jsonData = cJSON_GetObjectItemCaseSensitive(parser.Get(), "KnuckleGesture");
794 if (!cJSON_IsObject(jsonData)) {
795 MMI_HILOGE("Knuckle gesture is not object");
796 return false;
797 }
798 if (!IsPackageKnuckleGesture(jsonData, ability, knuckleGesture.ability)) {
799 MMI_HILOGE("Package knuckle gesture failed");
800 return false;
801 }
802 return true;
803 }
804
AbsDiff(KnuckleGesture knuckleGesture,const std::shared_ptr<PointerEvent> pointerEvent)805 float AbsDiff(KnuckleGesture knuckleGesture, const std::shared_ptr<PointerEvent> pointerEvent)
806 {
807 CHKPR(pointerEvent, -1);
808 auto id = pointerEvent->GetPointerId();
809 PointerEvent::PointerItem item;
810 pointerEvent->GetPointerItem(id, item);
811 return static_cast<float>(sqrt(pow(knuckleGesture.lastDownPointer.x - item.GetDisplayX(), POW_SQUARE) +
812 pow(knuckleGesture.lastDownPointer.y - item.GetDisplayY(), POW_SQUARE)));
813 }
814
IsEqual(float f1,float f2)815 bool IsEqual(float f1, float f2)
816 {
817 return (std::fabs(f1 - f2) <= std::numeric_limits<double>::epsilon());
818 }
819
ParseMultiFingersTap(const JsonParser & parser,const std::string ability,MultiFingersTap & mulFingersTap)820 bool ParseMultiFingersTap(const JsonParser &parser, const std::string ability, MultiFingersTap &mulFingersTap)
821 {
822 cJSON *jsonData = cJSON_GetObjectItemCaseSensitive(parser.Get(), "TouchPadMultiFingersTap");
823 if (!cJSON_IsObject(jsonData)) {
824 MMI_HILOGE("Multi fingers tap is not object");
825 return false;
826 }
827 if (!IsPackageKnuckleGesture(jsonData, ability, mulFingersTap.ability)) {
828 MMI_HILOGE("Package mulFingersTap gesture failed");
829 return false;
830 }
831 return true;
832 }
833
GetProFileAbsPath(const char * fileName,char * buf,int32_t length)834 char* GetProFileAbsPath(const char* fileName, char* buf, int32_t length)
835 {
836 return ::GetOneCfgFile(fileName, buf, length);
837 }
838 } // namespace MMI
839 } // namespace OHOS