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