diff --git a/BoardConfig.mk b/BoardConfig.mk index fd89dc6..3408fb1 100644 --- a/BoardConfig.mk +++ b/BoardConfig.mk @@ -130,6 +130,9 @@ SYSTEM_EXT_PRIVATE_SEPOLICY_DIRS += $(DEVICE_PATH)/sepolicy/private SYSTEM_EXT_PUBLIC_SEPOLICY_DIRS += $(DEVICE_PATH)/sepolicy/public BOARD_VENDOR_SEPOLICY_DIRS += $(DEVICE_PATH)/sepolicy/vendor +# UDFPS +TARGET_SURFACEFLINGER_UDFPS_LIB := //$(DEVICE_PATH):libudfps_extension.a32 + # Verified Boot BOARD_AVB_ENABLE := true BOARD_AVB_MAKE_VBMETA_IMAGE_ARGS += --flags 3 diff --git a/device.mk b/device.mk index 80a5e56..271408b 100644 --- a/device.mk +++ b/device.mk @@ -127,7 +127,8 @@ PRODUCT_PACKAGES += \ # Fingerprint PRODUCT_PACKAGES += \ - android.hardware.biometrics.fingerprint@2.1.vendor + android.hardware.biometrics.fingerprint@2.3-service-samsung.a32 \ + SamsungUdfpsHandler.a32 # Gatekeeper PRODUCT_PACKAGES += \ diff --git a/fingerprint/Android.bp b/fingerprint/Android.bp new file mode 100644 index 0000000..6f406af --- /dev/null +++ b/fingerprint/Android.bp @@ -0,0 +1,7 @@ +cc_library_static { + name: "libudfps_extension.a32", + srcs: ["UdfpsExtension.cpp"], + include_dirs: [ + "frameworks/native/services/surfaceflinger/CompositionEngine/include" + ], +} diff --git a/fingerprint/Android.mk b/fingerprint/Android.mk new file mode 100644 index 0000000..04deaa6 --- /dev/null +++ b/fingerprint/Android.mk @@ -0,0 +1,64 @@ +# +# Copyright (C) 2021 The LineageOS Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + BiometricsFingerprint.cpp \ + service.cpp + +LOCAL_SHARED_LIBRARIES := \ + libbase \ + libhardware \ + libhidlbase \ + liblog \ + libutils \ + android.hardware.biometrics.fingerprint@2.1 \ + android.hardware.biometrics.fingerprint@2.2 \ + android.hardware.biometrics.fingerprint@2.3 + +ifeq ($(TARGET_SEC_FP_CALL_NOTIFY_ON_CANCEL),true) + LOCAL_CFLAGS += -DCALL_NOTIFY_ON_CANCEL +endif + +ifeq ($(TARGET_SEC_FP_USES_PERCENTAGE_SAMPLES),true) + LOCAL_CFLAGS += -DUSES_PERCENTAGE_SAMPLES +endif + +ifeq ($(TARGET_SEC_FP_CALL_CANCEL_ON_ENROLL_COMPLETION),true) + LOCAL_CFLAGS += -DCALL_CANCEL_ON_ENROLL_COMPLETION +endif + +ifeq ($(TARGET_SEC_FP_HAS_FINGERPRINT_GESTURES),true) + LOCAL_CFLAGS += -DHAS_FINGERPRINT_GESTURES +endif + +ifeq ($(TARGET_SEC_FP_REQUEST_FORCE_CALIBRATE),true) + LOCAL_CFLAGS += -DREQUEST_FORCE_CALIBRATE +endif + +LOCAL_CFLAGS += -DLOG_TAG="\"$(LOCAL_MODULE)\"" +LOCAL_MODULE := android.hardware.biometrics.fingerprint@2.3-service-samsung.a32 +LOCAL_INIT_RC := android.hardware.biometrics.fingerprint@2.3-service-samsung.a32.rc +LOCAL_VINTF_FRAGMENTS := android.hardware.biometrics.fingerprint@2.3-service-samsung.a32.xml +LOCAL_MODULE_RELATIVE_PATH := hw +LOCAL_MODULE_TAGS := optional +LOCAL_MODULE_OWNER := samsung +LOCAL_VENDOR_MODULE := true + +include $(BUILD_EXECUTABLE) diff --git a/fingerprint/BiometricsFingerprint.cpp b/fingerprint/BiometricsFingerprint.cpp new file mode 100644 index 0000000..0afc604 --- /dev/null +++ b/fingerprint/BiometricsFingerprint.cpp @@ -0,0 +1,542 @@ +/* + * Copyright (C) 2019 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +#include +#include +#include "BiometricsFingerprint.h" +#include +#include +#include +#include +#include + +#ifdef HAS_FINGERPRINT_GESTURES +#include +#endif + +#define TSP_CMD_PATH "/sys/class/sec/tsp/cmd" +#define HBM_PATH "/sys/class/lcd/panel/mask_brightness" + +namespace android { +namespace hardware { +namespace biometrics { +namespace fingerprint { +namespace V2_3 { +namespace implementation { + +using RequestStatus = android::hardware::biometrics::fingerprint::V2_1::RequestStatus; + +BiometricsFingerprint* BiometricsFingerprint::sInstance = nullptr; + +template +static void set(const std::string& path, const T& value) { + std::ofstream file(path); + file << value; +} + +BiometricsFingerprint::BiometricsFingerprint() : mClientCallback(nullptr) { + sInstance = this; // keep track of the most recent instance + if (!openHal()) { + LOG(ERROR) << "Can't open HAL module"; + } + + std::ifstream in("/sys/devices/virtual/fingerprint/fingerprint/position"); + mIsUdfps = !!in; + if (in) + in.close(); + +#ifdef HAS_FINGERPRINT_GESTURES + request(FINGERPRINT_REQUEST_NAVIGATION_MODE_START, 1); + + uinputFd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); + if (uinputFd < 0) { + LOG(ERROR) << "Unable to open uinput node"; + return; + } + + int err = ioctl(uinputFd, UI_SET_EVBIT, EV_KEY) | + ioctl(uinputFd, UI_SET_KEYBIT, KEY_UP) | + ioctl(uinputFd, UI_SET_KEYBIT, KEY_DOWN); + if (err != 0) { + LOG(ERROR) << "Unable to enable key events"; + return; + } + + struct uinput_user_dev uidev; + sprintf(uidev.name, "uinput-sec-fp"); + uidev.id.bustype = BUS_VIRTUAL; + + err = write(uinputFd, &uidev, sizeof(uidev)); + if (err < 0) { + LOG(ERROR) << "Write user device to uinput node failed"; + return; + } + + err = ioctl(uinputFd, UI_DEV_CREATE); + if (err < 0) { + LOG(ERROR) << "Unable to create uinput device"; + return; + } + + LOG(INFO) << "Successfully registered uinput-sec-fp for fingerprint gestures"; +#endif + + set(TSP_CMD_PATH, "fod_enable,1,1,0"); +} + +BiometricsFingerprint::~BiometricsFingerprint() { + if (ss_fingerprint_close() != 0) { + LOG(ERROR) << "Can't close HAL module"; + } +} + +Return BiometricsFingerprint::isUdfps(uint32_t) { + return mIsUdfps; +} + +Return BiometricsFingerprint::onFingerDown(uint32_t, uint32_t, float, float) { + std::thread([this]() { + std::this_thread::sleep_for(std::chrono::milliseconds(35)); + set(HBM_PATH, "331"); + }).detach(); + + request(SEM_REQUEST_TOUCH_EVENT, FINGERPRINT_REQUEST_SESSION_OPEN); + + return Void(); +} + +Return BiometricsFingerprint::onFingerUp() { + request(SEM_REQUEST_TOUCH_EVENT, FINGERPRINT_REQUEST_RESUME); + + set(HBM_PATH, "0"); + + return Void(); +} + +Return BiometricsFingerprint::ErrorFilter(int32_t error) { + switch (error) { + case 0: + return RequestStatus::SYS_OK; + case -2: + return RequestStatus::SYS_ENOENT; + case -4: + return RequestStatus::SYS_EINTR; + case -5: + return RequestStatus::SYS_EIO; + case -11: + return RequestStatus::SYS_EAGAIN; + case -12: + return RequestStatus::SYS_ENOMEM; + case -13: + return RequestStatus::SYS_EACCES; + case -14: + return RequestStatus::SYS_EFAULT; + case -16: + return RequestStatus::SYS_EBUSY; + case -22: + return RequestStatus::SYS_EINVAL; + case -28: + return RequestStatus::SYS_ENOSPC; + case -110: + return RequestStatus::SYS_ETIMEDOUT; + default: + LOG(ERROR) << "An unknown error returned from fingerprint vendor library: " << error; + return RequestStatus::SYS_UNKNOWN; + } +} + +// Translate from errors returned by traditional HAL (see fingerprint.h) to +// HIDL-compliant FingerprintError. +FingerprintError BiometricsFingerprint::VendorErrorFilter(int32_t error, int32_t* vendorCode) { + *vendorCode = 0; + switch (error) { + case FINGERPRINT_ERROR_HW_UNAVAILABLE: + return FingerprintError::ERROR_HW_UNAVAILABLE; + case FINGERPRINT_ERROR_UNABLE_TO_PROCESS: + return FingerprintError::ERROR_UNABLE_TO_PROCESS; + case FINGERPRINT_ERROR_TIMEOUT: + return FingerprintError::ERROR_TIMEOUT; + case FINGERPRINT_ERROR_NO_SPACE: + return FingerprintError::ERROR_NO_SPACE; + case FINGERPRINT_ERROR_CANCELED: + return FingerprintError::ERROR_CANCELED; + case FINGERPRINT_ERROR_UNABLE_TO_REMOVE: + return FingerprintError::ERROR_UNABLE_TO_REMOVE; + case FINGERPRINT_ERROR_LOCKOUT: + return FingerprintError::ERROR_LOCKOUT; + default: + if (error >= FINGERPRINT_ERROR_VENDOR_BASE) { + // vendor specific code. + *vendorCode = error - FINGERPRINT_ERROR_VENDOR_BASE; + return FingerprintError::ERROR_VENDOR; + } + } + LOG(ERROR) << "Unknown error from fingerprint vendor library: " << error; + return FingerprintError::ERROR_UNABLE_TO_PROCESS; +} + +// Translate acquired messages returned by traditional HAL (see fingerprint.h) +// to HIDL-compliant FingerprintAcquiredInfo. +FingerprintAcquiredInfo BiometricsFingerprint::VendorAcquiredFilter(int32_t info, + int32_t* vendorCode) { + *vendorCode = 0; + switch (info) { + case FINGERPRINT_ACQUIRED_GOOD: + return FingerprintAcquiredInfo::ACQUIRED_GOOD; + case FINGERPRINT_ACQUIRED_PARTIAL: + return FingerprintAcquiredInfo::ACQUIRED_PARTIAL; + case FINGERPRINT_ACQUIRED_INSUFFICIENT: + return FingerprintAcquiredInfo::ACQUIRED_INSUFFICIENT; + case FINGERPRINT_ACQUIRED_IMAGER_DIRTY: + return FingerprintAcquiredInfo::ACQUIRED_IMAGER_DIRTY; + case FINGERPRINT_ACQUIRED_TOO_SLOW: + return FingerprintAcquiredInfo::ACQUIRED_TOO_SLOW; + case FINGERPRINT_ACQUIRED_TOO_FAST: + return FingerprintAcquiredInfo::ACQUIRED_TOO_FAST; + default: + if (info >= FINGERPRINT_ACQUIRED_VENDOR_BASE) { + // vendor specific code. + *vendorCode = info - FINGERPRINT_ACQUIRED_VENDOR_BASE; + return FingerprintAcquiredInfo::ACQUIRED_VENDOR; + } + } + LOG(ERROR) << "Unknown acquiredmsg from fingerprint vendor library: " << info; + return FingerprintAcquiredInfo::ACQUIRED_INSUFFICIENT; +} + +Return BiometricsFingerprint::setNotify( + const sp& clientCallback) { + std::lock_guard lock(mClientCallbackMutex); + mClientCallback = clientCallback; + // This is here because HAL 2.3 doesn't have a way to propagate a + // unique token for its driver. Subsequent versions should send a unique + // token for each call to setNotify(). This is fine as long as there's only + // one fingerprint device on the platform. + return reinterpret_cast(this); +} + +Return BiometricsFingerprint::preEnroll() { + return ss_fingerprint_pre_enroll(); +} + +Return BiometricsFingerprint::enroll(const hidl_array& hat, + uint32_t gid, uint32_t timeoutSec) { + const hw_auth_token_t* authToken = reinterpret_cast(hat.data()); + +#ifdef REQUEST_FORCE_CALIBRATE + request(SEM_REQUEST_FORCE_CBGE, 1); +#endif + + return ErrorFilter(ss_fingerprint_enroll(authToken, gid, timeoutSec)); +} + +Return BiometricsFingerprint::postEnroll() { + return ErrorFilter(ss_fingerprint_post_enroll()); +} + +Return BiometricsFingerprint::getAuthenticatorId() { + return ss_fingerprint_get_auth_id(); +} + +Return BiometricsFingerprint::cancel() { + int32_t ret = ss_fingerprint_cancel(); + +#ifdef CALL_NOTIFY_ON_CANCEL + if (ret == 0) { + fingerprint_msg_t msg{}; + msg.type = FINGERPRINT_ERROR; + msg.data.error = FINGERPRINT_ERROR_CANCELED; + notify(&msg); + } +#endif + + return ErrorFilter(ret); +} + +Return BiometricsFingerprint::enumerate() { + if (ss_fingerprint_enumerate != nullptr) { + return ErrorFilter(ss_fingerprint_enumerate()); + } + + return RequestStatus::SYS_UNKNOWN; +} + +Return BiometricsFingerprint::remove(uint32_t gid, uint32_t fid) { + return ErrorFilter(ss_fingerprint_remove(gid, fid)); +} + +Return BiometricsFingerprint::setActiveGroup(uint32_t gid, + const hidl_string& storePath) { + if (storePath.size() >= PATH_MAX || storePath.size() <= 0) { + LOG(ERROR) << "Bad path length: " << storePath.size(); + return RequestStatus::SYS_EINVAL; + } + + if (access(storePath.c_str(), W_OK)) { + return RequestStatus::SYS_EINVAL; + } + + return ErrorFilter(ss_fingerprint_set_active_group(gid, storePath.c_str())); +} + +Return BiometricsFingerprint::authenticate(uint64_t operationId, uint32_t gid) { + return ErrorFilter(ss_fingerprint_authenticate(operationId, gid)); +} + +IBiometricsFingerprint* BiometricsFingerprint::getInstance() { + if (!sInstance) { + sInstance = new BiometricsFingerprint(); + } + return sInstance; +} + +bool BiometricsFingerprint::openHal() { + void* handle = dlopen("libbauthserver.so", RTLD_NOW); + if (handle) { + int err; + + ss_fingerprint_close = + reinterpret_cast(dlsym(handle, "ss_fingerprint_close")); + ss_fingerprint_open = + reinterpret_cast(dlsym(handle, "ss_fingerprint_open")); + + ss_set_notify_callback = reinterpret_cast( + dlsym(handle, "ss_set_notify_callback")); + ss_fingerprint_pre_enroll = reinterpret_cast( + dlsym(handle, "ss_fingerprint_pre_enroll")); + ss_fingerprint_enroll = + reinterpret_cast(dlsym(handle, "ss_fingerprint_enroll")); + ss_fingerprint_post_enroll = reinterpret_cast( + dlsym(handle, "ss_fingerprint_post_enroll")); + ss_fingerprint_get_auth_id = reinterpret_cast( + dlsym(handle, "ss_fingerprint_get_auth_id")); + ss_fingerprint_cancel = + reinterpret_cast(dlsym(handle, "ss_fingerprint_cancel")); + ss_fingerprint_enumerate = reinterpret_cast( + dlsym(handle, "ss_fingerprint_enumerate")); + ss_fingerprint_remove = + reinterpret_cast(dlsym(handle, "ss_fingerprint_remove")); + ss_fingerprint_set_active_group = reinterpret_cast( + dlsym(handle, "ss_fingerprint_set_active_group")); + ss_fingerprint_authenticate = reinterpret_cast( + dlsym(handle, "ss_fingerprint_authenticate")); + ss_fingerprint_request = reinterpret_cast( + dlsym(handle, "ss_fingerprint_request")); + + if ((err = ss_fingerprint_open(nullptr)) != 0) { + LOG(ERROR) << "Can't open fingerprint, error: " << err; + return false; + } + + if ((err = ss_set_notify_callback(BiometricsFingerprint::notify)) != 0) { + LOG(ERROR) << "Can't register fingerprint module callback, error: " << err; + return false; + } + + return true; + } + + return false; +} + +void BiometricsFingerprint::notify(const fingerprint_msg_t* msg) { + BiometricsFingerprint* thisPtr = + static_cast(BiometricsFingerprint::getInstance()); + std::lock_guard lock(thisPtr->mClientCallbackMutex); + if (thisPtr == nullptr || thisPtr->mClientCallback == nullptr) { + LOG(ERROR) << "Receiving callbacks before the client callback is registered."; + return; + } + const uint64_t devId = 1; + switch (msg->type) { + case FINGERPRINT_ERROR: { + int32_t vendorCode = 0; + FingerprintError result = VendorErrorFilter(msg->data.error, &vendorCode); + LOG(DEBUG) << "onError(" << static_cast(result) << ")"; + if (!thisPtr->mClientCallback->onError(devId, result, vendorCode).isOk()) { + LOG(ERROR) << "failed to invoke fingerprint onError callback"; + } + getInstance()->onFingerUp(); + } break; + case FINGERPRINT_ACQUIRED: { + if (msg->data.acquired.acquired_info > SEM_FINGERPRINT_EVENT_BASE) { + thisPtr->handleEvent(msg->data.acquired.acquired_info); + return; + } + int32_t vendorCode = 0; + FingerprintAcquiredInfo result = + VendorAcquiredFilter(msg->data.acquired.acquired_info, &vendorCode); + LOG(DEBUG) << "onAcquired(" << static_cast(result) << ")"; + if (!thisPtr->mClientCallback->onAcquired(devId, result, vendorCode).isOk()) { + LOG(ERROR) << "failed to invoke fingerprint onAcquired callback"; + } + } break; + case FINGERPRINT_TEMPLATE_ENROLLING: +#ifdef USES_PERCENTAGE_SAMPLES + const_cast(msg)->data.enroll.samples_remaining = + 100 - msg->data.enroll.samples_remaining; +#endif + if(msg->data.enroll.samples_remaining == 0) { + set(HBM_PATH, "0"); +#ifdef CALL_CANCEL_ON_ENROLL_COMPLETION + thisPtr->ss_fingerprint_cancel(); +#endif + } + LOG(DEBUG) << "onEnrollResult(fid=" << msg->data.enroll.finger.fid + << ", gid=" << msg->data.enroll.finger.gid + << ", rem=" << msg->data.enroll.samples_remaining << ")"; + if (!thisPtr->mClientCallback + ->onEnrollResult(devId, msg->data.enroll.finger.fid, + msg->data.enroll.finger.gid, msg->data.enroll.samples_remaining) + .isOk()) { + LOG(ERROR) << "failed to invoke fingerprint onEnrollResult callback"; + } + break; + case FINGERPRINT_TEMPLATE_REMOVED: + LOG(DEBUG) << "onRemove(fid=" << msg->data.removed.finger.fid + << ", gid=" << msg->data.removed.finger.gid + << ", rem=" << msg->data.removed.remaining_templates << ")"; + if (!thisPtr->mClientCallback + ->onRemoved(devId, msg->data.removed.finger.fid, msg->data.removed.finger.gid, + msg->data.removed.remaining_templates) + .isOk()) { + LOG(ERROR) << "failed to invoke fingerprint onRemoved callback"; + } + break; + case FINGERPRINT_AUTHENTICATED: + LOG(DEBUG) << "onAuthenticated(fid=" << msg->data.authenticated.finger.fid + << ", gid=" << msg->data.authenticated.finger.gid << ")"; + if (msg->data.authenticated.finger.fid != 0) { + const uint8_t* hat = reinterpret_cast(&msg->data.authenticated.hat); + const hidl_vec token( + std::vector(hat, hat + sizeof(msg->data.authenticated.hat))); + set(HBM_PATH, "0"); + if (!thisPtr->mClientCallback + ->onAuthenticated(devId, msg->data.authenticated.finger.fid, + msg->data.authenticated.finger.gid, token) + .isOk()) { + LOG(ERROR) << "failed to invoke fingerprint onAuthenticated callback"; + } + } else { + // Not a recognized fingerprint + if (!thisPtr->mClientCallback + ->onAuthenticated(devId, msg->data.authenticated.finger.fid, + msg->data.authenticated.finger.gid, hidl_vec()) + .isOk()) { + LOG(ERROR) << "failed to invoke fingerprint onAuthenticated callback"; + } + } + break; + case FINGERPRINT_TEMPLATE_ENUMERATING: + LOG(DEBUG) << "onEnumerate(fid=" << msg->data.enumerated.finger.fid + << ", gid=" << msg->data.enumerated.finger.gid + << ", rem=" << msg->data.enumerated.remaining_templates << ")"; + if (!thisPtr->mClientCallback + ->onEnumerate(devId, msg->data.enumerated.finger.fid, + msg->data.enumerated.finger.gid, + msg->data.enumerated.remaining_templates) + .isOk()) { + LOG(ERROR) << "failed to invoke fingerprint onEnumerate callback"; + } + break; + } +} + +void BiometricsFingerprint::handleEvent(int eventCode) { + switch (eventCode) { +#ifdef HAS_FINGERPRINT_GESTURES + case SEM_FINGERPRINT_EVENT_GESTURE_SWIPE_DOWN: + case SEM_FINGERPRINT_EVENT_GESTURE_SWIPE_UP: + struct input_event event {}; + int keycode = eventCode == SEM_FINGERPRINT_EVENT_GESTURE_SWIPE_UP ? + KEY_UP : KEY_DOWN; + + // Report the key + event.type = EV_KEY; + event.code = keycode; + event.value = 1; + if (write(uinputFd, &event, sizeof(event)) < 0) { + LOG(ERROR) << "Write EV_KEY to uinput node failed"; + return; + } + + // Force a flush with an EV_SYN + event.type = EV_SYN; + event.code = SYN_REPORT; + event.value = 0; + if (write(uinputFd, &event, sizeof(event)) < 0) { + LOG(ERROR) << "Write EV_SYN to uinput node failed"; + return; + } + + // Report the key + event.type = EV_KEY; + event.code = keycode; + event.value = 0; + if (write(uinputFd, &event, sizeof(event)) < 0) { + LOG(ERROR) << "Write EV_KEY to uinput node failed"; + return; + } + + // Force a flush with an EV_SYN + event.type = EV_SYN; + event.code = SYN_REPORT; + event.value = 0; + if (write(uinputFd, &event, sizeof(event)) < 0) { + LOG(ERROR) << "Write EV_SYN to uinput node failed"; + return; + } + break; +#endif + } +} + +int BiometricsFingerprint::request(int cmd, int param) { + // TO-DO: input, output handling not implemented + int result = ss_fingerprint_request(cmd, nullptr, 0, nullptr, 0, param); + LOG(INFO) << "request(cmd=" << cmd << ", param=" << param << ", result=" << result << ")"; + return result; +} + +int BiometricsFingerprint::waitForSensor(std::chrono::milliseconds pollWait, + std::chrono::milliseconds timeOut) { + int sensorStatus = SEM_SENSOR_STATUS_WORKING; + std::chrono::milliseconds timeWaited = 0ms; + while (sensorStatus != SEM_SENSOR_STATUS_OK) { + if (sensorStatus == SEM_SENSOR_STATUS_CALIBRATION_ERROR + || sensorStatus == SEM_SENSOR_STATUS_ERROR){ + return -1; + } + if (timeWaited >= timeOut) { + return -2; + } + sensorStatus = request(FINGERPRINT_REQUEST_GET_SENSOR_STATUS, 0); + std::this_thread::sleep_for(pollWait); + timeWaited += pollWait; + } + return 0; +} + +} // namespace implementation +} // namespace V2_3 +} // namespace fingerprint +} // namespace biometrics +} // namespace hardware +} // namespace android diff --git a/fingerprint/BiometricsFingerprint.h b/fingerprint/BiometricsFingerprint.h new file mode 100644 index 0000000..aa860c1 --- /dev/null +++ b/fingerprint/BiometricsFingerprint.h @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2019-2022 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +#ifdef HAS_FINGERPRINT_GESTURES +#include +#endif + +#include +#include +#include +#include +#include +#include + +#include "VendorConstants.h" + +namespace android { +namespace hardware { +namespace biometrics { +namespace fingerprint { +namespace V2_3 { +namespace implementation { + +using namespace std::chrono_literals; + +using ::android::sp; +using ::android::hardware::hidl_string; +using ::android::hardware::hidl_vec; +using ::android::hardware::Return; +using ::android::hardware::Void; +using ::android::hardware::biometrics::fingerprint::V2_3::IBiometricsFingerprint; +using ::android::hardware::biometrics::fingerprint::V2_1::IBiometricsFingerprintClientCallback; +using ::android::hardware::biometrics::fingerprint::V2_1::FingerprintAcquiredInfo; +using ::android::hardware::biometrics::fingerprint::V2_1::FingerprintError; +using ::android::hardware::biometrics::fingerprint::V2_1::RequestStatus; + +struct BiometricsFingerprint : public IBiometricsFingerprint { + BiometricsFingerprint(); + ~BiometricsFingerprint(); + + // Method to wrap legacy HAL with BiometricsFingerprint class + static IBiometricsFingerprint* getInstance(); + + // Methods from ::android::hardware::biometrics::fingerprint::V2_3::IBiometricsFingerprint follow. + Return setNotify( + const sp& clientCallback) override; + Return preEnroll() override; + Return enroll(const hidl_array& hat, uint32_t gid, + uint32_t timeoutSec) override; + Return postEnroll() override; + Return getAuthenticatorId() override; + Return cancel() override; + Return enumerate() override; + Return remove(uint32_t gid, uint32_t fid) override; + Return setActiveGroup(uint32_t gid, const hidl_string& storePath) override; + Return authenticate(uint64_t operationId, uint32_t gid) override; + Return isUdfps(uint32_t sensorID) override; + Return onFingerDown(uint32_t x, uint32_t y, float minor, float major) override; + Return onFingerUp() override; + + private: + bool openHal(); + int request(int cmd, int param); + int waitForSensor(std::chrono::milliseconds pollWait, std::chrono::milliseconds timeOut); + static void notify( + const fingerprint_msg_t* msg); /* Static callback for legacy HAL implementation */ + void handleEvent(int eventCode); + static Return ErrorFilter(int32_t error); + static FingerprintError VendorErrorFilter(int32_t error, int32_t* vendorCode); + static FingerprintAcquiredInfo VendorAcquiredFilter(int32_t error, int32_t* vendorCode); + static BiometricsFingerprint* sInstance; + + std::mutex mClientCallbackMutex; + sp mClientCallback; + bool mIsUdfps; +#ifdef HAS_FINGERPRINT_GESTURES + int uinputFd; +#endif + + int (*ss_fingerprint_close)(); + int (*ss_fingerprint_open)(const char* id); + + int (*ss_set_notify_callback)(fingerprint_notify_t notify); + uint64_t (*ss_fingerprint_pre_enroll)(); + int (*ss_fingerprint_enroll)(const hw_auth_token_t* hat, uint32_t gid, uint32_t timeout_sec); + int (*ss_fingerprint_post_enroll)(); + uint64_t (*ss_fingerprint_get_auth_id)(); + int (*ss_fingerprint_cancel)(); + int (*ss_fingerprint_enumerate)(); + int (*ss_fingerprint_remove)(uint32_t gid, uint32_t fid); + int (*ss_fingerprint_set_active_group)(uint32_t gid, const char* store_path); + int (*ss_fingerprint_authenticate)(uint64_t operation_id, uint32_t gid); + int (*ss_fingerprint_request)(uint32_t cmd, char *inBuf, uint32_t inBuf_length, char *outBuf, uint32_t outBuf_length, uint32_t param); +}; + +} // namespace implementation +} // namespace V2_3 +} // namespace fingerprint +} // namespace biometrics +} // namespace hardware +} // namespace android diff --git a/fingerprint/UdfpsExtension.cpp b/fingerprint/UdfpsExtension.cpp new file mode 100644 index 0000000..8a3445d --- /dev/null +++ b/fingerprint/UdfpsExtension.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2022 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +uint32_t getUdfpsZOrder(uint32_t z, bool touched) { + (void) touched; + return z; +} + +uint64_t getUdfpsUsageBits(uint64_t usageBits, bool touched) { + if (touched) { + usageBits |= 0x400000000LL; + } + return usageBits; +} diff --git a/fingerprint/VendorConstants.h b/fingerprint/VendorConstants.h new file mode 100644 index 0000000..86656b3 --- /dev/null +++ b/fingerprint/VendorConstants.h @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (C) 2020 The LineageOS Project + +#pragma once + +// Fingerprint requests +#define FINGERPRINT_REQUEST_ENROLL_SESSION 1002 +#define FINGERPRINT_REQUEST_ENROLL_TYPE 18 +#define FINGERPRINT_REQUEST_ENUMERATE 11 +#define FINGERPRINT_REQUEST_GET_FP_IDS 1003 +#define FINGERPRINT_REQUEST_GET_MAX_TEMPLATE_NUMBER 1004 +#define FINGERPRINT_REQUEST_GET_SENSOR_INFO 5 +#define FINGERPRINT_REQUEST_GET_SENSOR_STATUS 6 +#define FINGERPRINT_REQUEST_GET_TOUCH_CNT 1007 +#define FINGERPRINT_REQUEST_GET_UNIQUE_ID 7 +#define FINGERPRINT_REQUEST_GET_USERIDS 12 +#define FINGERPRINT_REQUEST_GET_VERSION 4 +#define FINGERPRINT_REQUEST_HAS_FEATURE 1006 +#define FINGERPRINT_REQUEST_LOCKOUT 1001 +#define FINGERPRINT_REQUEST_NAVIGATION_LCD_ONOFF 17 +#define FINGERPRINT_REQUEST_NAVIGATION_MODE_END 16 +#define FINGERPRINT_REQUEST_NAVIGATION_MODE_START 15 +#define FINGERPRINT_REQUEST_PAUSE 0 +#define FINGERPRINT_REQUEST_PROCESS_FIDO 9 +#define FINGERPRINT_REQUEST_REMOVE_FINGER 1000 +#define FINGERPRINT_REQUEST_RESUME 1 +#define FINGERPRINT_REQUEST_SENSOR_TEST_NORMALSCAN 3 +#define FINGERPRINT_REQUEST_SESSION_OPEN 2 +#define FINGERPRINT_REQUEST_SET_ACTIVE_GROUP 8 +#define FINGERPRINT_REQUEST_UPDATE_SID 10 + +#define SEM_REQUEST_FORCE_CBGE 21 +#define SEM_REQUEST_GET_FINGER_ICON_REMAIN_TIME 1010 +#define SEM_REQUEST_GET_SECURITY_LEVEL 30 +#define SEM_REQUEST_GET_SENSOR_TEST_RESULT 19 +#define SEM_REQUEST_GET_TA_VERSION 10000 +#define SEM_REQUEST_GET_TSP_BLOCK_STATUS 0x3F9 +#define SEM_REQUEST_HIDE_INDISPLAY_AUTH_ANIMATION 0x3F4 +#define SEM_REQUEST_INSTALL_TA 10001 +#define SEM_REQUEST_IS_NEW_MATCHER 27 +#define SEM_REQUEST_IS_TEMPLATE_CHANGED 25 +#define SEM_REQUEST_MASK_CTL 0x3F5 +#define SEM_REQUEST_MOVE_INDISPLAY_ICON 0x3F3 +#define SEM_REQUEST_OPTICAL_CALIBRATION 0x3F8 +#define SEM_REQUEST_REMOVE_ALL_USER 0x3F6 +#define SEM_REQUEST_SET_ASP_LEVEL 20 +#define SEM_REQUEST_SET_BOUNCER_SCREEN_STATUS 0x3FA +#define SEM_REQUEST_SET_SCREEN_STATUS 0x3F0 +#define SEM_REQUEST_SHOW_INDISPLAY_AUTH_ANIMATION 1009 +#define SEM_REQUEST_TOUCH_EVENT 22 +#define SEM_REQUEST_TOUCH_SENSITIVE_CHANGE 0x3F7 +#define SEM_REQUEST_UPDATE_MATCHER 28 +#define SEM_REQUEST_VENDOR_EGIS_CALIBRATION 23 +#define SEM_REQUEST_VENDOR_QCOM_REMOVE_CBGE 24 +#define SEM_REQUEST_WIRELESS_CHARGER_STATUS 29 + +// Fingerprint aquired codes +#define SEM_FINGERPRINT_ACQUIRED_DUPLICATED_IMAGE 1002 +#define SEM_FINGERPRINT_ACQUIRED_LIGHT_TOUCH 1003 +#define SEM_FINGERPRINT_ACQUIRED_TSP_BLOCK 1004 +#define SEM_FINGERPRINT_ACQUIRED_TSP_UNBLOCK 1005 +#define SEM_FINGERPRINT_ACQUIRED_WET_FINGER 1001 + +// Fingerprint errors +#define SEM_FINGERPRINT_ERROR_CALIBRATION 1001 +#define SEM_FINGERPRINT_ERROR_DISABLED_BIOMETRICS 5002 +#define SEM_FINGERPRINT_ERROR_INVALID_HW 1005 +#define SEM_FINGERPRINT_ERROR_NEED_TO_RETRY 5000 +#define SEM_FINGERPRINT_ERROR_ONE_HAND_MODE 5001 +#define SEM_FINGERPRINT_ERROR_PATTERN_DETECTED 1007 +#define SEM_FINGERPRINT_ERROR_SERVICE_FAILURE 1003 +#define SEM_FINGERPRINT_ERROR_SMART_VIEW 5003 +#define SEM_FINGERPRINT_ERROR_SYSTEM_FAILURE 1002 +#define SEM_FINGERPRINT_ERROR_TA_UPDATE -100 +#define SEM_FINGERPRINT_ERROR_TEMPLATE_CORRUPTED 1004 +#define SEM_FINGERPRINT_ERROR_TEMPLATE_FORMAT_CHANGED 1006 +#define SEM_FINGERPRINT_ERROR_WIRELESS_CHARGING 5004 + +// Fingerprint events +#define SEM_FINGERPRINT_EVENT_BASE 10000 +#define SEM_FINGERPRINT_EVENT_CAPTURE_COMPLETED 10003 +#define SEM_FINGERPRINT_EVENT_CAPTURE_FAILED 10006 +#define SEM_FINGERPRINT_EVENT_CAPTURE_READY 10001 +#define SEM_FINGERPRINT_EVENT_CAPTURE_STARTED 10002 +#define SEM_FINGERPRINT_EVENT_CAPTURE_SUCCESS 10005 +#define SEM_FINGERPRINT_EVENT_FACTORY_SNSR_SCRIPT_END 10009 +#define SEM_FINGERPRINT_EVENT_FACTORY_SNSR_SCRIPT_START 10008 +#define SEM_FINGERPRINT_EVENT_FINGER_LEAVE 10004 +#define SEM_FINGERPRINT_EVENT_FINGER_LEAVE_TIMEOUT 10007 +#define SEM_FINGERPRINT_EVENT_GESTURE_DTAP 20003 +#define SEM_FINGERPRINT_EVENT_GESTURE_LPRESS 20004 +#define SEM_FINGERPRINT_EVENT_GESTURE_SWIPE_DOWN 20002 +#define SEM_FINGERPRINT_EVENT_GESTURE_SWIPE_UP 20001 +#define SEM_FINGERPRINT_EVENT_SPEN_CONTROL_OFF 30002 +#define SEM_FINGERPRINT_EVENT_SPEN_CONTROL_ON 30001 + +// Fingerprint sensor status codes +#define SEM_SENSOR_STATUS_CALIBRATION_ERROR 100045 +#define SEM_SENSOR_STATUS_ERROR 100042 +#define SEM_SENSOR_STATUS_OK 100040 +#define SEM_SENSOR_STATUS_WORKING 100041 diff --git a/fingerprint/android.hardware.biometrics.fingerprint@2.3-service-samsung.a32.rc b/fingerprint/android.hardware.biometrics.fingerprint@2.3-service-samsung.a32.rc new file mode 100644 index 0000000..c50dc1f --- /dev/null +++ b/fingerprint/android.hardware.biometrics.fingerprint@2.3-service-samsung.a32.rc @@ -0,0 +1,7 @@ +service vendor.fps_hal /vendor/bin/hw/android.hardware.biometrics.fingerprint@2.3-service-samsung.a32 + # "class hal" causes a race condition on some devices due to files created + # in /data. As a workaround, postpone startup until later in boot once + # /data is mounted. + class late_start + user system + group system input diff --git a/fingerprint/android.hardware.biometrics.fingerprint@2.3-service-samsung.a32.xml b/fingerprint/android.hardware.biometrics.fingerprint@2.3-service-samsung.a32.xml new file mode 100644 index 0000000..146077f --- /dev/null +++ b/fingerprint/android.hardware.biometrics.fingerprint@2.3-service-samsung.a32.xml @@ -0,0 +1,12 @@ + + + android.hardware.biometrics.fingerprint + hwbinder + 2.3 + + IBiometricsFingerprint + default + + @2.3::IBiometricsFingerprint/default + + diff --git a/fingerprint/service.cpp b/fingerprint/service.cpp new file mode 100644 index 0000000..2caad82 --- /dev/null +++ b/fingerprint/service.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2019 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include +#include + +#include "BiometricsFingerprint.h" + +using android::hardware::configureRpcThreadpool; +using android::hardware::joinRpcThreadpool; + +using android::hardware::biometrics::fingerprint::V2_3::IBiometricsFingerprint; +using android::hardware::biometrics::fingerprint::V2_3::implementation::BiometricsFingerprint; + +using android::OK; +using android::sp; + +int main() { + android::sp bio = BiometricsFingerprint::getInstance(); + + configureRpcThreadpool(1, true); + + if (bio == nullptr || bio->registerAsService() != OK) { + LOG(ERROR) << "Could not register service for Fingerprint HAL"; + goto shutdown; + } + + LOG(INFO) << "Fingerprint HAL service is Ready."; + joinRpcThreadpool(); + +shutdown: + // In normal operation, we don't expect the thread pool to shutdown + LOG(ERROR) << "Fingerprint HAL failed to join thread pool."; + return 1; +} diff --git a/proprietary-files.txt b/proprietary-files.txt index c04db80..416c436 100644 --- a/proprietary-files.txt +++ b/proprietary-files.txt @@ -895,26 +895,20 @@ vendor/lib/libfidoauthnr_v2.so vendor/lib64/libfidoauthnr_v2.so # Fingerprint -vendor/bin/hw/vendor.samsung.hardware.biometrics.fingerprint@3.0-service vendor/etc/init/fingerprint_common.rc vendor/etc/init/init.fingerprint.rc -vendor/etc/init/vendor.samsung.hardware.biometrics.fingerprint@3.0-service.rc --vendor/etc/vintf/manifest/vendor.samsung.hardware.biometrics.fingerprint@3.0-service.xml -vendor/lib/hw/fingerprint.default.so vendor/lib/libbauthserver.so vendor/lib/libbauthtzcommon.so vendor/lib/libegis_fp_normal_sensor_test.so vendor/lib/libgf_in_system_lib.so vendor/lib/libqfp_sensortest.so vendor/lib/libsynaFpSensorTestNwd.so -vendor/lib64/hw/fingerprint.default.so vendor/lib64/libbauthserver.so vendor/lib64/libbauthtzcommon.so vendor/lib64/libegis_fp_normal_sensor_test.so vendor/lib64/libgf_in_system_lib.so vendor/lib64/libqfp_sensortest.so vendor/lib64/libsynaFpSensorTestNwd.so -vendor/lib64/vendor.samsung.hardware.biometrics.fingerprint@3.0.so # Firmware vendor/firmware/nfc/sec_s3nrn4v_firmware.bin diff --git a/sepolicy/vendor/file_contexts b/sepolicy/vendor/file_contexts index 3076572..d84e876 100644 --- a/sepolicy/vendor/file_contexts +++ b/sepolicy/vendor/file_contexts @@ -26,7 +26,7 @@ /(vendor|system/vendor)/bin/vendor\.samsung\.hardware\.security\.engmode@1\.0-service u:object_r:hal_engmode_default_exec:s0 # Fingerprint -/(vendor|system/vendor)/bin/hw/vendor\.samsung\.hardware\.biometrics\.fingerprint@3\.0-service u:object_r:hal_fingerprint_default_exec:s0 +/(vendor|system/vendor)/bin/hw/android\.hardware\.biometrics\.fingerprint@[0-9]\.[0-9]-service-samsung\.a32 u:object_r:hal_fingerprint_default_exec:s0 /data/vendor/biometrics(/.*)? u:object_r:biometrics_vendor_data_file:s0 /dev/esfp[0-9] u:object_r:fp_sensor_device:s0 diff --git a/sepolicy/vendor/hwservice_contexts b/sepolicy/vendor/hwservice_contexts index 5bc1929..dd66af0 100644 --- a/sepolicy/vendor/hwservice_contexts +++ b/sepolicy/vendor/hwservice_contexts @@ -11,9 +11,6 @@ vendor.samsung.hardware.security.drk@2.0-service u:object_r:h # Engmode vendor.samsung.hardware.security.engmode::ISehEngmode u:object_r:hal_engmode_hwservice:s0 -# Fingerprint -vendor.samsung.hardware.biometrics.fingerprint::ISehBiometricsFingerprint u:object_r:hal_fingerprint_hwservice:s0 - # GNSS vendor.samsung.hardware.gnss::ISehGnss u:object_r:hal_gnss_hwservice:s0 diff --git a/udfps/Android.mk b/udfps/Android.mk new file mode 100644 index 0000000..2c4c85c --- /dev/null +++ b/udfps/Android.mk @@ -0,0 +1,20 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := optional + +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_PACKAGE_NAME := SamsungUdfpsHandler.a32 +LOCAL_CERTIFICATE := platform +LOCAL_PRIVATE_PLATFORM_APIS := true +LOCAL_PRIVILEGED_MODULE := true + +LOCAL_USE_AAPT2 := true + +ifneq ($(INCREMENTAL_BUILDS),) + LOCAL_PROGUARD_ENABLED := disabled + LOCAL_JACK_ENABLED := incremental +endif + +include $(BUILD_PACKAGE) diff --git a/udfps/AndroidManifest.xml b/udfps/AndroidManifest.xml new file mode 100644 index 0000000..1718085 --- /dev/null +++ b/udfps/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/udfps/src/android/hardware/display/BootCompletedReceiver.java b/udfps/src/android/hardware/display/BootCompletedReceiver.java new file mode 100644 index 0000000..6e399a2 --- /dev/null +++ b/udfps/src/android/hardware/display/BootCompletedReceiver.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.display; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +public class BootCompletedReceiver extends BroadcastReceiver { + + private static final boolean DEBUG = false; + private static final String TAG = "SamsungUdfpsHandler"; + + @Override + public void onReceive(final Context context, Intent intent) { + if (DEBUG) Log.d(TAG, "Received boot completed intent"); + Utils.startService(context); + } +} diff --git a/udfps/src/android/hardware/display/SamsungUdfpsHandlerService.java b/udfps/src/android/hardware/display/SamsungUdfpsHandlerService.java new file mode 100644 index 0000000..e3a8585 --- /dev/null +++ b/udfps/src/android/hardware/display/SamsungUdfpsHandlerService.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2022 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.display; + +import android.app.Service; +import android.content.Intent; +import android.hardware.display.ColorDisplayManager; +import android.os.SystemProperties; +import android.os.IBinder; +import android.util.Log; +import android.os.Handler; + +public class SamsungUdfpsHandlerService extends Service { + private static final String TAG = "SamsungUdfpsHandler"; + private static final boolean DEBUG = false; + + private int mNightDisplayTemp = 0; + private boolean mExtraDimActive = false; + + private ColorDisplayManager mColorDisplayManager; + private Handler mHandler; + private static Runnable mRunnable; + + private void disable() { + if (DEBUG) Log.d(TAG, "Finger down, set NightDisplay and ExtraDim off"); + + mNightDisplayTemp = mColorDisplayManager.getNightDisplayColorTemperature(); + + if (mColorDisplayManager.isNightDisplayActivated()) { + // 6500 Kelvin is AOSP's default white balance value + mColorDisplayManager.setNightDisplayColorTemperature(6500); + } + + if (mColorDisplayManager.isReduceBrightColorsActivated()) { + mExtraDimActive = true; + mColorDisplayManager.setReduceBrightColorsActivated(false); + } + + SystemProperties.set("vendor.finger.down", "0"); + } + + private void enable() { + if (DEBUG) Log.d(TAG, "Finger up, reset NightDisplay and ExtraDim to previous state"); + + SystemProperties.set("vendor.finger.down", "-1"); + + mColorDisplayManager.setNightDisplayColorTemperature(mNightDisplayTemp); + mColorDisplayManager.setReduceBrightColorsActivated(mExtraDimActive); + + mExtraDimActive = false; + } + + @Override + public void onCreate() { + if (DEBUG) Log.d(TAG, "SamsungUdfpsHandler Started"); + mColorDisplayManager = getSystemService(ColorDisplayManager.class); + + SystemProperties.set("vendor.finger.down", "-1"); + + mHandler = new Handler(); + mRunnable = new Runnable() { + public void run() { + if (DEBUG) Log.d(TAG, "onCreate: " + TAG + " is running"); + + if (SystemProperties.get("vendor.finger.down").equals("1")) { + disable(); + try { + Thread.sleep(2000); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + } else if (SystemProperties.get("vendor.finger.down").equals("0")) { + enable(); + } + mHandler.postDelayed(mRunnable, 100); + } + }; + + mHandler.postDelayed(mRunnable, 0); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + if (DEBUG) Log.d(TAG, "Starting service"); + return START_STICKY; + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } +} diff --git a/udfps/src/android/hardware/display/Utils.java b/udfps/src/android/hardware/display/Utils.java new file mode 100644 index 0000000..070d5f7 --- /dev/null +++ b/udfps/src/android/hardware/display/Utils.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2022 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.display; + +import android.content.Context; +import android.content.Intent; +import android.os.UserHandle; +import android.util.Log; + +public final class Utils { + + private static final String TAG = "UDFPSUtils"; + private static final boolean DEBUG = false; + + protected static void startService(Context context) { + if (DEBUG) Log.d(TAG, "Starting service"); + context.startServiceAsUser(new Intent(context, SamsungUdfpsHandlerService.class), + UserHandle.CURRENT); + } + + protected static void stopService(Context context) { + if (DEBUG) Log.d(TAG, "Stopping service"); + context.stopServiceAsUser(new Intent(context, SamsungUdfpsHandlerService.class), + UserHandle.CURRENT); + } +}