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