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 "service_ability_impl.h"
17 #include "app_log_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 ServiceAbilityImpl::HandleAbilityTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState)
30 {
31 APP_LOGI("ServiceAbilityImpl::HandleAbilityTransaction begin sourceState:%{public}d; targetState: %{public}d; "
32 "isNewWant: %{public}d",
33 lifecycleState_,
34 targetState.state,
35 targetState.isNewWant);
36 if (lifecycleState_ == targetState.state) {
37 APP_LOGE("Org lifeCycleState equals to Dst lifeCycleState.");
38 return;
39 }
40
41 bool ret = true;
42
43 switch (targetState.state) {
44 case AAFwk::ABILITY_STATE_INITIAL: {
45 if (lifecycleState_ == AAFwk::ABILITY_STATE_ACTIVE) {
46 Background();
47 Stop();
48 }
49 break;
50 }
51 case AAFwk::ABILITY_STATE_INACTIVE: {
52 if (lifecycleState_ == AAFwk::ABILITY_STATE_INITIAL) {
53 SerUriString(targetState.caller.deviceId + "/" + targetState.caller.bundleName + "/" +
54 targetState.caller.abilityName);
55 Start(want);
56 }
57 break;
58 }
59 default: {
60 ret = false;
61 APP_LOGE("ServiceAbilityImpl::HandleAbilityTransaction state is error");
62 break;
63 }
64 }
65
66 if (ret) {
67 APP_LOGI("ServiceAbilityImpl::HandleAbilityTransaction before AbilityManagerClient->AbilityTransitionDone");
68 AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_, targetState.state);
69 APP_LOGI("ServiceAbilityImpl::HandleAbilityTransaction after AbilityManagerClient->AbilityTransitionDone");
70 }
71 APP_LOGI("ServiceAbilityImpl::HandleAbilityTransaction end");
72 }
73 } // namespace AppExecFwk
74 } // namespace OHOS
75