1 /* 2 * Copyright (c) 2021-2022 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_MANAGER_H 17 #define KEY_COMMAND_MANAGER_H 18 19 #include <fstream> 20 #include <map> 21 #include <condition_variable> 22 #include <thread> 23 #include <chrono> 24 #include <functional> 25 #include <mutex> 26 #include <vector> 27 #include <set> 28 29 #include "nlohmann/json.hpp" 30 #include "nocopyable.h" 31 #include "struct_multimodal.h" 32 #include "key_event.h" 33 #include "i_key_command_manager.h" 34 35 namespace OHOS { 36 namespace MMI { 37 using json = nlohmann::json; 38 struct Ability { 39 std::string bundleName; 40 std::string abilityName; 41 std::string action; 42 std::string type; 43 std::string deviceId; 44 std::string uri; 45 std::vector<std::string> entities; 46 std::map<std::string, std::string> params; 47 }; 48 49 struct ShortcutKey { 50 std::set<int32_t> preKeys; 51 int32_t finalKey { -1 }; 52 int32_t keyDownDuration { 0 }; 53 int32_t triggerType { KeyEvent::KEY_ACTION_DOWN }; 54 int32_t timerId { -1 }; 55 Ability ability; 56 }; 57 58 class KeyCommandManager : public IKeyCommandManager { 59 public: 60 KeyCommandManager(); 61 DISALLOW_COPY_AND_MOVE(KeyCommandManager); 62 ~KeyCommandManager() = default; 63 bool HandlerEvent(const std::shared_ptr<KeyEvent> event); 64 private: 65 void ResolveConfig(std::string configFile); 66 bool ConvertToShortcutKey(const json &jsonData, ShortcutKey &shortcutKey); 67 std::string GetConfigFilePath(); 68 void LaunchAbility(ShortcutKey key); 69 std::string GenerateKey(const ShortcutKey& key); 70 bool PackageAbility(const json &jsonStr, Ability &ability); 71 void Print(); 72 bool IsKeyMatch(const ShortcutKey &shortcutKey, const std::shared_ptr<KeyEvent> &key); 73 bool HandleKeyUp(const std::shared_ptr<KeyEvent> &keyEvent, const ShortcutKey &shortcutKey); 74 bool HandleKeyDown(ShortcutKey &shortcutKey); 75 bool HandleKeyCancel(ShortcutKey &shortcutKey); 76 void ResetLastMatchedKey(); 77 ShortcutKey lastMatchedKey_; 78 std::map<std::string, ShortcutKey> shortcutKeys_; 79 }; 80 } // namespace MMI 81 } // namespace OHOS 82 #endif // KEY_COMMAND_MANAGER_H