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