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 if (keyOption == nullptr) {
41 HILOG_ERROR("Create keyOption failed.");
42 return;
43 }
44
45 keyOption->SetPreKeys(preKeys);
46 keyOption->SetFinalKey(finalKey);
47 keyOption->SetFinalKeyDown(true);
48 // MMI will get the real holdTime in SettingsData, so the input parameter may not take effect here.
49 keyOption->SetFinalKeyDownDuration(holdTime);
50
51 auto keyEventCallBack = std::bind(&AccessibilityShortKey::OnShortKey, this);
52 int32_t subscribeId = MMI::InputManager::GetInstance()->SubscribeKeyEvent(keyOption, keyEventCallBack);
53 subscribeId_ = subscribeId;
54 if (subscribeId < 0) {
55 HILOG_ERROR("Subscribe key event failed, finalKey: %{public}d id: %{public}d", finalKey, subscribeId);
56 return;
57 }
58 }
59
Register()60 void AccessibilityShortKey::Register()
61 {
62 HILOG_INFO();
63
64 std::set<int32_t> preDownKeysUp;
65 preDownKeysUp.insert(MMI::KeyEvent::KEYCODE_VOLUME_UP);
66 SubscribeShortKey(preDownKeysUp, MMI::KeyEvent::KEYCODE_VOLUME_DOWN, SHORTCUT_TIMEOUT);
67 }
68
OnShortKey()69 void AccessibilityShortKey::OnShortKey()
70 {
71 HILOG_INFO();
72
73 Singleton<AccessibleAbilityManagerService>::GetInstance().OnShortKeyProcess();
74 }
75 } // namespace Accessibility
76 } // namespace OHOS
77