aidl: fingerprint: When force calibrating wait for sensor to become ready

* Otherwise on first enrollment user might get an error dialog if
  the sensor is used too early

Change-Id: If526bbe6d8dc355b85b828dc6ff33db4057cdd5c
diff --git a/aidl/fingerprint/Fingerprint.cpp b/aidl/fingerprint/Fingerprint.cpp
index 4ca160d..21d16f5 100644
--- a/aidl/fingerprint/Fingerprint.cpp
+++ b/aidl/fingerprint/Fingerprint.cpp
@@ -174,7 +174,7 @@
 void Fingerprint::handleEvent(int eventCode) {
     switch (eventCode) {
         case SEM_FINGERPRINT_EVENT_GESTURE_SWIPE_DOWN:
-        case SEM_FINGERPRINT_EVENT_GESTURE_SWIPE_UP:
+        case SEM_FINGERPRINT_EVENT_GESTURE_SWIPE_UP: {
             if (!mSupportsGestures) return;
 
             struct input_event event {};
@@ -216,7 +216,12 @@
                 LOG(ERROR) << "Write EV_SYN to uinput node failed";
                 return;
             }
-        break;
+        } break;
+        case SEM_FINGERPRINT_EVENT_CAPTURE_READY: {
+            if (mSession != nullptr && !mSession->isClosed()) {
+                mSession->onCaptureReady();
+            }
+        } break;
     }
 }
 
diff --git a/aidl/fingerprint/Session.cpp b/aidl/fingerprint/Session.cpp
index e5429f6..aa4bdae 100644
--- a/aidl/fingerprint/Session.cpp
+++ b/aidl/fingerprint/Session.cpp
@@ -17,6 +17,7 @@
 #include <thread>
 
 using namespace ::android::fingerprint::samsung;
+using namespace ::std::chrono_literals;
 
 namespace aidl {
 namespace android {
@@ -68,6 +69,7 @@
     LOG(INFO) << "enroll";
 
     if (FingerprintHalProperties::force_calibrate().value_or(false)) {
+        mCaptureReady = false;
         mHal.request(SEM_REQUEST_FORCE_CBGE, 1);
     }
 
@@ -80,6 +82,12 @@
         mCb->onError(Error::UNABLE_TO_PROCESS, error);
     }
 
+    if (FingerprintHalProperties::force_calibrate().value_or(false)) {
+        while (!mCaptureReady) {
+            std::this_thread::sleep_for(100ms);
+        }
+    }
+
     *out = SharedRefBase::make<CancellationSignal>(this);
     return ndk::ScopedAStatus::ok();
 }
@@ -422,6 +430,10 @@
     }
 }
 
+void Session::onCaptureReady() {
+    mCaptureReady = true;
+}
+
 } // namespace fingerprint
 } // namespace biometrics
 } // namespace hardware
diff --git a/aidl/fingerprint/Session.h b/aidl/fingerprint/Session.h
index 15dfdcb..dc3bd3d 100644
--- a/aidl/fingerprint/Session.h
+++ b/aidl/fingerprint/Session.h
@@ -69,11 +69,13 @@
     bool isClosed();
     void notify(
         const fingerprint_msg_t* msg);
+    void onCaptureReady();
 
 private:
     LegacyHAL mHal;
     LockoutTracker mLockoutTracker;
     bool mClosed = false;
+    bool mCaptureReady = false;
 
     Error VendorErrorFilter(int32_t error, int32_t* vendorCode);
     AcquiredInfo VendorAcquiredFilter(int32_t info, int32_t* vendorCode);