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