Merge "Allows the user to go back to Encryption Screen"
diff --git a/src/com/android/settings/ChooseLockGeneric.java b/src/com/android/settings/ChooseLockGeneric.java
index 65f0ba6..114637c 100644
--- a/src/com/android/settings/ChooseLockGeneric.java
+++ b/src/com/android/settings/ChooseLockGeneric.java
@@ -220,13 +220,17 @@
                     && !dpm.getDoNotAskCredentialsOnBoot()) {
                 mEncryptionRequestQuality = quality;
                 mEncryptionRequestDisabled = disabled;
+                // Get the intent that the encryption interstitial should start for creating
+                // the new unlock method.
+                Intent unlockMethodIntent = getIntentForUnlockMethod(quality, disabled);
                 final Context context = getActivity();
                 // If accessibility is enabled and the user hasn't seen this dialog before, set the
                 // default state to agree with that which is compatible with accessibility
                 // (password not required).
                 final boolean accEn = AccessibilityManager.getInstance(context).isEnabled();
                 final boolean required = mLockPatternUtils.isCredentialRequiredToDecrypt(!accEn);
-                Intent intent = getEncryptionInterstitialIntent(context, quality, required);
+                Intent intent = getEncryptionInterstitialIntent(context, quality, required,
+                        unlockMethodIntent);
                 intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT,
                         mForFingerprint);
                 startActivityForResult(intent, ENABLE_ENCRYPTION_REQUEST);
