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