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 "input_method_status.h" 20 #include "settings_data_utils.h" 21 22 namespace OHOS { 23 namespace MiscServices { 24 struct SwitchInfo { 25 std::chrono::system_clock::time_point timestamp{}; 26 std::string bundleName; 27 std::string subName; 28 bool operator==(const SwitchInfo &info) const 29 { 30 return (timestamp == info.timestamp && bundleName == info.bundleName && subName == info.subName); 31 } 32 }; 33 34 struct EnableKeyBoardCfg : public Serializable { 35 UserImeConfig userImeCfg; UnmarshalEnableKeyBoardCfg36 bool Unmarshal(cJSON *node) override 37 { 38 GetValue(node, GET_NAME(enableKeyboardList), userImeCfg); 39 return true; 40 } 41 }; 42 43 struct EnableImeCfg : public Serializable { 44 UserImeConfig userImeCfg; UnmarshalEnableImeCfg45 bool Unmarshal(cJSON *node) override 46 { 47 GetValue(node, GET_NAME(enableImeList), userImeCfg); 48 return true; 49 } MarshalEnableImeCfg50 bool Marshal(cJSON *node) const override 51 { 52 SetValue(node, GET_NAME(enableImeList), userImeCfg); 53 return true; 54 } 55 }; 56 57 struct TempImeCfg : public Serializable { 58 UserImeConfig tempImeList; UnmarshalTempImeCfg59 bool Unmarshal(cJSON *node) override 60 { 61 GetValue(node, GET_NAME(tempImeList), tempImeList); 62 return true; 63 } 64 }; 65 66 class EnableImeDataParser : public RefBase { 67 public: 68 static sptr<EnableImeDataParser> GetInstance(); 69 int32_t Initialize(const int32_t userId); 70 int32_t GetEnableData(const std::string &key, std::vector<std::string> &enableVec, const int32_t userId); 71 int32_t GetEnableIme(int32_t userId, std::vector<std::string> &enableVec); 72 // for enable list changed 73 bool CheckNeedSwitch(const std::string &key, SwitchInfo &switchInfo, const int32_t userId); 74 // for switch target ime 75 bool CheckNeedSwitch(const SwitchInfo &info, const int32_t userId); 76 void OnUserChanged(const int32_t userId); 77 void OnConfigChanged(int32_t userId, const std::string &key); 78 void OnPackageAdded(int32_t userId, const std::string &bundleName); 79 int32_t GetImeEnablePattern(int32_t userId, const std::string &bundleName, EnabledStatus &status); 80 static constexpr const char *ENABLE_IME = "settings.inputmethod.enable_ime"; 81 static constexpr const char *ENABLE_KEYBOARD = "settings.inputmethod.enable_keyboard"; 82 static constexpr const char *TEMP_IME = "settings.inputmethod.temp_ime"; 83 84 private: 85 EnableImeDataParser() = default; 86 ~EnableImeDataParser(); 87 int32_t UpdateEnableData(int32_t userId, const std::string &key); 88 void CoverGlobalEnableTable(const std::string &valueStr); 89 std::string GetUserEnableTable(int32_t userId); 90 std::string GetEanbleIme(int32_t userId, const std::string &globalStr); 91 std::string GetGlobalTableUserId(const std::string &valueStr); 92 int32_t GetEnableImeFromCache(std::vector<std::string> &enableVec); 93 bool ParseEnableIme(const std::string &valueStr, int32_t userId, std::vector<std::string> &enableVec); 94 bool ParseEnableKeyboard(const std::string &valueStr, int32_t userId, std::vector<std::string> &enableVec); 95 bool ParseTempIme(const std::string &valueStr, int32_t userId, std::vector<std::string> &tempVector); 96 bool CheckTargetEnableName( 97 const std::string &key, const std::string &targetName, std::string &nextIme, const int32_t userId); 98 std::shared_ptr<Property> GetDefaultIme(); 99 void OnBackgroundPackageAdded(int32_t userId, const std::string &bundleName, const std::string &globalContent); 100 void OnForegroundPackageAdded(int32_t userId, const std::string &bundleName, const std::string &globalContent); 101 int32_t AddToUserEnableTable(int32_t userId, const std::string &bundleName, std::string &userContent); 102 int32_t AddToGlobalEnableTable(int32_t userId, const std::string &bundleName, std::string &globalContent); 103 int32_t AddToEnableTable( 104 int32_t userId, const std::string &bundleName, const std::string &uriProxy, std::string &tableContent); 105 int32_t CoverUserEnableTable(int32_t userId, const std::string &userContent); 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 std::mutex userIdLock_; 117 }; 118 } // namespace MiscServices 119 } // namespace OHOS 120 121 #endif // ENABLE_IME_DATA_PARSER_H 122