• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "fingerprint_event_processor.h"
17 
18 #include "ability_manager_client.h"
19 #include "event_log_helper.h"
20 #include "ffrt.h"
21 #include "input_event_handler.h"
22 #include "pointer_event.h"
23 #include "res_sched_client.h"
24 #include "res_type.h"
25 #include "setting_datashare.h"
26 #include "system_ability_definition.h"
27 
28 #undef MMI_LOG_DOMAIN
29 #define MMI_LOG_DOMAIN MMI_LOG_DISPATCH
30 #undef MMI_LOG_TAG
31 #define MMI_LOG_TAG "FingerprintEventProcessor"
32 
33 namespace OHOS {
34 namespace MMI {
35 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
36 namespace {
37 constexpr int32_t KEY_INIT { 0 };
38 constexpr int32_t KEY_DOWN { 1 };
39 constexpr int32_t KEY_UP { 2 };
40 constexpr int32_t POWER_KEY_UP_TIME { 1000 }; // 1000ms
41 const std::string IS_START_SMART_KEY = "close_fingerprint_nav_event_key";
42 const std::string IS_SMART_KEY_USE = "close_fingerprint_event_key";
43 const std::string NEED_SHOW_DIALOG = "1";
44 const std::string SMART_KEY_IS_OPEN = "1";
45 const std::string SMART_KEY_IS_CLOSE = "0";
46 constexpr int32_t IS_SHOW_DIALOG = 1;
47 constexpr int32_t VOLUME_KEY_UP_TIME { 500 }; // 500ms
48 }
FingerprintEventProcessor()49 FingerprintEventProcessor::FingerprintEventProcessor()
50 {}
51 
~FingerprintEventProcessor()52 FingerprintEventProcessor::~FingerprintEventProcessor()
53 {}
54 
IsFingerprintEvent(struct libinput_event * event)55 bool FingerprintEventProcessor::IsFingerprintEvent(struct libinput_event* event)
56 {
57     CALL_DEBUG_ENTER;
58     CHKPR(event, false);
59     if (!isStartedSmartKey_) {
60         StartSmartKeyIfNeeded();
61         isStartedSmartKey_ = true;
62     }
63     if (!isCreatedObserver_) {
64         smartKeySwitch_.keyString = IS_START_SMART_KEY;
65         CreateStatusConfigObserver(smartKeySwitch_);
66         isCreatedObserver_ = true;
67     }
68     auto device = libinput_event_get_device(event);
69     CHKPR(device, false);
70     std::string name = libinput_device_get_name(device);
71     if (name != FINGERPRINT_SOURCE_KEY && name != FINGERPRINT_SOURCE_POINT) {
72         MMI_HILOGD("Not FingerprintEvent");
73         return false;
74     }
75     if (name == FINGERPRINT_SOURCE_KEY) {
76         struct libinput_event_keyboard* keyBoard = libinput_event_get_keyboard_event(event);
77         CHKPR(keyBoard, false);
78         auto key = libinput_event_keyboard_get_key(keyBoard);
79         if (key != FINGERPRINT_CODE_DOWN && key != FINGERPRINT_CODE_UP
80             && key != FINGERPRINT_CODE_CLICK && key != FINGERPRINT_CODE_RETOUCH
81             && key != FINGERPRINT_CODE_CANCEL) {
82             MMI_HILOGD("Not FingerprintEvent event");
83             return false;
84         }
85     }
86     return true;
87 }
88 
SetPowerAndVolumeKeyState(struct libinput_event * event)89 void FingerprintEventProcessor::SetPowerAndVolumeKeyState(struct libinput_event* event)
90 {
91     CALL_DEBUG_ENTER;
92     CHKPV(event);
93     auto device = libinput_event_get_device(event);
94     CHKPV(device);
95     auto data = libinput_event_get_keyboard_event(event);
96     CHKPV(data);
97     int32_t keyCode = static_cast<int32_t>(libinput_event_keyboard_get_key(data));
98     auto iter = keyStateMap_.find(keyCode);
99     if (iter == keyStateMap_.end()) {
100         MMI_HILOGD("current keycode is not mistouch key, keycode is %{private}d", keyCode);
101         return;
102     }
103     int32_t keyAction = (libinput_event_keyboard_get_key_state(data) == 0) ?
104         (KeyEvent::KEY_ACTION_UP) : (KeyEvent::KEY_ACTION_DOWN);
105     MMI_HILOGD("current keycode is %{private}d, keyaction is %{private}d", keyCode, keyAction);
106     if (keyAction ==  KeyEvent::KEY_ACTION_DOWN) {
107         iter->second.first = KEY_DOWN;
108         SendFingerprintCancelEvent();
109     } else {
110         iter->second.first = KEY_UP;
111         iter->second.second = std::chrono::steady_clock::now();
112     }
113 }
114 
SetScreenState(struct libinput_event * event)115 void FingerprintEventProcessor::SetScreenState(struct libinput_event* event)
116 {
117     CALL_DEBUG_ENTER;
118     CHKPV(event);
119     auto type = libinput_event_get_type(event);
120     MMI_HILOGD("smart key screen state is %{public}d", type);
121     switch (type) {
122         case LIBINPUT_EVENT_TOUCH_DOWN: {
123             screenState_ = true;
124             break;
125         }
126         case LIBINPUT_EVENT_TOUCH_UP: {
127             screenState_ = false;
128             break;
129         }
130         default: {
131             MMI_HILOGD("Unknown event type, touchType:%{public}d", type);
132             return;
133         }
134     }
135     ChangeScreenMissTouchFlag(screenState_, cancelState_);
136 }
137 
ChangeScreenMissTouchFlag(bool screen,bool cancel)138 void FingerprintEventProcessor::ChangeScreenMissTouchFlag(bool screen, bool cancel)
139 {
140     int32_t flag = screenMissTouchFlag_ ? 1 : 0;
141     MMI_HILOGD("screenMissTouchFlag_ : %{private}d, screen : %{private}d, cancel : %{private}d", flag, screen, screen);
142     if (screenMissTouchFlag_ == false) {
143         if (screen == true) {
144             screenMissTouchFlag_ = true;
145             SendFingerprintCancelEvent();
146             return;
147         }
148     } else {
149         if (screen == false && cancel == true) {
150             screenMissTouchFlag_ = false;
151             return;
152         }
153     }
154 }
155 
CheckMisTouchState()156 bool FingerprintEventProcessor::CheckMisTouchState()
157 {
158     if (CheckKeyMisTouchState() || CheckScreenMisTouchState()) {
159         return true;
160     }
161     return false;
162 }
163 
CheckScreenMisTouchState()164 bool FingerprintEventProcessor::CheckScreenMisTouchState()
165 {
166     int32_t flag = screenMissTouchFlag_ ? 1 : 0;
167     MMI_HILOGI("screenMissTouchFlag_ is %{public}d", flag);
168     return screenMissTouchFlag_;
169 }
170 
CheckKeyMisTouchState()171 bool FingerprintEventProcessor::CheckKeyMisTouchState()
172 {
173     CALL_DEBUG_ENTER;
174     bool ret = false;
175     for (auto &[key, value] : keyStateMap_) {
176         auto keystate = value.first;
177         MMI_HILOGD("keycode : %{private}d, state : %{public}d", key, value.first);
178         if (keystate == KEY_DOWN) {
179             ret = true;
180         } else if (keystate == KEY_UP) {
181             auto currentTime = std::chrono::steady_clock::now();
182             auto duration = currentTime - value.second;
183             auto durationMs = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
184             int32_t time = POWER_KEY_UP_TIME;
185             if (key != KEY_POWER) {
186                 time = VOLUME_KEY_UP_TIME;
187             }
188             if (durationMs < time) {
189                 MMI_HILOGD("Dont report because time diff < threshold, keycode : %{private}d, state : %{public}d",
190                     key, value.first);
191                 ret = true;
192             } else {
193                 value.first = KEY_INIT;
194             }
195         }
196     }
197     MMI_HILOGI("KeyMisTouchState is %{public}d", ret);
198     return ret;
199 }
200 
SendFingerprintCancelEvent()201 int32_t FingerprintEventProcessor::SendFingerprintCancelEvent()
202 {
203     CALL_DEBUG_ENTER;
204     auto pointerEvent = PointerEvent::Create();
205     CHKPR(pointerEvent, ERROR_NULL_POINTER);
206     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_FINGERPRINT_CANCEL);
207     int64_t time = GetSysClockTime();
208     pointerEvent->SetActionTime(time);
209     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_FINGERPRINT);
210     pointerEvent->SetPointerId(0);
211     EventLogHelper::PrintEventData(pointerEvent, MMI_LOG_HEADER);
212     MMI_HILOGD("Fingerprint key:%{public}d", pointerEvent->GetPointerAction());
213 #if (defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)) && defined(OHOS_BUILD_ENABLE_MONITOR)
214     auto eventMonitorHandler_ = InputHandler->GetMonitorHandler();
215     if (eventMonitorHandler_ != nullptr) {
216         eventMonitorHandler_->OnHandleEvent(pointerEvent);
217     }
218 #endif
219     return ERR_OK;
220 }
221 
HandleFingerprintEvent(struct libinput_event * event)222 int32_t FingerprintEventProcessor::HandleFingerprintEvent(struct libinput_event* event)
223 {
224     CALL_DEBUG_ENTER;
225     CHKPR(event, ERROR_NULL_POINTER);
226     auto device = libinput_event_get_device(event);
227     CHKPR(device, PARAM_INPUT_INVALID);
228     std::string name = libinput_device_get_name(device);
229     if (name == FINGERPRINT_SOURCE_KEY) {
230         return AnalyseKeyEvent(event);
231     } else if (name == FINGERPRINT_SOURCE_POINT) {
232         ProcessSlideEvent();
233         return AnalysePointEvent(event);
234     } else {
235         MMI_HILOGI("Unknown input device name:%{public}s", name.c_str());
236         return PARAM_INPUT_INVALID;
237     }
238 }
239 
AnalyseKeyEvent(struct libinput_event * event)240 int32_t FingerprintEventProcessor::AnalyseKeyEvent(struct libinput_event *event)
241 {
242     CALL_DEBUG_ENTER;
243     CHKPR(event, ERROR_NULL_POINTER);
244     struct libinput_event_keyboard* keyEvent = libinput_event_get_keyboard_event(event);
245     CHKPR(keyEvent, ERROR_NULL_POINTER);
246     auto key = libinput_event_keyboard_get_key(keyEvent);
247     enum libinput_key_state state = libinput_event_keyboard_get_key_state(keyEvent);
248     if (state == LIBINPUT_KEY_STATE_PRESSED) {
249         MMI_HILOGI("Dont analyse the press status for %{public}d", key);
250         return ERR_OK;
251     }
252     auto pointerEvent = PointerEvent::Create();
253     CHKPR(pointerEvent, ERROR_NULL_POINTER);
254     isStartedSmartKeyBySlide_ = false;
255     switch (key) {
256         case FINGERPRINT_CODE_DOWN: {
257             cancelState_ = false;
258             ChangeScreenMissTouchFlag(screenState_, true);
259             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_FINGERPRINT_DOWN);
260             ReportResSched(ResourceSchedule::ResType::RES_TYPE_CLICK_RECOGNIZE,
261                 ResourceSchedule::ResType::ClickEventType::TOUCH_EVENT_DOWN);
262             break;
263         }
264         case FINGERPRINT_CODE_CANCEL: {
265             cancelState_ = true;
266             ChangeScreenMissTouchFlag(screenState_, cancelState_);
267             MMI_HILOGI("change cancel state and dont send point event");
268             return RET_OK;
269         }
270         case FINGERPRINT_CODE_UP: {
271             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_FINGERPRINT_UP);
272             ReportResSched(ResourceSchedule::ResType::RES_TYPE_CLICK_RECOGNIZE,
273                 ResourceSchedule::ResType::ClickEventType::TOUCH_EVENT_UP);
274             break;
275         }
276         case FINGERPRINT_CODE_RETOUCH: {
277             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_FINGERPRINT_RETOUCH);
278             ReportResSched(ResourceSchedule::ResType::RES_TYPE_CLICK_RECOGNIZE,
279                 ResourceSchedule::ResType::ClickEventType::TOUCH_EVENT_DOWN);
280             break;
281         }
282         case FINGERPRINT_CODE_CLICK: {
283             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_FINGERPRINT_CLICK);
284             ProcessClickEvent();
285             break;
286         }
287         default:
288             MMI_HILOGW("Unknown key event:%{public}d", key);
289             return UNKNOWN_EVENT;
290     }
291     int64_t time = GetSysClockTime();
292     pointerEvent->SetActionTime(time);
293     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_FINGERPRINT);
294     pointerEvent->SetPointerId(0);
295     EventLogHelper::PrintEventData(pointerEvent, MMI_LOG_HEADER);
296     MMI_HILOGI("Fingerprint key:%{public}d", pointerEvent->GetPointerAction());
297     if (CheckMisTouchState()) {
298         MMI_HILOGD("in mistouch state, dont report event");
299         return ERR_OK;
300     }
301 #if (defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)) && defined(OHOS_BUILD_ENABLE_MONITOR)
302     auto eventMonitorHandler_ = InputHandler->GetMonitorHandler();
303     if (eventMonitorHandler_ != nullptr) {
304         eventMonitorHandler_->OnHandleEvent(pointerEvent);
305     }
306 #endif // (OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH) && OHOS_BUILD_ENABLE_MONITOR
307     return RET_OK;
308 }
309 
AnalysePointEvent(libinput_event * event)310 int32_t FingerprintEventProcessor::AnalysePointEvent(libinput_event * event)
311 {
312     CALL_DEBUG_ENTER;
313     struct libinput_event_pointer* rawPointerEvent = libinput_event_get_pointer_event(event);
314     CHKPR(rawPointerEvent, ERROR_NULL_POINTER);
315     double ux = libinput_event_pointer_get_dx_unaccelerated(rawPointerEvent);
316     double uy = libinput_event_pointer_get_dy_unaccelerated(rawPointerEvent);
317     auto pointerEvent = PointerEvent::Create();
318     CHKPR(pointerEvent, ERROR_NULL_POINTER);
319     int64_t time = GetSysClockTime();
320     pointerEvent->SetActionTime(time);
321     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE);
322     pointerEvent->SetFingerprintDistanceX(ux);
323     pointerEvent->SetFingerprintDistanceY(uy);
324     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_FINGERPRINT);
325     pointerEvent->SetPointerId(0);
326     EventLogHelper::PrintEventData(pointerEvent, MMI_LOG_HEADER);
327     MMI_HILOGI("Fingerprint key:%{public}d, ux:%f, uy:%f", pointerEvent->GetPointerAction(), ux, uy);
328     if (CheckMisTouchState()) {
329         MMI_HILOGD("in mistouch state, dont report event");
330         return ERR_OK;
331     }
332 #if (defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)) && defined(OHOS_BUILD_ENABLE_MONITOR)
333     auto eventMonitorHandler_ = InputHandler->GetMonitorHandler();
334     if (eventMonitorHandler_ != nullptr) {
335         eventMonitorHandler_->OnHandleEvent(pointerEvent);
336     }
337 #endif // (OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH) && OHOS_BUILD_ENABLE_MONITOR
338     return RET_OK;
339 }
340 
341 template <class T>
CreateStatusConfigObserver(T & item)342 void FingerprintEventProcessor::CreateStatusConfigObserver(T& item)
343 {
344     CALL_DEBUG_ENTER;
345     SettingObserver::UpdateFunc updateFunc = [&item](const std::string& key) {
346         std::string value = NEED_SHOW_DIALOG;
347         auto ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
348             .GetStringValue(key, value);
349         if (ret != RET_OK) {
350             MMI_HILOGE("Get value from settings db failed, ret: %{public}d", ret);
351             return;
352         }
353         MMI_HILOGI("Config changed, key: %{public}s, value: %{public}s", key.c_str(), value.c_str());
354         item.valueString = value;
355     };
356 
357     sptr<SettingObserver> statusObserver = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
358         .CreateObserver(item.keyString, updateFunc);
359     ErrCode ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID).RegisterObserver(statusObserver);
360     if (ret != RET_OK) {
361         MMI_HILOGE("Register setting observer failed, ret: %{public}d", ret);
362         statusObserver = nullptr;
363     }
364 
365     std::string value = NEED_SHOW_DIALOG;
366     ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
367         .SettingDataShare::GetStringValue(item.keyString, value);
368     if (ret != RET_OK) {
369         MMI_HILOGE("Get value from settings db failed, ret: %{public}d", ret);
370         return;
371     }
372     MMI_HILOGI("Get value success, key: %{public}s, value: %{public}s", item.keyString.c_str(), value.c_str());
373     item.valueString = value;
374 }
375 
StartSmartKeyIfNeeded()376 void FingerprintEventProcessor::StartSmartKeyIfNeeded()
377 {
378     std::string isStartSmartKey = SMART_KEY_IS_CLOSE;
379     ErrCode ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
380         .SettingDataShare::GetStringValue(IS_SMART_KEY_USE, isStartSmartKey);
381     if (ret != RET_OK) {
382         MMI_HILOGE("Get value from settings db failed, ret: %{public}d", ret);
383         return;
384     }
385     if (isStartSmartKey == SMART_KEY_IS_OPEN) {
386         MMI_HILOGI("Before start smart-key");
387         StartSmartKey(false);
388     }
389 }
390 
StartSmartKey(bool isShowDialog)391 void FingerprintEventProcessor::StartSmartKey(bool isShowDialog)
392 {
393     ffrt::submit([isShowDialog] {
394         MMI_HILOGI("StartServiceExtAbility start");
395         std::shared_ptr<AAFwk::AbilityManagerClient> abmc = AAFwk::AbilityManagerClient::GetInstance();
396         CHKPV(abmc);
397         const std::string smartKeyBundleName = "com.hmos.hwquickaccessmenu";
398         const std::string smartKeyAbilityName = "ServiceAbility";
399         AAFwk::Want want;
400         want.SetElementName(smartKeyBundleName, smartKeyAbilityName);
401         if (isShowDialog) {
402             want.SetParam("isShowDialog", IS_SHOW_DIALOG);
403         }
404 
405         auto ret = abmc->StartExtensionAbility(want, nullptr, -1, AppExecFwk::ExtensionAbilityType::SERVICE);
406         if (ret != RET_OK) {
407             MMI_HILOGE("StartExtensionAbility failed, ret: %{public}d", ret);
408             return;
409         }
410         MMI_HILOGI("StartServiceExtAbility finished");
411     });
412 }
413 
ProcessSlideEvent()414 void FingerprintEventProcessor::ProcessSlideEvent()
415 {
416     if ((smartKeySwitch_.valueString == NEED_SHOW_DIALOG || smartKeySwitch_.valueString.empty()) &&
417         !isStartedSmartKeyBySlide_) {
418         isStartedSmartKeyBySlide_ = true;
419         StartSmartKey(true);
420     }
421 }
422 
ProcessClickEvent()423 void FingerprintEventProcessor::ProcessClickEvent()
424 {
425     if (smartKeySwitch_.valueString == NEED_SHOW_DIALOG || smartKeySwitch_.valueString.empty()) {
426         StartSmartKey(true);
427     }
428     ReportResSched(ResourceSchedule::ResType::RES_TYPE_CLICK_RECOGNIZE,
429         ResourceSchedule::ResType::ClickEventType::TOUCH_EVENT_DOWN);
430 }
431 
ReportResSched(uint32_t resType,int64_t value)432 void FingerprintEventProcessor::ReportResSched(uint32_t resType, int64_t value)
433 {
434     std::unordered_map<std::string, std::string> payload { {"msg", ""} };
435     ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, value, payload);
436 }
437 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
438 } // namespace MMI
439 } // namespace OHOS
440