• 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 "page_ability_impl.h"
17 #include "hilog_wrapper.h"
18 
19 namespace OHOS {
20 namespace AppExecFwk {
21 using AbilityManagerClient = OHOS::AAFwk::AbilityManagerClient;
22 /**
23  * @brief Handling the life cycle switching of PageAbility.
24  *
25  * @param want Indicates the structure containing information about the ability.
26  * @param targetState The life cycle state to switch to.
27  *
28  */
HandleAbilityTransaction(const Want & want,const AAFwk::LifeCycleStateInfo & targetState)29 void PageAbilityImpl::HandleAbilityTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState)
30 {
31     AbilityImpl::SetUseNewMission(targetState.useNewMission);
32     HILOG_INFO("PageAbilityImpl::HandleAbilityTransaction begin sourceState:%{public}d; targetState: %{public}d; "
33              "isNewWant: %{public}d, sceneFlag: %{public}d",
34         lifecycleState_,
35         targetState.state,
36         targetState.isNewWant,
37         targetState.sceneFlag);
38     if ((lifecycleState_ == targetState.state) && !targetState.isNewWant) {
39         if (ability_ != nullptr && targetState.state == AAFwk::ABILITY_STATE_FOREGROUND_NEW) {
40             ability_->RequsetFocus(want);
41             AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_, targetState.state, GetRestoreData());
42         }
43         HILOG_ERROR("Org lifeCycleState equals to Dst lifeCycleState.");
44         return;
45     }
46 
47     if (lifecycleState_ == AAFwk::ABILITY_STATE_BACKGROUND ||
48         lifecycleState_ == AAFwk::ABILITY_STATE_BACKGROUND_NEW) {
49         if (targetState.state == AAFwk::ABILITY_STATE_ACTIVE ||
50             targetState.state == AAFwk::ABILITY_STATE_INACTIVE) {
51             HILOG_ERROR("Invalid state.");
52             return;
53         }
54     }
55 
56     SetLifeCycleStateInfo(targetState);
57 
58     if (ability_ && lifecycleState_ == AAFwk::ABILITY_STATE_INITIAL) {
59         ability_->SetStartAbilitySetting(targetState.setting);
60         Start(want);
61         CheckAndRestore();
62     }
63 
64     if (lifecycleState_ == AAFwk::ABILITY_STATE_ACTIVE &&
65         targetState.state != AAFwk::ABILITY_STATE_FOREGROUND_NEW) {
66         Inactive();
67     }
68 
69     if (targetState.state == AAFwk::ABILITY_STATE_BACKGROUND_NEW ||
70         targetState.state == AAFwk::ABILITY_STATE_BACKGROUND) {
71         CheckAndSave();
72     }
73 
74     bool ret = false;
75     if (ability_ != nullptr) {
76         ability_->sceneFlag_ = targetState.sceneFlag;
77     }
78     if (AbilityImpl::IsUseNewMission()) {
79         ret = AbilityTransactionNew(want, targetState);
80     } else {
81         ret = AbilityTransaction(want, targetState);
82     }
83     if (ret) {
84         HILOG_INFO("AbilityThread::HandleAbilityTransaction before AbilityManagerClient->AbilityTransitionDone");
85         AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_, targetState.state, GetRestoreData());
86         HILOG_INFO("AbilityThread::HandleAbilityTransaction after AbilityManagerClient->AbilityTransitionDone");
87     }
88     HILOG_INFO("PageAbilityImpl::HandleAbilityTransaction end");
89 }
90 
91 /**
92  * @brief Handling the life cycle switching of PageAbility in switch.
93  *
94  * @param want Indicates the structure containing information about the ability.
95  * @param targetState The life cycle state to switch to.
96  *
97  * @return return true if the lifecycle transaction successfully, otherwise return false.
98  *
99  */
AbilityTransaction(const Want & want,const AAFwk::LifeCycleStateInfo & targetState)100 bool PageAbilityImpl::AbilityTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState)
101 {
102     HILOG_INFO("PageAbilityImpl::AbilityTransaction begin");
103     bool ret = true;
104     switch (targetState.state) {
105         case AAFwk::ABILITY_STATE_INITIAL: {
106             if (lifecycleState_ == AAFwk::ABILITY_STATE_INACTIVE) {
107                 Background();
108             }
109             Stop();
110             break;
111         }
112         case AAFwk::ABILITY_STATE_INACTIVE: {
113             if (lifecycleState_ == AAFwk::ABILITY_STATE_BACKGROUND) {
114                 Foreground(want);
115             }
116             break;
117         }
118         case AAFwk::ABILITY_STATE_ACTIVE: {
119             if (lifecycleState_ == AAFwk::ABILITY_STATE_BACKGROUND) {
120                 Foreground(want);
121             }
122             if (targetState.isNewWant) {
123                 NewWant(want);
124             }
125             SerUriString(targetState.caller.deviceId + "/" + targetState.caller.bundleName + "/" +
126                          targetState.caller.abilityName);
127             Active();
128             break;
129         }
130         case AAFwk::ABILITY_STATE_BACKGROUND: {
131             if (lifecycleState_ == AAFwk::ABILITY_STATE_INACTIVE) {
132                 Background();
133             }
134             break;
135         }
136         default: {
137             ret = false;
138             HILOG_ERROR("PageAbilityImpl::HandleAbilityTransaction state error");
139             break;
140         }
141     }
142     HILOG_INFO("PageAbilityImpl::AbilityTransaction end: retVal = %{public}d", (int)ret);
143     return ret;
144 }
145 
146 /**
147  * @brief Handling the life cycle switching of PageAbility in switch.
148  *
149  * @param want Indicates the structure containing information about the ability.
150  * @param targetState The life cycle state to switch to.
151  *
152  * @return return true if the lifecycle transaction successfully, otherwise return false.
153  *
154  */
AbilityTransactionNew(const Want & want,const AAFwk::LifeCycleStateInfo & targetState)155 bool PageAbilityImpl::AbilityTransactionNew(const Want &want, const AAFwk::LifeCycleStateInfo &targetState)
156 {
157     HILOG_INFO("PageAbilityImpl::AbilityTransaction begin");
158     bool ret = true;
159     switch (targetState.state) {
160         case AAFwk::ABILITY_STATE_INITIAL: {
161             if (lifecycleState_ == AAFwk::ABILITY_STATE_INACTIVE) {
162                 Background();
163             }
164             Stop();
165             break;
166         }
167         case AAFwk::ABILITY_STATE_INACTIVE: {
168             if (lifecycleState_ == AAFwk::ABILITY_STATE_ACTIVE) {
169                 Inactive();
170             }
171             ret = false;
172             break;
173         }
174         case AAFwk::ABILITY_STATE_FOREGROUND_NEW: {
175             if (lifecycleState_ == AAFwk::ABILITY_STATE_BACKGROUND_NEW ||
176                 lifecycleState_ == AAFwk::ABILITY_STATE_BACKGROUND) {
177                 Foreground(want);
178             }
179             if (targetState.isNewWant) {
180                 NewWant(want);
181             }
182             SerUriString(targetState.caller.deviceId + "/" + targetState.caller.bundleName + "/" +
183                          targetState.caller.abilityName);
184             if (ability_) {
185                 ability_->RequsetFocus(want);
186             }
187             break;
188         }
189         case AAFwk::ABILITY_STATE_ACTIVE: {
190             if (lifecycleState_ == AAFwk::ABILITY_STATE_BACKGROUND) {
191                 Foreground(want);
192             }
193             Active();
194             ret = false;
195             break;
196         }
197         case AAFwk::ABILITY_STATE_BACKGROUND_NEW: {
198             if (lifecycleState_ != AAFwk::ABILITY_STATE_INACTIVE) {
199                 Inactive();
200             }
201             Background();
202             break;
203         }
204         default: {
205             ret = false;
206             HILOG_ERROR("PageAbilityImpl::HandleAbilityTransaction state error");
207             break;
208         }
209     }
210     HILOG_INFO("PageAbilityImpl::AbilityTransaction end: retVal = %{public}d", (int)ret);
211     return ret;
212 }
213 
214 /**
215  * @brief Execution the KeyDown callback of the ability
216  * @param keyEvent Indicates the key-down event.
217  *
218  * @return Returns true if this event is handled and will not be passed further; returns false if this event is
219  * not handled and should be passed to other handlers.
220  *
221  */
DoKeyDown(const std::shared_ptr<MMI::KeyEvent> & keyEvent)222 void PageAbilityImpl::DoKeyDown(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
223 {
224     HILOG_INFO("PageAbilityImpl::DoKeyDown begin");
225     if (ability_ == nullptr) {
226         HILOG_ERROR("PageAbilityImpl::DoKeyDown ability_ == nullptr");
227         return;
228     }
229     auto abilitInfo = ability_->GetAbilityInfo();
230     HILOG_INFO("PageAbilityImpl::DoKeyDown called %{public}s And Focus is %{public}s",
231         abilitInfo->name.c_str(),
232         ability_->HasWindowFocus() ? "true" : "false");
233 
234     ability_->OnKeyDown(keyEvent);
235     HILOG_INFO("PageAbilityImpl::DoKeyDown end");
236 }
237 
238 /**
239  * @brief Execution the KeyUp callback of the ability
240  * @param keyEvent Indicates the key-up event.
241  *
242  * @return Returns true if this event is handled and will not be passed further; returns false if this event is
243  * not handled and should be passed to other handlers.
244  *
245  */
DoKeyUp(const std::shared_ptr<MMI::KeyEvent> & keyEvent)246 void PageAbilityImpl::DoKeyUp(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
247 {
248     HILOG_INFO("PageAbilityImpl::DoKeyUp begin");
249     if (ability_ == nullptr) {
250         HILOG_ERROR("PageAbilityImpl::DoKeyUp ability_ == nullptr");
251         return;
252     }
253     auto abilitInfo = ability_->GetAbilityInfo();
254     HILOG_INFO("PageAbilityImpl::DoKeyUp called %{public}s And Focus is %{public}s",
255         abilitInfo->name.c_str(),
256         ability_->HasWindowFocus() ? "true" : "false");
257 
258     ability_->OnKeyUp(keyEvent);
259     HILOG_INFO("PageAbilityImpl::DoKeyUp end");
260 }
261 
262 /**
263  * @brief Called when a touch event is dispatched to this ability. The default implementation of this callback
264  * does nothing and returns false.
265  * @param touchEvent Indicates information about the touch event.
266  *
267  * @return Returns true if the event is handled; returns false otherwise.
268  *
269  */
DoPointerEvent(std::shared_ptr<MMI::PointerEvent> & pointerEvent)270 void PageAbilityImpl::DoPointerEvent(std::shared_ptr<MMI::PointerEvent>& pointerEvent)
271 {
272     HILOG_INFO("PageAbilityImpl::DoPointerEvent begin");
273     if (ability_ == nullptr) {
274         HILOG_ERROR("PageAbilityImpl::DoPointerEvent ability_ == nullptr");
275         return;
276     }
277     auto abilitInfo = ability_->GetAbilityInfo();
278     HILOG_INFO("PageAbilityImpl::DoPointerEvent called %{public}s And Focus is %{public}s",
279         abilitInfo->name.c_str(),
280         ability_->HasWindowFocus() ? "true" : "false");
281 
282     ability_->OnPointerEvent(pointerEvent);
283     HILOG_INFO("PageAbilityImpl::DoPointerEvent end");
284 }
285 }  // namespace AppExecFwk
286 }  // namespace OHOS