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 #include "ime_cfg_manager.h"
17
18 #include <fcntl.h>
19
20 #include <algorithm>
21 #include <ios>
22 #include <string>
23
24 #include "file_operator.h"
25 #include "global.h"
26 namespace OHOS {
27 namespace MiscServices {
28 namespace {
29 constexpr const char *IME_CFG_FILE_PATH = "/data/service/el1/public/imf/ime_cfg.json";
30 } // namespace
GetInstance()31 ImeCfgManager &ImeCfgManager::GetInstance()
32 {
33 static ImeCfgManager instance;
34 return instance;
35 }
36
Init()37 void ImeCfgManager::Init()
38 {
39 ReadImeCfg();
40 }
41
ReadImeCfg()42 void ImeCfgManager::ReadImeCfg()
43 {
44 if (!FileOperator::IsExist(IME_CFG_FILE_PATH)) {
45 IMSA_HILOGD("ime cfg file not found.");
46 return;
47 }
48 std::string cfg;
49 bool ret = FileOperator::Read(IME_CFG_FILE_PATH, cfg);
50 if (!ret) {
51 IMSA_HILOGE("failed to ReadJsonFile!");
52 return;
53 }
54 ParseImeCfg(cfg);
55 }
56
WriteImeCfg()57 void ImeCfgManager::WriteImeCfg()
58 {
59 auto content = PackageImeCfg();
60 if (content.empty()) {
61 IMSA_HILOGE("failed to Package imeCfg!");
62 return;
63 }
64 if (!FileOperator::Write(IME_CFG_FILE_PATH, content, O_CREAT | O_WRONLY | O_SYNC | O_TRUNC)) {
65 IMSA_HILOGE("failed to WriteJsonFile!");
66 }
67 }
68
ParseImeCfg(const std::string & content)69 bool ImeCfgManager::ParseImeCfg(const std::string &content)
70 {
71 IMSA_HILOGD("content: %{public}s", content.c_str());
72 ImePersistCfg cfg;
73 auto ret = cfg.Unmarshall(content);
74 if (!ret) {
75 IMSA_HILOGE("Unmarshall failed!");
76 return false;
77 }
78 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
79 imeConfigs_ = cfg.imePersistInfo;
80 return true;
81 }
82
PackageImeCfg()83 std::string ImeCfgManager::PackageImeCfg()
84 {
85 ImePersistCfg cfg;
86 {
87 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
88 cfg.imePersistInfo = imeConfigs_;
89 }
90 std::string content;
91 auto ret = cfg.Marshall(content);
92 IMSA_HILOGD("ret: %{public}d, content: %{public}s, size: %{public}zu", ret, content.c_str(),
93 cfg.imePersistInfo.size());
94 return content;
95 }
96
AddImeCfg(const ImePersistInfo & cfg)97 void ImeCfgManager::AddImeCfg(const ImePersistInfo &cfg)
98 {
99 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
100 imeConfigs_.push_back(cfg);
101 WriteImeCfg();
102 }
103
ModifyImeCfg(const ImePersistInfo & cfg)104 void ImeCfgManager::ModifyImeCfg(const ImePersistInfo &cfg)
105 {
106 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
107 auto it = std::find_if(imeConfigs_.begin(), imeConfigs_.end(),
108 [&cfg](const ImePersistInfo &imeCfg) { return imeCfg.userId == cfg.userId && !cfg.currentIme.empty(); });
109 if (it != imeConfigs_.end()) {
110 ImePersistInfo imePersistInfo;
111 imePersistInfo.userId = cfg.userId;
112 imePersistInfo.currentIme = it->tempScreenLockIme.empty() ? cfg.currentIme : it->currentIme;
113 imePersistInfo.currentSubName = it->tempScreenLockIme.empty() ? cfg.currentSubName : it->currentSubName;
114 imePersistInfo.tempScreenLockIme = it->tempScreenLockIme;
115 imePersistInfo.isDefaultImeSet = it->isDefaultImeSet ? true : cfg.isDefaultImeSet;
116 *it = imePersistInfo;
117 }
118 WriteImeCfg();
119 }
120
ModifyTempScreenLockImeCfg(int32_t userId,const std::string & ime)121 void ImeCfgManager::ModifyTempScreenLockImeCfg(int32_t userId, const std::string &ime)
122 {
123 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
124 auto it = std::find_if(imeConfigs_.begin(), imeConfigs_.end(),
125 [userId, &ime](const ImePersistInfo &imeCfg) { return imeCfg.userId == userId; });
126 if (it != imeConfigs_.end()) {
127 it->tempScreenLockIme = ime;
128 }
129 WriteImeCfg();
130 }
131
DeleteImeCfg(int32_t userId)132 void ImeCfgManager::DeleteImeCfg(int32_t userId)
133 {
134 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
135 for (auto iter = imeConfigs_.begin(); iter != imeConfigs_.end(); iter++) {
136 if (iter->userId == userId) {
137 imeConfigs_.erase(iter);
138 break;
139 }
140 }
141 WriteImeCfg();
142 }
143
GetImeCfg(int32_t userId)144 ImePersistInfo ImeCfgManager::GetImeCfg(int32_t userId)
145 {
146 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
147 auto it = std::find_if(
148 imeConfigs_.begin(), imeConfigs_.end(), [userId](const ImePersistInfo &cfg) { return cfg.userId == userId; });
149 if (it != imeConfigs_.end()) {
150 return *it;
151 }
152 return {};
153 }
154
GetCurrentImeCfg(int32_t userId)155 std::shared_ptr<ImeNativeCfg> ImeCfgManager::GetCurrentImeCfg(int32_t userId)
156 {
157 auto cfg = GetImeCfg(userId);
158 ImeNativeCfg info;
159 if (!cfg.tempScreenLockIme.empty()) {
160 info.imeId = cfg.tempScreenLockIme;
161 } else {
162 info.subName = cfg.currentSubName;
163 info.imeId = cfg.currentIme;
164 }
165 auto pos = info.imeId.find('/');
166 if (pos != std::string::npos && pos + 1 < info.imeId.size()) {
167 info.bundleName = info.imeId.substr(0, pos);
168 info.extName = info.imeId.substr(pos + 1);
169 }
170 return std::make_shared<ImeNativeCfg>(info);
171 }
172
IsDefaultImeSet(int32_t userId)173 bool ImeCfgManager::IsDefaultImeSet(int32_t userId)
174 {
175 IMSA_HILOGI("ImeCfgManager::IsDefaultImeSet enter.");
176 auto cfg = GetImeCfg(userId);
177 IMSA_HILOGI("isDefaultImeSet: %{public}d", cfg.isDefaultImeSet);
178 return cfg.isDefaultImeSet;
179 }
180 } // namespace MiscServices
181 } // namespace OHOS