• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 &currentIme)
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 // LCOV_EXCL_START
IsSecurityImeStarted()77 bool InputTypeManager::IsSecurityImeStarted()
78 {
79     InputType type = InputType::SECURITY_INPUT;
80     return IsInputTypeImeStarted(type);
81     std::lock_guard<std::mutex> lock(typesLock_);
82 }
83 
IsCameraImeStarted()84 bool InputTypeManager::IsCameraImeStarted()
85 {
86     InputType type = InputType::CAMERA_INPUT;
87     return IsInputTypeImeStarted(type);
88 }
89 
IsVoiceImeStarted()90 bool InputTypeManager::IsVoiceImeStarted()
91 {
92     InputType type = InputType::VOICE_INPUT;
93     return IsInputTypeImeStarted(type);
94 }
95 
IsVoiceKbImeStarted()96 bool InputTypeManager::IsVoiceKbImeStarted()
97 {
98     InputType type = InputType::VOICEKB_INPUT;
99     return IsInputTypeImeStarted(type);
100 }
101 
IsInputTypeImeStarted(InputType type)102 bool InputTypeManager::IsInputTypeImeStarted(InputType type)
103 {
104     if (!IsStarted()) {
105         return false;
106     }
107     std::lock_guard<std::mutex> lock(typesLock_);
108     return inputTypes_.find(type) != inputTypes_.end() &&
109            inputTypes_[type] == GetCurrentIme();
110 }
111 
GetCurrentInputType()112 InputType InputTypeManager::GetCurrentInputType()
113 {
114     if (IsSecurityImeStarted()) {
115         return InputType::SECURITY_INPUT;
116     }
117     if (IsCameraImeStarted()) {
118         return InputType::CAMERA_INPUT;
119     }
120     if (IsVoiceImeStarted()) {
121         return InputType::VOICE_INPUT;
122     }
123     if (IsVoiceKbImeStarted()) {
124         return InputType::VOICEKB_INPUT;
125     }
126     return InputType::NONE;
127 }
128 
GetCurrentIme()129 ImeIdentification InputTypeManager::GetCurrentIme()
130 {
131     std::lock_guard<std::mutex> lock(stateLock_);
132     return currentTypeIme_;
133 }
134 // LCOV_EXCL_STOP
Init()135 bool InputTypeManager::Init()
136 {
137     IMSA_HILOGD("start.");
138     if (isInitInProgress_.load()) {
139         return isInitSuccess_.GetValue();
140     }
141     isInitInProgress_.store(true);
142     isInitSuccess_.Clear(false);
143     std::vector<InputTypeInfo> configs;
144     auto isSuccess = SysCfgParser::ParseInputType(configs);
145     IMSA_HILOGD("ParseInputType isSuccess: %{public}d.", isSuccess);
146     if (isSuccess) {
147         std::lock_guard<std::mutex> lk(typesLock_);
148         for (const auto &config : configs) {
149             inputTypes_.insert({ config.type, { config.bundleName, config.subName } });
150         }
151         for (const auto &cfg : inputTypes_) {
152             std::lock_guard<std::mutex> lock(listLock_);
153             inputTypeImeList_.insert(cfg.second);
154         }
155     } else {
156         std::lock_guard<std::mutex> lk(typesLock_);
157         inputTypes_.clear();
158     }
159     isTypeCfgReady_.store(isSuccess);
160     isInitSuccess_.SetValue(isSuccess);
161     isInitInProgress_.store(false);
162     return isSuccess;
163 }
164 } // namespace MiscServices
165 } // namespace OHOS