Block "Screen lock" menu if password is managed.

"Screen lock" menu item is completely disabled in case, if
PASSWORD_QUALITY_MANAGED is set.

BUG: 25549437
Change-Id: I1958157946d29c013465e995af2226e061d5c726
diff --git a/src/com/android/settings/GearPreference.java b/src/com/android/settings/GearPreference.java
index 30f9a68..0f303d3 100644
--- a/src/com/android/settings/GearPreference.java
+++ b/src/com/android/settings/GearPreference.java
@@ -17,15 +17,16 @@
 package com.android.settings;
 
 import android.content.Context;
-import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceViewHolder;
 import android.util.AttributeSet;
 import android.view.View;
 
+import com.android.settingslib.RestrictedPreference;
+
 /**
  * A preference with a Gear on the side
  */
-public class GearPreference extends Preference implements View.OnClickListener {
+public class GearPreference extends RestrictedPreference implements View.OnClickListener {
 
     private OnGearClickListener mOnGearClickListener;
 
@@ -42,7 +43,9 @@
     @Override
     public void onBindViewHolder(PreferenceViewHolder holder) {
         super.onBindViewHolder(holder);
-        holder.findViewById(R.id.settings_button).setOnClickListener(this);
+        final View gear = holder.findViewById(R.id.settings_button);
+        gear.setOnClickListener(this);
+        gear.setEnabled(true);  // Make gear available even if the preference itself is disabled.
     }
 
     @Override
diff --git a/src/com/android/settings/SecuritySettings.java b/src/com/android/settings/SecuritySettings.java
index ccc158f..2f21e29 100644
--- a/src/com/android/settings/SecuritySettings.java
+++ b/src/com/android/settings/SecuritySettings.java
@@ -228,6 +228,15 @@
         final int resid = getResIdForLockUnlockScreen(getActivity(), mLockPatternUtils, MY_USER_ID);
         addPreferencesFromResource(resid);
 
+        final EnforcedAdmin admin = RestrictedLockUtils.checkIfPasswordQualityIsSet(
+                getActivity(), MY_USER_ID);
+        if (admin != null && mDPM.getPasswordQuality(admin.component) ==
+                DevicePolicyManager.PASSWORD_QUALITY_MANAGED) {
+            final GearPreference unlockSetOrChangePref =
+                    (GearPreference) getPreferenceScreen().findPreference(KEY_UNLOCK_SET_OR_CHANGE);
+            unlockSetOrChangePref.setDisabledByAdmin(admin);
+        }
+
         mProfileChallengeUserId = Utils.getManagedProfileId(mUm, MY_USER_ID);
         if (mProfileChallengeUserId != UserHandle.USER_NULL
                 && mLockPatternUtils.isSeparateProfileChallengeAllowed(mProfileChallengeUserId)) {
@@ -742,59 +751,58 @@
         @Override
         public List<SearchIndexableResource> getXmlResourcesToIndex(
                 Context context, boolean enabled) {
+            final List<SearchIndexableResource> index = new ArrayList<SearchIndexableResource>();
 
-            List<SearchIndexableResource> result = new ArrayList<SearchIndexableResource>();
-
-            LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
-            // Add options for lock/unlock screen
-            int resId = getResIdForLockUnlockScreen(context, lockPatternUtils, MY_USER_ID);
-
-            SearchIndexableResource sir = new SearchIndexableResource(context);
-            sir.xmlResId = resId;
-            result.add(sir);
-
+            final LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
+            final EnforcedAdmin admin = RestrictedLockUtils.checkIfPasswordQualityIsSet(
+                    context, MY_USER_ID);
+            final DevicePolicyManager dpm = (DevicePolicyManager)
+                    context.getSystemService(Context.DEVICE_POLICY_SERVICE);
             final UserManager um = UserManager.get(context);
+
+            if (admin == null || dpm.getPasswordQuality(admin.component) !=
+                    DevicePolicyManager.PASSWORD_QUALITY_MANAGED) {
+                // Add options for lock/unlock screen
+                final int resId = getResIdForLockUnlockScreen(context, lockPatternUtils,
+                        MY_USER_ID);
+                index.add(getSearchResource(context, resId));
+            }
+
             final int profileUserId = Utils.getManagedProfileId(um, MY_USER_ID);
             if (profileUserId != UserHandle.USER_NULL
                     && lockPatternUtils.isSeparateProfileChallengeAllowed(profileUserId)) {
-                sir = new SearchIndexableResource(context);
-                sir.xmlResId = getResIdForLockUnlockScreen(
-                        context, lockPatternUtils, profileUserId);
-                result.add(sir);
+                index.add(getSearchResource(context, getResIdForLockUnlockScreen(context,
+                        lockPatternUtils, profileUserId)));
             }
 
             if (um.isAdminUser()) {
-                DevicePolicyManager dpm = (DevicePolicyManager)
-                        context.getSystemService(Context.DEVICE_POLICY_SERVICE);
-
                 switch (dpm.getStorageEncryptionStatus()) {
                     case DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE:
                         // The device is currently encrypted.
-                        resId = R.xml.security_settings_encrypted;
+                        index.add(getSearchResource(context, R.xml.security_settings_encrypted));
                         break;
                     case DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE:
                         // This device supports encryption but isn't encrypted.
-                        resId = R.xml.security_settings_unencrypted;
+                        index.add(getSearchResource(context, R.xml.security_settings_unencrypted));
                         break;
                 }
-
-                sir = new SearchIndexableResource(context);
-                sir.xmlResId = resId;
-                result.add(sir);
             }
 
-            sir = new SearchIndexableResource(context);
-            sir.xmlResId = SecuritySubSettings.getResIdForLockUnlockSubScreen(context,
-                    lockPatternUtils);
+            final SearchIndexableResource sir = getSearchResource(context,
+                    SecuritySubSettings.getResIdForLockUnlockSubScreen(context, lockPatternUtils));
             sir.className = SecuritySubSettings.class.getName();
-            result.add(sir);
+            index.add(sir);
 
             // Append the rest of the settings
-            sir = new SearchIndexableResource(context);
-            sir.xmlResId = R.xml.security_settings_misc;
-            result.add(sir);
+            index.add(getSearchResource(context, R.xml.security_settings_misc));
 
-            return result;
+            return index;
+        }
+
+        private SearchIndexableResource getSearchResource(Context context, int xmlResId) {
+            final SearchIndexableResource sir = new SearchIndexableResource(context);
+            sir.xmlResId = xmlResId;
+            return sir;
         }
 
         @Override