Merge "Handle fp enrollment errors more gracefully - When finger can't be analyzed for enrollment (FINGERPRINT_ERROR_UNABLE_TO_PROCESS), tell the user to try again or use a different finger. - When timeout is reached (FINGERPRINT_ERROR_TIMEOUT), stop enrollment and ask the user to try again." into mnc-dr-dev
diff --git a/res/values/strings.xml b/res/values/strings.xml
index f693b71..cc78bd9 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -772,6 +772,12 @@
     <string name="security_settings_fingerprint_enroll_touch_dialog_title">Whoops, that\u2019s not the sensor</string>
     <!-- Dialog message for dialog which shows when user touches the icon on the screen, instead of the sensor at the back [CHAR LIMIT=45] -->
     <string name="security_settings_fingerprint_enroll_touch_dialog_message">Use the fingerprint sensor on your device.</string>
+    <!-- Dialog message for dialog which shows when finger cannot be enrolled. [CHAR LIMIT=45] -->
+    <string name="security_settings_fingerprint_enroll_error_dialog_title">Enrollment was not completed</string>
+    <!-- Dialog message for dialog which shows when finger cannot be enrolled due to being idle too long. -->
+    <string name="security_settings_fingerprint_enroll_error_timeout_dialog_message">Fingerprint enrollment time limit reached. Try again.</string>
+    <!-- Dialog message for dialog which shows when finger cannot be enrolled due to an internal error or fingerprint can't be read. -->
+    <string name="security_settings_fingerprint_enroll_error_generic_dialog_message">Fingerprint enrollment didn\'t work. Try again or use a different finger.</string>
     <!-- Button text shown at the end of enrollment that allows the user to add another fingerprint -->
     <string name="fingerprint_enroll_button_add">Add another</string>
     <!-- Button text shown at the end of enrollment that allows the user to move to the next step -->
diff --git a/src/com/android/settings/fingerprint/FingerprintEnrollBase.java b/src/com/android/settings/fingerprint/FingerprintEnrollBase.java
index 781efa3..d164800 100644
--- a/src/com/android/settings/fingerprint/FingerprintEnrollBase.java
+++ b/src/com/android/settings/fingerprint/FingerprintEnrollBase.java
@@ -38,23 +38,9 @@
  */
 public abstract class FingerprintEnrollBase extends InstrumentedActivity
         implements View.OnClickListener {
-
-    /**
-     * Used by the choose fingerprint wizard to indicate the wizard is
-     * finished, and each activity in the wizard should finish.
-     * <p>
-     * Previously, each activity in the wizard would finish itself after
-     * starting the next activity. However, this leads to broken 'Back'
-     * behavior. So, now an activity does not finish itself until it gets this
-     * result.
-     */
-    protected static final int RESULT_FINISHED = RESULT_FIRST_USER;
-
-    /**
-     * Used by the enrolling screen during setup wizard to skip over setting up fingerprint, which
-     * will be useful if the user accidentally entered this flow.
-     */
-    protected static final int RESULT_SKIP = RESULT_FIRST_USER + 1;
+    static final int RESULT_FINISHED = FingerprintSettings.RESULT_FINISHED;
+    static final int RESULT_SKIP = FingerprintSettings.RESULT_SKIP;
+    static final int RESULT_TIMEOUT = FingerprintSettings.RESULT_TIMEOUT;
 
     protected byte[] mToken;
 
diff --git a/src/com/android/settings/fingerprint/FingerprintEnrollEnrolling.java b/src/com/android/settings/fingerprint/FingerprintEnrollEnrolling.java
index 3d2ce4d..fe0bb63 100644
--- a/src/com/android/settings/fingerprint/FingerprintEnrollEnrolling.java
+++ b/src/com/android/settings/fingerprint/FingerprintEnrollEnrolling.java
@@ -20,6 +20,7 @@
 import android.animation.AnimatorListenerAdapter;
 import android.animation.ObjectAnimator;
 import android.animation.ValueAnimator;
+import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.app.DialogFragment;
@@ -29,6 +30,7 @@
 import android.graphics.drawable.Animatable2;
 import android.graphics.drawable.AnimatedVectorDrawable;
 import android.graphics.drawable.Drawable;
+import android.hardware.fingerprint.FingerprintManager;
 import android.os.Bundle;
 import android.view.MotionEvent;
 import android.view.View;
@@ -245,9 +247,22 @@
     }
 
     @Override
