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