Skip to content

Commit 800e2f7

Browse files
committed
Disable inactive password constraints.
Some constraints are only use with certain at certain qualities so reflect this in the UI. Bug: 30109030 Change-Id: I5170492c97aae570650a3c5d5a51c8543cad4f70
1 parent 6faa6b9 commit 800e2f7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

app/src/main/java/com/afwsamples/testdpc/policy/keyguard/PasswordConstraintsFragment.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
157157
setup(Keys.MIN_UPPERCASE, getDpm().getPasswordMinimumUpperCase(getAdmin()));
158158
setup(Keys.MIN_SYMBOLS, getDpm().getPasswordMinimumSymbols(getAdmin()));
159159
setup(Keys.MIN_NONLETTER, getDpm().getPasswordMinimumNonLetter(getAdmin()));
160+
161+
enableMinimumsForQuality();
160162
}
161163

162164
@Override
@@ -199,6 +201,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
199201
list.setValue((String) newValue);
200202
summary = list.getEntry();
201203
getDpm().setPasswordQuality(getAdmin(), value);
204+
enableMinimumsForQuality();
202205
break;
203206
}
204207
case Keys.MIN_LENGTH:
@@ -231,6 +234,55 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
231234
return true;
232235
}
233236

237+
/**
238+
* Enable and disable password constraint preferences based on the current password quality.
239+
*/
240+
private void enableMinimumsForQuality() {
241+
final int currentQuality = getDpm().getPasswordQuality(getAdmin());
242+
243+
// Minimum length can be set for most qualities
244+
final EditTextPreference minLength = (EditTextPreference) findPreference(Keys.MIN_LENGTH);
245+
if (currentQuality >= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC) {
246+
minLength.setEnabled(true);
247+
enableEditTextPreference(minLength);
248+
} else {
249+
Util.disablePreference(minLength, R.string.not_for_password_quality);
250+
}
251+
252+
// Other minimums are only active for the highest quality
253+
final EditTextPreference minLetters = (EditTextPreference) findPreference(Keys.MIN_LETTERS);
254+
final EditTextPreference minNumeric = (EditTextPreference) findPreference(Keys.MIN_NUMERIC);
255+
final EditTextPreference minLower = (EditTextPreference) findPreference(Keys.MIN_LOWERCASE);
256+
final EditTextPreference minUpper = (EditTextPreference) findPreference(Keys.MIN_UPPERCASE);
257+
final EditTextPreference minSymbols = (EditTextPreference) findPreference(Keys.MIN_SYMBOLS);
258+
final EditTextPreference minNonLetter =
259+
(EditTextPreference) findPreference(Keys.MIN_NONLETTER);
260+
261+
if (currentQuality == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) {
262+
enableEditTextPreference(minLetters);
263+
enableEditTextPreference(minNumeric);
264+
enableEditTextPreference(minLower);
265+
enableEditTextPreference(minUpper);
266+
enableEditTextPreference(minSymbols);
267+
enableEditTextPreference(minNonLetter);
268+
} else {
269+
Util.disablePreference(minLetters, R.string.not_for_password_quality);
270+
Util.disablePreference(minNumeric, R.string.not_for_password_quality);
271+
Util.disablePreference(minLower, R.string.not_for_password_quality);
272+
Util.disablePreference(minUpper, R.string.not_for_password_quality);
273+
Util.disablePreference(minSymbols, R.string.not_for_password_quality);
274+
Util.disablePreference(minNonLetter, R.string.not_for_password_quality);
275+
}
276+
}
277+
278+
/**
279+
* Enable the preference and display the value in the summary.
280+
*/
281+
private void enableEditTextPreference(EditTextPreference preference) {
282+
preference.setEnabled(true);
283+
preference.setSummary(preference.getText());
284+
}
285+
234286
/**
235287
* Set an initial value. Updates the summary to match.
236288
*/

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<string name="profile_owner_only">Profile owner only</string>
8989
<string name="user_restricted">Disallowed by user restriction</string>
9090
<string name="requires_process_logs">Requires process logging to be enabled</string>
91+
<string name="not_for_password_quality">Not applicable to current password quality</string>
9192

9293
<!-- Strings for device owner management -->
9394
<string name="this_is_not_a_device_owner">This app is currently not the device owner.</string>

0 commit comments

Comments
 (0)