1 /* 2 * Copyright (c) 2023 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 ENABLE_IME_DATA_PARSER_H 17 #define ENABLE_IME_DATA_PARSER_H 18 19 #include <map> 20 #include <mutex> 21 #include <string> 22 #include <unordered_map> 23 #include <utility> 24 #include <vector> 25 26 #include "datashare_helper.h" 27 #include "global.h" 28 #include "input_method_property.h" 29 #include "input_method_status.h" 30 #include "serializable.h" 31 #include "settings_data_utils.h" 32 33 namespace OHOS { 34 namespace MiscServices { 35 struct SwitchInfo { 36 std::chrono::system_clock::time_point timestamp{}; 37 std::string bundleName; 38 std::string subName; 39 bool operator==(const SwitchInfo &info) const 40 { 41 return (timestamp == info.timestamp && bundleName == info.bundleName && subName == info.subName); 42 } 43 }; 44 45 struct EnableKeyBoardCfg : public Serializable { 46 UserImeConfig userImeCfg; UnmarshalEnableKeyBoardCfg47 bool Unmarshal(cJSON *node) override 48 { 49 GetValue(node, GET_NAME(enableKeyboardList), userImeCfg); 50 return true; 51 } 52 }; 53 54 struct EnableImeCfg : public Serializable { 55 UserImeConfig userImeCfg; UnmarshalEnableImeCfg56 bool Unmarshal(cJSON *node) override 57 { 58 GetValue(node, GET_NAME(enableImeList), userImeCfg); 59 return true; 60 } 61 }; 62 63 struct TempImeCfg : public Serializable { 64 UserImeConfig tempImeList; UnmarshalTempImeCfg65 bool Unmarshal(cJSON *node) override 66 { 67 GetValue(node, GET_NAME(tempImeList), tempImeList); 68 return true; 69 } 70 }; 71 72 class EnableImeDataParser : public RefBase { 73 public: 74 static sptr<EnableImeDataParser> GetInstance(); 75 int32_t Initialize(const int32_t userId); 76 int32_t GetEnableData(const std::string &key, std::vector<std::string> &enableVec, const int32_t userId); 77 int32_t GetEnableIme(int32_t userId, std::vector<std::string> &enableVec); 78 // for enable list changed 79 bool CheckNeedSwitch(const std::string &key, SwitchInfo &switchInfo, const int32_t userId); 80 // for switch target ime 81 bool CheckNeedSwitch(const SwitchInfo &info, const int32_t userId); 82 void OnUserChanged(const int32_t userId); 83 void OnConfigChanged(int32_t userId, const std::string &key); 84 int32_t GetImeEnablePattern(int32_t userId, const std::string &bundleName, EnabledStatus &status); 85 86 static constexpr const char *ENABLE_IME = "settings.inputmethod.enable_ime"; 87 static constexpr const char *ENABLE_KEYBOARD = "settings.inputmethod.enable_keyboard"; 88 static constexpr const char *TEMP_IME = "settings.inputmethod.temp_ime"; 89 90 private: 91 EnableImeDataParser() = default; 92 ~EnableImeDataParser(); 93 94 int32_t UpdateEnableData(int32_t userId, const std::string &key); 95 void CoverGlobalTable(const std::string &key, std::string &valueStr); 96 std::string GetUserEnableTable(int32_t userId, const std::string &key); 97 std::string GetEanbleIme(int32_t userId, const std::string &key, const std::string &globalStr); 98 std::string GetGlobalTableUserId(const std::string &valueStr); 99 int32_t GetEnableImeFromCache(std::vector<std::string> &enableVec); 100 bool ParseEnableIme(const std::string &valueStr, int32_t userId, std::vector<std::string> &enableVec); 101 bool ParseEnableKeyboard(const std::string &valueStr, int32_t userId, std::vector<std::string> &enableVec); 102 bool ParseTempIme(const std::string &valueStr, int32_t userId, std::vector<std::string> &tempVector); 103 bool CheckTargetEnableName( 104 const std::string &key, const std::string &targetName, std::string &nextIme, const int32_t userId); 105 std::shared_ptr<Property> GetDefaultIme(); 106 107 private: 108 static std::mutex instanceMutex_; 109 static sptr<EnableImeDataParser> instance_; 110 std::mutex listMutex_; 111 std::unordered_map<std::string, std::vector<std::string>> enableList_; 112 std::mutex defaultImeMutex_; 113 std::shared_ptr<Property> defaultImeInfo_{ nullptr }; 114 int32_t currentUserId_ = 0; 115 bool isEnableImeInit_{ false }; 116 }; 117 } // namespace MiscServices 118 } // namespace OHOS 119 120 #endif // ENABLE_IME_DATA_PARSER_H 121