1 /*
2  * Copyright (c) 2020 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 "app_restart_task.h"
17 
18 #include "ability_stack_manager.h"
19 #include "app_manager.h"
20 #include "util/abilityms_helper.h"
21 
22 namespace OHOS {
AppRestartTask(AbilityMgrContext * context,const BundleInfo * bundleInfo)23 AppRestartTask::AppRestartTask(AbilityMgrContext *context, const BundleInfo *bundleInfo)
24     : AbilityTask(context), bundleInfo_(bundleInfo)
25 {
26 }
27 
Execute()28 AbilityMsStatus AppRestartTask::Execute()
29 {
30     PRINTD("AppRestartTask", "start");
31     if (abilityMgrContext_ == nullptr || bundleInfo_ == nullptr || bundleInfo_->bundleName == nullptr) {
32         return AbilityMsStatus::TaskStatus("app terminate", "invalid argument");
33     }
34 
35     // Step1: Clear app MissionStack and AbilityRecord
36     AbilityStackManager &stackManager = AbilityStackManager::GetInstance();
37     stackManager.ClearAbilityStack(bundleInfo_->bundleName, *abilityMgrContext_);
38 
39     // Step2: Remove app record
40     AppManager::GetInstance().RemoveAppRecord(bundleInfo_->bundleName);
41 
42     // Step3: Get top page ability
43     auto topRecord = const_cast<PageAbilityRecord *>(stackManager.GetTopPageAbility(*abilityMgrContext_));
44     if (topRecord == nullptr) {
45         return AbilityMsStatus::NoActiveAbilityStatus("restart app", "start launcher");
46     } else {
47         // Step4: Active top ability
48         topRecord->ActiveAbility();
49     }
50     return AbilityMsStatus::Ok();
51 }
52 }  // namespace OHOS
53