Merge "Revert "Import translations. DO NOT MERGE""
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 1825423..e4abbca 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1042,6 +1042,8 @@
 
     <!-- Title of the dialog shown when the user removes the device lock [CHAR LIMIT=NONE] -->
     <string name="unlock_disable_frp_warning_title">Remove device protection?</string>
+    <!-- Title of the dialog shown when the user removes the profile lock [CHAR LIMIT=NONE] -->
+    <string name="unlock_disable_frp_warning_title_profile">Remove profile protection?</string>
 
     <!-- Content of the dialog shown when the user removes the device lock pattern [CHAR LIMIT=NONE] -->
     <string name="unlock_disable_frp_warning_content_pattern">"Device protection features will not work without your pattern."</string>
@@ -1060,6 +1062,23 @@
     <!-- Content of the dialog shown when the user removes the device lock of unknown type and the user has fingerprints enrolled [CHAR LIMIT=NONE] -->
     <string name="unlock_disable_frp_warning_content_unknown_fingerprint">"Device protection features will not work without your screen lock.<xliff:g id="empty_line">\n\n</xliff:g>Your saved fingerprints will also be removed from this device and you won't be able to unlock your phone, authorize purchases, or sign in to apps with them."</string>
 
+    <!-- Content of the dialog shown when the user removes the profile lock pattern [CHAR LIMIT=NONE] -->
+    <string name="unlock_disable_frp_warning_content_pattern_profile">"Profile protection features will not work without your pattern."</string>
+    <!-- Content of the dialog shown when the user removes the profile lock pattern and the user has fingerprints enrolled [CHAR LIMIT=NONE] -->
+    <string name="unlock_disable_frp_warning_content_pattern_fingerprint_profile">"Profile protection features will not work without your pattern.<xliff:g id="empty_line">\n\n</xliff:g>Your saved fingerprints will also be removed from this profile and you won't be able to unlock your profile, authorize purchases, or sign in to apps with them."</string>
+    <!-- Content of the dialog shown when the user removes the profile lock PIN [CHAR LIMIT=NONE] -->
+    <string name="unlock_disable_frp_warning_content_pin_profile">"Profile protection features will not work without your PIN."</string>
+    <!-- Content of the dialog shown when the user removes the profile lock PIN and the user has fingerprints enrolled [CHAR LIMIT=NONE] -->
+    <string name="unlock_disable_frp_warning_content_pin_fingerprint_profile">"Profile protection features will not work without your PIN.<xliff:g id="empty_line">\n\n</xliff:g>Your saved fingerprints will also be removed from this profile and you won't be able to unlock your profile, authorize purchases, or sign in to apps with them."</string>
+    <!-- Content of the dialog shown when the user removes the profile lock password [CHAR LIMIT=NONE] -->
+    <string name="unlock_disable_frp_warning_content_password_profile">"Profile protection features will not work without your password."</string>
+    <!-- Content of the dialog shown when the user removes the profile lock password and the user has fingerprints enrolled [CHAR LIMIT=NONE] -->
+    <string name="unlock_disable_frp_warning_content_password_fingerprint_profile">"Profile protection features will not work without your password.<xliff:g id="empty_line">\n\n</xliff:g>Your saved fingerprints will also be removed from this profile and you won't be able to unlock your profile, authorize purchases, or sign in to apps with them."</string>
+    <!-- Content of the dialog shown when the user removes the profile lock of unknown type [CHAR LIMIT=NONE] -->
+    <string name="unlock_disable_frp_warning_content_unknown_profile">"Profile protection features will not work without your screen lock."</string>
+    <!-- Content of the dialog shown when the user removes the profile lock of unknown type and the user has fingerprints enrolled [CHAR LIMIT=NONE] -->
+    <string name="unlock_disable_frp_warning_content_unknown_fingerprint_profile">"Profile protection features will not work without your screen lock.<xliff:g id="empty_line">\n\n</xliff:g>Your saved fingerprints will also be removed from this profile and you won't be able to unlock your profile, authorize purchases, or sign in to apps with them."</string>
+
     <!-- Affirmative action of the dialog shown when the user removes the device lock [CHAR LIMIT=25] -->
     <string name="unlock_disable_frp_warning_ok">Yes, remove</string>
 
