1 /*
2  * Copyright (c) 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 SERVICES_INCLUDE_IME_CFG_MANAGER_H
17 #define SERVICES_INCLUDE_IME_CFG_MANAGER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "serializable.h"
25 namespace OHOS {
26 namespace MiscServices {
27 struct ImePersistInfo : public Serializable {
28     ImePersistInfo() = default;
ImePersistInfoImePersistInfo29     ImePersistInfo(int32_t userId, std::string currentIme, std::string currentSubName, bool isDefaultImeSet)
30         : userId(userId), currentIme(std::move(currentIme)), currentSubName(std::move(currentSubName)),
31         isDefaultImeSet(isDefaultImeSet){};
32     static constexpr int32_t INVALID_USERID = -1;
33     int32_t userId{ INVALID_USERID };
34     std::string currentIme;
35     std::string currentSubName;
36     std::string tempScreenLockIme;
37     bool isDefaultImeSet{ false };
38 
MarshalImePersistInfo39     bool Marshal(cJSON *node) const override
40     {
41         auto ret = SetValue(node, GET_NAME(userId), userId);
42         ret = SetValue(node, GET_NAME(currentIme), currentIme) && ret;
43         ret = SetValue(node, GET_NAME(currentSubName), currentSubName) && ret;
44         SetValue(node, GET_NAME(tempScreenLockIme), tempScreenLockIme);
45         ret = SetValue(node, GET_NAME(isDefaultImeSet), isDefaultImeSet) && ret;
46         return ret;
47     }
UnmarshalImePersistInfo48     bool Unmarshal(cJSON *node) override
49     {
50         auto ret = GetValue(node, GET_NAME(userId), userId);
51         ret = GetValue(node, GET_NAME(currentIme), currentIme) && ret;
52         ret = GetValue(node, GET_NAME(currentSubName), currentSubName) && ret;
53         GetValue(node, GET_NAME(tempScreenLockIme), tempScreenLockIme);
54         ret = GetValue(node, GET_NAME(isDefaultImeSet), isDefaultImeSet) && ret;
55         return ret;
56     }
57 };
58 
59 struct ImePersistCfg : public Serializable {
60     std::vector<ImePersistInfo> imePersistInfo;
MarshalImePersistCfg61     bool Marshal(cJSON *node) const override
62     {
63         return SetValue(node, GET_NAME(imeCfgList), imePersistInfo);
64     }
UnmarshalImePersistCfg65     bool Unmarshal(cJSON *node) override
66     {
67         return GetValue(node, GET_NAME(imeCfgList), imePersistInfo);
68     }
69 };
70 
71 struct ImeNativeCfg {
72     std::string imeId;
73     std::string bundleName;
74     std::string subName;
75     std::string extName;
76 };
77 
78 class ImeCfgManager {
79 public:
80     static ImeCfgManager &GetInstance();
81     void Init();
82     void AddImeCfg(const ImePersistInfo &cfg);
83     void ModifyImeCfg(const ImePersistInfo &cfg);
84     void ModifyTempScreenLockImeCfg(int32_t userId, const std::string &ime);
85     void DeleteImeCfg(int32_t userId);
86     std::shared_ptr<ImeNativeCfg> GetCurrentImeCfg(int32_t userId);
87     bool IsDefaultImeSet(int32_t userId);
88 
89 private:
90     ImeCfgManager() = default;
91     ~ImeCfgManager() = default;
92     void ReadImeCfg();
93     void WriteImeCfg();
94     ImePersistInfo GetImeCfg(int32_t userId);
95     bool ParseImeCfg(const std::string &content);
96     std::string PackageImeCfg();
97     std::recursive_mutex imeCfgLock_;
98     std::vector<ImePersistInfo> imeConfigs_;
99 };
100 } // namespace MiscServices
101 } // namespace OHOS
102 #endif // SERVICES_INCLUDE_IME_CFG_MANAGER_H
103