1885b47fbSopenharmony_ci/*
2885b47fbSopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3885b47fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4885b47fbSopenharmony_ci * you may not use this file except in compliance with the License.
5885b47fbSopenharmony_ci * You may obtain a copy of the License at
6885b47fbSopenharmony_ci *
7885b47fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8885b47fbSopenharmony_ci *
9885b47fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10885b47fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11885b47fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12885b47fbSopenharmony_ci * See the License for the specific language governing permissions and
13885b47fbSopenharmony_ci * limitations under the License.
14885b47fbSopenharmony_ci */
15885b47fbSopenharmony_ci
16885b47fbSopenharmony_ci#include "accessibility_short_key.h"
17885b47fbSopenharmony_ci#include "accessible_ability_manager_service.h"
18885b47fbSopenharmony_ci#include "hilog_wrapper.h"
19885b47fbSopenharmony_ci
20885b47fbSopenharmony_cinamespace OHOS {
21885b47fbSopenharmony_cinamespace Accessibility {
22885b47fbSopenharmony_cinamespace {
23885b47fbSopenharmony_ci    constexpr int32_t SHORTCUT_TIMEOUT = 3000; // ms
24885b47fbSopenharmony_ci} // namespace
25885b47fbSopenharmony_ci
26885b47fbSopenharmony_ciAccessibilityShortKey::~AccessibilityShortKey()
27885b47fbSopenharmony_ci{
28885b47fbSopenharmony_ci    HILOG_INFO();
29885b47fbSopenharmony_ci
30885b47fbSopenharmony_ci    if (subscribeId_ < 0) {
31885b47fbSopenharmony_ci        return;
32885b47fbSopenharmony_ci    }
33885b47fbSopenharmony_ci
34885b47fbSopenharmony_ci    MMI::InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeId_);
35885b47fbSopenharmony_ci}
36885b47fbSopenharmony_ci
37885b47fbSopenharmony_civoid AccessibilityShortKey::SubscribeShortKey(std::set<int32_t> preKeys, int32_t finalKey, int32_t holdTime)
38885b47fbSopenharmony_ci{
39885b47fbSopenharmony_ci    std::shared_ptr<MMI::KeyOption> keyOption = std::make_shared<MMI::KeyOption>();
40885b47fbSopenharmony_ci    keyOption->SetPreKeys(preKeys);
41885b47fbSopenharmony_ci    keyOption->SetFinalKey(finalKey);
42885b47fbSopenharmony_ci    keyOption->SetFinalKeyDown(true);
43885b47fbSopenharmony_ci    // MMI will get the real holdTime in SettingsData, so the input parameter may not take effect here.
44885b47fbSopenharmony_ci    keyOption->SetFinalKeyDownDuration(holdTime);
45885b47fbSopenharmony_ci
46885b47fbSopenharmony_ci    auto keyEventCallBack = std::bind(&AccessibilityShortKey::OnShortKey, this);
47885b47fbSopenharmony_ci    int32_t subscribeId = MMI::InputManager::GetInstance()->SubscribeKeyEvent(keyOption, keyEventCallBack);
48885b47fbSopenharmony_ci    subscribeId_ = subscribeId;
49885b47fbSopenharmony_ci    if (subscribeId < 0) {
50885b47fbSopenharmony_ci        HILOG_ERROR("Subscribe key event failed, finalKey: %{public}d id: %{public}d", finalKey, subscribeId);
51885b47fbSopenharmony_ci        return;
52885b47fbSopenharmony_ci    }
53885b47fbSopenharmony_ci}
54885b47fbSopenharmony_ci
55885b47fbSopenharmony_civoid AccessibilityShortKey::Register()
56885b47fbSopenharmony_ci{
57885b47fbSopenharmony_ci    HILOG_INFO();
58885b47fbSopenharmony_ci
59885b47fbSopenharmony_ci    if (subscribeId_ >= 0) {
60885b47fbSopenharmony_ci        HILOG_WARN("shortcut key is not unregistered, id: %{public}d", subscribeId_);
61885b47fbSopenharmony_ci    }
62885b47fbSopenharmony_ci
63885b47fbSopenharmony_ci    std::set<int32_t> preDownKeysUp;
64885b47fbSopenharmony_ci    preDownKeysUp.insert(MMI::KeyEvent::KEYCODE_VOLUME_UP);
65885b47fbSopenharmony_ci    SubscribeShortKey(preDownKeysUp, MMI::KeyEvent::KEYCODE_VOLUME_DOWN, SHORTCUT_TIMEOUT);
66885b47fbSopenharmony_ci}
67885b47fbSopenharmony_ci
68885b47fbSopenharmony_civoid AccessibilityShortKey::Unregister()
69885b47fbSopenharmony_ci{
70885b47fbSopenharmony_ci    if (subscribeId_ < 0) {
71885b47fbSopenharmony_ci        HILOG_INFO("shortcut key is not registered");
72885b47fbSopenharmony_ci        return;
73885b47fbSopenharmony_ci    }
74885b47fbSopenharmony_ci
75885b47fbSopenharmony_ci    HILOG_INFO("unregister shortcut key, last subscribeId is %{public}d", subscribeId_);
76885b47fbSopenharmony_ci    MMI::InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeId_);
77885b47fbSopenharmony_ci    subscribeId_ = -1;
78885b47fbSopenharmony_ci}
79885b47fbSopenharmony_ci
80885b47fbSopenharmony_civoid AccessibilityShortKey::OnShortKey()
81885b47fbSopenharmony_ci{
82885b47fbSopenharmony_ci    HILOG_INFO();
83885b47fbSopenharmony_ci
84885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().OnShortKeyProcess();
85885b47fbSopenharmony_ci}
86885b47fbSopenharmony_ci} // namespace Accessibility
87885b47fbSopenharmony_ci} // namespace OHOS
88