@@ -2973,6 +2992,15 @@
     <!-- Message to be used to explain the user that he needs to enter his password to continue a
          particular operation. [CHAR LIMIT=70]-->
     <string name="lockpassword_confirm_your_password_generic">Enter your device password to continue.</string>
+    <!-- Message to be used to explain the user that he needs to enter his profile pattern to continue a
+         particular operation. [CHAR LIMIT=70]-->
+    <string name="lockpassword_confirm_your_pattern_generic_profile">Use your profile pattern to continue.</string>
+    <!-- Message to be used to explain the user that he needs to enter his profile PIN to continue a
+         particular operation. [CHAR LIMIT=70]-->
+    <string name="lockpassword_confirm_your_pin_generic_profile">Enter your profile PIN to continue.</string>
+    <!-- Message to be used to explain the user that he needs to enter his profile password to continue a
+         particular operation. [CHAR LIMIT=70]-->
+    <string name="lockpassword_confirm_your_password_generic_profile">Enter your profile password to continue.</string>
 
     <!-- Security & location settings screen, change security method screen instruction if user
          enters incorrect PIN [CHAR LIMIT=30] -->
diff --git a/src/com/android/settings/ChooseLockGeneric.java b/src/com/android/settings/ChooseLockGeneric.java
index c576897..0e6cf3b 100644
--- a/src/com/android/settings/ChooseLockGeneric.java
+++ b/src/com/android/settings/ChooseLockGeneric.java
@@ -581,28 +581,62 @@
             return R.string.help_url_choose_lockscreen;
         }
 
+        private int getResIdForFactoryResetProtectionWarningTitle() {
+            boolean isProfile = Utils.isManagedProfile(UserManager.get(getActivity()), mUserId);
+            return isProfile ? R.string.unlock_disable_frp_warning_title_profile
+                    : R.string.unlock_disable_frp_warning_title;
+        }
+
         private int getResIdForFactoryResetProtectionWarningMessage() {
             boolean hasFingerprints = mFingerprintManager.hasEnrolledFingerprints();
+            boolean isProfile = Utils.isManagedProfile(UserManager.get(getActivity()), mUserId);
             switch (mLockPatternUtils.getKeyguardStoredPasswordQuality(mUserId)) {
                 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
-                    return hasFingerprints
-                            ? R.string.unlock_disable_frp_warning_content_pattern_fingerprint
-                            : R.string.unlock_disable_frp_warning_content_pattern;
+                    if (hasFingerprints && isProfile) {
+                        return R.string
+                                .unlock_disable_frp_warning_content_pattern_fingerprint_profile;
+                    } else if (hasFingerprints && !isProfile) {
+                        return R.string.unlock_disable_frp_warning_content_pattern_fingerprint;
+                    } else if (isProfile) {
+                        return R.string.unlock_disable_frp_warning_content_pattern_profile;
+                    } else {
+                        return R.string.unlock_disable_frp_warning_content_pattern;
+                    }
                 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
                 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
-                    return hasFingerprints
-                            ? R.string.unlock_disable_frp_warning_content_pin_fingerprint
-                            : R.string.unlock_disable_frp_warning_content_pin;
+                    if (hasFingerprints && isProfile) {
+                        return R.string.unlock_disable_frp_warning_content_pin_fingerprint_profile;
+                    } else if (hasFingerprints && !isProfile) {
+                        return R.string.unlock_disable_frp_warning_content_pin_fingerprint;
+                    } else if (isProfile) {
+                        return R.string.unlock_disable_frp_warning_content_pin_profile;
+                    } else {
+                        return R.string.unlock_disable_frp_warning_content_pin;
+                    }
                 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
                 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
                 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
-                    return hasFingerprints
-                            ? R.string.unlock_disable_frp_warning_content_password_fingerprint
-                            : R.string.unlock_disable_frp_warning_content_password;
+                    if (hasFingerprints && isProfile) {
+                        return R.string
+                                .unlock_disable_frp_warning_content_password_fingerprint_profile;
+                    } else if (hasFingerprints && !isProfile) {
+                        return R.string.unlock_disable_frp_warning_content_password_fingerprint;
+                    } else if (isProfile) {
+                        return R.string.unlock_disable_frp_warning_content_password_profile;
+                    } else {
+                        return R.string.unlock_disable_frp_warning_content_password;
+                    }
                 default:
-                    return hasFingerprints
-                            ? R.string.unlock_disable_frp_warning_content_unknown_fingerprint
-                            : R.string.unlock_disable_frp_warning_content_unknown;
+                    if (hasFingerprints && isProfile) {
+                        return R.string
+                                .unlock_disable_frp_warning_content_unknown_fingerprint_profile;
+                    } else if (hasFingerprints && !isProfile) {
+                        return R.string.unlock_disable_frp_warning_content_unknown_fingerprint;
+                    } else if (isProfile) {
+                        return R.string.unlock_disable_frp_warning_content_unknown_profile;
+                    } else {
+                        return R.string.unlock_disable_frp_warning_content_unknown;
+                    }
             }
         }
 