-    public void onEnrollmentError(CharSequence errString) {
-        showError(errString);
+    public void onEnrollmentError(int errMsgId, CharSequence errString) {
+        int msgId;
+        switch (errMsgId) {
+            case FingerprintManager.FINGERPRINT_ERROR_TIMEOUT:
+                // This message happens when the underlying crypto layer decides to revoke the
+                // enrollment auth token.
+                msgId = R.string.security_settings_fingerprint_enroll_error_timeout_dialog_message;
+                break;
+            default:
+                // There's nothing specific to tell the user about. Ask them to try again.
+                msgId = R.string.security_settings_fingerprint_enroll_error_generic_dialog_message;
+                break;
+        }
+        showErrorDialog(getText(msgId), errMsgId);
         stopIconAnimation();
+        mErrorText.removeCallbacks(mTouchAgainRunnable);
     }
 
     @Override
@@ -278,6 +293,11 @@
         return PROGRESS_BAR_MAX * progress / (steps + 1);
     }
 
+    private void showErrorDialog(CharSequence msg, int msgId) {
+        ErrorDialog dlg = ErrorDialog.newInstance(msg, msgId);
+        dlg.show(getFragmentManager(), ErrorDialog.class.getName());
+    }
+
     private void showIconTouchDialog() {
         mIconTouchCount = 0;
         new IconTouchDialog().show(getFragmentManager(), null /* tag */);
@@ -403,4 +423,49 @@
             return builder.create();
         }
     }
+
+    public static class ErrorDialog extends DialogFragment {
+
+        /**
+         * Create a new instance of ErrorDialog.
+         *
+         * @param msg the string to show for message text
+         * @param msgId the FingerprintManager error id so we know the cause
+         * @return a new ErrorDialog
+         */
+        static ErrorDialog newInstance(CharSequence msg, int msgId) {
+            ErrorDialog dlg = new ErrorDialog();
+            Bundle args = new Bundle();
+            args.putCharSequence("error_msg", msg);
+            args.putInt("error_id", msgId);
+            dlg.setArguments(args);
+            return dlg;
+        }
+
+        @Override
+        public Dialog onCreateDialog(Bundle savedInstanceState) {
+            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
+            CharSequence errorString = getArguments().getCharSequence("error_msg");
+            final int errMsgId = getArguments().getInt("error_id");
+            builder.setTitle(R.string.security_settings_fingerprint_enroll_error_dialog_title)
+                    .setMessage(errorString)
+                    .setCancelable(false)
+                    .setPositiveButton(R.string.security_settings_fingerprint_enroll_dialog_ok,
+                            new DialogInterface.OnClickListener() {
+                                @Override
+                                public void onClick(DialogInterface dialog, int which) {
+                                    dialog.dismiss();
+                                    boolean wasTimeout =
+                                        errMsgId == FingerprintManager.FINGERPRINT_ERROR_TIMEOUT;
+                                    Activity activity = getActivity();
+                                    activity.setResult(wasTimeout ?
+                                            RESULT_TIMEOUT : RESULT_FINISHED);
+                                    activity.finish();
+                                }
+                            });
+            AlertDialog dialog = builder.create();
+            dialog.setCanceledOnTouchOutside(false);
+            return dialog;
+        }
+    }
 }
