• 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 "ability_window.h"
17 #include "ability.h"
18 #include "ability_handler.h"
19 #include "app_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
AbilityWindow()23 AbilityWindow::AbilityWindow()
24 {}
25 
~AbilityWindow()26 AbilityWindow::~AbilityWindow()
27 {}
28 
29 /**
30  * @brief Init the AbilityWindow object.
31  *
32  * @param handler The EventHandler of the Ability the AbilityWindow belong.
33  */
Init(std::shared_ptr<AbilityHandler> & handler,std::shared_ptr<Ability> ability)34 void AbilityWindow::Init(std::shared_ptr<AbilityHandler> &handler, std::shared_ptr<Ability> ability)
35 {
36     APP_LOGI("%{public}s begin.", __func__);
37     handler_ = handler;
38     ability_ = std::weak_ptr<IAbilityEvent>(ability);
39 
40     auto wmi = WindowManager::GetInstance();
41     if (wmi == nullptr) {
42         APP_LOGE("AbilityWindow::Init WindowManager::GetInstance() is nullptr.");
43         return;
44     }
45 
46     auto wret = wmi->Init();
47     if (wret != WM_OK) {
48         APP_LOGE("AbilityWindow::Init WindowManager::Init() return %d", wret);
49         return;
50     }
51     APP_LOGI("%{public}s end.", __func__);
52 }
53 
54 /**
55  * @brief Sets the window config for the host ability to create window.
56  *
57  * @param config Indicates window config.
58  */
SetWindowConfig(const sptr<WindowOption> & config)59 bool AbilityWindow::SetWindowConfig(const sptr<WindowOption> &config)
60 {
61     APP_LOGI("%{public}s begin.", __func__);
62 
63     APP_LOGI("config width = %{public}d, height = %{public}d.", config->GetWidth(), config->GetHeight());
64     APP_LOGI("config pos_x = %{public}d, pos_y = %{public}d, type = %{public}d.",
65         config->GetX(),
66         config->GetY(),
67         config->GetWindowType());
68 
69     auto wmi = WindowManager::GetInstance();
70     if (wmi == nullptr) {
71         APP_LOGE("AbilityWindow::Init WindowManager::GetInstance() is nullptr.");
72         return false;
73     }
74 
75     APP_LOGI("%{public}s begin wms->CreateWindow.", __func__);
76     auto retvalCreate = wmi->CreateWindow(windowNew_, config);
77     APP_LOGI("%{public}s end wms->CreateWindow.", __func__);
78     if (retvalCreate != WM_OK) {
79         APP_LOGE("AbilityWindow::SetWindowConfig WindowManager::CreateWindow() return %d", retvalCreate);
80         return false;
81     }
82 
83     if (windowNew_ == nullptr) {
84         APP_LOGE("AbilityWindow::SetWindowConfig the window is nullptr.");
85         return false;
86     }
87 
88     auto callback = [abilityWindow = this](KeyEvent event) -> bool { return abilityWindow->OnKeyEvent(event); };
89     APP_LOGI("%{public}s begin windowNew_->RegistOnKeyCb.", __func__);
90     auto retvalKeyboardKey = windowNew_->OnKey(callback);
91     APP_LOGI("%{public}s end windowNew_->RegistOnKeyCb.", __func__);
92     if (retvalKeyboardKey != WM_OK) {
93         APP_LOGE("AbilityWindow::SetWindowConfig WindowManager::OnKey() return %d", retvalKeyboardKey);
94         return false;
95     }
96 
97     isWindowAttached = true;
98     APP_LOGI("%{public}s end.", __func__);
99 
100     return true;
101 }
102 
103 /**
104  * @brief Called when the KeyEvent sent.
105  *
106  * @param KeyEvent the key event.
107  *
108  * @return Returns true if the listener has processed the event; returns false otherwise.
109  *
110  */
OnKeyEvent(KeyEvent event)111 bool AbilityWindow::OnKeyEvent(KeyEvent event)
112 {
113     APP_LOGI("%{public}s begin.", __func__);
114 
115     bool ret = false;
116     std::shared_ptr<IAbilityEvent> ability = nullptr;
117     ability = ability_.lock();
118     if (ability == nullptr) {
119         APP_LOGE("AbilityWindow::OnKeyEvent ability is nullptr.");
120         return ret;
121     }
122     switch (event.GetKeyCode()) {
123 #ifdef MMI_COMPILE
124         case OHOS::KeyEventEnum::KEY_BACK:
125 #else
126         case KeyEvent::CODE_BACK:
127 #endif
128             APP_LOGI("AbilityWindow::OnKeyEvent Back key pressed.");
129             if (!event.IsKeyDown()) {
130                 ret = OnBackPressed(ability);
131             }
132             break;
133         default:
134             APP_LOGI("AbilityWindow::OnKeyEvent the key event is %{public}d.", event.GetKeyCode());
135             break;
136     }
137     APP_LOGI("%{public}s end.", __func__);
138     return ret;
139 }
140 
141 /**
142  * @brief Called back when the Back key is pressed.
143  *
144  * @param ability The ability receive the event.
145  *
146  * @return Returns true if the listener has processed the event; returns false otherwise.
147  *
148  */
OnBackPressed(std::shared_ptr<IAbilityEvent> & ability)149 bool AbilityWindow::OnBackPressed(std::shared_ptr<IAbilityEvent> &ability)
150 {
151     APP_LOGI("%{public}s begin.", __func__);
152     if (handler_ == nullptr) {
153         APP_LOGE("AbilityWindow::OnBackPressed handler_ is nullptr.");
154         return false;
155     }
156     auto task = [abilityRun = ability]() { abilityRun->OnBackPressed(); };
157     handler_->PostTask(task);
158     APP_LOGI("%{public}s end.", __func__);
159     return true;
160 }
161 
162 /**
163  * @brief Called when this ability is started.
164  *
165  */
OnPostAbilityStart()166 void AbilityWindow::OnPostAbilityStart()
167 {
168     APP_LOGI("%{public}s begin.", __func__);
169     if (!isWindowAttached) {
170         APP_LOGE("AbilityWindow::OnPostAbilityStart window not attached.");
171         return;
172     }
173 
174     if (windowNew_ != nullptr) {
175         APP_LOGI("%{public}s begin windowNew_->Hide.", __func__);
176         windowNew_->Hide();
177         APP_LOGI("%{public}s end windowNew_->Hide.", __func__);
178     }
179 
180     APP_LOGI("%{public}s end.", __func__);
181 }
182 
183 /**
184  * @brief Called when this ability is activated.
185  *
186  */
OnPostAbilityActive()187 void AbilityWindow::OnPostAbilityActive()
188 {
189     APP_LOGI("AbilityWindow::OnPostAbilityActive called.");
190     if (!isWindowAttached) {
191         APP_LOGE("AbilityWindow::OnPostAbilityActive window not attached.");
192         return;
193     }
194 
195     if (windowNew_ != nullptr) {
196         APP_LOGI("%{public}s begin windowNew_->SwitchTop.", __func__);
197         windowNew_->SwitchTop();
198         APP_LOGI("%{public}s end windowNew_->SwitchTop.", __func__);
199 
200         APP_LOGI("%{public}s begin windowNew_->Show.", __func__);
201         windowNew_->Show();
202         APP_LOGI("%{public}s end windowNew_->Show.", __func__);
203     }
204 
205     APP_LOGI("AbilityWindow::OnPostAbilityActive end.");
206 }
207 
208 /**
209  * @brief Called when this ability is inactivated.
210  *
211  */
OnPostAbilityInactive()212 void AbilityWindow::OnPostAbilityInactive()
213 {
214     APP_LOGI("AbilityWindow::OnPostAbilityInactive called.");
215     if (!isWindowAttached) {
216         APP_LOGE("AbilityWindow::OnPostAbilityInactive window not attached.");
217         return;
218     }
219 
220     if (windowNew_ != nullptr) {
221         APP_LOGI("%{public}s begin windowNew_->Hide.", __func__);
222         windowNew_->Hide();
223         APP_LOGI("%{public}s end windowNew_->Hide.", __func__);
224     }
225 
226     APP_LOGI("AbilityWindow::OnPostAbilityInactive end.");
227 }
228 
229 /**
230  * @brief Called when this ability is background.
231  *
232  */
OnPostAbilityBackground()233 void AbilityWindow::OnPostAbilityBackground()
234 {
235     APP_LOGI("AbilityWindow::OnPostAbilityBackground called.");
236     if (!isWindowAttached) {
237         APP_LOGE("AbilityWindow::OnPostAbilityBackground window not attached.");
238         return;
239     }
240 
241     if (windowNew_ != nullptr) {
242         APP_LOGI("%{public}s begin windowNew_->Hide.", __func__);
243         windowNew_->Hide();
244         APP_LOGI("%{public}s end windowNew_->Hide.", __func__);
245     }
246 
247     APP_LOGI("AbilityWindow::OnPostAbilityBackground end.");
248 }
249 
250 /**
251  * @brief Called when this ability is foreground.
252  *
253  */
OnPostAbilityForeground()254 void AbilityWindow::OnPostAbilityForeground()
255 {
256     APP_LOGI("AbilityWindow::OnPostAbilityForeground called.");
257     if (!isWindowAttached) {
258         APP_LOGE("AbilityWindow::OnPostAbilityForeground window not attached.");
259         return;
260     }
261 
262     if (windowNew_ != nullptr) {
263         APP_LOGI("%{public}s begin windowNew_->Show.", __func__);
264         windowNew_->Show();
265         APP_LOGI("%{public}s end windowNew_->Show.", __func__);
266     }
267 
268     APP_LOGI("AbilityWindow::OnPostAbilityForeground end.");
269 }
270 
271 /**
272  * @brief Called when this ability is stopped.
273  *
274  */
OnPostAbilityStop()275 void AbilityWindow::OnPostAbilityStop()
276 {
277     APP_LOGI("AbilityWindow::OnPostAbilityStop called.");
278     if (!isWindowAttached) {
279         APP_LOGE("AbilityWindow::OnPostAbilityStop window not attached.");
280         return;
281     }
282 
283     if (windowNew_ != nullptr) {
284         int32_t windowID = windowNew_->GetID();
285         APP_LOGI("AbilityWindow::widow::DestroyWindow called windowID=%{public}d begin.", windowID);
286         windowNew_->Destroy();
287         APP_LOGI("AbilityWindow::widow::DestroyWindow called windowID=%{public}d end.", windowID);
288         windowNew_ = nullptr;
289         APP_LOGI("AbilityWindow::widow:: windowNew_ release end.");
290     }
291 
292     isWindowAttached = false;
293     APP_LOGI("AbilityWindow::OnPostAbilityStop end.");
294 }
295 
296 /**
297  * @brief Get the window belong to the ability.
298  *
299  * @return Returns a Window object pointer.
300  */
GetWindow()301 const sptr<Window> AbilityWindow::GetWindow()
302 {
303     if (!isWindowAttached) {
304         APP_LOGE("AbilityWindow::GetWindow window not attached.");
305     }
306 
307     if (windowNew_ == nullptr) {
308         APP_LOGE("AbilityWindow::GetWindow the window is nullptr.");
309     }
310     return windowNew_;
311 }
312 }  // namespace AppExecFwk
313 }  // namespace OHOS