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