• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "bytrace.h"
19 #include "data_ability_predicates.h"
20 #include "hilog_wrapper.h"
21 #include "values_bucket.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 static bool g_useNewMission = false;
26 static bool g_isMissionFlagSetted = false;
27 const std::string PERMISSION_KEY = "ohos.user.grant.permission";
28 const std::string GRANTED_RESULT_KEY = "ohos.user.grant.permission.result";
29 
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)30 void AbilityImpl::Init(std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &record,
31     std::shared_ptr<Ability> &ability, std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token,
32     std::shared_ptr<ContextDeal> &contextDeal)
33 {
34     BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
35     HILOG_INFO("AbilityImpl::init begin");
36     if ((token == nullptr) || (application == nullptr) || (handler == nullptr) || (record == nullptr) ||
37         ability == nullptr || contextDeal == nullptr) {
38         HILOG_ERROR("AbilityImpl::init failed, token is nullptr, application is nullptr, handler is nullptr, record is "
39                  "nullptr, ability is nullptr, contextDeal is nullptr");
40         return;
41     }
42 
43     token_ = record->GetToken();
44     record->SetAbilityImpl(shared_from_this());
45     ability_ = ability;
46     handler_ = handler;
47     auto info = record->GetAbilityInfo();
48     isStageBasedModel_ = info && info->isStageBasedModel;
49 #ifdef SUPPORT_GRAPHICS
50     if (info && info->type == AbilityType::PAGE) {
51         ability_->SetSceneListener(
52             sptr<WindowLifeCycleImpl>(new (std::nothrow) WindowLifeCycleImpl(token_, shared_from_this())));
53     }
54 #endif
55     ability_->Init(record->GetAbilityInfo(), application, handler, token);
56     lifecycleState_ = AAFwk::ABILITY_STATE_INITIAL;
57     abilityLifecycleCallbacks_ = application;
58     contextDeal_ = contextDeal;
59     HILOG_INFO("AbilityImpl::init end");
60 }
61 
62 /**
63  * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_INACTIVE. And notifies the application
64  * that it belongs to of the lifecycle status.
65  *
66  * @param want  The Want object to switch the life cycle.
67  */
Start(const Want & want)68 void AbilityImpl::Start(const Want &want)
69 {
70     HILOG_INFO("%{public}s begin.", __func__);
71     if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
72         HILOG_ERROR("AbilityImpl::Start ability_ or abilityLifecycleCallbacks_ is nullptr");
73         return;
74     }
75 #ifdef SUPPORT_GRAPHICS
76     if ((ability_->GetAbilityInfo()->type == AbilityType::PAGE) &&
77         (!ability_->GetAbilityInfo()->isStageBasedModel)) {
78         ability_->HandleCreateAsContinuation(want);
79     }
80 #endif
81     HILOG_INFO("AbilityImpl::Start");
82     ability_->OnStart(want);
83 #ifdef SUPPORT_GRAPHICS
84     if ((ability_->GetAbilityInfo()->type == AppExecFwk::AbilityType::PAGE) &&
85         (ability_->GetAbilityInfo()->isStageBasedModel)) {
86         lifecycleState_ = AAFwk::ABILITY_STATE_STARTED_NEW;
87     } else {
88 #endif
89         if (ability_->GetAbilityInfo()->type == AbilityType::DATA) {
90             lifecycleState_ = AAFwk::ABILITY_STATE_ACTIVE;
91         } else {
92             lifecycleState_ = AAFwk::ABILITY_STATE_INACTIVE;
93         }
94 #ifdef SUPPORT_GRAPHICS
95     }
96 #endif
97 
98     abilityLifecycleCallbacks_->OnAbilityStart(ability_);
99 #ifdef SUPPORT_GRAPHICS
100     // Multimodal Events Register
101     if ((ability_->GetAbilityInfo()->type == AppExecFwk::AbilityType::PAGE) &&
102         (!ability_->GetAbilityInfo()->isStageBasedModel)) {
103         WindowEventRegister();
104     }
105 #endif
106     HILOG_INFO("%{public}s end.", __func__);
107 }
108 
109 /**
110  * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_INITIAL. And notifies the application
111  * that it belongs to of the lifecycle status.
112  *
113  */
Stop()114 void AbilityImpl::Stop()
115 {
116     HILOG_INFO("%{public}s begin.", __func__);
117     if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
118         HILOG_ERROR("AbilityImpl::Stop ability_ or abilityLifecycleCallbacks_ is nullptr");
119         return;
120     }
121 
122     ability_->OnStop();
123 #ifdef SUPPORT_GRAPHICS
124     if ((ability_->GetAbilityInfo()->type == AppExecFwk::AbilityType::PAGE) &&
125         (ability_->GetAbilityInfo()->isStageBasedModel)) {
126         lifecycleState_ = AAFwk::ABILITY_STATE_STOPED_NEW;
127     } else {
128 #endif
129         lifecycleState_ = AAFwk::ABILITY_STATE_INITIAL;
130 #ifdef SUPPORT_GRAPHICS
131     }
132 #endif
133     abilityLifecycleCallbacks_->OnAbilityStop(ability_);
134     ability_->DestroyInstance(); // Release window and ability.
135     ability_ = nullptr;
136     HILOG_INFO("%{public}s end.", __func__);
137 }
138 
139 /**
140  * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_ACTIVE. And notifies the application
141  * that it belongs to of the lifecycle status.
142  *
143  */
Active()144 void AbilityImpl::Active()
145 {
146     HILOG_INFO("%{public}s begin.", __func__);
147     if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
148         HILOG_ERROR("AbilityImpl::Active ability_ or abilityLifecycleCallbacks_ is nullptr");
149         return;
150     }
151 
152     HILOG_INFO("AbilityImpl::Active");
153     ability_->OnActive();
154 #ifdef SUPPORT_GRAPHICS
155     if ((lifecycleState_ == AAFwk::ABILITY_STATE_INACTIVE) && (ability_->GetAbilityInfo()->type == AbilityType::PAGE)) {
156         ability_->OnTopActiveAbilityChanged(true);
157         ability_->OnWindowFocusChanged(true);
158     }
159 #endif
160     lifecycleState_ = AAFwk::ABILITY_STATE_ACTIVE;
161     abilityLifecycleCallbacks_->OnAbilityActive(ability_);
162     HILOG_INFO("%{public}s end.", __func__);
163 }
164 
165 /**
166  * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_INACTIVE. And notifies the application
167  * that it belongs to of the lifecycle status.
168  *
169  */
Inactive()170 void AbilityImpl::Inactive()
171 {
172     HILOG_INFO("%{public}s begin.", __func__);
173     if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
174         HILOG_ERROR("AbilityImpl::Inactive ability_ or abilityLifecycleCallbacks_ is nullptr");
175         return;
176     }
177 
178     HILOG_INFO("AbilityImpl::Inactive");
179     ability_->OnInactive();
180 #ifdef SUPPORT_GRAPHICS
181     if ((lifecycleState_ == AAFwk::ABILITY_STATE_ACTIVE) && (ability_->GetAbilityInfo()->type == AbilityType::PAGE)) {
182         ability_->OnTopActiveAbilityChanged(false);
183         ability_->OnWindowFocusChanged(false);
184     }
185 #endif
186     lifecycleState_ = AAFwk::ABILITY_STATE_INACTIVE;
187     abilityLifecycleCallbacks_->OnAbilityInactive(ability_);
188     HILOG_INFO("%{public}s end.", __func__);
189 }
190 
IsStageBasedModel() const191 bool AbilityImpl::IsStageBasedModel() const
192 {
193     return isStageBasedModel_;
194 }
195 
GetCompatibleVersion()196 int AbilityImpl::GetCompatibleVersion()
197 {
198     if (ability_) {
199         return ability_->GetCompatibleVersion();
200     }
201 
202     return -1;
203 }
204 
205 #ifdef SUPPORT_GRAPHICS
AfterUnFocused()206 void AbilityImpl::AfterUnFocused()
207 {
208     HILOG_INFO("%{public}s begin.", __func__);
209     if (!ability_ || !ability_->GetAbilityInfo() || !contextDeal_ || !handler_) {
210         HILOG_ERROR("AbilityImpl::AfterUnFocused failed");
211         return;
212     }
213 
214     if (ability_->GetAbilityInfo()->isStageBasedModel) {
215         HILOG_INFO("new version ability, do nothing when after unfocused.");
216         return;
217     }
218 
219     HILOG_INFO("old version ability, window after unfocused.");
220     auto task = [abilityImpl = shared_from_this(), want = *(ability_->GetWant()), contextDeal = contextDeal_]() {
221         auto info = contextDeal->GetLifeCycleStateInfo();
222         info.state = AbilityLifeCycleState::ABILITY_STATE_INACTIVE;
223         info.isNewWant = false;
224         abilityImpl->HandleAbilityTransaction(want, info);
225     };
226     handler_->PostTask(task);
227     HILOG_INFO("%{public}s end.", __func__);
228 }
229 
AfterFocused()230 void AbilityImpl::AfterFocused()
231 {
232     HILOG_INFO("%{public}s begin.", __func__);
233     if (ability_->GetAbilityInfo()->isStageBasedModel) {
234         HILOG_INFO("new version ability, do nothing when after focused.");
235         return;
236     }
237 
238     if (!ability_ || !ability_->GetAbilityInfo() || !contextDeal_ || !handler_) {
239         HILOG_ERROR("AbilityImpl::AfterFocused failed");
240         return;
241     }
242 
243     HILOG_INFO("fa mode ability, window after focused.");
244     auto task = [abilityImpl = shared_from_this(), want = *(ability_->GetWant()), contextDeal = contextDeal_]() {
245         auto info = contextDeal->GetLifeCycleStateInfo();
246         info.state = AbilityLifeCycleState::ABILITY_STATE_ACTIVE;
247         info.isNewWant = false;
248         abilityImpl->HandleAbilityTransaction(want, info);
249     };
250     handler_->PostTask(task);
251     HILOG_INFO("%{public}s end.", __func__);
252 }
253 
AfterForeground()254 void AbilityImpl::WindowLifeCycleImpl::AfterForeground()
255 {
256     BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
257     HILOG_INFO("%{public}s begin.", __func__);
258     auto owner = owner_.lock();
259     if (owner && !owner->IsStageBasedModel()) {
260         return;
261     }
262 
263     HILOG_INFO("new version ability, window after foreground.");
264     PacMap restoreData;
265     AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_,
266         AbilityLifeCycleState::ABILITY_STATE_FOREGROUND_NEW, restoreData);
267 }
268 
AfterBackground()269 void AbilityImpl::WindowLifeCycleImpl::AfterBackground()
270 {
271     BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
272     HILOG_INFO("%{public}s begin.", __func__);
273     auto owner = owner_.lock();
274     if (owner && !owner->IsStageBasedModel()) {
275         return;
276     }
277 
278     HILOG_INFO("new version ability, window after background.");
279     PacMap restoreData;
280     AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_,
281         AbilityLifeCycleState::ABILITY_STATE_BACKGROUND_NEW, restoreData);
282 }
283 
AfterFocused()284 void AbilityImpl::WindowLifeCycleImpl::AfterFocused()
285 {
286     HILOG_INFO("%{public}s begin.", __func__);
287     auto owner = owner_.lock();
288     if (owner) {
289         owner->AfterFocused();
290     }
291     HILOG_INFO("%{public}s end.", __func__);
292 }
293 
AfterUnfocused()294 void AbilityImpl::WindowLifeCycleImpl::AfterUnfocused()
295 {
296     HILOG_INFO("%{public}s begin.", __func__);
297     auto owner = owner_.lock();
298     if (owner) {
299         owner->AfterUnFocused();
300     }
301     HILOG_INFO("%{public}s end.", __func__);
302 }
303 
304 /**
305  * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_INACTIVE. And notifies the application
306  * that it belongs to of the lifecycle status.
307  *
308  * @param want The Want object to switch the life cycle.
309  */
Foreground(const Want & want)310 void AbilityImpl::Foreground(const Want &want)
311 {
312     HILOG_INFO("%{public}s begin.", __func__);
313     if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
314         HILOG_ERROR("AbilityImpl::Foreground ability_ or abilityLifecycleCallbacks_ is nullptr");
315         return;
316     }
317 
318     HILOG_INFO("AbilityImpl::Foreground");
319     ability_->OnForeground(want);
320     if ((ability_->GetAbilityInfo()->type == AppExecFwk::AbilityType::PAGE) &&
321         (ability_->GetAbilityInfo()->isStageBasedModel)) {
322         lifecycleState_ = AAFwk::ABILITY_STATE_FOREGROUND_NEW;
323     } else {
324         lifecycleState_ = AAFwk::ABILITY_STATE_INACTIVE;
325     }
326     abilityLifecycleCallbacks_->OnAbilityForeground(ability_);
327     HILOG_INFO("%{public}s end.", __func__);
328 }
329 
330 /**
331  * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_BACKGROUND. And notifies the application
332  * that it belongs to of the lifecycle status.
333  *
334  */
Background()335 void AbilityImpl::Background()
336 {
337     HILOG_INFO("%{public}s begin.", __func__);
338     if (ability_ == nullptr || ability_->GetAbilityInfo() == nullptr || abilityLifecycleCallbacks_ == nullptr) {
339         HILOG_ERROR("AbilityImpl::Background ability_ or abilityLifecycleCallbacks_ is nullptr");
340         return;
341     }
342 
343     HILOG_INFO("AbilityImpl::Background");
344     ability_->OnLeaveForeground();
345     ability_->OnBackground();
346     if ((ability_->GetAbilityInfo()->type == AppExecFwk::AbilityType::PAGE) &&
347         (ability_->GetAbilityInfo()->isStageBasedModel)) {
348         lifecycleState_ = AAFwk::ABILITY_STATE_BACKGROUND_NEW;
349     } else {
350         lifecycleState_ = AAFwk::ABILITY_STATE_BACKGROUND;
351     }
352     abilityLifecycleCallbacks_->OnAbilityBackground(ability_);
353     HILOG_INFO("%{public}s end.", __func__);
354 }
355 #endif
356 
357 /**
358  * @brief Save data and states of an ability when it is restored by the system. and Calling information back to Ability.
359  *        This method should be implemented by a Page ability.
360  *
361  */
DispatchSaveAbilityState()362 void AbilityImpl::DispatchSaveAbilityState()
363 {
364     HILOG_INFO("%{public}s begin.", __func__);
365     if (ability_ == nullptr || abilityLifecycleCallbacks_ == nullptr) {
366         HILOG_ERROR("AbilityImpl::DispatchSaveAbilityState ability_ or abilityLifecycleCallbacks_ is nullptr");
367         return;
368     }
369 
370     HILOG_INFO("AbilityImpl::DispatchSaveAbilityState");
371     needSaveDate_ = true;
372     HILOG_INFO("%{public}s end.", __func__);
373 }
374 
375 /**
376  * @brief Restores data and states of an ability when it is restored by the system. and Calling information back to
377  * Ability. This method should be implemented by a Page ability.
378  * @param instate The Want object to connect to.
379  *
380  */
DispatchRestoreAbilityState(const PacMap & inState)381 void AbilityImpl::DispatchRestoreAbilityState(const PacMap &inState)
382 {
383     HILOG_INFO("%{public}s begin.", __func__);
384     if (ability_ == nullptr) {
385         HILOG_ERROR("AbilityImpl::DispatchRestoreAbilityState ability_ is nullptr");
386         return;
387     }
388 
389     hasSaveData_ = true;
390     restoreData_ = inState;
391     HILOG_INFO("%{public}s end.", __func__);
392 }
393 
HandleAbilityTransaction(const Want & want,const AAFwk::LifeCycleStateInfo & targetState)394 void AbilityImpl::HandleAbilityTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState)
395 {}
396 
397 /**
398  * @brief Connect the ability. and Calling information back to Ability.
399  *
400  * @param want The Want object to connect to.
401  *
402  */
ConnectAbility(const Want & want)403 sptr<IRemoteObject> AbilityImpl::ConnectAbility(const Want &want)
404 {
405     HILOG_INFO("%{public}s begin.", __func__);
406     if (ability_ == nullptr) {
407         HILOG_ERROR("AbilityImpl::ConnectAbility ability_ is nullptr");
408         return nullptr;
409     }
410 
411     HILOG_INFO("AbilityImpl:: ConnectAbility");
412     sptr<IRemoteObject> object = ability_->OnConnect(want);
413     lifecycleState_ = AAFwk::ABILITY_STATE_ACTIVE;
414     abilityLifecycleCallbacks_->OnAbilityActive(ability_);
415     HILOG_INFO("%{public}s end.", __func__);
416 
417     return object;
418 }
419 
420 /**
421  * @brief Disconnects the connected object.
422  *
423  * @param want The Want object to disconnect to.
424  */
DisconnectAbility(const Want & want)425 void AbilityImpl::DisconnectAbility(const Want &want)
426 {
427     HILOG_INFO("%{public}s begin.", __func__);
428     if (ability_ == nullptr) {
429         HILOG_ERROR("AbilityImpl::DisconnectAbility ability_ is nullptr");
430         return;
431     }
432 
433     ability_->OnDisconnect(want);
434     HILOG_INFO("%{public}s end.", __func__);
435 }
436 
437 /**
438  * @brief Command the ability. and Calling information back to Ability.
439  *
440  * @param want The Want object to command to.
441  *
442  * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
443  * destroyed, and the value false indicates a normal startup.
444  *
445  * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by 1
446  * every time the ability is started. For example, if the ability has been started for six times, the value of startId
447  * is 6.
448  */
CommandAbility(const Want & want,bool restart,int startId)449 void AbilityImpl::CommandAbility(const Want &want, bool restart, int startId)
450 {
451     HILOG_INFO("%{public}s begin.", __func__);
452     if (ability_ == nullptr) {
453         HILOG_ERROR("AbilityImpl::CommandAbility ability_ is nullptr");
454         return;
455     }
456 
457     HILOG_INFO("AbilityImpl:: CommandAbility");
458     ability_->OnCommand(want, restart, startId);
459     lifecycleState_ = AAFwk::ABILITY_STATE_ACTIVE;
460     abilityLifecycleCallbacks_->OnAbilityActive(ability_);
461     HILOG_INFO("%{public}s end.", __func__);
462 }
463 
464 /**
465  * @brief Gets the current Ability status.
466  *
467  */
GetCurrentState()468 int AbilityImpl::GetCurrentState()
469 {
470     return lifecycleState_;
471 }
472 
473 #ifdef SUPPORT_GRAPHICS
474 /**
475  * @brief Execution the KeyDown callback of the ability
476  * @param keyEvent Indicates the key-down event.
477  *
478  * @return Returns true if this event is handled and will not be passed further; returns false if this event is
479  * not handled and should be passed to other handlers.
480  *
481  */
DoKeyDown(const std::shared_ptr<MMI::KeyEvent> & keyEvent)482 void AbilityImpl::DoKeyDown(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
483 {
484     HILOG_INFO("AbilityImpl::DoKeyDown called");
485 }
486 
487 /**
488  * @brief Execution the KeyUp callback of the ability
489  * @param keyEvent Indicates the key-up event.
490  *
491  * @return Returns true if this event is handled and will not be passed further; returns false if this event is
492  * not handled and should be passed to other handlers.
493  *
494  */
DoKeyUp(const std::shared_ptr<MMI::KeyEvent> & keyEvent)495 void AbilityImpl::DoKeyUp(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
496 {
497     HILOG_INFO("AbilityImpl::DoKeyUp called");
498 }
499 
500 /**
501  * @brief Called when a touch event is dispatched to this ability. The default implementation of this callback
502  * does nothing and returns false.
503  * @param touchEvent Indicates information about the touch event.
504  *
505  * @return Returns true if the event is handled; returns false otherwise.
506  *
507  */
DoPointerEvent(std::shared_ptr<MMI::PointerEvent> & pointerEvent)508 void AbilityImpl::DoPointerEvent(std::shared_ptr<MMI::PointerEvent>& pointerEvent)
509 {
510     HILOG_INFO("AbilityImpl::DoPointerEvent called");
511 }
512 #endif
513 
514 /**
515  * @brief Send the result code and data to be returned by this Page ability to the caller.
516  * When a Page ability is destroyed, the caller overrides the AbilitySlice#onAbilityResult(int, int, Want) method to
517  * receive the result set in the current method. This method can be called only after the ability has been initialized.
518  *
519  * @param requestCode Indicates the request code.
520  * @param resultCode Indicates the result code returned after the ability is destroyed. You can define the result code
521  * to identify an error.
522  * @param resultData Indicates the data returned after the ability is destroyed. You can define the data returned. This
523  * parameter can be null.
524  */
SendResult(int requestCode,int resultCode,const Want & resultData)525 void AbilityImpl::SendResult(int requestCode, int resultCode, const Want &resultData)
526 {
527     HILOG_INFO("%{public}s begin.", __func__);
528     if (ability_ == nullptr) {
529         HILOG_ERROR("AbilityImpl::SendResult ability_ is nullptr");
530         return;
531     }
532 
533     if (resultData.HasParameter(PERMISSION_KEY)) {
534         std::vector<std::string> permissions = resultData.GetStringArrayParam(PERMISSION_KEY);
535         std::vector<int> grantedResult(permissions.size(), -1);
536         if (resultCode > 0) {
537             grantedResult = resultData.GetIntArrayParam(GRANTED_RESULT_KEY);
538             HILOG_INFO("%{public}s Get user granted result.", __func__);
539         }
540         ability_->OnRequestPermissionsFromUserResult(requestCode, permissions, grantedResult);
541     } else {
542         ability_->OnAbilityResult(requestCode, resultCode, resultData);
543     }
544 
545     // for api5 FeatureAbility::startAbilityForResult
546     ability_->OnFeatureAbilityResult(requestCode, resultCode, resultData);
547     HILOG_INFO("%{public}s end.", __func__);
548 }
549 
550 /**
551  * @brief Called when the launch mode of an ability is set to singleInstance. This happens when you re-launch
552  * an ability that has been at the top of the ability stack.
553  *
554  * @param want  Indicates the new Want containing information about the ability.
555  */
NewWant(const Want & want)556 void AbilityImpl::NewWant(const Want &want)
557 {
558     HILOG_INFO("%{public}s begin.", __func__);
559     if (ability_ == nullptr) {
560         HILOG_ERROR("AbilityImpl::NewWant ability_ is nullptr");
561         return;
562     }
563     ability_->SetWant(want);
564     ability_->OnNewWant(want);
565     HILOG_INFO("%{public}s end.", __func__);
566 }
567 
568 /**
569  * @brief Obtains the MIME types of files supported.
570  *
571  * @param uri Indicates the path of the files to obtain.
572  * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
573  *
574  * @return Returns the matched MIME types. If there is no match, null is returned.
575  */
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)576 std::vector<std::string> AbilityImpl::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
577 {
578     HILOG_INFO("AbilityImpl::GetFileTypes");
579     std::vector<std::string> types;
580     return types;
581 }
582 
583 /**
584  * @brief Opens a file in a specified remote path.
585  *
586  * @param uri Indicates the path of the file to open.
587  * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
588  * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
589  * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
590  *  or "rwt" for read and write access that truncates any existing file.
591  *
592  * @return Returns the file descriptor.
593  */
OpenFile(const Uri & uri,const std::string & mode)594 int AbilityImpl::OpenFile(const Uri &uri, const std::string &mode)
595 {
596     HILOG_INFO("AbilityImpl::OpenFile");
597     return -1;
598 }
599 
600 /**
601  * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
602  * inside of their .hap.
603  *
604  * @param uri Indicates the path of the file to open.
605  * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
606  * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
607  * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
608  * data, or "rwt" for read and write access that truncates any existing file.
609  *
610  * @return Returns the RawFileDescriptor object containing file descriptor.
611  */
OpenRawFile(const Uri & uri,const std::string & mode)612 int AbilityImpl::OpenRawFile(const Uri &uri, const std::string &mode)
613 {
614     HILOG_INFO("AbilityImpl::OpenRawFile");
615     return -1;
616 }
617 
618 /**
619  * @brief Inserts a single data record into the database.
620  *
621  * @param uri Indicates the path of the data to operate.
622  * @param value  Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
623  *
624  * @return Returns the index of the inserted data record.
625  */
Insert(const Uri & uri,const NativeRdb::ValuesBucket & value)626 int AbilityImpl::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value)
627 {
628     HILOG_INFO("AbilityImpl::Insert");
629     return -1;
630 }
631 
Call(const Uri & uri,const std::string & method,const std::string & arg,const AppExecFwk::PacMap & pacMap)632 std::shared_ptr<AppExecFwk::PacMap> AbilityImpl::Call(
633     const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap)
634 {
635     HILOG_INFO("AbilityImpl::Call");
636     return nullptr;
637 }
638 
639 /**
640  * @brief Updates data records in the database.
641  *
642  * @param uri Indicates the path of data to update.
643  * @param value Indicates the data to update. This parameter can be null.
644  * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
645  *
646  * @return Returns the number of data records updated.
647  */
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)648 int AbilityImpl::Update(
649     const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
650 {
651     HILOG_INFO("AbilityImpl::Update");
652     return -1;
653 }
654 
655 /**
656  * @brief Deletes one or more data records from the database.
657  *
658  * @param uri Indicates the path of the data to operate.
659  * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
660  *
661  * @return Returns the number of data records deleted.
662  */
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)663 int AbilityImpl::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
664 {
665     HILOG_INFO("AbilityImpl::Delete");
666     return -1;
667 }
668 
669 /**
670  * @brief Deletes one or more data records from the database.
671  *
672  * @param uri Indicates the path of data to query.
673  * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
674  * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
675  *
676  * @return Returns the query result.
677  */
Query(const Uri & uri,std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)678 std::shared_ptr<NativeRdb::AbsSharedResultSet> AbilityImpl::Query(
679     const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
680 {
681     HILOG_INFO("AbilityImpl::Query");
682     return nullptr;
683 }
684 
685 /**
686  * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be
687  * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG.
688  *
689  * @param uri Indicates the URI of the data.
690  *
691  * @return Returns the MIME type that matches the data specified by uri.
692  */
GetType(const Uri & uri)693 std::string AbilityImpl::GetType(const Uri &uri)
694 {
695     HILOG_INFO("AbilityImpl::GetType");
696     return "";
697 }
698 
699 /**
700  * @brief Reloads data in the database.
701  *
702  * @param uri Indicates the position where the data is to reload. This parameter is mandatory.
703  * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This
704  * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across
705  * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.
706  *
707  * @return Returns true if the data is successfully reloaded; returns false otherwise.
708  */
Reload(const Uri & uri,const PacMap & extras)709 bool AbilityImpl::Reload(const Uri &uri, const PacMap &extras)
710 {
711     return false;
712 }
713 
714 /**
715  * @brief Inserts multiple data records into the database.
716  *
717  * @param uri Indicates the path of the data to operate.
718  * @param values Indicates the data records to insert.
719  *
720  * @return Returns the number of data records inserted.
721  */
BatchInsert(const Uri & uri,const std::vector<NativeRdb::ValuesBucket> & values)722 int AbilityImpl::BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values)
723 {
724     HILOG_INFO("AbilityImpl::BatchInsert");
725     return -1;
726 }
727 
728 /**
729  * @brief SerUriString
730  */
SerUriString(const std::string & uri)731 void AbilityImpl::SerUriString(const std::string &uri)
732 {
733     HILOG_INFO("%{public}s begin.", __func__);
734     if (contextDeal_ == nullptr) {
735         HILOG_ERROR("AbilityImpl::SerUriString contextDeal_ is nullptr");
736         return;
737     }
738     contextDeal_->SerUriString(uri);
739     HILOG_INFO("%{public}s end.", __func__);
740 }
741 
742 /**
743  * @brief Set the LifeCycleStateInfo to the deal.
744  *
745  * @param info the info to set.
746  */
SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo & info)747 void AbilityImpl::SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo &info)
748 {
749     if (contextDeal_ == nullptr) {
750         HILOG_ERROR("AbilityImpl::SetLifeCycleStateInfo contextDeal_ is nullptr");
751         return;
752     }
753     contextDeal_->SetLifeCycleStateInfo(info);
754 }
755 
756 /**
757  * @brief Check if it needs to restore the data to the ability.
758  *
759  * @return Return true if need and success, otherwise return false.
760  */
CheckAndRestore()761 bool AbilityImpl::CheckAndRestore()
762 {
763     HILOG_INFO("AbilityImpl::CheckAndRestore called start");
764     if (!hasSaveData_) {
765         HILOG_ERROR("AbilityImpl::CheckAndRestore hasSaveData_ is false");
766         return false;
767     }
768 
769     if (ability_ == nullptr) {
770         HILOG_ERROR("AbilityImpl::CheckAndRestore ability_ is nullptr");
771         return false;
772     }
773 
774     HILOG_INFO("AbilityImpl::CheckAndRestore ready to restore");
775     ability_->OnRestoreAbilityState(restoreData_);
776 
777     HILOG_INFO("AbilityImpl::CheckAndRestore called end");
778     return true;
779 }
780 
781 /**
782  * @brief Check if it needs to save the data to the ability.
783  *
784  * @return Return true if need and success, otherwise return false.
785  */
CheckAndSave()786 bool AbilityImpl::CheckAndSave()
787 {
788     HILOG_INFO("AbilityImpl::CheckAndSave called start");
789     if (!needSaveDate_) {
790         HILOG_ERROR("AbilityImpl::CheckAndSave needSaveDate_ is false");
791         return false;
792     }
793 
794     if (ability_ == nullptr) {
795         HILOG_ERROR("AbilityImpl::CheckAndSave ability_ is nullptr");
796         return false;
797     }
798 
799     HILOG_INFO("AbilityImpl::CheckAndSave ready to save");
800     ability_->OnSaveAbilityState(restoreData_);
801     abilityLifecycleCallbacks_->OnAbilitySaveState(restoreData_);
802 
803     needSaveDate_ = false;
804 
805     HILOG_INFO("AbilityImpl::CheckAndSave called end");
806     return true;
807 }
808 
GetRestoreData()809 PacMap &AbilityImpl::GetRestoreData()
810 {
811     return restoreData_;
812 }
813 
814 /**
815  * @brief Set deviceId/bundleName/abilityName of the calling ability
816  *
817  * @param deviceId deviceId of the calling ability
818  *
819  * @param deviceId bundleName of the calling ability
820  *
821  * @param deviceId abilityName of the calling ability
822  */
SetCallingContext(const std::string & deviceId,const std::string & bundleName,const std::string & abilityName)823 void AbilityImpl::SetCallingContext(
824     const std::string &deviceId, const std::string &bundleName, const std::string &abilityName)
825 {
826     if (ability_ != nullptr) {
827         ability_->SetCallingContext(deviceId, bundleName, abilityName);
828     }
829 }
830 
831 /**
832  * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
833  * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
834  * context has changed. If you implement URI normalization for a Data ability, you must also implement
835  * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to any
836  * method that is called on the Data ability must require normalization verification and denormalization. The default
837  * implementation of this method returns null, indicating that this Data ability does not support URI normalization.
838  *
839  * @param uri Indicates the Uri object to normalize.
840  *
841  * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
842  */
NormalizeUri(const Uri & uri)843 Uri AbilityImpl::NormalizeUri(const Uri &uri)
844 {
845     HILOG_INFO("AbilityImpl::NormalizeUri");
846     return uri;
847 }
848 
849 /**
850  * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
851  * The default implementation of this method returns the original URI passed to it.
852  *
853  * @param uri uri Indicates the Uri object to denormalize.
854  *
855  * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed to
856  * this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found in the
857  * current environment.
858  */
DenormalizeUri(const Uri & uri)859 Uri AbilityImpl::DenormalizeUri(const Uri &uri)
860 {
861     HILOG_INFO("AbilityImpl::DenormalizeUri");
862     return uri;
863 }
864 
865 /*
866  * @brief ScheduleUpdateConfiguration, scheduling update configuration.
867  */
ScheduleUpdateConfiguration(const Configuration & config)868 void AbilityImpl::ScheduleUpdateConfiguration(const Configuration &config)
869 {
870     HILOG_INFO("%{public}s begin.", __func__);
871     if (ability_ == nullptr) {
872         HILOG_ERROR("AbilityImpl::ScheduleUpdateConfiguration ability_ is nullptr");
873         return;
874     }
875 
876     if (lifecycleState_ != AAFwk::ABILITY_STATE_INITIAL) {
877         HILOG_INFO("ability name: [%{public}s]", ability_->GetAbilityName().c_str());
878         ability_->OnConfigurationUpdatedNotify(config);
879     }
880 
881     HILOG_INFO("%{public}s end.", __func__);
882 }
883 
884 #ifdef SUPPORT_GRAPHICS
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const885 void AbilityImpl::InputEventConsumerImpl::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
886 {
887     int32_t code = keyEvent->GetKeyAction();
888     if (code == MMI::KeyEvent::KEY_ACTION_DOWN) {
889         abilityImpl_->DoKeyDown(keyEvent);
890         HILOG_INFO("AbilityImpl::OnKeyDown keyAction: %{public}d.", code);
891     } else if (code == MMI::KeyEvent::KEY_ACTION_UP) {
892         abilityImpl_->DoKeyUp(keyEvent);
893         HILOG_INFO("AbilityImpl::DoKeyUp keyAction: %{public}d.", code);
894     }
895 }
896 
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const897 void AbilityImpl::InputEventConsumerImpl::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
898 {
899     HILOG_INFO("AbilityImpl::DoPointerEvent called.");
900     abilityImpl_->DoPointerEvent(pointerEvent);
901 }
902 
903 /**
904  * @brief Multimodal Events Register.
905  */
WindowEventRegister()906 void AbilityImpl::WindowEventRegister()
907 {
908     HILOG_INFO("%{public}s called.", __func__);
909     if (!ability_->GetAbilityInfo()->isStageBasedModel) {
910         auto window = ability_->GetWindow();
911         if (window) {
912             std::shared_ptr<MMI::IInputEventConsumer> inputEventListener =
913                 std::make_shared<AbilityImpl::InputEventConsumerImpl>(shared_from_this());
914             window->AddInputEventListener(inputEventListener);
915         }
916     }
917 }
918 #endif
919 
920 /**
921  * @brief Create a PostEvent timeout task. The default delay is 5000ms
922  *
923  * @return Return a smart pointer to a timeout object
924  */
CreatePostEventTimeouter(std::string taskstr)925 std::shared_ptr<AbilityPostEventTimeout> AbilityImpl::CreatePostEventTimeouter(std::string taskstr)
926 {
927     if (ability_ == nullptr) {
928         HILOG_ERROR("AbilityImpl::CreatePostEventTimeouter ability_ is nullptr");
929         return nullptr;
930     }
931 
932     return ability_->CreatePostEventTimeouter(taskstr);
933 }
934 
ExecuteBatch(const std::vector<std::shared_ptr<DataAbilityOperation>> & operations)935 std::vector<std::shared_ptr<DataAbilityResult>> AbilityImpl::ExecuteBatch(
936     const std::vector<std::shared_ptr<DataAbilityOperation>> &operations)
937 {
938     HILOG_INFO("AbilityImpl::ExecuteBatch");
939     std::vector<std::shared_ptr<DataAbilityResult>> results;
940     return results;
941 }
942 
ContinueAbility(const std::string & deviceId)943 void AbilityImpl::ContinueAbility(const std::string& deviceId)
944 {
945     if (ability_ == nullptr) {
946         HILOG_ERROR("AbilityImpl::ContinueAbility ability_ is nullptr");
947         return;
948     }
949     ability_->ContinueAbilityWithStack(deviceId);
950 }
951 
NotifyContinuationResult(int32_t result)952 void AbilityImpl::NotifyContinuationResult(int32_t result)
953 {
954     if (ability_ == nullptr) {
955         HILOG_ERROR("AbilityImpl::NotifyContinuationResult ability_ is nullptr");
956         return;
957     }
958     ability_->OnCompleteContinuation(result);
959 }
960 
SetUseNewMission(bool useNewMission)961 void AbilityImpl::SetUseNewMission(bool useNewMission)
962 {
963     if (!g_isMissionFlagSetted) {
964         g_isMissionFlagSetted = true;
965         g_useNewMission = useNewMission;
966     }
967 }
968 
IsUseNewMission()969 bool AbilityImpl::IsUseNewMission()
970 {
971     return g_useNewMission;
972 }
973 }  // namespace AppExecFwk
974 }  // namespace OHOS
975