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