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 #ifndef KEY_COMMAND_HANDLER_UTIL_H
17 #define KEY_COMMAND_HANDLER_UTIL_H
18 
19 #include "cJSON.h"
20 #include "config_policy_utils.h"
21 #include "file_ex.h"
22 #include "system_ability_definition.h"
23 
24 #include "ability_manager_client.h"
25 #include "bytrace_adapter.h"
26 #include "define_multimodal.h"
27 #include "dfx_hisysevent.h"
28 #include "display_event_monitor.h"
29 #include "error_multimodal.h"
30 #include "input_event_data_transformation.h"
31 #include "input_event_handler.h"
32 #include "i_preference_manager.h"
33 #include "key_command_handler.h"
34 #include "mmi_log.h"
35 #include "nap_process.h"
36 #include "net_packet.h"
37 #include "proto.h"
38 #include "setting_datashare.h"
39 #include "timer_manager.h"
40 #include "util_ex.h"
41 
42 #undef MMI_LOG_TAG
43 #define MMI_LOG_TAG "KeyCommandHandlerUtil"
44 
45 namespace OHOS {
46 namespace MMI {
47 constexpr int32_t MAX_PREKEYS_NUM = 4;
48 constexpr int32_t MAX_SEQUENCEKEYS_NUM = 10;
49 constexpr int64_t MAX_DELAY_TIME = 1000000;
50 constexpr int64_t SECONDS_SYSTEM = 1000;
51 constexpr int32_t SPECIAL_KEY_DOWN_DELAY = 150;
52 constexpr int32_t MAX_SHORT_KEY_DOWN_DURATION = 4000;
53 constexpr int32_t MIN_SHORT_KEY_DOWN_DURATION = 0;
54 constexpr int32_t TOUCH_MAX_THRESHOLD = 20;
55 constexpr int32_t TWO_FINGERS_DISTANCE_LIMIT = 16;
56 constexpr int32_t TWO_FINGERS_TIME_LIMIT = 150000;
57 constexpr int32_t SIMULATE_POINTER_ID = 10000;
58 constexpr int32_t TOUCH_LIFT_LIMIT = 24;
59 constexpr int32_t TOUCH_RIGHT_LIMIT = 24;
60 constexpr int32_t TOUCH_TOP_LIMIT = 80;
61 constexpr int32_t TOUCH_BOTTOM_LIMIT = 41;
62 constexpr int32_t PARAMETER_ERROR = 401;
63 constexpr int32_t KNUCKLE_KNOCKS = 1;
64 constexpr size_t SINGLE_KNUCKLE_SIZE = 1;
65 constexpr size_t DOUBLE_KNUCKLE_SIZE = 2;
66 constexpr int32_t MAX_TIME_FOR_ADJUST_CONFIG = 5;
67 constexpr int32_t POW_SQUARE = 2;
68 constexpr int64_t DOUBLE_CLICK_INTERVAL_TIME_DEFAULT = 250000;
69 constexpr int64_t DOUBLE_CLICK_INTERVAL_TIME_SLOW = 450000;
70 constexpr float DOUBLE_CLICK_DISTANCE_DEFAULT_CONFIG = 64.0f;
71 constexpr float DOUBLE_CLICK_DISTANCE_LONG_CONFIG = 96.0f;
72 constexpr float VPR_CONFIG = 3.25f;
73 constexpr int32_t REMOVE_OBSERVER = -2;
74 constexpr int32_t ACTIVE_EVENT = 2;
75 constexpr int32_t LONG_ABILITY_START_DELAY = 2000;
76 constexpr int32_t WINDOW_INPUT_METHOD_TYPE = 2105;
77 const std::string EXTENSION_ABILITY = "extensionAbility";
78 const std::string SINGLE_KNUCKLE_ABILITY = "SingleKnuckleDoubleClickGesture";
79 const std::string DOUBLE_KNUCKLE_ABILITY = "DoubleKnuckleDoubleClickGesture";
80 const std::string TOUCHPAD_TRIP_TAP_ABILITY = "ThreeFingersTap";
81 const std::string SETTING_KNUCKLE_SWITCH = "settings.game.forbid_finger_knuckle";
82 
83 enum SpecialType {
84     SPECIAL_ALL = 0,
85     SUBSCRIBER_BEFORE_DELAY = 1,
86     KEY_DOWN_ACTION = 2
87 };
88 const std::map<int32_t, SpecialType> SPECIAL_KEYS = {
89     { KeyEvent::KEYCODE_POWER, SpecialType::KEY_DOWN_ACTION },
90     { KeyEvent::KEYCODE_VOLUME_DOWN, SpecialType::KEY_DOWN_ACTION },
91     { KeyEvent::KEYCODE_VOLUME_UP, SpecialType::KEY_DOWN_ACTION }
92 };
93 struct JsonParser {
94     JsonParser() = default;
~JsonParserJsonParser95     ~JsonParser()
96     {
97         if (json_ != nullptr) {
98             cJSON_Delete(json_);
99         }
100     }
101     operator cJSON *()
102     {
103         return json_;
104     }
105     cJSON *json_ { nullptr };
106 };
107 
108 bool IsSpecialType(int32_t keyCode, SpecialType type);
109 bool GetBusinessId(const cJSON* jsonData, std::string &businessIdValue, std::vector<std::string> &businessIds);
110 bool GetPreKeys(const cJSON* jsonData, ShortcutKey &shortcutKey);
111 bool GetTrigger(const cJSON* jsonData, int32_t &triggerType);
112 bool GetKeyDownDuration(const cJSON* jsonData, int32_t &keyDownDurationInt);
113 bool GetKeyFinalKey(const cJSON* jsonData, int32_t &finalKeyInt);
114 void GetKeyVal(const cJSON* json, const std::string &key, std::string &value);
115 bool GetEntities(const cJSON* jsonAbility, Ability &ability);
116 bool GetParams(const cJSON* jsonAbility, Ability &ability);
117 bool PackageAbility(const cJSON* jsonAbility, Ability &ability);
118 bool ConvertToShortcutKey(const cJSON* jsonData, ShortcutKey &shortcutKey, std::vector<std::string> &businessIds);
119 bool GetKeyCode(const cJSON* jsonData, int32_t &keyCodeInt);
120 bool GetKeyAction(const cJSON* jsonData, int32_t &keyActionInt);
121 bool GetDelay(const cJSON* jsonData, int64_t &delayInt);
122 bool GetRepeatTimes(const cJSON* jsonData, int32_t &repeatTimesInt);
123 bool GetAbilityStartDelay(const cJSON* jsonData, int64_t &abilityStartDelayInt);
124 bool PackageSequenceKey(const cJSON* sequenceKeysJson, SequenceKey &sequenceKey);
125 bool GetSequenceKeys(const cJSON* jsonData, Sequence &sequence);
126 bool IsSequenceKeysValid(const Sequence &sequence);
127 bool ConvertToKeySequence(const cJSON* jsonData, Sequence &sequence);
128 bool ConvertToExcludeKey(const cJSON* jsonData, ExcludeKey &exKey);
129 bool ConvertToKeyRepeat(const cJSON* jsonData, RepeatKey &repeatKey);
130 std::string GenerateKey(const ShortcutKey& key);
131 bool ParseShortcutKeys(const JsonParser& parser, std::map<std::string, ShortcutKey>& shortcutKeyMap,
132     std::vector<std::string>& businessIds);
133 bool ParseSequences(const JsonParser& parser, std::vector<Sequence>& sequenceVec);
134 bool ParseExcludeKeys(const JsonParser& parser, std::vector<ExcludeKey>& excludeKeyVec);
135 bool ParseRepeatKeys(const JsonParser& parser, std::vector<RepeatKey>& repeatKeyVec,
136     std::map<int32_t, int32_t>& repeatKeyMaxTimes);
137 bool ParseTwoFingerGesture(const JsonParser& parser, TwoFingerGesture& gesture);
138 bool IsPackageKnuckleGesture(const cJSON* jsonData, const std::string knuckleGesture, Ability &launchAbility);
139 bool IsParseKnuckleGesture(const JsonParser &parser, const std::string ability, KnuckleGesture &knuckleGesture);
140 float AbsDiff(KnuckleGesture knuckleGesture, const std::shared_ptr<PointerEvent> pointerEvent);
141 bool IsEqual(float f1, float f2);
142 bool ParseMultiFingersTap(const JsonParser &parser, const std::string ability, MultiFingersTap &mulFingersTap);
143 } // namespace MMI
144 } // namespace OHOS
145 #endif // KEY_COMMAND_HANDLER_UTIL_H