1 /*
2 * Copyright (c) 2020-2021 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 #include "event_listener.h"
16 #include "ace_log.h"
17 #include "jerryscript.h"
18 #include "js_fwk_common.h"
19 #include "root_view.h"
20 #include "js_app_context.h"
21
22 namespace OHOS {
23 namespace ACELite {
24 #ifdef JS_EXTRA_EVENT_SUPPORT
KeyBoardEventListener(jerry_value_t fn,const uint16_t id)25 KeyBoardEventListener::KeyBoardEventListener(jerry_value_t fn, const uint16_t id)
26 : fn_ (jerry_acquire_value(fn)), id_ (id) {}
27
~KeyBoardEventListener()28 KeyBoardEventListener::~KeyBoardEventListener()
29 {
30 jerry_release_value(fn_);
31 }
32
OnKeyAct(UIView & view,const KeyEvent & event)33 bool KeyBoardEventListener::OnKeyAct(UIView &view, const KeyEvent &event)
34 {
35 if (jerry_value_is_undefined(fn_)) {
36 return false;
37 }
38
39 jerry_value_t *args = ConvertKeyEventInfo(event);
40 jerry_release_value(CallJSFunctionOnRoot(fn_, args, 1));
41 ClearEventListener(args, 1);
42 return true;
43 }
44
ViewOnTouchCancelListener(jerry_value_t fn,uint16_t id)45 ViewOnTouchCancelListener::ViewOnTouchCancelListener(jerry_value_t fn, uint16_t id)
46 {
47 fn_ = jerry_acquire_value(fn);
48 id_ = id;
49 }
50
~ViewOnTouchCancelListener()51 ViewOnTouchCancelListener::~ViewOnTouchCancelListener()
52 {
53 jerry_release_value(fn_);
54 }
55
OnCancel(UIView & view,const CancelEvent & event)56 bool ViewOnTouchCancelListener::OnCancel(UIView &view, const CancelEvent &event)
57 {
58 return CallBaseEvent(fn_, event, id_);
59 }
60 #endif // JS_EXTRA_EVENT_SUPPORT
61
SetBindTouchStartFuncName(jerry_value_t bindTouchStartFunc)62 void ViewOnTouchListener::SetBindTouchStartFuncName(jerry_value_t bindTouchStartFunc)
63 {
64 if (!jerry_value_is_undefined(bindTouchStartFunc)) {
65 bindTouchStartFunc_ = jerry_acquire_value(bindTouchStartFunc);
66 }
67 }
68
SetBindTouchMoveFuncName(jerry_value_t bindTouchMoveFunc)69 void ViewOnTouchListener::SetBindTouchMoveFuncName(jerry_value_t bindTouchMoveFunc)
70 {
71 if (!jerry_value_is_undefined(bindTouchMoveFunc)) {
72 bindTouchMoveFunc_ = jerry_acquire_value(bindTouchMoveFunc);
73 }
74 }
75
76
SetBindTouchEndFuncName(jerry_value_t bindTouchEndFunc)77 void ViewOnTouchListener::SetBindTouchEndFuncName(jerry_value_t bindTouchEndFunc)
78 {
79 if (!jerry_value_is_undefined(bindTouchEndFunc)) {
80 bindTouchEndFunc_ = jerry_acquire_value(bindTouchEndFunc);
81 }
82 }
83
SetBindSwipeFuncName(jerry_value_t bindSwipeFunc)84 void ViewOnTouchListener::SetBindSwipeFuncName(jerry_value_t bindSwipeFunc)
85 {
86 if (!jerry_value_is_undefined(bindSwipeFunc)) {
87 bindSwipeFunc_ = jerry_acquire_value(bindSwipeFunc);
88 }
89 }
90
SetStopPropagation(bool isStopPropogation)91 void ViewOnTouchListener::SetStopPropagation(bool isStopPropogation)
92 {
93 isStopPropagation_ = isStopPropogation;
94 }
95
OnDragStart(UIView & view,const DragEvent & event)96 bool ViewOnTouchListener::OnDragStart(UIView& view, const DragEvent &event)
97 {
98 if (!AsyncTaskManager::GetInstance().IsFront()) {
99 return isStopPropagation_;
100 }
101
102 if (JSUndefined::Is(bindTouchStartFunc_)) {
103 return isStopPropagation_;
104 }
105
106 HILOG_DEBUG(HILOG_MODULE_ACE, "OnDragStart received");
107
108 JSValue arg = EventUtil::CreateTouchEvent(view, event);
109 EventUtil::InvokeCallback(vm_, bindTouchStartFunc_, arg, this);
110 return isStopPropagation_;
111 }
112
OnDrag(UIView & view,const DragEvent & event)113 bool ViewOnTouchListener::OnDrag(UIView& view, const DragEvent& event)
114 {
115 if (!AsyncTaskManager::GetInstance().IsFront()) {
116 return isStopPropagation_;
117 }
118
119 if (JSUndefined::Is(bindTouchMoveFunc_)) {
120 return isStopPropagation_;
121 }
122
123 HILOG_DEBUG(HILOG_MODULE_ACE, "OnDrag received");
124
125 JSValue arg = EventUtil::CreateTouchEvent(view, event);
126 EventUtil::InvokeCallback(vm_, bindTouchMoveFunc_, arg, this);
127 return isStopPropagation_;
128 }
129
OnDragEnd(UIView & view,const DragEvent & event)130 bool ViewOnTouchListener::OnDragEnd(UIView& view, const DragEvent &event)
131 {
132 if (!AsyncTaskManager::GetInstance().IsFront()) {
133 return isStopPropagation_;
134 }
135
136 if (!JSUndefined::Is(bindSwipeFunc_)) {
137 JSValue argSwipe = EventUtil::CreateSwipeEvent(view, event);
138 EventUtil::InvokeCallback(vm_, bindSwipeFunc_, argSwipe, this);
139 }
140
141 if (!JSUndefined::Is(bindTouchEndFunc_)) {
142 JSValue argDragEnd = EventUtil::CreateTouchEvent(view, event);
143 EventUtil::InvokeCallback(vm_, bindTouchEndFunc_, argDragEnd, this);
144 }
145
146 HILOG_DEBUG(HILOG_MODULE_ACE, "OnDragEnd received");
147 HILOG_DEBUG(HILOG_MODULE_ACE, "Swipe received");
148 return isStopPropagation_;
149 }
150
OnChange(UIView & view,const char * value)151 void ValueChangeListener::OnChange(UIView &view, const char *value)
152 {
153 if (IS_UNDEFINED(fn_)) {
154 return;
155 }
156
157 jerry_value_t textValue = jerry_create_string(reinterpret_cast<const jerry_char_t *>(value));
158 jerry_value_t args[1] = {textValue};
159 if (jerry_value_is_function(fn_)) {
160 CallJSFunctionAutoRelease(fn_, UNDEFINED, args, 1);
161 }
162 ReleaseJerryValue(textValue, VA_ARG_END_FLAG);
163 return;
164 }
165
EventExcute(const int16_t index,jerry_value_t bindScrollFunc) const166 void ListEventListener::EventExcute(const int16_t index, jerry_value_t bindScrollFunc) const
167 {
168 TopAbilityState abilityState = JsAppContext::GetInstance()->GetAbilityState();
169 if (abilityState != TopAbilityState::ABILITY_SHOWN) {
170 return;
171 }
172
173 if (IS_UNDEFINED(bindScrollFunc)) {
174 return;
175 }
176
177 int8_t currentState = this->GetScrollState();
178 jerry_value_t currentStateValue = jerry_create_number(currentState);
179 jerry_value_t componentIndex = jerry_create_number(index);
180 jerry_value_t args[ARGS_LEN] = {currentStateValue, componentIndex};
181 CallJSFunctionAutoRelease(bindScrollFunc, UNDEFINED, args, ARGS_LEN);
182 ReleaseJerryValue(currentStateValue, componentIndex, VA_ARG_END_FLAG);
183 }
184 } // namespace ACELite
185 } // namespace OHOS
186