• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_impl.h"
17 #include <regex>
18 #include <sstream>
19 #include "permission.h"
20 #include "securec.h"
21 #include "ui_content.h"
22 #include "window_helper.h"
23 #include "window_manager_hilog.h"
24 #include "window_utils.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29 constexpr Rect g_emptyRect = {0, 0, 0, 0};
30 }
31 static thread_local std::map<std::string, sptr<CJWindowImpl>> g_cjWindowMap;
32 std::recursive_mutex g_mutex;
33 constexpr int32_t MIN_DECOR_HEIGHT = 37;
34 constexpr int32_t MAX_DECOR_HEIGHT = 112;
35 constexpr int32_t MIN_GRAY_SCALE = 0.0;
36 constexpr int32_t MAX_GRAY_SCALE = 1.0;
37 
38 const std::vector<MaximizePresentation> MAXIMIZE_PRESENTATION = {
39     MaximizePresentation::FOLLOW_APP_IMMERSIVE_SETTING,
40     MaximizePresentation::EXIT_IMMERSIVE,
41     MaximizePresentation::ENTER_IMMERSIVE};
42 const std::vector<SpecificSystemBar> SYSTEM_BAR = {
43     SpecificSystemBar::STATUS, SpecificSystemBar::NAVIGATION,
44     SpecificSystemBar::NAVIGATION_INDICATOR};
45 static int g_ctorCnt = 0;
46 
FindCjWindowObject(const std::string & windowName)47 sptr<CJWindowImpl> FindCjWindowObject(const std::string& windowName)
48 {
49     TLOGD(WmsLogTag::WMS_DIALOG, "Try to find window %{public}s in g_cjWindowMap", windowName.c_str());
50     std::lock_guard<std::recursive_mutex> lock(g_mutex);
51     if (g_cjWindowMap.find(windowName) == g_cjWindowMap.end()) {
52         TLOGD(WmsLogTag::WMS_DIALOG, "Can not find window %{public}s in g_cjWindowMap", windowName.c_str());
53         return nullptr;
54     }
55     return g_cjWindowMap[windowName];
56 }
57 
CreateCjWindowObject(sptr<Window> & window)58 sptr<CJWindowImpl> CreateCjWindowObject(sptr<Window>& window)
59 {
60     if (window == nullptr) {
61         TLOGI(WmsLogTag::WMS_DIALOG, "Invalid input");
62         return nullptr;
63     }
64     const std::string& windowName = window->GetWindowName();
65     sptr<CJWindowImpl> windowImpl = FindCjWindowObject(windowName);
66     if (windowImpl != nullptr) {
67         TLOGI(WmsLogTag::WMS_DIALOG, "FindCjWindowObject %{public}s", windowName.c_str());
68         return windowImpl;
69     }
70 
71     windowImpl = FFI::FFIData::Create<CJWindowImpl>(window);
72     if (windowImpl == nullptr) {
73         TLOGI(WmsLogTag::WMS_DIALOG, "Failed to create window %{public}s", windowName.c_str());
74         return nullptr;
75     }
76     std::lock_guard<std::recursive_mutex> lock(g_mutex);
77     g_cjWindowMap[windowName] = windowImpl;
78     return windowImpl;
79 }
80 
CJWindowImpl(sptr<Window> ptr)81 CJWindowImpl::CJWindowImpl(sptr<Window> ptr)
82     : windowToken_(ptr), registerManager_(std::make_unique<CjWindowRegisterManager>())
83 {
84     NotifyNativeWinDestroyFunc func = [](std::string windowName) {
85         std::lock_guard<std::recursive_mutex> lock(g_mutex);
86         if (windowName.empty() || g_cjWindowMap.count(windowName) == 0) {
87             TLOGE(WmsLogTag::WMS_DIALOG, "Can not find window %{public}s ", windowName.c_str());
88             return;
89         }
90         g_cjWindowMap.erase(windowName);
91         TLOGI(WmsLogTag::WMS_DIALOG, "Destroy window %{public}s in js window", windowName.c_str());
92     };
93     if (windowToken_ == nullptr) {
94         TLOGI(WmsLogTag::WMS_DIALOG, "constructe failed");
95         return;
96     }
97     windowToken_->RegisterWindowDestroyedListener(func);
98     TLOGI(WmsLogTag::WMS_DIALOG, " constructorCnt: %{public}d", ++g_ctorCnt);
99 }
100 
GetWindowToken()101 sptr<Window> CJWindowImpl::GetWindowToken()
102 {
103     return windowToken_;
104 }
105 
CheckWindow()106 ResWindow CJWindowImpl::CheckWindow()
107 {
108     ResWindow result;
109     result.ret = static_cast<int32_t>(WmErrorCode::WM_OK);
110     result.nativeWindow = nullptr;
111     if (windowToken_ == nullptr) {
112         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
113         result.ret = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
114         return result;
115     }
116     result.nativeWindow = windowToken_;
117     return result;
118 }
119 
Hide()120 int32_t CJWindowImpl::Hide()
121 {
122     ResWindow result = CheckWindow();
123     if (result.ret != 0) {
124         return result.ret;
125     }
126     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(result.nativeWindow->Hide(0, false, false));
127     TLOGI(WmsLogTag::WMS_DIALOG,
128         "Window [%{public}u] hide end, ret=%{public}d", result.nativeWindow->GetWindowId(), ret);
129     return static_cast<int32_t>(ret);
130 }
131 
HideWithAnimation()132 int32_t CJWindowImpl::HideWithAnimation()
133 {
134     WmErrorCode errCode = WmErrorCode::WM_OK;
135     if (windowToken_ != nullptr) {
136         auto winType = windowToken_->GetType();
137         if (!WindowHelper::IsSystemWindow(winType)) {
138             TLOGE(WmsLogTag::WMS_DIALOG, "window Type %{public}u is not supported", static_cast<uint32_t>(winType));
139             errCode = WmErrorCode::WM_ERROR_INVALID_CALLING;
140         }
141     } else {
142         errCode = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
143     }
144     if (errCode != WmErrorCode::WM_OK) {
145         return static_cast<int32_t>(errCode);
146     }
147     errCode = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->Hide(0, true, false));
148     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, ret=%{public}d",
149         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), errCode);
150     return static_cast<int32_t>(errCode);
151 }
152 
ShowWindow()153 int32_t CJWindowImpl::ShowWindow()
154 {
155     ResWindow result = CheckWindow();
156     if (result.ret != 0) {
157         return result.ret;
158     }
159     sptr<Window> weakWindow = result.nativeWindow;
160     if (weakWindow == nullptr) {
161         TLOGE(WmsLogTag::WMS_DIALOG, "window is nullptr");
162         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
163     }
164     WMError ret = weakWindow->Show(0, false);
165     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] show with ret=%{public}d",
166         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
167     return static_cast<int32_t>(WM_JS_TO_ERROR_CODE_MAP.at(ret));
168 }
169 
ShowWithAnimation()170 int32_t CJWindowImpl::ShowWithAnimation()
171 {
172     WmErrorCode errCode = WmErrorCode::WM_OK;
173     if (windowToken_ == nullptr) {
174         errCode = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
175     } else {
176         auto winType = windowToken_->GetType();
177         if (!WindowHelper::IsSystemWindow(winType)) {
178             TLOGE(WmsLogTag::WMS_DIALOG, "window Type %{public}u is not supported", static_cast<uint32_t>(winType));
179             errCode = WmErrorCode::WM_ERROR_INVALID_CALLING;
180         }
181     }
182     if (errCode != WmErrorCode::WM_OK) {
183         return static_cast<int32_t>(errCode);
184     }
185     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->Show(0, true));
186     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, ret=%{public}d",
187         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
188     return static_cast<int32_t>(ret);
189 }
190 
DestroyWindow()191 int32_t CJWindowImpl::DestroyWindow()
192 {
193     ResWindow result = CheckWindow();
194     if (result.ret != 0) {
195         return result.ret;
196     }
197     sptr<Window> weakWindow = result.nativeWindow;
198     WMError ret = weakWindow->Destroy();
199     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] destroy end, ret=%{public}d",
200         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
201     windowToken_ = nullptr;
202     return static_cast<int32_t>(ret);
203 }
204 
MoveWindowTo(int32_t x,int32_t y)205 int32_t CJWindowImpl::MoveWindowTo(int32_t x, int32_t y)
206 {
207     ResWindow result = CheckWindow();
208     if (result.ret != 0) {
209         return result.ret;
210     }
211     sptr<Window> weakWindow = result.nativeWindow;
212     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->MoveTo(x, y));
213     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] move end, ret=%{public}d",
214         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
215     return static_cast<int32_t>(ret);
216 }
217 
Resize(uint32_t width,uint32_t height)218 int32_t CJWindowImpl::Resize(uint32_t width, uint32_t height)
219 {
220     if (width == 0 || height == 0) {
221         TLOGE(WmsLogTag::WMS_DIALOG, "width or height should greater than 0!");
222         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
223     }
224     ResWindow result = CheckWindow();
225     if (result.ret != 0) {
226         return result.ret;
227     }
228     sptr<Window> weakWindow = result.nativeWindow;
229     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->Resize(width, height));
230     TLOGD(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] resize end, ret=%{public}d",
231         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
232     return static_cast<int32_t>(ret);
233 }
234 
SetWindowMode(uint32_t mode)235 int32_t CJWindowImpl::SetWindowMode(uint32_t mode)
236 {
237     if (windowToken_ == nullptr) {
238         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
239         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
240     }
241     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
242         TLOGE(WmsLogTag::WMS_DIALOG, "set window mode permission denied!");
243         return static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
244     }
245     WindowMode winMode = CJ_TO_NATIVE_WINDOW_MODE_MAP.at(static_cast<ApiWindowMode>(mode));
246     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetWindowMode(winMode));
247     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] set type end, ret=%{public}d",
248         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
249     return static_cast<int32_t>(ret);
250 }
251 
GetWindowProperties(int32_t * errCode)252 CWindowProperties CJWindowImpl::GetWindowProperties(int32_t* errCode)
253 {
254     CWindowProperties wp;
255     if (windowToken_ == nullptr) {
256         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
257         *errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
258         return wp;
259     }
260     Rect drawableRect = g_emptyRect;
261     auto uicontent = windowToken_->GetUIContent();
262     if (uicontent == nullptr) {
263         TLOGW(WmsLogTag::WMS_DIALOG, "uicontent is nullptr");
264     } else {
265         uicontent->GetAppPaintSize(drawableRect);
266     }
267     wp.drawableRect.posX = drawableRect.posX_;
268     wp.drawableRect.posY = drawableRect.posY_;
269     wp.drawableRect.height = drawableRect.height_;
270     wp.drawableRect.width = drawableRect.width_;
271     Rect rect = windowToken_->GetRect();
272     wp.windowRect.posX = rect.posX_;
273     wp.windowRect.posY = rect.posY_;
274     wp.windowRect.height = rect.height_;
275     wp.windowRect.width = rect.width_;
276     WindowType type = windowToken_->GetType();
277     if (type == WindowType::WINDOW_TYPE_APP_MAIN_WINDOW) {
278         wp.type = static_cast<uint32_t>(type);
279     } else if (CJ_TO_WINDOW_TYPE_MAP.count(type) != 0) {
280         wp.type = static_cast<uint32_t>(CJ_TO_WINDOW_TYPE_MAP.at(type));
281     } else {
282         wp.type = static_cast<uint32_t>(type);
283     }
284     wp.isFullScreen = windowToken_->IsFullScreen();
285     wp.isLayoutFullScreen = windowToken_->IsLayoutFullScreen();
286     wp.focusable = windowToken_->GetFocusable();
287     wp.touchable = windowToken_->GetTouchable();
288     wp.brightness = windowToken_->GetBrightness();
289     wp.isKeepScreenOn = windowToken_->IsKeepScreenOn();
290     wp.isPrivacyMode = windowToken_->IsPrivacyMode();
291     wp.isRoundCorner = false;
292     wp.isTransparent = windowToken_->IsTransparent();
293     wp.id = windowToken_->GetWindowId();
294     *errCode = 0;
295     return wp;
296 }
297 
SetWindowLayoutFullScreen(bool isLayoutFullScreen)298 int32_t CJWindowImpl::SetWindowLayoutFullScreen(bool isLayoutFullScreen)
299 {
300     ResWindow result = CheckWindow();
301     if (result.ret != 0) {
302         return result.ret;
303     }
304     sptr<Window> weakWindow = result.nativeWindow;
305     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetLayoutFullScreen(isLayoutFullScreen));
306     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, ret=%{public}d",
307         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
308     return static_cast<int32_t>(ret);
309 }
310 
SetWindowBackgroundColor(const char * color)311 int32_t CJWindowImpl::SetWindowBackgroundColor(const char* color)
312 {
313     if (windowToken_ == nullptr) {
314         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
315         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
316     }
317     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBackgroundColor(color));
318     if (ret == WmErrorCode::WM_OK) {
319         TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end",
320             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
321     }
322     return static_cast<int32_t>(ret);
323 }
324 
SetWindowBrightness(float brightness)325 int32_t CJWindowImpl::SetWindowBrightness(float brightness)
326 {
327     ResWindow result = CheckWindow();
328     if (result.ret != 0) {
329         return result.ret;
330     }
331     sptr<Window> weakWindow = result.nativeWindow;
332     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetBrightness(brightness));
333     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end",
334         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
335     return static_cast<int32_t>(ret);
336 }
337 
SetBackdropBlurStyle(uint32_t blurStyle)338 int32_t CJWindowImpl::SetBackdropBlurStyle(uint32_t blurStyle)
339 {
340     if (windowToken_ == nullptr) {
341         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
342         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
343     }
344     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
345         TLOGE(WmsLogTag::WMS_DIALOG, "Not allowed since window is not system window");
346         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
347     }
348     if (blurStyle > static_cast<uint32_t>(WindowBlurStyle::WINDOW_BLUR_THICK)) {
349         TLOGE(WmsLogTag::WMS_DIALOG, "Invalid window blur style");
350         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
351     }
352     WmErrorCode ret =
353         WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBackdropBlurStyle(static_cast<WindowBlurStyle>(blurStyle)));
354     if (ret != WmErrorCode::WM_OK) {
355         TLOGE(WmsLogTag::WMS_DIALOG, "failed");
356     } else {
357         TLOGI(WmsLogTag::WMS_DIALOG,
358             "Window [%{public}u, %{public}s] end, blurStyle=%{public}u",
359             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), blurStyle);
360     }
361     return static_cast<int32_t>(ret);
362 }
363 
SetPreferredOrientation(uint32_t orientation)364 int32_t CJWindowImpl::SetPreferredOrientation(uint32_t orientation)
365 {
366     if (windowToken_ == nullptr) {
367         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
368         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
369     }
370     auto winOrientation = static_cast<Orientation>(orientation);
371     if (winOrientation < Orientation::UNSPECIFIED || winOrientation > Orientation::LOCKED) {
372         TLOGE(WmsLogTag::WMS_DIALOG, "Orientation %{public}u invalid!", orientation);
373         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
374     }
375 
376     windowToken_->SetRequestedOrientation(winOrientation);
377     TLOGI(WmsLogTag::WMS_DIALOG,
378         "Window [%{public}u, %{public}s] end, orientation=%{public}u",
379         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(),
380         static_cast<uint32_t>(winOrientation));
381     return static_cast<int32_t>(WmErrorCode::WM_OK);
382 }
383 
SetWindowFocusable(bool focusable)384 int32_t CJWindowImpl::SetWindowFocusable(bool focusable)
385 {
386     ResWindow result = CheckWindow();
387     if (result.ret != 0) {
388         return result.ret;
389     }
390     sptr<Window> weakWindow = result.nativeWindow;
391     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetFocusable(focusable));
392     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end",
393         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
394     return static_cast<int32_t>(ret);
395 }
396 
SetWindowKeepScreenOn(bool keepScreenOn)397 int32_t CJWindowImpl::SetWindowKeepScreenOn(bool keepScreenOn)
398 {
399     ResWindow result = CheckWindow();
400     if (result.ret != 0) {
401         return result.ret;
402     }
403     sptr<Window> weakWindow = result.nativeWindow;
404     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetKeepScreenOn(keepScreenOn));
405     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end",
406         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
407     return static_cast<int32_t>(ret);
408 }
409 
GetWindowAvoidArea(uint32_t areaType,CAvoidArea * retPtr)410 int32_t CJWindowImpl::GetWindowAvoidArea(uint32_t areaType, CAvoidArea* retPtr)
411 {
412     if (retPtr == nullptr) {
413         TLOGE(WmsLogTag::WMS_DIALOG, "Invalid input");
414         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
415     }
416     if (windowToken_ == nullptr) {
417         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
418         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
419     }
420     AvoidAreaType avoidAreaType = static_cast<AvoidAreaType>(areaType);
421     if (avoidAreaType >= AvoidAreaType::TYPE_END) {
422         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
423     }
424     AvoidArea avoidArea;
425     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->GetAvoidAreaByType(avoidAreaType, avoidArea));
426     retPtr->visible = avoidAreaType == AvoidAreaType::TYPE_CUTOUT ? false : true;
427     if (ret != WmErrorCode::WM_OK) {
428         retPtr->topRect = g_emptyRect;
429         retPtr->leftRect = g_emptyRect;
430         retPtr->rightRect = g_emptyRect;
431         retPtr->bottomRect = g_emptyRect;
432     } else {
433         retPtr->topRect = avoidArea.topRect_;
434         retPtr->leftRect = avoidArea.leftRect_;
435         retPtr->rightRect = avoidArea.rightRect_;
436         retPtr->bottomRect = avoidArea.bottomRect_;
437     }
438     return static_cast<int32_t>(ret);
439 }
440 
SetWindowPrivacyMode(bool isPrivacyMode)441 int32_t CJWindowImpl::SetWindowPrivacyMode(bool isPrivacyMode)
442 {
443     ResWindow result = CheckWindow();
444     if (result.ret != 0) {
445         return result.ret;
446     }
447     sptr<Window> weakWindow = result.nativeWindow;
448     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetPrivacyMode(isPrivacyMode));
449     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, mode=%{public}u",
450         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), isPrivacyMode);
451     return static_cast<int32_t>(ret);
452 }
453 
SetWindowTouchable(bool touchable)454 int32_t CJWindowImpl::SetWindowTouchable(bool touchable)
455 {
456     ResWindow result = CheckWindow();
457     if (result.ret != 0) {
458         return result.ret;
459     }
460     sptr<Window> weakWindow = result.nativeWindow;
461     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetTouchable(touchable));
462     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end",
463         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
464     return static_cast<int32_t>(ret);
465 }
466 
SetForbidSplitMove(bool isForbidSplitMove)467 int32_t CJWindowImpl::SetForbidSplitMove(bool isForbidSplitMove)
468 {
469     ResWindow result = CheckWindow();
470     if (result.ret != 0) {
471         return result.ret;
472     }
473     sptr<Window> weakWindow = result.nativeWindow;
474     WmErrorCode ret;
475     if (isForbidSplitMove) {
476         ret = WM_JS_TO_ERROR_CODE_MAP.at(
477             weakWindow->AddWindowFlag(WindowFlag::WINDOW_FLAG_FORBID_SPLIT_MOVE));
478     } else {
479         ret = WM_JS_TO_ERROR_CODE_MAP.at(
480             weakWindow->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_FORBID_SPLIT_MOVE));
481     }
482     return static_cast<int32_t>(ret);
483 }
484 
IsWindowSupportWideGamut(int32_t * errCode)485 bool CJWindowImpl::IsWindowSupportWideGamut(int32_t* errCode)
486 {
487     if (windowToken_ == nullptr) {
488         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
489         *errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
490         return false;
491     }
492     bool flag = windowToken_->IsSupportWideGamut();
493     *errCode = 0;
494     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, ret=%{public}u",
495         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), flag);
496     return flag;
497 }
498 
IsWindowShowing(int32_t * errCode)499 bool CJWindowImpl::IsWindowShowing(int32_t* errCode)
500 {
501     if (windowToken_ == nullptr) {
502         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
503         *errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
504         return false;
505     }
506     bool state = (windowToken_->GetWindowState() == WindowState::STATE_SHOWN);
507     *errCode = 0;
508     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] get show state end, state=%{public}u",
509         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), state);
510     return state;
511 }
512 
SetWaterMarkFlag(bool enable)513 int32_t CJWindowImpl::SetWaterMarkFlag(bool enable)
514 {
515     if (windowToken_ == nullptr) {
516         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
517         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
518     }
519     WMError ret;
520     if (enable) {
521         ret = windowToken_->AddWindowFlag(WindowFlag::WINDOW_FLAG_WATER_MARK);
522     } else {
523         ret = windowToken_->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_WATER_MARK);
524     }
525     if (ret != WMError::WM_OK) {
526         TLOGE(WmsLogTag::WMS_DIALOG, "failed");
527     } else {
528         TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, ret=%{public}d",
529             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
530     }
531     return static_cast<int32_t>(ret);
532 }
533 
SetShadowRadius(double radius)534 int32_t CJWindowImpl::SetShadowRadius(double radius)
535 {
536     if (windowToken_ == nullptr) {
537         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
538         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
539     }
540     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
541         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
542     }
543     if (MathHelper::LessNotEqual(radius, 0.0)) {
544         TLOGE(WmsLogTag::WMS_DIALOG, "invalid radius");
545         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
546     }
547     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowRadius(radius));
548     if (ret != WmErrorCode::WM_OK) {
549         TLOGE(WmsLogTag::WMS_DIALOG, "failed");
550     }
551     return static_cast<int32_t>(ret);
552 }
553 
SetShadowColor(std::string color)554 int32_t CJWindowImpl::SetShadowColor(std::string color)
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_->SetShadowColor(color));
564     if (ret != WmErrorCode::WM_OK) {
565         TLOGE(WmsLogTag::WMS_DIALOG, "failed");
566     }
567     return static_cast<int32_t>(ret);
568 }
569 
SetShadowOffsetX(double offsetX)570 int32_t CJWindowImpl::SetShadowOffsetX(double offsetX)
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_->SetShadowOffsetX(offsetX));
580     if (ret != WmErrorCode::WM_OK) {
581         TLOGE(WmsLogTag::WMS_DIALOG, "failed");
582     }
583     return static_cast<int32_t>(ret);
584 }
585 
SetShadowOffsetY(double offsetY)586 int32_t CJWindowImpl::SetShadowOffsetY(double offsetY)
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         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
594     }
595     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetShadowOffsetY(offsetY));
596     if (ret != WmErrorCode::WM_OK) {
597         TLOGE(WmsLogTag::WMS_DIALOG, "failed");
598     }
599     return static_cast<int32_t>(ret);
600 }
601 
SetBackdropBlur(double radius)602 int32_t CJWindowImpl::SetBackdropBlur(double radius)
603 {
604     if (windowToken_ == nullptr) {
605         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
606         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
607     }
608     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
609         TLOGE(WmsLogTag::WMS_DIALOG, "not allowed since window is not system window");
610         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
611     }
612     if (MathHelper::LessNotEqual(radius, 0.0)) {
613         TLOGE(WmsLogTag::WMS_DIALOG, "invalid radius");
614         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
615     }
616     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBackdropBlur(radius));
617     if (ret != WmErrorCode::WM_OK) {
618         TLOGE(WmsLogTag::WMS_DIALOG, "failed");
619     } else {
620         TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, radius=%{public}f",
621             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), radius);
622     }
623     return static_cast<int32_t>(ret);
624 }
625 
SetBlur(double radius)626 int32_t CJWindowImpl::SetBlur(double radius)
627 {
628     if (windowToken_ == nullptr) {
629         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
630         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
631     }
632     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
633         TLOGE(WmsLogTag::WMS_DIALOG, "not allowed since window is not system window");
634         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
635     }
636     if (MathHelper::LessNotEqual(radius, 0.0)) {
637         TLOGE(WmsLogTag::WMS_DIALOG, "invalid radius");
638         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
639     }
640     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetBlur(radius));
641     if (ret != WmErrorCode::WM_OK) {
642         TLOGE(WmsLogTag::WMS_DIALOG, "failed");
643     } else {
644         TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, radius=%{public}f",
645             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), radius);
646     }
647     return static_cast<int32_t>(ret);
648 }
649 
SetAspectRatio(double ratio)650 int32_t CJWindowImpl::SetAspectRatio(double ratio)
651 {
652     if (windowToken_ == nullptr) {
653         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
654         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
655     }
656     if (!WindowHelper::IsMainWindow(windowToken_->GetType())) {
657         TLOGE(WmsLogTag::WMS_DIALOG, "not allowed since window is not main window");
658         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
659     }
660     if (ratio <= 0.0) {
661         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
662     }
663     WMError ret = windowToken_->SetAspectRatio(ratio);
664     if (ret != WMError::WM_OK) {
665         TLOGE(WmsLogTag::WMS_DIALOG, "failed");
666     } else {
667         TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, ret=%{public}d",
668             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
669     }
670     return static_cast<int32_t>(ret);
671 }
672 
ResetAspectRatio()673 int32_t CJWindowImpl::ResetAspectRatio()
674 {
675     if (windowToken_ == nullptr) {
676         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
677         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
678     }
679     if (!WindowHelper::IsMainWindow(windowToken_->GetType())) {
680         TLOGE(WmsLogTag::WMS_DIALOG, "not allowed since window is not main window");
681         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
682     }
683     WMError ret = windowToken_->ResetAspectRatio();
684     if (ret != WMError::WM_OK) {
685         TLOGE(WmsLogTag::WMS_DIALOG, "failed");
686     } else {
687         TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] reset aspect ratio end, ret=%{public}d",
688             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
689     }
690     return static_cast<int32_t>(ret);
691 }
692 
SetWindowColorSpace(uint32_t colorSpace)693 int32_t CJWindowImpl::SetWindowColorSpace(uint32_t colorSpace)
694 {
695     ResWindow result = CheckWindow();
696     if (result.ret != 0) {
697         return result.ret;
698     }
699     sptr<Window> weakWindow = result.nativeWindow;
700     weakWindow->SetColorSpace(ColorSpace(colorSpace));
701     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, colorSpace=%{public}u",
702         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
703     return 0;
704 }
705 
Minimize()706 int32_t CJWindowImpl::Minimize()
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 (WindowHelper::IsSubWindow(windowToken_->GetType())) {
713         TLOGE(WmsLogTag::WMS_DIALOG, "subWindow hide");
714         return Hide();
715     }
716     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->Minimize());
717     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, ret=%{public}d",
718         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
719     return static_cast<int32_t>(ret);
720 }
721 
SetCornerRadius(float radius)722 int32_t CJWindowImpl::SetCornerRadius(float radius)
723 {
724     if (windowToken_ == nullptr) {
725         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
726         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
727     }
728     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
729         TLOGE(WmsLogTag::WMS_DIALOG, "set corner radius permission denied!");
730         return static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
731     }
732     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
733         TLOGE(WmsLogTag::WMS_DIALOG, "not allowed since window is not system window");
734         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
735     }
736     if (MathHelper::LessNotEqual(radius, 0.0)) {
737         TLOGE(WmsLogTag::WMS_DIALOG, "invalid radius");
738         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
739     }
740     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetCornerRadius(radius));
741     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, radius=%{public}f",
742         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), radius);
743     return static_cast<int32_t>(ret);
744 }
745 
SetResizeByDragEnabled(bool enable)746 int32_t CJWindowImpl::SetResizeByDragEnabled(bool enable)
747 {
748     ResWindow result = CheckWindow();
749     if (result.ret != 0) {
750         return result.ret;
751     }
752     sptr<Window> weakWindow = result.nativeWindow;
753     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetResizeByDragEnabled(enable));
754     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end",
755         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
756     return static_cast<int32_t>(ret);
757 }
758 
759 /** @note @window.hierarchy */
RaiseToAppTop()760 int32_t CJWindowImpl::RaiseToAppTop()
761 {
762     ResWindow result = CheckWindow();
763     if (result.ret != 0) {
764         return result.ret;
765     }
766     sptr<Window> weakWindow = result.nativeWindow;
767     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->RaiseToAppTop());
768     TLOGI(WmsLogTag::WMS_HIERARCHY, "Window [%{public}u, %{public}s] zorder raise success",
769         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
770     return static_cast<int32_t>(ret);
771 }
772 
SetSnapshotSkip(bool isSkip)773 int32_t CJWindowImpl::SetSnapshotSkip(bool isSkip)
774 {
775     ResWindow result = CheckWindow();
776     if (result.ret != 0) {
777         return result.ret;
778     }
779     sptr<Window> weakWindow = result.nativeWindow;
780     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetSnapshotSkip(isSkip));
781     TLOGI(WmsLogTag::WMS_DIALOG, "[%{public}u, %{public}s] end",
782         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
783     return static_cast<int32_t>(ret);
784 }
785 
SetWakeUpScreen(bool wakeUp)786 int32_t CJWindowImpl::SetWakeUpScreen(bool wakeUp)
787 {
788     if (windowToken_ == nullptr) {
789         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
790         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
791     }
792     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
793         TLOGE(WmsLogTag::WMS_DIALOG, "permission denied!");
794         return static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
795     }
796     windowToken_->SetTurnScreenOn(wakeUp);
797     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] screen %{public}d end",
798         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), wakeUp);
799     return 0;
800 }
801 
GetWindowColorSpace(int32_t * errCode)802 uint32_t CJWindowImpl::GetWindowColorSpace(int32_t* errCode)
803 {
804     if (windowToken_ == nullptr) {
805         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
806         *errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
807         return 0;
808     }
809     ColorSpace colorSpace = windowToken_->GetColorSpace();
810     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, colorSpace=%{public}u",
811         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
812     *errCode = 0;
813     return static_cast<uint32_t>(colorSpace);
814 }
815 
816 /** @note @window.hierarchy */
SetRaiseByClickEnabled(bool enable)817 int32_t CJWindowImpl::SetRaiseByClickEnabled(bool enable)
818 {
819     ResWindow result = CheckWindow();
820     if (result.ret != 0) {
821         return result.ret;
822     }
823     sptr<Window> weakWindow = result.nativeWindow;
824     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetRaiseByClickEnabled(enable));
825     TLOGI(WmsLogTag::WMS_HIERARCHY, "Window [%{public}u, %{public}s] end",
826         weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
827     return static_cast<int32_t>(ret);
828 }
829 
830 /** @note @window.hierarchy */
RaiseAboveTarget(int32_t windowId)831 int32_t CJWindowImpl::RaiseAboveTarget(int32_t windowId)
832 {
833     ResWindow result = CheckWindow();
834     if (result.ret != 0) {
835         return result.ret;
836     }
837     sptr<Window> weakWindow = result.nativeWindow;
838     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->RaiseAboveTarget(windowId));
839     return static_cast<int32_t>(ret);
840 }
841 
Translate(double x,double y,double z)842 int32_t CJWindowImpl::Translate(double x, double y, double z)
843 {
844     if (windowToken_ == nullptr) {
845         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
846         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
847     }
848     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
849         TLOGE(WmsLogTag::WMS_DIALOG, "not allowed since window is not system window");
850         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
851     }
852     auto trans = windowToken_->GetTransform();
853     trans.translateX_ = x;
854     trans.translateY_ = y;
855     trans.translateZ_ = z;
856     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTransform(trans));
857     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end, "
858         "translateX=%{public}f, translateY=%{public}f, translateZ=%{public}f",
859         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(),
860         trans.translateX_, trans.translateY_, trans.translateZ_);
861     return static_cast<int32_t>(ret);
862 }
863 
IsPivotValid(double data)864 static bool IsPivotValid(double data)
865 {
866     if (MathHelper::LessNotEqual(data, 0.0) || (MathHelper::GreatNotEqual(data, 1.0))) {
867         return false;
868     }
869     return true;
870 }
871 
Rotate(double x,double y,double z,double pivotX,double pivotY)872 int32_t CJWindowImpl::Rotate(double x, double y, double z, double pivotX, double pivotY)
873 {
874     if (windowToken_ == nullptr) {
875         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
876         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
877     }
878     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
879         TLOGE(WmsLogTag::WMS_DIALOG, "not allowed since window is not system window");
880         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
881     }
882     if (!IsPivotValid(pivotX) || !IsPivotValid(pivotY)) {
883         TLOGE(WmsLogTag::WMS_DIALOG, " PivotX or PivotY should between 0.0 ~ 1.0");
884         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
885     }
886     auto trans = windowToken_->GetTransform();
887     trans.translateX_ = x;
888     trans.translateY_ = y;
889     trans.translateZ_ = z;
890     trans.pivotX_ = pivotX;
891     trans.pivotY_ = pivotY;
892     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTransform(trans));
893     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end",
894         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
895     return static_cast<int32_t>(ret);
896 }
897 
IsScaleValid(double data)898 static bool IsScaleValid(double data)
899 {
900     if (!MathHelper::GreatNotEqual(data, 0.0)) {
901         return false;
902     }
903     return true;
904 }
905 
Scale(double x,double y,double pivotX,double pivotY)906 int32_t CJWindowImpl::Scale(double x, double y, double pivotX, double pivotY)
907 {
908     if (windowToken_ == nullptr) {
909         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
910         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
911     }
912     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
913         TLOGE(WmsLogTag::WMS_DIALOG, "not allowed since window is not system window");
914         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
915     }
916     if (!IsPivotValid(pivotX) || !IsPivotValid(pivotY) || !IsScaleValid(x) || !IsScaleValid(y)) {
917         TLOGE(WmsLogTag::WMS_DIALOG, " PivotX or PivotY should between 0.0 ~ 1.0, scale should greater than 0.0");
918         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
919     }
920     auto trans = windowToken_->GetTransform();
921     trans.pivotX_ = pivotX;
922     trans.pivotY_ = pivotY;
923     trans.scaleX_ = x;
924     trans.scaleY_ = y;
925     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetTransform(trans));
926     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] end",
927         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
928     return static_cast<int32_t>(ret);
929 }
930 
Opacity(double opacity)931 int32_t CJWindowImpl::Opacity(double opacity)
932 {
933     if (windowToken_ == nullptr) {
934         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
935         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
936     }
937     if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
938         TLOGE(WmsLogTag::WMS_DIALOG, "not allowed since window is not system window");
939         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
940     }
941     if (!IsPivotValid(opacity)) {
942         TLOGE(WmsLogTag::WMS_DIALOG, "opacity should greater than 0 or smaller than 1.0");
943         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
944     }
945     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetAlpha(opacity));
946     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] Opacity end, alpha=%{public}f",
947         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), opacity);
948     return static_cast<int32_t>(ret);
949 }
950 
Snapshot(int32_t * errCode)951 std::shared_ptr<Media::PixelMap> CJWindowImpl::Snapshot(int32_t* errCode)
952 {
953     if (windowToken_ == nullptr) {
954         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
955         *errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
956         return nullptr;
957     }
958     std::shared_ptr<Media::PixelMap> pixelMap = windowToken_->Snapshot();
959     if (pixelMap == nullptr) {
960         TLOGE(WmsLogTag::WMS_DIALOG, "get pixelmap is null");
961         *errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
962         return nullptr;
963     }
964     TLOGI(WmsLogTag::WMS_DIALOG, "Window [%{public}u, %{public}s] OnSnapshot, WxH=%{public}dx%{public}d",
965         windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(),
966         pixelMap->GetWidth(), pixelMap->GetHeight());
967     *errCode = 0;
968     return pixelMap;
969 }
970 
GetColorFromJs(const std::string & colorStr,uint32_t defaultColor,bool & flag)971 static uint32_t GetColorFromJs(const std::string& colorStr, uint32_t defaultColor, bool& flag)
972 {
973     std::regex pattern("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$");
974     if (!std::regex_match(colorStr, pattern)) {
975         TLOGE(WmsLogTag::WMS_DIALOG, "Invalid color input");
976         return defaultColor;
977     }
978     std::string color = colorStr.substr(1);
979     if (color.length() == RGB_LENGTH) {
980         color = "FF" + color; // ARGB
981     }
982     flag = true;
983     std::stringstream ss;
984     uint32_t hexColor;
985     ss << std::hex << color;
986     ss >> hexColor;
987     TLOGI(WmsLogTag::WMS_DIALOG, "Origin %{public}s, process %{public}s, final %{public}x",
988         colorStr.c_str(), color.c_str(), hexColor);
989     return hexColor;
990 }
991 
UpdateSystemBarProperties(std::map<WindowType,SystemBarProperty> & systemBarProperties,const std::map<WindowType,SystemBarPropertyFlag> & systemBarPropertyFlags,sptr<Window> weakToken)992 static void UpdateSystemBarProperties(std::map<WindowType, SystemBarProperty>& systemBarProperties,
993     const std::map<WindowType, SystemBarPropertyFlag>& systemBarPropertyFlags, sptr<Window> weakToken)
994 {
995     for (auto it : systemBarPropertyFlags) {
996         WindowType type = it.first;
997         SystemBarPropertyFlag flag = it.second;
998         auto property = weakToken->GetSystemBarPropertyByType(type);
999         if (flag.enableFlag == false) {
1000             systemBarProperties[type].enable_ = property.enable_;
1001         } else {
1002             systemBarProperties[type].settingFlag_ = static_cast<SystemBarSettingFlag>(
1003                 static_cast<uint32_t>(systemBarProperties[type].settingFlag_) |
1004                 static_cast<uint32_t>(SystemBarSettingFlag::ENABLE_SETTING));
1005         }
1006         if (flag.backgroundColorFlag == false) {
1007             systemBarProperties[type].backgroundColor_ = property.backgroundColor_;
1008         }
1009         if (flag.contentColorFlag == false) {
1010             systemBarProperties[type].contentColor_ = property.contentColor_;
1011         }
1012         if (flag.backgroundColorFlag || flag.contentColorFlag) {
1013             systemBarProperties[type].settingFlag_ = static_cast<SystemBarSettingFlag>(
1014                 static_cast<uint32_t>(systemBarProperties[type].settingFlag_) |
1015                 static_cast<uint32_t>(SystemBarSettingFlag::COLOR_SETTING));
1016         }
1017     }
1018 }
1019 
SetBarPropertyMap(std::map<WindowType,SystemBarProperty> & properties,std::map<WindowType,SystemBarPropertyFlag> & propertyFlags,const CBarProperties & cProperties,sptr<Window> nativeWindow)1020 void SetBarPropertyMap(
1021     std::map<WindowType, SystemBarProperty>& properties,
1022     std::map<WindowType, SystemBarPropertyFlag>& propertyFlags,
1023     const CBarProperties& cProperties,
1024     sptr<Window> nativeWindow)
1025 {
1026     auto statusProperty = nativeWindow->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR);
1027     auto navProperty = nativeWindow->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_NAVIGATION_BAR);
1028     properties[WindowType::WINDOW_TYPE_STATUS_BAR] = statusProperty;
1029     properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = navProperty;
1030     propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR] = SystemBarPropertyFlag();
1031     propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = SystemBarPropertyFlag();
1032     properties[WindowType::WINDOW_TYPE_STATUS_BAR].backgroundColor_ = GetColorFromJs(cProperties.statusBarColor,
1033         statusProperty.backgroundColor_, propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].backgroundColorFlag);
1034     properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].backgroundColor_ =
1035         GetColorFromJs(cProperties.navigationBarColor, navProperty.backgroundColor_,
1036             propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].backgroundColorFlag);
1037     properties[WindowType::WINDOW_TYPE_STATUS_BAR].enableAnimation_ = cProperties.enableStatusBarAnimation;
1038     properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enableAnimation_ = cProperties.enableNavigationBarAnimation;
1039     if (!cProperties.statusBarContentColor.empty()) {
1040         properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ =
1041             GetColorFromJs(cProperties.statusBarContentColor, statusProperty.contentColor_,
1042                 propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].contentColorFlag);
1043     } else {
1044         if (cProperties.isStatusBarLightIcon) {
1045             properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ = SYSTEM_COLOR_WHITE;
1046         } else {
1047             properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ = SYSTEM_COLOR_BLACK;
1048         }
1049         propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].contentColorFlag = true;
1050     }
1051     if (!cProperties.navigationBarContentColor.empty()) {
1052         properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ =
1053             GetColorFromJs(cProperties.navigationBarContentColor, navProperty.contentColor_,
1054                 propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColorFlag);
1055     } else {
1056         if (cProperties.isNavigationBarLightIcon) {
1057             properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = SYSTEM_COLOR_WHITE;
1058         } else {
1059             properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = SYSTEM_COLOR_BLACK;
1060         }
1061         propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColorFlag = true;
1062     }
1063 }
1064 
SetWindowSystemBarProperties(const CBarProperties & cProperties)1065 int32_t CJWindowImpl::SetWindowSystemBarProperties(const CBarProperties& cProperties)
1066 {
1067     if (windowToken_ == nullptr) {
1068         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
1069         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1070     }
1071     std::map<WindowType, SystemBarProperty> properties;
1072     std::map<WindowType, SystemBarPropertyFlag> propertyFlags;
1073     SetBarPropertyMap(properties, propertyFlags, cProperties, windowToken_);
1074     UpdateSystemBarProperties(properties, propertyFlags, windowToken_);
1075     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetSystemBarProperty(
1076         WindowType::WINDOW_TYPE_STATUS_BAR, properties.at(WindowType::WINDOW_TYPE_STATUS_BAR)));
1077     if (ret != WmErrorCode::WM_OK) {
1078         return static_cast<int32_t>(ret);
1079     }
1080     ret = WM_JS_TO_ERROR_CODE_MAP.at(
1081         windowToken_->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
1082             properties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR)));
1083     return static_cast<int32_t>(ret);
1084 }
1085 
SetWindowSystemBarEnable(char ** arr,uint32_t size)1086 int32_t CJWindowImpl::SetWindowSystemBarEnable(char** arr, uint32_t size)
1087 {
1088     if (windowToken_ == nullptr) {
1089         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
1090         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1091     }
1092     std::map<WindowType, SystemBarProperty> systemBarProperties;
1093     std::map<WindowType, SystemBarPropertyFlag> systemBarPropertyFlags;
1094     auto statusProperty = windowToken_->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR);
1095     auto navProperty = windowToken_->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_NAVIGATION_BAR);
1096     statusProperty.enable_ = false;
1097     navProperty.enable_ = false;
1098     systemBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR] = statusProperty;
1099     systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = navProperty;
1100     systemBarPropertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR] = SystemBarPropertyFlag();
1101     systemBarPropertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = SystemBarPropertyFlag();
1102     for (uint32_t i = 0; i < size; i++) {
1103         std::string name = arr[i];
1104         if (name.compare("status") == 0) {
1105             systemBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR].enable_ = true;
1106         } else if (name.compare("navigation") == 0) {
1107             systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enable_ = true;
1108         }
1109     }
1110     systemBarPropertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].enableFlag = true;
1111     systemBarPropertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enableFlag = true;
1112     UpdateSystemBarProperties(systemBarProperties, systemBarPropertyFlags, windowToken_);
1113     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetSystemBarProperty(
1114         WindowType::WINDOW_TYPE_STATUS_BAR, systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR)));
1115     if (ret != WmErrorCode::WM_OK) {
1116         return static_cast<int32_t>(ret);
1117     }
1118     ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
1119         systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR)));
1120     return static_cast<int32_t>(ret);
1121 }
1122 
OnRegisterWindowCallback(const std::string & type,int64_t funcId,int64_t parameter)1123 int32_t CJWindowImpl::OnRegisterWindowCallback(const std::string& type, int64_t funcId, int64_t parameter)
1124 {
1125     if (windowToken_ == nullptr) {
1126         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
1127         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1128     }
1129     WmErrorCode ret = registerManager_->RegisterListener(windowToken_, type, CaseType::CASE_WINDOW, funcId, parameter);
1130     if (ret != WmErrorCode::WM_OK) {
1131         TLOGE(WmsLogTag::WMS_DIALOG, "Register failed, window [%{public}u, %{public}s] type: %{public}s",
1132             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), type.c_str());
1133     }
1134     return static_cast<int32_t>(ret);
1135 }
1136 
UnregisterWindowCallback(const std::string & type,int64_t funcId)1137 int32_t CJWindowImpl::UnregisterWindowCallback(const std::string& type, int64_t funcId)
1138 {
1139     if (windowToken_ == nullptr) {
1140         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
1141         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1142     }
1143     WmErrorCode ret = registerManager_->UnregisterListener(windowToken_, type, CaseType::CASE_WINDOW, funcId);
1144     if (ret != WmErrorCode::WM_OK) {
1145         TLOGE(WmsLogTag::WMS_DIALOG, "Unregister failed, window [%{public}u, %{public}s] type: %{public}s",
1146             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), type.c_str());
1147     }
1148     return static_cast<int32_t>(ret);
1149 }
1150 
SetSubWindowModal(bool isModal)1151 int32_t CJWindowImpl::SetSubWindowModal(bool isModal)
1152 {
1153     if (windowToken_ == nullptr) {
1154         TLOGE(WmsLogTag::WMS_SUB, "WindowToken_ is nullptr");
1155         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1156     }
1157 
1158     if (!WindowHelper::IsSubWindow(windowToken_->GetType())) {
1159         TLOGE(WmsLogTag::WMS_SUB, "Invalid Window Type");
1160         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
1161     }
1162 
1163     ModalityType modalityType = ModalityType::WINDOW_MODALITY;
1164     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
1165         windowToken_->SetSubWindowModal(isModal, modalityType));
1166     if (ret != WmErrorCode::WM_OK) {
1167         TLOGE(WmsLogTag::WMS_SUB, "Set subWindow modal failed, window [%{public}u, %{public}s]",
1168             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1169     }
1170     return static_cast<int32_t>(ret);
1171 }
1172 
SetWindowLimits(const CWindowLimits & cWindowLimits,CWindowLimits & retPtr)1173 int32_t CJWindowImpl::SetWindowLimits(const CWindowLimits& cWindowLimits,
1174                                       CWindowLimits& retPtr)
1175 {
1176     ResWindow result = CheckWindow();
1177     if (result.ret != 0) {
1178         return result.ret;
1179     }
1180     sptr<Window> weakWindow = result.nativeWindow;
1181 
1182     WindowLimits windowLimits;
1183     windowLimits.maxWidth_ = cWindowLimits.maxWidth;
1184     windowLimits.maxHeight_ = cWindowLimits.maxHeight;
1185     windowLimits.minWidth_ = cWindowLimits.minWidth;
1186     windowLimits.minHeight_ = cWindowLimits.minHeight;
1187 
1188     if (windowLimits.maxWidth_ < 0 || windowLimits.maxHeight_ < 0 ||
1189         windowLimits.minWidth_ < 0 || windowLimits.minHeight_ < 0) {
1190         TLOGE(WmsLogTag::WMS_LAYOUT, "Width or height should be greatr or equal to 0");
1191         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
1192     }
1193 
1194     WindowLimits sizeLimits(windowLimits);
1195     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetWindowLimits(sizeLimits));
1196     if (ret != WmErrorCode::WM_OK) {
1197         TLOGE(WmsLogTag::WMS_LAYOUT, "Set window limits failed, window [%{public}u, %{public}s]",
1198             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1199     }
1200 
1201     retPtr.maxWidth = sizeLimits.maxWidth_;
1202     retPtr.maxHeight = sizeLimits.maxHeight_;
1203     retPtr.minWidth = sizeLimits.minWidth_;
1204     retPtr.maxHeight = sizeLimits.minHeight_;
1205     return static_cast<int32_t>(ret);
1206 }
1207 
GetWindowLimits(CWindowLimits & retPtr)1208 int32_t CJWindowImpl::GetWindowLimits(CWindowLimits& retPtr)
1209 {
1210     if (windowToken_ == nullptr) {
1211         TLOGE(WmsLogTag::WMS_LAYOUT, "WindowToken_ is nullptr");
1212         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1213     }
1214 
1215     WindowLimits windowLimits;
1216     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->GetWindowLimits(windowLimits));
1217     if (ret != WmErrorCode::WM_OK) {
1218         TLOGE(WmsLogTag::WMS_LAYOUT, "Get Window limits failed, window [%{public}u, %{public}s]",
1219               windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1220     }
1221     retPtr.maxWidth = windowLimits.maxWidth_;
1222     retPtr.maxHeight = windowLimits.maxHeight_;
1223     retPtr.minWidth = windowLimits.minWidth_;
1224     retPtr.maxHeight = windowLimits.minHeight_;
1225     return static_cast<int32_t>(ret);
1226 }
1227 
GetImmersiveModeEnabledState(int32_t & errCode)1228 bool CJWindowImpl::GetImmersiveModeEnabledState(int32_t& errCode)
1229 {
1230     if (windowToken_ == nullptr) {
1231         TLOGE(WmsLogTag::WMS_IMMS, "WindowToken_ is nullptr");
1232         errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1233         return false;
1234     }
1235 
1236     bool ret = windowToken_->GetImmersiveModeEnabledState();
1237     errCode = 0;
1238     return ret;
1239 }
1240 
SetImmersiveModeEnabledState(bool enabled)1241 int32_t CJWindowImpl::SetImmersiveModeEnabledState(bool enabled)
1242 {
1243     if (windowToken_ == nullptr) {
1244         TLOGE(WmsLogTag::WMS_IMMS, "WindowToken_ is nullptr");
1245         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1246     }
1247 
1248     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetImmersiveModeEnabledState(enabled));
1249     if (ret != WmErrorCode::WM_OK) {
1250         TLOGE(WmsLogTag::WMS_IMMS, "Set immersive mode failed, window [%{public}u, %{public}s]",
1251             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1252     }
1253     return static_cast<int32_t>(ret);
1254 }
1255 
KeepKeyboardOnFocus(bool keepKeyboardFlag)1256 int32_t CJWindowImpl::KeepKeyboardOnFocus(bool keepKeyboardFlag)
1257 {
1258     if (windowToken_ == nullptr) {
1259         TLOGE(WmsLogTag::WMS_KEYBOARD, "WindowToken_ is nullptr");
1260         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1261     }
1262 
1263     if (!WindowHelper::IsSystemWindow(windowToken_->GetType()) &&
1264         !WindowHelper::IsSubWindow(windowToken_->GetType())) {
1265         TLOGE(WmsLogTag::WMS_KEYBOARD,
1266             "KeepkeyboardOnFocus is not allowed since window is not system window or app subwindow");
1267         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
1268     }
1269 
1270     WmErrorCode ret = windowToken_->KeepKeyboardOnFocus(keepKeyboardFlag);
1271     if (ret != WmErrorCode::WM_OK) {
1272         TLOGE(WmsLogTag::WMS_KEYBOARD, "Keep keyboard on focus failed, window [%{public}u, %{public}s]",
1273             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1274     }
1275     return static_cast<int32_t>(ret);
1276 }
1277 
GetWindowDecorHeight(int32_t & height)1278 int32_t CJWindowImpl::GetWindowDecorHeight(int32_t& height)
1279 {
1280     ResWindow result = CheckWindow();
1281     if (result.ret != 0) {
1282         return result.ret;
1283     }
1284     sptr<Window> weakWindow = result.nativeWindow;
1285 
1286     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->GetDecorHeight(height));
1287     if (ret != WmErrorCode::WM_OK) {
1288         TLOGE(WmsLogTag::WMS_DECOR, "Get window decor height failed, window [%{public}u, %{public}s]",
1289             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1290     }
1291 
1292     return static_cast<int32_t>(ret);
1293 }
1294 
SetWindowDecorHeight(int32_t height)1295 int32_t CJWindowImpl::SetWindowDecorHeight(int32_t height)
1296 {
1297     if (windowToken_ == nullptr) {
1298         TLOGE(WmsLogTag::WMS_DECOR, "WindowToken_ is nullptr");
1299         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1300     }
1301 
1302     if (height < MIN_DECOR_HEIGHT || height > MAX_DECOR_HEIGHT) {
1303         TLOGE(WmsLogTag::WMS_DECOR, "height should be greater than 37 or smaller than 112");
1304         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
1305     }
1306 
1307     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetDecorHeight(height));
1308     if (ret != WmErrorCode::WM_OK) {
1309         TLOGE(WmsLogTag::WMS_DECOR, "Set window decor height failed, window [%{public}u, %{public}s]",
1310             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1311     }
1312 
1313     return static_cast<int32_t>(ret);
1314 }
1315 
Recover()1316 int32_t CJWindowImpl::Recover()
1317 {
1318     ResWindow result = CheckWindow();
1319     if (result.ret != 0) {
1320         return result.ret;
1321     }
1322     sptr<Window> weakWindow = result.nativeWindow;
1323 
1324     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->Recover());
1325     if (ret != WmErrorCode::WM_OK) {
1326         TLOGE(WmsLogTag::WMS_DIALOG, "Recover failed, window [%{public}u, %{public}s]",
1327             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1328     }
1329 
1330     return static_cast<int32_t>(ret);
1331 }
1332 
SetWindowDecorVisible(bool isVisible)1333 int32_t CJWindowImpl::SetWindowDecorVisible(bool isVisible)
1334 {
1335     if (windowToken_ == nullptr) {
1336         TLOGE(WmsLogTag::WMS_DECOR, "WindowToken_ is nullptr");
1337         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1338     }
1339 
1340     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetDecorVisible(isVisible));
1341     if (ret != WmErrorCode::WM_OK) {
1342         TLOGE(WmsLogTag::WMS_DECOR, "Set window decor visible failed, window [%{public}u, %{public}s]",
1343             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1344     }
1345 
1346     return static_cast<int32_t>(ret);
1347 }
1348 
GetTitleButtonRect(CTitleButtonRect & retPtr)1349 int32_t CJWindowImpl::GetTitleButtonRect(CTitleButtonRect& retPtr)
1350 {
1351     if (windowToken_ == nullptr) {
1352         TLOGE(WmsLogTag::WMS_DECOR, "WindowToken_ is nullptr");
1353         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1354     }
1355 
1356     TitleButtonRect titleButtonRect;
1357     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->GetTitleButtonArea(titleButtonRect));
1358     if (ret != WmErrorCode::WM_OK) {
1359         TLOGE(WmsLogTag::WMS_DECOR, "Get window title button rect failed, window [%{public}u, %{public}s]",
1360             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1361     }
1362     retPtr.right = titleButtonRect.posX_;
1363     retPtr.top = titleButtonRect.posY_;
1364     retPtr.width = titleButtonRect.width_;
1365     retPtr.height = titleButtonRect.height_;
1366     return static_cast<int32_t>(ret);
1367 }
1368 
SetDialogBackGestureEnabled(bool enabled)1369 int32_t CJWindowImpl::SetDialogBackGestureEnabled(bool enabled)
1370 {
1371     if (windowToken_ == nullptr) {
1372         TLOGE(WmsLogTag::WMS_DIALOG, "WindowToken_ is nullptr");
1373         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1374     }
1375 
1376     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->SetDialogBackGestureEnabled(enabled));
1377     if (ret != WmErrorCode::WM_OK) {
1378         TLOGE(WmsLogTag::WMS_DIALOG, "Set dialog backgesture failed, window [%{public}u, %{public}s]",
1379             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1380     }
1381 
1382     return static_cast<int32_t>(ret);
1383 }
1384 
DisableLandscapeMultiWindow()1385 int32_t CJWindowImpl::DisableLandscapeMultiWindow()
1386 {
1387     ResWindow result = CheckWindow();
1388     if (result.ret != 0) {
1389         return result.ret;
1390     }
1391     sptr<Window> weakWindow = result.nativeWindow;
1392 
1393     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetLandscapeMultiWindow(false));
1394     if (ret != WmErrorCode::WM_OK) {
1395         TLOGE(WmsLogTag::WMS_MULTI_WINDOW, "Disable landscape multi window failed, window [%{public}u, %{public}s]",
1396             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1397     }
1398 
1399     return static_cast<int32_t>(ret);
1400 }
1401 
EnableLandscapeMultiWindow()1402 int32_t CJWindowImpl::EnableLandscapeMultiWindow()
1403 {
1404     ResWindow result = CheckWindow();
1405     if (result.ret != 0) {
1406         return result.ret;
1407     }
1408     sptr<Window> weakWindow = result.nativeWindow;
1409 
1410     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetLandscapeMultiWindow(true));
1411     if (ret != WmErrorCode::WM_OK) {
1412         TLOGE(WmsLogTag::WMS_MULTI_WINDOW, "Enable landscape multi window failed, window [%{public}u, %{public}s]",
1413             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1414     }
1415 
1416     return static_cast<int32_t>(ret);
1417 }
1418 
SetWindowGrayScale(float grayScale)1419 int32_t CJWindowImpl::SetWindowGrayScale(float grayScale)
1420 {
1421     ResWindow result = CheckWindow();
1422     if (result.ret != 0) {
1423         return result.ret;
1424     }
1425     sptr<Window> weakWindow = result.nativeWindow;
1426     constexpr float eps = 1e-6;
1427     if (grayScale < (MIN_GRAY_SCALE - eps) || grayScale > (MAX_GRAY_SCALE + eps)) {
1428         TLOGE(WmsLogTag::WMS_ATTRIBUTE,
1429             "graysclae should be greater than or equal to 0.0 or smaller than or equal to 1.0");
1430         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
1431     }
1432 
1433     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetGrayScale(grayScale));
1434     if (ret != WmErrorCode::WM_OK) {
1435         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "Set window grayscale failed, window [%{public}u, %{public}s]",
1436               weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1437     }
1438 
1439     return static_cast<int32_t>(ret);
1440 }
1441 
GetPreferredOrientation(int32_t & errCode)1442 uint32_t CJWindowImpl::GetPreferredOrientation(int32_t& errCode)
1443 {
1444     if (windowToken_ == nullptr) {
1445         TLOGE(WmsLogTag::DEFAULT, "WindowToken_ is nullptr");
1446         errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1447         return 0;
1448     }
1449 
1450     Orientation orientation = windowToken_->GetRequestedOrientation();
1451     if (orientation < Orientation::UNSPECIFIED || orientation > Orientation::LOCKED) {
1452         TLOGE(WmsLogTag::DEFAULT, "Orientation is invalid");
1453         errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
1454         return static_cast<uint32_t>(orientation);
1455     }
1456 
1457     return static_cast<uint32_t>(orientation);
1458 }
1459 
GetWindowStatus(int32_t & errCode)1460 int32_t CJWindowImpl::GetWindowStatus(int32_t& errCode)
1461 {
1462     if (windowToken_ == nullptr) {
1463         TLOGE(WmsLogTag::WMS_PC, "WindowToken_ is nullptr");
1464         errCode = static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1465         return 0;
1466     }
1467 
1468     WindowStatus windowStatus;
1469     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowToken_->GetWindowStatus(windowStatus));
1470     errCode = static_cast<int32_t>(ret);
1471     if (ret != WmErrorCode::WM_OK) {
1472         TLOGE(WmsLogTag::WMS_PC, "Get window status failed, window [%{public}u, %{public}s]",
1473             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1474     }
1475 
1476     return static_cast<int32_t>(windowStatus);
1477 }
1478 
GetHexColor(uint32_t color)1479 static std::string GetHexColor(uint32_t color)
1480 {
1481     const int32_t rgbaLength = 8;
1482 
1483     std::stringstream ioss;
1484     std::string temp;
1485     ioss << std::setiosflags(std::ios::uppercase) << std::hex << color;
1486     ioss >> temp;
1487     int count = rgbaLength - static_cast<int>(temp.length());
1488     std::string finalColor("#");
1489     std::string tmpColor(count, '0');
1490     tmpColor += temp;
1491     finalColor += tmpColor;
1492 
1493     return finalColor;
1494 }
1495 
CopyToHeap(const std::string & str)1496 static char* CopyToHeap(const std::string& str)
1497 {
1498     char* heapStr = new char[str.length() + 1];
1499     errno_t err = strcpy_s(heapStr, str.length() + 1, str.c_str());
1500     if (err != EOK) {
1501         delete[] heapStr;
1502         return nullptr;
1503     }
1504     return heapStr;
1505 }
1506 
GetWindowSystemBarProperties(CJBarProperties & retPtr)1507 int32_t CJWindowImpl::GetWindowSystemBarProperties(CJBarProperties& retPtr)
1508 {
1509     ResWindow result = CheckWindow();
1510     if (result.ret != 0) {
1511         return result.ret;
1512     }
1513     sptr<Window> weakWindow = result.nativeWindow;
1514 
1515     SystemBarProperty status = weakWindow->GetSystemBarPropertyByType(
1516         WindowType::WINDOW_TYPE_STATUS_BAR);
1517     SystemBarProperty navi = weakWindow->GetSystemBarPropertyByType(
1518         WindowType::WINDOW_TYPE_NAVIGATION_BAR);
1519 
1520     retPtr.statusBarColor = CopyToHeap(GetHexColor(status.backgroundColor_));
1521     retPtr.statusBarContentColor = CopyToHeap(GetHexColor(status.contentColor_));
1522     retPtr.isStatusBarLightIcon = status.contentColor_ == SYSTEM_COLOR_WHITE;
1523     retPtr.navigationBarColor = CopyToHeap(GetHexColor(navi.backgroundColor_));
1524     retPtr.navigationBarContentColor = CopyToHeap(GetHexColor(navi.contentColor_));
1525     retPtr.isNavigationBarLightIcon = navi.contentColor_ == SYSTEM_COLOR_WHITE;
1526     retPtr.enableStatusBarAnimation = status.enableAnimation_;
1527     retPtr.enableNavigationBarAnimation = navi.enableAnimation_;
1528     return result.ret;
1529 }
1530 
SpecificSystemBarEnabled(int32_t name,bool enable,bool enableAnimation)1531 int32_t CJWindowImpl::SpecificSystemBarEnabled(int32_t name, bool enable,
1532                                                bool enableAnimation)
1533 {
1534     ResWindow result = CheckWindow();
1535     if (result.ret != 0) {
1536         return result.ret;
1537     }
1538     sptr<Window> weakWindow = result.nativeWindow;
1539     std::map<WindowType, SystemBarProperty> systemBarProperties;
1540     if (name < 0 || name >= static_cast<int32_t>(SYSTEM_BAR.size())) {
1541         TLOGE(WmsLogTag::WMS_IMMS, "Invalid input");
1542         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
1543     }
1544     SpecificSystemBar barName = SYSTEM_BAR[name];
1545     if (barName == SpecificSystemBar::STATUS) {
1546         systemBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR].enable_ = enable;
1547         systemBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR].enableAnimation_ = enableAnimation;
1548     } else if (barName == SpecificSystemBar::NAVIGATION) {
1549         systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enable_ = enable;
1550         systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enableAnimation_ = enableAnimation;
1551     } else if (barName == SpecificSystemBar::NAVIGATION_INDICATOR) {
1552         systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR].enable_ = enable;
1553         systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR].enableAnimation_ = enableAnimation;
1554     }
1555 
1556     WmErrorCode ret = WmErrorCode::WM_OK;
1557     if (barName == SpecificSystemBar::STATUS) {
1558         ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetSpecificBarProperty(
1559             WindowType::WINDOW_TYPE_STATUS_BAR,
1560             systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR)));
1561     } else if (barName == SpecificSystemBar::NAVIGATION) {
1562         ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetSpecificBarProperty(
1563             WindowType::WINDOW_TYPE_NAVIGATION_BAR,
1564             systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR)));
1565     } else if (barName == SpecificSystemBar::NAVIGATION_INDICATOR) {
1566         ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetSpecificBarProperty(
1567             WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR,
1568             systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR)));
1569     }
1570 
1571     if (ret != WmErrorCode::WM_OK) {
1572         TLOGE(WmsLogTag::WMS_IMMS, "Get window status failed, window [%{public}u, %{public}s]",
1573               weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
1574     }
1575 
1576     return static_cast<int32_t>(ret);
1577 }
1578 
Maximize(int32_t presentation)1579 int32_t CJWindowImpl::Maximize(int32_t presentation)
1580 {
1581     if (windowToken_ == nullptr) {
1582         TLOGE(WmsLogTag::WMS_LAYOUT_PC, "WindowToken_ is nullptr");
1583         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1584     }
1585 
1586     if (!WindowHelper::IsSubWindow(windowToken_->GetType())) {
1587         TLOGE(WmsLogTag::WMS_LAYOUT_PC, "subwindow hide");
1588         Hide();
1589         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1590     }
1591     if (presentation < 0 || presentation >= static_cast<int32_t>(MAXIMIZE_PRESENTATION.size())) {
1592         TLOGE(WmsLogTag::WMS_LAYOUT_PC, "Invalid input");
1593         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
1594     }
1595     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(
1596         windowToken_->Maximize(MAXIMIZE_PRESENTATION[presentation]));
1597     if (ret != WmErrorCode::WM_OK) {
1598         TLOGE(WmsLogTag::WMS_LAYOUT_PC, "maximize window failed, window [%{public}u, %{public}s] ",
1599             windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
1600     }
1601     return static_cast<int32_t>(ret);
1602 }
1603 
CreateSubWindowWithOptions(std::string name,int64_t & windowId,CSubWindowOptions option)1604 int32_t CJWindowImpl::CreateSubWindowWithOptions(std::string name,
1605                                                  int64_t &windowId,
1606                                                  CSubWindowOptions option)
1607 {
1608     if (windowToken_ == nullptr) {
1609         TLOGE(WmsLogTag::WMS_SUB, "WindowToken_ is nullptr");
1610         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1611     }
1612 
1613     if (!windowToken_->IsPcOrFreeMultiWindowCapabilityEnabled()) {
1614         TLOGE(WmsLogTag::WMS_SUB, "Device not supported");
1615         return static_cast<int32_t>(WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
1616     }
1617 
1618     if (!WindowHelper::IsSystemWindow(windowToken_->GetType()) &&
1619         !WindowHelper::IsSubWindow(windowToken_->GetType())) {
1620         TLOGE(WmsLogTag::WMS_SUB, "This is not subWindow or mainWindow");
1621         return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING);
1622     }
1623 
1624     sptr<WindowOption> windowOption = new WindowOption();
1625     windowOption->SetSubWindowTitle(option.title);
1626     windowOption->SetSubWindowDecorEnable(option.decorEnabled);
1627 
1628     if ((windowOption->GetWindowFlags() &
1629             static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_IS_APPLICATION_MODAL))) {
1630         TLOGE(WmsLogTag::WMS_SUB, "Device not supported");
1631         return static_cast<int32_t>(WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
1632     }
1633 
1634     if ((windowOption->GetWindowTopmost() && !Permission::IsSystemCalling() &&
1635             !Permission::IsStartByHdcd())) {
1636         TLOGE(WmsLogTag::WMS_SUB, "Modal subwindow has topmost, but no system permission");
1637         return static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP);
1638     }
1639 
1640     windowOption->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
1641     windowOption->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
1642     windowOption->SetOnlySupportSceneBoard(true);
1643     windowOption->SetParentId(windowToken_->GetWindowId());
1644     windowOption->SetWindowTag(WindowTag::SUB_WINDOW);
1645     auto window = Window::Create(name, windowOption, windowToken_->GetContext());
1646     if (window == nullptr) {
1647         TLOGE(WmsLogTag::WMS_SUB, "Create sub window failed");
1648         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1649     }
1650 
1651     sptr<CJWindowImpl> windowImpl = CreateCjWindowObject(window);
1652     if (windowImpl == nullptr) {
1653         TLOGE(WmsLogTag::WMS_SUB, "[createSubWindow] windowImpl is null");
1654         return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1655     }
1656     windowId = windowImpl->GetID();
1657     return static_cast<int32_t>(WmErrorCode::WM_OK);
1658 }
1659 }
1660 }
1661