diff --git a/src/com/android/settings/fingerprint/FingerprintEnrollFindSensor.java b/src/com/android/settings/fingerprint/FingerprintEnrollFindSensor.java
index 8864822..a6b0dca 100644
--- a/src/com/android/settings/fingerprint/FingerprintEnrollFindSensor.java
+++ b/src/com/android/settings/fingerprint/FingerprintEnrollFindSensor.java
@@ -80,6 +80,9 @@
             } else if (resultCode == RESULT_SKIP) {
                 setResult(RESULT_SKIP);
                 finish();
+            } else if (resultCode == RESULT_TIMEOUT) {
+                setResult(RESULT_TIMEOUT);
+                finish();
             } else {
                 FingerprintManager fpm = getSystemService(FingerprintManager.class);
                 int enrolled = fpm.getEnrolledFingerprints().size();
diff --git a/src/com/android/settings/fingerprint/FingerprintEnrollSidecar.java b/src/com/android/settings/fingerprint/FingerprintEnrollSidecar.java
index 5b677f8..5feb08c 100644
--- a/src/com/android/settings/fingerprint/FingerprintEnrollSidecar.java
+++ b/src/com/android/settings/fingerprint/FingerprintEnrollSidecar.java
@@ -130,7 +130,7 @@
         @Override
         public void onEnrollmentError(int errMsgId, CharSequence errString) {
             if (mListener != null) {
-                mListener.onEnrollmentError(errString);
+                mListener.onEnrollmentError(errMsgId, errString);
             }
         }
     };
@@ -149,7 +149,7 @@
 
     public interface Listener {
         void onEnrollmentHelp(CharSequence helpString);
-        void onEnrollmentError(CharSequence errString);
+        void onEnrollmentError(int errMsgId, CharSequence errString);
         void onEnrollmentProgressChange(int steps, int remaining);
     }
 }
diff --git a/src/com/android/settings/fingerprint/FingerprintSettings.java b/src/com/android/settings/fingerprint/FingerprintSettings.java
index 8e63a51..cacd5dd 100644
--- a/src/com/android/settings/fingerprint/FingerprintSettings.java
+++ b/src/com/android/settings/fingerprint/FingerprintSettings.java
@@ -70,8 +70,9 @@
  * Settings screen for fingerprints
  */
 public class FingerprintSettings extends SubSettings {
+
     /**
-     * Used by the FP settings wizard to indicate the wizard is
+     * Used by the choose fingerprint wizard to indicate the wizard is
      * finished, and each activity in the wizard should finish.
      * <p>
      * Previously, each activity in the wizard would finish itself after
@@ -79,7 +80,21 @@
      * behavior. So, now an activity does not finish itself until it gets this
      * result.
      */
-    static final int RESULT_FINISHED = RESULT_FIRST_USER;
+    protected static final int RESULT_FINISHED = RESULT_FIRST_USER;
+
+    /**
+     * Used by the enrolling screen during setup wizard to skip over setting up fingerprint, which
+     * will be useful if the user accidentally entered this flow.
+     */
+    protected static final int RESULT_SKIP = RESULT_FIRST_USER + 1;
+
+    /**
+     * Like {@link #RESULT_FINISHED} except this one indicates enrollment failed because the
+     * device was left idle. This is used to clear the credential token to require the user to
+     * re-enter their pin/pattern/password before continuing.
+     */
+    protected static final int RESULT_TIMEOUT = RESULT_FIRST_USER + 2;
+
     private static final long LOCKOUT_DURATION = 30000; // time we have to wait for fp to reset, ms
 
     @Override
@@ -441,6 +456,12 @@
                                 ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN);
                     }
                 }
+            } else if (requestCode == ADD_FINGERPRINT_REQUEST) {
+                if (resultCode == RESULT_TIMEOUT) {
+                    Activity activity = getActivity();
+                    activity.setResult(RESULT_TIMEOUT);
+                    activity.finish();
+                }
             }
 
             if (mToken == null) {