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 "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 PageAbilityImpl::HandleAbilityTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState)
30 {
31 APP_LOGI("PageAbilityImpl::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) && !targetState.isNewWant) {
37 APP_LOGE("Org lifeCycleState equals to Dst lifeCycleState.");
38 return;
39 }
40
41 SetLifeCycleStateInfo(targetState);
42
43 if (lifecycleState_ == AAFwk::ABILITY_STATE_INITIAL) {
44 ability_->SetStartAbilitySetting(targetState.setting);
45 Start(want);
46 CheckAndRestore();
47 }
48
49 if (lifecycleState_ == AAFwk::ABILITY_STATE_ACTIVE) {
50 Inactive();
51 }
52
53 bool ret = false;
54 ret = AbilityTransaction(want, targetState);
55 if (ret) {
56 APP_LOGI("AbilityThread::HandleAbilityTransaction before AbilityManagerClient->AbilityTransitionDone");
57 AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_, targetState.state);
58 APP_LOGI("AbilityThread::HandleAbilityTransaction after AbilityManagerClient->AbilityTransitionDone");
59 }
60 APP_LOGI("PageAbilityImpl::HandleAbilityTransaction end");
61 }
62
63 /**
64 * @brief Handling the life cycle switching of PageAbility in switch.
65 *
66 * @param want Indicates the structure containing information about the ability.
67 * @param targetState The life cycle state to switch to.
68 *
69 * @return return true if the lifecycle transaction successfully, otherwise return false.
70 *
71 */
AbilityTransaction(const Want & want,const AAFwk::LifeCycleStateInfo & targetState)72 bool PageAbilityImpl::AbilityTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState)
73 {
74 APP_LOGI("PageAbilityImpl::AbilityTransaction begin");
75 bool ret = true;
76 switch (targetState.state) {
77 case AAFwk::ABILITY_STATE_INITIAL: {
78 if (lifecycleState_ == AAFwk::ABILITY_STATE_INACTIVE) {
79 Background();
80 }
81 Stop();
82 break;
83 }
84 case AAFwk::ABILITY_STATE_INACTIVE: {
85 if (lifecycleState_ == AAFwk::ABILITY_STATE_BACKGROUND) {
86 Foreground(want);
87 }
88 break;
89 }
90 case AAFwk::ABILITY_STATE_ACTIVE: {
91 if (lifecycleState_ == AAFwk::ABILITY_STATE_BACKGROUND) {
92 Foreground(want);
93 }
94 if (targetState.isNewWant) {
95 NewWant(want);
96 }
97 SerUriString(targetState.caller.deviceId + "/" + targetState.caller.bundleName + "/" +
98 targetState.caller.abilityName);
99 Active();
100 break;
101 }
102 case AAFwk::ABILITY_STATE_BACKGROUND: {
103 if (lifecycleState_ == AAFwk::ABILITY_STATE_INACTIVE) {
104 Background();
105 }
106 break;
107 }
108 default: {
109 ret = false;
110 APP_LOGE("PageAbilityImpl::HandleAbilityTransaction state error");
111 break;
112 }
113 }
114 APP_LOGI("PageAbilityImpl::AbilityTransaction end: retVal = %{public}d", (int)ret);
115 return ret;
116 }
117
118 /**
119 * @brief Execution the KeyDown callback of the ability
120 * @param keyCode Indicates the code of the key pressed.
121 * @param keyEvent Indicates the key-down event.
122 *
123 * @return Returns true if this event is handled and will not be passed further; returns false if this event is
124 * not handled and should be passed to other handlers.
125 *
126 */
DoKeyDown(int keyCode,const KeyEvent & keyEvent)127 bool PageAbilityImpl::DoKeyDown(int keyCode, const KeyEvent &keyEvent)
128 {
129 APP_LOGI("PageAbilityImpl::DoKeyDown begin");
130 if (ability_ == nullptr) {
131 APP_LOGE("PageAbilityImpl::DoKeyDown ability_ == nullptr");
132 return false;
133 }
134 auto abilitInfo = ability_->GetAbilityInfo();
135 APP_LOGI("PageAbilityImpl::DoKeyDown called %{public}s And Focus is %{public}s",
136 abilitInfo->name.c_str(),
137 ability_->HasWindowFocus() ? "true" : "false");
138
139 APP_LOGI("PageAbilityImpl::DoKeyDown end");
140 return ability_->OnKeyDown(keyCode, keyEvent);
141 }
142
143 /**
144 * @brief Execution the KeyUp callback of the ability
145 * @param keyCode Indicates the code of the key released.
146 * @param keyEvent Indicates the key-up event.
147 *
148 * @return Returns true if this event is handled and will not be passed further; returns false if this event is
149 * not handled and should be passed to other handlers.
150 *
151 */
DoKeyUp(int keyCode,const KeyEvent & keyEvent)152 bool PageAbilityImpl::DoKeyUp(int keyCode, const KeyEvent &keyEvent)
153 {
154 APP_LOGI("PageAbilityImpl::DoKeyUp begin");
155 if (ability_ == nullptr) {
156 APP_LOGE("PageAbilityImpl::DoKeyUp ability_ == nullptr");
157 return false;
158 }
159 auto abilitInfo = ability_->GetAbilityInfo();
160 APP_LOGI("PageAbilityImpl::DoKeyUp called %{public}s And Focus is %{public}s",
161 abilitInfo->name.c_str(),
162 ability_->HasWindowFocus() ? "true" : "false");
163
164 APP_LOGI("PageAbilityImpl::DoKeyUp end");
165 return ability_->OnKeyUp(keyCode, keyEvent);
166 }
167
168 /**
169 * @brief Called when a touch event is dispatched to this ability. The default implementation of this callback
170 * does nothing and returns false.
171 * @param touchEvent Indicates information about the touch event.
172 *
173 * @return Returns true if the event is handled; returns false otherwise.
174 *
175 */
DoTouchEvent(const TouchEvent & touchEvent)176 bool PageAbilityImpl::DoTouchEvent(const TouchEvent &touchEvent)
177 {
178 APP_LOGI("PageAbilityImpl::DoTouchEvent begin");
179 if (ability_ == nullptr) {
180 APP_LOGE("PageAbilityImpl::DoTouchEvent ability_ == nullptr");
181 return false;
182 }
183 auto abilitInfo = ability_->GetAbilityInfo();
184 APP_LOGI("PageAbilityImpl::OnTouchEvent called %{public}s And Focus is %{public}s",
185 abilitInfo->name.c_str(),
186 ability_->HasWindowFocus() ? "true" : "false");
187
188 APP_LOGI("PageAbilityImpl::DoTouchEvent end");
189 return ability_->OnTouchEvent(touchEvent);
190 }
191 } // namespace AppExecFwk
192 } // namespace OHOS