• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "ability.h"
17 
18 #include <cinttypes>
19 #include <thread>
20 
21 #include "ability_post_event_timeout.h"
22 #include "ability_runtime/js_ability.h"
23 #include "abs_shared_result_set.h"
24 #include "bundle_mgr_helper.h"
25 #include "configuration_convertor.h"
26 #include "connection_manager.h"
27 #include "continuation_manager.h"
28 #include "continuation_register_manager.h"
29 #include "continuation_register_manager_proxy.h"
30 #include "data_ability_operation.h"
31 #include "data_ability_predicates.h"
32 #include "data_ability_result.h"
33 #include "data_uri_utils.h"
34 #include "display_util.h"
35 #include "event_report.h"
36 #include "hilog_tag_wrapper.h"
37 #include "hitrace_meter.h"
38 #include "if_system_ability_manager.h"
39 #include "iservice_registry.h"
40 #include "ohos_application.h"
41 #include "reverse_continuation_scheduler_primary.h"
42 #include "reverse_continuation_scheduler_replica.h"
43 #include "reverse_continuation_scheduler_replica_handler_interface.h"
44 #include "runtime.h"
45 #include "singleton.h"
46 #include "system_ability_definition.h"
47 #include "task_handler_client.h"
48 #include "values_bucket.h"
49 
50 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
51 #include "background_task_mgr_helper.h"
52 #include "continuous_task_param.h"
53 #endif
54 
55 #ifdef SUPPORT_SCREEN
56 #include "scene_board_judgement.h"
57 #include "display_type.h"
58 #include "key_event.h"
59 #endif
60 
61 namespace OHOS {
62 namespace AppExecFwk {
63 const std::string Ability::SYSTEM_UI("com.ohos.systemui");
64 const std::string Ability::STATUS_BAR("com.ohos.systemui.statusbar.MainAbility");
65 const std::string Ability::NAVIGATION_BAR("com.ohos.systemui.navigationbar.MainAbility");
66 const std::string Ability::KEYGUARD("com.ohos.screenlock");
67 const std::string DEVICE_MANAGER_BUNDLE_NAME = "com.ohos.devicemanagerui";
68 const std::string DEVICE_MANAGER_NAME = "com.ohos.devicemanagerui.MainAbility";
69 const std::string Ability::DMS_SESSION_ID("sessionId");
70 const std::string Ability::DMS_ORIGIN_DEVICE_ID("deviceId");
71 const int Ability::DEFAULT_DMS_SESSION_ID(0);
72 const std::string LAUNCHER_BUNDLE_NAME = "com.ohos.launcher";
73 const std::string LAUNCHER_ABILITY_NAME = "com.ohos.launcher.MainAbility";
74 const std::string SHOW_ON_LOCK_SCREEN = "ShowOnLockScreen";
75 #ifdef WITH_DLP
76 const std::string DLP_PARAMS_SECURITY_FLAG = "ohos.dlp.params.securityFlag";
77 #endif // WITH_DLP
78 const std::string COMPONENT_STARTUP_NEW_RULES = "component.startup.newRules";
79 
Create(const std::unique_ptr<AbilityRuntime::Runtime> & runtime)80 Ability* Ability::Create(const std::unique_ptr<AbilityRuntime::Runtime>& runtime)
81 {
82     if (!runtime) {
83         return new Ability;
84     }
85 
86     switch (runtime->GetLanguage()) {
87         case AbilityRuntime::Runtime::Language::JS:
88             return AbilityRuntime::JsAbility::Create(runtime);
89 
90         default:
91             return new Ability();
92     }
93 }
94 
Init(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<OHOSApplication> application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)95 void Ability::Init(const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> application,
96     std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token)
97 {
98     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
99     TAG_LOGD(AAFwkTag::ABILITY, "called");
100     application_ = application;
101     abilityInfo_ = abilityInfo;
102     handler_ = handler;
103     AbilityContext::token_ = token;
104 
105 #ifdef SUPPORT_SCREEN
106     // page ability only.
107     if (abilityInfo_->type == AbilityType::PAGE) {
108         if (!abilityInfo_->isStageBasedModel) {
109             abilityWindow_ = std::make_shared<AbilityWindow>();
110             abilityWindow_->Init(handler_, shared_from_this());
111         }
112         continuationManager_ = std::make_shared<ContinuationManager>();
113         std::weak_ptr<Ability> ability = shared_from_this();
114         std::weak_ptr<ContinuationManager> continuationManager = continuationManager_;
115         continuationHandler_ = std::make_shared<ContinuationHandler>(continuationManager, ability);
116         if (!continuationManager_->Init(shared_from_this(), GetToken(), GetAbilityInfo(), continuationHandler_)) {
117             continuationManager_.reset();
118         } else {
119             std::weak_ptr<ContinuationHandler> continuationHandler = continuationHandler_;
120             sptr<ReverseContinuationSchedulerPrimary> primary = sptr<ReverseContinuationSchedulerPrimary>(
121                 new (std::nothrow) ReverseContinuationSchedulerPrimary(continuationHandler, handler_));
122             if (primary == nullptr) {
123                 TAG_LOGE(AAFwkTag::ABILITY, "null primary");
124             } else {
125                 continuationHandler_->SetPrimaryStub(primary);
126                 continuationHandler_->SetAbilityInfo(abilityInfo_);
127             }
128         }
129 
130         // register displayid change callback
131         TAG_LOGD(AAFwkTag::ABILITY, "Start RegisterDisplayListener");
132         abilityDisplayListener_ = new AbilityDisplayListener(ability);
133         Rosen::DisplayManager::GetInstance().RegisterDisplayListener(abilityDisplayListener_);
134     }
135 #endif
136     lifecycle_ = std::make_shared<LifeCycle>();
137     abilityLifecycleExecutor_ = std::make_shared<AbilityLifecycleExecutor>();
138     abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INITIAL);
139 
140     if (abilityContext_ != nullptr) {
141         abilityContext_->RegisterAbilityCallback(weak_from_this());
142     }
143     TAG_LOGD(AAFwkTag::ABILITY, "end");
144 }
145 
AttachAbilityContext(const std::shared_ptr<AbilityRuntime::AbilityContext> & abilityContext)146 void Ability::AttachAbilityContext(const std::shared_ptr<AbilityRuntime::AbilityContext> &abilityContext)
147 {
148     abilityContext_ = abilityContext;
149 }
150 
GetResourceManager() const151 std::shared_ptr<Global::Resource::ResourceManager> Ability::GetResourceManager() const
152 {
153     return AbilityContext::GetResourceManager();
154 }
155 
IsUpdatingConfigurations()156 bool Ability::IsUpdatingConfigurations()
157 {
158     return AbilityContext::IsUpdatingConfigurations();
159 }
160 
OnStart(const Want & want,sptr<AAFwk::SessionInfo> sessionInfo)161 void Ability::OnStart(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo)
162 {
163     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
164     if (abilityInfo_ == nullptr) {
165         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
166         return;
167     }
168 
169 #ifdef WITH_DLP
170     securityFlag_ = want.GetBoolParam(DLP_PARAMS_SECURITY_FLAG, false);
171     (const_cast<Want &>(want)).RemoveParam(DLP_PARAMS_SECURITY_FLAG);
172 #endif // WITH_DLP
173     SetWant(want);
174 #ifdef SUPPORT_SCREEN
175     if (sessionInfo != nullptr) {
176         SetSessionToken(sessionInfo->sessionToken);
177     }
178     TAG_LOGD(AAFwkTag::ABILITY, "ability:%{public}s", abilityInfo_->name.c_str());
179     if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) {
180         int32_t defualtDisplayId = AAFwk::DisplayUtil::GetDefaultDisplayId();
181         int32_t displayId = want.GetIntParam(Want::PARAM_RESV_DISPLAY_ID, defualtDisplayId);
182         TAG_LOGD(AAFwkTag::ABILITY, "abilityName:%{public}s, displayId:%{public}d",
183             abilityInfo_->name.c_str(), displayId);
184         InitFAWindow(want, displayId);
185 
186         if (!UpdateResMgrAndConfiguration(displayId)) {
187             return;
188         }
189     }
190 #endif
191     if (abilityLifecycleExecutor_ == nullptr) {
192         TAG_LOGE(AAFwkTag::ABILITY, "null abilityLifecycleExecutor_");
193         return;
194     }
195     if (!abilityInfo_->isStageBasedModel) {
196         abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INACTIVE);
197     } else {
198         abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::STARTED_NEW);
199     }
200 
201     if (lifecycle_ == nullptr) {
202         TAG_LOGE(AAFwkTag::ABILITY, "null lifecycle_");
203         return;
204     }
205     lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_START, want);
206     TAG_LOGD(AAFwkTag::ABILITY, "end");
207 }
208 
OnStop()209 void Ability::OnStop()
210 {
211     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
212     TAG_LOGD(AAFwkTag::ABILITY, "called");
213 #ifdef SUPPORT_SCREEN
214     (void)Rosen::DisplayManager::GetInstance().UnregisterDisplayListener(abilityDisplayListener_);
215     auto && window = GetWindow();
216     if (window != nullptr) {
217         TAG_LOGD(AAFwkTag::ABILITY, "unregisterDisplayMoveListener");
218         window->UnregisterDisplayMoveListener(abilityDisplayMoveListener_);
219     }
220     // Call JS Func(onWindowStageDestroy) and Release the scene.
221     if (scene_ != nullptr) {
222         scene_->GoDestroy();
223         onSceneDestroyed();
224     }
225 #endif
226     if (abilityLifecycleExecutor_ == nullptr) {
227         TAG_LOGE(AAFwkTag::ABILITY, "null abilityLifecycleExecutor_");
228         return;
229     }
230     abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INITIAL);
231     if (lifecycle_ == nullptr) {
232         TAG_LOGE(AAFwkTag::ABILITY, "null lifecycle_");
233         return;
234     }
235     lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_STOP);
236     TAG_LOGD(AAFwkTag::ABILITY, "end");
237 }
238 
OnStop(AbilityTransactionCallbackInfo<> * callbackInfo,bool & isAsyncCallback)239 void Ability::OnStop(AbilityTransactionCallbackInfo<> *callbackInfo, bool &isAsyncCallback)
240 {
241     isAsyncCallback = false;
242     OnStop();
243 }
244 
OnStopCallback()245 void Ability::OnStopCallback()
246 {
247 }
248 
DestroyInstance()249 void Ability::DestroyInstance()
250 {
251     TAG_LOGD(AAFwkTag::ABILITY, "called");
252 #ifdef SUPPORT_SCREEN
253     // Release the window.
254     if (abilityWindow_ != nullptr && abilityInfo_->type == AppExecFwk::AbilityType::PAGE) {
255         abilityWindow_->OnPostAbilityStop(); // Ability instance will been released when window destroy.
256     }
257 #endif
258     TAG_LOGD(AAFwkTag::ABILITY, "end");
259 }
260 
OnActive()261 void Ability::OnActive()
262 {
263     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
264     TAG_LOGD(AAFwkTag::ABILITY, "called");
265 #ifdef SUPPORT_SCREEN
266     bWindowFocus_ = true;
267 #endif
268     if (abilityLifecycleExecutor_ == nullptr) {
269         TAG_LOGE(AAFwkTag::ABILITY, "null abilityLifecycleExecutor_");
270         return;
271     }
272     abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::ACTIVE);
273 
274     if (lifecycle_ == nullptr) {
275         TAG_LOGE(AAFwkTag::ABILITY, "null lifecycle_");
276         return;
277     }
278     lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE);
279     if (abilityInfo_ == nullptr) {
280         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
281         return;
282     }
283     AAFwk::EventInfo eventInfo;
284     eventInfo.bundleName = abilityInfo_->bundleName;
285     eventInfo.moduleName = abilityInfo_->moduleName;
286     eventInfo.abilityName = abilityInfo_->name;
287     eventInfo.abilityType = static_cast<int32_t>(abilityInfo_->type);
288     eventInfo.bundleType = static_cast<int32_t>(abilityInfo_->applicationInfo.bundleType);
289     if (setWant_ != nullptr) {
290         eventInfo.callerBundleName = setWant_->GetStringParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
291         TAG_LOGI(AAFwkTag::ABILITY, "caller:%{public}s", eventInfo.callerBundleName.c_str());
292     } else {
293         TAG_LOGE(AAFwkTag::ABILITY, "null setWant_");
294     }
295     AAFwk::EventReport::SendAbilityEvent(AAFwk::EventName::ABILITY_ONACTIVE,
296         HiSysEventType::BEHAVIOR, eventInfo);
297     TAG_LOGD(AAFwkTag::ABILITY, "end");
298 }
299 
OnInactive()300 void Ability::OnInactive()
301 {
302     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
303     TAG_LOGD(AAFwkTag::ABILITY, "called");
304 #ifdef SUPPORT_SCREEN
305     bWindowFocus_ = false;
306 #endif
307     if (abilityLifecycleExecutor_ == nullptr) {
308         TAG_LOGE(AAFwkTag::ABILITY, "null abilityLifecycleExecutor_");
309         return;
310     }
311     abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INACTIVE);
312 
313     if (lifecycle_ == nullptr) {
314         TAG_LOGE(AAFwkTag::ABILITY, "null lifecycle_");
315         return;
316     }
317     lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_INACTIVE);
318     if (abilityInfo_ == nullptr) {
319         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
320         return;
321     }
322     AAFwk::EventInfo eventInfo;
323     eventInfo.bundleName = abilityInfo_->bundleName;
324     eventInfo.moduleName = abilityInfo_->moduleName;
325     eventInfo.abilityName = abilityInfo_->name;
326     eventInfo.bundleType = static_cast<int32_t>(abilityInfo_->applicationInfo.bundleType);
327     AAFwk::EventReport::SendAbilityEvent(AAFwk::EventName::ABILITY_ONINACTIVE,
328         HiSysEventType::BEHAVIOR, eventInfo);
329     TAG_LOGD(AAFwkTag::ABILITY, "end");
330 }
331 
IsRestoredInContinuation() const332 bool Ability::IsRestoredInContinuation() const
333 {
334     if (abilityContext_ == nullptr) {
335         TAG_LOGE(AAFwkTag::ABILITY, "null abilityContext_");
336         return false;
337     }
338 
339     if (launchParam_.launchReason != LaunchReason::LAUNCHREASON_CONTINUATION) {
340         TAG_LOGD(AAFwkTag::ABILITY, "launchReason:%{public}d", launchParam_.launchReason);
341         return false;
342     }
343 
344     TAG_LOGD(AAFwkTag::ABILITY, "restored In Continuation");
345     return true;
346 }
347 
ShouldRecoverState(const Want & want)348 bool Ability::ShouldRecoverState(const Want& want)
349 {
350     if (!want.GetBoolParam(Want::PARAM_ABILITY_RECOVERY_RESTART, false)) {
351         TAG_LOGI(AAFwkTag::ABILITY, "not recovery restart");
352         return false;
353     }
354 
355     if (abilityContext_ == nullptr) {
356         TAG_LOGW(AAFwkTag::ABILITY, "null abilityContext_");
357         return false;
358     }
359 
360     if (abilityContext_->GetContentStorage() == nullptr) {
361         TAG_LOGW(AAFwkTag::ABILITY, "null GetContentStorage");
362         return false;
363     }
364 
365     return true;
366 }
367 
NotifyContinuationResult(const Want & want,bool success)368 void Ability::NotifyContinuationResult(const Want& want, bool success)
369 {
370     TAG_LOGI(AAFwkTag::ABILITY, "called");
371 
372     int sessionId = want.GetIntParam(DMS_SESSION_ID, DEFAULT_DMS_SESSION_ID);
373     std::string originDeviceId = want.GetStringParam(DMS_ORIGIN_DEVICE_ID);
374     TAG_LOGD(AAFwkTag::ABILITY, "notify complete continuation");
375     continuationManager_->NotifyCompleteContinuation(
376         originDeviceId, sessionId, success, reverseContinuationSchedulerReplica_);
377 }
378 
OnConnect(const Want & want)379 sptr<IRemoteObject> Ability::OnConnect(const Want &want)
380 {
381     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
382     TAG_LOGD(AAFwkTag::ABILITY, "called");
383     if (abilityLifecycleExecutor_ == nullptr) {
384         TAG_LOGE(AAFwkTag::ABILITY, "null abilityLifecycleExecutor_");
385         return nullptr;
386     }
387     abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::ACTIVE);
388 
389     if (lifecycle_ == nullptr) {
390         TAG_LOGE(AAFwkTag::ABILITY, "null lifecycle_");
391         return nullptr;
392     }
393     lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE);
394     TAG_LOGD(AAFwkTag::ABILITY, "end");
395     return nullptr;
396 }
397 
OnDisconnect(const Want & want)398 void Ability::OnDisconnect(const Want &want)
399 {
400     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
401     TAG_LOGD(AAFwkTag::ABILITY, "called");
402 }
403 
StartAbilityForResult(const Want & want,int requestCode)404 ErrCode Ability::StartAbilityForResult(const Want &want, int requestCode)
405 {
406     TAG_LOGD(AAFwkTag::ABILITY, "start");
407     if (abilityInfo_ == nullptr) {
408         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
409         return ERR_NULL_OBJECT;
410     }
411     TAG_LOGD(AAFwkTag::ABILITY, "abilityType:%{public}d", abilityInfo_->type);
412     if (abilityInfo_->type != AppExecFwk::AbilityType::PAGE) {
413         TAG_LOGE(AAFwkTag::ABILITY, "abilityType:%{public}d mismatch", abilityInfo_->type);
414         return ERR_INVALID_VALUE;
415     }
416     ErrCode err = AbilityContext::StartAbility(want, requestCode);
417     TAG_LOGD(AAFwkTag::ABILITY, "end");
418     return err;
419 }
420 
StartAbilityForResult(const Want & want,int requestCode,AbilityStartSetting abilityStartSetting)421 ErrCode Ability::StartAbilityForResult(const Want &want, int requestCode, AbilityStartSetting abilityStartSetting)
422 {
423     TAG_LOGD(AAFwkTag::ABILITY, "called");
424     if (abilityInfo_ == nullptr) {
425         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
426         return ERR_NULL_OBJECT;
427     }
428     TAG_LOGD(AAFwkTag::ABILITY, "abilityType:%{public}d", abilityInfo_->type);
429     if (abilityInfo_->type != AppExecFwk::AbilityType::PAGE) {
430         TAG_LOGE(AAFwkTag::ABILITY, "abilityType:%{public}d", abilityInfo_->type);
431         return ERR_INVALID_VALUE;
432     }
433     ErrCode err = AbilityContext::StartAbility(want, requestCode, abilityStartSetting);
434     TAG_LOGD(AAFwkTag::ABILITY, "end");
435     return err;
436 }
437 
StartAbility(const Want & want,AbilityStartSetting abilityStartSetting)438 ErrCode Ability::StartAbility(const Want &want, AbilityStartSetting abilityStartSetting)
439 {
440     TAG_LOGD(AAFwkTag::ABILITY, "called");
441     if (abilityInfo_ == nullptr) {
442         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
443         return ERR_NULL_OBJECT;
444     }
445     TAG_LOGD(AAFwkTag::ABILITY, "abilityType:%{public}d", abilityInfo_->type);
446     if (abilityInfo_->type != AppExecFwk::AbilityType::PAGE && abilityInfo_->type != AppExecFwk::AbilityType::SERVICE) {
447         TAG_LOGE(AAFwkTag::ABILITY, "abilityType:%{public}d mismatch", abilityInfo_->type);
448         return ERR_INVALID_VALUE;
449     }
450     ErrCode err = AbilityContext::StartAbility(want, -1, abilityStartSetting);
451     TAG_LOGD(AAFwkTag::ABILITY, "end");
452     return err;
453 }
454 
AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> observer)455 ErrCode Ability::AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> observer)
456 {
457     return AbilityContext::AddFreeInstallObserver(observer);
458 }
459 
GetType(const Uri & uri)460 std::string Ability::GetType(const Uri &uri)
461 {
462     return "";
463 }
464 
Insert(const Uri & uri,const NativeRdb::ValuesBucket & value)465 int Ability::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value)
466 {
467     return 0;
468 }
469 
Call(const Uri & uri,const std::string & method,const std::string & arg,const AppExecFwk::PacMap & pacMap)470 std::shared_ptr<AppExecFwk::PacMap> Ability::Call(
471     const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap)
472 {
473     return nullptr;
474 }
475 
OnConfigurationUpdated(const Configuration & configuration)476 void Ability::OnConfigurationUpdated(const Configuration &configuration)
477 {
478     TAG_LOGD(AAFwkTag::ABILITY, "called");
479 }
480 
OnConfigurationUpdatedNotify(const Configuration & configuration)481 void Ability::OnConfigurationUpdatedNotify(const Configuration &configuration)
482 {
483     TAG_LOGD(AAFwkTag::ABILITY, "called");
484 
485     std::string language;
486     std::string colormode;
487     std::string hasPointerDevice;
488     InitConfigurationProperties(configuration, language, colormode, hasPointerDevice);
489     // Notify ResourceManager
490     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
491     if (resConfig == nullptr) {
492         TAG_LOGE(AAFwkTag::ABILITY, "null resConfig");
493         return;
494     }
495     auto resourceManager = GetResourceManager();
496     if (resourceManager != nullptr) {
497         resourceManager->GetResConfig(*resConfig);
498 #ifdef SUPPORT_SCREEN
499         if (!language.empty()) {
500             UErrorCode status = U_ZERO_ERROR;
501             icu::Locale locale = icu::Locale::forLanguageTag(language, status);
502             TAG_LOGD(AAFwkTag::ABILITY, "get forLanguageTag:%{public}d", static_cast<int>(status));
503             if (status == U_ZERO_ERROR) {
504                 resConfig->SetLocaleInfo(locale);
505             }
506         }
507 #endif
508         if (!colormode.empty()) {
509             resConfig->SetColorMode(ConvertColorMode(colormode));
510         }
511         if (!hasPointerDevice.empty()) {
512             resConfig->SetInputDevice(ConvertHasPointerDevice(hasPointerDevice));
513         }
514         resourceManager->UpdateResConfig(*resConfig);
515         TAG_LOGI(AAFwkTag::ABILITY,
516             "colorMode:%{public}d,hasPointerDevice:%{publis}d",
517             resConfig->GetColorMode(), resConfig->GetInputDevice());
518     }
519 
520     if (abilityContext_ != nullptr && application_ != nullptr) {
521         abilityContext_->SetConfiguration(application_->GetConfiguration());
522     }
523     // Notify Ability Subclass
524     OnConfigurationUpdated(configuration);
525 }
526 
InitConfigurationProperties(const Configuration & changeConfiguration,std::string & language,std::string & colormode,std::string & hasPointerDevice)527 void Ability::InitConfigurationProperties(const Configuration& changeConfiguration, std::string& language,
528     std::string& colormode, std::string& hasPointerDevice)
529 {
530     if (setting_) {
531         auto displayId = std::atoi(setting_->GetProperty(AbilityStartSetting::WINDOW_DISPLAY_ID_KEY).c_str());
532         language = changeConfiguration.GetItem(displayId, AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE);
533         colormode = changeConfiguration.GetItem(displayId, AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
534         hasPointerDevice = changeConfiguration.GetItem(displayId, AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE);
535         TAG_LOGI(AAFwkTag::ABILITY, "displayId: [%{public}d], language: [%{public}s], colormode: [%{public}s], "
536             "hasPointerDevice: [%{public}s]", displayId, language.c_str(), colormode.c_str(), hasPointerDevice.c_str());
537     } else {
538         language = changeConfiguration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE);
539         colormode = changeConfiguration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
540         hasPointerDevice = changeConfiguration.GetItem(AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE);
541         TAG_LOGI(AAFwkTag::ABILITY, "language: [%{public}s], colormode: [%{public}s], hasPointerDevice: [%{public}s]",
542             language.c_str(), colormode.c_str(), hasPointerDevice.c_str());
543     }
544 }
545 
OnMemoryLevel(int level)546 void Ability::OnMemoryLevel(int level)
547 {
548     TAG_LOGD(AAFwkTag::ABILITY, "called");
549 #ifdef SUPPORT_SCREEN
550     if (scene_ == nullptr) {
551         TAG_LOGD(AAFwkTag::ABILITY, "null windowScene");
552         return;
553     }
554     scene_->NotifyMemoryLevel(level);
555 #endif
556 }
557 
OpenRawFile(const Uri & uri,const std::string & mode)558 int Ability::OpenRawFile(const Uri &uri, const std::string &mode)
559 {
560     return -1;
561 }
562 
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)563 int Ability::Update(
564     const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
565 {
566     return 0;
567 }
568 
GetApplication()569 std::shared_ptr<OHOSApplication> Ability::GetApplication()
570 {
571     TAG_LOGD(AAFwkTag::ABILITY, "called");
572     if (application_ == nullptr) {
573         TAG_LOGE(AAFwkTag::ABILITY, "null application_");
574         return nullptr;
575     }
576     TAG_LOGD(AAFwkTag::ABILITY, "end");
577     return application_;
578 }
579 
GetAbilityName()580 std::string Ability::GetAbilityName()
581 {
582     if (abilityInfo_ == nullptr) {
583         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
584         return "";
585     }
586 
587     return abilityInfo_->name;
588 }
589 
GetModuleName()590 std::string Ability::GetModuleName()
591 {
592     if (abilityInfo_ == nullptr) {
593         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
594         return "";
595     }
596 
597     return abilityInfo_->moduleName;
598 }
599 
IsTerminating()600 bool Ability::IsTerminating()
601 {
602     return false;
603 }
604 
OnAbilityResult(int requestCode,int resultCode,const Want & want)605 void Ability::OnAbilityResult(int requestCode, int resultCode, const Want &want)
606 {}
607 
OnBackPressed()608 void Ability::OnBackPressed()
609 {
610     TAG_LOGD(AAFwkTag::ABILITY, "called");
611     if (abilityInfo_ == nullptr) {
612         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
613         return;
614     }
615 
616     if (abilityInfo_->isLauncherAbility == false) {
617         TAG_LOGD(AAFwkTag::ABILITY, "not Launcher");
618         TerminateAbility();
619     }
620     TAG_LOGD(AAFwkTag::ABILITY, "end");
621 }
622 
OnNewWant(const Want & want)623 void Ability::OnNewWant(const Want &want)
624 {
625     TAG_LOGD(AAFwkTag::ABILITY, "called");
626 }
627 
OnRestoreAbilityState(const PacMap & inState)628 void Ability::OnRestoreAbilityState(const PacMap &inState)
629 {
630     TAG_LOGD(AAFwkTag::ABILITY, "called");
631 }
632 
OnSaveAbilityState(PacMap & outState)633 void Ability::OnSaveAbilityState(PacMap &outState)
634 {
635     TAG_LOGD(AAFwkTag::ABILITY, "called");
636 }
637 
OnEventDispatch()638 void Ability::OnEventDispatch()
639 {}
640 
SetWant(const AAFwk::Want & want)641 void Ability::SetWant(const AAFwk::Want &want)
642 {
643     setWant_ = std::make_shared<AAFwk::Want>(want);
644 }
645 
GetWant()646 std::shared_ptr<AAFwk::Want> Ability::GetWant()
647 {
648     return setWant_;
649 }
650 
SetResult(int resultCode,const Want & resultData)651 void Ability::SetResult(int resultCode, const Want &resultData)
652 {
653     TAG_LOGD(AAFwkTag::ABILITY, "called");
654     if (abilityInfo_ == nullptr) {
655         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
656         return;
657     }
658     TAG_LOGD(AAFwkTag::ABILITY, "abilityType:%{public}d", abilityInfo_->type);
659     if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) {
660         AbilityContext::resultWant_ = resultData;
661         AbilityContext::resultCode_ = resultCode;
662     }
663     TAG_LOGD(AAFwkTag::ABILITY, "end");
664 }
665 
OnCommand(const AAFwk::Want & want,bool restart,int startId)666 void Ability::OnCommand(const AAFwk::Want &want, bool restart, int startId)
667 {
668     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
669     TAG_LOGI(AAFwkTag::ABILITY, "restart:%{public}s, startId:%{public}d", restart ? "true" : "false", startId);
670     if (abilityLifecycleExecutor_ == nullptr) {
671         TAG_LOGE(AAFwkTag::ABILITY, "null abilityLifecycleExecutor_");
672         return;
673     }
674     abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::ACTIVE);
675 
676     if (lifecycle_ == nullptr) {
677         TAG_LOGE(AAFwkTag::ABILITY, "null lifecycle_");
678         return;
679     }
680     lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE);
681     TAG_LOGD(AAFwkTag::ABILITY, "end");
682 }
683 
Dump(const std::string & extra)684 void Ability::Dump(const std::string &extra)
685 {
686     TAG_LOGD(AAFwkTag::ABILITY, "called");
687     // abilityInfo
688     if (abilityInfo_ != nullptr) {
689         TAG_LOGD(AAFwkTag::ABILITY, "package:%{public}s", abilityInfo_->package.c_str());
690         TAG_LOGD(AAFwkTag::ABILITY, "name:%{public}s", abilityInfo_->name.c_str());
691         TAG_LOGD(AAFwkTag::ABILITY, "label:%{public}s", abilityInfo_->label.c_str());
692         TAG_LOGD(AAFwkTag::ABILITY, "description:%{public}s", abilityInfo_->description.c_str());
693         TAG_LOGD(AAFwkTag::ABILITY, "iconPath:%{public}s", abilityInfo_->iconPath.c_str());
694         TAG_LOGD(AAFwkTag::ABILITY, "visible:%{public}d", abilityInfo_->visible);
695         TAG_LOGD(AAFwkTag::ABILITY, "kind:%{public}s", abilityInfo_->kind.c_str());
696         TAG_LOGD(AAFwkTag::ABILITY, "type:%{public}d", abilityInfo_->type);
697         TAG_LOGD(AAFwkTag::ABILITY, "orientation:%{public}d", abilityInfo_->orientation);
698         TAG_LOGD(AAFwkTag::ABILITY, "launchMode:%{public}d", abilityInfo_->launchMode);
699         for (auto permission : abilityInfo_->permissions) {
700             TAG_LOGD(AAFwkTag::ABILITY, "permission:%{public}s", permission.c_str());
701         }
702         TAG_LOGD(AAFwkTag::ABILITY, "bundleName:%{public}s", abilityInfo_->bundleName.c_str());
703         TAG_LOGD(AAFwkTag::ABILITY, "applicationName:%{public}s", abilityInfo_->applicationName.c_str());
704     } else {
705         TAG_LOGD(AAFwkTag::ABILITY, "null abilityInfo");
706     }
707 
708     // lifecycle_Event
709     if (lifecycle_ != nullptr) {
710         TAG_LOGD(AAFwkTag::ABILITY, "lifecycle_Event:launchMode:%{public}d", lifecycle_->GetLifecycleState());
711     } else {
712         TAG_LOGD(AAFwkTag::ABILITY, "null lifecycle");
713     }
714 
715     // lifecycle_State
716     if (abilityLifecycleExecutor_ != nullptr) {
717         TAG_LOGD(AAFwkTag::ABILITY, "lifecycle_State:launchMode:%{public}d", abilityLifecycleExecutor_->GetState());
718     } else {
719         TAG_LOGD(AAFwkTag::ABILITY, "null abilityLifecycleExecutor");
720     }
721 
722     // applicationInfo
723     std::shared_ptr<ApplicationInfo> ApplicationInfoPtr = GetApplicationInfo();
724     if (ApplicationInfoPtr != nullptr) {
725         TAG_LOGD(AAFwkTag::ABILITY, "applicationInfo:name:%{public}s", ApplicationInfoPtr->name.c_str());
726         TAG_LOGD(AAFwkTag::ABILITY, "applicationInfo:bundleName:%{public}s", ApplicationInfoPtr->bundleName.c_str());
727     } else {
728         TAG_LOGD(AAFwkTag::ABILITY, "null ApplicationInfoPtr");
729     }
730 }
731 
Dump(const std::vector<std::string> & params,std::vector<std::string> & info)732 void Ability::Dump(const std::vector<std::string> &params, std::vector<std::string> &info)
733 {}
734 
KeepBackgroundRunning(int id,const NotificationRequest & notificationRequest)735 void Ability::KeepBackgroundRunning(int id, const NotificationRequest &notificationRequest)
736 {}
737 
CancelBackgroundRunning()738 void Ability::CancelBackgroundRunning()
739 {}
740 
NormalizeUri(const Uri & uri)741 Uri Ability::NormalizeUri(const Uri &uri)
742 {
743     return uri;
744 }
745 
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)746 int Ability::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
747 {
748     return 0;
749 }
750 
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)751 std::vector<std::string> Ability::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
752 {
753     return types_;
754 }
755 
OpenFile(const Uri & uri,const std::string & mode)756 int Ability::OpenFile(const Uri &uri, const std::string &mode)
757 {
758     return -1;
759 }
760 
Query(const Uri & uri,const std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)761 std::shared_ptr<NativeRdb::AbsSharedResultSet> Ability::Query(
762     const Uri &uri, const std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
763 {
764     return nullptr;
765 }
766 
Reload(const Uri & uri,const PacMap & extras)767 bool Ability::Reload(const Uri &uri, const PacMap &extras)
768 {
769     return false;
770 }
771 
BatchInsert(const Uri & uri,const std::vector<NativeRdb::ValuesBucket> & values)772 int Ability::BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values)
773 {
774     TAG_LOGD(AAFwkTag::ABILITY, "called");
775     int amount = 0;
776     for (auto it = values.begin(); it != values.end(); it++) {
777         if (Insert(uri, *it) >= 0) {
778             amount++;
779         }
780     }
781     TAG_LOGD(AAFwkTag::ABILITY, "insert amount:%{public}d", amount);
782     return amount;
783 }
784 
ContinueAbilityReversibly(const std::string & deviceId)785 void Ability::ContinueAbilityReversibly(const std::string &deviceId)
786 {
787     if (!VerifySupportForContinuation()) {
788         TAG_LOGE(AAFwkTag::ABILITY, "invalid continuation");
789         return;
790     }
791     continuationManager_->ContinueAbility(true, deviceId);
792 }
793 
GetOriginalDeviceId()794 std::string Ability::GetOriginalDeviceId()
795 {
796     return "";
797 }
798 
GetContinuationState()799 ContinuationState Ability::GetContinuationState()
800 {
801     if (!VerifySupportForContinuation()) {
802         TAG_LOGE(AAFwkTag::ABILITY, "invalid continuation");
803         return ContinuationState::LOCAL_RUNNING;
804     }
805     return continuationManager_->GetContinuationState();
806 }
807 
DenormalizeUri(const Uri & uri)808 Uri Ability::DenormalizeUri(const Uri &uri)
809 {
810     return uri;
811 }
812 
GetLifecycle()813 std::shared_ptr<LifeCycle> Ability::GetLifecycle()
814 {
815     TAG_LOGD(AAFwkTag::ABILITY, "called");
816     return lifecycle_;
817 }
818 
RegisterAbilityLifecycleObserver(const std::shared_ptr<ILifecycleObserver> & observer)819 void Ability::RegisterAbilityLifecycleObserver(const std::shared_ptr<ILifecycleObserver> &observer)
820 {
821     TAG_LOGD(AAFwkTag::ABILITY, "called");
822     if (observer == nullptr) {
823         TAG_LOGE(AAFwkTag::ABILITY, "null observer");
824         return;
825     }
826     if (lifecycle_ == nullptr) {
827         TAG_LOGE(AAFwkTag::ABILITY, "null lifecycle_");
828         return;
829     }
830     lifecycle_->AddObserver(observer);
831 }
832 
UnregisterAbilityLifecycleObserver(const std::shared_ptr<ILifecycleObserver> & observer)833 void Ability::UnregisterAbilityLifecycleObserver(const std::shared_ptr<ILifecycleObserver> &observer)
834 {
835     TAG_LOGD(AAFwkTag::ABILITY, "called");
836     if (observer == nullptr) {
837         TAG_LOGE(AAFwkTag::ABILITY, "null observer");
838         return;
839     }
840     if (lifecycle_ == nullptr) {
841         TAG_LOGE(AAFwkTag::ABILITY, "null lifecycle_");
842         return;
843     }
844     lifecycle_->RemoveObserver(observer);
845 }
846 
GetState()847 AbilityLifecycleExecutor::LifecycleState Ability::GetState()
848 {
849     TAG_LOGD(AAFwkTag::ABILITY, "called");
850 
851     if (abilityLifecycleExecutor_ == nullptr) {
852         TAG_LOGE(AAFwkTag::ABILITY, "null abilityLifecycleExecutor_");
853         return AbilityLifecycleExecutor::LifecycleState::UNINITIALIZED;
854     }
855 
856     return (AbilityLifecycleExecutor::LifecycleState)abilityLifecycleExecutor_->GetState();
857 }
858 
StartAbility(const Want & want)859 ErrCode Ability::StartAbility(const Want &want)
860 {
861     TAG_LOGD(AAFwkTag::ABILITY, "called");
862     return AbilityContext::StartAbility(want, -1);
863 }
864 
PostTask(std::function<void ()> task,long delayTime)865 void Ability::PostTask(std::function<void()> task, long delayTime)
866 {
867     TAG_LOGD(AAFwkTag::ABILITY, "called");
868     TaskHandlerClient::GetInstance()->PostTask(task, delayTime);
869     TAG_LOGD(AAFwkTag::ABILITY, "end");
870 }
871 
OnContinue(WantParams & wantParams)872 int32_t Ability::OnContinue(WantParams &wantParams)
873 {
874     return ContinuationManager::OnContinueResult::ON_CONTINUE_ERR;
875 }
876 
ContinueAbilityWithStack(const std::string & deviceId,uint32_t versionCode)877 void Ability::ContinueAbilityWithStack(const std::string &deviceId, uint32_t versionCode)
878 {
879     if (deviceId.empty()) {
880         TAG_LOGE(AAFwkTag::ABILITY, "empty deviceId");
881         return;
882     }
883 
884     if (!VerifySupportForContinuation()) {
885         TAG_LOGE(AAFwkTag::ABILITY, "invalid continuation");
886         return;
887     }
888     continuationManager_->ContinueAbilityWithStack(deviceId, versionCode);
889 }
890 
ContinueAbility(const std::string & deviceId)891 void Ability::ContinueAbility(const std::string &deviceId)
892 {
893     if (deviceId.empty()) {
894         TAG_LOGE(AAFwkTag::ABILITY, "empty deviceId");
895         return;
896     }
897 
898     if (!VerifySupportForContinuation()) {
899         TAG_LOGE(AAFwkTag::ABILITY, "invalid continuation");
900         return;
901     }
902     continuationManager_->ContinueAbility(false, deviceId);
903 }
904 
OnStartContinuation()905 bool Ability::OnStartContinuation()
906 {
907     return false;
908 }
909 
OnSaveData(WantParams & saveData)910 bool Ability::OnSaveData(WantParams &saveData)
911 {
912     return false;
913 }
914 
OnRestoreData(WantParams & restoreData)915 bool Ability::OnRestoreData(WantParams &restoreData)
916 {
917     return false;
918 }
919 
OnSaveState(int32_t reason,WantParams & wantParams)920 int32_t Ability::OnSaveState(int32_t reason, WantParams &wantParams)
921 {
922     return 0;
923 }
924 
OnCompleteContinuation(int result)925 void Ability::OnCompleteContinuation(int result)
926 {
927     TAG_LOGD(AAFwkTag::ABILITY, "initial");
928     if (continuationManager_ == nullptr) {
929         TAG_LOGE(AAFwkTag::ABILITY, "null Continuation manager_");
930         return;
931     }
932 
933     continuationManager_->ChangeProcessStateToInit();
934 }
935 
OnRemoteTerminated()936 void Ability::OnRemoteTerminated()
937 {}
938 
DispatchLifecycleOnForeground(const Want & want)939 void Ability::DispatchLifecycleOnForeground(const Want &want)
940 {
941     if (abilityLifecycleExecutor_ == nullptr) {
942         TAG_LOGE(AAFwkTag::ABILITY, "null abilityLifecycleExecutor_");
943         return;
944     }
945     if (abilityInfo_ != nullptr && abilityInfo_->isStageBasedModel) {
946         abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::FOREGROUND_NEW);
947     } else {
948         abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::INACTIVE);
949     }
950     if (lifecycle_ == nullptr) {
951         TAG_LOGE(AAFwkTag::ABILITY, "null lifecycle_");
952         return;
953     }
954     lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_FOREGROUND, want);
955 }
956 
VerifySupportForContinuation()957 bool Ability::VerifySupportForContinuation()
958 {
959     if (continuationManager_ == nullptr) {
960         TAG_LOGE(AAFwkTag::ABILITY, "null Continuation manager");
961         return false;
962     }
963     return true;
964 }
965 
HandleCreateAsContinuation(const Want & want)966 void Ability::HandleCreateAsContinuation(const Want &want)
967 {
968     if (!IsFlagExists(Want::FLAG_ABILITY_CONTINUATION, want.GetFlags())) {
969         TAG_LOGD(AAFwkTag::ABILITY, "not continuated ability");
970         return;
971     }
972 
973     // check whether it needs reversible
974     bool reversible = false;
975     reversible = IsFlagExists(Want::FLAG_ABILITY_CONTINUATION_REVERSIBLE, want.GetFlags());
976     if (!VerifySupportForContinuation()) {
977         TAG_LOGE(AAFwkTag::ABILITY, "invalid continuation");
978         return;
979     }
980     bool success = continuationManager_->RestoreData(
981         want.GetParams(), reversible, want.GetStringParam(ContinuationHandler::ORIGINAL_DEVICE_ID));
982     if (success && reversible) {
983         // Register this ability to receive reverse continuation callback.
984         std::weak_ptr<IReverseContinuationSchedulerReplicaHandler> ReplicaHandler = continuationHandler_;
985         reverseContinuationSchedulerReplica_ = sptr<ReverseContinuationSchedulerReplica>(
986             new (std::nothrow) ReverseContinuationSchedulerReplica(handler_, ReplicaHandler));
987 
988         if (reverseContinuationSchedulerReplica_ == nullptr) {
989             TAG_LOGE(AAFwkTag::ABILITY, "null reverseContinuationSchedulerReplica");
990             return;
991         }
992     }
993 
994     int sessionId = want.GetIntParam(DMS_SESSION_ID, DEFAULT_DMS_SESSION_ID);
995     std::string originDeviceId = want.GetStringParam(DMS_ORIGIN_DEVICE_ID);
996     TAG_LOGD(AAFwkTag::ABILITY, "notify complete continuation");
997     continuationManager_->NotifyCompleteContinuation(
998         originDeviceId, sessionId, success, reverseContinuationSchedulerReplica_);
999 }
1000 
HandleCreateAsRecovery(const Want & want)1001 void Ability::HandleCreateAsRecovery(const Want &want)
1002 {
1003     TAG_LOGD(AAFwkTag::ABILITY, "called");
1004 }
1005 
IsFlagExists(unsigned int flag,unsigned int flagSet)1006 bool Ability::IsFlagExists(unsigned int flag, unsigned int flagSet)
1007 {
1008     return (flag & flagSet) == flag;
1009 }
1010 
OnSetCaller()1011 Uri Ability::OnSetCaller()
1012 {
1013     return Uri("");
1014 }
1015 
CreatePostEventTimeouter(std::string taskstr)1016 std::shared_ptr<AbilityPostEventTimeout> Ability::CreatePostEventTimeouter(std::string taskstr)
1017 {
1018     return std::make_shared<AbilityPostEventTimeout>(taskstr, handler_);
1019 }
1020 
StartBackgroundRunning(const AbilityRuntime::WantAgent::WantAgent & wantAgent)1021 int Ability::StartBackgroundRunning(const AbilityRuntime::WantAgent::WantAgent &wantAgent)
1022 {
1023 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
1024     auto bundleMgrHelper = DelayedSingleton<BundleMgrHelper>::GetInstance();
1025     if (bundleMgrHelper == nullptr) {
1026         TAG_LOGE(AAFwkTag::ABILITY, "null bundleMgrHelper");
1027         return ERR_NULL_OBJECT;
1028     }
1029     if (abilityInfo_ == nullptr) {
1030         TAG_LOGE(AAFwkTag::ABILITY, "null ability info");
1031         return ERR_INVALID_VALUE;
1032     }
1033     Want want;
1034     want.SetAction("action.system.home");
1035     want.AddEntity("entity.system.home");
1036     want.SetElementName("", abilityInfo_->bundleName, "", "");
1037     AppExecFwk::AbilityInfo abilityInfo;
1038     bundleMgrHelper->QueryAbilityInfo(want, abilityInfo);
1039     std::string appName = bundleMgrHelper->GetAbilityLabel(abilityInfo_->bundleName, abilityInfo.name);
1040     uint32_t defaultBgMode = 0;
1041     BackgroundTaskMgr::ContinuousTaskParam taskParam = BackgroundTaskMgr::ContinuousTaskParam(false, defaultBgMode,
1042         std::make_shared<AbilityRuntime::WantAgent::WantAgent>(wantAgent), abilityInfo_->name, GetToken(), appName);
1043     return BackgroundTaskMgr::BackgroundTaskMgrHelper::RequestStartBackgroundRunning(taskParam);
1044 #else
1045     return ERR_INVALID_OPERATION;
1046 #endif
1047 }
1048 
StopBackgroundRunning()1049 int Ability::StopBackgroundRunning()
1050 {
1051 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
1052     return BackgroundTaskMgr::BackgroundTaskMgrHelper::RequestStopBackgroundRunning(abilityInfo_->name, GetToken());
1053 #else
1054     return ERR_INVALID_OPERATION;
1055 #endif
1056 }
1057 
SetStartAbilitySetting(std::shared_ptr<AbilityStartSetting> setting)1058 void Ability::SetStartAbilitySetting(std::shared_ptr<AbilityStartSetting> setting)
1059 {
1060     TAG_LOGD(AAFwkTag::ABILITY, "called");
1061     setting_ = setting;
1062 }
1063 
SetLaunchParam(const AAFwk::LaunchParam & launchParam)1064 void Ability::SetLaunchParam(const AAFwk::LaunchParam &launchParam)
1065 {
1066     TAG_LOGD(AAFwkTag::ABILITY, "called");
1067     launchParam_ = launchParam;
1068 }
1069 
GetLaunchParam() const1070 const AAFwk::LaunchParam& Ability::GetLaunchParam() const
1071 {
1072     return launchParam_;
1073 }
1074 
ExecuteBatch(const std::vector<std::shared_ptr<DataAbilityOperation>> & operations)1075 std::vector<std::shared_ptr<DataAbilityResult>> Ability::ExecuteBatch(
1076     const std::vector<std::shared_ptr<DataAbilityOperation>> &operations)
1077 {
1078     TAG_LOGD(AAFwkTag::ABILITY, "called");
1079     std::vector<std::shared_ptr<DataAbilityResult>> results;
1080     if (abilityInfo_ == nullptr) {
1081         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo");
1082         return results;
1083     }
1084     if (abilityInfo_->type != AppExecFwk::AbilityType::DATA) {
1085         TAG_LOGE(AAFwkTag::ABILITY, "failed,abilityType:%{public}d", abilityInfo_->type);
1086         return results;
1087     }
1088     size_t len = operations.size();
1089     TAG_LOGD(AAFwkTag::ABILITY, "null operation, len %{public}zu", len);
1090     for (size_t i = 0; i < len; i++) {
1091         std::shared_ptr<DataAbilityOperation> operation = operations[i];
1092         if (operation == nullptr) {
1093             TAG_LOGD(AAFwkTag::ABILITY, "null operation, create DataAbilityResult");
1094             results.push_back(std::make_shared<DataAbilityResult>(0));
1095             continue;
1096         }
1097         ExecuteOperation(operation, results, i);
1098     }
1099     TAG_LOGD(AAFwkTag::ABILITY, "end,%{public}zu", results.size());
1100     return results;
1101 }
ExecuteOperation(std::shared_ptr<DataAbilityOperation> & operation,std::vector<std::shared_ptr<DataAbilityResult>> & results,int index)1102 void Ability::ExecuteOperation(std::shared_ptr<DataAbilityOperation> &operation,
1103     std::vector<std::shared_ptr<DataAbilityResult>> &results, int index)
1104 {
1105     TAG_LOGD(AAFwkTag::ABILITY, "start, index=%{public}d", index);
1106     if (abilityInfo_->type != AppExecFwk::AbilityType::DATA) {
1107         TAG_LOGE(AAFwkTag::ABILITY, "failed,type:%{public}d", abilityInfo_->type);
1108         return;
1109     }
1110     if (index < 0) {
1111         TAG_LOGE(AAFwkTag::ABILITY, "invalid index:%{public}d", index);
1112         return;
1113     }
1114     if (operation == nullptr) {
1115         TAG_LOGW(AAFwkTag::ABILITY, "null operation");
1116         results.push_back(std::make_shared<DataAbilityResult>(0));
1117         return;
1118     }
1119 
1120     int numRows = 0;
1121     std::shared_ptr<NativeRdb::ValuesBucket> valuesBucket = ParseValuesBucketReference(results, operation, index);
1122     auto predicates = ParsePredictionArgsReference(results, operation, index);
1123     if (operation->IsInsertOperation()) {
1124         TAG_LOGD(AAFwkTag::ABILITY, "IsInsertOperation");
1125         numRows = Insert(*(operation->GetUri().get()), *valuesBucket);
1126     } else if (operation->IsDeleteOperation() && predicates) {
1127         TAG_LOGD(AAFwkTag::ABILITY, "IsDeleteOperation");
1128         numRows = Delete(*(operation->GetUri().get()), *predicates);
1129     } else if (operation->IsUpdateOperation() && predicates) {
1130         TAG_LOGD(AAFwkTag::ABILITY, "IsUpdateOperation");
1131         numRows = Update(*(operation->GetUri().get()), *valuesBucket, *predicates);
1132     } else if (operation->IsAssertOperation() && predicates) {
1133         TAG_LOGD(AAFwkTag::ABILITY, "IsAssertOperation");
1134         std::vector<std::string> columns;
1135         auto queryResult = Query(*(operation->GetUri().get()), columns, *predicates);
1136         if (queryResult == nullptr) {
1137             TAG_LOGE(AAFwkTag::ABILITY, "null queryResult");
1138             results.push_back(std::make_shared<DataAbilityResult>(0));
1139             return;
1140         }
1141         (void)CheckAssertQueryResult(queryResult, operation->GetValuesBucket());
1142         queryResult->Close();
1143     } else {
1144         TAG_LOGE(AAFwkTag::ABILITY, "bad type %{public}d", operation->GetType());
1145     }
1146     if (operation->GetExpectedCount() == numRows) {
1147         if (operation->GetUri() != nullptr) {
1148             results.push_back(std::make_shared<DataAbilityResult>(*operation->GetUri(), numRows));
1149         } else {
1150             results.push_back(std::make_shared<DataAbilityResult>(Uri(std::string("")), numRows));
1151         }
1152     }
1153 }
1154 
ParsePredictionArgsReference(std::vector<std::shared_ptr<DataAbilityResult>> & results,std::shared_ptr<DataAbilityOperation> & operation,int numRefs)1155 std::shared_ptr<NativeRdb::DataAbilityPredicates> Ability::ParsePredictionArgsReference(
1156     std::vector<std::shared_ptr<DataAbilityResult>> &results, std::shared_ptr<DataAbilityOperation> &operation,
1157     int numRefs)
1158 {
1159     if (operation == nullptr) {
1160         TAG_LOGE(AAFwkTag::ABILITY, "null intput");
1161         return nullptr;
1162     }
1163 
1164     std::map<int, int> predicatesBackReferencesMap = operation->GetDataAbilityPredicatesBackReferences();
1165     if (predicatesBackReferencesMap.empty()) {
1166         return operation->GetDataAbilityPredicates();
1167     }
1168 
1169     std::vector<std::string> strPredicatesList;
1170     strPredicatesList.clear();
1171     std::shared_ptr<NativeRdb::DataAbilityPredicates> predicates = operation->GetDataAbilityPredicates();
1172     if (predicates == nullptr) {
1173         TAG_LOGD(AAFwkTag::ABILITY, "null predicates");
1174     } else {
1175         TAG_LOGD(AAFwkTag::ABILITY, "operation->GetDataAbilityPredicates isn`t nullptr");
1176         strPredicatesList = predicates->GetWhereArgs();
1177     }
1178 
1179     if (strPredicatesList.empty()) {
1180         TAG_LOGE(AAFwkTag::ABILITY, "strList empty");
1181     }
1182 
1183     for (auto iterMap : predicatesBackReferencesMap) {
1184         TAG_LOGD(AAFwkTag::ABILITY, "predicatesBackReferencesMap first:%{public}d second:%{public}d",
1185             iterMap.first,
1186             iterMap.second);
1187         int tempCount = ChangeRef2Value(results, numRefs, iterMap.second);
1188         if (tempCount < 0) {
1189             TAG_LOGE(AAFwkTag::ABILITY, "tempCount:%{public}d", tempCount);
1190             continue;
1191         }
1192         std::string strPredicates = std::to_string(tempCount);
1193         strPredicatesList.push_back(strPredicates);
1194     }
1195 
1196     if (predicates) {
1197         predicates->SetWhereArgs(strPredicatesList);
1198     }
1199 
1200     return predicates;
1201 }
1202 
ParseValuesBucketReference(std::vector<std::shared_ptr<DataAbilityResult>> & results,std::shared_ptr<DataAbilityOperation> & operation,int numRefs)1203 std::shared_ptr<NativeRdb::ValuesBucket> Ability::ParseValuesBucketReference(
1204     std::vector<std::shared_ptr<DataAbilityResult>> &results, std::shared_ptr<DataAbilityOperation> &operation,
1205     int numRefs)
1206 {
1207     if (operation == nullptr) {
1208         TAG_LOGE(AAFwkTag::ABILITY, "null intput");
1209         return nullptr;
1210     }
1211     if (operation->GetValuesBucketReferences() == nullptr) {
1212         return operation->GetValuesBucket();
1213     }
1214 
1215     NativeRdb::ValuesBucket retValueBucket;
1216     retValueBucket.Clear();
1217     if (operation->GetValuesBucket() != nullptr) {
1218         retValueBucket = *operation->GetValuesBucket();
1219     }
1220 
1221     std::map<std::string, NativeRdb::ValueObject> valuesMapReferences;
1222     operation->GetValuesBucketReferences()->GetAll(valuesMapReferences);
1223 
1224     for (auto itermap : valuesMapReferences) {
1225         std::string key = itermap.first;
1226         TAG_LOGD(AAFwkTag::ABILITY, "key:%{public}s", key.c_str());
1227         NativeRdb::ValueObject obj;
1228         if (!operation->GetValuesBucketReferences()->GetObject(key, obj)) {
1229             TAG_LOGE(AAFwkTag::ABILITY, "GetObject error");
1230             continue;
1231         }
1232         switch (obj.GetType()) {
1233             case NativeRdb::ValueObjectType::TYPE_INT:
1234                 ParseIntValue(obj, key, retValueBucket);
1235                 break;
1236             case NativeRdb::ValueObjectType::TYPE_DOUBLE:
1237                 ParseDoubleValue(obj, key, retValueBucket);
1238                 break;
1239             case NativeRdb::ValueObjectType::TYPE_STRING:
1240                 ParseStringValue(obj, key, retValueBucket);
1241                 break;
1242             case NativeRdb::ValueObjectType::TYPE_BLOB:
1243                 ParseBlobValue(obj, key, retValueBucket);
1244                 break;
1245             case NativeRdb::ValueObjectType::TYPE_BOOL:
1246                 ParseBoolValue(obj, key, retValueBucket);
1247                 break;
1248             default:
1249                 retValueBucket.PutNull(key);
1250                 break;
1251         }
1252     }
1253 
1254     std::map<std::string, NativeRdb::ValueObject> valuesMap;
1255     retValueBucket.GetAll(valuesMap);
1256     return std::make_shared<NativeRdb::ValuesBucket>(valuesMap);
1257 }
1258 
ParseIntValue(const NativeRdb::ValueObject & obj,const std::string & key,NativeRdb::ValuesBucket & retValueBucket) const1259 void Ability::ParseIntValue(const NativeRdb::ValueObject &obj, const std::string &key,
1260     NativeRdb::ValuesBucket &retValueBucket) const
1261 {
1262     int val = 0;
1263     if (obj.GetInt(val) != 0) {
1264         TAG_LOGE(AAFwkTag::ABILITY, "GetInt failed");
1265         return;
1266     }
1267     TAG_LOGD(AAFwkTag::ABILITY, "retValueBucket->PutInt(%{public}s, %{public}d)", key.c_str(), val);
1268     retValueBucket.PutInt(key, val);
1269 }
1270 
ParseDoubleValue(const NativeRdb::ValueObject & obj,const std::string & key,NativeRdb::ValuesBucket & retValueBucket) const1271 void Ability::ParseDoubleValue(const NativeRdb::ValueObject &obj, const std::string &key,
1272     NativeRdb::ValuesBucket &retValueBucket) const
1273 {
1274     double val = 0.0;
1275     if (obj.GetDouble(val) != 0) {
1276         TAG_LOGE(AAFwkTag::ABILITY, "GetDouble failed");
1277         return;
1278     }
1279     TAG_LOGD(AAFwkTag::ABILITY, "retValueBucket->PutDouble(%{public}s, %{public}f)", key.c_str(), val);
1280     retValueBucket.PutDouble(key, val);
1281 }
1282 
ParseStringValue(const NativeRdb::ValueObject & obj,const std::string & key,NativeRdb::ValuesBucket & retValueBucket) const1283 void Ability::ParseStringValue(const NativeRdb::ValueObject &obj, const std::string &key,
1284     NativeRdb::ValuesBucket &retValueBucket) const
1285 {
1286     std::string val = "";
1287     if (obj.GetString(val) != 0) {
1288         TAG_LOGE(AAFwkTag::ABILITY, "GetString failed");
1289         return;
1290     }
1291     TAG_LOGD(AAFwkTag::ABILITY, "retValueBucket->PutString(%{public}s, %{public}s)", key.c_str(), val.c_str());
1292     retValueBucket.PutString(key, val);
1293 }
1294 
ParseBlobValue(const NativeRdb::ValueObject & obj,const std::string & key,NativeRdb::ValuesBucket & retValueBucket) const1295 void Ability::ParseBlobValue(const NativeRdb::ValueObject &obj, const std::string &key,
1296     NativeRdb::ValuesBucket &retValueBucket) const
1297 {
1298     std::vector<uint8_t> val;
1299     if (obj.GetBlob(val) != 0) {
1300         TAG_LOGE(AAFwkTag::ABILITY, "GetBlob failed");
1301         return;
1302     }
1303     TAG_LOGD(AAFwkTag::ABILITY, "retValueBucket->PutBlob(%{public}s, %{public}zu)", key.c_str(), val.size());
1304     retValueBucket.PutBlob(key, val);
1305 }
1306 
ParseBoolValue(const NativeRdb::ValueObject & obj,const std::string & key,NativeRdb::ValuesBucket & retValueBucket) const1307 void Ability::ParseBoolValue(const NativeRdb::ValueObject &obj, const std::string &key,
1308     NativeRdb::ValuesBucket &retValueBucket) const
1309 {
1310     bool val = false;
1311     if (obj.GetBool(val) != 0) {
1312         TAG_LOGE(AAFwkTag::ABILITY, "GetBool failed");
1313         return;
1314     }
1315     TAG_LOGD(AAFwkTag::ABILITY, "retValueBucket->PutBool(%{public}s, %{public}s)", key.c_str(), val ? "true" : "false");
1316     retValueBucket.PutBool(key, val);
1317 }
1318 
ChangeRef2Value(std::vector<std::shared_ptr<DataAbilityResult>> & results,int numRefs,int index)1319 int Ability::ChangeRef2Value(std::vector<std::shared_ptr<DataAbilityResult>> &results, int numRefs, int index)
1320 {
1321     int retval = -1;
1322     if (index >= numRefs) {
1323         TAG_LOGE(AAFwkTag::ABILITY, "index >= numRefs");
1324         return retval;
1325     }
1326 
1327     if (index >= static_cast<int>(results.size())) {
1328         TAG_LOGE(AAFwkTag::ABILITY, "index:%{public}d >= results.size():%{public}zu",
1329             index, results.size());
1330         return retval;
1331     }
1332 
1333     std::shared_ptr<DataAbilityResult> refResult = results[index];
1334     if (refResult == nullptr) {
1335         TAG_LOGE(AAFwkTag::ABILITY, "No.%{public}d refResult", index);
1336         return retval;
1337     }
1338 
1339     if (refResult->GetUri().ToString().empty()) {
1340         retval = refResult->GetCount();
1341     } else {
1342         retval = DataUriUtils::GetId(refResult->GetUri());
1343     }
1344 
1345     return retval;
1346 }
1347 
CheckAssertQueryResult(std::shared_ptr<NativeRdb::AbsSharedResultSet> & queryResult,std::shared_ptr<NativeRdb::ValuesBucket> && valuesBucket)1348 bool Ability::CheckAssertQueryResult(std::shared_ptr<NativeRdb::AbsSharedResultSet> &queryResult,
1349     std::shared_ptr<NativeRdb::ValuesBucket> &&valuesBucket)
1350 {
1351     if (queryResult == nullptr) {
1352         TAG_LOGE(AAFwkTag::ABILITY, "null queryResult");
1353         return true;
1354     }
1355 
1356     if (valuesBucket == nullptr) {
1357         TAG_LOGE(AAFwkTag::ABILITY, "null valuesBucket");
1358         return true;
1359     }
1360 
1361     std::map<std::string, NativeRdb::ValueObject> valuesMap;
1362     valuesBucket->GetAll(valuesMap);
1363     if (valuesMap.empty()) {
1364         TAG_LOGE(AAFwkTag::ABILITY, "empty valuesMap");
1365         return true;
1366     }
1367     int count = 0;
1368     if (queryResult->GetRowCount(count) != 0) {
1369         TAG_LOGE(AAFwkTag::ABILITY, "getRowCount:0");
1370         return true;
1371     }
1372 
1373     for (auto iterMap : valuesMap) {
1374         std::string strObject;
1375         if (iterMap.second.GetString(strObject) != 0) {
1376             TAG_LOGE(AAFwkTag::ABILITY, "strObject failed");
1377             continue;
1378         }
1379         if (strObject.empty()) {
1380             TAG_LOGE(AAFwkTag::ABILITY, "empty strObject");
1381             continue;
1382         }
1383         for (int i = 0; i < count; ++i) {
1384             std::string strName;
1385             if (queryResult->GetString(i, strName) != 0) {
1386                 TAG_LOGE(AAFwkTag::ABILITY, "strName failed");
1387                 continue;
1388             }
1389             if (strName.empty()) {
1390                 TAG_LOGE(AAFwkTag::ABILITY, "empty strName");
1391                 continue;
1392             }
1393             if (strName == strObject) {
1394                 TAG_LOGE(AAFwkTag::ABILITY, "strName=strObject");
1395                 continue;
1396             }
1397 
1398             return false;
1399         }
1400     }
1401 
1402     return true;
1403 }
1404 
CallRequest()1405 sptr<IRemoteObject> Ability::CallRequest()
1406 {
1407     return nullptr;
1408 }
1409 
StartFeatureAbilityForResult(const Want & want,int requestCode,FeatureAbilityTask && task)1410 ErrCode Ability::StartFeatureAbilityForResult(const Want &want, int requestCode, FeatureAbilityTask &&task)
1411 {
1412     TAG_LOGD(AAFwkTag::ABILITY, "called");
1413     resultCallbacks_.insert(make_pair(requestCode, std::move(task)));
1414     ErrCode err = StartAbilityForResult(want, requestCode);
1415     TAG_LOGD(AAFwkTag::ABILITY, "ret:%{public}d", err);
1416     return err;
1417 }
1418 
OnFeatureAbilityResult(int requestCode,int resultCode,const Want & want)1419 void Ability::OnFeatureAbilityResult(int requestCode, int resultCode, const Want &want)
1420 {
1421     TAG_LOGD(AAFwkTag::ABILITY, "called");
1422     auto callback = resultCallbacks_.find(requestCode);
1423     if (callback != resultCallbacks_.end()) {
1424         if (callback->second) {
1425             callback->second(resultCode, want);
1426         }
1427         resultCallbacks_.erase(requestCode);
1428     }
1429     TAG_LOGD(AAFwkTag::ABILITY, "end");
1430 }
1431 
IsUseNewStartUpRule()1432 bool Ability::IsUseNewStartUpRule()
1433 {
1434     if (!isNewRuleFlagSetted_ && setWant_) {
1435         startUpNewRule_ = setWant_->GetBoolParam(COMPONENT_STARTUP_NEW_RULES, false);
1436         isNewRuleFlagSetted_ = true;
1437     }
1438     return startUpNewRule_;
1439 }
1440 
EnableAbilityRecovery(const std::shared_ptr<AbilityRecovery> & abilityRecovery)1441 void Ability::EnableAbilityRecovery(const std::shared_ptr<AbilityRecovery>& abilityRecovery)
1442 {
1443     TAG_LOGD(AAFwkTag::ABILITY, "called");
1444 }
1445 
OnShare(WantParams & wantParams)1446 int32_t Ability::OnShare(WantParams &wantParams)
1447 {
1448     return ERR_OK;
1449 }
1450 
1451 #ifdef SUPPORT_SCREEN
PrintDrawnCompleted()1452 bool Ability::PrintDrawnCompleted()
1453 {
1454     return AbilityContext::PrintDrawnCompleted();
1455 }
1456 
OnSceneCreated()1457 void Ability::OnSceneCreated()
1458 {
1459     TAG_LOGD(AAFwkTag::ABILITY, "called");
1460 }
1461 
OnSceneRestored()1462 void Ability::OnSceneRestored()
1463 {
1464     TAG_LOGD(AAFwkTag::ABILITY, "called");
1465 }
1466 
onSceneDestroyed()1467 void Ability::onSceneDestroyed()
1468 {
1469     TAG_LOGD(AAFwkTag::ABILITY, "called");
1470 }
1471 
OnForeground(const Want & want)1472 void Ability::OnForeground(const Want &want)
1473 {
1474     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1475     TAG_LOGD(AAFwkTag::ABILITY, "called");
1476     DoOnForeground(want);
1477     DispatchLifecycleOnForeground(want);
1478     TAG_LOGD(AAFwkTag::ABILITY, "end");
1479     AAFwk::EventInfo eventInfo;
1480     eventInfo.bundleName = want.GetElement().GetBundleName();
1481     eventInfo.moduleName = want.GetElement().GetModuleName();
1482     eventInfo.abilityName = want.GetElement().GetAbilityName();
1483     eventInfo.callerBundleName = want.GetStringParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
1484     if (abilityInfo_ != nullptr) {
1485         eventInfo.bundleType = static_cast<int32_t>(abilityInfo_->applicationInfo.bundleType);
1486     } else {
1487         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
1488     }
1489     AAFwk::EventReport::SendAbilityEvent(AAFwk::EventName::ABILITY_ONFOREGROUND,
1490         HiSysEventType::BEHAVIOR, eventInfo);
1491 }
1492 
OnBackground()1493 void Ability::OnBackground()
1494 {
1495     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1496     TAG_LOGD(AAFwkTag::ABILITY, "called");
1497     if (abilityInfo_ == nullptr) {
1498         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
1499         return;
1500     }
1501     if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) {
1502         if (abilityInfo_->isStageBasedModel) {
1503             if (scene_ != nullptr) {
1504                 TAG_LOGD(AAFwkTag::ABILITY, "sceneFlag:%{public}d", sceneFlag_);
1505                 scene_->GoBackground(sceneFlag_);
1506             }
1507         } else {
1508             if (abilityWindow_ == nullptr) {
1509                 TAG_LOGE(AAFwkTag::ABILITY, "null abilityWindow_");
1510                 return;
1511             }
1512             TAG_LOGD(AAFwkTag::ABILITY, "sceneFlag:%{public}d", sceneFlag_);
1513             abilityWindow_->OnPostAbilityBackground(sceneFlag_);
1514         }
1515     }
1516 
1517     if (abilityLifecycleExecutor_ == nullptr) {
1518         TAG_LOGE(AAFwkTag::ABILITY, "null abilityLifecycleExecutor_");
1519         return;
1520     }
1521 
1522     if (abilityInfo_->isStageBasedModel) {
1523         abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::BACKGROUND_NEW);
1524     } else {
1525         abilityLifecycleExecutor_->DispatchLifecycleState(AbilityLifecycleExecutor::LifecycleState::BACKGROUND);
1526     }
1527 
1528     if (lifecycle_ == nullptr) {
1529         TAG_LOGE(AAFwkTag::ABILITY, "null lifecycle_");
1530         return;
1531     }
1532     lifecycle_->DispatchLifecycle(LifeCycle::Event::ON_BACKGROUND);
1533     AAFwk::EventInfo eventInfo;
1534     eventInfo.bundleName = abilityInfo_->bundleName;
1535     eventInfo.moduleName = abilityInfo_->moduleName;
1536     eventInfo.abilityName = abilityInfo_->name;
1537     eventInfo.bundleType = static_cast<int32_t>(abilityInfo_->applicationInfo.bundleType);
1538     AAFwk::EventReport::SendAbilityEvent(AAFwk::EventName::ABILITY_ONBACKGROUND,
1539         HiSysEventType::BEHAVIOR, eventInfo);
1540 }
1541 
OnBackPress()1542 bool Ability::OnBackPress()
1543 {
1544     TAG_LOGD(AAFwkTag::ABILITY, "call");
1545     return false;
1546 }
1547 
OnPrepareTerminate()1548 bool Ability::OnPrepareTerminate()
1549 {
1550     TAG_LOGD(AAFwkTag::ABILITY, "call");
1551     return false;
1552 }
1553 
OnKeyDown(const std::shared_ptr<MMI::KeyEvent> & keyEvent)1554 void Ability::OnKeyDown(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
1555 {
1556     TAG_LOGD(AAFwkTag::ABILITY, "called");
1557 }
1558 
OnKeyUp(const std::shared_ptr<MMI::KeyEvent> & keyEvent)1559 void Ability::OnKeyUp(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
1560 {
1561     TAG_LOGD(AAFwkTag::ABILITY, "called");
1562     auto code = keyEvent->GetKeyCode();
1563     if (code == MMI::KeyEvent::KEYCODE_BACK) {
1564         TAG_LOGD(AAFwkTag::ABILITY, "back key pressed");
1565         OnBackPressed();
1566     }
1567 }
1568 
OnPointerEvent(std::shared_ptr<MMI::PointerEvent> & pointerEvent)1569 void Ability::OnPointerEvent(std::shared_ptr<MMI::PointerEvent>& pointerEvent)
1570 {
1571     TAG_LOGD(AAFwkTag::ABILITY, "called");
1572 }
1573 
InitWindow(int32_t displayId,sptr<Rosen::WindowOption> option)1574 void Ability::InitWindow(int32_t displayId, sptr<Rosen::WindowOption> option)
1575 {
1576     if (abilityWindow_ == nullptr) {
1577         TAG_LOGE(AAFwkTag::ABILITY, "null Ability window");
1578         return;
1579     }
1580     abilityWindow_->SetSessionToken(sessionToken_);
1581     abilityWindow_->InitWindow(abilityContext_, sceneListener_, displayId, option, securityFlag_);
1582 }
1583 
GetWindow()1584 const sptr<Rosen::Window> Ability::GetWindow()
1585 {
1586     if (abilityWindow_ == nullptr) {
1587         TAG_LOGD(AAFwkTag::ABILITY, "null Ability window");
1588         return nullptr;
1589     }
1590     return abilityWindow_->GetWindow();
1591 }
1592 
GetScene()1593 std::shared_ptr<Rosen::WindowScene> Ability::GetScene()
1594 {
1595     return scene_;
1596 }
1597 
HasWindowFocus()1598 bool Ability::HasWindowFocus()
1599 {
1600     if (abilityInfo_ == nullptr) {
1601         TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
1602         return false;
1603     }
1604 
1605     if (abilityInfo_->type == AppExecFwk::AbilityType::PAGE) {
1606         return bWindowFocus_;
1607     }
1608 
1609     return false;
1610 }
1611 
SetShowOnLockScreen(bool showOnLockScreen)1612 void Ability::SetShowOnLockScreen(bool showOnLockScreen)
1613 {
1614     TAG_LOGD(AAFwkTag::ABILITY, "showOnLockScreen:%{public}d", showOnLockScreen);
1615     showOnLockScreen_ = showOnLockScreen;
1616     sptr<Rosen::Window> window = nullptr;
1617     if (abilityWindow_ == nullptr || (window = abilityWindow_->GetWindow()) == nullptr) {
1618         TAG_LOGE(AAFwkTag::ABILITY, "window");
1619         return;
1620     }
1621     TAG_LOGD(AAFwkTag::ABILITY, "addWindowFlag, showOnLockScreen:%{public}d",
1622         showOnLockScreen);
1623     if (showOnLockScreen) {
1624         window->AddWindowFlag(Rosen::WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED);
1625         if (abilityInfo_ == nullptr) {
1626             TAG_LOGE(AAFwkTag::ABILITY, "null abilityInfo_");
1627             return;
1628         }
1629         AAFwk::EventInfo eventInfo;
1630         eventInfo.bundleName = abilityInfo_->bundleName;
1631         eventInfo.moduleName = abilityInfo_->moduleName;
1632         eventInfo.abilityName = abilityInfo_->name;
1633         AAFwk::EventReport::SendKeyEvent(AAFwk::EventName::FA_SHOW_ON_LOCK, HiSysEventType::BEHAVIOR, eventInfo);
1634     } else {
1635         window->RemoveWindowFlag(Rosen::WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED);
1636     }
1637 }
1638 
OnLeaveForeground()1639 void Ability::OnLeaveForeground()
1640 {}
1641 
SetVolumeTypeAdjustedByKey(int volumeType)1642 void Ability::SetVolumeTypeAdjustedByKey(int volumeType)
1643 {}
1644 
SetWindowBackgroundColor(int red,int green,int blue)1645 int Ability::SetWindowBackgroundColor(int red, int green, int blue)
1646 {
1647     return -1;
1648 }
1649 
GetContentInfo()1650 std::string Ability::GetContentInfo()
1651 {
1652     if (scene_ == nullptr) {
1653         return "";
1654     }
1655     return scene_->GetContentInfo(Rosen::BackupAndRestoreType::CONTINUATION);
1656 }
1657 
OnWindowFocusChanged(bool hasFocus)1658 void Ability::OnWindowFocusChanged(bool hasFocus)
1659 {}
1660 
OnTopActiveAbilityChanged(bool topActive)1661 void Ability::OnTopActiveAbilityChanged(bool topActive)
1662 {}
1663 
OnCreate(const Want & want)1664 FormProviderInfo Ability::OnCreate(const Want &want)
1665 {
1666     TAG_LOGD(AAFwkTag::ABILITY, "called");
1667     FormProviderInfo formProviderInfo;
1668     return formProviderInfo;
1669 }
1670 
OnShare(int64_t formId,AAFwk::WantParams & wantParams)1671 bool Ability::OnShare(int64_t formId, AAFwk::WantParams &wantParams)
1672 {
1673     TAG_LOGD(AAFwkTag::ABILITY, "called");
1674     return false;
1675 }
1676 
OnDelete(const int64_t formId)1677 void Ability::OnDelete(const int64_t formId)
1678 {}
1679 
OnUpdate(const int64_t formId,const AAFwk::WantParams & wantParams)1680 void Ability::OnUpdate(const int64_t formId, const AAFwk::WantParams &wantParams)
1681 {}
1682 
OnCastTemptoNormal(const int64_t formId)1683 void Ability::OnCastTemptoNormal(const int64_t formId)
1684 {}
1685 
OnVisibilityChanged(const std::map<int64_t,int32_t> & formEventsMap)1686 void Ability::OnVisibilityChanged(const std::map<int64_t, int32_t> &formEventsMap)
1687 {}
1688 
OnTriggerEvent(const int64_t formId,const std::string & message)1689 void Ability::OnTriggerEvent(const int64_t formId, const std::string &message)
1690 {}
1691 
OnAcquireFormState(const Want & want)1692 FormState Ability::OnAcquireFormState(const Want &want)
1693 {
1694     return FormState::DEFAULT;
1695 }
1696 
SetSceneListener(const sptr<Rosen::IWindowLifeCycle> & listener)1697 void Ability::SetSceneListener(const sptr<Rosen::IWindowLifeCycle> &listener)
1698 {
1699     sceneListener_ = listener;
1700 }
1701 
GetWindowOption(const Want & want)1702 sptr<Rosen::WindowOption> Ability::GetWindowOption(const Want &want)
1703 {
1704     sptr<Rosen::WindowOption> option = new Rosen::WindowOption();
1705     if (option == nullptr) {
1706         TAG_LOGE(AAFwkTag::ABILITY, "null option");
1707         return nullptr;
1708     }
1709     auto windowMode = want.GetIntParam(Want::PARAM_RESV_WINDOW_MODE,
1710         AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_UNDEFINED);
1711     TAG_LOGD(AAFwkTag::ABILITY, "window mode:%{public}d", windowMode);
1712     option->SetWindowMode(static_cast<Rosen::WindowMode>(windowMode));
1713     bool showOnLockScreen = false;
1714     if (abilityInfo_) {
1715         std::vector<CustomizeData> datas = abilityInfo_->metaData.customizeData;
1716         for (CustomizeData data : datas) {
1717             if (data.name == SHOW_ON_LOCK_SCREEN) {
1718                 showOnLockScreen = true;
1719             }
1720         }
1721     }
1722     if (showOnLockScreen_ || showOnLockScreen) {
1723         TAG_LOGD(AAFwkTag::ABILITY, "add window flag WINDOW_FLAG_SHOW_WHEN_LOCKED");
1724         option->AddWindowFlag(Rosen::WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED);
1725     }
1726 
1727     if (want.GetElement().GetBundleName() == LAUNCHER_BUNDLE_NAME &&
1728         want.GetElement().GetAbilityName() == LAUNCHER_ABILITY_NAME) {
1729         TAG_LOGD(AAFwkTag::ABILITY, "set window type for launcher");
1730         option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_DESKTOP);
1731     }
1732     return option;
1733 }
1734 
DoOnForeground(const Want & want)1735 void Ability::DoOnForeground(const Want& want)
1736 {
1737     if (abilityWindow_ == nullptr) {
1738         TAG_LOGD(AAFwkTag::ABILITY, "null Ability window");
1739         return;
1740     }
1741 
1742     TAG_LOGD(AAFwkTag::ABILITY, "sceneFlag:%{public}d", sceneFlag_);
1743     auto window = abilityWindow_->GetWindow();
1744     if (window != nullptr && want.HasParameter(Want::PARAM_RESV_WINDOW_MODE)) {
1745         auto windowMode = want.GetIntParam(Want::PARAM_RESV_WINDOW_MODE,
1746             AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_UNDEFINED);
1747         window->SetWindowMode(static_cast<Rosen::WindowMode>(windowMode));
1748         TAG_LOGD(AAFwkTag::ABILITY, "set window mode = %{public}d", windowMode);
1749     }
1750     abilityWindow_->OnPostAbilityForeground(sceneFlag_);
1751     TAG_LOGD(AAFwkTag::ABILITY, "end");
1752 }
1753 
GetCurrentWindowMode()1754 int Ability::GetCurrentWindowMode()
1755 {
1756     TAG_LOGD(AAFwkTag::ABILITY, "called");
1757     auto windowMode = static_cast<int>(Rosen::WindowMode::WINDOW_MODE_UNDEFINED);
1758     if (scene_ == nullptr) {
1759         return windowMode;
1760     }
1761     auto window = scene_->GetMainWindow();
1762     if (window != nullptr) {
1763         windowMode = static_cast<int>(window->GetWindowMode());
1764     }
1765     return windowMode;
1766 }
1767 
SetMissionLabel(const std::string & label)1768 ErrCode Ability::SetMissionLabel(const std::string &label)
1769 {
1770     TAG_LOGD(AAFwkTag::ABILITY, "called");
1771     if (!abilityInfo_ || abilityInfo_->type != AppExecFwk::AbilityType::PAGE) {
1772         TAG_LOGE(AAFwkTag::ABILITY, "invalid ability info");
1773         return -1;
1774     }
1775 
1776     // stage mode
1777     if (abilityInfo_->isStageBasedModel) {
1778         if (scene_ == nullptr) {
1779             TAG_LOGE(AAFwkTag::ABILITY, "null scene_");
1780             return -1;
1781         }
1782         auto window = scene_->GetMainWindow();
1783         if (window == nullptr) {
1784             TAG_LOGE(AAFwkTag::ABILITY, "null window");
1785             return -1;
1786         }
1787 
1788         if (window->SetAPPWindowLabel(label) != OHOS::Rosen::WMError::WM_OK) {
1789             TAG_LOGE(AAFwkTag::ABILITY, "failed");
1790             return -1;
1791         }
1792         return ERR_OK;
1793     }
1794 
1795     // fa mode
1796     if (abilityWindow_ == nullptr) {
1797         TAG_LOGE(AAFwkTag::ABILITY, "null abilityWindow");
1798         return -1;
1799     }
1800     return abilityWindow_->SetMissionLabel(label);
1801 }
1802 
SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> & icon)1803 ErrCode Ability::SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon)
1804 {
1805     TAG_LOGD(AAFwkTag::ABILITY, "called");
1806     if (!abilityInfo_ || abilityInfo_->type != AppExecFwk::AbilityType::PAGE) {
1807         TAG_LOGE(AAFwkTag::ABILITY, "invalid ability info");
1808         return -1;
1809     }
1810 
1811     // stage mode
1812     if (abilityInfo_->isStageBasedModel) {
1813         if (scene_ == nullptr) {
1814             TAG_LOGE(AAFwkTag::ABILITY, "null scene_");
1815             return -1;
1816         }
1817         auto window = scene_->GetMainWindow();
1818         if (window == nullptr) {
1819             TAG_LOGE(AAFwkTag::ABILITY, "null window");
1820             return -1;
1821         }
1822 
1823         if (window->SetAPPWindowIcon(icon) != OHOS::Rosen::WMError::WM_OK) {
1824             TAG_LOGE(AAFwkTag::ABILITY, "failed");
1825             return -1;
1826         }
1827         return ERR_OK;
1828     }
1829 
1830     // fa mode
1831     if (abilityWindow_ == nullptr) {
1832         TAG_LOGE(AAFwkTag::ABILITY, "null abilityWindow");
1833         return -1;
1834     }
1835     return abilityWindow_->SetMissionIcon(icon);
1836 }
1837 
GetWindowRect(int32_t & left,int32_t & top,int32_t & width,int32_t & height)1838 void Ability::GetWindowRect(int32_t &left, int32_t &top, int32_t &width, int32_t &height)
1839 {
1840     TAG_LOGD(AAFwkTag::ABILITY, "call");
1841     if (scene_ == nullptr) {
1842         TAG_LOGE(AAFwkTag::ABILITY, "null scene_");
1843         return;
1844     }
1845     auto window = scene_->GetMainWindow();
1846     if (window == nullptr) {
1847         TAG_LOGE(AAFwkTag::ABILITY, "null window");
1848         return;
1849     }
1850     left = window->GetRect().posX_;
1851     top = window->GetRect().posY_;
1852     width = static_cast<int32_t>(window->GetRect().width_);
1853     height = static_cast<int32_t>(window->GetRect().height_);
1854     TAG_LOGI(AAFwkTag::ABILITY, "left:%{public}d, top:%{public}d, width:%{public}d, height:%{public}d",
1855         left, top, width, height);
1856 }
1857 
GetUIContent()1858 Ace::UIContent* Ability::GetUIContent()
1859 {
1860     TAG_LOGD(AAFwkTag::ABILITY, "call");
1861     if (scene_ == nullptr) {
1862         TAG_LOGE(AAFwkTag::ABILITY, "null scene_");
1863         return nullptr;
1864     }
1865     auto window = scene_->GetMainWindow();
1866     if (window == nullptr) {
1867         TAG_LOGE(AAFwkTag::ABILITY, "null window");
1868         return nullptr;
1869     }
1870     return window->GetUIContent();
1871 }
1872 
OnCreate(Rosen::DisplayId displayId)1873 void Ability::OnCreate(Rosen::DisplayId displayId)
1874 {
1875     TAG_LOGD(AAFwkTag::ABILITY, "called");
1876 }
1877 
OnDestroy(Rosen::DisplayId displayId)1878 void Ability::OnDestroy(Rosen::DisplayId displayId)
1879 {
1880     TAG_LOGD(AAFwkTag::ABILITY, "called");
1881 }
1882 
OnChange(Rosen::DisplayId displayId)1883 void Ability::OnChange(Rosen::DisplayId displayId)
1884 {
1885     TAG_LOGD(AAFwkTag::ABILITY, "displayId:%{public}" PRIu64"", displayId);
1886 
1887     // Get display
1888     auto display = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
1889     if (!display) {
1890         TAG_LOGE(AAFwkTag::ABILITY, "displayId %{public}" PRIu64" failed", displayId);
1891         return;
1892     }
1893 
1894     // Notify ResourceManager
1895     float density = display->GetVirtualPixelRatio();
1896     int32_t width = display->GetWidth();
1897     int32_t height = display->GetHeight();
1898     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
1899     if (resConfig != nullptr) {
1900         auto resourceManager = GetResourceManager();
1901         if (resourceManager != nullptr) {
1902             resourceManager->GetResConfig(*resConfig);
1903             resConfig->SetScreenDensity(density);
1904             resConfig->SetDirection(ConvertDirection(height, width));
1905             resourceManager->UpdateResConfig(*resConfig);
1906             TAG_LOGI(AAFwkTag::ABILITY, "notify ResourceManager, Density:%{public}f, Direction:%{public}d",
1907                 resConfig->GetScreenDensity(), resConfig->GetDirection());
1908         }
1909     }
1910 
1911     // Notify ability
1912     Configuration newConfig;
1913     newConfig.AddItem(displayId, ConfigurationInner::APPLICATION_DIRECTION, GetDirectionStr(height, width));
1914     newConfig.AddItem(displayId, ConfigurationInner::APPLICATION_DENSITYDPI, GetDensityStr(density));
1915 
1916     if (application_ == nullptr) {
1917         TAG_LOGE(AAFwkTag::ABILITY, "null application_");
1918         return;
1919     }
1920 
1921     auto configuration = application_->GetConfiguration();
1922     if (!configuration) {
1923         TAG_LOGE(AAFwkTag::ABILITY, "null configuration");
1924         return;
1925     }
1926 
1927     std::vector<std::string> changeKeyV;
1928     configuration->CompareDifferent(changeKeyV, newConfig);
1929     TAG_LOGD(AAFwkTag::ABILITY, "changeKeyV size:%{public}zu", changeKeyV.size());
1930     if (!changeKeyV.empty()) {
1931         configuration->Merge(changeKeyV, newConfig);
1932         auto task = [ability = shared_from_this(), configuration = *configuration]() {
1933             ability->OnConfigurationUpdated(configuration);
1934         };
1935         handler_->PostTask(task, "Ability:OnChange");
1936 
1937         auto diffConfiguration = std::make_shared<AppExecFwk::Configuration>(newConfig);
1938         TAG_LOGI(AAFwkTag::ABILITY, "update display config %{public}s for all windows",
1939             diffConfiguration->GetName().c_str());
1940         Rosen::Window::UpdateConfigurationForAll(diffConfiguration);
1941     }
1942 
1943     TAG_LOGD(AAFwkTag::ABILITY, "end");
1944 }
1945 
OnDisplayMove(Rosen::DisplayId from,Rosen::DisplayId to)1946 void Ability::OnDisplayMove(Rosen::DisplayId from, Rosen::DisplayId to)
1947 {
1948     TAG_LOGI(AAFwkTag::ABILITY, "displayId %{public}" PRIu64" to %{public}" PRIu64"", from, to);
1949 
1950     auto display = Rosen::DisplayManager::GetInstance().GetDisplayById(to);
1951     if (!display) {
1952         TAG_LOGE(AAFwkTag::ABILITY, "displayId %{public}" PRIu64" failed", to);
1953         return;
1954     }
1955 
1956     // Get new display config
1957     float density = display->GetVirtualPixelRatio();
1958     int32_t width = display->GetWidth();
1959     int32_t height = display->GetHeight();
1960     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
1961     if (resConfig != nullptr) {
1962         auto resourceManager = GetResourceManager();
1963         if (resourceManager != nullptr) {
1964             resourceManager->GetResConfig(*resConfig);
1965             resConfig->SetScreenDensity(density);
1966             resConfig->SetDirection(ConvertDirection(height, width));
1967             resourceManager->UpdateResConfig(*resConfig);
1968             TAG_LOGI(AAFwkTag::ABILITY, "notify ResourceManager, Density:%{public}f, Direction:%{public}d",
1969                 resConfig->GetScreenDensity(), resConfig->GetDirection());
1970         }
1971     }
1972 
1973     Configuration newConfig;
1974     newConfig.AddItem(ConfigurationInner::APPLICATION_DISPLAYID, std::to_string(to));
1975     newConfig.AddItem(to, ConfigurationInner::APPLICATION_DIRECTION, GetDirectionStr(height, width));
1976     newConfig.AddItem(to, ConfigurationInner::APPLICATION_DENSITYDPI, GetDensityStr(density));
1977 
1978     if (application_ == nullptr) {
1979         TAG_LOGE(AAFwkTag::ABILITY, "null application_");
1980         return;
1981     }
1982 
1983     std::vector<std::string> changeKeyV;
1984     auto configuration = application_->GetConfiguration();
1985     if (!configuration) {
1986         TAG_LOGE(AAFwkTag::ABILITY, "null configuration");
1987         return;
1988     }
1989 
1990     configuration->CompareDifferent(changeKeyV, newConfig);
1991     TAG_LOGD(AAFwkTag::ABILITY, "changeKeyV size :%{public}zu", changeKeyV.size());
1992     if (!changeKeyV.empty()) {
1993         configuration->Merge(changeKeyV, newConfig);
1994         auto task = [ability = shared_from_this(), configuration = *configuration]() {
1995             ability->OnConfigurationUpdated(configuration);
1996         };
1997         handler_->PostTask(task, "Ability:OnDisplayMove");
1998     }
1999 }
2000 
RequestFocus(const Want & want)2001 void Ability::RequestFocus(const Want &want)
2002 {
2003     TAG_LOGD(AAFwkTag::ABILITY, "called");
2004     if (abilityWindow_ == nullptr) {
2005         return;
2006     }
2007     auto window = abilityWindow_->GetWindow();
2008     if (window != nullptr && want.HasParameter(Want::PARAM_RESV_WINDOW_MODE)) {
2009         auto windowMode = want.GetIntParam(Want::PARAM_RESV_WINDOW_MODE,
2010             AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_UNDEFINED);
2011         window->SetWindowMode(static_cast<Rosen::WindowMode>(windowMode));
2012         TAG_LOGD(AAFwkTag::ABILITY, "set window mode = %{public}d", windowMode);
2013     }
2014     abilityWindow_->OnPostAbilityForeground(sceneFlag_);
2015 }
2016 
SetWakeUpScreen(bool wakeUp)2017 void Ability::SetWakeUpScreen(bool wakeUp)
2018 {
2019     TAG_LOGD(AAFwkTag::ABILITY, "wakeUp:%{public}d", wakeUp);
2020     if (abilityWindow_ == nullptr) {
2021         TAG_LOGE(AAFwkTag::ABILITY, "null abilityWindow_");
2022         return;
2023     }
2024     auto window = abilityWindow_->GetWindow();
2025     if (window == nullptr) {
2026         TAG_LOGE(AAFwkTag::ABILITY, "null window");
2027         return;
2028     }
2029     window->SetTurnScreenOn(wakeUp);
2030 }
2031 
SetDisplayOrientation(int orientation)2032 void Ability::SetDisplayOrientation(int orientation)
2033 {
2034     TAG_LOGD(AAFwkTag::ABILITY, "fa mode, orientation:%{public}d", orientation);
2035     if (abilityWindow_ == nullptr) {
2036         TAG_LOGE(AAFwkTag::ABILITY, "null Ability window");
2037         return;
2038     }
2039     auto window = abilityWindow_->GetWindow();
2040     if (window == nullptr) {
2041         TAG_LOGE(AAFwkTag::ABILITY, "null window");
2042         return;
2043     }
2044     if (orientation == static_cast<int>(DisplayOrientation::FOLLOWRECENT)) {
2045         int defaultOrientation = 0;
2046         if (setWant_) {
2047             orientation = setWant_->GetIntParam("ohos.aafwk.Orientation", defaultOrientation);
2048         } else {
2049             orientation = defaultOrientation;
2050         }
2051     }
2052     if (orientation == static_cast<int>(DisplayOrientation::LANDSCAPE)) {
2053         TAG_LOGD(AAFwkTag::ABILITY, "set LANDSCAPE");
2054         window->SetRequestedOrientation(Rosen::Orientation::HORIZONTAL);
2055     } else if (orientation == static_cast<int>(DisplayOrientation::PORTRAIT)) {
2056         TAG_LOGD(AAFwkTag::ABILITY, "set PORTRAIT");
2057         window->SetRequestedOrientation(Rosen::Orientation::VERTICAL);
2058     } else {
2059         TAG_LOGD(AAFwkTag::ABILITY, "set UNSPECIFIED");
2060         window->SetRequestedOrientation(Rosen::Orientation::UNSPECIFIED);
2061     }
2062 }
2063 
GetDisplayOrientation()2064 int Ability::GetDisplayOrientation()
2065 {
2066     TAG_LOGD(AAFwkTag::ABILITY, "called");
2067     if (abilityWindow_ == nullptr) {
2068         TAG_LOGE(AAFwkTag::ABILITY, "null abilityWindow_");
2069         return -1;
2070     }
2071     TAG_LOGD(AAFwkTag::ABILITY, "fa mode");
2072     auto window = abilityWindow_->GetWindow();
2073     if (window == nullptr) {
2074         TAG_LOGE(AAFwkTag::ABILITY, "null window");
2075         return -1;
2076     }
2077     auto orientation = window->GetRequestedOrientation();
2078     if (orientation == Rosen::Orientation::HORIZONTAL) {
2079         TAG_LOGD(AAFwkTag::ABILITY, "get window orientation: LANDSCAPE");
2080         return static_cast<int>(DisplayOrientation::LANDSCAPE);
2081     }
2082     if (orientation == Rosen::Orientation::VERTICAL) {
2083         TAG_LOGD(AAFwkTag::ABILITY, "get window orientation: PORTRAIT");
2084         return static_cast<int>(DisplayOrientation::PORTRAIT);
2085     }
2086     TAG_LOGD(AAFwkTag::ABILITY, "get window orientation: UNSPECIFIED");
2087     return 0;
2088 }
2089 
ContinuationRestore(const Want & want)2090 void Ability::ContinuationRestore(const Want &want)
2091 {
2092     TAG_LOGD(AAFwkTag::ABILITY, "called");
2093 }
2094 
CreateModalUIExtension(const Want & want)2095 int Ability::CreateModalUIExtension(const Want &want)
2096 {
2097     TAG_LOGD(AAFwkTag::ABILITY, "call");
2098     auto abilityContextImpl = GetAbilityContext();
2099     if (abilityContextImpl == nullptr) {
2100         TAG_LOGE(AAFwkTag::ABILITY, "null abilityContext");
2101         return ERR_INVALID_VALUE;
2102     }
2103     return abilityContextImpl->CreateModalUIExtensionWithApp(want);
2104 }
2105 
SetSessionToken(sptr<IRemoteObject> sessionToken)2106 void Ability::SetSessionToken(sptr<IRemoteObject> sessionToken)
2107 {
2108     std::lock_guard lock(sessionTokenMutex_);
2109     sessionToken_ = sessionToken;
2110 }
2111 
UpdateSessionToken(sptr<IRemoteObject> sessionToken)2112 void Ability::UpdateSessionToken(sptr<IRemoteObject> sessionToken)
2113 {
2114     SetSessionToken(sessionToken);
2115     if (abilityWindow_ == nullptr) {
2116         TAG_LOGE(AAFwkTag::ABILITY, "null Ability window");
2117         return;
2118     }
2119     abilityWindow_->SetSessionToken(sessionToken);
2120 }
2121 
InitFAWindow(const Want & want,int32_t displayId)2122 void Ability::InitFAWindow(const Want &want, int32_t displayId)
2123 {
2124     if (!abilityInfo_->isStageBasedModel) {
2125         auto option = GetWindowOption(want);
2126         InitWindow(displayId, option);
2127     }
2128 
2129     if (abilityWindow_ != nullptr) {
2130         TAG_LOGD(AAFwkTag::ABILITY, "get window from abilityWindow");
2131         auto window = abilityWindow_->GetWindow();
2132         if (window) {
2133             TAG_LOGD(AAFwkTag::ABILITY, "call RegisterDisplayMoveListener, windowId:%{public}d",
2134                 window->GetWindowId());
2135             abilityDisplayMoveListener_ = new AbilityDisplayMoveListener(weak_from_this());
2136             window->RegisterDisplayMoveListener(abilityDisplayMoveListener_);
2137         }
2138     }
2139 }
2140 
UpdateResMgrAndConfiguration(int32_t displayId)2141 bool Ability::UpdateResMgrAndConfiguration(int32_t displayId)
2142 {
2143     auto display = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
2144     if (!display) {
2145         TAG_LOGI(AAFwkTag::ABILITY, "invalid display");
2146         return true;
2147     }
2148     float density = display->GetVirtualPixelRatio();
2149     int32_t width = display->GetWidth();
2150     int32_t height = display->GetHeight();
2151     std::shared_ptr<Configuration> configuration = nullptr;
2152     if (application_) {
2153         configuration = application_->GetConfiguration();
2154     }
2155     if (configuration) {
2156         std::string direction = GetDirectionStr(height, width);
2157         configuration->AddItem(displayId, ConfigurationInner::APPLICATION_DIRECTION, direction);
2158         configuration->AddItem(displayId, ConfigurationInner::APPLICATION_DENSITYDPI, GetDensityStr(density));
2159         configuration->AddItem(ConfigurationInner::APPLICATION_DISPLAYID, std::to_string(displayId));
2160         UpdateContextConfiguration();
2161     }
2162 
2163     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
2164     if (resConfig == nullptr) {
2165         TAG_LOGE(AAFwkTag::ABILITY, "null resConfig");
2166         return false;
2167     }
2168     auto resourceManager = GetResourceManager();
2169     if (resourceManager != nullptr) {
2170         resourceManager->GetResConfig(*resConfig);
2171         resConfig->SetScreenDensity(density);
2172         resConfig->SetDirection(ConvertDirection(height, width));
2173         resourceManager->UpdateResConfig(*resConfig);
2174         TAG_LOGD(AAFwkTag::ABILITY, "notify ResourceManager, Density:%{public}f, Direction:%{public}d",
2175             resConfig->GetScreenDensity(), resConfig->GetDirection());
2176     }
2177     return true;
2178 }
2179 
EraseUIExtension(int32_t sessionId)2180 void Ability::EraseUIExtension(int32_t sessionId)
2181 {
2182     TAG_LOGD(AAFwkTag::ABILITY, "call");
2183     auto abilityContextImpl = GetAbilityContext();
2184     if (abilityContextImpl == nullptr) {
2185         TAG_LOGE(AAFwkTag::ABILITY, "null abilityContext");
2186         return;
2187     }
2188     abilityContextImpl->EraseUIExtension(sessionId);
2189 }
2190 #endif
2191 }  // namespace AppExecFwk
2192 }  // namespace OHOS
2193