• 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 "lifecycle_deal.h"
17 
18 #include "ability_record.h"
19 #include "ability_util.h"
20 #include "hilog_wrapper.h"
21 
22 namespace OHOS {
23 namespace AAFwk {
LifecycleDeal()24 LifecycleDeal::LifecycleDeal()
25 {}
26 
~LifecycleDeal()27 LifecycleDeal::~LifecycleDeal()
28 {}
29 
SetScheduler(const sptr<IAbilityScheduler> & scheduler)30 void LifecycleDeal::SetScheduler(const sptr<IAbilityScheduler> &scheduler)
31 {
32     std::unique_lock<std::shared_mutex> lock(schedulerMutex_);
33     abilityScheduler_ = scheduler;
34 }
35 
GetScheduler()36 sptr<IAbilityScheduler> LifecycleDeal::GetScheduler()
37 {
38     std::shared_lock<std::shared_mutex> lock(schedulerMutex_);
39     return abilityScheduler_;
40 }
41 
Activate(const Want & want,LifeCycleStateInfo & stateInfo)42 void LifecycleDeal::Activate(const Want &want, LifeCycleStateInfo &stateInfo)
43 {
44     HILOG_INFO("call");
45     auto abilityScheduler = GetScheduler();
46     CHECK_POINTER(abilityScheduler);
47     HILOG_DEBUG("caller %{public}s, %{public}s",
48         stateInfo.caller.bundleName.c_str(),
49         stateInfo.caller.abilityName.c_str());
50     stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_ACTIVE;
51     abilityScheduler->ScheduleAbilityTransaction(want, stateInfo);
52 }
53 
Inactivate(const Want & want,LifeCycleStateInfo & stateInfo,sptr<SessionInfo> sessionInfo)54 void LifecycleDeal::Inactivate(const Want &want, LifeCycleStateInfo &stateInfo,
55     sptr<SessionInfo> sessionInfo)
56 {
57     HILOG_INFO("call");
58     auto abilityScheduler = GetScheduler();
59     CHECK_POINTER(abilityScheduler);
60     stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_INACTIVE;
61     abilityScheduler->ScheduleAbilityTransaction(want, stateInfo, sessionInfo);
62 }
63 
MoveToBackground(const Want & want,LifeCycleStateInfo & stateInfo)64 void LifecycleDeal::MoveToBackground(const Want &want, LifeCycleStateInfo &stateInfo)
65 {
66     HILOG_INFO("call");
67     auto abilityScheduler = GetScheduler();
68     CHECK_POINTER(abilityScheduler);
69     stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_BACKGROUND;
70     abilityScheduler->ScheduleAbilityTransaction(want, stateInfo);
71 }
72 
ConnectAbility(const Want & want)73 void LifecycleDeal::ConnectAbility(const Want &want)
74 {
75     HILOG_INFO("call");
76     auto abilityScheduler = GetScheduler();
77     CHECK_POINTER(abilityScheduler);
78     abilityScheduler->ScheduleConnectAbility(want);
79 }
80 
DisconnectAbility(const Want & want)81 void LifecycleDeal::DisconnectAbility(const Want &want)
82 {
83     HILOG_INFO("call");
84     auto abilityScheduler = GetScheduler();
85     CHECK_POINTER(abilityScheduler);
86     abilityScheduler->ScheduleDisconnectAbility(want);
87 }
88 
Terminate(const Want & want,LifeCycleStateInfo & stateInfo)89 void LifecycleDeal::Terminate(const Want &want, LifeCycleStateInfo &stateInfo)
90 {
91     HILOG_INFO("call");
92     auto abilityScheduler = GetScheduler();
93     CHECK_POINTER(abilityScheduler);
94     stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_INITIAL;
95     abilityScheduler->ScheduleAbilityTransaction(want, stateInfo);
96 }
97 
CommandAbility(const Want & want,bool reStart,int startId)98 void LifecycleDeal::CommandAbility(const Want &want, bool reStart, int startId)
99 {
100     HILOG_INFO("startId:%{public}d", startId);
101     auto abilityScheduler = GetScheduler();
102     CHECK_POINTER(abilityScheduler);
103     abilityScheduler->ScheduleCommandAbility(want, reStart, startId);
104 }
105 
CommandAbilityWindow(const Want & want,const sptr<SessionInfo> & sessionInfo,WindowCommand winCmd)106 void LifecycleDeal::CommandAbilityWindow(const Want &want, const sptr<SessionInfo> &sessionInfo, WindowCommand winCmd)
107 {
108     HILOG_INFO("call");
109     auto abilityScheduler = GetScheduler();
110     CHECK_POINTER(abilityScheduler);
111     abilityScheduler->ScheduleCommandAbilityWindow(want, sessionInfo, winCmd);
112 }
113 
SaveAbilityState()114 void LifecycleDeal::SaveAbilityState()
115 {
116     HILOG_INFO("call");
117     auto abilityScheduler = GetScheduler();
118     CHECK_POINTER(abilityScheduler);
119     abilityScheduler->ScheduleSaveAbilityState();
120 }
121 
RestoreAbilityState(const PacMap & inState)122 void LifecycleDeal::RestoreAbilityState(const PacMap &inState)
123 {
124     HILOG_INFO("call");
125     auto abilityScheduler = GetScheduler();
126     CHECK_POINTER(abilityScheduler);
127     abilityScheduler->ScheduleRestoreAbilityState(inState);
128 }
129 
ForegroundNew(const Want & want,LifeCycleStateInfo & stateInfo,sptr<SessionInfo> sessionInfo)130 void LifecycleDeal::ForegroundNew(const Want &want, LifeCycleStateInfo &stateInfo,
131     sptr<SessionInfo> sessionInfo)
132 {
133     HILOG_INFO("call");
134     auto abilityScheduler = GetScheduler();
135     CHECK_POINTER(abilityScheduler);
136     HILOG_DEBUG("caller %{public}s, %{public}s",
137         stateInfo.caller.bundleName.c_str(),
138         stateInfo.caller.abilityName.c_str());
139     stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_FOREGROUND_NEW;
140     abilityScheduler->ScheduleAbilityTransaction(want, stateInfo, sessionInfo);
141 }
142 
BackgroundNew(const Want & want,LifeCycleStateInfo & stateInfo,sptr<SessionInfo> sessionInfo)143 void LifecycleDeal::BackgroundNew(const Want &want, LifeCycleStateInfo &stateInfo,
144     sptr<SessionInfo> sessionInfo)
145 {
146     HILOG_INFO("call");
147     auto abilityScheduler = GetScheduler();
148     CHECK_POINTER(abilityScheduler);
149     HILOG_DEBUG("caller %{public}s, %{public}s",
150         stateInfo.caller.bundleName.c_str(),
151         stateInfo.caller.abilityName.c_str());
152     stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_BACKGROUND_NEW;
153     abilityScheduler->ScheduleAbilityTransaction(want, stateInfo, sessionInfo);
154 }
155 
ContinueAbility(const std::string & deviceId,uint32_t versionCode)156 void LifecycleDeal::ContinueAbility(const std::string& deviceId, uint32_t versionCode)
157 {
158     HILOG_INFO("call");
159     CHECK_POINTER(abilityScheduler_);
160     abilityScheduler_->ContinueAbility(deviceId, versionCode);
161 }
162 
NotifyContinuationResult(int32_t result)163 void LifecycleDeal::NotifyContinuationResult(int32_t result)
164 {
165     HILOG_INFO("call");
166     auto abilityScheduler = GetScheduler();
167     CHECK_POINTER(abilityScheduler);
168     abilityScheduler->NotifyContinuationResult(result);
169 }
170 
ShareData(const int32_t & uniqueId)171 void LifecycleDeal::ShareData(const int32_t &uniqueId)
172 {
173     HILOG_INFO("uniqueId is %{public}d.", uniqueId);
174     auto abilityScheduler = GetScheduler();
175     CHECK_POINTER(abilityScheduler);
176     abilityScheduler->ScheduleShareData(uniqueId);
177 }
178 
PrepareTerminateAbility()179 bool LifecycleDeal::PrepareTerminateAbility()
180 {
181     HILOG_DEBUG("call");
182     auto abilityScheduler = GetScheduler();
183     if (abilityScheduler == nullptr) {
184         HILOG_ERROR("abilityScheduler is nullptr.");
185         return false;
186     }
187     return abilityScheduler->SchedulePrepareTerminateAbility();
188 }
189 }  // namespace AAFwk
190 }  // namespace OHOS
191