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