@@ -246,9 +250,8 @@
                 updatePreferencesOrFinish();
             } else if (requestCode == ENABLE_ENCRYPTION_REQUEST
                     && resultCode == Activity.RESULT_OK) {
-                mRequirePassword = data.getBooleanExtra(
-                        EncryptionInterstitial.EXTRA_REQUIRE_PASSWORD, true);
-                updateUnlockMethodAndFinish(mEncryptionRequestQuality, mEncryptionRequestDisabled);
+                getActivity().setResult(resultCode, data);
+                finish();
             } else if (requestCode == CHOOSE_LOCK_REQUEST) {
                 getActivity().setResult(resultCode, data);
                 finish();
@@ -451,8 +454,9 @@
         }
 
         protected Intent getEncryptionInterstitialIntent(Context context, int quality,
-                boolean required) {
-            return EncryptionInterstitial.createStartIntent(context, quality, required);
+                boolean required, Intent unlockMethodIntent) {
+            return EncryptionInterstitial.createStartIntent(context, quality, required,
+                    unlockMethodIntent);
         }
 
         /**
@@ -471,34 +475,13 @@
             }
 
             quality = upgradeQuality(quality);
+            Intent intent = getIntentForUnlockMethod(quality, disabled);
+            if (intent != null) {
+                startActivityForResult(intent, CHOOSE_LOCK_REQUEST);
+                return;
+            }
 
-            final Context context = getActivity();
-            if (quality >= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC) {
-                int minLength = mDPM.getPasswordMinimumLength(null);
-                if (minLength < MIN_PASSWORD_LENGTH) {
-                    minLength = MIN_PASSWORD_LENGTH;
-                }
-                final int maxLength = mDPM.getPasswordMaximumLength(quality);
-                Intent intent;
-                if (mHasChallenge) {
-                    intent = getLockPasswordIntent(context, quality, minLength,
-                            maxLength, mRequirePassword, mChallenge, mUserId);
-                } else {
-                    intent = getLockPasswordIntent(context, quality, minLength,
-                        maxLength, mRequirePassword, mUserPassword, mUserId);
-                }
-                startActivityForResult(intent, CHOOSE_LOCK_REQUEST);
-            } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) {
-                Intent intent;
-                if (mHasChallenge) {
-                    intent = getLockPatternIntent(context, mRequirePassword,
-                        mChallenge, mUserId);
-                } else {
-                    intent = getLockPatternIntent(context, mRequirePassword,
-                        mUserPassword, mUserId);
-                }
-                startActivityForResult(intent, CHOOSE_LOCK_REQUEST);
-            } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
+            if (quality == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
                 mChooseLockSettingsHelper.utils().clearLock(mUserId);
                 mChooseLockSettingsHelper.utils().setLockScreenDisabled(disabled, mUserId);
                 removeAllFingerprintTemplatesAndFinish();
@@ -508,6 +491,34 @@
             }
         }
 
+        private Intent getIntentForUnlockMethod(int quality, boolean disabled) {
+            Intent intent = null;
+            final Context context = getActivity();
+            if (quality >= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC) {
+                int minLength = mDPM.getPasswordMinimumLength(null);
+                if (minLength < MIN_PASSWORD_LENGTH) {
+                    minLength = MIN_PASSWORD_LENGTH;
+                }
+                final int maxLength = mDPM.getPasswordMaximumLength(quality);
+                if (mHasChallenge) {
+                    intent = getLockPasswordIntent(context, quality, minLength,
+                            maxLength, mRequirePassword, mChallenge, mUserId);
+                } else {
+                    intent = getLockPasswordIntent(context, quality, minLength,
+                            maxLength, mRequirePassword, mUserPassword, mUserId);
+                }
+            } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) {
+                if (mHasChallenge) {
+                    intent = getLockPatternIntent(context, mRequirePassword,
+                            mChallenge, mUserId);
+                } else {
+                    intent = getLockPatternIntent(context, mRequirePassword,
+                            mUserPassword, mUserId);
+                }
+            }
+            return intent;
+        }
+
         private void removeAllFingerprintTemplatesAndFinish() {
             if (mFingerprintManager != null && mFingerprintManager.isHardwareDetected()
                     && mFingerprintManager.getEnrolledFingerprints().size() > 0) {
diff --git a/src/com/android/settings/EncryptionInterstitial.java b/src/com/android/settings/EncryptionInterstitial.java
index 3aec03e..5cd0508 100644
--- a/src/com/android/settings/EncryptionInterstitial.java
+++ b/src/com/android/settings/EncryptionInterstitial.java
@@ -26,10 +26,12 @@
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.UserHandle;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.accessibility.AccessibilityManager;
+import android.widget.Button;
 import android.widget.RadioButton;
 import android.widget.TextView;
 
@@ -39,9 +41,12 @@
 import java.util.List;
 
 public class EncryptionInterstitial extends SettingsActivity {
+    private final static String TAG = EncryptionInterstitial.class.getSimpleName();
 
     protected static final String EXTRA_PASSWORD_QUALITY = "extra_password_quality";
+    protected static final String EXTRA_UNLOCK_METHOD_INTENT = "extra_unlock_method_intent";
     public static final String EXTRA_REQUIRE_PASSWORD = "extra_require_password";
+    private static final int CHOOSE_LOCK_REQUEST = 100;
 
     @Override
     public Intent getIntent() {
@@ -56,7 +61,7 @@
     }
 
     public static Intent createStartIntent(Context ctx, int quality,
-            boolean requirePasswordDefault) {
+            boolean requirePasswordDefault, Intent unlockMethodIntent) {
         return new Intent(ctx, EncryptionInterstitial.class)
                 .putExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, true)
                 .putExtra(EXTRA_PREFS_SET_BACK_TEXT, (String) null)
@@ -64,7 +69,8 @@
                         R.string.encryption_continue_button))
                 .putExtra(EXTRA_PASSWORD_QUALITY, quality)
                 .putExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, R.string.encryption_interstitial_header)
-                .putExtra(EXTRA_REQUIRE_PASSWORD, requirePasswordDefault);
+                .putExtra(EXTRA_REQUIRE_PASSWORD, requirePasswordDefault)
+                .putExtra(EXTRA_UNLOCK_METHOD_INTENT, unlockMethodIntent);
     }
 
     public static class EncryptionInterstitialFragment extends SettingsPreferenceFragment
@@ -75,6 +81,7 @@
         private RadioButton mDontRequirePasswordToDecryptButton;
         private TextView mEncryptionMessage;
         private boolean mPasswordRequired;
+        private Intent mUnlockMethodIntent;
 
         @Override
         protected int getMetricsCategory() {
@@ -98,7 +105,9 @@
                     (TextView) view.findViewById(R.id.encryption_message);
             boolean forFingerprint = getActivity().getIntent().getBooleanExtra(
                     ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT, false);
-            int quality = getActivity().getIntent().getIntExtra(EXTRA_PASSWORD_QUALITY, 0);
+            Intent intent = getActivity().getIntent();
+            final int quality = intent.getIntExtra(EXTRA_PASSWORD_QUALITY, 0);
+            mUnlockMethodIntent = (Intent) intent.getParcelableExtra(EXTRA_UNLOCK_METHOD_INTENT);
             final int msgId;
             final int enableId;
             final int disableId;
@@ -136,6 +145,36 @@
 
             setRequirePasswordState(getActivity().getIntent().getBooleanExtra(
                     EXTRA_REQUIRE_PASSWORD, true));
+
+            Button nextButton = getNextButton();
+            if (nextButton != null) {
+                nextButton.setOnClickListener(new View.OnClickListener() {
+                    @Override
+                    public void onClick(View v) {
+                        startLockIntent();
+                    }
+                });
+            }
+        }
+
+        protected void startLockIntent() {
+            if (mUnlockMethodIntent != null) {
+                mUnlockMethodIntent.putExtra(EXTRA_REQUIRE_PASSWORD, mPasswordRequired);
+                startActivityForResult(mUnlockMethodIntent, CHOOSE_LOCK_REQUEST);
+            } else {
+                Log.wtf(TAG, "no unlock intent to start");
+                finish();
+            }
+        }
+
+        @Override
+        public void onActivityResult(int requestCode, int resultCode, Intent data) {
+            super.onActivityResult(requestCode, resultCode, data);
+            if (requestCode == CHOOSE_LOCK_REQUEST &&
+                    resultCode == RESULT_FIRST_USER) {
+                getActivity().setResult(RESULT_OK, data);
+                finish();
+            }
         }
 
         @Override
@@ -206,15 +245,6 @@
             mPasswordRequired = required;
             mRequirePasswordToDecryptButton.setChecked(required);
             mDontRequirePasswordToDecryptButton.setChecked(!required);
-
-            // Updates value returned by SettingsActivity.onActivityResult().
-            SettingsActivity sa = (SettingsActivity)getActivity();
-            Intent resultIntentData = sa.getResultIntentData();
-            if (resultIntentData == null) {
-                resultIntentData = new Intent();
-                sa.setResultIntentData(resultIntentData);
-            }
-            resultIntentData.putExtra(EXTRA_REQUIRE_PASSWORD, mPasswordRequired);
         }
 
         @Override
diff --git a/src/com/android/settings/SetupChooseLockGeneric.java b/src/com/android/settings/SetupChooseLockGeneric.java
index 0b0333b..9559f8d 100644
--- a/src/com/android/settings/SetupChooseLockGeneric.java
+++ b/src/com/android/settings/SetupChooseLockGeneric.java
@@ -170,9 +170,9 @@
 
         @Override
         protected Intent getEncryptionInterstitialIntent(Context context, int quality,
-                boolean required) {
+                boolean required, Intent unlockMethodIntent) {
             Intent intent = SetupEncryptionInterstitial.createStartIntent(context, quality,
-                    required);
+                    required, unlockMethodIntent);
             SetupWizardUtils.copySetupExtras(getActivity().getIntent(), intent);
             return intent;
         }
diff --git a/src/com/android/settings/SetupEncryptionInterstitial.java b/src/com/android/settings/SetupEncryptionInterstitial.java
index cd943af..8d061ec 100644
--- a/src/com/android/settings/SetupEncryptionInterstitial.java
+++ b/src/com/android/settings/SetupEncryptionInterstitial.java
@@ -38,9 +38,9 @@
 public class SetupEncryptionInterstitial extends EncryptionInterstitial {
 
     public static Intent createStartIntent(Context ctx, int quality,
-            boolean requirePasswordDefault) {
+            boolean requirePasswordDefault, Intent unlockMethodIntent) {
         Intent startIntent = EncryptionInterstitial.createStartIntent(ctx, quality,
-                requirePasswordDefault);
+                requirePasswordDefault, unlockMethodIntent);
         startIntent.setClass(ctx, SetupEncryptionInterstitial.class);
         startIntent.putExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)
                 .putExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
@@ -102,12 +102,7 @@
 
         @Override
         public void onNavigateNext() {
-            final SetupEncryptionInterstitial activity =
-                    (SetupEncryptionInterstitial) getActivity();
-            if (activity != null) {
-                activity.setResult(RESULT_OK, activity.getResultIntentData());
-                finish();
-            }
+            startLockIntent();
         }
     }
 }