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 #include "window_impl.h"
16 #include <regex>
17 #include <sstream>
18 #include "permission.h"
19 #include "ui_content.h"
20 #include "window_helper.h"
21 #include "window_manager_hilog.h"
22 #include "window_utils.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27 constexpr Rect g_emptyRect = {0, 0, 0, 0};
28 }
29 static thread_local std::map<std::string, sptr<CJWindowImpl>> g_cjWindowMap;
30 std::recursive_mutex g_mutex;
31 static int g_ctorCnt = 0;
32 
FindCjWindowObject(const std::string & windowName)33 sptr<CJWindowImpl> FindCjWindowObject(const std::string& windowName)
34 {
35     TLOGD(WmsLogTag::WMS_DIALOG, "Try to find window %{public}s in g_cjWindowMap", windowName.c_str());
36     std::lock_guard<std::recursive_mutex> lock(g_mutex);
37     if (g_cjWindowMap.find(windowName) == g_cjWindowMap.end()) {
38         TLOGD(WmsLogTag::WMS_DIALOG, "Can not find window %{public}s in g_cjWindowMap", windowName.c_str());
39         return nullptr;
40     }
41     return g_cjWindowMap[windowName];
42 }
43 
CreateCjWindowObject(sptr<Window> & window)44 sptr<CJWindowImpl> CreateCjWindowObject(sptr<Window>& window)
45 {
46     if (window == nullptr) {
47         TLOGI(WmsLogTag::WMS_DIALOG, "Invalid input");
48         return nullptr;
49     }
50     const std::string& windowName = window->GetWindowName();
51     sptr<CJWindowImpl> windowImpl = FindCjWindowObject(windowName);
52     if (windowImpl != nullptr) {
53         TLOGI(WmsLogTag::WMS_DIALOG, "FindCjWindowObject %{public}s", windowName.c_str());
54         return windowImpl;
55     }
56 
57     windowImpl = FFI::FFIData::Create<CJWindowImpl>(window);
58     if (windowImpl == nullptr) {
59         TLOGI(WmsLogTag::WMS_DIALOG, "Failed to create window %{public}s", windowName.c_str());
60         return nullptr;
61     }
62     std::lock_guard<std::recursive_mutex> lock(g_mutex);
63     g_cjWindowMap[windowName] = windowImpl;
64     return windowImpl;
65 }
66 
CJWindowImpl(sptr<Window> ptr)67 CJWindowImpl::CJWindowImpl(sptr<Window> ptr)
68     : windowToken_(ptr), registerManager_(std::make_unique<CjWindowRegisterManager>())
69 {
70     NotifyNativeWinDestroyFunc func = [](std::string windowName) {
71         std::lock_guard<std::recursive_mutex> lock(g_mutex);
72         if (windowName.empty() || g_cjWindowMap.count(windowName) == 0) {
73             TLOGE(WmsLogTag::WMS_DIALOG, "Can not find window %{public}s ", windowName.c_str());
74             return;
75         }
76         g_cjWindowMap.erase(windowName);
77         TLOGI(WmsLogTag::WMS_DIALOG, "Destroy window %{public}s in js window", windowName.c_str());
78     };
79     if (windowToken_ == nullptr) {
80         TLOGI(WmsLogTag::WMS_DIALOG, "constructe failed");
81         return;
82     }
83     windowToken_->RegisterWindowDestroyedListener(func);
84     TLOGI(WmsLogTag::WMS_DIALOG, " constructorCnt: %{public}d", ++g_ctorCnt);
85 }
86 
GetWindowToken()87 sptr<Window> CJWindowImpl::GetWindowToken()
88 {
89     return windowToken_;
90 }
91 
CheckWindow()92 ResWindow CJWindowImpl::CheckWindow()
93 {
94     ResWindow result;
95     result.ret = static_cast<int32_t>(WmErrorCode::WM_OK);
96     result.nativeWindow = nullptr;
97     if (windowToken_ == nullptr) {
98         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
99         result.ret = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
100         return result;
101     }
102     result.nativeWindow = windowToken_;
103     return result;
104 }
105 
Hide()106 int32_t CJWindowImpl::Hide()
107 {
108     ResWindow result = CheckWindow();
109     if (result.ret != 0) {
110         return result.ret;
111     }
112     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(result.nativeWindow->Hide(0, false, false));
113     TLOGI(WmsLogTag::WMS_DIALOG,
114         "Window [%{public}u] hide end, ret = %{public}d", result.nativeWindow->GetWindowId(), ret);
115     return static_cast<int32_t>(ret);
116 }
117 
HideWithAnimation()118 int32_t CJWindowImpl::HideWithAnimation()
119 {
120     WmErrorCode errCode = WmErrorCode::WM_OK;
121     if (windowToken_ != nullptr) {
122         auto winType = windowToken_->GetType();
123         if (!WindowHelper::IsSystemWindow(winType)) {
124             TLOGE(WmsLogTag::WMS_DIALOG, "window Type %{public}u is not supported", static_cast<uint32_t>(winType));
125             errCode = WmErrorCode::WM_ERROR_INVALID_CALLING;
126         }
127     } else {
128         errCode = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
129     }
130     if (errCode != WmErrorCode::WM_OK) {
131         return static_cast<int32_t>(errCode);
132     }
133     errCode = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->Hide(0, true, false));
134     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] HideWithAnimation end, ret = %{public}d",
135         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), errCode);
136     return static_cast<int32_t>(errCode);
137 }
138 
ShowWindow()139 int32_t CJWindowImpl::ShowWindow()
140 {
141     ResWindow result = CheckWindow();
142     if (result.ret != 0) {
143         return result.ret;
144     }
145     sptr<Window> weakWindow = result.nativeWindow;
146     if (weakWindow == nullptr) {
147         TLOGE(WmsLogTag::WMS_DIALOG, "window is nullptr");
148         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
149     }
150     WMError ret = weakWindow->Show(0, false);
151     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] show with ret = %{public}d",
152         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
153     return static_cast<int32_t>(WM_JS_TO_ERROR_CODE_MAP.at(ret));
154 }
155 
ShowWithAnimation()156 int32_t CJWindowImpl::ShowWithAnimation()
157 {
158     WmErrorCode errCode = WmErrorCode::WM_OK;
159     if (windowToken_ == nullptr) {
160         errCode = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
161     } else {
162         auto winType = windowToken_->GetType();
163         if (!WindowHelper::IsSystemWindow(winType)) {
164             TLOGE(WmsLogTag::WMS_DIALOG, "window Type %{public}u is not supported", static_cast<uint32_t>(winType));
165             errCode = WmErrorCode::WM_ERROR_INVALID_CALLING;
166         }
167     }
168     if (errCode != WmErrorCode::WM_OK) {
169         return static_cast<int32_t>(errCode);
170     }
171     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->Show(0, true));
172     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] ShowWithAnimation end, ret = %{public}d",
173         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
174     return static_cast<int32_t>(ret);
175 }
176 
DestroyWindow()177 int32_t CJWindowImpl::DestroyWindow()
178 {
179     ResWindow result = CheckWindow();
180     if (result.ret != 0) {
181         return result.ret;
182     }
183     sptr<Window> weakWindow = result.nativeWindow;
184     WMError ret = weakWindow->Destroy();
185     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] destroy end, ret = %{public}d",
186         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
187     windowToken_ = nullptr;
188     return static_cast<int32_t>(ret);
189 }
190 
MoveWindowTo(int32_t x,int32_t y)191 int32_t CJWindowImpl::MoveWindowTo(int32_t x, int32_t y)
192 {
193     ResWindow result = CheckWindow();
194     if (result.ret != 0) {
195         return result.ret;
196     }
197     sptr<Window> weakWindow = result.nativeWindow;
198     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->MoveTo(x, y));
199     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] move end, ret = %{public}d",
200         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
201     return static_cast<int32_t>(ret);
202 }
203 
Resize(uint32_t width,uint32_t height)204 int32_t CJWindowImpl::Resize(uint32_t width, uint32_t height)
205 {
206     if (width == 0 || height == 0) {
207         TLOGE(WmsLogTag::WMS_DIALOG, "width or height should greater than 0!");
208         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
209     }
210     ResWindow result = CheckWindow();
211     if (result.ret != 0) {
212         return result.ret;
213     }
214     sptr<Window> weakWindow = result.nativeWindow;
215     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->Resize(width, height));
216     TLOGD(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] resize end, ret = %{public}d",
217         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
218     return static_cast<int32_t>(ret);
219 }
220 
SetWindowMode(uint32_t mode)221 int32_t CJWindowImpl::SetWindowMode(uint32_t mode)
222 {
223     if (windowToken_ == nullptr) {
224         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
225         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
226     }
227     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
228         TLOGE(WmsLogTag::WMS_DIALOG, "set window mode permission denied!");
229         return static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
230     }
231     WindowMode winMode = CJ_TO_NATIVE_WINDOW_MODE_MAP.at(static_cast<ApiWindowMode>(mode));
232     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetWindowMode(winMode));
233     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set type end, ret = %{public}d",
234         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
235     return static_cast<int32_t>(ret);
236 }
237 
GetWindowProperties(int32_t * errCode)238 CWindowProperties CJWindowImpl::GetWindowProperties(int32_t* errCode)
239 {
240     CWindowProperties wp;
241     if (windowToken_ == nullptr) {
242         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
243         *errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
244         return wp;
245     }
246     Rect drawableRect = g_emptyRect;
247     auto uicontent = windowToken_->GetUIContent();
248     if (uicontent == nullptr) {
249         TLOGW(WmsLogTag::WMS_DIALOG, "uicontent is nullptr");
250     } else {
251         uicontent->GetAppPaintSize(drawableRect);
252     }
253     wp.drawableRect.posX = drawableRect.posX_;
254     wp.drawableRect.posY = drawableRect.posY_;
255     wp.drawableRect.height = drawableRect.height_;
256     wp.drawableRect.width = drawableRect.width_;
257     Rect rect = windowToken_->GetRect();
258     wp.windowRect.posX = rect.posX_;
259     wp.windowRect.posY = rect.posY_;
260     wp.windowRect.height = rect.height_;
261     wp.windowRect.width = rect.width_;
262     WindowType type = windowToken_->GetType();
263     if (CJ_TO_WINDOW_TYPE_MAP.count(type) != 0) {
264         wp.type = static_cast<uint32_t>(CJ_TO_WINDOW_TYPE_MAP.at(type));
265     } else {
266         wp.type = static_cast<uint32_t>(type);
267     }
268     wp.isFullScreen = windowToken_->IsFullScreen();
269     wp.isLayoutFullScreen = windowToken_->IsLayoutFullScreen();
270     wp.focusable = windowToken_->GetFocusable();
271     wp.touchable = windowToken_->GetTouchable();
272     wp.brightness = windowToken_->GetBrightness();
273     wp.isKeepScreenOn = windowToken_->IsKeepScreenOn();
274     wp.isPrivacyMode = windowToken_->IsPrivacyMode();
275     wp.isRoundCorner = false;
276     wp.isTransparent = windowToken_->IsTransparent();
277     wp.id = windowToken_->GetWindowId();
278     *errCode = 0;
279     return wp;
280 }
281 
SetWindowLayoutFullScreen(bool isLayoutFullScreen)282 int32_t CJWindowImpl::SetWindowLayoutFullScreen(bool isLayoutFullScreen)
283 {
284     ResWindow result = CheckWindow();
285     if (result.ret != 0) {
286         return result.ret;
287     }
288     sptr<Window> weakWindow = result.nativeWindow;
289     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetLayoutFullScreen(isLayoutFullScreen));
290     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set layout full screen end, ret = %{public}d",
291         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
292     return static_cast<int32_t>(ret);
293 }
294 
SetWindowBackgroundColor(const char * color)295 int32_t CJWindowImpl::SetWindowBackgroundColor(const char* color)
296 {
297     if (windowToken_ == nullptr) {
298         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
299         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
300     }
301     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBackgroundColor(color));
302     if (ret == WmErrorCode::WM_OK) {
303         TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set background color end",
304             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
305     }
306     return static_cast<int32_t>(ret);
307 }
308 
SetWindowBrightness(float brightness)309 int32_t CJWindowImpl::SetWindowBrightness(float brightness)
310 {
311     ResWindow result = CheckWindow();
312     if (result.ret != 0) {
313         return result.ret;
314     }
315     sptr<Window> weakWindow = result.nativeWindow;
316     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetBrightness(brightness));
317     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set brightness end",
318         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
319     return static_cast<int32_t>(ret);
320 }
321 
SetBackdropBlurStyle(uint32_t blurStyle)322 int32_t CJWindowImpl::SetBackdropBlurStyle(uint32_t blurStyle)
323 {
324     if (windowToken_ == nullptr) {
325         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
326         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
327     }
328     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
329         TLOGE(WmsLogTag::WMS_DIALOG, "SetBackdropBlurStyle is not allowed since window is not system window");
330         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
331     }
332     if (blurStyle > static_cast<uint32_t>(WindowBlurStyle::WINDOW_BLUR_THICK)) {
333         TLOGE(WmsLogTag::WMS_DIALOG, "SetBackdropBlurStyle Invalid window blur style");
334         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
335     }
336     WmErrorCode ret =
337         WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBackdropBlurStyle(static_cast<WindowBlurStyle>(blurStyle)));
338     if (ret != WmErrorCode::WM_OK) {
339         TLOGE(WmsLogTag::WMS_DIALOG, "Window SetBackdropBlurStyle failed");
340     } else {
341         TLOGI(WmsLogTag::WMS_DIALOG,
342             "Window [%{public}u, %{public}s] SetBackdropBlurStyle end, blurStyle = %{public}u",
343             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), blurStyle);
344     }
345     return static_cast<int32_t>(ret);
346 }
347 
SetPreferredOrientation(uint32_t orientation)348 int32_t CJWindowImpl::SetPreferredOrientation(uint32_t orientation)
349 {
350     if (windowToken_ == nullptr) {
351         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
352         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
353     }
354     auto winOrientation = static_cast<Orientation>(orientation);
355     if (winOrientation < Orientation::UNSPECIFIED || winOrientation > Orientation::LOCKED) {
356         TLOGE(WmsLogTag::WMS_DIALOG, "Orientation %{public}u invalid!", orientation);
357         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
358     }
359 
360     windowToken_->SetRequestedOrientation(winOrientation);
361     TLOGI(WmsLogTag::WMS_DIALOG,
362         "Window [%{public}u, %{public}s] OnSetPreferredOrientation end, orientation = %{public}u",
363         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(),
364         static_cast<uint32_t>(winOrientation));
365     return static_cast<int32_t>(WmErrorCode::WM_OK);
366 }
367 
SetWindowFocusable(bool focusable)368 int32_t CJWindowImpl::SetWindowFocusable(bool focusable)
369 {
370     ResWindow result = CheckWindow();
371     if (result.ret != 0) {
372         return result.ret;
373     }
374     sptr<Window> weakWindow = result.nativeWindow;
375     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetFocusable(focusable));
376     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set focusable end",
377         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
378     return static_cast<int32_t>(ret);
379 }
380 
SetWindowKeepScreenOn(bool keepScreenOn)381 int32_t CJWindowImpl::SetWindowKeepScreenOn(bool keepScreenOn)
382 {
383     ResWindow result = CheckWindow();
384     if (result.ret != 0) {
385         return result.ret;
386     }
387     sptr<Window> weakWindow = result.nativeWindow;
388     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetKeepScreenOn(keepScreenOn));
389     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set keep screen on end",
390         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
391     return static_cast<int32_t>(ret);
392 }
393 
GetWindowAvoidArea(uint32_t areaType,CAvoidArea * retPtr)394 int32_t CJWindowImpl::GetWindowAvoidArea(uint32_t areaType, CAvoidArea* retPtr)
395 {
396     if (retPtr == nullptr) {
397         TLOGE(WmsLogTag::WMS_DIALOG, "Invalid input");
398         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
399     }
400     if (windowToken_ == nullptr) {
401         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
402         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
403     }
404     AvoidAreaType avoidAreaType = static_cast<AvoidAreaType>(areaType);
405     if ((avoidAreaType > AvoidAreaType::TYPE_NAVIGATION_INDICATOR) || (avoidAreaType < AvoidAreaType::TYPE_SYSTEM)) {
406         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
407     }
408     AvoidArea avoidArea;
409     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->GetAvoidAreaByType(avoidAreaType, avoidArea));
410     retPtr->visible = avoidAreaType == AvoidAreaType::TYPE_CUTOUT ? false : true;
411     if (ret != WmErrorCode::WM_OK) {
412         retPtr->topRect = g_emptyRect;
413         retPtr->leftRect = g_emptyRect;
414         retPtr->rightRect = g_emptyRect;
415         retPtr->bottomRect = g_emptyRect;
416     } else {
417         retPtr->topRect = avoidArea.topRect_;
418         retPtr->leftRect = avoidArea.leftRect_;
419         retPtr->rightRect = avoidArea.rightRect_;
420         retPtr->bottomRect = avoidArea.bottomRect_;
421     }
422     return static_cast<int32_t>(ret);
423 }
424 
SetWindowPrivacyMode(bool isPrivacyMode)425 int32_t CJWindowImpl::SetWindowPrivacyMode(bool isPrivacyMode)
426 {
427     ResWindow result = CheckWindow();
428     if (result.ret != 0) {
429         return result.ret;
430     }
431     sptr<Window> weakWindow = result.nativeWindow;
432     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetPrivacyMode(isPrivacyMode));
433     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set privacy mode end, mode = %{public}u",
434         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), isPrivacyMode);
435     return static_cast<int32_t>(ret);
436 }
437 
SetWindowTouchable(bool touchable)438 int32_t CJWindowImpl::SetWindowTouchable(bool touchable)
439 {
440     ResWindow result = CheckWindow();
441     if (result.ret != 0) {
442         return result.ret;
443     }
444     sptr<Window> weakWindow = result.nativeWindow;
445     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetTouchable(touchable));
446     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set touchable end",
447         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
448     return static_cast<int32_t>(ret);
449 }
450 
SetForbidSplitMove(bool isForbidSplitMove)451 int32_t CJWindowImpl::SetForbidSplitMove(bool isForbidSplitMove)
452 {
453     ResWindow result = CheckWindow();
454     if (result.ret != 0) {
455         return result.ret;
456     }
457     sptr<Window> weakWindow = result.nativeWindow;
458     WmErrorCode ret;
459     if (isForbidSplitMove) {
460         ret = WM_JS_TO_ERROR_CODE_MAP.at(
461             weakWindow->AddWindowFlag(WindowFlag::WINDOW_FLAG_FORBID_SPLIT_MOVE));
462     } else {
463         ret = WM_JS_TO_ERROR_CODE_MAP.at(
464             weakWindow->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_FORBID_SPLIT_MOVE));
465     }
466     return static_cast<int32_t>(ret);
467 }
468 
IsWindowSupportWideGamut(int32_t * errCode)469 bool CJWindowImpl::IsWindowSupportWideGamut(int32_t* errCode)
470 {
471     if (windowToken_ == nullptr) {
472         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
473         *errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
474         return false;
475     }
476     bool flag = windowToken_->IsSupportWideGamut();
477     *errCode = 0;
478     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] OnIsWindowSupportWideGamut end, ret = %{public}u",
479         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), flag);
480     return flag;
481 }
482 
IsWindowShowing(int32_t * errCode)483 bool CJWindowImpl::IsWindowShowing(int32_t* errCode)
484 {
485     if (windowToken_ == nullptr) {
486         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
487         *errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
488         return false;
489     }
490     bool state = (windowToken_->GetWindowState() == WindowState::STATE_SHOWN);
491     *errCode = 0;
492     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] get show state end, state = %{public}u",
493         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), state);
494     return state;
495 }
496 
SetWaterMarkFlag(bool enable)497 int32_t CJWindowImpl::SetWaterMarkFlag(bool enable)
498 {
499     if (windowToken_ == nullptr) {
500         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
501         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
502     }
503     WMError ret;
504     if (enable) {
505         ret = windowToken_->AddWindowFlag(WindowFlag::WINDOW_FLAG_WATER_MARK);
506     } else {
507         ret = windowToken_->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_WATER_MARK);
508     }
509     if (ret != WMError::WM_OK) {
510         TLOGE(WmsLogTag::WMS_DIALOG, "Window SetWaterMarkFlag failed");
511     } else {
512         TLOGI(WmsLogTag::WMS_DIALOG, "[NAPI]Window [%{public}u, %{public}s] set waterMark flag end, ret = %{public}d",
513             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
514     }
515     return static_cast<int32_t>(ret);
516 }
517 
SetShadowRadius(double radius)518 int32_t CJWindowImpl::SetShadowRadius(double radius)
519 {
520     if (windowToken_ == nullptr) {
521         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
522         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
523     }
524     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
525         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
526     }
527     if (MathHelper::LessNotEqual(radius, 0.0)) {
528         TLOGE(WmsLogTag::WMS_DIALOG, "SetShadowRadius invalid radius");
529         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
530     }
531     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowRadius(radius));
532     if (ret != WmErrorCode::WM_OK) {
533         TLOGE(WmsLogTag::WMS_DIALOG, "Window SetShadowRadius failed");
534     }
535     return static_cast<int32_t>(ret);
536 }
537 
SetShadowColor(std::string color)538 int32_t CJWindowImpl::SetShadowColor(std::string color)
539 {
540     if (windowToken_ == nullptr) {
541         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
542         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
543     }
544     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
545         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
546     }
547     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowColor(color));
548     if (ret != WmErrorCode::WM_OK) {
549         TLOGE(WmsLogTag::WMS_DIALOG, "Window SetShadowColor failed");
550     }
551     return static_cast<int32_t>(ret);
552 }
553 
SetShadowOffsetX(double offsetX)554 int32_t CJWindowImpl::SetShadowOffsetX(double offsetX)
555 {
556     if (windowToken_ == nullptr) {
557         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
558         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
559     }
560     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
561         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
562     }
563     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowOffsetX(offsetX));
564     if (ret != WmErrorCode::WM_OK) {
565         TLOGE(WmsLogTag::WMS_DIALOG, "Window SetShadowOffsetX failed");
566     }
567     return static_cast<int32_t>(ret);
568 }
569 
SetShadowOffsetY(double offsetY)570 int32_t CJWindowImpl::SetShadowOffsetY(double offsetY)
571 {
572     if (windowToken_ == nullptr) {
573         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
574         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
575     }
576     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
577         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
578     }
579     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowOffsetY(offsetY));
580     if (ret != WmErrorCode::WM_OK) {
581         TLOGE(WmsLogTag::WMS_DIALOG, "Window SetShadowOffsetY failed");
582     }
583     return static_cast<int32_t>(ret);
584 }
585 
SetBackdropBlur(double radius)586 int32_t CJWindowImpl::SetBackdropBlur(double radius)
587 {
588     if (windowToken_ == nullptr) {
589         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
590         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
591     }
592     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
593         TLOGE(WmsLogTag::WMS_DIALOG, "SetBackdropBlur is not allowed since window is not system window");
594         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
595     }
596     if (MathHelper::LessNotEqual(radius, 0.0)) {
597         TLOGE(WmsLogTag::WMS_DIALOG, "SetBackdropBlur invalid radius");
598         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
599     }
600     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBackdropBlur(radius));
601     if (ret != WmErrorCode::WM_OK) {
602         TLOGE(WmsLogTag::WMS_DIALOG, "Window SetBackdropBlur failed");
603     } else {
604         TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] SetBackdropBlur end, radius = %{public}f",
605             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), radius);
606     }
607     return static_cast<int32_t>(ret);
608 }
609 
SetBlur(double radius)610 int32_t CJWindowImpl::SetBlur(double radius)
611 {
612     if (windowToken_ == nullptr) {
613         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
614         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
615     }
616     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
617         TLOGE(WmsLogTag::WMS_DIALOG, "SetBlur is not allowed since window is not system window");
618         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
619     }
620     if (MathHelper::LessNotEqual(radius, 0.0)) {
621         TLOGE(WmsLogTag::WMS_DIALOG, "SetBlur invalid radius");
622         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
623     }
624     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBlur(radius));
625     if (ret != WmErrorCode::WM_OK) {
626         TLOGE(WmsLogTag::WMS_DIALOG, "Window SetBlur failed");
627     } else {
628         TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] SetBlur end, radius = %{public}f",
629             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), radius);
630     }
631     return static_cast<int32_t>(ret);
632 }
633 
SetAspectRatio(double ratio)634 int32_t CJWindowImpl::SetAspectRatio(double ratio)
635 {
636     if (windowToken_ == nullptr) {
637         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
638         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
639     }
640     if (!WindowHelper::IsMainWindow(windowToken_->GetType())) {
641         TLOGE(WmsLogTag::WMS_DIALOG, "SetAspectRatio is not allowed since window is not main window");
642         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
643     }
644     if (ratio <= 0.0) {
645         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
646     }
647     WMError ret = windowToken_->SetAspectRatio(ratio);
648     if (ret != WMError::WM_OK) {
649         TLOGE(WmsLogTag::WMS_DIALOG, "Window SetAspectRatio failed");
650     } else {
651         TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set aspect ratio end, ret = %{public}d",
652             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
653     }
654     return static_cast<int32_t>(ret);
655 }
656 
ResetAspectRatio()657 int32_t CJWindowImpl::ResetAspectRatio()
658 {
659     if (windowToken_ == nullptr) {
660         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
661         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
662     }
663     if (!WindowHelper::IsMainWindow(windowToken_->GetType())) {
664         TLOGE(WmsLogTag::WMS_DIALOG, "ResetAspectRatio is not allowed since window is not main window");
665         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
666     }
667     WMError ret = windowToken_->ResetAspectRatio();
668     if (ret != WMError::WM_OK) {
669         TLOGE(WmsLogTag::WMS_DIALOG, "Window ResetAspectRatio failed");
670     } else {
671         TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] reset aspect ratio end, ret = %{public}d",
672             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
673     }
674     return static_cast<int32_t>(ret);
675 }
676 
SetWindowColorSpace(uint32_t colorSpace)677 int32_t CJWindowImpl::SetWindowColorSpace(uint32_t colorSpace)
678 {
679     ResWindow result = CheckWindow();
680     if (result.ret != 0) {
681         return result.ret;
682     }
683     sptr<Window> weakWindow = result.nativeWindow;
684     weakWindow->SetColorSpace(ColorSpace(colorSpace));
685     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] OnSetWindowColorSpace end, colorSpace = %{public}u",
686         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
687     return 0;
688 }
689 
Minimize()690 int32_t CJWindowImpl::Minimize()
691 {
692     if (windowToken_ == nullptr) {
693         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
694         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
695     }
696     if (WindowHelper::IsSubWindow(windowToken_->GetType())) {
697         TLOGE(WmsLogTag::WMS_DIALOG, "subWindow hide");
698         return Hide();
699     }
700     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->Minimize());
701     TLOGI(WmsLogTag::WMS_DIALOG, "[NAPI]Window [%{public}u, %{public}s] minimize end, ret = %{public}d",
702         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
703     return static_cast<int32_t>(ret);
704 }
705 
SetCornerRadius(float radius)706 int32_t CJWindowImpl::SetCornerRadius(float radius)
707 {
708     if (windowToken_ == nullptr) {
709         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
710         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
711     }
712     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
713         TLOGE(WmsLogTag::WMS_DIALOG, "set corner radius permission denied!");
714         return static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
715     }
716     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
717         TLOGE(WmsLogTag::WMS_DIALOG, "SetCornerRadius is not allowed since window is not system window");
718         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
719     }
720     if (MathHelper::LessNotEqual(radius, 0.0)) {
721         TLOGE(WmsLogTag::WMS_DIALOG, "SetCornerRadius invalid radius");
722         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
723     }
724     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetCornerRadius(radius));
725     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] SetCornerRadius end, radius = %{public}f",
726         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), radius);
727     return static_cast<int32_t>(ret);
728 }
729 
SetResizeByDragEnabled(bool enable)730 int32_t CJWindowImpl::SetResizeByDragEnabled(bool enable)
731 {
732     ResWindow result = CheckWindow();
733     if (result.ret != 0) {
734         return result.ret;
735     }
736     sptr<Window> weakWindow = result.nativeWindow;
737     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetResizeByDragEnabled(enable));
738     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set dragEnabled end",
739         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
740     return static_cast<int32_t>(ret);
741 }
742 
743 /** @note @window.hierarchy */
RaiseToAppTop()744 int32_t CJWindowImpl::RaiseToAppTop()
745 {
746     ResWindow result = CheckWindow();
747     if (result.ret != 0) {
748         return result.ret;
749     }
750     sptr<Window> weakWindow = result.nativeWindow;
751     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->RaiseToAppTop());
752     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] zorder raise success",
753         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
754     return static_cast<int32_t>(ret);
755 }
756 
SetSnapshotSkip(bool isSkip)757 int32_t CJWindowImpl::SetSnapshotSkip(bool isSkip)
758 {
759     ResWindow result = CheckWindow();
760     if (result.ret != 0) {
761         return result.ret;
762     }
763     sptr<Window> weakWindow = result.nativeWindow;
764     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetSnapshotSkip(isSkip));
765     TLOGI(WmsLogTag::WMS_DIALOG, "[%{public}u, %{public}s] set snapshotSkip end",
766         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
767     return static_cast<int32_t>(ret);
768 }
769 
SetWakeUpScreen(bool wakeUp)770 int32_t CJWindowImpl::SetWakeUpScreen(bool wakeUp)
771 {
772     if (windowToken_ == nullptr) {
773         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
774         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
775     }
776     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
777         TLOGE(WmsLogTag::WMS_DIALOG, "set wake up screen permission denied!");
778         return static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
779     }
780     windowToken_->SetTurnScreenOn(wakeUp);
781     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set wake up screen %{public}d end",
782         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), wakeUp);
783     return 0;
784 }
785 
GetWindowColorSpace(int32_t * errCode)786 uint32_t CJWindowImpl::GetWindowColorSpace(int32_t* errCode)
787 {
788     if (windowToken_ == nullptr) {
789         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
790         *errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
791         return 0;
792     }
793     ColorSpace colorSpace = windowToken_->GetColorSpace();
794     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] OnGetColorSpace end, colorSpace = %{public}u",
795         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
796     *errCode = 0;
797     return static_cast<uint32_t>(colorSpace);
798 }
799 
800 /** @note @window.hierarchy */
SetRaiseByClickEnabled(bool enable)801 int32_t CJWindowImpl::SetRaiseByClickEnabled(bool enable)
802 {
803     ResWindow result = CheckWindow();
804     if (result.ret != 0) {
805         return result.ret;
806     }
807     sptr<Window> weakWindow = result.nativeWindow;
808     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetRaiseByClickEnabled(enable));
809     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set raiseEnabled end",
810         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
811     return static_cast<int32_t>(ret);
812 }
813 
814 /** @note @window.hierarchy */
RaiseAboveTarget(int32_t windowId)815 int32_t CJWindowImpl::RaiseAboveTarget(int32_t windowId)
816 {
817     ResWindow result = CheckWindow();
818     if (result.ret != 0) {
819         return result.ret;
820     }
821     sptr<Window> weakWindow = result.nativeWindow;
822     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->RaiseAboveTarget(windowId));
823     return static_cast<int32_t>(ret);
824 }
825 
Translate(double x,double y,double z)826 int32_t CJWindowImpl::Translate(double x, double y, double z)
827 {
828     if (windowToken_ == nullptr) {
829         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
830         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
831     }
832     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
833         TLOGE(WmsLogTag::WMS_DIALOG, "Translate is not allowed since window is not system window");
834         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
835     }
836     auto trans = windowToken_->GetTransform();
837     trans.translateX_ = x;
838     trans.translateY_ = y;
839     trans.translateZ_ = z;
840     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTransform(trans));
841     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] Translate end," \
842         "translateX = %{public}f, translateY = %{public}f, translateZ = %{public}f",
843         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(),
844         trans.translateX_, trans.translateY_, trans.translateZ_);
845     return static_cast<int32_t>(ret);
846 }
847 
IsPivotValid(double data)848 static bool IsPivotValid(double data)
849 {
850     if (MathHelper::LessNotEqual(data, 0.0) || (MathHelper::GreatNotEqual(data, 1.0))) {
851         return false;
852     }
853     return true;
854 }
855 
Rotate(double x,double y,double z,double pivotX,double pivotY)856 int32_t CJWindowImpl::Rotate(double x, double y, double z, double pivotX, double pivotY)
857 {
858     if (windowToken_ == nullptr) {
859         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
860         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
861     }
862     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
863         TLOGE(WmsLogTag::WMS_DIALOG, "Translate is not allowed since window is not system window");
864         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
865     }
866     if (!IsPivotValid(pivotX) || !IsPivotValid(pivotY)) {
867         TLOGE(WmsLogTag::WMS_DIALOG, " PivotX or PivotY should between 0.0 ~ 1.0");
868         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
869     }
870     auto trans = windowToken_->GetTransform();
871     trans.translateX_ = x;
872     trans.translateY_ = y;
873     trans.translateZ_ = z;
874     trans.pivotX_ = pivotX;
875     trans.pivotY_ = pivotY;
876     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTransform(trans));
877     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] Rotate end",
878         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
879     return static_cast<int32_t>(ret);
880 }
881 
IsScaleValid(double data)882 static bool IsScaleValid(double data)
883 {
884     if (!MathHelper::GreatNotEqual(data, 0.0)) {
885         return false;
886     }
887     return true;
888 }
889 
Scale(double x,double y,double pivotX,double pivotY)890 int32_t CJWindowImpl::Scale(double x, double y, double pivotX, double pivotY)
891 {
892     if (windowToken_ == nullptr) {
893         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
894         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
895     }
896     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
897         TLOGE(WmsLogTag::WMS_DIALOG, "Translate is not allowed since window is not system window");
898         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
899     }
900     if (!IsPivotValid(pivotX) || !IsPivotValid(pivotY) || !IsScaleValid(x) || !IsScaleValid(y)) {
901         TLOGE(WmsLogTag::WMS_DIALOG, " PivotX or PivotY should between 0.0 ~ 1.0, scale should greater than 0.0");
902         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
903     }
904     auto trans = windowToken_->GetTransform();
905     trans.pivotX_ = pivotX;
906     trans.pivotY_ = pivotY;
907     trans.scaleX_ = x;
908     trans.scaleY_ = y;
909     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTransform(trans));
910     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] Scale end",
911         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
912     return static_cast<int32_t>(ret);
913 }
914 
Opacity(double opacity)915 int32_t CJWindowImpl::Opacity(double opacity)
916 {
917     if (windowToken_ == nullptr) {
918         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
919         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
920     }
921     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
922         TLOGE(WmsLogTag::WMS_DIALOG, "Translate is not allowed since window is not system window");
923         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
924     }
925     if (!IsPivotValid(opacity)) {
926         TLOGE(WmsLogTag::WMS_DIALOG, "opacity should greater than 0 or smaller than 1.0");
927         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
928     }
929     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetAlpha(opacity));
930     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] Opacity end, alpha = %{public}f",
931         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), opacity);
932     return static_cast<int32_t>(ret);
933 }
934 
Snapshot(int32_t * errCode)935 std::shared_ptr<Media::PixelMap> CJWindowImpl::Snapshot(int32_t* errCode)
936 {
937     if (windowToken_ == nullptr) {
938         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
939         *errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
940         return nullptr;
941     }
942     std::shared_ptr<Media::PixelMap> pixelMap = windowToken_->Snapshot();
943     if (pixelMap == nullptr) {
944         TLOGE(WmsLogTag::WMS_DIALOG, "window snapshot get pixelmap is null");
945         *errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
946         return nullptr;
947     }
948     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] OnSnapshot, WxH=%{public}dx%{public}d",
949         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(),
950         pixelMap->GetWidth(), pixelMap->GetHeight());
951     *errCode = 0;
952     return pixelMap;
953 }
954 
GetColorFromJs(const std::string & colorStr,uint32_t defaultColor,bool & flag)955 static uint32_t GetColorFromJs(const std::string& colorStr, uint32_t defaultColor, bool& flag)
956 {
957     std::regex pattern("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$");
958     if (!std::regex_match(colorStr, pattern)) {
959         TLOGE(WmsLogTag::WMS_DIALOG, "Invalid color input");
960         return defaultColor;
961     }
962     std::string color = colorStr.substr(1);
963     if (color.length() == RGB_LENGTH) {
964         color = "FF" + color; // ARGB
965     }
966     flag = true;
967     std::stringstream ss;
968     uint32_t hexColor;
969     ss << std::hex << color;
970     ss >> hexColor;
971     TLOGI(WmsLogTag::WMS_DIALOG, "Origin %{public}s, process %{public}s, final %{public}x",
972         colorStr.c_str(), color.c_str(), hexColor);
973     return hexColor;
974 }
975 
UpdateSystemBarProperties(std::map<WindowType,SystemBarProperty> & systemBarProperties,const std::map<WindowType,SystemBarPropertyFlag> & systemBarPropertyFlags,sptr<Window> weakToken)976 static void UpdateSystemBarProperties(std::map<WindowType, SystemBarProperty>& systemBarProperties,
977     const std::map<WindowType, SystemBarPropertyFlag>& systemBarPropertyFlags, sptr<Window> weakToken)
978 {
979     for (auto it : systemBarPropertyFlags) {
980         WindowType type = it.first;
981         SystemBarPropertyFlag flag = it.second;
982         auto property = weakToken->GetSystemBarPropertyByType(type);
983         if (flag.enableFlag == false) {
984             systemBarProperties[type].enable_ = property.enable_;
985         }
986         if (flag.backgroundColorFlag == false) {
987             systemBarProperties[type].backgroundColor_ = property.backgroundColor_;
988         }
989         if (flag.contentColorFlag == false) {
990             systemBarProperties[type].contentColor_ = property.contentColor_;
991         }
992     }
993 }
994 
SetBarPropertyMap(std::map<WindowType,SystemBarProperty> & properties,std::map<WindowType,SystemBarPropertyFlag> & propertyFlags,const CBarProperties & cProperties,sptr<Window> nativeWindow)995 void SetBarPropertyMap(
996     std::map<WindowType, SystemBarProperty>& properties,
997     std::map<WindowType, SystemBarPropertyFlag>& propertyFlags,
998     const CBarProperties& cProperties,
999     sptr<Window> nativeWindow)
1000 {
1001     auto statusProperty = nativeWindow->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR);
1002     auto navProperty = nativeWindow->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_NAVIGATION_BAR);
1003     properties[WindowType::WINDOW_TYPE_STATUS_BAR] = statusProperty;
1004     properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = navProperty;
1005     propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR] = SystemBarPropertyFlag();
1006     propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = SystemBarPropertyFlag();
1007     properties[WindowType::WINDOW_TYPE_STATUS_BAR].backgroundColor_ = GetColorFromJs(cProperties.statusBarColor,
1008         statusProperty.backgroundColor_, propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].backgroundColorFlag);
1009     properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].backgroundColor_ =
1010         GetColorFromJs(cProperties.navigationBarColor, navProperty.backgroundColor_,
1011             propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].backgroundColorFlag);
1012     if (!cProperties.statusBarContentColor.empty()) {
1013         properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ =
1014             GetColorFromJs(cProperties.statusBarContentColor, statusProperty.contentColor_,
1015                 propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].contentColorFlag);
1016     } else {
1017         if (cProperties.isStatusBarLightIcon) {
1018             properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ = SYSTEM_COLOR_WHITE;
1019         } else {
1020             properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ = SYSTEM_COLOR_BLACK;
1021         }
1022         propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].contentColorFlag = true;
1023     }
1024     if (!cProperties.navigationBarContentColor.empty()) {
1025         properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ =
1026             GetColorFromJs(cProperties.navigationBarContentColor, navProperty.contentColor_,
1027                 propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColorFlag);
1028     } else {
1029         if (cProperties.isNavigationBarLightIcon) {
1030             properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = SYSTEM_COLOR_WHITE;
1031         } else {
1032             properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = SYSTEM_COLOR_BLACK;
1033         }
1034         propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColorFlag = true;
1035     }
1036 }
1037 
SetWindowSystemBarProperties(const CBarProperties & cProperties)1038 int32_t CJWindowImpl::SetWindowSystemBarProperties(const CBarProperties& cProperties)
1039 {
1040     if (windowToken_ == nullptr) {
1041         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
1042         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1043     }
1044     std::map<WindowType, SystemBarProperty> properties;
1045     std::map<WindowType, SystemBarPropertyFlag> propertyFlags;
1046     SetBarPropertyMap(properties, propertyFlags, cProperties, windowToken_);
1047     UpdateSystemBarProperties(properties, propertyFlags, windowToken_);
1048     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetSystemBarProperty(
1049         WindowType::WINDOW_TYPE_STATUS_BAR, properties.at(WindowType::WINDOW_TYPE_STATUS_BAR)));
1050     if (ret != WmErrorCode::WM_OK) {
1051         return static_cast<int32_t>(ret);
1052     }
1053     ret = WM_JS_TO_ERROR_CODE_MAP.at(
1054         windowToken_->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
1055             properties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR)));
1056     return static_cast<int32_t>(ret);
1057 }
1058 
SetWindowSystemBarEnable(char ** arr,uint32_t size)1059 int32_t CJWindowImpl::SetWindowSystemBarEnable(char** arr, uint32_t size)
1060 {
1061     if (windowToken_ == nullptr) {
1062         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
1063         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1064     }
1065     std::map<WindowType, SystemBarProperty> systemBarProperties;
1066     std::map<WindowType, SystemBarPropertyFlag> systemBarPropertyFlags;
1067     auto statusProperty = windowToken_->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR);
1068     auto navProperty = windowToken_->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_NAVIGATION_BAR);
1069     statusProperty.enable_ = false;
1070     navProperty.enable_ = false;
1071     systemBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR] = statusProperty;
1072     systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = navProperty;
1073     systemBarPropertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR] = SystemBarPropertyFlag();
1074     systemBarPropertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = SystemBarPropertyFlag();
1075     for (uint32_t i = 0; i < size; i++) {
1076         std::string name = arr[i];
1077         if (name.compare("status") == 0) {
1078             systemBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR].enable_ = true;
1079         } else if (name.compare("navigation") == 0) {
1080             systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enable_ = true;
1081         }
1082     }
1083     systemBarPropertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].enableFlag = true;
1084     systemBarPropertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enableFlag = true;
1085     UpdateSystemBarProperties(systemBarProperties, systemBarPropertyFlags, windowToken_);
1086     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetSystemBarProperty(
1087         WindowType::WINDOW_TYPE_STATUS_BAR, systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR)));
1088     if (ret != WmErrorCode::WM_OK) {
1089         return static_cast<int32_t>(ret);
1090     }
1091     ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
1092         systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR)));
1093     return static_cast<int32_t>(ret);
1094 }
1095 
OnRegisterWindowCallback(const std::string & type,int64_t funcId)1096 int32_t CJWindowImpl::OnRegisterWindowCallback(const std::string& type, int64_t funcId)
1097 {
1098     if (windowToken_ == nullptr) {
1099         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
1100         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1101     }
1102     WmErrorCode ret = registerManager_->RegisterListener(windowToken_, type, CaseType::CASE_WINDOW, funcId);
1103     if (ret != WmErrorCode::WM_OK) {
1104         TLOGE(WmsLogTag::WMS_DIALOG, "Register failed, window [%{public}u, %{public}s] type: %{public}s",
1105             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), type.c_str());
1106     }
1107     return static_cast<int32_t>(ret);
1108 }
1109 
UnregisterWindowCallback(const std::string & type,int64_t funcId)1110 int32_t CJWindowImpl::UnregisterWindowCallback(const std::string& type, int64_t funcId)
1111 {
1112     if (windowToken_ == nullptr) {
1113         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
1114         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1115     }
1116     WmErrorCode ret = registerManager_->UnregisterListener(windowToken_, type, CaseType::CASE_WINDOW, funcId);
1117     if (ret != WmErrorCode::WM_OK) {
1118         TLOGE(WmsLogTag::WMS_DIALOG, "Unregister failed, window [%{public}u, %{public}s] type: %{public}s",
1119             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), type.c_str());
1120     }
1121     return static_cast<int32_t>(ret);
1122 }
1123 }
1124 }
1125