1 /*
2 * Copyright (c) 2024 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 "window_listener.h"
17 #include <cstdint>
18 #include "cj_lambda.h"
19 #include "event_handler.h"
20 #include "event_runner.h"
21 #include "window_manager_hilog.h"
22
23 namespace OHOS {
24 namespace Rosen {
CjWindowListener(int64_t callbackObject,CaseType caseType)25 CjWindowListener::CjWindowListener(int64_t callbackObject, CaseType caseType)
26 : caseType_(caseType)
27 {
28 TLOGI(WmsLogTag::DEFAULT, "Create CjWindowListener");
29 weakRef_ = wptr<CjWindowListener>(this);
30 auto func = reinterpret_cast<void(*)(void*)>(callbackObject);
31 cjCallBack_ = CJLambda::Create(func);
32 }
33
~CjWindowListener()34 CjWindowListener::~CjWindowListener()
35 {
36 TLOGI(WmsLogTag::WMS_DIALOG, "~CjWindowListener");
37 }
38
SetMainEventHandler()39 void CjWindowListener::SetMainEventHandler()
40 {
41 auto mainRunner = AppExecFwk::EventRunner::GetMainEventRunner();
42 if (mainRunner == nullptr) {
43 return;
44 }
45 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(mainRunner);
46 }
47
CallCjMethod(const char * methodName,void * argv)48 void CjWindowListener::CallCjMethod(const char* methodName, void* argv)
49 {
50 TLOGD(WmsLogTag::WMS_DIALOG, "[WindowListener]methodName=%{public}s", methodName);
51 if (cjCallBack_ == nullptr) {
52 TLOGE(WmsLogTag::WMS_DIALOG, "[WindowListener]env_ nullptr or jsCallBack_ is nullptr");
53 return;
54 }
55 cjCallBack_(argv);
56 }
57
GetCjRect(const Rect & rect)58 CjRectInfo GetCjRect(const Rect& rect)
59 {
60 CjRectInfo result = {
61 .left = rect.posX_,
62 .top = rect.posY_,
63 .width = rect.width_,
64 .height = rect.height_
65 };
66 return result;
67 }
68
OnSystemBarPropertyChange(DisplayId displayId,const SystemBarRegionTints & tints)69 void CjWindowListener::OnSystemBarPropertyChange(DisplayId displayId, const SystemBarRegionTints& tints)
70 {
71 auto thisListener = weakRef_.promote();
72 if (thisListener == nullptr) {
73 return;
74 }
75
76 std::vector<SystemBarRegionTintInfo> tintInfos;
77 for (const auto& tint : tints) {
78 auto actType = CJ_TO_WINDOW_TYPE_MAP.count(tint.type_) != 0 ?
79 CJ_TO_WINDOW_TYPE_MAP.at(tint.type_) : static_cast<ApiWindowType>(tint.type_);
80 SystemBarRegionTintInfo info = {
81 .type = static_cast<uint32_t>(actType),
82 .isEnable = tint.prop_.enable_,
83 .backgroundColor = tint.prop_.backgroundColor_,
84 .contentColor = tint.prop_.contentColor_,
85 .region = GetCjRect(tint.region_)
86 };
87 tintInfos.push_back(info);
88 }
89
90 struct SystemBarPropertyInfo {
91 uint32_t displayId;
92 std::vector<SystemBarRegionTintInfo> tints;
93 };
94
95 SystemBarPropertyInfo propertyInfo = {
96 .displayId = static_cast<uint32_t>(displayId),
97 .tints = tintInfos
98 };
99
100 thisListener->CallCjMethod(SYSTEM_BAR_TINT_CHANGE_CB.c_str(), &propertyInfo);
101 }
102
OnSizeChange(Rect rect,WindowSizeChangeReason reason,const std::shared_ptr<RSTransaction> & rsTransaction)103 void CjWindowListener::OnSizeChange(Rect rect, WindowSizeChangeReason reason,
104 const std::shared_ptr<RSTransaction>& rsTransaction)
105 {
106 if (currRect_.width_ == rect.width_ && currRect_.height_ == rect.height_ &&
107 reason != WindowSizeChangeReason::DRAG_END) {
108 return;
109 }
110 auto thisListener = weakRef_.promote();
111 if (thisListener == nullptr) {
112 return;
113 }
114 struct SizeChangeInfo {
115 uint32_t width;
116 uint32_t height;
117 };
118 SizeChangeInfo argv = {
119 .width = rect.width_,
120 .height = rect.height_
121 };
122 thisListener->CallCjMethod(WINDOW_SIZE_CHANGE_CB.c_str(), &argv);
123 currRect_ = rect;
124 }
125
OnModeChange(WindowMode mode,bool hasDeco)126 void CjWindowListener::OnModeChange(WindowMode mode, bool hasDeco)
127 {
128 }
129
OnAvoidAreaChanged(const AvoidArea avoidArea,AvoidAreaType type,const sptr<OccupiedAreaChangeInfo> & info)130 void CjWindowListener::OnAvoidAreaChanged(const AvoidArea avoidArea, AvoidAreaType type,
131 const sptr<OccupiedAreaChangeInfo>& info)
132 {
133 auto thisListener = weakRef_.promote();
134 if (thisListener == nullptr) {
135 return;
136 }
137
138 struct AvoidAreaValue {
139 bool visible;
140 CjRectInfo leftRect;
141 CjRectInfo topRect;
142 CjRectInfo rightRect;
143 CjRectInfo bottomRect;
144 };
145 struct AvoidAreaInfo {
146 uint32_t type;
147 AvoidAreaValue area;
148 };
149 AvoidAreaInfo avoidAreaInfo = {
150 .type = static_cast<uint32_t>(type),
151 .area = {
152 .visible = (type != AvoidAreaType::TYPE_CUTOUT),
153 .leftRect = GetCjRect(avoidArea.leftRect_),
154 .topRect = GetCjRect(avoidArea.topRect_),
155 .rightRect = GetCjRect(avoidArea.rightRect_),
156 .bottomRect = GetCjRect(avoidArea.bottomRect_)
157 }
158 };
159
160 thisListener->CallCjMethod(AVOID_AREA_CHANGE_CB.c_str(), &avoidAreaInfo);
161 }
162
AfterForeground()163 void CjWindowListener::AfterForeground()
164 {
165 if (state_ == WindowState::STATE_INITIAL || state_ == WindowState::STATE_HIDDEN) {
166 LifeCycleCallBack(LifeCycleEventType::FOREGROUND);
167 state_ = WindowState::STATE_SHOWN;
168 }
169 }
170
AfterBackground()171 void CjWindowListener::AfterBackground()
172 {
173 if (state_ == WindowState::STATE_INITIAL || state_ == WindowState::STATE_SHOWN) {
174 LifeCycleCallBack(LifeCycleEventType::BACKGROUND);
175 state_ = WindowState::STATE_HIDDEN;
176 }
177 }
178
AfterFocused()179 void CjWindowListener::AfterFocused()
180 {
181 LifeCycleCallBack(LifeCycleEventType::ACTIVE);
182 }
183
AfterUnfocused()184 void CjWindowListener::AfterUnfocused()
185 {
186 LifeCycleCallBack(LifeCycleEventType::INACTIVE);
187 }
188
AfterResumed()189 void CjWindowListener::AfterResumed()
190 {
191 if (caseType_ == CaseType::CASE_STAGE) {
192 LifeCycleCallBack(LifeCycleEventType::RESUMED);
193 }
194 }
195
AfterPaused()196 void CjWindowListener::AfterPaused()
197 {
198 if (caseType_ == CaseType::CASE_STAGE) {
199 LifeCycleCallBack(LifeCycleEventType::PAUSED);
200 }
201 }
202
AfterDestroyed()203 void CjWindowListener::AfterDestroyed()
204 {
205 if (caseType_ == CaseType::CASE_WINDOW) {
206 LifeCycleCallBack(LifeCycleEventType::DESTROYED);
207 }
208 }
209
LifeCycleCallBack(LifeCycleEventType eventType)210 void CjWindowListener::LifeCycleCallBack(LifeCycleEventType eventType)
211 {
212 uint32_t type = static_cast<uint32_t>(eventType);
213 auto thisListener = weakRef_.promote();
214 if (thisListener == nullptr) {
215 return;
216 }
217
218 thisListener->CallCjMethod(LIFECYCLE_EVENT_CB.c_str(), &type);
219 }
220
OnSizeChange(const sptr<OccupiedAreaChangeInfo> & info,const std::shared_ptr<RSTransaction> & rsTransaction)221 void CjWindowListener::OnSizeChange(const sptr<OccupiedAreaChangeInfo>& info,
222 const std::shared_ptr<RSTransaction>& rsTransaction)
223 {
224 if (info == nullptr) {
225 TLOGE(WmsLogTag::WMS_DIALOG, "[WindowListener] this changeInfo is nullptr");
226 return;
227 }
228 TLOGI(WmsLogTag::WMS_DIALOG, "[WindowListener]OccupiedAreaChangeInfo, type: %{public}u, " \
229 "input rect: [%{public}d, %{public}d, %{public}u, %{public}u]",
230 static_cast<uint32_t>(info->type_),
231 info->rect_.posX_, info->rect_.posY_, info->rect_.width_, info->rect_.height_);
232 auto thisListener = weakRef_.promote();
233 if (thisListener == nullptr) {
234 TLOGE(WmsLogTag::WMS_DIALOG, "[WindowListener] this listener is nullptr");
235 return;
236 }
237 void* argv = &(info->rect_.height_);
238 thisListener->CallCjMethod(KEYBOARD_HEIGHT_CHANGE_CB.c_str(), argv);
239 }
240
OnTouchOutside() const241 void CjWindowListener::OnTouchOutside() const
242 {
243 auto thisListener = weakRef_.promote();
244 if (thisListener == nullptr) {
245 return;
246 }
247
248 thisListener->CallCjMethod(TOUCH_OUTSIDE_CB.c_str(), nullptr);
249 }
250
OnScreenshot()251 void CjWindowListener::OnScreenshot()
252 {
253 auto thisListener = weakRef_.promote();
254 if (thisListener == nullptr) {
255 return;
256 }
257
258 thisListener->CallCjMethod(SCREENSHOT_EVENT_CB.c_str(), nullptr);
259 }
260
OnDialogTargetTouch() const261 void CjWindowListener::OnDialogTargetTouch() const
262 {
263 auto thisListener = weakRef_.promote();
264 if (thisListener == nullptr) {
265 return;
266 }
267
268 thisListener->CallCjMethod(DIALOG_TARGET_TOUCH_CB.c_str(), nullptr);
269 }
270
OnDialogDeathRecipient() const271 void CjWindowListener::OnDialogDeathRecipient() const
272 {
273 auto thisListener = weakRef_.promote();
274 if (thisListener == nullptr) {
275 return;
276 }
277
278 thisListener->CallCjMethod(DIALOG_DEATH_RECIPIENT_CB.c_str(), nullptr);
279 }
280
OnGestureNavigationEnabledUpdate(bool enable)281 void CjWindowListener::OnGestureNavigationEnabledUpdate(bool enable)
282 {
283 auto thisListener = weakRef_.promote();
284 if (thisListener == nullptr) {
285 return;
286 }
287
288 bool navigationEnabled = enable;
289 thisListener->CallCjMethod(GESTURE_NAVIGATION_ENABLED_CHANGE_CB.c_str(), &navigationEnabled);
290 }
291
OnWaterMarkFlagUpdate(bool showWaterMark)292 void CjWindowListener::OnWaterMarkFlagUpdate(bool showWaterMark)
293 {
294 auto thisListener = weakRef_.promote();
295 if (thisListener == nullptr) {
296 return;
297 }
298
299 bool waterMarkFlag = showWaterMark;
300 thisListener->CallCjMethod(WATER_MARK_FLAG_CHANGE_CB.c_str(), &waterMarkFlag);
301 }
302
OnWindowVisibilityChangedCallback(const bool isVisible)303 void CjWindowListener::OnWindowVisibilityChangedCallback(const bool isVisible)
304 {
305 auto thisListener = weakRef_.promote();
306 if (thisListener == nullptr) {
307 return;
308 }
309
310 bool visibility = isVisible;
311 thisListener->CallCjMethod(WINDOW_VISIBILITY_CHANGE_CB.c_str(), &visibility);
312 }
313
OnWindowStatusChange(WindowStatus status)314 void CjWindowListener::OnWindowStatusChange(WindowStatus status)
315 {
316 auto thisListener = weakRef_.promote();
317 if (thisListener == nullptr) {
318 return;
319 }
320
321 WindowStatus currentStatus = status;
322 thisListener->CallCjMethod(WINDOW_STATUS_CHANGE_CB.c_str(), ¤tStatus);
323 }
324
SetTimeout(int64_t timeout)325 void CjWindowListener::SetTimeout(int64_t timeout)
326 {
327 noInteractionTimeout_ = timeout;
328 }
329
GetTimeout() const330 int64_t CjWindowListener::GetTimeout() const
331 {
332 return noInteractionTimeout_;
333 }
334
OnWindowNoInteractionCallback()335 void CjWindowListener::OnWindowNoInteractionCallback()
336 {
337 auto thisListener = weakRef_.promote();
338 if (thisListener == nullptr) {
339 return;
340 }
341 thisListener->CallCjMethod(WINDOW_NO_INTERACTION_DETECTED_CB.c_str(), nullptr);
342 }
343
OnWindowTitleButtonRectChanged(const TitleButtonRect & titleButtonRect)344 void CjWindowListener::OnWindowTitleButtonRectChanged(const TitleButtonRect& titleButtonRect)
345 {
346 auto thisListener = weakRef_.promote();
347 if (thisListener == nullptr) {
348 return;
349 }
350 struct TitleButtonRectInfo {
351 int32_t right;
352 int32_t top;
353 uint32_t width;
354 uint32_t height;
355 };
356 TitleButtonRectInfo argv = {
357 .right = titleButtonRect.posX_,
358 .top = titleButtonRect.posY_,
359 .width = titleButtonRect.width_,
360 .height = titleButtonRect.height_
361 };
362 thisListener->CallCjMethod(WINDOW_TITLE_BUTTON_RECT_CHANGE_CB.c_str(), &argv);
363 }
364
OnRectChange(Rect rect,WindowSizeChangeReason reason)365 void CjWindowListener::OnRectChange(Rect rect, WindowSizeChangeReason reason)
366 {
367 if (currRect_ == rect && reason == WindowSizeChangeReason::UNDEFINED) {
368 return;
369 }
370 RectChangeReason rectChangReason = RectChangeReason::UNDEFINED;
371 if (CJ_SIZE_CHANGE_REASON.count(reason) != 0 &&
372 !(reason == WindowSizeChangeReason::MAXIMIZE && rect.posX_ != 0)) {
373 rectChangReason = CJ_SIZE_CHANGE_REASON.at(reason);
374 }
375 if (currentReason_ != RectChangeReason::DRAG && rectChangReason == RectChangeReason::DRAG_END) {
376 rectChangReason = RectChangeReason::MOVE;
377 }
378
379 auto thisListener = weakRef_.promote();
380 if (thisListener == nullptr) {
381 return;
382 }
383
384 struct RectChangeInfo {
385 CjRectInfo rect;
386 WindowSizeChangeReason reason;
387 };
388
389 RectChangeInfo rectChangeInfo = {
390 .rect = GetCjRect(rect),
391 .reason = reason
392 };
393
394 thisListener->CallCjMethod(WINDOW_RECT_CHANGE_CB.c_str(), &rectChangeInfo);
395 currRect_ = rect;
396 }
397
OnSubWindowClose(bool & terminateCloseProcess)398 void CjWindowListener::OnSubWindowClose(bool& terminateCloseProcess)
399 {
400 auto thisListener = weakRef_.promote();
401 if (thisListener == nullptr) {
402 return;
403 }
404
405 bool value = terminateCloseProcess;
406 thisListener->CallCjMethod(WINDOW_SUB_WINDOW_CLOSE_CB.c_str(), &value);
407 terminateCloseProcess = value;
408 }
409
OnMainWindowClose(bool & terminateCloseProcess)410 void CjWindowListener::OnMainWindowClose(bool& terminateCloseProcess)
411 {
412 auto thisListener = weakRef_.promote();
413 if (thisListener == nullptr) {
414 return;
415 }
416
417 bool value = terminateCloseProcess;
418 thisListener->CallCjMethod(WINDOW_STAGE_CLOSE_CB.c_str(), &value);
419 terminateCloseProcess = value;
420 }
421
422 }
423 }
424