1 /*
2  * Copyright (c) 2024 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 "frameworks/base/input_manager/input_manager.h"
17 
18 #include "input_manager.h"
19 
20 #include "base/utils/utils.h"
21 
22 namespace OHOS::Ace {
23 
GetDeviceIds(std::vector<int32_t> & resDeviceIds)24 bool InputManager::GetDeviceIds(std::vector<int32_t>& resDeviceIds)
25 {
26     auto* inputManager = MMI::InputManager::GetInstance();
27     CHECK_NULL_RETURN(inputManager, false);
28     inputManager->GetDeviceIds([&resDeviceIds](std::vector<int32_t>& deviceIds) { resDeviceIds = deviceIds; });
29     return true;
30 }
31 
ConvertKeyboardType(int32_t type)32 KeyboardType ConvertKeyboardType(int32_t type)
33 {
34     switch (type) {
35         case MMI::KeyboardType::KEYBOARD_TYPE_NONE:
36             return KeyboardType::NONE;
37         case MMI::KeyboardType::KEYBOARD_TYPE_UNKNOWN:
38             return KeyboardType::UNKNOWN;
39         case MMI::KeyboardType::KEYBOARD_TYPE_ALPHABETICKEYBOARD:
40             return KeyboardType::ALPHABETICKEYBOARD;
41         case MMI::KeyboardType::KEYBOARD_TYPE_DIGITALKEYBOARD:
42             return KeyboardType::DIGITALKEYBOARD;
43         case MMI::KeyboardType::KEYBOARD_TYPE_HANDWRITINGPEN:
44             return KeyboardType::HANDWRITINGPEN;
45         case MMI::KeyboardType::KEYBOARD_TYPE_REMOTECONTROL:
46             return KeyboardType::REMOTECONTROL;
47         case MMI::KeyboardType::KEYBOARD_TYPE_MAX:
48             return KeyboardType::MAX;
49         default:
50             return KeyboardType::NONE;
51     }
52 }
53 
GetKeyboardType(int32_t deviceId)54 KeyboardType InputManager::GetKeyboardType(int32_t deviceId)
55 {
56     auto* inputManager = MMI::InputManager::GetInstance();
57     CHECK_NULL_RETURN(inputManager, KeyboardType::UNKNOWN);
58     int32_t mmiKeyboardType = 0;
59     inputManager->GetKeyboardType(
60         deviceId, [&mmiKeyboardType](int32_t keyboardType) { mmiKeyboardType = keyboardType; });
61     return ConvertKeyboardType(mmiKeyboardType);
62 }
63 
IsKeyboardConnected()64 bool InputManager::IsKeyboardConnected()
65 {
66     std::vector<int32_t> deviceIds;
67     auto resGetIds = GetDeviceIds(deviceIds);
68     if (!resGetIds) {
69         return false;
70     }
71     return std::any_of(deviceIds.begin(), deviceIds.end(),
72         [](int32_t id) { return GetKeyboardType(id) == KeyboardType::ALPHABETICKEYBOARD; });
73 }
74 
75 } // namespace OHOS::Ace
76