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 "accessibility_short_key.h"
17 #include "accessible_ability_manager_service.h"
18 #include "hilog_wrapper.h"
19
20 namespace OHOS {
21 namespace Accessibility {
22 namespace {
23 constexpr int32_t SHORTCUT_TIMEOUT = 3000; // ms
24 } // namespace
25
~AccessibilityShortKey()26 AccessibilityShortKey::~AccessibilityShortKey()
27 {
28 HILOG_INFO();
29
30 if (subscribeId_ < 0) {
31 return;
32 }
33
34 MMI::InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeId_);
35 }
36
SubscribeShortKey(std::set<int32_t> preKeys,int32_t finalKey,int32_t holdTime)37 void AccessibilityShortKey::SubscribeShortKey(std::set<int32_t> preKeys, int32_t finalKey, int32_t holdTime)
38 {
39 std::shared_ptr<MMI::KeyOption> keyOption = std::make_shared<MMI::KeyOption>();
40 keyOption->SetPreKeys(preKeys);
41 keyOption->SetFinalKey(finalKey);
42 keyOption->SetFinalKeyDown(true);
43 // MMI will get the real holdTime in SettingsData, so the input parameter may not take effect here.
44 keyOption->SetFinalKeyDownDuration(holdTime);
45
46 auto keyEventCallBack = std::bind(&AccessibilityShortKey::OnShortKey, this);
47 int32_t subscribeId = MMI::InputManager::GetInstance()->SubscribeKeyEvent(keyOption, keyEventCallBack);
48 subscribeId_ = subscribeId;
49 if (subscribeId < 0) {
50 HILOG_ERROR("Subscribe key event failed, finalKey: %{public}d id: %{public}d", finalKey, subscribeId);
51 return;
52 }
53 }
54
Register()55 void AccessibilityShortKey::Register()
56 {
57 HILOG_INFO();
58
59 if (subscribeId_ >= 0) {
60 HILOG_WARN("shortcut key is not unregistered, id: %{public}d", subscribeId_);
61 }
62
63 std::set<int32_t> preDownKeysUp;
64 preDownKeysUp.insert(MMI::KeyEvent::KEYCODE_VOLUME_UP);
65 SubscribeShortKey(preDownKeysUp, MMI::KeyEvent::KEYCODE_VOLUME_DOWN, SHORTCUT_TIMEOUT);
66 }
67
Unregister()68 void AccessibilityShortKey::Unregister()
69 {
70 if (subscribeId_ < 0) {
71 HILOG_INFO("shortcut key is not registered");
72 return;
73 }
74
75 HILOG_INFO("unregister shortcut key, last subscribeId is %{public}d", subscribeId_);
76 MMI::InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeId_);
77 subscribeId_ = -1;
78 }
79
OnShortKey()80 void AccessibilityShortKey::OnShortKey()
81 {
82 HILOG_INFO();
83
84 Singleton<AccessibleAbilityManagerService>::GetInstance().OnShortKeyProcess();
85 }
86 } // namespace Accessibility
87 } // namespace OHOS
88