1 /*
2 * Copyright (c) 2021-2022 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_impl.h"
17
18 #include "ability_runtime/js_ability.h"
19 #include "ability_transaction_callback_info.h"
20 #include "data_ability_predicates.h"
21 #include "hilog_wrapper.h"
22 #include "hitrace_meter.h"
23 #include "ohos_application.h"
24 #include "values_bucket.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 const std::string PERMISSION_KEY = "ohos.user.grant.permission";
30 const std::string GRANTED_RESULT_KEY = "ohos.user.grant.permission.result";
31 }
32
Init(std::shared_ptr<OHOSApplication> & application,const std::shared_ptr<AbilityLocalRecord> & record,std::shared_ptr<Ability> & ability,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token,std::shared_ptr<ContextDeal> & contextDeal)33 void AbilityImpl::Init(std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &record,
34 std::shared_ptr<Ability> &ability, std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token,
35 std::shared_ptr<ContextDeal> &contextDeal)
36 {
37 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
38 HILOG_DEBUG("AbilityImpl::init begin");
39 if ((token == nullptr) || (application == nullptr) || (handler == nullptr) || (record == nullptr) ||
40 ability == nullptr || contextDeal == nullptr) {
41 HILOG_ERROR("AbilityImpl::init failed, token is nullptr, application is nullptr, handler is nullptr, record is "
42 "nullptr, ability is nullptr, contextDeal is nullptr");
43 return;
44 }
45
46 token_ = record->GetToken();
47 record->SetAbilityImpl(shared_from_this());
48 ability_ = ability;
49 handler_ = handler;
50 auto info = record->GetAbilityInfo();
51 isStageBasedModel_ = info && info->isStageBasedModel;
52 #ifdef SUPPORT_GRAPHICS
53 if (info && info->type == AbilityType::PAGE) {
54 ability_->SetSceneListener(
55 sptr<WindowLifeCycleImpl>(new WindowLifeCycleImpl(token_, shared_from_this())));
56 }
57 #endif
58 ability_->Init(record->GetAbilityInfo(), application, handler, token);
59 lifecycleState_ = AAFwk::ABILITY_STATE_INITIAL;
60 abilityLifecycleCallbacks_ = application;
61 contextDeal_ = contextDeal;
62 HILOG_DEBUG("AbilityImpl::init end");
63 }
64
Start(const Want & want)65 void AbilityImpl::Start(const Want &want)
66 {
67 HILOG_DEBUG("%{public}s begin.", __func__);
68 if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
69 HILOG_ERROR("AbilityImpl::Start ability_ or abilityLifecycleCallbacks_ is nullptr");
70 return;
71 }
72 #ifdef SUPPORT_GRAPHICS
73 if ((ability_->GetAbilityInfo()->type == AbilityType::PAGE) &&
74 (!ability_->GetAbilityInfo()->isStageBasedModel)) {
75 ability_->HandleCreateAsContinuation(want);
76 }
77
78 if ((ability_->GetAbilityInfo()->type == AbilityType::PAGE) &&
79 (ability_->GetAbilityInfo()->isStageBasedModel)) {
80 ability_->HandleCreateAsRecovery(want);
81 }
82 #endif
83 HILOG_DEBUG("AbilityImpl::Start");
84 ability_->OnStart(want);
85 #ifdef SUPPORT_GRAPHICS
86 if ((ability_->GetAbilityInfo()->type == AppExecFwk::AbilityType::PAGE) &&
87 (ability_->GetAbilityInfo()->isStageBasedModel)) {
88 lifecycleState_ = AAFwk::ABILITY_STATE_STARTED_NEW;
89 } else {
90 #endif
91 if (ability_->GetAbilityInfo()->type == AbilityType::DATA) {
92 lifecycleState_ = AAFwk::ABILITY_STATE_ACTIVE;
93 } else {
94 lifecycleState_ = AAFwk::ABILITY_STATE_INACTIVE;
95 }
96 #ifdef SUPPORT_GRAPHICS
97 }
98 #endif
99
100 abilityLifecycleCallbacks_->OnAbilityStart(ability_);
101 HILOG_DEBUG("%{public}s end.", __func__);
102 }
103
Stop()104 void AbilityImpl::Stop()
105 {
106 HILOG_DEBUG("%{public}s begin.", __func__);
107 if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
108 HILOG_ERROR("AbilityImpl::Stop ability_ or abilityLifecycleCallbacks_ is nullptr");
109 return;
110 }
111
112 ability_->OnStop();
113 StopCallback();
114 HILOG_DEBUG("%{public}s end.", __func__);
115 }
116
Stop(bool & isAsyncCallback)117 void AbilityImpl::Stop(bool &isAsyncCallback)
118 {
119 HILOG_DEBUG("%{public}s begin.", __func__);
120 if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
121 HILOG_ERROR("AbilityImpl::Stop ability_ or abilityLifecycleCallbacks_ is nullptr");
122 isAsyncCallback = false;
123 return;
124 }
125
126 auto *callbackInfo = AbilityTransactionCallbackInfo<>::Create();
127 if (callbackInfo == nullptr) {
128 ability_->OnStop();
129 StopCallback();
130 isAsyncCallback = false;
131 return;
132 }
133 std::weak_ptr<AbilityImpl> weakPtr = shared_from_this();
134 auto asyncCallback = [abilityImplWeakPtr = weakPtr, state = ABILITY_STATE_INITIAL]() {
135 auto abilityImpl = abilityImplWeakPtr.lock();
136 if (abilityImpl == nullptr) {
137 HILOG_ERROR("abilityImpl is nullptr.");
138 return;
139 }
140 abilityImpl->StopCallback();
141 abilityImpl->AbilityTransactionCallback(state);
142 };
143 callbackInfo->Push(asyncCallback);
144
145 ability_->OnStop(callbackInfo, isAsyncCallback);
146 if (!isAsyncCallback) {
147 StopCallback();
148 AbilityTransactionCallbackInfo<>::Destroy(callbackInfo);
149 }
150 // else: callbackInfo will be destroyed after the async callback
151 HILOG_DEBUG("%{public}s end.", __func__);
152 }
153
StopCallback()154 void AbilityImpl::StopCallback()
155 {
156 if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
157 HILOG_ERROR("AbilityImpl::Stop ability_ or abilityLifecycleCallbacks_ is nullptr");
158 return;
159 }
160 #ifdef SUPPORT_GRAPHICS
161 if ((ability_->GetAbilityInfo()->type == AppExecFwk::AbilityType::PAGE) &&
162 (ability_->GetAbilityInfo()->isStageBasedModel)) {
163 lifecycleState_ = AAFwk::ABILITY_STATE_STOPED_NEW;
164 } else {
165 #endif
166 lifecycleState_ = AAFwk::ABILITY_STATE_INITIAL;
167 #ifdef SUPPORT_GRAPHICS
168 }
169 #endif
170 abilityLifecycleCallbacks_->OnAbilityStop(ability_);
171 ability_->DestroyInstance(); // Release window and ability.
172 }
173
Active()174 void AbilityImpl::Active()
175 {
176 HILOG_DEBUG("%{public}s begin.", __func__);
177 if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
178 HILOG_ERROR("AbilityImpl::Active ability_ or abilityLifecycleCallbacks_ is nullptr");
179 return;
180 }
181
182 ability_->OnActive();
183 #ifdef SUPPORT_GRAPHICS
184 if ((lifecycleState_ == AAFwk::ABILITY_STATE_INACTIVE) && (ability_->GetAbilityInfo()->type == AbilityType::PAGE)) {
185 ability_->OnTopActiveAbilityChanged(true);
186 ability_->OnWindowFocusChanged(true);
187 }
188 #endif
189 lifecycleState_ = AAFwk::ABILITY_STATE_ACTIVE;
190 abilityLifecycleCallbacks_->OnAbilityActive(ability_);
191 HILOG_DEBUG("%{public}s end.", __func__);
192 }
193
Inactive()194 void AbilityImpl::Inactive()
195 {
196 HILOG_DEBUG("%{public}s begin.", __func__);
197 if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
198 HILOG_ERROR("AbilityImpl::Inactive ability_ or abilityLifecycleCallbacks_ is nullptr");
199 return;
200 }
201
202 ability_->OnInactive();
203 #ifdef SUPPORT_GRAPHICS
204 if ((lifecycleState_ == AAFwk::ABILITY_STATE_ACTIVE) && (ability_->GetAbilityInfo()->type == AbilityType::PAGE)) {
205 ability_->OnTopActiveAbilityChanged(false);
206 ability_->OnWindowFocusChanged(false);
207 }
208 #endif
209 lifecycleState_ = AAFwk::ABILITY_STATE_INACTIVE;
210 abilityLifecycleCallbacks_->OnAbilityInactive(ability_);
211 HILOG_DEBUG("%{public}s end.", __func__);
212 }
213
IsStageBasedModel() const214 bool AbilityImpl::IsStageBasedModel() const
215 {
216 return isStageBasedModel_;
217 }
218
DispatchSaveAbilityState()219 void AbilityImpl::DispatchSaveAbilityState()
220 {
221 HILOG_DEBUG("%{public}s begin.", __func__);
222 if (ability_ == nullptr || abilityLifecycleCallbacks_ == nullptr) {
223 HILOG_ERROR("AbilityImpl::DispatchSaveAbilityState ability_ or abilityLifecycleCallbacks_ is nullptr");
224 return;
225 }
226
227 needSaveDate_ = true;
228 HILOG_DEBUG("%{public}s end.", __func__);
229 }
230
DispatchRestoreAbilityState(const PacMap & inState)231 void AbilityImpl::DispatchRestoreAbilityState(const PacMap &inState)
232 {
233 HILOG_DEBUG("%{public}s begin.", __func__);
234 if (ability_ == nullptr) {
235 HILOG_ERROR("AbilityImpl::DispatchRestoreAbilityState ability_ is nullptr");
236 return;
237 }
238
239 hasSaveData_ = true;
240 restoreData_ = inState;
241 HILOG_DEBUG("%{public}s end.", __func__);
242 }
243
HandleAbilityTransaction(const Want & want,const AAFwk::LifeCycleStateInfo & targetState)244 void AbilityImpl::HandleAbilityTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState)
245 {}
246
AbilityTransactionCallback(const AAFwk::AbilityLifeCycleState & state)247 void AbilityImpl::AbilityTransactionCallback(const AAFwk::AbilityLifeCycleState &state)
248 {}
249
ConnectAbility(const Want & want)250 sptr<IRemoteObject> AbilityImpl::ConnectAbility(const Want &want)
251 {
252 HILOG_DEBUG("%{public}s begin.", __func__);
253 if (ability_ == nullptr) {
254 HILOG_ERROR("AbilityImpl::ConnectAbility ability_ is nullptr");
255 return nullptr;
256 }
257 sptr<IRemoteObject> object = ability_->OnConnect(want);
258 lifecycleState_ = AAFwk::ABILITY_STATE_ACTIVE;
259 abilityLifecycleCallbacks_->OnAbilityActive(ability_);
260 HILOG_DEBUG("%{public}s end.", __func__);
261
262 return object;
263 }
264
DisconnectAbility(const Want & want)265 void AbilityImpl::DisconnectAbility(const Want &want)
266 {
267 if (ability_ == nullptr) {
268 HILOG_ERROR("Disconnect ability error, ability_ is nullptr.");
269 return;
270 }
271 HILOG_DEBUG("Disconnect ability begin, ability:%{public}s.", ability_->GetAbilityName().c_str());
272 ability_->OnDisconnect(want);
273 }
274
CommandAbility(const Want & want,bool restart,int startId)275 void AbilityImpl::CommandAbility(const Want &want, bool restart, int startId)
276 {
277 HILOG_DEBUG("%{public}s begin.", __func__);
278 if (ability_ == nullptr) {
279 HILOG_ERROR("AbilityImpl::CommandAbility ability_ is nullptr");
280 return;
281 }
282 ability_->OnCommand(want, restart, startId);
283 lifecycleState_ = AAFwk::ABILITY_STATE_ACTIVE;
284 abilityLifecycleCallbacks_->OnAbilityActive(ability_);
285 HILOG_DEBUG("%{public}s end.", __func__);
286 }
287
GetCurrentState()288 int AbilityImpl::GetCurrentState()
289 {
290 return lifecycleState_;
291 }
292
SendResult(int requestCode,int resultCode,const Want & resultData)293 void AbilityImpl::SendResult(int requestCode, int resultCode, const Want &resultData)
294 {
295 HILOG_DEBUG("%{public}s begin.", __func__);
296 if (ability_ == nullptr) {
297 HILOG_ERROR("AbilityImpl::SendResult ability_ is nullptr");
298 return;
299 }
300
301 ability_->OnAbilityResult(requestCode, resultCode, resultData);
302 // for api5 FeatureAbility::startAbilityForResult
303 ability_->OnFeatureAbilityResult(requestCode, resultCode, resultData);
304 HILOG_DEBUG("%{public}s end.", __func__);
305 }
306
NewWant(const Want & want)307 void AbilityImpl::NewWant(const Want &want)
308 {
309 HILOG_DEBUG("%{public}s begin.", __func__);
310 if (ability_ == nullptr) {
311 HILOG_ERROR("AbilityImpl::NewWant ability_ is nullptr");
312 return;
313 }
314 ability_->SetWant(want);
315 ability_->OnNewWant(want);
316 #ifdef SUPPORT_GRAPHICS
317 ability_->ContinuationRestore(want);
318 #endif
319 HILOG_DEBUG("%{public}s end.", __func__);
320 }
321
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)322 std::vector<std::string> AbilityImpl::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
323 {
324 HILOG_DEBUG("AbilityImpl::GetFileTypes");
325 std::vector<std::string> types;
326 return types;
327 }
328
OpenFile(const Uri & uri,const std::string & mode)329 int AbilityImpl::OpenFile(const Uri &uri, const std::string &mode)
330 {
331 HILOG_DEBUG("AbilityImpl::OpenFile");
332 return -1;
333 }
334
OpenRawFile(const Uri & uri,const std::string & mode)335 int AbilityImpl::OpenRawFile(const Uri &uri, const std::string &mode)
336 {
337 HILOG_DEBUG("AbilityImpl::OpenRawFile");
338 return -1;
339 }
340
Insert(const Uri & uri,const NativeRdb::ValuesBucket & value)341 int AbilityImpl::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value)
342 {
343 HILOG_DEBUG("AbilityImpl::Insert");
344 return -1;
345 }
346
Call(const Uri & uri,const std::string & method,const std::string & arg,const AppExecFwk::PacMap & pacMap)347 std::shared_ptr<AppExecFwk::PacMap> AbilityImpl::Call(
348 const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap)
349 {
350 HILOG_DEBUG("AbilityImpl::Call");
351 return nullptr;
352 }
353
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)354 int AbilityImpl::Update(
355 const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
356 {
357 HILOG_DEBUG("AbilityImpl::Update");
358 return -1;
359 }
360
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)361 int AbilityImpl::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
362 {
363 HILOG_DEBUG("AbilityImpl::Delete");
364 return -1;
365 }
366
Query(const Uri & uri,std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)367 std::shared_ptr<NativeRdb::AbsSharedResultSet> AbilityImpl::Query(
368 const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
369 {
370 HILOG_DEBUG("AbilityImpl::Query");
371 return nullptr;
372 }
373
GetType(const Uri & uri)374 std::string AbilityImpl::GetType(const Uri &uri)
375 {
376 HILOG_DEBUG("AbilityImpl::GetType");
377 return "";
378 }
379
Reload(const Uri & uri,const PacMap & extras)380 bool AbilityImpl::Reload(const Uri &uri, const PacMap &extras)
381 {
382 return false;
383 }
384
BatchInsert(const Uri & uri,const std::vector<NativeRdb::ValuesBucket> & values)385 int AbilityImpl::BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values)
386 {
387 HILOG_DEBUG("AbilityImpl::BatchInsert");
388 return -1;
389 }
390
SerUriString(const std::string & uri)391 void AbilityImpl::SerUriString(const std::string &uri)
392 {
393 HILOG_DEBUG("%{public}s begin.", __func__);
394 if (contextDeal_ == nullptr) {
395 HILOG_ERROR("AbilityImpl::SerUriString contextDeal_ is nullptr");
396 return;
397 }
398 contextDeal_->SerUriString(uri);
399 HILOG_DEBUG("%{public}s end.", __func__);
400 }
401
SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo & info)402 void AbilityImpl::SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo &info)
403 {
404 if (contextDeal_ == nullptr) {
405 HILOG_ERROR("AbilityImpl::SetLifeCycleStateInfo contextDeal_ is nullptr");
406 return;
407 }
408 contextDeal_->SetLifeCycleStateInfo(info);
409 }
410
CheckAndRestore()411 bool AbilityImpl::CheckAndRestore()
412 {
413 HILOG_DEBUG("AbilityImpl::CheckAndRestore called start");
414 if (!hasSaveData_) {
415 HILOG_ERROR("AbilityImpl::CheckAndRestore hasSaveData_ is false");
416 return false;
417 }
418
419 if (ability_ == nullptr) {
420 HILOG_ERROR("AbilityImpl::CheckAndRestore ability_ is nullptr");
421 return false;
422 }
423 ability_->OnRestoreAbilityState(restoreData_);
424
425 HILOG_DEBUG("AbilityImpl::CheckAndRestore called end");
426 return true;
427 }
428
CheckAndSave()429 bool AbilityImpl::CheckAndSave()
430 {
431 HILOG_DEBUG("AbilityImpl::CheckAndSave called start");
432 if (!needSaveDate_) {
433 HILOG_ERROR("AbilityImpl::CheckAndSave needSaveDate_ is false");
434 return false;
435 }
436
437 if (ability_ == nullptr) {
438 HILOG_ERROR("AbilityImpl::CheckAndSave ability_ is nullptr");
439 return false;
440 }
441
442 ability_->OnSaveAbilityState(restoreData_);
443 abilityLifecycleCallbacks_->OnAbilitySaveState(restoreData_);
444
445 needSaveDate_ = false;
446
447 HILOG_DEBUG("AbilityImpl::CheckAndSave called end");
448 return true;
449 }
450
GetRestoreData()451 PacMap &AbilityImpl::GetRestoreData()
452 {
453 return restoreData_;
454 }
455
SetCallingContext(const std::string & deviceId,const std::string & bundleName,const std::string & abilityName,const std::string & moduleName)456 void AbilityImpl::SetCallingContext(const std::string &deviceId, const std::string &bundleName,
457 const std::string &abilityName, const std::string &moduleName)
458 {
459 if (ability_ != nullptr) {
460 ability_->SetCallingContext(deviceId, bundleName, abilityName, moduleName);
461 }
462 }
463
NormalizeUri(const Uri & uri)464 Uri AbilityImpl::NormalizeUri(const Uri &uri)
465 {
466 HILOG_DEBUG("AbilityImpl::NormalizeUri");
467 return uri;
468 }
469
DenormalizeUri(const Uri & uri)470 Uri AbilityImpl::DenormalizeUri(const Uri &uri)
471 {
472 HILOG_DEBUG("AbilityImpl::DenormalizeUri");
473 return uri;
474 }
475
ScheduleUpdateConfiguration(const Configuration & config)476 void AbilityImpl::ScheduleUpdateConfiguration(const Configuration &config)
477 {
478 HILOG_DEBUG("%{public}s begin.", __func__);
479 if (ability_ == nullptr) {
480 HILOG_ERROR("AbilityImpl::ScheduleUpdateConfiguration ability_ is nullptr");
481 return;
482 }
483
484 if (lifecycleState_ != AAFwk::ABILITY_STATE_INITIAL) {
485 HILOG_INFO("ability name: [%{public}s]", ability_->GetAbilityName().c_str());
486 ability_->OnConfigurationUpdatedNotify(config);
487 }
488
489 HILOG_DEBUG("%{public}s end.", __func__);
490 }
491
CreatePostEventTimeouter(std::string taskstr)492 std::shared_ptr<AbilityPostEventTimeout> AbilityImpl::CreatePostEventTimeouter(std::string taskstr)
493 {
494 if (ability_ == nullptr) {
495 HILOG_ERROR("AbilityImpl::CreatePostEventTimeouter ability_ is nullptr");
496 return nullptr;
497 }
498
499 return ability_->CreatePostEventTimeouter(taskstr);
500 }
501
ExecuteBatch(const std::vector<std::shared_ptr<DataAbilityOperation>> & operations)502 std::vector<std::shared_ptr<DataAbilityResult>> AbilityImpl::ExecuteBatch(
503 const std::vector<std::shared_ptr<DataAbilityOperation>> &operations)
504 {
505 HILOG_DEBUG("AbilityImpl::ExecuteBatch");
506 std::vector<std::shared_ptr<DataAbilityResult>> results;
507 return results;
508 }
509
ContinueAbility(const std::string & deviceId,uint32_t versionCode)510 void AbilityImpl::ContinueAbility(const std::string& deviceId, uint32_t versionCode)
511 {
512 if (ability_ == nullptr) {
513 HILOG_ERROR("AbilityImpl::ContinueAbility ability_ is nullptr");
514 return;
515 }
516 ability_->ContinueAbilityWithStack(deviceId, versionCode);
517 }
518
NotifyContinuationResult(int32_t result)519 void AbilityImpl::NotifyContinuationResult(int32_t result)
520 {
521 if (ability_ == nullptr) {
522 HILOG_ERROR("AbilityImpl::NotifyContinuationResult ability_ is nullptr");
523 return;
524 }
525 ability_->OnCompleteContinuation(result);
526 }
527
NotifyMemoryLevel(int32_t level)528 void AbilityImpl::NotifyMemoryLevel(int32_t level)
529 {
530 if (ability_ == nullptr) {
531 HILOG_ERROR("AbilityImpl::NotifyMemoryLevel ability_ is nullptr");
532 return;
533 }
534 ability_->OnMemoryLevel(level);
535 }
536
537 #ifdef SUPPORT_GRAPHICS
AfterUnFocused()538 void AbilityImpl::AfterUnFocused()
539 {
540 AfterFocusedCommon(false);
541 }
542
AfterFocused()543 void AbilityImpl::AfterFocused()
544 {
545 AfterFocusedCommon(true);
546 }
547
AfterFocusedCommon(bool isFocused)548 void AbilityImpl::AfterFocusedCommon(bool isFocused)
549 {
550 if (!ability_ || !ability_->GetAbilityInfo() || !contextDeal_ || !handler_) {
551 HILOG_WARN("AbilityImpl::%{public}s failed", isFocused ? "AfterFocused" : "AfterUnFocused");
552 return;
553 }
554 HILOG_INFO("isStageBasedModel: %{public}d", ability_->GetAbilityInfo()->isStageBasedModel);
555 if (ability_->GetAbilityInfo()->isStageBasedModel) {
556 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext = ability_->GetAbilityContext();
557 if (abilityContext == nullptr) {
558 return;
559 }
560
561 std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
562 abilityContext->GetApplicationContext();
563 if (applicationContext != nullptr && !applicationContext->IsAbilityLifecycleCallbackEmpty()) {
564 AbilityRuntime::JsAbility& jsAbility = static_cast<AbilityRuntime::JsAbility&>(*ability_);
565 if (isFocused) {
566 applicationContext->DispatchWindowStageFocus(jsAbility.GetJsAbility(),
567 jsAbility.GetJsWindowStage());
568 } else {
569 applicationContext->DispatchWindowStageUnfocus(jsAbility.GetJsAbility(),
570 jsAbility.GetJsWindowStage());
571 }
572 }
573 return;
574 }
575 if (ability_->GetWant() == nullptr) {
576 HILOG_WARN("want is nullptr.");
577 return;
578 }
579
580 auto task = [abilityImpl = shared_from_this(), want = *(ability_->GetWant()), contextDeal = contextDeal_,
581 focuseMode = isFocused]() {
582 auto info = contextDeal->GetLifeCycleStateInfo();
583 if (focuseMode) {
584 info.state = AbilityLifeCycleState::ABILITY_STATE_ACTIVE;
585 } else {
586 info.state = AbilityLifeCycleState::ABILITY_STATE_INACTIVE;
587 }
588 info.isNewWant = false;
589 abilityImpl->HandleAbilityTransaction(want, info);
590 };
591 handler_->PostTask(task);
592 HILOG_DEBUG("%{public}s end.", __func__);
593 }
594
AfterForeground()595 void AbilityImpl::WindowLifeCycleImpl::AfterForeground()
596 {
597 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
598 HILOG_DEBUG("%{public}s begin.", __func__);
599 auto owner = owner_.lock();
600 if (owner == nullptr || !owner->IsStageBasedModel()) {
601 HILOG_ERROR("Not stage mode ability or abilityImpl is nullptr.");
602 return;
603 }
604 bool needNotifyAMS = false;
605 {
606 std::lock_guard<std::mutex> lock(owner->notifyForegroundLock_);
607 if (owner->notifyForegroundByAbility_) {
608 owner->notifyForegroundByAbility_ = false;
609 needNotifyAMS = true;
610 } else {
611 HILOG_DEBUG("Notify foreground by window, but client's foreground is running.");
612 owner->notifyForegroundByWindow_ = true;
613 }
614 }
615
616 if (needNotifyAMS) {
617 HILOG_INFO("Stage mode ability, window after foreground, notify ability manager service.");
618 PacMap restoreData;
619 AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_,
620 AbilityLifeCycleState::ABILITY_STATE_FOREGROUND_NEW, restoreData);
621 }
622 }
623
AfterBackground()624 void AbilityImpl::WindowLifeCycleImpl::AfterBackground()
625 {
626 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
627 HILOG_DEBUG("%{public}s begin.", __func__);
628 auto owner = owner_.lock();
629 if (owner && !owner->IsStageBasedModel()) {
630 return;
631 }
632
633 HILOG_INFO("UIAbility, window after background.");
634 PacMap restoreData;
635 AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_,
636 AbilityLifeCycleState::ABILITY_STATE_BACKGROUND_NEW, restoreData);
637 }
638
AfterFocused()639 void AbilityImpl::WindowLifeCycleImpl::AfterFocused()
640 {
641 HILOG_DEBUG("%{public}s begin.", __func__);
642 auto owner = owner_.lock();
643 if (owner) {
644 owner->AfterFocused();
645 }
646 HILOG_DEBUG("%{public}s end.", __func__);
647 }
648
AfterUnfocused()649 void AbilityImpl::WindowLifeCycleImpl::AfterUnfocused()
650 {
651 HILOG_DEBUG("%{public}s begin.", __func__);
652 auto owner = owner_.lock();
653 if (owner) {
654 owner->AfterUnFocused();
655 }
656 HILOG_DEBUG("%{public}s end.", __func__);
657 }
658
ForegroundFailed(int32_t type)659 void AbilityImpl::WindowLifeCycleImpl::ForegroundFailed(int32_t type)
660 {
661 HILOG_DEBUG("%{public}s begin.", __func__);
662 PacMap restoreData;
663 if (type == static_cast<int32_t>(OHOS::Rosen::WMError::WM_ERROR_INVALID_OPERATION)) {
664 HILOG_DEBUG("window was freezed.");
665 AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_,
666 AbilityLifeCycleState::ABILITY_STATE_WINDOW_FREEZED, restoreData);
667 } else if (type == static_cast<int32_t>(OHOS::Rosen::WMError::WM_ERROR_INVALID_WINDOW_MODE_OR_SIZE)) {
668 auto owner = owner_.lock();
669 if (owner == nullptr || !owner->IsStageBasedModel()) {
670 HILOG_ERROR("Not stage mode ability or abilityImpl is nullptr.");
671 return;
672 }
673
674 HILOG_DEBUG("The ability is stage mode, schedule foreground invalid mode.");
675 AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_,
676 AbilityLifeCycleState::ABILITY_STATE_INVALID_WINDOW_MODE, restoreData);
677 } else {
678 AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_,
679 AbilityLifeCycleState::ABILITY_STATE_FOREGROUND_FAILED, restoreData);
680 }
681 }
682
Foreground(const Want & want)683 void AbilityImpl::Foreground(const Want &want)
684 {
685 HILOG_DEBUG("%{public}s begin.", __func__);
686 if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
687 HILOG_ERROR("AbilityImpl::Foreground ability_ or abilityLifecycleCallbacks_ is nullptr");
688 return;
689 }
690
691 HILOG_DEBUG("AbilityImpl::Foreground");
692 ability_->OnForeground(want);
693 if ((ability_->GetAbilityInfo()->type == AppExecFwk::AbilityType::PAGE) &&
694 (ability_->GetAbilityInfo()->isStageBasedModel)) {
695 lifecycleState_ = AAFwk::ABILITY_STATE_FOREGROUND_NEW;
696 } else {
697 lifecycleState_ = AAFwk::ABILITY_STATE_INACTIVE;
698 }
699 {
700 std::lock_guard<std::mutex> lock(notifyForegroundLock_);
701 notifyForegroundByAbility_ = true;
702 }
703 abilityLifecycleCallbacks_->OnAbilityForeground(ability_);
704 HILOG_INFO("%{public}s end.", __func__);
705 }
706
Background()707 void AbilityImpl::Background()
708 {
709 HILOG_DEBUG("%{public}s begin.", __func__);
710 if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
711 HILOG_ERROR("AbilityImpl::Background ability_ or abilityLifecycleCallbacks_ is nullptr");
712 return;
713 }
714 ability_->OnLeaveForeground();
715 ability_->OnBackground();
716 if ((ability_->GetAbilityInfo()->type == AppExecFwk::AbilityType::PAGE) &&
717 (ability_->GetAbilityInfo()->isStageBasedModel)) {
718 lifecycleState_ = AAFwk::ABILITY_STATE_BACKGROUND_NEW;
719 } else {
720 lifecycleState_ = AAFwk::ABILITY_STATE_BACKGROUND;
721 }
722 abilityLifecycleCallbacks_->OnAbilityBackground(ability_);
723 HILOG_INFO("%{public}s end.", __func__);
724 }
725
DoKeyDown(const std::shared_ptr<MMI::KeyEvent> & keyEvent)726 void AbilityImpl::DoKeyDown(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
727 {
728 HILOG_DEBUG("AbilityImpl::DoKeyDown called");
729 }
730
DoKeyUp(const std::shared_ptr<MMI::KeyEvent> & keyEvent)731 void AbilityImpl::DoKeyUp(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
732 {
733 HILOG_DEBUG("AbilityImpl::DoKeyUp called");
734 }
735
DoPointerEvent(std::shared_ptr<MMI::PointerEvent> & pointerEvent)736 void AbilityImpl::DoPointerEvent(std::shared_ptr<MMI::PointerEvent>& pointerEvent)
737 {
738 HILOG_DEBUG("AbilityImpl::DoPointerEvent called");
739 }
740
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const741 void AbilityImpl::InputEventConsumerImpl::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
742 {
743 int32_t code = keyEvent->GetKeyAction();
744 if (code == MMI::KeyEvent::KEY_ACTION_DOWN) {
745 abilityImpl_->DoKeyDown(keyEvent);
746 HILOG_DEBUG("AbilityImpl::OnKeyDown keyAction: %{public}d.", code);
747 } else if (code == MMI::KeyEvent::KEY_ACTION_UP) {
748 abilityImpl_->DoKeyUp(keyEvent);
749 HILOG_DEBUG("AbilityImpl::DoKeyUp keyAction: %{public}d.", code);
750 }
751 }
752
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const753 void AbilityImpl::InputEventConsumerImpl::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
754 {
755 HILOG_DEBUG("AbilityImpl::DoPointerEvent called.");
756 abilityImpl_->DoPointerEvent(pointerEvent);
757 }
758 #endif
759 } // namespace AppExecFwk
760 } // namespace OHOS
761