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