1 /*
2 * Copyright (c) 2021-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 "adapter/ohos/entrance/ace_ability.h"
17
18 #include <regex>
19 #include <ui/rs_surface_node.h>
20
21 #include "ability_process.h"
22 #include "dm/display_manager.h"
23 #include "form_utils_impl.h"
24 #include "init_data.h"
25 #include "ipc_skeleton.h"
26 #include "res_config.h"
27 #include "resource_manager.h"
28 #include "session_info.h"
29 #include "string_wrapper.h"
30
31 #ifdef ENABLE_ROSEN_BACKEND
32 #include "render_service_client/core/ui/rs_ui_director.h"
33 #endif
34
35 #include "adapter/ohos/entrance/ace_application_info.h"
36 #include "adapter/ohos/entrance/ace_container.h"
37 #include "adapter/ohos/entrance/ace_new_pipe_judgement.h"
38 #include "adapter/ohos/entrance/ace_view_ohos.h"
39 #include "adapter/ohos/entrance/capability_registry.h"
40 #include "adapter/ohos/entrance/plugin_utils_impl.h"
41 #include "adapter/ohos/entrance/utils.h"
42 #include "base/geometry/rect.h"
43 #include "base/subwindow/subwindow_manager.h"
44 #include "base/utils/system_properties.h"
45 #include "base/utils/utils.h"
46 #include "core/common/ace_engine.h"
47 #include "core/common/container_scope.h"
48 #include "core/common/form_manager.h"
49 #include "core/common/frontend.h"
50 #include "core/common/layout_inspector.h"
51 #include "core/common/plugin_manager.h"
52 #include "core/common/plugin_utils.h"
53 namespace OHOS {
54 namespace Ace {
55 namespace {
56
57 const std::string ABS_BUNDLE_CODE_PATH = "/data/app/el1/bundle/public/";
58 const std::string LOCAL_BUNDLE_CODE_PATH = "/data/storage/el1/bundle/";
59 const std::string FILE_SEPARATOR = "/";
60 const std::string ACTION_VIEWDATA = "ohos.want.action.viewData";
61 constexpr int32_t PLATFORM_VERSION_TEN = 10;
62 static int32_t g_instanceId = 0;
63
GetFrontendType(const std::string & frontendType)64 FrontendType GetFrontendType(const std::string& frontendType)
65 {
66 if (frontendType == "normal") {
67 return FrontendType::JS;
68 } else if (frontendType == "form") {
69 return FrontendType::JS_CARD;
70 } else if (frontendType == "declarative") {
71 return FrontendType::DECLARATIVE_JS;
72 } else {
73 return FrontendType::JS;
74 }
75 }
76
GetFrontendTypeFromManifest(const std::string & packagePath,const std::string & srcPath,bool isHap)77 FrontendType GetFrontendTypeFromManifest(const std::string& packagePath, const std::string& srcPath, bool isHap)
78 {
79 std::string manifest = std::string("assets/js/default/manifest.json");
80 if (!srcPath.empty()) {
81 manifest = "assets/js/" + srcPath + "/manifest.json";
82 }
83 std::string jsonStr = isHap ? GetStringFromHap(packagePath, manifest) : GetStringFromFile(packagePath, manifest);
84 if (jsonStr.empty()) {
85 LOGE("return default frontend: JS frontend.");
86 return FrontendType::JS;
87 }
88 auto rootJson = JsonUtil::ParseJsonString(jsonStr);
89 if (rootJson == nullptr) {
90 LOGE("return default frontend: JS frontend.");
91 return FrontendType::JS;
92 }
93 auto mode = rootJson->GetObject("mode");
94 if (mode != nullptr) {
95 if (mode->GetString("syntax") == "ets" || mode->GetString("type") == "pageAbility") {
96 return FrontendType::DECLARATIVE_JS;
97 }
98 }
99 return GetFrontendType(rootJson->GetString("type"));
100 }
101
102 } // namespace
103
104 using namespace OHOS::AAFwk;
105 using namespace OHOS::AppExecFwk;
106
107 using AcePlatformFinish = std::function<void()>;
108 using AcePlatformStartAbility = std::function<void(const std::string& address)>;
109 class AcePlatformEventCallback final : public Platform::PlatformEventCallback {
110 public:
AcePlatformEventCallback(AcePlatformFinish onFinish)111 explicit AcePlatformEventCallback(AcePlatformFinish onFinish) : onFinish_(onFinish) {}
AcePlatformEventCallback(AcePlatformFinish onFinish,AcePlatformStartAbility onStartAbility)112 AcePlatformEventCallback(AcePlatformFinish onFinish, AcePlatformStartAbility onStartAbility)
113 : onFinish_(onFinish), onStartAbility_(onStartAbility)
114 {}
115
116 ~AcePlatformEventCallback() override = default;
117
OnFinish() const118 void OnFinish() const override
119 {
120 LOGI("AcePlatformEventCallback OnFinish");
121 CHECK_NULL_VOID_NOLOG(onFinish_);
122 onFinish_();
123 }
124
OnStartAbility(const std::string & address)125 void OnStartAbility(const std::string& address) override
126 {
127 LOGI("AcePlatformEventCallback OnStartAbility");
128 CHECK_NULL_VOID_NOLOG(onStartAbility_);
129 onStartAbility_(address);
130 }
131
OnStatusBarBgColorChanged(uint32_t color)132 void OnStatusBarBgColorChanged(uint32_t color) override
133 {
134 LOGI("AcePlatformEventCallback OnStatusBarBgColorChanged");
135 }
136
137 private:
138 AcePlatformFinish onFinish_;
139 AcePlatformStartAbility onStartAbility_;
140 };
141
142 const std::string AceAbility::START_PARAMS_KEY = "__startParams";
143 const std::string AceAbility::PAGE_URI = "url";
144 const std::string AceAbility::CONTINUE_PARAMS_KEY = "__remoteData";
145
REGISTER_AA(AceAbility)146 REGISTER_AA(AceAbility)
147 void AceWindowListener::OnDrag(int32_t x, int32_t y, OHOS::Rosen::DragEvent event)
148 {
149 CHECK_NULL_VOID(callbackOwner_);
150 callbackOwner_->OnDrag(x, y, event);
151 }
152
OnSizeChange(const sptr<OHOS::Rosen::OccupiedAreaChangeInfo> & info,const std::shared_ptr<OHOS::Rosen::RSTransaction> & rsTransaction)153 void AceWindowListener::OnSizeChange(const sptr<OHOS::Rosen::OccupiedAreaChangeInfo>& info,
154 const std::shared_ptr<OHOS::Rosen::RSTransaction>& rsTransaction)
155 {
156 CHECK_NULL_VOID(callbackOwner_);
157 callbackOwner_->OnSizeChange(info, rsTransaction);
158 }
159
SetBackgroundColor(uint32_t color)160 void AceWindowListener::SetBackgroundColor(uint32_t color)
161 {
162 CHECK_NULL_VOID(callbackOwner_);
163 callbackOwner_->SetBackgroundColor(color);
164 }
165
GetBackgroundColor()166 uint32_t AceWindowListener::GetBackgroundColor()
167 {
168 CHECK_NULL_RETURN(callbackOwner_, 0);
169 return callbackOwner_->GetBackgroundColor();
170 }
171
OnSizeChange(OHOS::Rosen::Rect rect,OHOS::Rosen::WindowSizeChangeReason reason,const std::shared_ptr<OHOS::Rosen::RSTransaction> & rsTransaction)172 void AceWindowListener::OnSizeChange(OHOS::Rosen::Rect rect, OHOS::Rosen::WindowSizeChangeReason reason,
173 const std::shared_ptr<OHOS::Rosen::RSTransaction>& rsTransaction)
174 {
175 CHECK_NULL_VOID(callbackOwner_);
176 callbackOwner_->OnSizeChange(rect, reason, rsTransaction);
177 }
178
OnModeChange(OHOS::Rosen::WindowMode mode,bool hasDeco)179 void AceWindowListener::OnModeChange(OHOS::Rosen::WindowMode mode, bool hasDeco)
180 {
181 CHECK_NULL_VOID(callbackOwner_);
182 callbackOwner_->OnModeChange(mode, hasDeco);
183 }
184
OnInputEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent) const185 bool AceWindowListener::OnInputEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const
186 {
187 CHECK_NULL_RETURN(callbackOwner_, false);
188 return callbackOwner_->OnInputEvent(keyEvent);
189 }
190
OnInputEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent) const191 bool AceWindowListener::OnInputEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const
192 {
193 CHECK_NULL_RETURN(callbackOwner_, false);
194 return callbackOwner_->OnInputEvent(pointerEvent);
195 }
196
OnInputEvent(const std::shared_ptr<MMI::AxisEvent> & axisEvent) const197 bool AceWindowListener::OnInputEvent(const std::shared_ptr<MMI::AxisEvent>& axisEvent) const
198 {
199 CHECK_NULL_RETURN(callbackOwner_, false);
200 return callbackOwner_->OnInputEvent(axisEvent);
201 }
202
OnAvoidAreaChanged(const OHOS::Rosen::AvoidArea avoidArea,OHOS::Rosen::AvoidAreaType type)203 void AceWindowListener::OnAvoidAreaChanged(const OHOS::Rosen::AvoidArea avoidArea, OHOS::Rosen::AvoidAreaType type)
204 {
205 CHECK_NULL_VOID(callbackOwner_);
206 return callbackOwner_->OnAvoidAreaChanged(avoidArea, type);
207 }
208
209 AceAbility::AceAbility() = default;
210
OnStart(const Want & want,sptr<AAFwk::SessionInfo> sessionInfo)211 void AceAbility::OnStart(const Want& want, sptr<AAFwk::SessionInfo> sessionInfo)
212 {
213 Ability::OnStart(want, sessionInfo);
214 LOGI("AceAbility::OnStart called");
215 abilityId_ = g_instanceId++;
216 static std::once_flag onceFlag;
217 auto abilityContext = GetAbilityContext();
218 auto cacheDir = abilityContext->GetCacheDir();
219 std::call_once(onceFlag, [abilityContext, cacheDir]() {
220 LOGI("Initialize for current process.");
221 SetHwIcuDirectory();
222 Container::UpdateCurrent(INSTANCE_ID_PLATFORM);
223 CapabilityRegistry::Register();
224 AceApplicationInfo::GetInstance().SetPackageName(abilityContext->GetBundleName());
225 AceApplicationInfo::GetInstance().SetDataFileDirPath(abilityContext->GetFilesDir());
226 AceApplicationInfo::GetInstance().SetApiTargetVersion(abilityContext->GetApplicationInfo()->apiTargetVersion);
227 AceApplicationInfo::GetInstance().SetAppVersionName(abilityContext->GetApplicationInfo()->versionName);
228 AceApplicationInfo::GetInstance().SetAppVersionCode(abilityContext->GetApplicationInfo()->versionCode);
229 AceApplicationInfo::GetInstance().SetUid(IPCSkeleton::GetCallingUid());
230 AceApplicationInfo::GetInstance().SetPid(IPCSkeleton::GetCallingPid());
231 ImageCache::SetImageCacheFilePath(cacheDir);
232 ImageCache::SetCacheFileInfo();
233 AceEngine::InitJsDumpHeadSignal();
234 });
235 AceNewPipeJudgement::InitAceNewPipeConfig();
236 // TODO: now choose pipeline using param set as package name, later enable for all.
237 auto apiCompatibleVersion = abilityContext->GetApplicationInfo()->apiCompatibleVersion;
238 auto apiReleaseType = abilityContext->GetApplicationInfo()->apiReleaseType;
239 auto apiTargetVersion = abilityContext->GetApplicationInfo()->apiTargetVersion;
240 auto useNewPipe = AceNewPipeJudgement::QueryAceNewPipeEnabledFA(
241 AceApplicationInfo::GetInstance().GetPackageName(), apiCompatibleVersion, apiTargetVersion, apiReleaseType);
242 LOGI("AceAbility: apiCompatibleVersion: %{public}d, apiTargetVersion: %{public}d, and apiReleaseType: %{public}s, "
243 "useNewPipe: %{public}d",
244 apiCompatibleVersion, apiTargetVersion, apiReleaseType.c_str(), useNewPipe);
245 OHOS::sptr<OHOS::Rosen::Window> window = Ability::GetWindow();
246 std::shared_ptr<AceAbility> self = std::static_pointer_cast<AceAbility>(shared_from_this());
247 OHOS::sptr<AceWindowListener> aceWindowListener = new AceWindowListener(self);
248 // register surface change callback and window mode change callback
249 window->RegisterWindowChangeListener(aceWindowListener);
250 // register drag event callback
251 window->RegisterDragListener(aceWindowListener);
252 // register Occupied Area callback
253 window->RegisterOccupiedAreaChangeListener(aceWindowListener);
254 // register ace ability handler callback
255 window->SetAceAbilityHandler(aceWindowListener);
256 // register input consumer callback
257 std::shared_ptr<AceWindowListener> aceInputConsumer = std::make_shared<AceWindowListener>(self);
258 window->SetInputEventConsumer(aceInputConsumer);
259
260 int32_t deviceWidth = 0;
261 int32_t deviceHeight = 0;
262 auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
263 if (defaultDisplay) {
264 density_ = defaultDisplay->GetVirtualPixelRatio();
265 deviceWidth = defaultDisplay->GetWidth();
266 deviceHeight = defaultDisplay->GetHeight();
267 LOGI("deviceWidth: %{public}d, deviceHeight: %{public}d, default density: %{public}f", deviceWidth,
268 deviceHeight, density_);
269 }
270 SystemProperties::InitDeviceInfo(deviceWidth, deviceHeight, deviceHeight >= deviceWidth ? 0 : 1, density_, false);
271 SystemProperties::SetColorMode(ColorMode::LIGHT);
272
273 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
274 auto resourceManager = GetResourceManager();
275 if (resourceManager != nullptr) {
276 resourceManager->GetResConfig(*resConfig);
277 auto localeInfo = resConfig->GetLocaleInfo();
278 Platform::AceApplicationInfoImpl::GetInstance().SetResourceManager(resourceManager);
279 if (localeInfo != nullptr) {
280 auto language = localeInfo->getLanguage();
281 auto region = localeInfo->getCountry();
282 auto script = localeInfo->getScript();
283 AceApplicationInfo::GetInstance().SetLocale((language == nullptr) ? "" : language,
284 (region == nullptr) ? "" : region, (script == nullptr) ? "" : script, "");
285 } else {
286 LOGW("localeInfo is null.");
287 AceApplicationInfo::GetInstance().SetLocale("", "", "", "");
288 }
289 if (resConfig->GetColorMode() == OHOS::Global::Resource::ColorMode::DARK) {
290 SystemProperties::SetColorMode(ColorMode::DARK);
291 LOGI("UIContent set dark mode");
292 } else {
293 SystemProperties::SetColorMode(ColorMode::LIGHT);
294 LOGI("UIContent set light mode");
295 }
296 SystemProperties::SetDeviceAccess(
297 resConfig->GetInputDevice() == Global::Resource::InputDevice::INPUTDEVICE_POINTINGDEVICE);
298 } else {
299 LOGW("resourceManager is null.");
300 AceApplicationInfo::GetInstance().SetLocale("", "", "", "");
301 }
302
303 auto packagePathStr = GetBundleCodePath();
304 auto moduleInfo = GetHapModuleInfo();
305 CHECK_NULL_VOID_NOLOG(moduleInfo);
306 packagePathStr += "/" + moduleInfo->package + "/";
307 std::shared_ptr<AbilityInfo> info = GetAbilityInfo();
308 std::string srcPath;
309 if (info != nullptr && !info->srcPath.empty()) {
310 srcPath = info->srcPath;
311 }
312 if (info != nullptr && !info->bundleName.empty()) {
313 AceApplicationInfo::GetInstance().SetPackageName(info->bundleName);
314 }
315
316 bool isHap = moduleInfo ? !moduleInfo->hapPath.empty() : false;
317 std::string& packagePath = isHap ? moduleInfo->hapPath : packagePathStr;
318 FrontendType frontendType = GetFrontendTypeFromManifest(packagePath, srcPath, isHap);
319 useNewPipe = useNewPipe && (frontendType == FrontendType::ETS_CARD || frontendType == FrontendType::DECLARATIVE_JS);
320 if (frontendType != FrontendType::ETS_CARD && frontendType != FrontendType::DECLARATIVE_JS) {
321 LOGI("AceAbility: JS project use old pipeline");
322 }
323 #ifdef ENABLE_ROSEN_BACKEND
324 std::shared_ptr<OHOS::Rosen::RSUIDirector> rsUiDirector;
325 if (SystemProperties::GetRosenBackendEnabled() && !useNewPipe) {
326 rsUiDirector = OHOS::Rosen::RSUIDirector::Create();
327 auto surfaceNode = window->GetSurfaceNode();
328 rsUiDirector->SetRSSurfaceNode(surfaceNode);
329 rsUiDirector->SetCacheDir(cacheDir);
330 rsUiDirector->Init();
331 }
332 #endif
333 AceApplicationInfo::GetInstance().SetAbilityName(info ? info->name : "");
334 std::string moduleName = info ? info->moduleName : "";
335 std::string moduleHapPath = info ? info->hapPath : "";
336
337 std::shared_ptr<ApplicationInfo> appInfo = GetApplicationInfo();
338 std::vector<ModuleInfo> moduleList = appInfo->moduleInfos;
339
340 std::string resPath;
341 for (const auto& module : moduleList) {
342 if (module.moduleName == moduleName && info != nullptr) {
343 std::regex pattern(ABS_BUNDLE_CODE_PATH + info->bundleName + FILE_SEPARATOR);
344 auto moduleSourceDir = std::regex_replace(module.moduleSourceDir, pattern, LOCAL_BUNDLE_CODE_PATH);
345 resPath = moduleSourceDir + "/assets/" + module.moduleName + FILE_SEPARATOR;
346 break;
347 }
348 }
349 std::string hapPath;
350 if (!moduleHapPath.empty()) {
351 if (moduleHapPath.find(ABS_BUNDLE_CODE_PATH) == std::string::npos) {
352 hapPath = moduleHapPath;
353 } else {
354 auto pos = moduleHapPath.find_last_of('/');
355 if (pos != std::string::npos) {
356 hapPath = LOCAL_BUNDLE_CODE_PATH + moduleHapPath.substr(pos + 1);
357 LOGI("In FA mode, hapPath:%{private}s", hapPath.c_str());
358 }
359 }
360 }
361
362 AceApplicationInfo::GetInstance().SetDebug(appInfo->debug, want.GetBoolParam("debugApp", false));
363
364 auto pluginUtils = std::make_shared<PluginUtilsImpl>();
365 PluginManager::GetInstance().SetAceAbility(this, pluginUtils);
366 auto formUtils = std::make_shared<FormUtilsImpl>();
367 FormManager::GetInstance().SetFormUtils(formUtils);
368 // create container
369 Platform::AceContainer::CreateContainer(abilityId_, frontendType, srcPath, shared_from_this(),
370 std::make_unique<AcePlatformEventCallback>([this]() { TerminateAbility(); },
371 [this](const std::string& address) {
372 AAFwk::Want want;
373 want.AddEntity(Want::ENTITY_BROWSER);
374 want.SetUri(address);
375 want.SetAction(ACTION_VIEWDATA);
376 this->StartAbility(want);
377 }),
378 false, useNewPipe);
379 auto container = Platform::AceContainer::GetContainer(abilityId_);
380 CHECK_NULL_VOID(container);
381 container->SetToken(token_);
382 auto aceResCfg = container->GetResourceConfiguration();
383 aceResCfg.SetOrientation(SystemProperties::GetDeviceOrientation());
384 aceResCfg.SetDensity(SystemProperties::GetResolution());
385 aceResCfg.SetDeviceType(SystemProperties::GetDeviceType());
386 aceResCfg.SetColorMode(SystemProperties::GetColorMode());
387 aceResCfg.SetDeviceAccess(SystemProperties::GetDeviceAccess());
388 container->SetResourceConfiguration(aceResCfg);
389 container->SetPackagePathStr(resPath);
390 container->SetHapPath(hapPath);
391 container->SetBundlePath(abilityContext->GetBundleCodeDir());
392 container->SetFilesDataPath(abilityContext->GetFilesDir());
393 if (window->IsDecorEnable()) {
394 LOGI("Container modal is enabled.");
395 container->SetWindowModal(WindowModal::CONTAINER_MODAL);
396 }
397 container->SetWindowName(window->GetWindowName());
398 container->SetWindowId(window->GetWindowId());
399 SubwindowManager::GetInstance()->AddContainerId(window->GetWindowId(), abilityId_);
400 // create view.
401 auto aceView = Platform::AceViewOhos::CreateView(abilityId_);
402 Platform::AceViewOhos::SurfaceCreated(aceView, window);
403
404 if (srcPath.empty()) {
405 auto assetBasePathStr = { std::string("assets/js/default/"), std::string("assets/js/share/") };
406 Platform::AceContainer::AddAssetPath(abilityId_, packagePathStr, moduleInfo->hapPath, assetBasePathStr);
407 } else {
408 auto assetBasePathStr = { "assets/js/" + srcPath + "/", std::string("assets/js/share/"),
409 std::string("assets/js/") };
410 Platform::AceContainer::AddAssetPath(abilityId_, packagePathStr, moduleInfo->hapPath, assetBasePathStr);
411 }
412
413 if (!useNewPipe) {
414 Ace::Platform::UIEnvCallback callback = nullptr;
415 #ifdef ENABLE_ROSEN_BACKEND
416 callback = [window, id = abilityId_, aceView, rsUiDirector](
417 const OHOS::Ace::RefPtr<OHOS::Ace::PipelineContext>& context) mutable {
418 if (rsUiDirector) {
419 rsUiDirector->SetUITaskRunner(
420 [taskExecutor = Platform::AceContainer::GetContainer(id)->GetTaskExecutor(), id](
421 const std::function<void()>& task) {
422 ContainerScope scope(id);
423 taskExecutor->PostTask(task, TaskExecutor::TaskType::UI);
424 });
425 if (context != nullptr) {
426 context->SetRSUIDirector(rsUiDirector);
427 }
428 LOGI("Init Rosen Backend");
429 } else {
430 LOGI("not Init Rosen Backend");
431 }
432 };
433 #endif
434 // set view
435 Platform::AceContainer::SetView(aceView, density_, 0, 0, window, callback);
436 } else {
437 Platform::AceContainer::SetViewNew(aceView, density_, 0, 0, window);
438 }
439
440 Platform::AceViewOhos::SurfaceChanged(aceView, 0, 0, deviceHeight >= deviceWidth ? 0 : 1);
441
442 // action event handler
443 auto&& actionEventHandler = [this](const std::string& action) {
444 LOGI("on Action called to event handler");
445
446 auto eventAction = JsonUtil::ParseJsonString(action);
447 auto bundleName = eventAction->GetValue("bundleName");
448 auto abilityName = eventAction->GetValue("abilityName");
449 auto params = eventAction->GetValue("params");
450 auto bundle = bundleName->GetString();
451 auto ability = abilityName->GetString();
452 LOGI("bundle:%{public}s ability:%{public}s, params:%{public}s", bundle.c_str(), ability.c_str(),
453 params->GetString().c_str());
454 if (bundle.empty() || ability.empty()) {
455 LOGE("action ability or bundle is empty");
456 return;
457 }
458
459 AAFwk::Want want;
460 want.SetElementName(bundle, ability);
461 this->StartAbility(want);
462 };
463
464 // set window id & action event handler
465 auto context = Platform::AceContainer::GetContainer(abilityId_)->GetPipelineContext();
466 if (context) {
467 context->SetActionEventHandler(actionEventHandler);
468 context->SetGetWindowRectImpl([window]() -> Rect {
469 Rect rect;
470 CHECK_NULL_RETURN_NOLOG(window, rect);
471 auto windowRect = window->GetRect();
472 rect.SetRect(windowRect.posX_, windowRect.posY_, windowRect.width_, windowRect.height_);
473 return rect;
474 });
475 auto rsConfig = window->GetKeyboardAnimationConfig();
476 KeyboardAnimationConfig config = { rsConfig.curveType_, rsConfig.curveParams_, rsConfig.durationIn_,
477 rsConfig.durationOut_ };
478 context->SetKeyboardAnimationConfig(config);
479 context->SetMinPlatformVersion(apiCompatibleVersion);
480
481 if (apiCompatibleVersion >= PLATFORM_VERSION_TEN && context->GetIsAppWindow()) {
482 context->UpdateSystemSafeArea(container->GetViewSafeAreaByType(Rosen::AvoidAreaType::TYPE_SYSTEM));
483 context->UpdateCutoutSafeArea(container->GetViewSafeAreaByType(Rosen::AvoidAreaType::TYPE_CUTOUT));
484 }
485 }
486
487 // get url
488 std::string parsedPageUrl;
489 if (!remotePageUrl_.empty()) {
490 parsedPageUrl = remotePageUrl_;
491 } else if (!pageUrl_.empty()) {
492 parsedPageUrl = pageUrl_;
493 } else if (want.HasParameter(PAGE_URI)) {
494 parsedPageUrl = want.GetStringParam(PAGE_URI);
495 } else {
496 parsedPageUrl = "";
497 }
498
499 auto windowRect = window->GetRect();
500 if (!windowRect.IsUninitializedRect()) {
501 LOGI("notify window rect explicitly");
502 OnSizeChange(windowRect, OHOS::Rosen::WindowSizeChangeReason::UNDEFINED);
503 }
504 // run page.
505 Platform::AceContainer::RunPage(abilityId_, Platform::AceContainer::GetContainer(abilityId_)->GeneratePageId(),
506 parsedPageUrl, want.GetStringParam(START_PARAMS_KEY));
507
508 if (!remoteData_.empty()) {
509 Platform::AceContainer::OnRestoreData(abilityId_, remoteData_);
510 }
511 LayoutInspector::SetCallback(abilityId_);
512 LOGI("AceAbility::OnStart called End");
513 }
514
OnStop()515 void AceAbility::OnStop()
516 {
517 LOGI("AceAbility::OnStop called ");
518 Ability::OnStop();
519 Platform::AceContainer::DestroyContainer(abilityId_);
520 abilityId_ = -1;
521 LOGI("AceAbility::OnStop called End");
522 }
523
OnActive()524 void AceAbility::OnActive()
525 {
526 LOGI("AceAbility::OnActive called ");
527 // AbilityManager will miss first OnForeground notification
528 if (isFirstActive_) {
529 Platform::AceContainer::OnShow(abilityId_);
530 isFirstActive_ = false;
531 }
532 Ability::OnActive();
533 Platform::AceContainer::OnActive(abilityId_);
534 LOGI("AceAbility::OnActive called End");
535 }
536
OnForeground(const Want & want)537 void AceAbility::OnForeground(const Want& want)
538 {
539 LOGI("AceAbility::OnForeground called ");
540 Ability::OnForeground(want);
541 Platform::AceContainer::OnShow(abilityId_);
542 LOGI("AceAbility::OnForeground called End");
543 }
544
OnBackground()545 void AceAbility::OnBackground()
546 {
547 LOGI("AceAbility::OnBackground called ");
548 Ability::OnBackground();
549 Platform::AceContainer::OnHide(abilityId_);
550 LOGI("AceAbility::OnBackground called End");
551 }
552
OnInactive()553 void AceAbility::OnInactive()
554 {
555 LOGI("AceAbility::OnInactive called ");
556 Ability::OnInactive();
557 Platform::AceContainer::OnInactive(abilityId_);
558 LOGI("AceAbility::OnInactive called End");
559 }
560
OnBackPressed()561 void AceAbility::OnBackPressed()
562 {
563 LOGI("AceAbility::OnBackPressed called ");
564 if (!Platform::AceContainer::OnBackPressed(abilityId_)) {
565 LOGI("AceAbility::OnBackPressed: passed to Ability to process");
566 Ability::OnBackPressed();
567 }
568 LOGI("AceAbility::OnBackPressed called End");
569 }
570
OnNewWant(const Want & want)571 void AceAbility::OnNewWant(const Want& want)
572 {
573 LOGI("AceAbility::OnNewWant called ");
574 Ability::OnNewWant(want);
575 std::string params = want.GetStringParam(START_PARAMS_KEY);
576 Platform::AceContainer::OnNewRequest(abilityId_, params);
577 std::string data = want.ToString();
578 Platform::AceContainer::OnNewWant(abilityId_, data);
579 LOGI("AceAbility::OnNewWant called End");
580 }
581
OnRestoreAbilityState(const PacMap & inState)582 void AceAbility::OnRestoreAbilityState(const PacMap& inState)
583 {
584 LOGI("AceAbility::OnRestoreAbilityState called ");
585 Ability::OnRestoreAbilityState(inState);
586 LOGI("AceAbility::OnRestoreAbilityState called End");
587 }
588
OnSaveAbilityState(PacMap & outState)589 void AceAbility::OnSaveAbilityState(PacMap& outState)
590 {
591 LOGI("AceAbility::OnSaveAbilityState called ");
592 Ability::OnSaveAbilityState(outState);
593 LOGI("AceAbility::OnSaveAbilityState called End");
594 }
595
OnConfigurationUpdated(const Configuration & configuration)596 void AceAbility::OnConfigurationUpdated(const Configuration& configuration)
597 {
598 LOGI("AceAbility::OnConfigurationUpdated called ");
599 Ability::OnConfigurationUpdated(configuration);
600
601 auto container = Platform::AceContainer::GetContainer(abilityId_);
602 CHECK_NULL_VOID(container);
603 auto taskExecutor = container->GetTaskExecutor();
604 CHECK_NULL_VOID(taskExecutor);
605 taskExecutor->PostTask(
606 [weakContainer = WeakPtr<Platform::AceContainer>(container), configuration]() {
607 auto container = weakContainer.Upgrade();
608 CHECK_NULL_VOID_NOLOG(container);
609 auto colorMode = configuration.GetItem(OHOS::AppExecFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
610 auto deviceAccess = configuration.GetItem(OHOS::AppExecFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE);
611 auto languageTag = configuration.GetItem(OHOS::AppExecFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE);
612 container->UpdateConfiguration(colorMode, deviceAccess, languageTag, configuration.GetName());
613 },
614 TaskExecutor::TaskType::UI);
615 LOGI("AceAbility::OnConfigurationUpdated called End, name:%{public}s", configuration.GetName().c_str());
616 }
617
OnAbilityResult(int requestCode,int resultCode,const OHOS::AAFwk::Want & resultData)618 void AceAbility::OnAbilityResult(int requestCode, int resultCode, const OHOS::AAFwk::Want& resultData)
619 {
620 LOGI("AceAbility::OnAbilityResult called ");
621 AbilityProcess::GetInstance()->OnAbilityResult(this, requestCode, resultCode, resultData);
622 LOGI("AceAbility::OnAbilityResult called End");
623 }
624
OnStartContinuation()625 bool AceAbility::OnStartContinuation()
626 {
627 LOGI("AceAbility::OnStartContinuation called.");
628 bool ret = Platform::AceContainer::OnStartContinuation(abilityId_);
629 LOGI("AceAbility::OnStartContinuation finish.");
630 return ret;
631 }
632
OnSaveData(OHOS::AAFwk::WantParams & saveData)633 bool AceAbility::OnSaveData(OHOS::AAFwk::WantParams& saveData)
634 {
635 LOGI("AceAbility::OnSaveData called.");
636 std::string data = Platform::AceContainer::OnSaveData(abilityId_);
637 if (data == "false") {
638 return false;
639 }
640 auto json = JsonUtil::ParseJsonString(data);
641 if (!json) {
642 return false;
643 }
644 if (json->Contains(PAGE_URI)) {
645 saveData.SetParam(PAGE_URI, OHOS::AAFwk::String::Box(json->GetString(PAGE_URI)));
646 }
647 if (json->Contains(CONTINUE_PARAMS_KEY)) {
648 std::string params = json->GetObject(CONTINUE_PARAMS_KEY)->ToString();
649 saveData.SetParam(CONTINUE_PARAMS_KEY, OHOS::AAFwk::String::Box(params));
650 }
651 LOGI("AceAbility::OnSaveData finish.");
652 return true;
653 }
654
OnRestoreData(OHOS::AAFwk::WantParams & restoreData)655 bool AceAbility::OnRestoreData(OHOS::AAFwk::WantParams& restoreData)
656 {
657 LOGI("AceAbility::OnRestoreData called.");
658 if (restoreData.HasParam(PAGE_URI)) {
659 auto value = restoreData.GetParam(PAGE_URI);
660 OHOS::AAFwk::IString* ao = OHOS::AAFwk::IString::Query(value);
661 if (ao != nullptr) {
662 remotePageUrl_ = OHOS::AAFwk::String::Unbox(ao);
663 }
664 }
665 if (restoreData.HasParam(CONTINUE_PARAMS_KEY)) {
666 auto value = restoreData.GetParam(CONTINUE_PARAMS_KEY);
667 OHOS::AAFwk::IString* ao = OHOS::AAFwk::IString::Query(value);
668 if (ao != nullptr) {
669 remoteData_ = OHOS::AAFwk::String::Unbox(ao);
670 }
671 }
672 LOGI("AceAbility::OnRestoreData finish.");
673 return true;
674 }
675
OnCompleteContinuation(int result)676 void AceAbility::OnCompleteContinuation(int result)
677 {
678 Ability::OnCompleteContinuation(result);
679 LOGI("AceAbility::OnCompleteContinuation called.");
680 Platform::AceContainer::OnCompleteContinuation(abilityId_, result);
681 LOGI("AceAbility::OnCompleteContinuation finish.");
682 }
683
OnRemoteTerminated()684 void AceAbility::OnRemoteTerminated()
685 {
686 LOGI("AceAbility::OnRemoteTerminated called.");
687 Platform::AceContainer::OnRemoteTerminated(abilityId_);
688 LOGI("AceAbility::OnRemoteTerminated finish.");
689 }
690
OnSizeChange(const OHOS::Rosen::Rect & rect,OHOS::Rosen::WindowSizeChangeReason reason,const std::shared_ptr<OHOS::Rosen::RSTransaction> & rsTransaction)691 void AceAbility::OnSizeChange(const OHOS::Rosen::Rect& rect, OHOS::Rosen::WindowSizeChangeReason reason,
692 const std::shared_ptr<OHOS::Rosen::RSTransaction>& rsTransaction)
693 {
694 LOGI("width: %{public}u, height: %{public}u, left: %{public}d, top: %{public}d", rect.width_, rect.height_,
695 rect.posX_, rect.posY_);
696 SystemProperties::SetDeviceOrientation(rect.height_ >= rect.width_ ? 0 : 1);
697 auto container = Platform::AceContainer::GetContainer(abilityId_);
698 CHECK_NULL_VOID(container);
699 container->SetWindowPos(rect.posX_, rect.posY_);
700 auto pipelineContext = container->GetPipelineContext();
701 if (pipelineContext) {
702 pipelineContext->SetDisplayWindowRectInfo(
703 Rect(Offset(rect.posX_, rect.posY_), Size(rect.width_, rect.height_)));
704 pipelineContext->SetIsLayoutFullScreen(
705 Ability::GetWindow()->GetMode() == Rosen::WindowMode::WINDOW_MODE_FULLSCREEN);
706 }
707 auto taskExecutor = container->GetTaskExecutor();
708 CHECK_NULL_VOID(taskExecutor);
709 taskExecutor->PostTask(
710 [rect, density = density_, reason, container, rsTransaction]() {
711 auto aceView = static_cast<Platform::AceViewOhos*>(container->GetView());
712 CHECK_NULL_VOID(aceView);
713 ViewportConfig config(rect.width_, rect.height_, density);
714 Platform::AceViewOhos::SetViewportMetrics(aceView, config);
715 Platform::AceViewOhos::SurfaceChanged(aceView, rect.width_, rect.height_,
716 rect.height_ >= rect.width_ ? 0 : 1, static_cast<WindowSizeChangeReason>(reason), rsTransaction);
717 },
718 TaskExecutor::TaskType::PLATFORM);
719 }
720
OnModeChange(OHOS::Rosen::WindowMode mode,bool hasDeco)721 void AceAbility::OnModeChange(OHOS::Rosen::WindowMode mode, bool hasDeco)
722 {
723 LOGI("OnModeChange, window mode is %{public}d", mode);
724 auto container = Platform::AceContainer::GetContainer(abilityId_);
725 CHECK_NULL_VOID(container);
726 auto taskExecutor = container->GetTaskExecutor();
727 CHECK_NULL_VOID(taskExecutor);
728 ContainerScope scope(abilityId_);
729 taskExecutor->PostTask(
730 [container, mode, hasDeco]() {
731 auto pipelineContext = container->GetPipelineContext();
732 CHECK_NULL_VOID(pipelineContext);
733 pipelineContext->ShowContainerTitle(mode == OHOS::Rosen::WindowMode::WINDOW_MODE_FLOATING, hasDeco);
734 },
735 TaskExecutor::TaskType::UI);
736 }
737
OnSizeChange(const sptr<OHOS::Rosen::OccupiedAreaChangeInfo> & info,const std::shared_ptr<OHOS::Rosen::RSTransaction> & rsTransaction)738 void AceAbility::OnSizeChange(const sptr<OHOS::Rosen::OccupiedAreaChangeInfo>& info,
739 const std::shared_ptr<OHOS::Rosen::RSTransaction>& rsTransaction)
740 {
741 auto rect = info->rect_;
742 auto type = info->type_;
743 Rect keyboardRect = Rect(rect.posX_, rect.posY_, rect.width_, rect.height_);
744 LOGI("AceAbility::OccupiedAreaChange rect:%{public}s type: %{public}d", keyboardRect.ToString().c_str(), type);
745 if (type == OHOS::Rosen::OccupiedAreaType::TYPE_INPUT) {
746 auto container = Platform::AceContainer::GetContainer(abilityId_);
747 CHECK_NULL_VOID(container);
748 auto taskExecutor = container->GetTaskExecutor();
749 CHECK_NULL_VOID(taskExecutor);
750 ContainerScope scope(abilityId_);
751 taskExecutor->PostTask(
752 [container, keyboardRect, rsTransaction] {
753 auto context = container->GetPipelineContext();
754 CHECK_NULL_VOID_NOLOG(context);
755 context->OnVirtualKeyboardAreaChange(keyboardRect, rsTransaction);
756 },
757 TaskExecutor::TaskType::UI);
758 }
759 }
760
Dump(const std::vector<std::string> & params,std::vector<std::string> & info)761 void AceAbility::Dump(const std::vector<std::string>& params, std::vector<std::string>& info)
762 {
763 auto container = Platform::AceContainer::GetContainer(abilityId_);
764 CHECK_NULL_VOID(container);
765 auto taskExecutor = container->GetTaskExecutor();
766 CHECK_NULL_VOID(taskExecutor);
767 ContainerScope scope(abilityId_);
768 taskExecutor->PostSyncTask(
769 [container, params, &info] { container->Dump(params, info); }, TaskExecutor::TaskType::UI);
770 }
771
OnDrag(int32_t x,int32_t y,OHOS::Rosen::DragEvent event)772 void AceAbility::OnDrag(int32_t x, int32_t y, OHOS::Rosen::DragEvent event)
773 {
774 LOGI("AceAbility::OnDrag called ");
775 auto container = Platform::AceContainer::GetContainer(abilityId_);
776 CHECK_NULL_VOID(container);
777 auto aceView = static_cast<Platform::AceViewOhos*>(container->GetView());
778 CHECK_NULL_VOID(aceView);
779 DragEventAction action;
780 switch (event) {
781 case OHOS::Rosen::DragEvent::DRAG_EVENT_END:
782 action = DragEventAction::DRAG_EVENT_END;
783 break;
784 case OHOS::Rosen::DragEvent::DRAG_EVENT_OUT:
785 action = DragEventAction::DRAG_EVENT_OUT;
786 break;
787 case OHOS::Rosen::DragEvent::DRAG_EVENT_MOVE:
788 action = DragEventAction::DRAG_EVENT_MOVE;
789 break;
790 case OHOS::Rosen::DragEvent::DRAG_EVENT_IN:
791 default:
792 action = DragEventAction::DRAG_EVENT_START;
793 break;
794 }
795
796 aceView->ProcessDragEvent(x, y, action);
797 }
798
OnInputEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent) const799 bool AceAbility::OnInputEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const
800 {
801 auto container = AceType::DynamicCast<Platform::AceContainer>(AceEngine::Get().GetContainer(abilityId_));
802 CHECK_NULL_RETURN(container, false);
803 container->SetCurPointerEvent(pointerEvent);
804 auto aceView = static_cast<Platform::AceViewOhos*>(container->GetView());
805 CHECK_NULL_RETURN(aceView, false);
806 aceView->DispatchTouchEvent(aceView, pointerEvent);
807 return true;
808 }
809
OnInputEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent) const810 bool AceAbility::OnInputEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const
811 {
812 auto container = Platform::AceContainer::GetContainer(abilityId_);
813 CHECK_NULL_RETURN(container, false);
814 auto aceView = static_cast<Platform::AceViewOhos*>(container->GetView());
815 CHECK_NULL_RETURN(aceView, false);
816 int32_t keyCode = keyEvent->GetKeyCode();
817 int32_t keyAction = keyEvent->GetKeyAction();
818 if (keyCode == MMI::KeyEvent::KEYCODE_BACK && keyAction == MMI::KeyEvent::KEY_ACTION_UP) {
819 LOGI("OnInputEvent: Platform::AceContainer::OnBackPressed called");
820 if (Platform::AceContainer::OnBackPressed(abilityId_)) {
821 LOGI("OnInputEvent: Platform::AceContainer::OnBackPressed return true");
822 return true;
823 }
824 LOGI("OnInputEvent: Platform::AceContainer::OnBackPressed return false");
825 return false;
826 }
827 LOGI("OnInputEvent: dispatch key to arkui");
828 if (aceView->DispatchKeyEvent(aceView, keyEvent)) {
829 LOGI("OnInputEvent: arkui consumed this key event");
830 return true;
831 }
832 LOGI("OnInputEvent: arkui do not consumed this key event");
833 return false;
834 }
835
OnInputEvent(const std::shared_ptr<MMI::AxisEvent> & axisEvent) const836 bool AceAbility::OnInputEvent(const std::shared_ptr<MMI::AxisEvent>& axisEvent) const
837 {
838 return false;
839 }
840
SetBackgroundColor(uint32_t color)841 void AceAbility::SetBackgroundColor(uint32_t color)
842 {
843 LOGI("AceAbilityHandler::SetBackgroundColor color is %{public}u", color);
844 auto container = Platform::AceContainer::GetContainer(abilityId_);
845 CHECK_NULL_VOID(container);
846 ContainerScope scope(abilityId_);
847 auto taskExecutor = container->GetTaskExecutor();
848 CHECK_NULL_VOID(taskExecutor);
849 taskExecutor->PostSyncTask(
850 [container, bgColor = color]() {
851 auto pipelineContext = container->GetPipelineContext();
852 CHECK_NULL_VOID(pipelineContext);
853 pipelineContext->SetAppBgColor(Color(bgColor));
854 },
855 TaskExecutor::TaskType::UI);
856 }
857
GetBackgroundColor()858 uint32_t AceAbility::GetBackgroundColor()
859 {
860 auto container = Platform::AceContainer::GetContainer(abilityId_);
861 CHECK_NULL_RETURN(container, 0x000000);
862 auto taskExecutor = container->GetTaskExecutor();
863 CHECK_NULL_RETURN(taskExecutor, 0x000000);
864 ContainerScope scope(abilityId_);
865 uint32_t bgColor = 0x000000;
866 taskExecutor->PostSyncTask(
867 [&bgColor, container]() {
868 CHECK_NULL_VOID(container);
869 auto pipelineContext = container->GetPipelineContext();
870 CHECK_NULL_VOID(pipelineContext);
871 bgColor = pipelineContext->GetAppBgColor().GetValue();
872 },
873 TaskExecutor::TaskType::UI);
874
875 LOGI("AceAbilityHandler::GetBackgroundColor, value is %{public}u", bgColor);
876 return bgColor;
877 }
878
OnAvoidAreaChanged(const OHOS::Rosen::AvoidArea & avoidArea,OHOS::Rosen::AvoidAreaType type)879 void AceAbility::OnAvoidAreaChanged(const OHOS::Rosen::AvoidArea& avoidArea, OHOS::Rosen::AvoidAreaType type)
880 {
881 auto container = Platform::AceContainer::GetContainer((abilityId_));
882 CHECK_NULL_VOID_NOLOG(container);
883 auto pipeline = container->GetPipelineContext();
884 CHECK_NULL_VOID_NOLOG(pipeline);
885 CHECK_NULL_VOID_NOLOG(pipeline->GetMinPlatformVersion() >= PLATFORM_VERSION_TEN && pipeline->GetIsAppWindow());
886 LOGI("AceAbility::OnAvoidAreaChanged type:%{public}d, avoidArea:topRect:x:%{public}d, y:%{public}d, "
887 "width:%{public}d, height%{public}d",
888 type, avoidArea.topRect_.posX_, avoidArea.topRect_.posY_, (int32_t)avoidArea.topRect_.width_,
889 (int32_t)avoidArea.topRect_.height_);
890 auto taskExecutor = container->GetTaskExecutor();
891 CHECK_NULL_VOID_NOLOG(taskExecutor);
892 auto safeArea = ConvertAvoidArea(avoidArea);
893 ContainerScope scope(abilityId_);
894 taskExecutor->PostTask(
895 [pipeline, safeArea, type]() {
896 if (type == OHOS::Rosen::AvoidAreaType::TYPE_SYSTEM) {
897 pipeline->UpdateSystemSafeArea(safeArea);
898 } else if (type == OHOS::Rosen::AvoidAreaType::TYPE_CUTOUT) {
899 pipeline->UpdateCutoutSafeArea(safeArea);
900 }
901 },
902 TaskExecutor::TaskType::UI);
903 }
904
905 } // namespace Ace
906 } // namespace OHOS
907