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