• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "JsAppImpl.h"
17 
18 #include "CommandParser.h"
19 #include "FileSystem.h"
20 #include "JsonReader.h"
21 #include "PreviewerEngineLog.h"
22 #include "SharedData.h"
23 #include "TraceTool.h"
24 #include "VirtualScreenImpl.h"
25 #include "external/EventHandler.h"
26 #include "external/StageContext.h"
27 #include "viewport_config.h"
28 #include "glfw_render_context.h"
29 #if defined(REPLACE_WINDOW_HEADER)
30 #include "window.h"
31 #endif
32 #include "window_model.h"
33 #include "window_display.h"
34 #include "ace_preview_helper.h"
35 #include "ClipboardHelper.h"
36 #include "CommandLineInterface.h"
37 #if defined(__APPLE__) || defined(_WIN32)
38 #include "options.h"
39 #include "simulator.h"
40 #endif
41 #include <fstream>
42 
43 using namespace OHOS;
44 using namespace OHOS::Ace;
45 
46 namespace {
47 ScreenInfo screenInfo;
48 CommandInfo commandInfo;
49 }
50 
51 class PreviewerListener : public OHOS::Rosen::IWindowSystemBarEnableListener {
52 public:
OnSetSpecificBarProperty(OHOS::Rosen::WindowType type,const OHOS::Rosen::SystemBarProperty & property)53     OHOS::Rosen::WMError OnSetSpecificBarProperty(OHOS::Rosen::WindowType type,
54         const OHOS::Rosen::SystemBarProperty& property)
55     {
56         JsAppImpl::GetInstance().CalculateAvoidAreaByType(type, property);
57         return OHOS::Rosen::WMError::WM_OK;
58     }
59 };
60 
61 namespace {
62 OHOS::sptr<PreviewerListener> listener = nullptr;
63 }
64 
JsAppImpl()65 JsAppImpl::JsAppImpl() noexcept : ability(nullptr), isStop(false)
66 {
67 #if defined(__APPLE__) || defined(_WIN32)
68     windowModel = std::make_shared<OHOS::Previewer::PreviewerWindowModel>();
69 #endif
70 }
71 
~JsAppImpl()72 JsAppImpl::~JsAppImpl() {}
73 
GetInstance()74 JsAppImpl& JsAppImpl::GetInstance()
75 {
76     static JsAppImpl instance;
77     return instance;
78 }
79 
Start()80 void JsAppImpl::Start()
81 {
82     VirtualScreenImpl::GetInstance().InitVirtualScreen();
83     VirtualScreenImpl::GetInstance().InitAll(pipeName, pipePort);
84     isFinished = false;
85     ILOG("Start run js app");
86     OHOS::AppExecFwk::EventHandler::SetMainThreadId(std::this_thread::get_id());
87     RunJsApp();
88     ILOG("Js app run finished");
89     while (!isStop) {
90         // Execute all tasks in the main thread
91         OHOS::AppExecFwk::EventHandler::Run();
92         glfwRenderContext->PollEvents();
93         std::this_thread::sleep_for(std::chrono::milliseconds(1));
94     }
95     StopAbility(); // start and stop ability at the same thread
96     ILOG("JsAppImpl::Interrupt finished");
97     isFinished = true;
98 }
99 
Restart()100 void JsAppImpl::Restart()
101 {
102     StopAbility();
103 }
104 
GetJSONTree()105 std::string JsAppImpl::GetJSONTree()
106 {
107     std::string jsonTree = ability->GetJSONTree();
108     return jsonTree;
109 }
110 
GetDefaultJSONTree()111 std::string JsAppImpl::GetDefaultJSONTree()
112 {
113     ILOG("Start getDefaultJsontree.");
114     std::string jsonTree = ability->GetDefaultJSONTree();
115     ILOG("GetDefaultJsontree finished.");
116     return jsonTree;
117 }
118 
OrientationChanged(std::string commandOrientation)119 void JsAppImpl::OrientationChanged(std::string commandOrientation)
120 {
121     aceRunArgs.deviceWidth = height;
122     aceRunArgs.deviceHeight = width;
123     VirtualScreenImpl::GetInstance().WidthAndHeightReverse();
124     AdaptDeviceType(aceRunArgs, commandInfo.deviceType, aceRunArgs.deviceWidth);
125     if (commandOrientation == "portrait") {
126         aceRunArgs.deviceConfig.orientation = DeviceOrientation::PORTRAIT;
127     } else {
128         aceRunArgs.deviceConfig.orientation = DeviceOrientation::LANDSCAPE;
129     }
130 
131     orientation = commandOrientation;
132     ILOG("OrientationChanged: %s %d %d %f", orientation.c_str(), aceRunArgs.deviceWidth,
133          aceRunArgs.deviceHeight, aceRunArgs.deviceConfig.density);
134     if (ability != nullptr) {
135         OHOS::AppExecFwk::EventHandler::PostTask([this]() {
136             glfwRenderContext->SetWindowSize(width, height);
137         });
138         ability->SurfaceChanged(aceRunArgs.deviceConfig.orientation, aceRunArgs.deviceConfig.density,
139                                 aceRunArgs.deviceWidth, aceRunArgs.deviceHeight);
140     }
141 }
142 
ColorModeChanged(const std::string commandColorMode)143 void JsAppImpl::ColorModeChanged(const std::string commandColorMode)
144 {
145     if (commandColorMode == "light") {
146         aceRunArgs.deviceConfig.colorMode = ColorMode::LIGHT;
147     } else {
148         aceRunArgs.deviceConfig.colorMode = ColorMode::DARK;
149     }
150 
151     if (ability != nullptr) {
152         ability->OnConfigurationChanged(aceRunArgs.deviceConfig);
153     }
154 }
155 
Interrupt()156 void JsAppImpl::Interrupt()
157 {
158     isStop = true;
159 }
160 
SetJsAppArgs(OHOS::Ace::Platform::AceRunArgs & args)161 void JsAppImpl::SetJsAppArgs(OHOS::Ace::Platform::AceRunArgs& args)
162 {
163     ILOG("foldStatus:%s foldWidth:%d foldHeight:%d",
164         screenInfo.foldStatus.c_str(), screenInfo.foldWidth, screenInfo.foldHeight);
165     SetAssetPath(args, jsAppPath);
166     SetProjectModel(args);
167     SetPageProfile(args, commandInfo.pages);
168     SetDeviceWidth(args, ConvertFoldStatus(screenInfo.foldStatus) ==
169         OHOS::Rosen::FoldStatus::FOLDED ? screenInfo.foldWidth : screenInfo.orignalResolutionWidth);
170     SetDeviceHeight(args, ConvertFoldStatus(screenInfo.foldStatus) ==
171         OHOS::Rosen::FoldStatus::FOLDED ? screenInfo.foldHeight : screenInfo.orignalResolutionHeight);
172     SetWindowTitle(args, "Ace");
173     SetUrl(args, urlPath);
174     SetConfigChanges(args, configChanges);
175     SetColorMode(args, colorMode);
176     SetOrientation(args, orientation);
177     SetAceVersionArgs(args, aceVersion);
178     SetDeviceScreenDensity(atoi(screenDensity.c_str()), commandInfo.deviceType);
179     SetLanguage(args, SharedData<std::string>::GetData(SharedDataType::LAN));
180     SetRegion(args, SharedData<std::string>::GetData(SharedDataType::REGION));
181     SetScript(args, "");
182     SetSystemResourcesPath(args);
183     SetAppResourcesPath(args, commandInfo.appResourcePath);
184     SetFormsEnabled(args, commandInfo.isCardDisplay);
185     SetContainerSdkPath(args, commandInfo.containerSdkPath);
186     AdaptDeviceType(args, commandInfo.deviceType, ConvertFoldStatus(screenInfo.foldStatus) ==
187         OHOS::Rosen::FoldStatus::FOLDED ? screenInfo.foldWidth : screenInfo.orignalResolutionWidth);
188     SetOnRouterChange(args);
189     SetOnError(args);
190     SetComponentModeEnabled(args, commandInfo.isComponentMode);
191     SetPkgContextInfo();
192 #ifdef COMPONENT_TEST_ENABLED
193     SetComponentTestConfig(args, componentTestModeConfig);
194 #endif // COMPONENT_TEST_ENABLED
195     ILOG("start ability: %d %d %f", args.deviceWidth, args.deviceHeight, args.deviceConfig.density);
196 }
197 
RunJsApp()198 void JsAppImpl::RunJsApp()
199 {
200     ILOG("RunJsApp 1");
201     InitScreenInfo();
202     AssignValueForWidthAndHeight(screenInfo.orignalResolutionWidth, screenInfo.orignalResolutionHeight,
203                                  screenInfo.compressionResolutionWidth, screenInfo.compressionResolutionHeight);
204     SetJsAppArgs(aceRunArgs);
205     OHOS::Ide::StageContext::GetInstance().SetLoaderJsonPath(commandInfo.loaderJsonPath);
206     OHOS::Ide::StageContext::GetInstance().SetHosSdkPath(commandInfo.containerSdkPath);
207     OHOS::Ide::StageContext::GetInstance().GetModulePathMapFromLoaderJson();
208     OHOS::Previewer::PreviewerDisplay::GetInstance().SetFoldable(screenInfo.foldable);
209     OHOS::Previewer::PreviewerDisplay::GetInstance().SetFoldStatus(ConvertFoldStatus(screenInfo.foldStatus));
210     InitGlfwEnv();
211     Platform::AcePreviewHelper::GetInstance()->SetCallbackOfPostTask(AppExecFwk::EventHandler::PostTask);
212     Platform::AcePreviewHelper::GetInstance()->
213         SetCallbackOfIsCurrentRunnerThread(AppExecFwk::EventHandler::IsCurrentRunnerThread);
214     Platform::AcePreviewHelper::GetInstance()->SetCallbackOfSetClipboardData(ClipboardHelper::SetClipboardData);
215     Platform::AcePreviewHelper::GetInstance()->SetCallbackOfGetClipboardData(ClipboardHelper::GetClipboardData);
216     listener = new(std::nothrow) PreviewerListener();
217     if (!listener) {
218         ELOG("Memory allocation failed: listener.");
219         return;
220     }
221     if (isDebug && debugServerPort >= 0) {
222         RunDebugAbility(); // for debug preview
223     } else {
224         RunNormalAbility(); // for normal preview
225     }
226 }
227 
RunNormalAbility()228 void JsAppImpl::RunNormalAbility()
229 {
230     Platform::AcePreviewHelper::GetInstance()->SetCallbackOfHspBufferTracker(
231         [](const std::string& inputPath, uint8_t** buff, size_t* buffSize, std::string &errorMsg) -> bool {
232             if (!buff || !buffSize || inputPath.empty()) {
233                 return false;
234             }
235             auto data = OHOS::Ide::StageContext::GetInstance().GetModuleBuffer(inputPath);
236             if (!data) {
237                 return false;
238             }
239             *buff = data->data();
240             *buffSize = data->size();
241             return true;
242         });
243     if (ability != nullptr) {
244         ability.reset();
245     }
246     TraceTool::GetInstance().HandleTrace("Launch Js App");
247     ability = Platform::AceAbility::CreateInstance(aceRunArgs);
248     if (ability == nullptr) {
249         ELOG("JsApp::Run ability create failed.");
250         return;
251     }
252     SetMockJsonInfo();
253     OHOS::Rosen::WMError errCode;
254     OHOS::sptr<OHOS::Rosen::WindowOption> sp = nullptr;
255     auto window = OHOS::Rosen::Window::Create("previewer", sp, nullptr, errCode);
256     window->RegisterSystemBarEnableListener(sptr<OHOS::Rosen::IWindowSystemBarEnableListener>(listener));
257     window->SetContentInfoCallback(std::move(VirtualScreenImpl::LoadContentCallback));
258     window->CreateSurfaceNode("preview_surface", std::move(VirtualScreenImpl::Callback));
259     ability->SetWindow(window);
260     InitAvoidAreas(window);
261     ability->InitEnv();
262 }
263 
264 #if defined(__APPLE__) || defined(_WIN32)
RunDebugAbility()265 void JsAppImpl::RunDebugAbility()
266 {
267     // init window params
268     SetWindowParams();
269     OHOS::Previewer::PreviewerWindow::GetInstance().SetWindowParams(*windowModel);
270     // start ability
271     OHOS::AbilityRuntime::Options options;
272     SetSimulatorParams(options);
273     simulator = OHOS::AbilityRuntime::Simulator::Create(options);
274     if (!simulator) {
275         ELOG("JsApp::Run simulator create failed.");
276         return;
277     }
278     simulator->SetHostResolveBufferTracker(
279         [](const std::string &inputPath, uint8_t **buff, size_t *buffSize, std::string &errorMsg) -> bool {
280             if (inputPath.empty() || buff == nullptr || buffSize == nullptr) {
281                 ELOG("Param invalid.");
282                 return false;
283             }
284 
285             DLOG("Get module buffer, input path: %{public}s.", inputPath.c_str());
286             auto data = Ide::StageContext::GetInstance().GetModuleBuffer(inputPath);
287             if (data == nullptr) {
288                 ELOG("Get module buffer failed, input path: %{public}s.", inputPath.c_str());
289                 return false;
290             }
291 
292             *buff = data->data();
293             *buffSize = data->size();
294             return true;
295         });
296     SetMockJsonInfo();
297     std::string abilitySrcPath = commandInfo.abilityPath;
298     std::string abilityName = commandInfo.abilityName;
299     debugAbilityId = simulator->StartAbility(abilitySrcPath, [](int64_t abilityId) {}, abilityName);
300     if (debugAbilityId < 0) {
301         ELOG("JsApp::Run ability start failed. abilitySrcPath:%s abilityName:%s", abilitySrcPath.c_str(),
302             abilityName.c_str());
303         return;
304     }
305     // set onRender callback
306     OHOS::Rosen::Window* window = OHOS::Previewer::PreviewerWindow::GetInstance().GetWindowObject();
307     if (!window) {
308         ELOG("JsApp::Run get window failed.");
309         return;
310     }
311     window->RegisterSystemBarEnableListener(sptr<OHOS::Rosen::IWindowSystemBarEnableListener>(listener));
312     window->SetContentInfoCallback(std::move(VirtualScreenImpl::LoadContentCallback));
313     window->CreateSurfaceNode(options.moduleName, std::move(VirtualScreenImpl::Callback));
314     InitAvoidAreas(window);
315 }
316 
SetSimulatorParams(OHOS::AbilityRuntime::Options & options)317 void JsAppImpl::SetSimulatorParams(OHOS::AbilityRuntime::Options& options)
318 {
319     const std::string path = commandInfo.appResourcePath + FileSystem::GetSeparator() + "module.json";
320     if (!FileSystem::IsFileExists(path)) {
321         ELOG("The module.json file is not exist.");
322         return;
323     }
324     std::optional<std::vector<uint8_t>> ctx = OHOS::Ide::StageContext::GetInstance().ReadFileContents(path);
325     if (ctx.has_value()) {
326         options.moduleJsonBuffer = ctx.value();
327     } else {
328         ELOG("get module.json content failed");
329     }
330     SetSimulatorCommonParams(options);
331     ILOG("setted bundleName:%s moduleName:%s", options.modulePath.c_str(), options.resourcePath.c_str());
332 }
333 
SetSimulatorCommonParams(OHOS::AbilityRuntime::Options & options)334 void JsAppImpl::SetSimulatorCommonParams(OHOS::AbilityRuntime::Options& options)
335 {
336     options.modulePath = aceRunArgs.assetPath + FileSystem::GetSeparator() + "modules.abc";
337     options.resourcePath = commandInfo.appResourcePath + FileSystem::GetSeparator() + "resources.index";
338     if (debugServerPort > 0) {
339         options.debugPort = debugServerPort;
340     }
341     options.assetPath = aceRunArgs.assetPath;
342     options.systemResourcePath = aceRunArgs.systemResourcesPath;
343     options.appResourcePath = aceRunArgs.appResourcesPath;
344     options.containerSdkPath = aceRunArgs.containerSdkPath;
345     options.url = aceRunArgs.url;
346     options.language = aceRunArgs.language;
347     options.region = aceRunArgs.region;
348     options.script = aceRunArgs.script;
349     options.themeId = aceRunArgs.themeId;
350     options.deviceWidth = aceRunArgs.deviceWidth;
351     options.deviceHeight = aceRunArgs.deviceHeight;
352     options.isRound = aceRunArgs.isRound;
353     options.onRouterChange = aceRunArgs.onRouterChange;
354     options.pkgContextInfoJsonStringMap = aceRunArgs.pkgContextInfoJsonStringMap;
355     options.packageNameList = aceRunArgs.packageNameList;
356     OHOS::AbilityRuntime::DeviceConfig deviceCfg;
357     deviceCfg.deviceType = SetDevice<OHOS::AbilityRuntime::DeviceType>(aceRunArgs.deviceConfig.deviceType);
358     deviceCfg.orientation = SetOrientation<OHOS::AbilityRuntime::DeviceOrientation>(
359         aceRunArgs.deviceConfig.orientation);
360     deviceCfg.colorMode = SetColorMode<OHOS::AbilityRuntime::ColorMode>(aceRunArgs.deviceConfig.colorMode);
361     deviceCfg.density = aceRunArgs.deviceConfig.density;
362     options.deviceConfig = deviceCfg;
363     std::string fPath = commandInfo.configPath;
364     options.configuration = UpdateConfiguration(aceRunArgs);
365     std::size_t pos = fPath.find(".idea");
366     if (pos == std::string::npos) {
367         ELOG("previewPath error:%s", fPath.c_str());
368     } else {
369         options.previewPath = fPath.substr(0, pos) + ".idea" + FileSystem::GetSeparator() + "previewer";
370         ILOG("previewPath info:%s", options.previewPath.c_str());
371     }
372     options.postTask = AppExecFwk::EventHandler::PostTask;
373 }
374 
UpdateConfiguration(OHOS::Ace::Platform::AceRunArgs & args)375 std::shared_ptr<AppExecFwk::Configuration> JsAppImpl::UpdateConfiguration(OHOS::Ace::Platform::AceRunArgs& args)
376 {
377     std::shared_ptr<AppExecFwk::Configuration> configuration = std::make_shared<AppExecFwk::Configuration>();
378     configuration->AddItem(OHOS::AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE,
379         SharedData<std::string>::GetData(SharedDataType::LANGUAGE));
380     std::string colorMode = "light";
381     if (aceRunArgs.deviceConfig.colorMode == ColorMode::DARK) {
382         colorMode = "dark";
383     }
384     configuration->AddItem(OHOS::AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, colorMode);
385     std::string direction = "portrait";
386     if (aceRunArgs.deviceConfig.orientation == DeviceOrientation::LANDSCAPE) {
387         orientation = "landscape";
388     }
389     configuration->AddItem(OHOS::AppExecFwk::ConfigurationInner::APPLICATION_DIRECTION, direction);
390     std::string density = std::to_string(aceRunArgs.deviceConfig.density);
391     configuration->AddItem(OHOS::AppExecFwk::ConfigurationInner::APPLICATION_DENSITYDPI, density);
392     return configuration;
393 }
394 
395 
SetWindowParams() const396 void JsAppImpl::SetWindowParams() const
397 {
398     windowModel->isRound = aceRunArgs.isRound;
399     windowModel->originWidth = aceRunArgs.deviceWidth;
400     windowModel->originHeight = aceRunArgs.deviceHeight;
401     windowModel->compressWidth = aceRunArgs.deviceWidth;
402     windowModel->compressHeight = aceRunArgs.deviceHeight;
403     windowModel->density = aceRunArgs.deviceConfig.density;
404     windowModel->deviceType = SetDevice<OHOS::Previewer::DeviceType>(aceRunArgs.deviceConfig.deviceType);
405     windowModel->orientation = SetOrientation<OHOS::Previewer::Orientation>(aceRunArgs.deviceConfig.orientation);
406     windowModel->colorMode = SetColorMode<OHOS::Previewer::ColorMode>(aceRunArgs.deviceConfig.colorMode);
407 }
408 #else
RunDebugAbility()409     void JsAppImpl::RunDebugAbility()
410     {
411         ELOG("JsApp::Run ability start failed.Linux is not supported.");
412         return;
413     }
414 #endif
415 
AdaptDeviceType(Platform::AceRunArgs & args,const std::string type,const int32_t realDeviceWidth,double screenDendity) const416 void JsAppImpl::AdaptDeviceType(Platform::AceRunArgs& args, const std::string type,
417                                 const int32_t realDeviceWidth, double screenDendity) const
418 {
419     if (type == "wearable") {
420         args.deviceConfig.deviceType = DeviceType::WATCH;
421         double density = screenDendity > 0 ? screenDendity : watchScreenDensity;
422         double adaptWidthWatch = realDeviceWidth * BASE_SCREEN_DENSITY / density;
423         args.deviceConfig.density = args.deviceWidth / adaptWidthWatch;
424         return;
425     }
426     if (type == "tv") {
427         args.deviceConfig.deviceType = DeviceType::TV;
428         double density = screenDendity > 0 ? screenDendity : tvScreenDensity;
429         double adaptWidthTv = realDeviceWidth * BASE_SCREEN_DENSITY / density;
430         args.deviceConfig.density = args.deviceWidth / adaptWidthTv;
431         return;
432     }
433     if (type == "phone" || type == "default") {
434         args.deviceConfig.deviceType = DeviceType::PHONE;
435         double density = screenDendity > 0 ? screenDendity : phoneScreenDensity;
436         double adaptWidthPhone = realDeviceWidth * BASE_SCREEN_DENSITY / density;
437         args.deviceConfig.density = args.deviceWidth / adaptWidthPhone;
438         return;
439     }
440     if (type == "2in1") {
441         args.deviceConfig.deviceType = DeviceType::TWO_IN_ONE;
442         double density = screenDendity > 0 ? screenDendity : twoInOneScreenDensity;
443         double adaptWidthPhone = realDeviceWidth * BASE_SCREEN_DENSITY / density;
444         args.deviceConfig.density = args.deviceWidth / adaptWidthPhone;
445         return;
446     }
447     if (type == "tablet") {
448         args.deviceConfig.deviceType = DeviceType::TABLET;
449         double density = screenDendity > 0 ? screenDendity : tabletScreenDensity;
450         double adaptWidthTablet = realDeviceWidth * BASE_SCREEN_DENSITY / density;
451         args.deviceConfig.density = args.deviceWidth / adaptWidthTablet;
452         return;
453     }
454     if (type == "car") {
455         args.deviceConfig.deviceType = DeviceType::CAR;
456         double density = screenDendity > 0 ? screenDendity : carScreenDensity;
457         double adaptWidthCar = realDeviceWidth * BASE_SCREEN_DENSITY / density;
458         args.deviceConfig.density = args.deviceWidth / adaptWidthCar;
459         return;
460     }
461     ELOG("DeviceType not supported : %s", type.c_str());
462     return;
463 }
464 
SetAssetPath(Platform::AceRunArgs & args,const std::string assetPath) const465 void JsAppImpl::SetAssetPath(Platform::AceRunArgs& args, const std::string assetPath) const
466 {
467     args.assetPath = assetPath;
468 }
469 
SetProjectModel(Platform::AceRunArgs & args) const470 void JsAppImpl::SetProjectModel(Platform::AceRunArgs& args) const
471 {
472     int idxVal = CommandParser::GetInstance().GetProjectModelEnumValue();
473     ILOG("ProjectModel: %s", CommandParser::GetInstance().GetProjectModelEnumName(idxVal).c_str());
474     args.projectModel = Platform::ProjectModel(idxVal);
475 }
476 
SetPageProfile(Platform::AceRunArgs & args,const std::string pageProfile) const477 void JsAppImpl::SetPageProfile(Platform::AceRunArgs& args, const std::string pageProfile) const
478 {
479     args.pageProfile = pageProfile;
480 }
481 
SetDeviceWidth(Platform::AceRunArgs & args,const int32_t deviceWidth) const482 void JsAppImpl::SetDeviceWidth(Platform::AceRunArgs& args, const int32_t deviceWidth) const
483 {
484     args.deviceWidth = deviceWidth;
485 }
486 
SetDeviceHeight(Platform::AceRunArgs & args,const int32_t deviceHeight) const487 void JsAppImpl::SetDeviceHeight(Platform::AceRunArgs& args, const int32_t deviceHeight) const
488 {
489     args.deviceHeight = deviceHeight;
490 }
491 
SetWindowTitle(Platform::AceRunArgs & args,const std::string windowTitle) const492 void JsAppImpl::SetWindowTitle(Platform::AceRunArgs& args, const std::string windowTitle) const
493 {
494     args.windowTitle = windowTitle;
495 }
496 
SetUrl(Platform::AceRunArgs & args,const std::string urlPath) const497 void JsAppImpl::SetUrl(Platform::AceRunArgs& args, const std::string urlPath) const
498 {
499     args.url = urlPath;
500 }
501 
SetConfigChanges(Platform::AceRunArgs & args,const std::string configChanges) const502 void JsAppImpl::SetConfigChanges(Platform::AceRunArgs& args, const std::string configChanges) const
503 {
504     args.configChanges = configChanges;
505 }
506 
SetColorMode(Platform::AceRunArgs & args,const std::string colorMode) const507 void JsAppImpl::SetColorMode(Platform::AceRunArgs& args, const std::string colorMode) const
508 {
509     ILOG("JsAppImpl::RunJsApp SetColorMode: %s", colorMode.c_str());
510     if (colorMode == "dark") {
511         args.deviceConfig.colorMode = ColorMode::DARK;
512     } else {
513         args.deviceConfig.colorMode = ColorMode::LIGHT;
514     }
515 }
516 
SetOrientation(Platform::AceRunArgs & args,const std::string orientation) const517 void JsAppImpl::SetOrientation(Platform::AceRunArgs& args, const std::string orientation) const
518 {
519     ILOG("JsAppImpl::RunJsApp SetOrientation: %s", orientation.c_str());
520     if (orientation == "landscape") {
521         args.deviceConfig.orientation = DeviceOrientation::LANDSCAPE;
522     } else {
523         args.deviceConfig.orientation = DeviceOrientation::PORTRAIT;
524     }
525 }
526 
SetAceVersionArgs(Platform::AceRunArgs & args,const std::string aceVersion) const527 void JsAppImpl::SetAceVersionArgs(Platform::AceRunArgs& args, const std::string aceVersion) const
528 {
529     ILOG("JsAppImpl::RunJsApp SetAceVersionArgs: %s", aceVersion.c_str());
530     if (aceVersion == "ACE_2_0") {
531         args.aceVersion = Platform::AceVersion::ACE_2_0;
532     } else {
533         args.aceVersion = Platform::AceVersion::ACE_1_0;
534     }
535 }
536 
SetLanguage(Platform::AceRunArgs & args,const std::string language) const537 void JsAppImpl::SetLanguage(Platform::AceRunArgs& args, const std::string language) const
538 {
539     args.language = language;
540 }
541 
SetRegion(Platform::AceRunArgs & args,const std::string region) const542 void JsAppImpl::SetRegion(Platform::AceRunArgs& args, const std::string region) const
543 {
544     args.region = region;
545 }
546 
SetScript(Platform::AceRunArgs & args,const std::string script) const547 void JsAppImpl::SetScript(Platform::AceRunArgs& args, const std::string script) const
548 {
549     args.script = script;
550 }
551 
SetSystemResourcesPath(Platform::AceRunArgs & args) const552 void JsAppImpl::SetSystemResourcesPath(Platform::AceRunArgs& args) const
553 {
554     std::string sep = FileSystem::GetSeparator();
555     std::string rPath = FileSystem::GetApplicationPath();
556     rPath = FileSystem::NormalizePath(rPath);
557     int idx = rPath.find_last_of(sep);
558     rPath = rPath.substr(0, idx + 1) + "resources";
559     args.systemResourcesPath = rPath;
560 }
561 
SetAppResourcesPath(Platform::AceRunArgs & args,const std::string appResourcesPath) const562 void JsAppImpl::SetAppResourcesPath(Platform::AceRunArgs& args, const std::string appResourcesPath) const
563 {
564     args.appResourcesPath = appResourcesPath;
565 }
566 
SetFormsEnabled(Platform::AceRunArgs & args,bool formsEnabled) const567 void JsAppImpl::SetFormsEnabled(Platform::AceRunArgs& args, bool formsEnabled) const
568 {
569     args.formsEnabled = formsEnabled;
570 }
571 
SetContainerSdkPath(Platform::AceRunArgs & args,const std::string containerSdkPath) const572 void JsAppImpl::SetContainerSdkPath(Platform::AceRunArgs& args, const std::string containerSdkPath) const
573 {
574     args.containerSdkPath = containerSdkPath;
575 }
576 
SetOnRouterChange(Platform::AceRunArgs & args) const577 void JsAppImpl::SetOnRouterChange(Platform::AceRunArgs& args) const
578 {
579     args.onRouterChange = std::move(VirtualScreenImpl::PageCallback);
580 }
581 
SetOnError(Platform::AceRunArgs & args) const582 void JsAppImpl::SetOnError(Platform::AceRunArgs& args) const
583 {
584     args.onError = std::move(VirtualScreenImpl::FastPreviewCallback);
585 }
586 
SetComponentModeEnabled(Platform::AceRunArgs & args,bool isComponentMode) const587 void JsAppImpl::SetComponentModeEnabled(Platform::AceRunArgs& args, bool isComponentMode) const
588 {
589     args.isComponentMode = isComponentMode;
590 }
591 
AssignValueForWidthAndHeight(const int32_t origWidth,const int32_t origHeight,const int32_t compWidth,const int32_t compHeight)592 void JsAppImpl::AssignValueForWidthAndHeight(const int32_t origWidth,
593                                              const int32_t origHeight,
594                                              const int32_t compWidth,
595                                              const int32_t compHeight)
596 {
597     orignalWidth = origWidth;
598     orignalHeight = origHeight;
599     width = compWidth;
600     height = compHeight;
601     ILOG("AssignValueForWidthAndHeight: %d %d %d %d", orignalWidth, orignalHeight, width, height);
602 }
603 
ResolutionChanged(ResolutionParam & param,int32_t screenDensity,std::string reason)604 void JsAppImpl::ResolutionChanged(ResolutionParam& param, int32_t screenDensity, std::string reason)
605 {
606     SetResolutionParams(param.orignalWidth, param.orignalHeight, param.compressionWidth,
607         param.compressionHeight, screenDensity);
608     if (isDebug && debugServerPort >= 0) {
609 #if defined(__APPLE__) || defined(_WIN32)
610         SetWindowParams();
611         OHOS::Ace::ViewportConfig config;
612         config.SetSize(windowModel->originWidth, windowModel->originHeight);
613         config.SetPosition(0, 0);
614         config.SetOrientation(static_cast<int32_t>(
615             OHOS::Previewer::PreviewerWindow::TransOrientation(windowModel->orientation)));
616         config.SetDensity(windowModel->density);
617         OHOS::Rosen::Window* window = OHOS::Previewer::PreviewerWindow::GetInstance().GetWindowObject();
618         if (!window) {
619             ELOG("JsApp::Run get window failed.");
620             return;
621         }
622         InitAvoidAreas(window);
623         OHOS::AppExecFwk::EventHandler::PostTask([this]() {
624             glfwRenderContext->SetWindowSize(aceRunArgs.deviceWidth, aceRunArgs.deviceHeight);
625         });
626         simulator->UpdateConfiguration(*(UpdateConfiguration(aceRunArgs).get()));
627         window->SetViewportConfig(config);
628 #endif
629     } else {
630         if (ability != nullptr) {
631             InitAvoidAreas(ability->GetWindow());
632             OHOS::AppExecFwk::EventHandler::PostTask([this]() {
633                 glfwRenderContext->SetWindowSize(aceRunArgs.deviceWidth, aceRunArgs.deviceHeight);
634             });
635             ability->SurfaceChanged(aceRunArgs.deviceConfig.orientation, aceRunArgs.deviceConfig.density,
636                 aceRunArgs.deviceWidth, aceRunArgs.deviceHeight, ConvertResizeReason(reason));
637         }
638     }
639 }
640 
ConvertResizeReason(std::string reason)641 WindowSizeChangeReason JsAppImpl::ConvertResizeReason(std::string reason)
642 {
643     if (reason == "undefined") {
644         return WindowSizeChangeReason::UNDEFINED;
645     } else if (reason == "rotation") {
646         return WindowSizeChangeReason::ROTATION;
647     } else {
648         return WindowSizeChangeReason::RESIZE;
649     }
650 }
651 
SetResolutionParams(int32_t changedOriginWidth,int32_t changedOriginHeight,int32_t changedWidth,int32_t changedHeight,int32_t screenDensity)652 void JsAppImpl::SetResolutionParams(int32_t changedOriginWidth, int32_t changedOriginHeight, int32_t changedWidth,
653     int32_t changedHeight, int32_t screenDensity)
654 {
655     SetDeviceWidth(aceRunArgs, changedWidth);
656     SetDeviceHeight(aceRunArgs, changedHeight);
657     orignalWidth = changedOriginWidth;
658     orignalHeight = changedOriginHeight;
659     SetDeviceScreenDensity(screenDensity, commandInfo.deviceType);
660     AdaptDeviceType(aceRunArgs, commandInfo.deviceType, changedOriginWidth);
661     AssignValueForWidthAndHeight(changedOriginWidth, changedOriginHeight, changedWidth, changedHeight);
662     if (changedWidth <= changedHeight) {
663         JsAppImpl::GetInstance().SetDeviceOrentation("portrait");
664     } else {
665         JsAppImpl::GetInstance().SetDeviceOrentation("landscape");
666     }
667     SetOrientation(aceRunArgs, orientation);
668     VirtualScreenImpl::GetInstance().SetCurrentResolution(aceRunArgs.deviceWidth, aceRunArgs.deviceHeight);
669     ILOG("ResolutionChanged: %s %d %d %f", orientation.c_str(), aceRunArgs.deviceWidth,
670          aceRunArgs.deviceHeight, aceRunArgs.deviceConfig.density);
671 }
672 
SetArgsColorMode(const std::string & value)673 void JsAppImpl::SetArgsColorMode(const std::string& value)
674 {
675     colorMode = value;
676 }
677 
SetArgsAceVersion(const std::string & value)678 void JsAppImpl::SetArgsAceVersion(const std::string& value)
679 {
680     aceVersion = value;
681 }
682 
SetDeviceOrentation(const std::string & value)683 void JsAppImpl::SetDeviceOrentation(const std::string& value)
684 {
685     orientation = value;
686 }
687 
GetOrientation() const688 std::string JsAppImpl::GetOrientation() const
689 {
690     return orientation;
691 }
692 
GetColorMode() const693 std::string JsAppImpl::GetColorMode() const
694 {
695     return colorMode;
696 }
697 
SetDeviceScreenDensity(const int32_t screenDensity,const std::string type)698 void JsAppImpl::SetDeviceScreenDensity(const int32_t screenDensity, const std::string type)
699 {
700     if (type == "wearable" && screenDensity != 0) {
701         watchScreenDensity = screenDensity;
702         return;
703     }
704     if (type == "tv" && screenDensity != 0) {
705         tvScreenDensity = screenDensity;
706         return;
707     }
708     if ((type == "phone" || type == "default") && screenDensity != 0) {
709         phoneScreenDensity = screenDensity;
710         return;
711     }
712     if (type == "2in1" && screenDensity != 0) {
713         twoInOneScreenDensity = screenDensity;
714         return;
715     }
716     if (type == "tablet" && screenDensity != 0) {
717         tabletScreenDensity = screenDensity;
718         return;
719     }
720     if (type == "car" && screenDensity != 0) {
721         carScreenDensity = screenDensity;
722         return;
723     }
724     ILOG("DeviceType not supported to SetDeviceScreenDensity: %s", type.c_str());
725     return;
726 }
727 
ReloadRuntimePage(const std::string currentPage)728 void JsAppImpl::ReloadRuntimePage(const std::string currentPage)
729 {
730     std::string params = "";
731     if (ability != nullptr) {
732         ability->ReplacePage(currentPage, params);
733     }
734 }
735 
SetScreenDensity(const std::string value)736 void JsAppImpl::SetScreenDensity(const std::string value)
737 {
738     screenDensity = value;
739 }
740 
SetConfigChanges(const std::string value)741 void JsAppImpl::SetConfigChanges(const std::string value)
742 {
743     configChanges = value;
744 }
745 
MemoryRefresh(const std::string memoryRefreshArgs) const746 bool JsAppImpl::MemoryRefresh(const std::string memoryRefreshArgs) const
747 {
748     ILOG("MemoryRefresh.");
749     if (ability != nullptr) {
750         return ability->OperateComponent(memoryRefreshArgs);
751     }
752     return false;
753 }
754 
ParseSystemParams(OHOS::Ace::Platform::AceRunArgs & args,const Json2::Value & paramObj)755 void JsAppImpl::ParseSystemParams(OHOS::Ace::Platform::AceRunArgs& args, const Json2::Value& paramObj)
756 {
757     if (paramObj.IsNull()) {
758         SetDeviceWidth(args, VirtualScreenImpl::GetInstance().GetCompressionWidth());
759         SetDeviceHeight(args, VirtualScreenImpl::GetInstance().GetCompressionHeight());
760         AssignValueForWidthAndHeight(args.deviceWidth, args.deviceHeight,
761                                      args.deviceWidth, args.deviceHeight);
762         SetColorMode(args, colorMode);
763         SetOrientation(args, orientation);
764         SetDeviceScreenDensity(atoi(screenDensity.c_str()), commandInfo.deviceType);
765         AdaptDeviceType(args, commandInfo.deviceType, aceRunArgs.deviceWidth);
766         SetLanguage(args, SharedData<std::string>::GetData(SharedDataType::LAN));
767         SetRegion(args, SharedData<std::string>::GetData(SharedDataType::REGION));
768     } else {
769         SetDeviceWidth(args, paramObj["width"].AsInt());
770         SetDeviceHeight(args, paramObj["height"].AsInt());
771         AssignValueForWidthAndHeight(args.deviceWidth, args.deviceHeight,
772                                      args.deviceWidth, args.deviceHeight);
773         SetColorMode(args, paramObj["colorMode"].AsString());
774         SetOrientation(args, paramObj["orientation"].AsString());
775         std::string deviceType = paramObj["deviceType"].AsString();
776         SetDeviceScreenDensity(atoi(screenDensity.c_str()), deviceType);
777         AdaptDeviceType(args, deviceType, args.deviceWidth, paramObj["dpi"].AsDouble());
778         std::string lanInfo = paramObj["locale"].AsString();
779         SetLanguage(args, lanInfo.substr(0, lanInfo.find("_")));
780         SetRegion(args, lanInfo.substr(lanInfo.find("_") + 1, lanInfo.length() - 1));
781     }
782 }
783 
SetSystemParams(OHOS::Ace::Platform::SystemParams & params,const Json2::Value & paramObj)784 void JsAppImpl::SetSystemParams(OHOS::Ace::Platform::SystemParams& params, const Json2::Value& paramObj)
785 {
786     ParseSystemParams(aceRunArgs, paramObj);
787     params.deviceWidth = aceRunArgs.deviceWidth;
788     params.deviceHeight = aceRunArgs.deviceHeight;
789     params.language = aceRunArgs.language;
790     params.region = aceRunArgs.region;
791     params.colorMode = aceRunArgs.deviceConfig.colorMode;
792     params.orientation = aceRunArgs.deviceConfig.orientation;
793     params.deviceType = aceRunArgs.deviceConfig.deviceType;
794     params.density = aceRunArgs.deviceConfig.density;
795     params.isRound = (paramObj.IsNull()) ? (commandInfo.screenShape == "circle") :
796         paramObj["roundScreen"].AsBool();
797 }
798 
LoadDocument(const std::string filePath,const std::string componentName,const Json2::Value & previewContext)799 void JsAppImpl::LoadDocument(const std::string filePath,
800                              const std::string componentName,
801                              const Json2::Value& previewContext)
802 {
803     ILOG("LoadDocument.");
804     if (ability != nullptr) {
805         OHOS::Ace::Platform::SystemParams params;
806         SetSystemParams(params, previewContext);
807         ILOG("LoadDocument params is density: %f region: %s language: %s deviceWidth: %d\
808              deviceHeight: %d isRound:%d colorMode:%s orientation: %s deviceType: %s",
809              params.density,
810              params.region.c_str(),
811              params.language.c_str(),
812              params.deviceWidth,
813              params.deviceHeight,
814              (params.isRound ? "true" : "false"),
815              ((params.colorMode == ColorMode::DARK) ? "dark" : "light"),
816              ((params.orientation == DeviceOrientation::LANDSCAPE) ? "landscape" : "portrait"),
817              GetDeviceTypeName(params.deviceType).c_str());
818         OHOS::AppExecFwk::EventHandler::PostTask([this]() {
819             glfwRenderContext->SetWindowSize(aceRunArgs.deviceWidth, aceRunArgs.deviceHeight);
820         });
821         ability->LoadDocument(filePath, componentName, params);
822     }
823 }
824 
DispatchBackPressedEvent() const825 void JsAppImpl::DispatchBackPressedEvent() const
826 {
827     ability->OnBackPressed();
828 }
DispatchKeyEvent(const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent) const829 void JsAppImpl::DispatchKeyEvent(const std::shared_ptr<OHOS::MMI::KeyEvent>& keyEvent) const
830 {
831     if (isDebug && debugServerPort >= 0) {
832 #if defined(__APPLE__) || defined(_WIN32)
833         OHOS::Rosen::Window* window = OHOS::Previewer::PreviewerWindow::GetInstance().GetWindowObject();
834         if (!window) {
835             ELOG("JsApp::Run get window failed.");
836             return;
837         }
838         window->ConsumeKeyEvent(keyEvent);
839 #endif
840     } else {
841         ability->OnInputEvent(keyEvent);
842     }
843 }
DispatchPointerEvent(const std::shared_ptr<OHOS::MMI::PointerEvent> & pointerEvent) const844 void JsAppImpl::DispatchPointerEvent(const std::shared_ptr<OHOS::MMI::PointerEvent>& pointerEvent) const
845 {
846     if (isDebug && debugServerPort >= 0) {
847 #if defined(__APPLE__) || defined(_WIN32)
848         OHOS::Rosen::Window* window = OHOS::Previewer::PreviewerWindow::GetInstance().GetWindowObject();
849         if (!window) {
850             ELOG("JsApp::Run get window failed.");
851             return;
852         }
853         window->ConsumePointerEvent(pointerEvent);
854 #endif
855     } else {
856         ability->OnInputEvent(pointerEvent);
857     }
858 }
DispatchAxisEvent(const std::shared_ptr<OHOS::MMI::AxisEvent> & axisEvent) const859 void JsAppImpl::DispatchAxisEvent(const std::shared_ptr<OHOS::MMI::AxisEvent>& axisEvent) const
860 {
861     ability->OnInputEvent(axisEvent);
862 }
DispatchInputMethodEvent(const unsigned int codePoint) const863 void JsAppImpl::DispatchInputMethodEvent(const unsigned int codePoint) const
864 {
865     ability->OnInputMethodEvent(codePoint);
866 }
867 
GetDeviceTypeName(const OHOS::Ace::DeviceType type) const868 std::string JsAppImpl::GetDeviceTypeName(const OHOS::Ace::DeviceType type) const
869 {
870     switch (type) {
871         case DeviceType::WATCH:
872             return "watch";
873         case DeviceType::TV:
874             return "tv";
875         case DeviceType::PHONE:
876             return "phone";
877         case DeviceType::TABLET:
878             return "tablet";
879         case DeviceType::CAR:
880             return "car";
881         default:
882             return "";
883     }
884 }
885 
InitGlfwEnv()886 void JsAppImpl::InitGlfwEnv()
887 {
888     ILOG("InitGlfwEnv started");
889     glfwRenderContext = OHOS::Rosen::GlfwRenderContext::GetGlobal();
890     if (!glfwRenderContext->Init()) {
891         ELOG("Could not create window: InitGlfwEnv failed.");
892         return;
893     }
894     glfwRenderContext->CreateGlfwWindow(aceRunArgs.deviceWidth, aceRunArgs.deviceHeight, false);
895     ILOG("InitGlfwEnv finished");
896 }
897 
SetMockJsonInfo()898 void JsAppImpl::SetMockJsonInfo()
899 {
900     std::string filePath = commandInfo.appResourcePath + FileSystem::GetSeparator() + "mock-config.json";
901     if (isDebug && debugServerPort >= 0) {
902 #if defined(__APPLE__) || defined(_WIN32)
903         simulator->SetMockList(Ide::StageContext::GetInstance().ParseMockJsonFile(filePath));
904 #endif
905     } else {
906         ability->SetMockModuleList(Ide::StageContext::GetInstance().ParseMockJsonFile(filePath));
907     }
908 }
909 
SetPkgContextInfo()910 void JsAppImpl::SetPkgContextInfo()
911 {
912     Ide::StageContext::GetInstance().SetPkgContextInfo(aceRunArgs.pkgContextInfoJsonStringMap,
913         aceRunArgs.packageNameList);
914 }
915 
FoldStatusChanged(const std::string commandFoldStatus,int32_t currentWidth,int32_t currentHeight)916 void JsAppImpl::FoldStatusChanged(const std::string commandFoldStatus, int32_t currentWidth, int32_t currentHeight)
917 {
918     std::string reason = "resize";
919     ILOG("FoldStatusChanged commandFoldStatus:%s", commandFoldStatus.c_str());
920     VirtualScreenImpl::GetInstance().SetFoldStatus(commandFoldStatus);
921     OHOS::Rosen::FoldStatus status = ConvertFoldStatus(commandFoldStatus);
922     // execute callback
923     OHOS::Previewer::PreviewerDisplay::GetInstance().SetFoldStatus(status);
924     OHOS::Previewer::PreviewerDisplay::GetInstance().ExecStatusChangedCallback();
925     if (status == OHOS::Rosen::FoldStatus::UNKNOWN) {
926         return; // unknown status do nothing
927     }
928     // change resolution
929     ResolutionParam param(currentWidth, currentHeight, currentWidth, currentHeight);
930     ResolutionChanged(param, atoi(screenDensity.c_str()), reason);
931 }
932 
ConvertFoldStatus(std::string value) const933 OHOS::Rosen::FoldStatus JsAppImpl::ConvertFoldStatus(std::string value) const
934 {
935     OHOS::Rosen::FoldStatus foldStatus = OHOS::Rosen::FoldStatus::EXPAND;
936     if (value == "fold") {
937         foldStatus = OHOS::Rosen::FoldStatus::FOLDED;
938     } else if (value == "unfold") {
939         foldStatus = OHOS::Rosen::FoldStatus::EXPAND;
940     } else if (value == "half_fold") {
941         foldStatus = OHOS::Rosen::FoldStatus::HALF_FOLD;
942     } else {
943         foldStatus = OHOS::Rosen::FoldStatus::UNKNOWN;
944     }
945     return foldStatus;
946 }
947 
SetAvoidArea(const AvoidAreas & areas)948 void JsAppImpl::SetAvoidArea(const AvoidAreas& areas)
949 {
950     avoidInitialAreas = areas;
951 }
952 
CalculateAvoidAreaByType(OHOS::Rosen::WindowType type,const OHOS::Rosen::SystemBarProperty & property)953 void JsAppImpl::CalculateAvoidAreaByType(OHOS::Rosen::WindowType type,
954     const OHOS::Rosen::SystemBarProperty& property)
955 {
956     uint32_t deviceWidth = static_cast<uint32_t>(aceRunArgs.deviceWidth);
957     uint32_t deviceHeight = static_cast<uint32_t>(aceRunArgs.deviceHeight);
958     OHOS::Rosen::Window* window = GetWindow();
959     if (!window) {
960         ELOG("GetWindow failed");
961         return;
962     }
963     sptr<OHOS::Rosen::AvoidArea> statusArea(new(std::nothrow) OHOS::Rosen::AvoidArea());
964     if (!statusArea) {
965         ELOG("new OHOS::Rosen::AvoidArea failed");
966         return;
967     }
968     if (OHOS::Rosen::WindowType::WINDOW_TYPE_STATUS_BAR == type) {
969         if (property.enable_) {
970             statusArea->topRect_ = {0, 0, deviceWidth, avoidInitialAreas.topRect.height};
971             window->UpdateAvoidArea(statusArea, OHOS::Rosen::AvoidAreaType::TYPE_SYSTEM);
972         } else {
973             statusArea->topRect_ = {0, 0, 0, 0};
974             window->UpdateAvoidArea(statusArea, OHOS::Rosen::AvoidAreaType::TYPE_SYSTEM);
975         }
976         UpdateAvoidArea2Ide("topRect", statusArea->topRect_);
977     } else if (OHOS::Rosen::WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR == type) {
978         if (property.enable_) {
979             statusArea->bottomRect_ = {0, deviceHeight - avoidInitialAreas.bottomRect.height,
980                 deviceWidth, avoidInitialAreas.bottomRect.height};
981             window->UpdateAvoidArea(statusArea, OHOS::Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR);
982         } else {
983             statusArea->bottomRect_ = {0, 0, 0, 0};
984             window->UpdateAvoidArea(statusArea, OHOS::Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR);
985         }
986         UpdateAvoidArea2Ide("bottomRect", statusArea->bottomRect_);
987     } else {
988         return; // currently not support
989     }
990 }
991 
UpdateAvoidArea2Ide(const std::string & key,const OHOS::Rosen::Rect & value)992 void JsAppImpl::UpdateAvoidArea2Ide(const std::string& key, const OHOS::Rosen::Rect& value)
993 {
994     Json2::Value son = JsonReader::CreateObject();
995     son.Add("posX", value.posX_);
996     son.Add("posY", value.posY_);
997     son.Add("width", value.width_);
998     son.Add("height", value.height_);
999     Json2::Value val = JsonReader::CreateObject();
1000     val.Add(key.c_str(), son);
1001     CommandLineInterface::GetInstance().CreatCommandToSendData("AvoidAreaChanged", val, "get");
1002 }
1003 
GetWindow() const1004 OHOS::Rosen::Window* JsAppImpl::GetWindow() const
1005 {
1006     if (isDebug && debugServerPort >= 0) {
1007 #if defined(__APPLE__) || defined(_WIN32)
1008         return OHOS::Previewer::PreviewerWindow::GetInstance().GetWindowObject();
1009 #else
1010         return nullptr;
1011 #endif
1012     } else {
1013         return ability->GetWindow();
1014     }
1015 }
1016 
InitAvoidAreas(OHOS::Rosen::Window * window)1017 void JsAppImpl::InitAvoidAreas(OHOS::Rosen::Window* window)
1018 {
1019     CalculateAvoidAreaByType(OHOS::Rosen::WindowType::WINDOW_TYPE_STATUS_BAR,
1020         window->GetSystemBarPropertyByType(OHOS::Rosen::WindowType::WINDOW_TYPE_STATUS_BAR));
1021     CalculateAvoidAreaByType(OHOS::Rosen::WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR,
1022         window->GetSystemBarPropertyByType(OHOS::Rosen::WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR));
1023 }
1024 
InitJsApp()1025 void JsAppImpl::InitJsApp()
1026 {
1027     CommandParser& parser = CommandParser::GetInstance();
1028     InitCommandInfo();
1029     SetJsAppPath(parser.Value("j"));
1030     if (parser.IsSet("s")) {
1031         SetPipeName(parser.Value("s"));
1032     }
1033     if (parser.IsSet("url")) {
1034         SetUrlPath(parser.Value("url"));
1035     }
1036     if (parser.IsSet("lws")) {
1037         SetPipePort(parser.Value("lws"));
1038     }
1039     if (parser.IsSet("cm")) {
1040         SetArgsColorMode(parser.Value("cm"));
1041     }
1042     if (parser.IsSet("av")) {
1043         SetArgsAceVersion(parser.Value("av"));
1044     }
1045     if (parser.IsSet("sd")) {
1046         SetScreenDensity(parser.Value("sd"));
1047     }
1048     if (parser.IsSet("cc")) {
1049         SetConfigChanges(parser.Value("cc"));
1050     }
1051     if (commandInfo.compressionResolutionWidth <= commandInfo.compressionResolutionHeight) {
1052         SetDeviceOrentation("portrait");
1053     } else {
1054         SetDeviceOrentation("landscape");
1055     }
1056     if (parser.IsSet("d")) {
1057         SetIsDebug(true);
1058         if (parser.IsSet("p")) {
1059             SetDebugServerPort(static_cast<uint16_t>(atoi(parser.Value("p").c_str())));
1060         }
1061     }
1062 #ifdef COMPONENT_TEST_ENABLED
1063     if (parser.IsSet("componentTest")) {
1064         SetComponentTestModeConfig(parser.Value("componentTest"));
1065     }
1066 #endif // COMPONENT_TEST_ENABLED
1067     VirtualScreenImpl::GetInstance().InitFoldParams();
1068     Start();
1069 }
1070 
1071 #ifdef COMPONENT_TEST_ENABLED
SetComponentTestConfig(Platform::AceRunArgs & args,const std::string componentTest) const1072 void JsAppImpl::SetComponentTestConfig(Platform::AceRunArgs& args, const std::string componentTest) const
1073 {
1074     args.componentTestConfig = componentTest;
1075     args.isComponentTestMode = true;
1076 }
1077 
SetComponentTestModeConfig(const std::string value)1078 void JsAppImpl::SetComponentTestModeConfig(const std::string value)
1079 {
1080     componentTestModeConfig = value;
1081 }
1082 #endif // COMPONENT_TEST_ENABLED
1083 
StopAbility()1084 void JsAppImpl::StopAbility()
1085 {
1086     if (listener) {
1087         OHOS::Rosen::Window* window = GetWindow();
1088         if (window) {
1089             window->UnRegisterSystemBarEnableListener(sptr<OHOS::Rosen::IWindowSystemBarEnableListener>(listener));
1090             listener = nullptr;
1091         }
1092     }
1093     if (isDebug && debugServerPort >= 0) {
1094 #if defined(__APPLE__) || defined(_WIN32)
1095         if (simulator) {
1096             simulator->TerminateAbility(debugAbilityId);
1097         }
1098 #endif
1099     } else {
1100         ability = nullptr;
1101     }
1102     OHOS::Ide::StageContext::GetInstance().ReleaseHspBuffers();
1103     if (glfwRenderContext != nullptr) {
1104         glfwRenderContext->DestroyWindow();
1105         glfwRenderContext->Terminate();
1106         ILOG("glfw Terminate finished");
1107     }
1108 }
1109 
InitCommandInfo()1110 void JsAppImpl::InitCommandInfo()
1111 {
1112     CommandParser::GetInstance().GetCommandInfo(commandInfo);
1113 }
1114 
InitScreenInfo()1115 void JsAppImpl::InitScreenInfo()
1116 {
1117     screenInfo = VirtualScreenImpl::GetInstance().GetScreenInfo();
1118 }
1119