Require two samples to enroll an new fingerprint template

Settings app doesn't handle the case well when the fingerprint sensor only
needs one touch to finish enrolling. Instead, we change the fingerprint
HAL to require two touches.
The finger ID associated with the last touch would be the one that will be recorded.

Bug fix: Switched back to STATE_IDLE after completing enrollment.
Reference:
https://android.googlesource.com/platform/hardware/libhardware/+/master/include/hardware/fingerprint.h#164

Bug: 69594238
Change-Id: I39f0da23d6ffea6003d05a5e75b0048e60289fac
Signed-off-by: Weilun Du <wdu@google.com>
diff --git a/fingerprint/fingerprint.c b/fingerprint/fingerprint.c
index 0c63ff8..8b54c11 100644
--- a/fingerprint/fingerprint.c
+++ b/fingerprint/fingerprint.c
@@ -68,6 +68,7 @@
 typedef struct worker_thread_t {
     pthread_t thread;
     worker_state_t state;
+    int samples_remaining;
     uint64_t secureid[MAX_NUM_FINGERS];
     uint64_t fingerid[MAX_NUM_FINGERS];
     char fp_filename[PATH_MAX];
@@ -340,6 +341,7 @@
 
     pthread_mutex_lock(&dev->lock);
     dev->listener.state = STATE_ENROLL;
+    dev->listener.samples_remaining = 2;
     pthread_mutex_unlock(&dev->lock);
 
     // fingerprint id, authenticator id, and secure_user_id
@@ -621,26 +623,26 @@
         }
     }
     if (idx >= MAX_NUM_FINGERS) {
-        qdev->listener.state = STATE_SCAN;
+        qdev->listener.state = STATE_IDLE;
         pthread_mutex_unlock(&qdev->lock);
         ALOGD("Fingerprint ID table is full");
         return;
     }
-
-    qdev->listener.secureid[idx] = qdev->secure_user_id;
-    qdev->listener.fingerid[idx] = fid;
-    saveFingerprint(&qdev->listener, idx);
-
-    qdev->listener.state = STATE_SCAN;
+    qdev->listener.samples_remaining--;
+    int samples_remaining = qdev->listener.samples_remaining;
+    if (samples_remaining <= 0) {
+        qdev->listener.secureid[idx] = qdev->secure_user_id;
+        qdev->listener.fingerid[idx] = fid;
+        saveFingerprint(&qdev->listener, idx);
+        qdev->listener.state = STATE_IDLE;
+    }
     pthread_mutex_unlock(&qdev->lock);
-
     // LOCKED notification?
     fingerprint_msg_t msg = {0, {0}};
     msg.type = FINGERPRINT_TEMPLATE_ENROLLING;
     msg.data.enroll.finger.fid = fid;
-    msg.data.enroll.samples_remaining = 0;
+    msg.data.enroll.samples_remaining = samples_remaining > 0 ? samples_remaining : 0;
     qdev->device.notify(&msg);
-
     return;
 }