• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "event_listener.h"
17 
18 #include <linux/input.h>
19 #include "dock/focus_manager.h"
20 #include "dock/input_device.h"
21 #include "page/page_manager.h"
22 #include "scope_guard.h"
23 
24 namespace Updater {
operator ()() const25 void CallBackDecorator::operator()() const
26 {
27     auto *page = view_.GetParent();
28     if (page == nullptr) {
29         LOG(ERROR) << "view hasn't a parent";
30         return;
31     }
32     if (view_.GetViewId() == nullptr) {
33         LOG(ERROR) << "view is invalid, please check your json config";
34         return;
35     }
36     std::string id = view_.GetViewId();
37     std::string pageId {};
38     if (page->GetViewId() != nullptr) {
39         pageId = page->GetViewId();
40     }
41     // page should be visible
42     if (!page->IsVisible()) {
43         LOG(ERROR) << pageId << " is not visible";
44         return;
45     }
46     // component should be visible
47     if (!view_.IsVisible()) {
48         LOG(ERROR) << id << " is not visible";
49         return;
50     }
51     // then can trigger callback
52     cb_();
53 }
54 
OnRelease(OHOS::UIView & view,const OHOS::ReleaseEvent & event)55 bool LabelOnTouchListener::OnRelease(OHOS::UIView &view, [[maybe_unused]] const OHOS::ReleaseEvent &event)
56 {
57     // wrap cb_ with CallBackDecorator, then call operator()()
58     CallBackDecorator(view, cb_)();
59     return isConsumed_;
60 }
61 
OnClick(OHOS::UIView & view,const OHOS::ClickEvent & event)62 bool BtnOnEventListener::OnClick(OHOS::UIView &view, [[maybe_unused]] const OHOS::ClickEvent &event)
63 {
64     CallBackDecorator(view, cb_)();
65     return isConsumed_;
66 }
67 
OnPress(OHOS::UIView & view,const OHOS::PressEvent & event)68 bool BtnOnEventListener::OnPress(OHOS::UIView &view, [[maybe_unused]] const OHOS::PressEvent &event)
69 {
70     KeyListener::SetButtonPressed(true);
71     return true;
72 }
73 
OnRelease(OHOS::UIView & view,const OHOS::ReleaseEvent & event)74 bool BtnOnEventListener::OnRelease(OHOS::UIView &view, [[maybe_unused]] const OHOS::ReleaseEvent &event)
75 {
76     KeyListener::SetButtonPressed(false);
77     return true;
78 }
79 
OnCancel(OHOS::UIView & view,const OHOS::CancelEvent & event)80 bool BtnOnEventListener::OnCancel(OHOS::UIView &view, [[maybe_unused]] const OHOS::CancelEvent &event)
81 {
82     KeyListener::SetButtonPressed(false);
83     return true;
84 }
85 
OnDragStart(OHOS::UIView & view,const OHOS::DragEvent & event)86 bool BtnOnDragListener::OnDragStart(OHOS::UIView &view, [[maybe_unused]] const OHOS::DragEvent &event)
87 {
88     CallBackDecorator(view, cb_)();
89     return isConsumed_;
90 }
91 
OnDrag(OHOS::UIView & view,const OHOS::DragEvent & event)92 bool BtnOnDragListener::OnDrag(OHOS::UIView &view, const OHOS::DragEvent &event)
93 {
94     CallBackDecorator(view, cb_)();
95     view.SetPosition(view.GetX() + event.GetDeltaX(), view.GetY() + event.GetDeltaY());
96     if (view.GetParent() != nullptr) {
97         view.GetParent()->Invalidate();
98     }
99     return isConsumed_;
100 }
101 
OnDragEnd(OHOS::UIView & view,const OHOS::DragEvent & event)102 bool BtnOnDragListener::OnDragEnd(OHOS::UIView &view, [[maybe_unused]] const OHOS::DragEvent &event)
103 {
104     CallBackDecorator(view, cb_)();
105     return isConsumed_;
106 }
107 
108 bool KeyListener::isButtonPressed_ {false};
109 
OnKeyAct(OHOS::UIView & view,const OHOS::KeyEvent & event)110 bool KeyListener::OnKeyAct(OHOS::UIView &view, const OHOS::KeyEvent &event)
111 {
112     bool consumed = false;
113     switch (event.GetKeyId()) {
114         case KEY_POWER:
115             consumed = ProcessPowerKey(view, event);
116             break;
117         case KEY_VOLUMEUP:
118         case KEY_VOLUMEDOWN:
119             consumed = ProcessVolumeKey(view, event);
120             break;
121         default:
122             LOG(ERROR) << "unsupported key id";
123     }
124     return consumed;
125 }
126 
ProcessPowerKey(OHOS::UIView & view,const OHOS::KeyEvent & event)127 bool KeyListener::ProcessPowerKey(OHOS::UIView &view, const OHOS::KeyEvent &event)
128 {
129     OHOS::UIView *pView = OHOS::FocusManager::GetInstance()->GetFocusedView();
130     // detect power key long press
131     ON_SCOPE_EXIT(LongPressPowerKey) {
132         if (event.GetState() == OHOS::InputDevice::STATE_PRESS) {
133             StartLongPressTimer();
134         } else if (event.GetState() == OHOS::InputDevice::STATE_RELEASE) {
135             StopLongPressTimer();
136         }
137     };
138     if (pView == nullptr) {
139         LOG(ERROR) << "focused view is nullptr";
140         return false;
141     }
142     // triggering button press event by key only supports label button
143     if (pView->GetViewType() != OHOS::UI_LABEL_BUTTON) {
144         LOG(ERROR) << "focused view is not label button";
145         return false;
146     }
147     int16_t centerX = pView->GetX() + static_cast<int16_t>(static_cast<uint16_t>(pView->GetWidth()) >> 1u);
148     int16_t centerY = pView->GetY() + static_cast<int16_t>(static_cast<uint16_t>(pView->GetHeight()) >> 1u);
149     if (event.GetState() == OHOS::InputDevice::STATE_PRESS) {
150         LOG(DEBUG) << "OnPress";
151         pView->OnClickEvent(OHOS::Point { centerX, centerY });
152     }
153     return true;
154 }
155 
ProcessVolumeKey(OHOS::UIView & view,const OHOS::KeyEvent & event)156 bool KeyListener::ProcessVolumeKey(OHOS::UIView &view, const OHOS::KeyEvent &event)
157 {
158     const static std::unordered_map<uint16_t, uint8_t> dirMap {
159         {KEY_VOLUMEUP, OHOS::FOCUS_DIRECTION_UP},
160         {KEY_VOLUMEDOWN, OHOS::FOCUS_DIRECTION_DOWN},
161     };
162     if (isButtonPressed_) {
163         return true;
164     }
165     if (auto it = dirMap.find(event.GetKeyId()); it != dirMap.end() &&
166         event.GetState() == OHOS::InputDevice::STATE_RELEASE) {
167         OHOS::FocusManager::GetInstance()->RequestFocusByDirection(it->second);
168     }
169     return true;
170 }
171 
SetButtonPressed(bool isPressed)172 void KeyListener::SetButtonPressed(bool isPressed)
173 {
174     isButtonPressed_ = isPressed;
175 }
176 }
177