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_keyevent.h"
17 #include "ability_impl.h"
18 #include "app_log_wrapper.h"
19 #include "ability_post_event_timeout.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
AbilityKeyEventHandle(const std::shared_ptr<AbilityImpl> & abilityImpl)23 AbilityKeyEventHandle::AbilityKeyEventHandle(const std::shared_ptr<AbilityImpl> &abilityImpl)
24 : abilityImpl_(abilityImpl)
25 {
26 APP_LOGI("AbilityKeyEventHandle is created");
27 }
28
~AbilityKeyEventHandle()29 AbilityKeyEventHandle::~AbilityKeyEventHandle()
30 {
31 APP_LOGI("AbilityKeyEventHandle is destroyed");
32 }
33
34 #ifdef MMI_COMPILE
35 /**
36 * @brief Called back when on key.
37 */
OnKey(const KeyEvent & keyEvent)38 bool AbilityKeyEventHandle::OnKey(const KeyEvent &keyEvent)
39 {
40 APP_LOGI("AbilityKeyEventHandle::OnKey called.");
41 bool ret = false;
42 if (abilityImpl_ == nullptr) {
43 APP_LOGE("AbilityImpl::OnKey abilityImpl_ is nullptr. KeyCode %{public}d.", keyEvent.GetKeyCode());
44 return ret;
45 }
46
47 if (keyEvent.IsKeyDown()) {
48 std::string taskHead("OnKey");
49 std::string taskCode = std::to_string(keyEvent.GetKeyCode());
50 std::string taskTail("Down");
51 auto timeOut = abilityImpl_->CreatePostEventTimeouter(taskHead + taskCode + taskTail);
52 if (timeOut == nullptr) {
53 APP_LOGW("AbilityKeyEventHandle::OnKeyDown timeouter Create return nullptr");
54 ret = abilityImpl_->DoKeyDown(keyEvent.GetKeyCode(), keyEvent);
55 } else {
56 timeOut->TimingBegin();
57 ret = abilityImpl_->DoKeyDown(keyEvent.GetKeyCode(), keyEvent);
58 timeOut->TimeEnd();
59 }
60 APP_LOGI("AbilityImpl::OnKeyDown keyCode: %{public}d.", keyEvent.GetKeyCode());
61 } else {
62 std::string taskHead("OnKey");
63 std::string taskCode = std::to_string(keyEvent.GetKeyCode());
64 std::string taskTail("Up");
65 auto timeOut = abilityImpl_->CreatePostEventTimeouter(taskHead + taskCode + taskTail);
66 if (timeOut == nullptr) {
67 APP_LOGW("AbilityKeyEventHandle::OnKeyUp timeouter Create return nullptr");
68 ret = abilityImpl_->DoKeyUp(keyEvent.GetKeyCode(), keyEvent);
69 } else {
70 timeOut->TimingBegin();
71 ret = abilityImpl_->DoKeyUp(keyEvent.GetKeyCode(), keyEvent);
72 timeOut->TimeEnd();
73 }
74 APP_LOGI("AbilityImpl::DoKeyUp keyCode: %{public}d.", keyEvent.GetKeyCode());
75 }
76
77 APP_LOGI("AbilityKeyEventHandle::OnKey called end. return %{public}s", ret ? "true" : "false");
78 return ret;
79 }
80 #endif
81 } // namespace AppExecFwk
82 } // namespace OHOS