Phone: Display message to indicate PIN/PUK status changes.

Changes done to display retry counter on wrong entry of PIN1,
and message to indicate Code accepted/PIN1 blocked during
PIN1 verification as per certain carrier requirements.
PhoneApp: Add APIs that report actual error code on PIN/PUK operations
The current APIs that are used to verify the PIN and PUK only convey
whether the operation succeeded or failed. As a result on ANY failure
clients ask the user to re-enter the PIN.
Add 2 new APIs that report the actual error code in case of failure.

Bug: 9928717
Change-Id: I73718c9e6a8aa7244097e0dd4593a6226ff0ac08
diff --git a/src/com/android/phone/FdnSetting.java b/src/com/android/phone/FdnSetting.java
index 283d612..a471093 100644
--- a/src/com/android/phone/FdnSetting.java
+++ b/src/com/android/phone/FdnSetting.java
@@ -243,16 +243,25 @@
                 // a toast, or just update the UI.
                 case EVENT_PIN2_ENTRY_COMPLETE: {
                         AsyncResult ar = (AsyncResult) msg.obj;
-                        if (ar.exception != null) {
+                        if (ar.exception != null && ar.exception instanceof CommandException) {
+                            int attemptsRemaining = msg.arg1;
                             // see if PUK2 is requested and alert the user accordingly.
-                            CommandException ce = (CommandException) ar.exception;
-                            if (ce.getCommandError() == CommandException.Error.SIM_PUK2) {
-                                // make sure we set the PUK2 state so that we can skip
-                                // some redundant behaviour.
-                                displayMessage(R.string.fdn_enable_puk2_requested);
-                                resetPinChangeStateForPUK2();
-                            } else {
-                                displayMessage(R.string.pin2_invalid);
+                            CommandException.Error e =
+                                    ((CommandException) ar.exception).getCommandError();
+                            switch (e) {
+                                case SIM_PUK2:
+                                    // make sure we set the PUK2 state so that we can skip
+                                    // some redundant behaviour.
+                                    displayMessage(R.string.fdn_enable_puk2_requested,
+                                            attemptsRemaining);
+                                    resetPinChangeStateForPUK2();
+                                    break;
+                                case PASSWORD_INCORRECT:
+                                    displayMessage(R.string.pin2_invalid, attemptsRemaining);
+                                    break;
+                                default:
+                                    displayMessage(R.string.fdn_failed, attemptsRemaining);
+                                    break;
                             }
                         }
                         updateEnableFDN();
@@ -267,6 +276,9 @@
                             log("Handle EVENT_PIN2_CHANGE_COMPLETE");
                         AsyncResult ar = (AsyncResult) msg.obj;
                         if (ar.exception != null) {
+                            int attemptsRemaining = msg.arg1;
+                            log("Handle EVENT_PIN2_CHANGE_COMPLETE attemptsRemaining="
+                                    + attemptsRemaining);
                             CommandException ce = (CommandException) ar.exception;
                             if (ce.getCommandError() == CommandException.Error.SIM_PUK2) {
                                 // throw an alert dialog on the screen, displaying the
@@ -284,16 +296,21 @@
                                 // set the correct error message depending upon the state.
                                 // Reset the state depending upon or knowledge of the PUK state.
                                 if (!mIsPuk2Locked) {
-                                    displayMessage(R.string.badPin2);
+                                    displayMessage(R.string.badPin2, attemptsRemaining);
                                     resetPinChangeState();
                                 } else {
-                                    displayMessage(R.string.badPuk2);
+                                    displayMessage(R.string.badPuk2, attemptsRemaining);
                                     resetPinChangeStateForPUK2();
                                 }
                             }
                         } else {
+                            if (mPinChangeState == PIN_CHANGE_PUK) {
+                                displayMessage(R.string.pin2_unblocked);
+                            } else {
+                                displayMessage(R.string.pin2_changed);
+                            }
+
                             // reset to normal behaviour on successful change.
-                            displayMessage(R.string.pin2_changed);
                             resetPinChangeState();
                         }
                     }
@@ -315,9 +332,22 @@
     /**
      * Display a toast for message, like the rest of the settings.
      */
+    private final void displayMessage(int strId, int attemptsRemaining) {
+        String s = getString(strId);
+        if ((strId == R.string.badPin2) || (strId == R.string.badPuk2) ||
+                (strId == R.string.pin2_invalid)) {
+            if (attemptsRemaining >= 0) {
+                s = getString(strId) + getString(R.string.pin2_attempts, attemptsRemaining);
+            } else {
+                s = getString(strId);
+            }
+        }
+        log("displayMessage: attemptsRemaining=" + attemptsRemaining + " s=" + s);
+        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
+    }
+
     private final void displayMessage(int strId) {
-        Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT)
-            .show();
+        displayMessage(strId, -1);
     }
 
     /**