• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ui_ability.h"
17 
18 #include "ability_lifecycle.h"
19 #include "ability_recovery.h"
20 #include "configuration_convertor.h"
21 #include "display_util.h"
22 #include "display_info.h"
23 #include "event_report.h"
24 #include "hilog_tag_wrapper.h"
25 #include "hitrace_meter.h"
26 #include "js_ui_ability.h"
27 #include "ability_context_impl.h"
28 #include "application_configuration_manager.h"
29 #ifdef CJ_FRONTEND
30 #include "cj_ui_ability.h"
31 #endif
32 #include "ohos_application.h"
33 #include "reverse_continuation_scheduler_primary_stage.h"
34 #include "runtime.h"
35 #include "resource_config_helper.h"
36 #ifdef SUPPORT_GRAPHICS
37 #include "wm_common.h"
38 #endif
39 
40 namespace OHOS {
41 namespace AbilityRuntime {
42 namespace {
43 constexpr char DMS_SESSION_ID[] = "sessionId";
44 constexpr char DMS_ORIGIN_DEVICE_ID[] = "deviceId";
45 constexpr int32_t DEFAULT_DMS_SESSION_ID = 0;
46 #ifdef SUPPORT_SCREEN
47 constexpr char LAUNCHER_BUNDLE_NAME[] = "com.ohos.launcher";
48 constexpr char LAUNCHER_ABILITY_NAME[] = "com.ohos.launcher.MainAbility";
49 constexpr char SHOW_ON_LOCK_SCREEN[] = "ShowOnLockScreen";
50 #endif
51 
52 #ifdef WITH_DLP
53 constexpr char DLP_PARAMS_SECURITY_FLAG[] = "ohos.dlp.params.securityFlag";
54 #endif // WITH_DLP
55 constexpr char COMPONENT_STARTUP_NEW_RULES[] = "component.startup.newRules";
56 #ifdef SUPPORT_SCREEN
57 constexpr int32_t ERR_INVALID_VALUE = -1;
58 #endif
59 }
Create(const std::unique_ptr<Runtime> & runtime)60 UIAbility *UIAbility::Create(const std::unique_ptr<Runtime> &runtime)
61 {
62     if (!runtime) {
63         return new (std::nothrow) UIAbility;
64     }
65 
66     switch (runtime->GetLanguage()) {
67         case Runtime::Language::JS:
68             return JsUIAbility::Create(runtime);
69 #ifdef CJ_FRONTEND
70         case Runtime::Language::CJ:
71             return CJUIAbility::Create(runtime);
72 #endif
73         default:
74             return new (std::nothrow) UIAbility();
75     }
76 }
77 
Init(std::shared_ptr<AppExecFwk::AbilityLocalRecord> record,const std::shared_ptr<AppExecFwk::OHOSApplication> application,std::shared_ptr<AppExecFwk::AbilityHandler> & handler,const sptr<IRemoteObject> & token)78 void UIAbility::Init(std::shared_ptr<AppExecFwk::AbilityLocalRecord> record,
79     const std::shared_ptr<AppExecFwk::OHOSApplication> application,
80     std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token)
81 {
82     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
83     TAG_LOGD(AAFwkTag::UIABILITY, "called");
84     if (record == nullptr) {
85         TAG_LOGE(AAFwkTag::UIABILITY, "null record");
86         return;
87     }
88     application_ = application;
89     abilityInfo_ = record->GetAbilityInfo();
90     handler_ = handler;
91     token_ = token;
92 #ifdef SUPPORT_SCREEN
93     continuationManager_ = std::make_shared<AppExecFwk::ContinuationManagerStage>();
94     std::weak_ptr<AppExecFwk::ContinuationManagerStage> continuationManager = continuationManager_;
95     continuationHandler_ =
96         std::make_shared<AppExecFwk::ContinuationHandlerStage>(continuationManager, weak_from_this());
97     if (!continuationManager_->Init(shared_from_this(), GetToken(), GetAbilityInfo(), continuationHandler_)) {
98         continuationManager_.reset();
99     } else {
100         std::weak_ptr<AppExecFwk::ContinuationHandlerStage> continuationHandler = continuationHandler_;
101         sptr<AppExecFwk::ReverseContinuationSchedulerPrimaryStage> primary =
102             new (std::nothrow) AppExecFwk::ReverseContinuationSchedulerPrimaryStage(continuationHandler, handler_);
103         if (primary == nullptr) {
104             TAG_LOGE(AAFwkTag::UIABILITY, "null primary");
105         } else {
106             continuationHandler_->SetPrimaryStub(primary);
107             continuationHandler_->SetAbilityInfo(abilityInfo_);
108         }
109     }
110     // register displayid change callback
111     TAG_LOGD(AAFwkTag::UIABILITY, "registerDisplayListener");
112     abilityDisplayListener_ = new (std::nothrow) UIAbilityDisplayListener(weak_from_this());
113     if (abilityDisplayListener_ == nullptr) {
114         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityDisplayListener_");
115         return;
116     }
117     TAG_LOGD(AAFwkTag::UIABILITY, "end register");
118     Rosen::WindowManager::GetInstance().RegisterDisplayInfoChangedListener(token_, abilityDisplayListener_);
119 #endif
120     lifecycle_ = std::make_shared<AppExecFwk::LifeCycle>();
121     abilityLifecycleExecutor_ = std::make_shared<AppExecFwk::AbilityLifecycleExecutor>();
122     abilityLifecycleExecutor_->DispatchLifecycleState(AppExecFwk::AbilityLifecycleExecutor::LifecycleState::INITIAL);
123     if (abilityContext_ != nullptr) {
124         abilityContext_->RegisterAbilityCallback(weak_from_this());
125     }
126     TAG_LOGD(AAFwkTag::UIABILITY, "end");
127 }
128 
GetLifecycle()129 std::shared_ptr<OHOS::AppExecFwk::LifeCycle> UIAbility::GetLifecycle()
130 {
131     TAG_LOGD(AAFwkTag::UIABILITY, "called");
132     return lifecycle_;
133 }
134 
RegisterAbilityLifecycleObserver(const std::shared_ptr<AppExecFwk::ILifecycleObserver> & observer)135 void UIAbility::RegisterAbilityLifecycleObserver(const std::shared_ptr<AppExecFwk::ILifecycleObserver> &observer)
136 {
137     TAG_LOGD(AAFwkTag::UIABILITY, "called");
138     if (observer == nullptr) {
139         TAG_LOGE(AAFwkTag::UIABILITY, "null observer");
140         return;
141     }
142     if (lifecycle_ == nullptr) {
143         TAG_LOGE(AAFwkTag::UIABILITY, "null lifecycle_");
144         return;
145     }
146     lifecycle_->AddObserver(observer);
147 }
148 
UnregisterAbilityLifecycleObserver(const std::shared_ptr<AppExecFwk::ILifecycleObserver> & observer)149 void UIAbility::UnregisterAbilityLifecycleObserver(const std::shared_ptr<AppExecFwk::ILifecycleObserver> &observer)
150 {
151     TAG_LOGD(AAFwkTag::UIABILITY, "called");
152     if (observer == nullptr) {
153         TAG_LOGE(AAFwkTag::UIABILITY, "null observer");
154         return;
155     }
156     if (lifecycle_ == nullptr) {
157         TAG_LOGE(AAFwkTag::UIABILITY, "null lifecycle_");
158         return;
159     }
160     lifecycle_->RemoveObserver(observer);
161 }
162 
AttachAbilityContext(const std::shared_ptr<AbilityRuntime::AbilityContext> & abilityContext)163 void UIAbility::AttachAbilityContext(const std::shared_ptr<AbilityRuntime::AbilityContext> &abilityContext)
164 {
165     abilityContext_ = abilityContext;
166     std::weak_ptr<UIAbility> abilityWptr = weak_from_this();
167     abilityContext_->RegisterAbilityConfigUpdateCallback(
168         [abilityWptr, abilityContext = abilityContext_](AppExecFwk::Configuration &config) {
169         std::shared_ptr<UIAbility> abilitySptr = abilityWptr.lock();
170         if (abilitySptr == nullptr || abilityContext == nullptr ||
171             abilityContext->GetAbilityInfo() == nullptr) {
172             TAG_LOGE(AAFwkTag::UIABILITY, "null abilitySptr or null abilityContext or null GetAbilityInfo");
173             return;
174         }
175         if (abilityContext->GetAbilityConfiguration() == nullptr) {
176             auto abilityModuleContext = abilityContext->CreateModuleContext(
177                 abilityContext->GetAbilityInfo()->moduleName);
178             if (abilityModuleContext == nullptr) {
179                 TAG_LOGE(AAFwkTag::UIABILITY, "null abilityModuleContext");
180                 return;
181             }
182             auto abilityResourceMgr = abilityModuleContext->GetResourceManager();
183             abilityContext->SetAbilityResourceManager(abilityResourceMgr);
184             AbilityRuntime::ApplicationConfigurationManager::GetInstance().
185                 AddIgnoreContext(abilityContext, abilityResourceMgr);
186         }
187         abilityContext->SetAbilityConfiguration(config);
188         if (config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE).
189             compare(AppExecFwk::ConfigurationInner::COLOR_MODE_AUTO) == 0) {
190             config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE,
191                 AbilityRuntime::ApplicationConfigurationManager::GetInstance().GetColorMode());
192             if (AbilityRuntime::ApplicationConfigurationManager::GetInstance().
193                 GetColorModeSetLevel() > AbilityRuntime::SetLevel::System) {
194                 config.AddItem(AAFwk::GlobalConfigurationKey::COLORMODE_IS_SET_BY_APP,
195                     AppExecFwk::ConfigurationInner::IS_SET_BY_APP);
196             }
197             abilityContext->GetAbilityConfiguration()->
198                 RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
199             abilityContext->GetAbilityConfiguration()->
200                 RemoveItem(AAFwk::GlobalConfigurationKey::COLORMODE_IS_SET_BY_APP);
201         }
202         ResourceConfigHelper resourceConfig;
203         abilitySptr->InitConfigurationProperties(config, resourceConfig);
204         resourceConfig.SetISAbilityColor(true);
205         auto resourceManager = abilityContext->GetResourceManager();
206         resourceConfig.UpdateResConfig(config, resourceManager);
207         auto diffConfiguration = std::make_shared<AppExecFwk::Configuration>(config);
208         if (abilitySptr->GetScene()) {
209             abilitySptr->GetScene()->UpdateConfigurationForSpecified(diffConfiguration, resourceManager);
210         }
211         abilitySptr->OnConfigurationUpdated(config);
212     });
213 }
214 
OnStart(const AAFwk::Want & want,sptr<AAFwk::SessionInfo> sessionInfo)215 void UIAbility::OnStart(const AAFwk::Want &want, sptr<AAFwk::SessionInfo> sessionInfo)
216 {
217     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
218     if (abilityInfo_ == nullptr) {
219         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityInfo_");
220         return;
221     }
222 
223 #ifdef WITH_DLP
224     securityFlag_ = want.GetBoolParam(DLP_PARAMS_SECURITY_FLAG, false);
225     (const_cast<AAFwk::Want &>(want)).RemoveParam(DLP_PARAMS_SECURITY_FLAG);
226 #endif // WITH_DLP
227     SetWant(want);
228     TAG_LOGD(AAFwkTag::UIABILITY, "ability: %{public}s", abilityInfo_->name.c_str());
229 #ifdef SUPPORT_SCREEN
230     if (sessionInfo != nullptr) {
231         SetSessionToken(sessionInfo->sessionToken);
232         SetIdentityToken(sessionInfo->identityToken);
233     }
234     OnStartForSupportGraphics(want);
235 #endif
236     if (abilityLifecycleExecutor_ == nullptr) {
237         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityLifecycleExecutor_");
238         return;
239     }
240     abilityLifecycleExecutor_->DispatchLifecycleState(
241         AppExecFwk::AbilityLifecycleExecutor::LifecycleState::STARTED_NEW);
242 
243     if (lifecycle_ == nullptr) {
244         TAG_LOGE(AAFwkTag::UIABILITY, "null lifecycle_");
245         return;
246     }
247     lifecycle_->DispatchLifecycle(AppExecFwk::LifeCycle::Event::ON_START, want);
248     TAG_LOGD(AAFwkTag::UIABILITY, "end");
249 }
250 
OnStop()251 void UIAbility::OnStop()
252 {
253     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
254     TAG_LOGD(AAFwkTag::UIABILITY, "called");
255 #ifdef SUPPORT_SCREEN
256     TAG_LOGD(AAFwkTag::UIABILITY, "unregisterDisplayInfoChangedListener");
257     (void)Rosen::WindowManager::GetInstance().UnregisterDisplayInfoChangedListener(token_, abilityDisplayListener_);
258     auto &&window = GetWindow();
259     if (window != nullptr) {
260         TAG_LOGD(AAFwkTag::UIABILITY, "UnregisterDisplayMoveListener");
261         window->UnregisterDisplayMoveListener(abilityDisplayMoveListener_);
262     }
263     // Call JS Func(onWindowStageDestroy) and Release the scene.
264     if (scene_ != nullptr) {
265         OnSceneWillDestroy();
266         scene_->GoDestroy();
267         onSceneDestroyed();
268     }
269 #endif
270     if (abilityLifecycleExecutor_ == nullptr) {
271         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityLifecycleExecutor_");
272         return;
273     }
274     abilityLifecycleExecutor_->DispatchLifecycleState(AppExecFwk::AbilityLifecycleExecutor::LifecycleState::INITIAL);
275     AbilityRuntime::ApplicationConfigurationManager::GetInstance().
276         DeleteIgnoreContext(abilityContext_);
277     if (lifecycle_ == nullptr) {
278         TAG_LOGE(AAFwkTag::UIABILITY, "null lifecycle_");
279         return;
280     }
281     lifecycle_->DispatchLifecycle(AppExecFwk::LifeCycle::Event::ON_STOP);
282 #ifdef SUPPORT_SCREEN
283     Rosen::DisplayManager::GetInstance().RemoveDisplayIdFromAms(token_);
284 #endif
285     TAG_LOGD(AAFwkTag::UIABILITY, "end");
286 }
287 
OnStop(AppExecFwk::AbilityTransactionCallbackInfo<> * callbackInfo,bool & isAsyncCallback)288 void UIAbility::OnStop(AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo, bool &isAsyncCallback)
289 {
290     isAsyncCallback = false;
291     OnStop();
292 }
293 
OnStopCallback()294 void UIAbility::OnStopCallback()
295 {
296     TAG_LOGD(AAFwkTag::UIABILITY, "called");
297 }
298 
DestroyInstance()299 void UIAbility::DestroyInstance()
300 {
301     TAG_LOGD(AAFwkTag::UIABILITY, "called");
302 }
303 
IsRestoredInContinuation() const304 bool UIAbility::IsRestoredInContinuation() const
305 {
306     if (abilityContext_ == nullptr) {
307         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityContext_");
308         return false;
309     }
310 
311     if (launchParam_.launchReason != AAFwk::LaunchReason::LAUNCHREASON_CONTINUATION) {
312         TAG_LOGD(AAFwkTag::UIABILITY, "launchReason: %{public}d", launchParam_.launchReason);
313         return false;
314     }
315 
316     return true;
317 }
318 
ShouldRecoverState(const AAFwk::Want & want)319 bool UIAbility::ShouldRecoverState(const AAFwk::Want &want)
320 {
321     if (!want.GetBoolParam(Want::PARAM_ABILITY_RECOVERY_RESTART, false)) {
322         TAG_LOGE(AAFwkTag::UIABILITY, "appRecovery not recovery restart");
323         return false;
324     }
325 
326     if (abilityRecovery_ == nullptr) {
327         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityRecovery_");
328         return false;
329     }
330 
331     if (abilityContext_ == nullptr) {
332         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityContext_");
333         return false;
334     }
335 
336     if (abilityContext_->GetContentStorage() == nullptr) {
337         TAG_LOGE(AAFwkTag::UIABILITY, "get content failed");
338         return false;
339     }
340     return true;
341 }
342 
ShouldDefaultRecoverState(const AAFwk::Want & want)343 bool UIAbility::ShouldDefaultRecoverState(const AAFwk::Want &want)
344 {
345     auto launchParam = GetLaunchParam();
346     if (CheckDefaultRecoveryEnabled() && IsStartByScb() &&
347         want.GetBoolParam(Want::PARAM_ABILITY_RECOVERY_RESTART, false) &&
348         (launchParam.lastExitReason == AAFwk::LastExitReason::LASTEXITREASON_PERFORMANCE_CONTROL ||
349         launchParam.lastExitReason == AAFwk::LastExitReason::LASTEXITREASON_RESOURCE_CONTROL)) {
350         return true;
351     }
352     return false;
353 }
354 
NotifyContinuationResult(const AAFwk::Want & want,bool success)355 void UIAbility::NotifyContinuationResult(const AAFwk::Want &want, bool success)
356 {
357     TAG_LOGD(AAFwkTag::UIABILITY, "called");
358     int sessionId = want.GetIntParam(DMS_SESSION_ID, DEFAULT_DMS_SESSION_ID);
359     std::string originDeviceId = want.GetStringParam(DMS_ORIGIN_DEVICE_ID);
360 
361     if (continuationManager_ == nullptr) {
362         TAG_LOGE(AAFwkTag::UIABILITY, "null continuationManager_");
363         return;
364     }
365     continuationManager_->NotifyCompleteContinuation(
366         originDeviceId, sessionId, success, reverseContinuationSchedulerReplica_);
367 }
368 
OnConfigurationUpdatedNotify(const AppExecFwk::Configuration & configuration)369 void UIAbility::OnConfigurationUpdatedNotify(const AppExecFwk::Configuration &configuration)
370 {
371     TAG_LOGD(AAFwkTag::UIABILITY, "begin");
372     auto newConfig = AppExecFwk::Configuration(configuration);
373     auto abilityConfig = GetAbilityContext()->GetAbilityConfiguration();
374     if (abilityConfig != nullptr) {
375         newConfig.FilterDuplicates(*abilityConfig);
376         TAG_LOGI(AAFwkTag::UIABILITY, "newConfig: %{public}s", newConfig.GetName().c_str());
377         if (newConfig.GetItemSize() == 0) {
378             return;
379         }
380         auto diffConfiguration = std::make_shared<AppExecFwk::Configuration>(newConfig);
381         auto resourceManager = abilityContext_->GetResourceManager();
382         ResourceConfigHelper resourceConfig;
383         InitConfigurationProperties(newConfig, resourceConfig);
384         resourceConfig.UpdateResConfig(newConfig, resourceManager);
385         auto windowScene = GetScene();
386         if (windowScene) {
387             windowScene->UpdateConfigurationForSpecified(
388                 diffConfiguration, resourceManager);
389         }
390     } else {
391         ResourceConfigHelper resourceConfig;
392         InitConfigurationProperties(configuration, resourceConfig);
393         auto resourceManager = GetResourceManager();
394         resourceConfig.UpdateResConfig(configuration, resourceManager);
395     }
396     if (abilityContext_ != nullptr && application_ != nullptr) {
397         abilityContext_->SetConfiguration(application_->GetConfiguration());
398     }
399     // Notify Ability Subclass
400     OnConfigurationUpdated(newConfig);
401     TAG_LOGD(AAFwkTag::UIABILITY, "end");
402 }
403 
InitConfigurationProperties(const AppExecFwk::Configuration & changeConfiguration,ResourceConfigHelper & resourceConfig)404 void UIAbility::InitConfigurationProperties(const AppExecFwk::Configuration &changeConfiguration,
405     ResourceConfigHelper &resourceConfig)
406 {
407     resourceConfig.SetMcc(changeConfiguration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_MCC));
408     resourceConfig.SetMnc(changeConfiguration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_MNC));
409     if (setting_) {
410         auto displayId =
411             std::atoi(setting_->GetProperty(AppExecFwk::AbilityStartSetting::WINDOW_DISPLAY_ID_KEY).c_str());
412         resourceConfig.SetLanguage(changeConfiguration.GetItem(displayId,
413             AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE));
414         resourceConfig.SetColormode(changeConfiguration.GetItem(displayId,
415             AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE));
416         resourceConfig.SetHasPointerDevice(changeConfiguration.GetItem(displayId,
417             AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE));
418         TAG_LOGD(AAFwkTag::UIABILITY, "displayId: [%{public}d], language: [%{public}s], colormode: [%{public}s], "
419             "hasPointerDevice: [%{public}s] mcc: [%{public}s], mnc: [%{public}s]", displayId,
420             resourceConfig.GetLanguage().c_str(), resourceConfig.GetColormode().c_str(),
421             resourceConfig.GetHasPointerDevice().c_str(), resourceConfig.GetMcc().c_str(),
422             resourceConfig.GetMnc().c_str());
423     } else {
424         resourceConfig.SetLanguage(changeConfiguration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE));
425         resourceConfig.SetColormode(changeConfiguration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE));
426         resourceConfig.SetHasPointerDevice(changeConfiguration.GetItem(
427             AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE));
428         TAG_LOGD(AAFwkTag::UIABILITY,
429             "language: [%{public}s], colormode: [%{public}s], hasPointerDevice: [%{public}s] "
430             "mcc: [%{public}s], mnc: [%{public}s]",
431             resourceConfig.GetLanguage().c_str(), resourceConfig.GetColormode().c_str(),
432             resourceConfig.GetHasPointerDevice().c_str(), resourceConfig.GetMcc().c_str(),
433             resourceConfig.GetMnc().c_str());
434     }
435 }
436 
OnMemoryLevel(int level)437 void UIAbility::OnMemoryLevel(int level)
438 {
439     TAG_LOGD(AAFwkTag::UIABILITY, "called");
440 #ifdef SUPPORT_SCREEN
441     if (scene_ == nullptr) {
442         TAG_LOGE(AAFwkTag::UIABILITY, "null scene");
443         return;
444     }
445     scene_->NotifyMemoryLevel(level);
446 #endif
447 }
448 
GetAbilityName()449 std::string UIAbility::GetAbilityName()
450 {
451     if (abilityInfo_ == nullptr) {
452         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityInfo_");
453         return "";
454     }
455     return abilityInfo_->name;
456 }
457 
GetModuleName()458 std::string UIAbility::GetModuleName()
459 {
460     if (abilityInfo_ == nullptr) {
461         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityInfo_");
462         return "";
463     }
464 
465     return abilityInfo_->moduleName;
466 }
467 
OnAbilityResult(int requestCode,int resultCode,const AAFwk::Want & want)468 void UIAbility::OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &want)
469 {
470     TAG_LOGD(AAFwkTag::UIABILITY, "called");
471 }
472 
OnNewWant(const AAFwk::Want & want)473 void UIAbility::OnNewWant(const AAFwk::Want &want)
474 {
475     TAG_LOGD(AAFwkTag::UIABILITY, "called");
476 }
477 
OnRestoreAbilityState(const AppExecFwk::PacMap & inState)478 void UIAbility::OnRestoreAbilityState(const AppExecFwk::PacMap &inState)
479 {
480     TAG_LOGD(AAFwkTag::UIABILITY, "called");
481 }
482 
SetWant(const AAFwk::Want & want)483 void UIAbility::SetWant(const AAFwk::Want &want)
484 {
485     std::lock_guard<std::mutex> lock(wantMutexlock_);
486     setWant_ = std::make_shared<AAFwk::Want>(want);
487 }
488 
GetWant()489 std::shared_ptr<AAFwk::Want> UIAbility::GetWant()
490 {
491     std::lock_guard<std::mutex> lock(wantMutexlock_);
492     return setWant_;
493 }
494 
OnConfigurationUpdated(const AppExecFwk::Configuration & configuration)495 void UIAbility::OnConfigurationUpdated(const AppExecFwk::Configuration &configuration)
496 {
497     TAG_LOGD(AAFwkTag::UIABILITY, "called");
498 }
499 
Dump(const std::vector<std::string> & params,std::vector<std::string> & info)500 void UIAbility::Dump(const std::vector<std::string> &params, std::vector<std::string> &info)
501 {
502     TAG_LOGD(AAFwkTag::UIABILITY, "called");
503 }
504 
GetState()505 AppExecFwk::AbilityLifecycleExecutor::LifecycleState UIAbility::GetState()
506 {
507     TAG_LOGD(AAFwkTag::UIABILITY, "called");
508     if (abilityLifecycleExecutor_ == nullptr) {
509         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityLifecycleExecutor_");
510         return AppExecFwk::AbilityLifecycleExecutor::LifecycleState::UNINITIALIZED;
511     }
512     return static_cast<AppExecFwk::AbilityLifecycleExecutor::LifecycleState>(abilityLifecycleExecutor_->GetState());
513 }
514 
OnContinueAsyncCB(napi_ref jsWantParams,int32_t status,const AppExecFwk::AbilityInfo & abilityInfo)515 int32_t UIAbility::OnContinueAsyncCB(napi_ref jsWantParams, int32_t status,
516     const AppExecFwk::AbilityInfo &abilityInfo)
517 {
518     TAG_LOGD(AAFwkTag::UIABILITY, "called");
519     return ERR_OK;
520 }
521 
OnContinue(AAFwk::WantParams & wantParams,bool & isAsyncOnContinue,const AppExecFwk::AbilityInfo & abilityInfo)522 int32_t UIAbility::OnContinue(AAFwk::WantParams &wantParams, bool &isAsyncOnContinue,
523     const AppExecFwk::AbilityInfo &abilityInfo)
524 {
525     return AppExecFwk::ContinuationManagerStage::OnContinueResult::ON_CONTINUE_ERR;
526 }
527 
ContinueAbilityWithStack(const std::string & deviceId,uint32_t versionCode)528 void UIAbility::ContinueAbilityWithStack(const std::string &deviceId, uint32_t versionCode)
529 {
530     if (deviceId.empty()) {
531         TAG_LOGE(AAFwkTag::UIABILITY, "empty deviceId");
532         return;
533     }
534 
535     if (continuationManager_ == nullptr) {
536         TAG_LOGE(AAFwkTag::UIABILITY, "null continuationManager_");
537         return;
538     }
539     continuationManager_->ContinueAbilityWithStack(deviceId, versionCode);
540 }
541 
OnStartContinuation()542 bool UIAbility::OnStartContinuation()
543 {
544     return false;
545 }
546 
OnSaveData(AAFwk::WantParams & saveData)547 bool UIAbility::OnSaveData(AAFwk::WantParams &saveData)
548 {
549     return false;
550 }
551 
OnRestoreData(AAFwk::WantParams & restoreData)552 bool UIAbility::OnRestoreData(AAFwk::WantParams &restoreData)
553 {
554     return false;
555 }
556 
OnSaveState(int32_t reason,AAFwk::WantParams & wantParams)557 int32_t UIAbility::OnSaveState(int32_t reason, AAFwk::WantParams &wantParams)
558 {
559     return ERR_OK;
560 }
561 
OnCompleteContinuation(int result)562 void UIAbility::OnCompleteContinuation(int result)
563 {
564     TAG_LOGD(AAFwkTag::UIABILITY, "called");
565     if (continuationManager_ == nullptr) {
566         TAG_LOGE(AAFwkTag::UIABILITY, "null continuationManager_");
567         return;
568     }
569 
570     continuationManager_->ChangeProcessStateToInit();
571 }
572 
OnRemoteTerminated()573 void UIAbility::OnRemoteTerminated()
574 {
575     TAG_LOGD(AAFwkTag::UIABILITY, "called");
576 }
577 
DispatchLifecycleOnForeground(const AAFwk::Want & want)578 void UIAbility::DispatchLifecycleOnForeground(const AAFwk::Want &want)
579 {
580     if (abilityLifecycleExecutor_ == nullptr) {
581         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityLifecycleExecutor_");
582         return;
583     }
584     abilityLifecycleExecutor_->DispatchLifecycleState(
585         AppExecFwk::AbilityLifecycleExecutor::LifecycleState::FOREGROUND_NEW);
586 
587     if (lifecycle_ == nullptr) {
588         TAG_LOGE(AAFwkTag::UIABILITY, "null lifecycle_");
589         return;
590     }
591     lifecycle_->DispatchLifecycle(AppExecFwk::LifeCycle::Event::ON_FOREGROUND, want);
592 }
593 
HandleCreateAsRecovery(const AAFwk::Want & want)594 void UIAbility::HandleCreateAsRecovery(const AAFwk::Want &want)
595 {
596     if (!want.GetBoolParam(Want::PARAM_ABILITY_RECOVERY_RESTART, false)) {
597         TAG_LOGE(AAFwkTag::UIABILITY, "appRecovery not recovery restart");
598         return;
599     }
600 
601     if (abilityRecovery_ != nullptr) {
602         abilityRecovery_->ScheduleRestoreAbilityState(AppExecFwk::StateReason::DEVELOPER_REQUEST, want);
603     }
604 }
605 
SetStartAbilitySetting(std::shared_ptr<AppExecFwk::AbilityStartSetting> setting)606 void UIAbility::SetStartAbilitySetting(std::shared_ptr<AppExecFwk::AbilityStartSetting> setting)
607 {
608     TAG_LOGD(AAFwkTag::UIABILITY, "called");
609     setting_ = setting;
610 }
611 
SetLaunchParam(const AAFwk::LaunchParam & launchParam)612 void UIAbility::SetLaunchParam(const AAFwk::LaunchParam &launchParam)
613 {
614     TAG_LOGD(AAFwkTag::UIABILITY, "called");
615     launchParam_ = launchParam;
616 }
617 
GetLaunchParam() const618 const AAFwk::LaunchParam &UIAbility::GetLaunchParam() const
619 {
620     return launchParam_;
621 }
622 
GetAbilityContext()623 std::shared_ptr<AbilityRuntime::AbilityContext> UIAbility::GetAbilityContext()
624 {
625     return abilityContext_;
626 }
627 
CallRequest()628 sptr<IRemoteObject> UIAbility::CallRequest()
629 {
630     return nullptr;
631 }
632 
IsUseNewStartUpRule()633 bool UIAbility::IsUseNewStartUpRule()
634 {
635     std::lock_guard<std::mutex> lock(wantMutexlock_);
636     if (!isNewRuleFlagSetted_ && setWant_) {
637         startUpNewRule_ = setWant_->GetBoolParam(COMPONENT_STARTUP_NEW_RULES, false);
638         isNewRuleFlagSetted_ = true;
639     }
640     return startUpNewRule_;
641 }
642 
EnableAbilityRecovery(const std::shared_ptr<AppExecFwk::AbilityRecovery> & abilityRecovery,bool useAppSettedRecoveryValue)643 void UIAbility::EnableAbilityRecovery(const std::shared_ptr<AppExecFwk::AbilityRecovery> &abilityRecovery,
644     bool useAppSettedRecoveryValue)
645 {
646     abilityRecovery_ = abilityRecovery;
647     useAppSettedRecoveryValue_.store(useAppSettedRecoveryValue);
648 }
649 
OnShare(AAFwk::WantParams & wantParams)650 int32_t UIAbility::OnShare(AAFwk::WantParams &wantParams)
651 {
652     return ERR_OK;
653 }
654 
CheckIsSilentForeground() const655 bool UIAbility::CheckIsSilentForeground() const
656 {
657     return isSilentForeground_;
658 }
659 
SetIsSilentForeground(bool isSilentForeground)660 void UIAbility::SetIsSilentForeground(bool isSilentForeground)
661 {
662     isSilentForeground_ = isSilentForeground;
663 }
664 
665 #ifdef SUPPORT_SCREEN
OnSceneCreated()666 void UIAbility::OnSceneCreated()
667 {
668     TAG_LOGD(AAFwkTag::UIABILITY, "called");
669 }
670 
OnSceneRestored()671 void UIAbility::OnSceneRestored()
672 {
673     TAG_LOGD(AAFwkTag::UIABILITY, "called");
674 }
675 
OnSceneWillDestroy()676 void UIAbility::OnSceneWillDestroy()
677 {
678     TAG_LOGD(AAFwkTag::UIABILITY, "called");
679 }
680 
onSceneDestroyed()681 void UIAbility::onSceneDestroyed()
682 {
683     TAG_LOGD(AAFwkTag::UIABILITY, "called");
684 }
685 
OnForeground(const AAFwk::Want & want)686 void UIAbility::OnForeground(const AAFwk::Want &want)
687 {
688     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
689     TAG_LOGD(AAFwkTag::UIABILITY, "called");
690     DoOnForeground(want);
691     if (isSilentForeground_) {
692         TAG_LOGD(AAFwkTag::UIABILITY, "silent foreground, return");
693         return;
694     }
695     DispatchLifecycleOnForeground(want);
696     AAFwk::EventInfo eventInfo;
697     eventInfo.bundleName = want.GetElement().GetBundleName();
698     eventInfo.moduleName = want.GetElement().GetModuleName();
699     eventInfo.abilityName = want.GetElement().GetAbilityName();
700     eventInfo.callerBundleName = want.GetStringParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
701     if (abilityInfo_ != nullptr) {
702         eventInfo.bundleType = static_cast<int32_t>(abilityInfo_->applicationInfo.bundleType);
703     } else {
704         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityInfo_");
705     }
706     AAFwk::EventReport::SendAbilityEvent(AAFwk::EventName::ABILITY_ONFOREGROUND, HiSysEventType::BEHAVIOR, eventInfo);
707 }
708 
OnBackground()709 void UIAbility::OnBackground()
710 {
711     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
712     TAG_LOGD(AAFwkTag::UIABILITY, "called");
713     if (abilityInfo_ == nullptr) {
714         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityInfo_");
715         return;
716     }
717 
718     if (scene_ != nullptr) {
719         TAG_LOGD(AAFwkTag::UIABILITY, "goBackground sceneFlag: %{public}d", sceneFlag_);
720         scene_->GoBackground(sceneFlag_);
721     }
722 
723     if (abilityRecovery_ != nullptr && abilityContext_ != nullptr && abilityContext_->GetRestoreEnabled() &&
724         CheckRecoveryEnabled()) {
725         abilityRecovery_->ScheduleSaveAbilityState(AppExecFwk::StateReason::LIFECYCLE);
726     }
727 
728     if (abilityLifecycleExecutor_ == nullptr) {
729         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityLifecycleExecutor_");
730         return;
731     }
732     abilityLifecycleExecutor_->DispatchLifecycleState(
733         AppExecFwk::AbilityLifecycleExecutor::LifecycleState::BACKGROUND_NEW);
734 
735     if (lifecycle_ == nullptr) {
736         TAG_LOGE(AAFwkTag::UIABILITY, "null lifecycle_");
737         return;
738     }
739     lifecycle_->DispatchLifecycle(AppExecFwk::LifeCycle::Event::ON_BACKGROUND);
740     AAFwk::EventInfo eventInfo;
741     eventInfo.bundleName = abilityInfo_->bundleName;
742     eventInfo.moduleName = abilityInfo_->moduleName;
743     eventInfo.abilityName = abilityInfo_->name;
744     eventInfo.bundleType = static_cast<int32_t>(abilityInfo_->applicationInfo.bundleType);
745     AAFwk::EventReport::SendAbilityEvent(AAFwk::EventName::ABILITY_ONBACKGROUND, HiSysEventType::BEHAVIOR, eventInfo);
746 }
747 
OnWillForeground()748 void UIAbility::OnWillForeground()
749 {
750     TAG_LOGD(AAFwkTag::UIABILITY, "OnWillForeground is called");
751 }
752 
OnDidForeground()753 void UIAbility::OnDidForeground()
754 {
755     TAG_LOGD(AAFwkTag::UIABILITY, "OnDidForeground is called");
756 }
757 
OnWillBackground()758 void UIAbility::OnWillBackground()
759 {
760     TAG_LOGD(AAFwkTag::UIABILITY, "OnWillBackground is called");
761 }
762 
OnDidBackground()763 void UIAbility::OnDidBackground()
764 {
765     TAG_LOGD(AAFwkTag::UIABILITY, "OnDidBackground is called");
766 }
767 
OnAfterFocusedCommon(bool isFocused)768 void UIAbility::OnAfterFocusedCommon(bool isFocused)
769 {
770     TAG_LOGD(AAFwkTag::UIABILITY, "called");
771     return;
772 }
773 
OnPrepareTerminate()774 bool UIAbility::OnPrepareTerminate()
775 {
776     TAG_LOGI(AAFwkTag::UIABILITY, "called");
777     return false;
778 }
779 
OnPrepareTerminate(AppExecFwk::AbilityTransactionCallbackInfo<bool> * callbackInfo,bool & isAsync)780 void UIAbility::OnPrepareTerminate(AppExecFwk::AbilityTransactionCallbackInfo<bool> *callbackInfo, bool &isAsync)
781 {
782     TAG_LOGI(AAFwkTag::UIABILITY, "called");
783     return;
784 }
785 
GetWindow()786 const sptr<Rosen::Window> UIAbility::GetWindow()
787 {
788     TAG_LOGD(AAFwkTag::UIABILITY, "called");
789     return nullptr;
790 }
791 
GetScene()792 std::shared_ptr<Rosen::WindowScene> UIAbility::GetScene()
793 {
794     return scene_;
795 }
796 
OnLeaveForeground()797 void UIAbility::OnLeaveForeground()
798 {
799     TAG_LOGD(AAFwkTag::UIABILITY, "called");
800 }
801 
HandleCollaboration(const AAFwk::Want & want)802 void UIAbility::HandleCollaboration(const AAFwk::Want &want)
803 {
804     TAG_LOGD(AAFwkTag::UIABILITY, "called");
805 }
806 
GetContentInfo()807 std::string UIAbility::GetContentInfo()
808 {
809     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
810     if (scene_ == nullptr) {
811         TAG_LOGE(AAFwkTag::UIABILITY, "null scene");
812         return "";
813     }
814     return scene_->GetContentInfo(Rosen::BackupAndRestoreType::CONTINUATION);
815 }
816 
GetContentInfoForRecovery()817 std::string UIAbility::GetContentInfoForRecovery()
818 {
819     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
820     if (scene_ == nullptr) {
821         TAG_LOGE(AAFwkTag::UIABILITY, "null scene");
822         return "";
823     }
824     return scene_->GetContentInfo(Rosen::BackupAndRestoreType::APP_RECOVERY);
825 }
826 
GetContentInfoForDefaultRecovery()827 std::string UIAbility::GetContentInfoForDefaultRecovery()
828 {
829     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
830     if (scene_ == nullptr) {
831         TAG_LOGE(AAFwkTag::UIABILITY, "null scene");
832         return "";
833     }
834     return scene_->GetContentInfo(Rosen::BackupAndRestoreType::RESOURCESCHEDULE_RECOVERY);
835 }
836 
SetSceneListener(const sptr<Rosen::IWindowLifeCycle> & listener)837 void UIAbility::SetSceneListener(const sptr<Rosen::IWindowLifeCycle> &listener)
838 {
839     sceneListener_ = listener;
840 }
841 
DoOnForeground(const AAFwk::Want & want)842 void UIAbility::DoOnForeground(const AAFwk::Want &want)
843 {
844     TAG_LOGD(AAFwkTag::UIABILITY, "called");
845 }
846 
GetCurrentWindowMode()847 int32_t UIAbility::GetCurrentWindowMode()
848 {
849     TAG_LOGD(AAFwkTag::UIABILITY, "called");
850     auto windowMode = static_cast<int>(Rosen::WindowMode::WINDOW_MODE_UNDEFINED);
851     if (scene_ == nullptr) {
852         return windowMode;
853     }
854     auto window = scene_->GetMainWindow();
855     if (window != nullptr) {
856         windowMode = static_cast<int>(window->GetWindowMode());
857     }
858     return windowMode;
859 }
860 
SetMissionLabel(const std::string & label)861 ErrCode UIAbility::SetMissionLabel(const std::string &label)
862 {
863     TAG_LOGD(AAFwkTag::UIABILITY, "called");
864     if (!abilityInfo_ || abilityInfo_->type != AppExecFwk::AbilityType::PAGE) {
865         TAG_LOGE(AAFwkTag::UIABILITY, "invalid ability info");
866         return ERR_INVALID_VALUE;
867     }
868 
869     if (scene_ == nullptr) {
870         TAG_LOGE(AAFwkTag::UIABILITY, "null scene");
871         return ERR_INVALID_VALUE;
872     }
873     auto window = scene_->GetMainWindow();
874     if (window == nullptr) {
875         TAG_LOGE(AAFwkTag::UIABILITY, "null window");
876         return ERR_INVALID_VALUE;
877     }
878 
879     if (window->SetAPPWindowLabel(label) != OHOS::Rosen::WMError::WM_OK) {
880         TAG_LOGE(AAFwkTag::UIABILITY, "setAPPWindowLabel failed");
881         return ERR_INVALID_VALUE;
882     }
883     return ERR_OK;
884 }
885 
SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> & icon)886 ErrCode UIAbility::SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon)
887 {
888     TAG_LOGD(AAFwkTag::UIABILITY, "called");
889     if (!abilityInfo_ || abilityInfo_->type != AppExecFwk::AbilityType::PAGE) {
890         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityInfo_ or not page type");
891         return ERR_INVALID_VALUE;
892     }
893 
894     if (scene_ == nullptr) {
895         TAG_LOGE(AAFwkTag::UIABILITY, "null scene_");
896         return ERR_INVALID_VALUE;
897     }
898     auto window = scene_->GetMainWindow();
899     if (window == nullptr) {
900         TAG_LOGE(AAFwkTag::UIABILITY, "null window");
901         return ERR_INVALID_VALUE;
902     }
903 
904     if (window->SetAPPWindowIcon(icon) != OHOS::Rosen::WMError::WM_OK) {
905         TAG_LOGE(AAFwkTag::UIABILITY, "setAPPWindowIcon failed");
906         return ERR_INVALID_VALUE;
907     }
908     return ERR_OK;
909 }
910 
GetWindowRect(int32_t & left,int32_t & top,int32_t & width,int32_t & height)911 void UIAbility::GetWindowRect(int32_t &left, int32_t &top, int32_t &width, int32_t &height)
912 {
913     TAG_LOGD(AAFwkTag::UIABILITY, "called");
914     if (scene_ == nullptr) {
915         TAG_LOGE(AAFwkTag::UIABILITY, "null scene");
916         return;
917     }
918     auto window = scene_->GetMainWindow();
919     if (window == nullptr) {
920         TAG_LOGE(AAFwkTag::UIABILITY, "null window");
921         return;
922     }
923     left = window->GetRect().posX_;
924     top = window->GetRect().posY_;
925     width = static_cast<int32_t>(window->GetRect().width_);
926     height = static_cast<int32_t>(window->GetRect().height_);
927     TAG_LOGD(AAFwkTag::UIABILITY, "left: %{public}d, top: %{public}d, width: %{public}d, height: %{public}d",
928         left, top, width, height);
929 }
930 
GetUIContent()931 Ace::UIContent *UIAbility::GetUIContent()
932 {
933     TAG_LOGD(AAFwkTag::UIABILITY, "called");
934     if (scene_ == nullptr) {
935         TAG_LOGE(AAFwkTag::UIABILITY, "null scene_");
936         return nullptr;
937     }
938     auto window = scene_->GetMainWindow();
939     if (window == nullptr) {
940         TAG_LOGE(AAFwkTag::UIABILITY, "null window");
941         return nullptr;
942     }
943     return window->GetUIContent();
944 }
945 
OnCreate(Rosen::DisplayId displayId)946 void UIAbility::OnCreate(Rosen::DisplayId displayId)
947 {
948     TAG_LOGD(AAFwkTag::UIABILITY, "called");
949 }
950 
OnDestroy(Rosen::DisplayId displayId)951 void UIAbility::OnDestroy(Rosen::DisplayId displayId)
952 {
953     TAG_LOGD(AAFwkTag::UIABILITY, "called");
954 }
955 
OnDisplayInfoChange(const sptr<IRemoteObject> & token,Rosen::DisplayId displayId,float density,Rosen::DisplayOrientation orientation)956 void UIAbility::OnDisplayInfoChange(const sptr<IRemoteObject>& token, Rosen::DisplayId displayId, float density,
957     Rosen::DisplayOrientation orientation)
958 {
959     TAG_LOGD(AAFwkTag::UIABILITY, "displayId: %{public}" PRIu64, displayId);
960     // Get display
961     auto display = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
962     if (!display) {
963         TAG_LOGE(AAFwkTag::UIABILITY, "get display by displayId %{public}" PRIu64 " failed", displayId);
964         return;
965     }
966 
967     // Notify ResourceManager
968     int32_t width = display->GetWidth();
969     int32_t height = display->GetHeight();
970     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
971     if (resConfig != nullptr) {
972         auto resourceManager = GetResourceManager();
973         if (resourceManager != nullptr) {
974             resourceManager->GetResConfig(*resConfig);
975             resConfig->SetScreenDensity(density);
976             resConfig->SetDirection(AppExecFwk::ConvertDirection(height, width));
977             resourceManager->UpdateResConfig(*resConfig);
978             TAG_LOGD(AAFwkTag::UIABILITY, "notify resourceManager, density: %{public}f, direction: %{public}d",
979                 resConfig->GetScreenDensity(), resConfig->GetDirection());
980         }
981     }
982 
983     // Notify ability
984     Configuration newConfig;
985     newConfig.AddItem(
986         displayId, AppExecFwk::ConfigurationInner::APPLICATION_DIRECTION, AppExecFwk::GetDirectionStr(height, width));
987     newConfig.AddItem(
988         displayId, AppExecFwk::ConfigurationInner::APPLICATION_DENSITYDPI, AppExecFwk::GetDensityStr(density));
989 
990     if (application_ == nullptr) {
991         TAG_LOGE(AAFwkTag::UIABILITY, "null application_");
992         return;
993     }
994 
995     OnChangeForUpdateConfiguration(newConfig);
996     TAG_LOGD(AAFwkTag::UIABILITY, "end");
997 }
998 
OnChange(Rosen::DisplayId displayId)999 void UIAbility::OnChange(Rosen::DisplayId displayId)
1000 {
1001     TAG_LOGD(AAFwkTag::UIABILITY, "displayId: %{public}" PRIu64 "", displayId);
1002     // Get display
1003     auto display = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
1004     if (!display) {
1005         TAG_LOGE(AAFwkTag::UIABILITY, "get display by displayId %{public}" PRIu64 " failed", displayId);
1006         return;
1007     }
1008 
1009     // Notify ResourceManager
1010     float density = display->GetVirtualPixelRatio();
1011     int32_t width = display->GetWidth();
1012     int32_t height = display->GetHeight();
1013     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
1014     if (resConfig != nullptr) {
1015         auto resourceManager = GetResourceManager();
1016         if (resourceManager != nullptr) {
1017             resourceManager->GetResConfig(*resConfig);
1018             resConfig->SetScreenDensity(density);
1019             resConfig->SetDirection(AppExecFwk::ConvertDirection(height, width));
1020             resourceManager->UpdateResConfig(*resConfig);
1021             TAG_LOGD(AAFwkTag::UIABILITY, "notify ResourceManager, density: %{public}f, direction: %{public}d",
1022                 resConfig->GetScreenDensity(), resConfig->GetDirection());
1023         }
1024     }
1025 
1026     // Notify ability
1027     Configuration newConfig;
1028     newConfig.AddItem(
1029         displayId, AppExecFwk::ConfigurationInner::APPLICATION_DIRECTION, AppExecFwk::GetDirectionStr(height, width));
1030     newConfig.AddItem(
1031         displayId, AppExecFwk::ConfigurationInner::APPLICATION_DENSITYDPI, AppExecFwk::GetDensityStr(density));
1032 
1033     if (application_ == nullptr) {
1034         TAG_LOGE(AAFwkTag::UIABILITY, "null application_");
1035         return;
1036     }
1037 
1038     OnChangeForUpdateConfiguration(newConfig);
1039     TAG_LOGD(AAFwkTag::UIABILITY, "end");
1040 }
1041 
OnDisplayMove(Rosen::DisplayId from,Rosen::DisplayId to)1042 void UIAbility::OnDisplayMove(Rosen::DisplayId from, Rosen::DisplayId to)
1043 {
1044     TAG_LOGD(AAFwkTag::UIABILITY, "from displayId %{public}" PRIu64 " to %{public}" PRIu64 "", from, to);
1045     auto display = Rosen::DisplayManager::GetInstance().GetDisplayById(to);
1046     if (!display) {
1047         TAG_LOGE(AAFwkTag::UIABILITY, "get display by displayId %{public}" PRIu64 " failed", to);
1048         return;
1049     }
1050     // Get new display config
1051     float density = display->GetVirtualPixelRatio();
1052     int32_t width = display->GetWidth();
1053     int32_t height = display->GetHeight();
1054     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
1055     if (resConfig != nullptr) {
1056         auto resourceManager = GetResourceManager();
1057         if (resourceManager != nullptr) {
1058             resourceManager->GetResConfig(*resConfig);
1059             resConfig->SetScreenDensity(density);
1060             resConfig->SetDirection(AppExecFwk::ConvertDirection(height, width));
1061             resourceManager->UpdateResConfig(*resConfig);
1062             TAG_LOGD(AAFwkTag::UIABILITY,
1063                 "Density: %{public}f, direction: %{public}d", resConfig->GetScreenDensity(), resConfig->GetDirection());
1064         }
1065     }
1066         UpdateConfiguration(to, density, width, height);
1067 }
1068 
UpdateConfiguration(Rosen::DisplayId to,float density,int32_t width,int32_t height)1069 void UIAbility::UpdateConfiguration(Rosen::DisplayId to, float density, int32_t width, int32_t height)
1070 {
1071     AppExecFwk::Configuration newConfig;
1072     newConfig.AddItem(AppExecFwk::ConfigurationInner::APPLICATION_DISPLAYID, std::to_string(to));
1073     newConfig.AddItem(
1074         to, AppExecFwk::ConfigurationInner::APPLICATION_DIRECTION, AppExecFwk::GetDirectionStr(height, width));
1075     newConfig.AddItem(to, AppExecFwk::ConfigurationInner::APPLICATION_DENSITYDPI, AppExecFwk::GetDensityStr(density));
1076     if (application_ == nullptr || handler_ == nullptr) {
1077         TAG_LOGE(AAFwkTag::UIABILITY, "null application_ or handler_");
1078         return;
1079     }
1080     std::vector<std::string> changeKeyV;
1081     auto configuration = application_->GetConfiguration();
1082     if (!configuration) {
1083         TAG_LOGE(AAFwkTag::UIABILITY, "null configuration");
1084         return;
1085     }
1086 
1087     configuration->CompareDifferent(changeKeyV, newConfig);
1088     TAG_LOGD(AAFwkTag::UIABILITY, "changeKeyV size: %{public}zu", changeKeyV.size());
1089     if (!changeKeyV.empty()) {
1090         configuration->Merge(changeKeyV, newConfig);
1091         auto task = [abilityWptr = weak_from_this(), configuration = *configuration]() {
1092             auto ability = abilityWptr.lock();
1093             if (ability == nullptr) {
1094                 TAG_LOGE(AAFwkTag::UIABILITY, "null ability");
1095                 return;
1096             }
1097             ability->OnConfigurationUpdated(configuration);
1098         };
1099         handler_->PostTask(task);
1100     }
1101 }
1102 
RequestFocus(const AAFwk::Want & want)1103 void UIAbility::RequestFocus(const AAFwk::Want &want)
1104 {
1105     TAG_LOGD(AAFwkTag::UIABILITY, "called");
1106 }
1107 
InitWindow(int32_t displayId,sptr<Rosen::WindowOption> option)1108 void UIAbility::InitWindow(int32_t displayId, sptr<Rosen::WindowOption> option)
1109 {
1110     TAG_LOGD(AAFwkTag::UIABILITY, "called");
1111 }
1112 
GetWindowOption(const AAFwk::Want & want)1113 sptr<Rosen::WindowOption> UIAbility::GetWindowOption(const AAFwk::Want &want)
1114 {
1115     auto option = sptr<Rosen::WindowOption>::MakeSptr();
1116     if (option == nullptr) {
1117         TAG_LOGE(AAFwkTag::UIABILITY, "null option");
1118         return nullptr;
1119     }
1120     auto windowMode = want.GetIntParam(
1121         AAFwk::Want::PARAM_RESV_WINDOW_MODE, AAFwk::AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_UNDEFINED);
1122     TAG_LOGD(AAFwkTag::UIABILITY, "window mode: %{public}d", windowMode);
1123     option->SetWindowMode(static_cast<Rosen::WindowMode>(windowMode));
1124     bool showOnLockScreen = false;
1125     if (abilityInfo_) {
1126         std::vector<AppExecFwk::CustomizeData> datas = abilityInfo_->metaData.customizeData;
1127         for (AppExecFwk::CustomizeData data : datas) {
1128             if (data.name == SHOW_ON_LOCK_SCREEN) {
1129                 showOnLockScreen = true;
1130             }
1131         }
1132     }
1133     if (showOnLockScreen_ || showOnLockScreen) {
1134         TAG_LOGD(AAFwkTag::UIABILITY, "add window flag WINDOW_FLAG_SHOW_WHEN_LOCKED");
1135         option->AddWindowFlag(Rosen::WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED);
1136     }
1137 
1138     if (want.GetElement().GetBundleName() == LAUNCHER_BUNDLE_NAME &&
1139         want.GetElement().GetAbilityName() == LAUNCHER_ABILITY_NAME) {
1140         TAG_LOGD(AAFwkTag::UIABILITY, "set window type for launcher");
1141         option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_DESKTOP);
1142     }
1143     return option;
1144 }
1145 
ContinuationRestore(const AAFwk::Want & want)1146 void UIAbility::ContinuationRestore(const AAFwk::Want &want)
1147 {
1148     TAG_LOGD(AAFwkTag::UIABILITY, "called");
1149 }
1150 
OnStartForSupportGraphics(const AAFwk::Want & want)1151 void UIAbility::OnStartForSupportGraphics(const AAFwk::Want &want)
1152 {
1153     if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) {
1154         int32_t displayId = want.GetIntParam(AAFwk::Want::PARAM_RESV_DISPLAY_ID,
1155             static_cast<int32_t>(Rosen::DISPLAY_ID_INVALID));
1156         if (displayId == static_cast<int32_t>(Rosen::DISPLAY_ID_INVALID)) {
1157             displayId = AAFwk::DisplayUtil::GetDefaultDisplayId();
1158         }
1159         TAG_LOGD(AAFwkTag::UIABILITY, "abilityName: %{public}s, displayId: %{public}d",
1160             abilityInfo_->name.c_str(), displayId);
1161 #ifdef SUPPORT_SCREEN
1162         Rosen::DisplayManager::GetInstance().AddDisplayIdFromAms(displayId, token_);
1163 #endif
1164         auto option = GetWindowOption(want);
1165         InitWindow(displayId, option);
1166 
1167         // Update resMgr, Configuration
1168         TAG_LOGD(AAFwkTag::UIABILITY, "displayId: %{public}d", displayId);
1169         auto display = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
1170         if (display) {
1171             float density = 1.0f;
1172             int32_t width = 0;
1173             int32_t height = 0;
1174             if (auto displayInfo = display->GetDisplayInfo(); displayInfo != nullptr) {
1175                 density = displayInfo->GetVirtualPixelRatio();
1176                 width = displayInfo->GetWidth();
1177                 height = displayInfo->GetHeight();
1178             }
1179             std::shared_ptr<AppExecFwk::Configuration> configuration = nullptr;
1180             if (application_) {
1181                 configuration = application_->GetConfiguration();
1182             }
1183             if (configuration) {
1184                 std::string direction = AppExecFwk::GetDirectionStr(height, width);
1185                 configuration->AddItem(displayId, AppExecFwk::ConfigurationInner::APPLICATION_DIRECTION, direction);
1186                 configuration->AddItem(displayId, AppExecFwk::ConfigurationInner::APPLICATION_DENSITYDPI,
1187                     AppExecFwk::GetDensityStr(density));
1188                 configuration->AddItem(
1189                     AppExecFwk::ConfigurationInner::APPLICATION_DISPLAYID, std::to_string(displayId));
1190                 UpdateContextConfiguration();
1191             }
1192 
1193             std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
1194             if (resConfig == nullptr) {
1195                 TAG_LOGE(AAFwkTag::UIABILITY, "null resConfig");
1196                 return;
1197             }
1198             auto resourceManager = GetResourceManager();
1199             if (resourceManager != nullptr) {
1200                 resourceManager->GetResConfig(*resConfig);
1201                 resConfig->SetScreenDensity(density);
1202                 resConfig->SetDirection(AppExecFwk::ConvertDirection(height, width));
1203                 resourceManager->UpdateResConfig(*resConfig);
1204                 TAG_LOGD(AAFwkTag::UIABILITY, "density: %{public}f, direction: %{public}d",
1205                     resConfig->GetScreenDensity(), resConfig->GetDirection());
1206             }
1207         }
1208     }
1209 }
1210 
OnChangeForUpdateConfiguration(const AppExecFwk::Configuration & newConfig)1211 void UIAbility::OnChangeForUpdateConfiguration(const AppExecFwk::Configuration &newConfig)
1212 {
1213     if (application_ == nullptr || handler_ == nullptr) {
1214         TAG_LOGE(AAFwkTag::UIABILITY, "null application_ or handler_");
1215         return;
1216     }
1217     auto configuration = application_->GetConfiguration();
1218     if (!configuration) {
1219         TAG_LOGE(AAFwkTag::UIABILITY, "null configuration");
1220         return;
1221     }
1222 
1223     std::vector<std::string> changeKeyV;
1224     configuration->CompareDifferent(changeKeyV, newConfig);
1225     TAG_LOGD(AAFwkTag::UIABILITY, "changeKeyV size: %{public}zu", changeKeyV.size());
1226     if (!changeKeyV.empty()) {
1227         configuration->Merge(changeKeyV, newConfig);
1228         auto task = [abilityWptr = weak_from_this(), configuration = *configuration]() {
1229             auto ability = abilityWptr.lock();
1230             if (ability == nullptr) {
1231                 TAG_LOGE(AAFwkTag::UIABILITY, "null ability");
1232                 return;
1233             }
1234             ability->OnConfigurationUpdated(configuration);
1235         };
1236         handler_->PostTask(task);
1237     }
1238 }
1239 
CallOnForegroundFunc(const AAFwk::Want & want)1240 void UIAbility::CallOnForegroundFunc(const AAFwk::Want &want)
1241 {
1242     TAG_LOGD(AAFwkTag::UIABILITY, "called");
1243 }
1244 
ExecuteInsightIntentRepeateForeground(const AAFwk::Want & want,const std::shared_ptr<InsightIntentExecuteParam> & executeParam,std::unique_ptr<InsightIntentExecutorAsyncCallback> callback)1245 void UIAbility::ExecuteInsightIntentRepeateForeground(const AAFwk::Want &want,
1246     const std::shared_ptr<InsightIntentExecuteParam> &executeParam,
1247     std::unique_ptr<InsightIntentExecutorAsyncCallback> callback)
1248 {
1249     TAG_LOGD(AAFwkTag::UIABILITY, "called");
1250 }
1251 
ExecuteInsightIntentMoveToForeground(const AAFwk::Want & want,const std::shared_ptr<InsightIntentExecuteParam> & executeParam,std::unique_ptr<InsightIntentExecutorAsyncCallback> callback)1252 void UIAbility::ExecuteInsightIntentMoveToForeground(const AAFwk::Want &want,
1253     const std::shared_ptr<InsightIntentExecuteParam> &executeParam,
1254     std::unique_ptr<InsightIntentExecutorAsyncCallback> callback)
1255 {
1256     TAG_LOGD(AAFwkTag::UIABILITY, "called");
1257 }
1258 
ExecuteInsightIntentBackground(const AAFwk::Want & want,const std::shared_ptr<InsightIntentExecuteParam> & executeParam,std::unique_ptr<InsightIntentExecutorAsyncCallback> callback)1259 void UIAbility::ExecuteInsightIntentBackground(const AAFwk::Want &want,
1260     const std::shared_ptr<InsightIntentExecuteParam> &executeParam,
1261     std::unique_ptr<InsightIntentExecutorAsyncCallback> callback)
1262 {
1263     TAG_LOGD(AAFwkTag::UIABILITY, "called");
1264 }
1265 
CreateModalUIExtension(const AAFwk::Want & want)1266 int UIAbility::CreateModalUIExtension(const AAFwk::Want &want)
1267 {
1268     TAG_LOGD(AAFwkTag::UIABILITY, "call");
1269     auto abilityContextImpl = GetAbilityContext();
1270     if (abilityContextImpl == nullptr) {
1271         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityContext");
1272         return ERR_INVALID_VALUE;
1273     }
1274     return abilityContextImpl->CreateModalUIExtensionWithApp(want);
1275 }
1276 
SetSessionToken(sptr<IRemoteObject> sessionToken)1277 void UIAbility::SetSessionToken(sptr<IRemoteObject> sessionToken)
1278 {
1279     std::lock_guard lock(sessionTokenMutex_);
1280     sessionToken_ = sessionToken;
1281     auto abilityContextImpl = GetAbilityContext();
1282     if (abilityContextImpl == nullptr) {
1283         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityContext");
1284         return;
1285     }
1286     abilityContextImpl->SetWeakSessionToken(sessionToken);
1287 }
1288 
UpdateSessionToken(sptr<IRemoteObject> sessionToken)1289 void UIAbility::UpdateSessionToken(sptr<IRemoteObject> sessionToken)
1290 {
1291     SetSessionToken(sessionToken);
1292 }
1293 
EraseUIExtension(int32_t sessionId)1294 void UIAbility::EraseUIExtension(int32_t sessionId)
1295 {
1296     TAG_LOGD(AAFwkTag::UIABILITY, "call");
1297     auto abilityContextImpl = GetAbilityContext();
1298     if (abilityContextImpl == nullptr) {
1299         TAG_LOGE(AAFwkTag::UIABILITY, "null abilityContext");
1300         return;
1301     }
1302     abilityContextImpl->EraseUIExtension(sessionId);
1303 }
1304 
SetIdentityToken(const std::string & identityToken)1305 void UIAbility::SetIdentityToken(const std::string &identityToken)
1306 {
1307     identityToken_ = identityToken;
1308 }
1309 
GetIdentityToken() const1310 std::string UIAbility::GetIdentityToken() const
1311 {
1312     return identityToken_;
1313 }
1314 
CheckRecoveryEnabled()1315 bool UIAbility::CheckRecoveryEnabled()
1316 {
1317     if (useAppSettedRecoveryValue_.load()) {
1318         TAG_LOGD(AAFwkTag::UIABILITY, "use app setted value");
1319         // Check in app recovery, here return true.
1320         return true;
1321     }
1322 
1323     return CheckDefaultRecoveryEnabled();
1324 }
1325 
CheckDefaultRecoveryEnabled()1326 bool UIAbility::CheckDefaultRecoveryEnabled()
1327 {
1328     if (abilityContext_ == nullptr) {
1329         TAG_LOGW(AAFwkTag::UIABILITY, "null context");
1330         return false;
1331     }
1332 
1333     return abilityContext_->GetRestoreEnabled();
1334 }
1335 
IsStartByScb()1336 bool UIAbility::IsStartByScb()
1337 {
1338     if (setting_ == nullptr) {
1339         TAG_LOGW(AAFwkTag::UIABILITY, "null setting_");
1340         return false;
1341     }
1342 
1343     auto value = setting_->GetProperty(AppExecFwk::AbilityStartSetting::IS_START_BY_SCB_KEY);
1344     if (value == "true") {
1345         TAG_LOGD(AAFwkTag::UIABILITY, "start by scb");
1346         return true;
1347     }
1348 
1349     return false;
1350 }
1351 #endif
1352 } // namespace AbilityRuntime
1353 } // namespace OHOS
1354