• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
30         : userId(userId), currentIme(std::move(currentIme)), currentSubName(std::move(currentSubName)){};
31     static constexpr int32_t INVALID_USERID = -1;
32     int32_t userId{ INVALID_USERID };
33     std::string currentIme;
34     std::string currentSubName;
35 
MarshalImePersistInfo36     bool Marshal(cJSON *node) const override
37     {
38         auto ret = SetValue(node, GET_NAME(userId), userId);
39         ret = SetValue(node, GET_NAME(currentIme), currentIme) && ret;
40         SetValue(node, GET_NAME(currentSubName), currentSubName);
41         return ret;
42     }
UnmarshalImePersistInfo43     bool Unmarshal(cJSON *node) override
44     {
45         auto ret = GetValue(node, GET_NAME(userId), userId);
46         ret = GetValue(node, GET_NAME(currentIme), currentIme) && ret;
47         GetValue(node, GET_NAME(currentSubName), currentSubName);
48         return ret;
49     }
50 };
51 
52 struct ImePersistCfg : public Serializable {
53     std::vector<ImePersistInfo> imePersistInfo;
MarshalImePersistCfg54     bool Marshal(cJSON *node) const override
55     {
56         return SetValue(node, GET_NAME(imeCfgList), imePersistInfo);
57     }
UnmarshalImePersistCfg58     bool Unmarshal(cJSON *node) override
59     {
60         return GetValue(node, GET_NAME(imeCfgList), imePersistInfo);
61     }
62 };
63 
64 struct ImeNativeCfg {
65     std::string imeId;
66     std::string bundleName;
67     std::string subName;
68     std::string extName;
69 };
70 
71 class ImeCfgManager {
72 public:
73     static ImeCfgManager &GetInstance();
74     void Init();
75     void AddImeCfg(const ImePersistInfo &cfg);
76     void ModifyImeCfg(const ImePersistInfo &cfg);
77     void DeleteImeCfg(int32_t userId);
78     std::shared_ptr<ImeNativeCfg> GetCurrentImeCfg(int32_t userId);
79 
80 private:
81     ImeCfgManager() = default;
82     ~ImeCfgManager() = default;
83     void ReadImeCfg();
84     void WriteImeCfg();
85     ImePersistInfo GetImeCfg(int32_t userId);
86     bool ParseImeCfg(const std::string &content);
87     std::string PackageImeCfg();
88     std::recursive_mutex imeCfgLock_;
89     std::vector<ImePersistInfo> imeConfigs_;
90 };
91 } // namespace MiscServices
92 } // namespace OHOS
93 #endif // SERVICES_INCLUDE_IME_CFG_MANAGER_H
94