1 /*
2 * Copyright (c) 2025 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 "input_type_manager.h"
17
18
19 namespace OHOS {
20 namespace MiscServices {
GetInstance()21 InputTypeManager &InputTypeManager::GetInstance()
22 {
23 static InputTypeManager instance;
24 return instance;
25 }
26
IsSupported(InputType type)27 bool InputTypeManager::IsSupported(InputType type)
28 {
29 if (!isTypeCfgReady_.load() && !Init()) {
30 IMSA_HILOGE("init cfg failed!");
31 return false;
32 }
33 std::lock_guard<std::mutex> lock(typesLock_);
34 return inputTypes_.find(type) != inputTypes_.end();
35 }
36
IsInputType(const ImeIdentification & ime)37 bool InputTypeManager::IsInputType(const ImeIdentification &ime)
38 {
39 if (!isTypeCfgReady_.load() && !Init()) {
40 IMSA_HILOGD("init cfg failed.");
41 return false;
42 }
43 std::lock_guard<std::mutex> lock(listLock_);
44 return inputTypeImeList_.find(ime) != inputTypeImeList_.end();
45 }
46
GetImeByInputType(InputType type,ImeIdentification & ime)47 int32_t InputTypeManager::GetImeByInputType(InputType type, ImeIdentification &ime)
48 {
49 if (!isTypeCfgReady_.load() && !Init()) {
50 IMSA_HILOGE("init cfg failed!");
51 return ErrorCode::ERROR_PARSE_CONFIG_FILE;
52 }
53 std::lock_guard<std::mutex> lock(typesLock_);
54 auto iter = inputTypes_.find(type);
55 if (iter == inputTypes_.end()) {
56 IMSA_HILOGE("type: %{public}d not supported!", type);
57 return ErrorCode::ERROR_IMSA_INPUT_TYPE_NOT_FOUND;
58 }
59 ime = iter->second;
60 IMSA_HILOGI("type: %{public}d find ime: %{public}s|%{public}s.", type, ime.bundleName.c_str(), ime.subName.c_str());
61 return ErrorCode::NO_ERROR;
62 }
63
Set(bool isStarted,const ImeIdentification & currentIme)64 void InputTypeManager::Set(bool isStarted, const ImeIdentification ¤tIme)
65 {
66 std::lock_guard<std::mutex> lock(stateLock_);
67 isStarted_ = isStarted;
68 currentTypeIme_ = currentIme;
69 }
70
IsStarted()71 bool InputTypeManager::IsStarted()
72 {
73 std::lock_guard<std::mutex> lock(stateLock_);
74 return isStarted_;
75 }
76
IsSecurityImeStarted()77 bool InputTypeManager::IsSecurityImeStarted()
78 {
79 if (!IsStarted()) {
80 return false;
81 }
82
83 std::lock_guard<std::mutex> lock(typesLock_);
84 return inputTypes_.find(InputType::SECURITY_INPUT) != inputTypes_.end() &&
85 inputTypes_[InputType::SECURITY_INPUT] == GetCurrentIme();
86 }
87
IsCameraImeStarted()88 bool InputTypeManager::IsCameraImeStarted()
89 {
90 if (!IsStarted()) {
91 return false;
92 }
93
94 std::lock_guard<std::mutex> lock(typesLock_);
95 return inputTypes_.find(InputType::CAMERA_INPUT) != inputTypes_.end() &&
96 inputTypes_[InputType::CAMERA_INPUT] == GetCurrentIme();
97 }
98
IsVoiceImeStarted()99 bool InputTypeManager::IsVoiceImeStarted()
100 {
101 if (!IsStarted()) {
102 return false;
103 }
104
105 std::lock_guard<std::mutex> lock(typesLock_);
106 return inputTypes_.find(InputType::VOICE_INPUT) != inputTypes_.end() &&
107 inputTypes_[InputType::VOICE_INPUT] == GetCurrentIme();
108 }
109
IsVoiceKbImeStarted()110 bool InputTypeManager::IsVoiceKbImeStarted()
111 {
112 if (!IsStarted()) {
113 return false;
114 }
115
116 std::lock_guard<std::mutex> lock(typesLock_);
117 return inputTypes_.find(InputType::VOICEKB_INPUT) != inputTypes_.end() &&
118 inputTypes_[InputType::VOICEKB_INPUT] == GetCurrentIme();
119 }
120
GetCurrentInputType()121 InputType InputTypeManager::GetCurrentInputType()
122 {
123 if (IsSecurityImeStarted()) {
124 return InputType::SECURITY_INPUT;
125 }
126 if (IsCameraImeStarted()) {
127 return InputType::CAMERA_INPUT;
128 }
129 if (IsVoiceImeStarted()) {
130 return InputType::VOICE_INPUT;
131 }
132 if (IsVoiceKbImeStarted()) {
133 return InputType::VOICEKB_INPUT;
134 }
135 return InputType::NONE;
136 }
137
GetCurrentIme()138 ImeIdentification InputTypeManager::GetCurrentIme()
139 {
140 std::lock_guard<std::mutex> lock(stateLock_);
141 return currentTypeIme_;
142 }
143
Init()144 bool InputTypeManager::Init()
145 {
146 IMSA_HILOGD("start.");
147 if (isInitInProgress_.load()) {
148 return isInitSuccess_.GetValue();
149 }
150 isInitInProgress_.store(true);
151 isInitSuccess_.Clear(false);
152 std::vector<InputTypeInfo> configs;
153 auto isSuccess = SysCfgParser::ParseInputType(configs);
154 IMSA_HILOGD("ParseInputType isSuccess: %{public}d.", isSuccess);
155 if (isSuccess) {
156 std::lock_guard<std::mutex> lk(typesLock_);
157 for (const auto &config : configs) {
158 inputTypes_.insert({ config.type, { config.bundleName, config.subName } });
159 }
160 for (const auto &cfg : inputTypes_) {
161 std::lock_guard<std::mutex> lock(listLock_);
162 inputTypeImeList_.insert(cfg.second);
163 }
164 } else {
165 std::lock_guard<std::mutex> lk(typesLock_);
166 inputTypes_.clear();
167 }
168 isTypeCfgReady_.store(isSuccess);
169 isInitSuccess_.SetValue(isSuccess);
170 isInitInProgress_.store(false);
171 return isSuccess;
172 }
173 } // namespace MiscServices
174 } // namespace OHOS