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 #include "enable_ime_data_parser.h"
17
18 #include <algorithm>
19
20 #include "ime_info_inquirer.h"
21
22 namespace OHOS {
23 namespace MiscServices {
24 std::mutex EnableImeDataParser::instanceMutex_;
25 sptr<EnableImeDataParser> EnableImeDataParser::instance_ = nullptr;
~EnableImeDataParser()26 EnableImeDataParser::~EnableImeDataParser()
27 {
28 }
29
GetInstance()30 sptr<EnableImeDataParser> EnableImeDataParser::GetInstance()
31 {
32 if (instance_ == nullptr) {
33 std::lock_guard<std::mutex> autoLock(instanceMutex_);
34 if (instance_ == nullptr) {
35 IMSA_HILOGI("need to create instance.");
36 instance_ = new (std::nothrow) EnableImeDataParser();
37 if (instance_ == nullptr) {
38 IMSA_HILOGE("instance is nullptr!");
39 return instance_;
40 }
41 }
42 }
43 return instance_;
44 }
45
Initialize(const int32_t userId)46 int32_t EnableImeDataParser::Initialize(const int32_t userId)
47 {
48 currentUserId_ = userId;
49 {
50 std::lock_guard<std::mutex> autoLock(listMutex_);
51 enableList_.insert({ std::string(ENABLE_IME), {} });
52 enableList_.insert({ std::string(ENABLE_KEYBOARD), {} });
53 }
54 UpdateEnableData(userId, ENABLE_IME);
55 UpdateEnableData(userId, ENABLE_KEYBOARD);
56 GetDefaultIme();
57 return ErrorCode::NO_ERROR;
58 }
59
OnUserChanged(const int32_t targetUserId)60 void EnableImeDataParser::OnUserChanged(const int32_t targetUserId)
61 {
62 std::lock_guard<std::mutex> lock(userIdLock_);
63 IMSA_HILOGI("run in %{public}d}.", targetUserId);
64 currentUserId_ = targetUserId;
65 UpdateEnableData(targetUserId, ENABLE_IME);
66 UpdateEnableData(targetUserId, ENABLE_KEYBOARD);
67 }
68
CheckNeedSwitch(const std::string & key,SwitchInfo & switchInfo,const int32_t userId)69 bool EnableImeDataParser::CheckNeedSwitch(const std::string &key, SwitchInfo &switchInfo, const int32_t userId)
70 {
71 IMSA_HILOGD("start, data changed.");
72 auto currentIme = ImeInfoInquirer::GetInstance().GetCurrentInputMethod(userId);
73 auto defaultIme = GetDefaultIme();
74 if (defaultIme == nullptr) {
75 IMSA_HILOGE("defaultIme is nullptr!");
76 return false;
77 }
78 switchInfo.bundleName = defaultIme->name;
79 switchInfo.subName = "";
80 if (currentIme == nullptr) {
81 IMSA_HILOGE("currentIme is nullptr!");
82 return true;
83 }
84 if (key == std::string(ENABLE_IME)) {
85 if (currentIme->name == defaultIme->name) {
86 std::lock_guard<std::mutex> autoLock(listMutex_);
87 GetEnableData(key, enableList_[key], userId);
88 IMSA_HILOGD("current ime is default, do not need switch ime.");
89 return false;
90 }
91 return CheckTargetEnableName(key, currentIme->name, switchInfo.bundleName, userId);
92 } else if (key == std::string(ENABLE_KEYBOARD)) {
93 if (currentIme->name != defaultIme->name || currentIme->id == defaultIme->id) {
94 IMSA_HILOGD("current ime is not default or id is default.");
95 std::lock_guard<std::mutex> autoLock(listMutex_);
96 GetEnableData(key, enableList_[key], userId);
97 return false;
98 }
99 switchInfo.subName = defaultIme->id;
100 return CheckTargetEnableName(key, currentIme->id, switchInfo.subName, userId);
101 }
102 IMSA_HILOGW("invalid key: %{public}s.", key.c_str());
103 return false;
104 }
105
CheckNeedSwitch(const SwitchInfo & info,const int32_t userId)106 bool EnableImeDataParser::CheckNeedSwitch(const SwitchInfo &info, const int32_t userId)
107 {
108 IMSA_HILOGD("current userId: %{public}d, target userId: %{public}d, check bundleName: %{public}s", currentUserId_,
109 userId, info.bundleName.c_str());
110 if (info.bundleName == GetDefaultIme()->name) {
111 IMSA_HILOGD("default ime, permit to switch");
112 return true;
113 }
114 IMSA_HILOGD("check ime.");
115 std::vector<std::string> tempVec;
116 int32_t tempRet = GetEnableData(TEMP_IME, tempVec, userId);
117 if (tempRet != ErrorCode::NO_ERROR || tempVec.empty()) {
118 IMSA_HILOGD("get tempVec list failed, or tempVec list is empty.");
119 } else {
120 auto iter = std::find_if(
121 tempVec.begin(), tempVec.end(), [&info](const std::string &ime) { return info.bundleName == ime; });
122 if (iter != tempVec.end()) {
123 IMSA_HILOGD("In tempVec list.");
124 return true;
125 }
126 }
127 std::vector<std::string> enableVec;
128 int32_t ret = GetEnableData(ENABLE_IME, enableVec, userId);
129 if (ret != ErrorCode::NO_ERROR || enableVec.empty()) {
130 IMSA_HILOGD("get enable list failed, or enable list is empty.");
131 return false;
132 }
133
134 auto iter = std::find_if(enableVec.begin(), enableVec.end(),
135 [&info](const std::string &ime) { return info.bundleName == ime; });
136 if (iter != enableVec.end()) {
137 IMSA_HILOGD("in enable list.");
138 return true;
139 }
140 return false;
141 }
142
CheckTargetEnableName(const std::string & key,const std::string & targetName,std::string & nextIme,const int32_t userId)143 bool EnableImeDataParser::CheckTargetEnableName(const std::string &key, const std::string &targetName,
144 std::string &nextIme, const int32_t userId)
145 {
146 IMSA_HILOGD("start.");
147 std::vector<std::string> enableVec;
148 int32_t ret = GetEnableData(key, enableVec, userId);
149 if (ret != ErrorCode::NO_ERROR) {
150 IMSA_HILOGE("get enable list abnormal.");
151 return false;
152 }
153
154 if (enableVec.empty()) {
155 IMSA_HILOGE("enable empty, switch default ime.");
156 return true;
157 }
158 std::lock_guard<std::mutex> autoLock(listMutex_);
159 auto iter = std::find_if(enableVec.begin(), enableVec.end(),
160 [&targetName](const std::string &ime) { return ime == targetName; });
161 if (iter != enableVec.end()) {
162 IMSA_HILOGD("enable list has current ime, do not need switch.");
163 enableList_[key].assign(enableVec.begin(), enableVec.end());
164 return false;
165 }
166
167 auto it = std::find_if(enableList_[key].begin(), enableList_[key].end(),
168 [&targetName](const std::string &ime) { return ime == targetName; });
169 if (it == enableList_[key].end()) {
170 enableList_[key].assign(enableVec.begin(), enableVec.end());
171 return true;
172 }
173
174 std::rotate(enableList_[key].begin(), it, enableList_[key].end());
175 auto result =
176 std::find_first_of(enableList_[key].begin(), enableList_[key].end(), enableVec.begin(), enableVec.end());
177 if (result != enableList_[key].end()) {
178 IMSA_HILOGD("found the next cached ime in enable ime list.");
179 nextIme = *result;
180 }
181 enableList_[key].assign(enableVec.begin(), enableVec.end());
182 return true;
183 }
184
CoverGlobalEnableTable(const std::string & valueStr)185 void EnableImeDataParser::CoverGlobalEnableTable(const std::string &valueStr)
186 {
187 SettingsDataUtils::GetInstance()->SetStringValue(SETTING_URI_PROXY, ENABLE_IME, valueStr);
188 }
189
GetUserEnableTable(int32_t userId)190 std::string EnableImeDataParser::GetUserEnableTable(int32_t userId)
191 {
192 std::string valueStr;
193 int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(
194 SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true", ENABLE_IME, valueStr);
195 IMSA_HILOGI("get user enable table, userId = %{public}d, ret = %{public}d, valurStr = %{public}s",
196 userId, ret, valueStr.c_str());
197 if (valueStr.empty()) {
198 auto defaultIme = GetDefaultIme();
199 if (defaultIme != nullptr) {
200 valueStr =
201 "{\"enableImeList\" : {\"" + std::to_string(userId) + "\" : [\"" + defaultIme->name + "\"]}}";
202 }
203 }
204 return valueStr;
205 }
206
GetEanbleIme(int32_t userId,const std::string & globalStr)207 std::string EnableImeDataParser::GetEanbleIme(int32_t userId, const std::string &globalStr)
208 {
209 std::string enableStr = globalStr;
210 std::string globaleUserId;
211 if (enableStr.empty()) {
212 enableStr = GetUserEnableTable(currentUserId_);
213 CoverGlobalEnableTable(enableStr);
214 globaleUserId = std::to_string(currentUserId_);
215 } else {
216 globaleUserId = GetGlobalTableUserId(enableStr);
217 }
218 SettingsDataUtils::GetInstance()->SetStringValue(SETTINGS_USER_DATA_URI + globaleUserId + "?Proxy=true",
219 ENABLE_IME, enableStr);
220 if (globaleUserId != std::to_string(currentUserId_)) {
221 enableStr = GetUserEnableTable(currentUserId_);
222 CoverGlobalEnableTable(enableStr);
223 }
224 if (currentUserId_ != userId) {
225 enableStr = GetUserEnableTable(userId);
226 }
227 return enableStr;
228 }
229
GetEnableData(const std::string & key,std::vector<std::string> & enableVec,const int32_t userId)230 int32_t EnableImeDataParser::GetEnableData(
231 const std::string &key, std::vector<std::string> &enableVec, const int32_t userId)
232 {
233 if (key != std::string(ENABLE_IME) && key != std::string(ENABLE_KEYBOARD) && key != std::string(TEMP_IME)) {
234 IMSA_HILOGD("invalid key: %{public}s.", key.c_str());
235 return ErrorCode::ERROR_ENABLE_IME;
236 }
237 IMSA_HILOGD("userId: %{public}d, key: %{public}s.", userId, key.c_str());
238 std::string valueStr;
239 int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, key, valueStr);
240 if (ret == ErrorCode::ERROR_KEYWORD_NOT_FOUND) {
241 IMSA_HILOGW("no keyword exist");
242 enableVec.clear();
243 return ErrorCode::NO_ERROR;
244 }
245 if (key != ENABLE_IME && (ret != ErrorCode::NO_ERROR || valueStr.empty())) {
246 IMSA_HILOGW("get value failed, or valueStr is empty.");
247 return ErrorCode::ERROR_ENABLE_IME;
248 }
249 auto parseRet = false;
250 if (key == ENABLE_IME) {
251 valueStr = GetEanbleIme(userId, valueStr);
252 if (valueStr.empty()) {
253 IMSA_HILOGW("valueStr is empty, userId = %{public}d", userId);
254 return ErrorCode::NO_ERROR;
255 }
256 parseRet = ParseEnableIme(valueStr, userId, enableVec);
257 }
258 if (key == ENABLE_KEYBOARD) {
259 parseRet = ParseEnableKeyboard(valueStr, userId, enableVec);
260 }
261 if (key == TEMP_IME) {
262 parseRet = ParseTempIme(valueStr, userId, enableVec);
263 }
264 return parseRet ? ErrorCode::NO_ERROR : ErrorCode::ERROR_ENABLE_IME;
265 }
266
GetGlobalTableUserId(const std::string & valueStr)267 std::string EnableImeDataParser::GetGlobalTableUserId(const std::string &valueStr)
268 {
269 auto root = cJSON_Parse(valueStr.c_str());
270 if (root == nullptr) {
271 IMSA_HILOGE("valueStr content parse failed!");
272 return "";
273 }
274 auto subNode = Serializable::GetSubNode(root, "enableImeList");
275 if (subNode == nullptr || !cJSON_IsObject(subNode)) {
276 IMSA_HILOGW("subNode is null or not object");
277 cJSON_Delete(root);
278 return "";
279 }
280 if (subNode->child == nullptr) {
281 IMSA_HILOGW("subNode has not child");
282 cJSON_Delete(root);
283 return "";
284 }
285 std::string userId = subNode->child->string;
286 cJSON_Delete(root);
287 return userId;
288 }
289
ParseTempIme(const std::string & valueStr,int32_t userId,std::vector<std::string> & tempVector)290 bool EnableImeDataParser::ParseTempIme(const std::string &valueStr, int32_t userId,
291 std::vector<std::string> &tempVector)
292 {
293 TempImeCfg tempIme;
294 tempIme.tempImeList.userId = std::to_string(userId);
295 auto ret = tempIme.Unmarshall(valueStr);
296 if (!ret) {
297 return ret;
298 }
299 tempVector = tempIme.tempImeList.identities;
300 return true;
301 }
302
ParseEnableIme(const std::string & valueStr,int32_t userId,std::vector<std::string> & enableVec)303 bool EnableImeDataParser::ParseEnableIme(const std::string &valueStr, int32_t userId,
304 std::vector<std::string> &enableVec)
305 {
306 EnableImeCfg enableIme;
307 enableIme.userImeCfg.userId = std::to_string(userId);
308 auto ret = enableIme.Unmarshall(valueStr);
309 if (!ret) {
310 return ret;
311 }
312 enableVec = enableIme.userImeCfg.identities;
313 return true;
314 }
315
ParseEnableKeyboard(const std::string & valueStr,int32_t userId,std::vector<std::string> & enableVec)316 bool EnableImeDataParser::ParseEnableKeyboard(const std::string &valueStr, int32_t userId,
317 std::vector<std::string> &enableVec)
318 {
319 EnableKeyBoardCfg enableKeyboard;
320 enableKeyboard.userImeCfg.userId = std::to_string(userId);
321 auto ret = enableKeyboard.Unmarshall(valueStr);
322 if (!ret) {
323 return ret;
324 }
325 enableVec = enableKeyboard.userImeCfg.identities;
326 return true;
327 }
328
GetDefaultIme()329 std::shared_ptr<Property> EnableImeDataParser::GetDefaultIme()
330 {
331 std::lock_guard<std::mutex> lock(defaultImeMutex_);
332 if (defaultImeInfo_ == nullptr) {
333 defaultImeInfo_ = std::make_shared<Property>();
334 }
335 if (!defaultImeInfo_->name.empty() && !defaultImeInfo_->id.empty()) {
336 IMSA_HILOGD("defaultImeInfo_ has cached default time: %{public}s.", defaultImeInfo_->name.c_str());
337 return defaultImeInfo_;
338 }
339
340 auto defaultIme = ImeInfoInquirer::GetInstance().GetDefaultImeCfgProp();
341 if (defaultIme == nullptr) {
342 IMSA_HILOGE("defaultIme is nullptr!");
343 return defaultImeInfo_;
344 }
345 defaultImeInfo_->name = defaultIme->name;
346 defaultImeInfo_->id = defaultIme->id;
347 return defaultImeInfo_;
348 }
349
OnConfigChanged(int32_t userId,const std::string & key)350 void EnableImeDataParser::OnConfigChanged(int32_t userId, const std::string &key)
351 {
352 UpdateEnableData(userId, key);
353 }
354
OnPackageAdded(int32_t userId,const std::string & bundleName)355 void EnableImeDataParser::OnPackageAdded(int32_t userId, const std::string &bundleName)
356 {
357 IMSA_HILOGI("run in:%{public}d,%{public}s.", userId, bundleName.c_str());
358 auto initEnabledState = ImeInfoInquirer::GetInstance().GetSystemInitEnabledState();
359 if (initEnabledState != EnabledStatus::BASIC_MODE) {
360 IMSA_HILOGI("init enabled state is %{public}d.", static_cast<int32_t>(initEnabledState));
361 return;
362 }
363 if (bundleName == ImeInfoInquirer::GetInstance().GetDefaultIme().bundleName) {
364 IMSA_HILOGI("default ime not deal:%{public}d,%{public}s.", userId, bundleName.c_str());
365 return;
366 }
367 auto settingInstance = SettingsDataUtils::GetInstance();
368 if (settingInstance == nullptr) {
369 return;
370 }
371 std::lock_guard<std::mutex> lock(userIdLock_);
372 std::string globalStr;
373 int32_t ret = settingInstance->GetStringValue(SETTING_URI_PROXY, ENABLE_IME, globalStr);
374 if (ret != ErrorCode::NO_ERROR && ret != ErrorCode::ERROR_KEYWORD_NOT_FOUND) {
375 IMSA_HILOGE("get global table failed:%{public}d.", ret);
376 return;
377 }
378 if (userId == currentUserId_) {
379 OnForegroundPackageAdded(userId, bundleName, globalStr);
380 return;
381 }
382 OnBackgroundPackageAdded(userId, bundleName, globalStr);
383 }
384
OnBackgroundPackageAdded(int32_t userId,const std::string & bundleName,const std::string & globalContent)385 void EnableImeDataParser::OnBackgroundPackageAdded(
386 int32_t userId, const std::string &bundleName, const std::string &globalContent)
387 {
388 IMSA_HILOGI("run in:%{public}d,%{public}s,%{public}s.", userId, bundleName.c_str(), globalContent.c_str());
389 auto globalUserId = GetGlobalTableUserId(globalContent);
390 if (!globalUserId.empty() && globalUserId == std::to_string(userId)) {
391 IMSA_HILOGW("background add, but globalUserId same with userId:[%{public}d,%{public}d,%{public}s].",
392 currentUserId_, userId, bundleName.c_str());
393 }
394 std::string finalUserContent;
395 AddToUserEnableTable(userId, bundleName, finalUserContent);
396 }
397
OnForegroundPackageAdded(int32_t userId,const std::string & bundleName,const std::string & globalContent)398 void EnableImeDataParser::OnForegroundPackageAdded(
399 int32_t userId, const std::string &bundleName, const std::string &globalContent)
400 {
401 IMSA_HILOGI("run in:%{public}d,%{public}s,%{public}s.", userId, bundleName.c_str(), globalContent.c_str());
402 auto globalUserId = GetGlobalTableUserId(globalContent);
403 if (globalUserId.empty() || globalUserId == std::to_string(currentUserId_)) {
404 std::string finalGlobalContent;
405 auto ret = AddToGlobalEnableTable(userId, bundleName, finalGlobalContent);
406 if (ret != ErrorCode::NO_ERROR) {
407 IMSA_HILOGE("add failed:[%{public}d,%{public}s,%{public}s,%{public}d].", userId, globalUserId.c_str(),
408 bundleName.c_str(), ret);
409 return;
410 }
411 CoverUserEnableTable(userId, finalGlobalContent);
412 return;
413 }
414 IMSA_HILOGW("globalUserId not same with currentUserId:%{public}d,%{public}s,%{public}s.", userId,
415 bundleName.c_str(), globalContent.c_str());
416 auto ret = CoverUserEnableTable(atoi(globalUserId.c_str()), globalContent);
417 if (ret != ErrorCode::NO_ERROR) {
418 IMSA_HILOGE("cover failed:[%{public}d,%{public}s,%{public}s,%{public}d].", userId, globalUserId.c_str(),
419 bundleName.c_str(), ret);
420 return;
421 }
422 std::string finalUserContent;
423 ret = AddToUserEnableTable(userId, bundleName, finalUserContent);
424 if (ret != ErrorCode::NO_ERROR) {
425 IMSA_HILOGE("userId not same, add failed:[%{public}d,%{public}s,%{public}s,%{public}d].", userId,
426 globalUserId.c_str(), bundleName.c_str(), ret);
427 return;
428 }
429 CoverGlobalEnableTable(finalUserContent);
430 }
431
AddToUserEnableTable(int32_t userId,const std::string & bundleName,std::string & userContent)432 int32_t EnableImeDataParser::AddToUserEnableTable(
433 int32_t userId, const std::string &bundleName, std::string &userContent)
434 {
435 return AddToEnableTable(
436 userId, bundleName, SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true", userContent);
437 }
438
AddToGlobalEnableTable(int32_t userId,const std::string & bundleName,std::string & globalContent)439 int32_t EnableImeDataParser::AddToGlobalEnableTable(
440 int32_t userId, const std::string &bundleName, std::string &globalContent)
441 {
442 return AddToEnableTable(userId, bundleName, SETTING_URI_PROXY, globalContent);
443 }
444
AddToEnableTable(int32_t userId,const std::string & bundleName,const std::string & uriProxy,std::string & tableContent)445 int32_t EnableImeDataParser::AddToEnableTable(
446 int32_t userId, const std::string &bundleName, const std::string &uriProxy, std::string &tableContent)
447 {
448 auto settingInstance = SettingsDataUtils::GetInstance();
449 if (settingInstance == nullptr) {
450 return ErrorCode::ERROR_ENABLE_IME;
451 }
452 std::string valueStr;
453 int32_t ret = settingInstance->GetStringValue(uriProxy, ENABLE_IME, valueStr);
454 if (ret != ErrorCode::NO_ERROR && ret != ErrorCode::ERROR_KEYWORD_NOT_FOUND) {
455 IMSA_HILOGE("get enable table failed:[%{public}d, %{public}s, %{public}s, %{public}d].", userId,
456 bundleName.c_str(), uriProxy.c_str(), ret);
457 return ret;
458 }
459 IMSA_HILOGI("start:%{public}d,%{public}s,%{public}s,%{public}s.", userId, bundleName.c_str(), uriProxy.c_str(),
460 valueStr.c_str());
461 EnableImeCfg imeCfg;
462 imeCfg.userImeCfg.userId = std::to_string(userId);
463 imeCfg.Unmarshall(valueStr);
464 auto it = std::find_if(imeCfg.userImeCfg.identities.begin(), imeCfg.userImeCfg.identities.end(),
465 [&bundleName](const std::string &bundleNameTmp) { return bundleNameTmp == bundleName; });
466 if (it == imeCfg.userImeCfg.identities.end()) {
467 imeCfg.userImeCfg.identities.push_back(bundleName);
468 }
469 imeCfg.Marshall(tableContent);
470 if (tableContent.empty()) {
471 IMSA_HILOGE(
472 "Marshall failed:[%{public}d, %{public}s, %{public}s].", userId, bundleName.c_str(), uriProxy.c_str());
473 return ErrorCode::ERROR_ENABLE_IME;
474 }
475 settingInstance->SetStringValue(uriProxy, ENABLE_IME, tableContent);
476 IMSA_HILOGI("end:%{public}d,%{public}s,%{public}s,%{public}s.", userId, bundleName.c_str(), uriProxy.c_str(),
477 tableContent.c_str());
478 return ErrorCode::NO_ERROR;
479 }
480
CoverUserEnableTable(int32_t userId,const std::string & userContent)481 int32_t EnableImeDataParser::CoverUserEnableTable(int32_t userId, const std::string &userContent)
482 {
483 auto settingInstance = SettingsDataUtils::GetInstance();
484 if (settingInstance == nullptr) {
485 return ErrorCode::ERROR_ENABLE_IME;
486 }
487 auto ret = settingInstance->SetStringValue(
488 SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true", ENABLE_IME, userContent);
489 if (!ret) {
490 return ErrorCode::ERROR_ENABLE_IME;
491 }
492 return ErrorCode::NO_ERROR;
493 }
494
GetImeEnablePattern(int32_t userId,const std::string & bundleName,EnabledStatus & status)495 int32_t EnableImeDataParser::GetImeEnablePattern(int32_t userId, const std::string &bundleName, EnabledStatus &status)
496 {
497 if (ImeInfoInquirer::GetInstance().GetDefaultIme().bundleName == bundleName) {
498 status = EnabledStatus::BASIC_MODE;
499 return ErrorCode::NO_ERROR;
500 }
501 std::vector<std::string> bundleNames;
502 auto ret = GetEnableIme(userId, bundleNames);
503 if (ret != ErrorCode::NO_ERROR) {
504 IMSA_HILOGE("[%{public}d, %{public}s] GetEnableIme failed:%{public}d.", userId, bundleName.c_str(), ret);
505 return ErrorCode::ERROR_ENABLE_IME;
506 }
507 auto it = std::find_if(bundleNames.begin(), bundleNames.end(),
508 [&bundleName](const std::string &bundleNameTmp) { return bundleNameTmp == bundleName; });
509 if (it == bundleNames.end()) {
510 status = EnabledStatus::DISABLED;
511 return ErrorCode::NO_ERROR;
512 }
513 status = EnabledStatus::BASIC_MODE;
514 return ErrorCode::NO_ERROR;
515 }
516
UpdateEnableData(int32_t userId,const std::string & key)517 int32_t EnableImeDataParser::UpdateEnableData(int32_t userId, const std::string &key)
518 {
519 std::lock_guard<std::mutex> autoLock(listMutex_);
520 if (key == ENABLE_IME) {
521 isEnableImeInit_ = false;
522 }
523 std::vector<std::string> enableData;
524 IMSA_HILOGD("update userId: %{public}d %{public}s", userId, key.c_str());
525 auto ret = GetEnableData(key, enableData, userId);
526 if (ret != ErrorCode::NO_ERROR) {
527 IMSA_HILOGE("userId: %{public}d get enable %{public}s list failed!", userId, key.c_str());
528 return ret;
529 }
530 enableList_.insert_or_assign(key, enableData);
531 if (key == ENABLE_IME) {
532 isEnableImeInit_ = true;
533 }
534 return ErrorCode::NO_ERROR;
535 }
536
GetEnableIme(int32_t userId,std::vector<std::string> & enableVec)537 int32_t EnableImeDataParser::GetEnableIme(int32_t userId, std::vector<std::string> &enableVec)
538 {
539 if (userId != currentUserId_) {
540 return GetEnableData(ENABLE_IME, enableVec, userId);
541 }
542 auto ret = GetEnableImeFromCache(enableVec);
543 if (ret == ErrorCode::NO_ERROR) {
544 return ErrorCode::NO_ERROR;
545 }
546 ret = UpdateEnableData(userId, ENABLE_IME);
547 if (ret != ErrorCode::NO_ERROR) {
548 return ret;
549 }
550 return GetEnableImeFromCache(enableVec);
551 }
552
GetEnableImeFromCache(std::vector<std::string> & enableVec)553 int32_t EnableImeDataParser::GetEnableImeFromCache(std::vector<std::string> &enableVec)
554 {
555 std::lock_guard<std::mutex> autoLock(listMutex_);
556 if (isEnableImeInit_) {
557 auto it = enableList_.find(ENABLE_IME);
558 if (it != enableList_.end()) {
559 IMSA_HILOGD("GetEnableIme from cache.");
560 enableVec = it->second;
561 return ErrorCode::NO_ERROR;
562 }
563 }
564 return ErrorCode::ERROR_ENABLE_IME;
565 }
566 } // namespace MiscServices
567 } // namespace OHOS