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