Merge "Fix crash from battery saver switch" into nyc-dev
diff --git a/src/com/android/settings/CredentialStorage.java b/src/com/android/settings/CredentialStorage.java
index 6cdf0b9..eed380b 100644
--- a/src/com/android/settings/CredentialStorage.java
+++ b/src/com/android/settings/CredentialStorage.java
@@ -103,16 +103,11 @@
     static final int MIN_PASSWORD_QUALITY = DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
 
     private static final int CONFIRM_KEY_GUARD_REQUEST = 1;
+    private static final int CONFIRM_CLEAR_SYSTEM_CREDENTIAL_REQUEST = 2;
 
     private final KeyStore mKeyStore = KeyStore.getInstance();
 
     /**
-     * The UIDs that are used for system credential storage in keystore.
-     */
-    private static final int[] SYSTEM_CREDENTIAL_UIDS = {Process.WIFI_UID, Process.VPN_UID,
-        Process.ROOT_UID, Process.SYSTEM_UID};
-
-    /**
      * When non-null, the bundle containing credentials to install.
      */
     private Bundle mInstallBundle;
@@ -197,7 +192,7 @@
             return;
         }
         // force key guard confirmation
-        if (confirmKeyGuard()) {
+        if (confirmKeyGuard(CONFIRM_KEY_GUARD_REQUEST)) {
             // will return password value via onActivityResult
             return;
         }
@@ -328,8 +323,10 @@
         @Override public void onDismiss(DialogInterface dialog) {
             if (mResetConfirmed) {
                 mResetConfirmed = false;
-                new ResetKeyStoreAndKeyChain().execute();
-                return;
+                if (confirmKeyGuard(CONFIRM_CLEAR_SYSTEM_CREDENTIAL_REQUEST)) {
+                    // will return password value via onActivityResult
+                    return;
+                }
             }
             finish();
         }
@@ -343,12 +340,7 @@
         @Override protected Boolean doInBackground(Void... unused) {
 
             // Clear all the users credentials could have been installed in for this user.
-            final UserManager um = (UserManager) getSystemService(USER_SERVICE);
-            for (int userId : um.getProfileIdsWithDisabled(UserHandle.myUserId())) {
-                for (int uid : SYSTEM_CREDENTIAL_UIDS) {
-                    mKeyStore.clearUid(UserHandle.getUid(userId, uid));
-                }
-            }
+            new LockPatternUtils(CredentialStorage.this).resetKeyStore(UserHandle.myUserId());
 
             try {
                 KeyChainConnection keyChainConnection = KeyChain.bind(CredentialStorage.this);
@@ -454,10 +446,10 @@
     /**
      * Confirm existing key guard, returning password via onActivityResult.
      */
-    private boolean confirmKeyGuard() {
+    private boolean confirmKeyGuard(int requestCode) {
         Resources res = getResources();
         boolean launched = new ChooseLockSettingsHelper(this)
-                .launchConfirmationActivity(CONFIRM_KEY_GUARD_REQUEST,
+                .launchConfirmationActivity(requestCode,
                         res.getText(R.string.credentials_title), true);
         return launched;
     }
@@ -481,6 +473,13 @@
             }
             // failed confirmation, bail
             finish();
+        } else if (requestCode == CONFIRM_CLEAR_SYSTEM_CREDENTIAL_REQUEST) {
+            if (resultCode == Activity.RESULT_OK) {
+                new ResetKeyStoreAndKeyChain().execute();
+                return;
+            }
+            // failed confirmation, bail
+            finish();
         }
     }