1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#include "lifecycle_deal.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include "ability_record.h"
19eace7efcSopenharmony_ci#include "ability_util.h"
20eace7efcSopenharmony_ci
21eace7efcSopenharmony_cinamespace OHOS {
22eace7efcSopenharmony_cinamespace AAFwk {
23eace7efcSopenharmony_ciLifecycleDeal::LifecycleDeal()
24eace7efcSopenharmony_ci{}
25eace7efcSopenharmony_ci
26eace7efcSopenharmony_ciLifecycleDeal::~LifecycleDeal()
27eace7efcSopenharmony_ci{}
28eace7efcSopenharmony_ci
29eace7efcSopenharmony_civoid LifecycleDeal::SetScheduler(const sptr<IAbilityScheduler> &scheduler)
30eace7efcSopenharmony_ci{
31eace7efcSopenharmony_ci    std::unique_lock<std::shared_mutex> lock(schedulerMutex_);
32eace7efcSopenharmony_ci    abilityScheduler_ = scheduler;
33eace7efcSopenharmony_ci}
34eace7efcSopenharmony_ci
35eace7efcSopenharmony_cisptr<IAbilityScheduler> LifecycleDeal::GetScheduler()
36eace7efcSopenharmony_ci{
37eace7efcSopenharmony_ci    std::shared_lock<std::shared_mutex> lock(schedulerMutex_);
38eace7efcSopenharmony_ci    return abilityScheduler_;
39eace7efcSopenharmony_ci}
40eace7efcSopenharmony_ci
41eace7efcSopenharmony_civoid LifecycleDeal::Activate(const Want &want, LifeCycleStateInfo &stateInfo)
42eace7efcSopenharmony_ci{
43eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
44eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
45eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
46eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "caller %{public}s, %{public}s",
47eace7efcSopenharmony_ci        stateInfo.caller.bundleName.c_str(),
48eace7efcSopenharmony_ci        stateInfo.caller.abilityName.c_str());
49eace7efcSopenharmony_ci    stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_ACTIVE;
50eace7efcSopenharmony_ci    abilityScheduler->ScheduleAbilityTransaction(want, stateInfo);
51eace7efcSopenharmony_ci}
52eace7efcSopenharmony_ci
53eace7efcSopenharmony_civoid LifecycleDeal::Inactivate(const Want &want, LifeCycleStateInfo &stateInfo,
54eace7efcSopenharmony_ci    sptr<SessionInfo> sessionInfo)
55eace7efcSopenharmony_ci{
56eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
57eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
58eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
59eace7efcSopenharmony_ci    stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_INACTIVE;
60eace7efcSopenharmony_ci    abilityScheduler->ScheduleAbilityTransaction(want, stateInfo, sessionInfo);
61eace7efcSopenharmony_ci}
62eace7efcSopenharmony_ci
63eace7efcSopenharmony_civoid LifecycleDeal::MoveToBackground(const Want &want, LifeCycleStateInfo &stateInfo)
64eace7efcSopenharmony_ci{
65eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
66eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
67eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
68eace7efcSopenharmony_ci    stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_BACKGROUND;
69eace7efcSopenharmony_ci    abilityScheduler->ScheduleAbilityTransaction(want, stateInfo);
70eace7efcSopenharmony_ci}
71eace7efcSopenharmony_ci
72eace7efcSopenharmony_civoid LifecycleDeal::ConnectAbility(const Want &want)
73eace7efcSopenharmony_ci{
74eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
75eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
76eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
77eace7efcSopenharmony_ci    abilityScheduler->ScheduleConnectAbility(want);
78eace7efcSopenharmony_ci}
79eace7efcSopenharmony_ci
80eace7efcSopenharmony_civoid LifecycleDeal::DisconnectAbility(const Want &want)
81eace7efcSopenharmony_ci{
82eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
83eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
84eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
85eace7efcSopenharmony_ci    abilityScheduler->ScheduleDisconnectAbility(want);
86eace7efcSopenharmony_ci}
87eace7efcSopenharmony_ci
88eace7efcSopenharmony_civoid LifecycleDeal::Terminate(const Want &want, LifeCycleStateInfo &stateInfo, sptr<SessionInfo> sessionInfo)
89eace7efcSopenharmony_ci{
90eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
91eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
92eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
93eace7efcSopenharmony_ci    stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_INITIAL;
94eace7efcSopenharmony_ci    abilityScheduler->ScheduleAbilityTransaction(want, stateInfo, sessionInfo);
95eace7efcSopenharmony_ci}
96eace7efcSopenharmony_ci
97eace7efcSopenharmony_civoid LifecycleDeal::CommandAbility(const Want &want, bool reStart, int startId)
98eace7efcSopenharmony_ci{
99eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "startId:%{public}d", startId);
100eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
101eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
102eace7efcSopenharmony_ci    abilityScheduler->ScheduleCommandAbility(want, reStart, startId);
103eace7efcSopenharmony_ci}
104eace7efcSopenharmony_ci
105eace7efcSopenharmony_civoid LifecycleDeal::CommandAbilityWindow(const Want &want, const sptr<SessionInfo> &sessionInfo, WindowCommand winCmd)
106eace7efcSopenharmony_ci{
107eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
108eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
109eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
110eace7efcSopenharmony_ci    abilityScheduler->ScheduleCommandAbilityWindow(want, sessionInfo, winCmd);
111eace7efcSopenharmony_ci}
112eace7efcSopenharmony_ci
113eace7efcSopenharmony_civoid LifecycleDeal::SaveAbilityState()
114eace7efcSopenharmony_ci{
115eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
116eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
117eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
118eace7efcSopenharmony_ci    abilityScheduler->ScheduleSaveAbilityState();
119eace7efcSopenharmony_ci}
120eace7efcSopenharmony_ci
121eace7efcSopenharmony_civoid LifecycleDeal::RestoreAbilityState(const PacMap &inState)
122eace7efcSopenharmony_ci{
123eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
124eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
125eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
126eace7efcSopenharmony_ci    abilityScheduler->ScheduleRestoreAbilityState(inState);
127eace7efcSopenharmony_ci}
128eace7efcSopenharmony_ci
129eace7efcSopenharmony_cibool LifecycleDeal::ForegroundNew(const Want &want, LifeCycleStateInfo &stateInfo,
130eace7efcSopenharmony_ci    sptr<SessionInfo> sessionInfo)
131eace7efcSopenharmony_ci{
132eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
133eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
134eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(abilityScheduler, false);
135eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "caller %{public}s, %{public}s",
136eace7efcSopenharmony_ci        stateInfo.caller.bundleName.c_str(),
137eace7efcSopenharmony_ci        stateInfo.caller.abilityName.c_str());
138eace7efcSopenharmony_ci    stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_FOREGROUND_NEW;
139eace7efcSopenharmony_ci    return abilityScheduler->ScheduleAbilityTransaction(want, stateInfo, sessionInfo);
140eace7efcSopenharmony_ci}
141eace7efcSopenharmony_ci
142eace7efcSopenharmony_civoid LifecycleDeal::BackgroundNew(const Want &want, LifeCycleStateInfo &stateInfo,
143eace7efcSopenharmony_ci    sptr<SessionInfo> sessionInfo)
144eace7efcSopenharmony_ci{
145eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
146eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
147eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
148eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "caller %{public}s, %{public}s",
149eace7efcSopenharmony_ci        stateInfo.caller.bundleName.c_str(),
150eace7efcSopenharmony_ci        stateInfo.caller.abilityName.c_str());
151eace7efcSopenharmony_ci    stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_BACKGROUND_NEW;
152eace7efcSopenharmony_ci    abilityScheduler->ScheduleAbilityTransaction(want, stateInfo, sessionInfo);
153eace7efcSopenharmony_ci}
154eace7efcSopenharmony_ci
155eace7efcSopenharmony_civoid LifecycleDeal::ContinueAbility(const std::string& deviceId, uint32_t versionCode)
156eace7efcSopenharmony_ci{
157eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
158eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler_);
159eace7efcSopenharmony_ci    abilityScheduler_->ContinueAbility(deviceId, versionCode);
160eace7efcSopenharmony_ci}
161eace7efcSopenharmony_ci
162eace7efcSopenharmony_civoid LifecycleDeal::NotifyContinuationResult(int32_t result)
163eace7efcSopenharmony_ci{
164eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
165eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
166eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
167eace7efcSopenharmony_ci    abilityScheduler->NotifyContinuationResult(result);
168eace7efcSopenharmony_ci}
169eace7efcSopenharmony_ci
170eace7efcSopenharmony_civoid LifecycleDeal::ShareData(const int32_t &uniqueId)
171eace7efcSopenharmony_ci{
172eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "uniqueId is %{public}d.", uniqueId);
173eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
174eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
175eace7efcSopenharmony_ci    abilityScheduler->ScheduleShareData(uniqueId);
176eace7efcSopenharmony_ci}
177eace7efcSopenharmony_ci
178eace7efcSopenharmony_cibool LifecycleDeal::PrepareTerminateAbility()
179eace7efcSopenharmony_ci{
180eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "call");
181eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
182eace7efcSopenharmony_ci    if (abilityScheduler == nullptr) {
183eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "null abilityScheduler");
184eace7efcSopenharmony_ci        return false;
185eace7efcSopenharmony_ci    }
186eace7efcSopenharmony_ci    return abilityScheduler->SchedulePrepareTerminateAbility();
187eace7efcSopenharmony_ci}
188eace7efcSopenharmony_ci
189eace7efcSopenharmony_civoid LifecycleDeal::UpdateSessionToken(sptr<IRemoteObject> sessionToken)
190eace7efcSopenharmony_ci{
191eace7efcSopenharmony_ci    auto abilityScheduler = GetScheduler();
192eace7efcSopenharmony_ci    CHECK_POINTER(abilityScheduler);
193eace7efcSopenharmony_ci    abilityScheduler->UpdateSessionToken(sessionToken);
194eace7efcSopenharmony_ci}
195eace7efcSopenharmony_ci}  // namespace AAFwk
196eace7efcSopenharmony_ci}  // namespace OHOS
197