Unlock latency improvements

- Make sure the latency also gets tracked on first unlocked in
which we cancel the AsyncTask.
- Also add it for pattern authentication

Change-Id: Ie1561264b0a3b75c09819ccc6d269e61e367e1be
diff --git a/core/java/com/android/internal/widget/LockPatternChecker.java b/core/java/com/android/internal/widget/LockPatternChecker.java
index df9b0dd..3d43f5d 100644
--- a/core/java/com/android/internal/widget/LockPatternChecker.java
+++ b/core/java/com/android/internal/widget/LockPatternChecker.java
@@ -29,6 +29,11 @@
          * the call. Only non-0 if matched is false.
          */
         void onChecked(boolean matched, int throttleTimeoutMs);
+
+        /**
+         * Called when the underlying AsyncTask was cancelled.
+         */
+        default void onCancelled() {}
     }
 
     /**
@@ -110,6 +115,11 @@
             protected void onPostExecute(Boolean result) {
                 callback.onChecked(result, mThrottleTimeout);
             }
+
+            @Override
+            protected void onCancelled() {
+                callback.onCancelled();
+            }
         };
         task.execute();
         return task;
@@ -217,6 +227,11 @@
             protected void onPostExecute(Boolean result) {
                 callback.onChecked(result, mThrottleTimeout);
             }
+
+            @Override
+            protected void onCancelled() {
+                callback.onCancelled();
+            }
         };
         task.execute();
         return task;
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardAbsKeyInputView.java b/packages/Keyguard/src/com/android/keyguard/KeyguardAbsKeyInputView.java
index 1a61f7a..4330573 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardAbsKeyInputView.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardAbsKeyInputView.java
@@ -168,6 +168,16 @@
                                     true /* isValidPassword */);
                         }
                     }
+
+                    @Override
+                    public void onCancelled() {
+                        // We already got dismissed with the early matched callback, so we cancelled
+                        // the check. However, we still need to note down the latency.
+                        if (LatencyTracker.isEnabled(mContext)) {
+                            LatencyTracker.getInstance(mContext).onActionEnd(
+                                    ACTION_CHECK_CREDENTIAL_UNLOCKED);
+                        }
+                    }
                 });
     }
 
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardPatternView.java b/packages/Keyguard/src/com/android/keyguard/KeyguardPatternView.java
index 7d1a6fb..a9ce197 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardPatternView.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardPatternView.java
@@ -15,6 +15,9 @@
  */
 package com.android.keyguard;
 
+import static com.android.keyguard.LatencyTracker.ACTION_CHECK_CREDENTIAL;
+import static com.android.keyguard.LatencyTracker.ACTION_CHECK_CREDENTIAL_UNLOCKED;
+
 import android.content.Context;
 import android.graphics.Rect;
 import android.os.AsyncTask;
@@ -242,6 +245,10 @@
                 return;
             }
 
+            if (LatencyTracker.isEnabled(mContext)) {
+                LatencyTracker.getInstance(mContext).onActionStart(ACTION_CHECK_CREDENTIAL);
+                LatencyTracker.getInstance(mContext).onActionStart(ACTION_CHECK_CREDENTIAL_UNLOCKED);
+            }
             mPendingLockCheck = LockPatternChecker.checkPattern(
                     mLockPatternUtils,
                     pattern,
@@ -250,12 +257,20 @@
 
                         @Override
                         public void onEarlyMatched() {
+                            if (LatencyTracker.isEnabled(mContext)) {
+                                LatencyTracker.getInstance(mContext).onActionEnd(
+                                        ACTION_CHECK_CREDENTIAL);
+                            }
                             onPatternChecked(userId, true /* matched */, 0 /* timeoutMs */,
                                     true /* isValidPattern */);
                         }
 
                         @Override
                         public void onChecked(boolean matched, int timeoutMs) {
+                            if (LatencyTracker.isEnabled(mContext)) {
+                                LatencyTracker.getInstance(mContext).onActionEnd(
+                                        ACTION_CHECK_CREDENTIAL_UNLOCKED);
+                            }
                             mLockPatternView.enableInput();
                             mPendingLockCheck = null;
                             if (!matched) {
@@ -263,6 +278,16 @@
                                         true /* isValidPattern */);
                             }
                         }
+
+                        @Override
+                        public void onCancelled() {
+                            // We already got dismissed with the early matched callback, so we
+                            // cancelled the check. However, we still need to note down the latency.
+                            if (LatencyTracker.isEnabled(mContext)) {
+                                LatencyTracker.getInstance(mContext).onActionEnd(
+                                        ACTION_CHECK_CREDENTIAL_UNLOCKED);
+                            }
+                        }
                     });
             if (pattern.size() > MIN_PATTERN_BEFORE_POKE_WAKELOCK) {
                 mCallback.userActivity();