@@ -637,22 +671,26 @@
         }
 
         private void showFactoryResetProtectionWarningDialog(String unlockMethodToSet) {
+            int title = getResIdForFactoryResetProtectionWarningTitle();
             int message = getResIdForFactoryResetProtectionWarningMessage();
             FactoryResetProtectionWarningDialog dialog =
-                    FactoryResetProtectionWarningDialog.newInstance(message, unlockMethodToSet);
+                    FactoryResetProtectionWarningDialog.newInstance(
+                            title, message, unlockMethodToSet);
             dialog.show(getChildFragmentManager(), TAG_FRP_WARNING_DIALOG);
         }
 
         public static class FactoryResetProtectionWarningDialog extends DialogFragment {
 
+            private static final String ARG_TITLE_RES = "titleRes";
             private static final String ARG_MESSAGE_RES = "messageRes";
             private static final String ARG_UNLOCK_METHOD_TO_SET = "unlockMethodToSet";
 
-            public static FactoryResetProtectionWarningDialog newInstance(int messageRes,
-                    String unlockMethodToSet) {
+            public static FactoryResetProtectionWarningDialog newInstance(
+                    int titleRes, int messageRes, String unlockMethodToSet) {
                 FactoryResetProtectionWarningDialog frag =
                         new FactoryResetProtectionWarningDialog();
                 Bundle args = new Bundle();
+                args.putInt(ARG_TITLE_RES, titleRes);
                 args.putInt(ARG_MESSAGE_RES, messageRes);
                 args.putString(ARG_UNLOCK_METHOD_TO_SET, unlockMethodToSet);
                 frag.setArguments(args);
@@ -672,7 +710,7 @@
                 final Bundle args = getArguments();
 
                 return new AlertDialog.Builder(getActivity())
-                        .setTitle(R.string.unlock_disable_frp_warning_title)
+                        .setTitle(args.getInt(ARG_TITLE_RES))
                         .setMessage(args.getInt(ARG_MESSAGE_RES))
                         .setPositiveButton(R.string.unlock_disable_frp_warning_ok,
                                 new DialogInterface.OnClickListener() {
diff --git a/src/com/android/settings/ConfirmLockPassword.java b/src/com/android/settings/ConfirmLockPassword.java
index 00a68bf..f6347c1 100644
--- a/src/com/android/settings/ConfirmLockPassword.java
+++ b/src/com/android/settings/ConfirmLockPassword.java
@@ -26,6 +26,7 @@
 import android.os.CountDownTimer;
 import android.os.Handler;
 import android.os.SystemClock;
+import android.os.UserManager;
 import android.os.storage.StorageManager;
 import android.text.InputType;
 import android.text.TextUtils;
@@ -180,8 +181,16 @@
         }
 
         private int getDefaultDetails() {
-            return mIsAlpha ? R.string.lockpassword_confirm_your_password_generic
-                    : R.string.lockpassword_confirm_your_pin_generic;
+            boolean isProfile = Utils.isManagedProfile(
+                    UserManager.get(getActivity()), mEffectiveUserId);
+            if (mIsAlpha && !isProfile) {
+                return R.string.lockpassword_confirm_your_password_generic;
+            } else if (mIsAlpha && isProfile) {
+                return R.string.lockpassword_confirm_your_password_generic_profile;
+            } else if (!isProfile) {
+                return R.string.lockpassword_confirm_your_pin_generic;
+            }
+            return R.string.lockpassword_confirm_your_pin_generic_profile;
         }
 
         private int getErrorMessage() {
diff --git a/src/com/android/settings/ConfirmLockPattern.java b/src/com/android/settings/ConfirmLockPattern.java
index 087a0b9..5c72bd1 100644
--- a/src/com/android/settings/ConfirmLockPattern.java
+++ b/src/com/android/settings/ConfirmLockPattern.java
@@ -23,6 +23,7 @@
 import android.os.Bundle;
 import android.os.CountDownTimer;
 import android.os.SystemClock;
+import android.os.UserManager;
 import android.os.storage.StorageManager;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -279,9 +280,13 @@
                     }
                     if (mDetailsText != null) {
                         mDetailsTextView.setText(mDetailsText);
-                    } else {
+                    } else if (!Utils.isManagedProfile(
+                            UserManager.get(getActivity()), mEffectiveUserId)) {
                         mDetailsTextView.setText(
                                 R.string.lockpassword_confirm_your_pattern_generic);
+                    } else {
+                        mDetailsTextView.setText(
+                                R.string.lockpassword_confirm_your_pattern_generic_profile);
                     }
                     mErrorTextView.setText("");
 
diff --git a/src/com/android/settings/ProfileChallengePreferenceFragment.java b/src/com/android/settings/ProfileChallengePreferenceFragment.java
index 676f61f..4b57552 100644
--- a/src/com/android/settings/ProfileChallengePreferenceFragment.java
+++ b/src/com/android/settings/ProfileChallengePreferenceFragment.java
@@ -60,7 +60,8 @@
     public static final String TAG_UNIFICATION_DIALOG = "unification_dialog";
 
     private static final int SET_OR_CHANGE_LOCK_METHOD_REQUEST = 123;
-    private static final int UNIFY_LOCK_METHOD_REQUEST = 124;
+    private static final int UNIFY_LOCK_CONFIRM_DEVICE_REQUEST = 124;
+    private static final int UNIFY_LOCK_CONFIRM_PROFILE_REQUEST = 125;
 
     // Not all preferences make sense for the Work Challenge, this is a whitelist.
     private static final Set<String> ALLOWED_PREFERENCE_KEYS = new HashSet<>();
@@ -77,6 +78,9 @@
     private LockPatternUtils mLockPatternUtils;
     private int mProfileUserId;
 
+    private String mCurrentDevicePassword;
+    private String mCurrentProfilePassword;
+
     private SwitchPreference mVisiblePattern;
 
     @Override
@@ -127,15 +131,58 @@
     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
-        if (requestCode == UNIFY_LOCK_METHOD_REQUEST && resultCode == Activity.RESULT_OK) {
+        if (requestCode == UNIFY_LOCK_CONFIRM_DEVICE_REQUEST && resultCode == Activity.RESULT_OK) {
+            mCurrentDevicePassword =
+                    data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
+            launchConfirmProfileLockForUnification();
+            return;
+        } else if (requestCode == UNIFY_LOCK_CONFIRM_PROFILE_REQUEST
+                && resultCode == Activity.RESULT_OK) {
+            mCurrentProfilePassword =
+                    data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
             unifyLocks();
             return;
         }
     }
 
+    private void launchConfirmDeviceLockForUnification() {
+        final String title = getActivity().getString(
+                R.string.lock_settings_profile_screen_lock_title);
+        final ChooseLockSettingsHelper helper =
+                new ChooseLockSettingsHelper(getActivity(), this);
+        if (!helper.launchConfirmationActivity(
+                UNIFY_LOCK_CONFIRM_DEVICE_REQUEST, title, true, UserHandle.myUserId())) {
+            launchConfirmProfileLockForUnification();
+        }
+    }
+
+    private void launchConfirmProfileLockForUnification() {
+        final String title = getActivity().getString(
+                R.string.lock_settings_profile_screen_lock_title);
+        final ChooseLockSettingsHelper helper =
+                new ChooseLockSettingsHelper(getActivity(), this);
+        if (!helper.launchConfirmationActivity(
+                UNIFY_LOCK_CONFIRM_PROFILE_REQUEST, title, true, mProfileUserId)) {
+            unifyLocks();
+            createPreferenceHierarchy();
+        }
+    }
+
     private void unifyLocks() {
+        int profileQuality = mLockPatternUtils.getKeyguardStoredPasswordQuality(mProfileUserId);
         mLockPatternUtils.clearLock(mProfileUserId);
         mLockPatternUtils.setSeparateProfileChallengeEnabled(mProfileUserId, false);
+        if (profileQuality == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) {
+            mLockPatternUtils.saveLockPattern(
+                    LockPatternUtils.stringToPattern(mCurrentProfilePassword),
+                    mCurrentDevicePassword, UserHandle.myUserId());
+        } else {
+            mLockPatternUtils.saveLockPassword(
+                    mCurrentProfilePassword, mCurrentDevicePassword,
+                    profileQuality, UserHandle.myUserId());
+        }
+        mCurrentDevicePassword = null;
+        mCurrentProfilePassword = null;
     }
 
     @Override
@@ -172,7 +219,7 @@
                 root.findPreference(KEY_SECURITY_CATEGORY);
         if (securityCategory != null) {
             if (mLockPatternUtils.isSeparateProfileChallengeEnabled(mProfileUserId)) {
-                addUnificationPreference(securityCategory);
+                maybeAddUnificationPreference(securityCategory);
             } else {
                 Preference lockPreference =
                         securityCategory.findPreference(KEY_UNLOCK_SET_OR_CHANGE);
@@ -183,12 +230,15 @@
         }
     }
 
-    private void addUnificationPreference(PreferenceGroup securityCategory) {
-        Preference unificationPreference = new Preference(securityCategory.getContext());
-        unificationPreference.setKey(KEY_UNIFICATION);
-        unificationPreference.setTitle(R.string.lock_settings_profile_unification_title);
-        unificationPreference.setSummary(R.string.lock_settings_profile_unification_summary);
-        securityCategory.addPreference(unificationPreference);
+    private void maybeAddUnificationPreference(PreferenceGroup securityCategory) {
+        if (mLockPatternUtils.getKeyguardStoredPasswordQuality(mProfileUserId)
+                >= DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) {
+            Preference unificationPreference = new Preference(securityCategory.getContext());
+            unificationPreference.setKey(KEY_UNIFICATION);
+            unificationPreference.setTitle(R.string.lock_settings_profile_unification_title);
+            unificationPreference.setSummary(R.string.lock_settings_profile_unification_summary);
+            securityCategory.addPreference(unificationPreference);
+        }
     }
 
     private void removeNonWhitelistedItems(PreferenceGroup prefScreen) {
@@ -243,13 +293,9 @@
     }
 
     public static class UnificationConfirmationDialog extends DialogFragment {
-        private static final String ARG_USER_ID = "userId";
 
         public static UnificationConfirmationDialog newIntance(int userId) {
             UnificationConfirmationDialog dialog = new UnificationConfirmationDialog();
-            Bundle args = new Bundle();
-            args.putInt(ARG_USER_ID, userId);
-            dialog.setArguments(args);
             return dialog;
         }
 
@@ -263,7 +309,6 @@
 
         @Override
         public Dialog onCreateDialog(Bundle savedInstanceState) {
-            final Bundle args = getArguments();
             final ProfileChallengePreferenceFragment parentFragment =
                     ((ProfileChallengePreferenceFragment) getParentFragment());
             return new AlertDialog.Builder(getActivity())
@@ -273,17 +318,7 @@
                             new DialogInterface.OnClickListener() {
                                 @Override
                                 public void onClick(DialogInterface dialog, int whichButton) {
-                                    String title = getContext().getString(
-                                            R.string.lock_settings_profile_screen_lock_title);
-                                    ChooseLockSettingsHelper helper =
-                                            new ChooseLockSettingsHelper(
-                                                    getActivity(), parentFragment);
-                                    if (!helper.launchConfirmationActivity(
-                                            UNIFY_LOCK_METHOD_REQUEST,
-                                            title, true, args.getInt(ARG_USER_ID))) {
-                                        parentFragment.unifyLocks();
-                                        parentFragment.createPreferenceHierarchy();
-                                    }
+                                    parentFragment.launchConfirmDeviceLockForUnification();
                                 }
                             }
                     )
diff --git a/tests/src/com/android/settings/UserCredentialsTests.java b/tests/unit/src/com/android/settings/UserCredentialsTests.java
similarity index 100%
rename from tests/src/com/android/settings/UserCredentialsTests.java
rename to tests/unit/src/com/android/settings/UserCredentialsTests.java