|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.afwsamples.testdpc.policy.keyguard; |
| 18 | + |
| 19 | +import android.app.Fragment; |
| 20 | +import android.app.admin.DevicePolicyManager; |
| 21 | +import android.content.ComponentName; |
| 22 | +import android.content.Context; |
| 23 | +import android.os.Bundle; |
| 24 | +import android.text.Editable; |
| 25 | +import android.text.TextUtils; |
| 26 | +import android.text.TextWatcher; |
| 27 | +import android.view.LayoutInflater; |
| 28 | +import android.view.View; |
| 29 | +import android.view.ViewGroup; |
| 30 | +import android.widget.Button; |
| 31 | +import android.widget.EditText; |
| 32 | +import android.widget.RadioButton; |
| 33 | +import android.widget.RadioGroup; |
| 34 | +import android.widget.Toast; |
| 35 | + |
| 36 | +import com.afwsamples.testdpc.DeviceAdminReceiver; |
| 37 | +import com.afwsamples.testdpc.R; |
| 38 | + |
| 39 | +import java.util.LinkedHashMap; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +/** |
| 43 | + * This fragment provides functionalities to set password constraint policies as a profile |
| 44 | + * or device owner. |
| 45 | + * |
| 46 | + * <p>These include: |
| 47 | + * <ul> |
| 48 | + * <li>{@link DevicePolicyManager#setPasswordMinimumLength(ComponentName, String)}</li> |
| 49 | + * <li>{@link DevicePolicyManager#setPasswordMinimumLetters(ComponentName, String)}</li> |
| 50 | + * <li>{@link DevicePolicyManager#setPasswordMinimumNumeric(ComponentName, String)}</li> |
| 51 | + * <li>{@link DevicePolicyManager#setPasswordMinimumLowerCase(ComponentName, String)}</li> |
| 52 | + * <li>{@link DevicePolicyManager#setPasswordMinimumUpperCase(ComponentName, String)}</li> |
| 53 | + * <li>{@link DevicePolicyManager#setPasswordMinimumSymbols(ComponentName, String)}</li> |
| 54 | + * <li>{@link DevicePolicyManager#setPasswordMinimumNonLetter(ComponentName, String)}</li> |
| 55 | + * </ul> |
| 56 | + */ |
| 57 | +public final class PasswordConstraintsFragment extends Fragment implements |
| 58 | + RadioGroup.OnCheckedChangeListener, TextWatcher { |
| 59 | + |
| 60 | + private static final Map<Integer, Integer> PASSWORD_QUALITIES = new LinkedHashMap<>(7); |
| 61 | + static { |
| 62 | + // IDs of settings for {@link DevicePolicyManager#setPasswordQuality(ComponentName, int)}. |
| 63 | + final int[] policyIds = new int[] { |
| 64 | + DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, |
| 65 | + DevicePolicyManager.PASSWORD_QUALITY_SOMETHING, |
| 66 | + DevicePolicyManager.PASSWORD_QUALITY_NUMERIC, |
| 67 | + DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX, |
| 68 | + DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC, |
| 69 | + DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC, |
| 70 | + DevicePolicyManager.PASSWORD_QUALITY_COMPLEX |
| 71 | + }; |
| 72 | + // Strings to show for each password quality setting. |
| 73 | + final int[] policyNames = new int[] { |
| 74 | + R.string.password_quality_unspecified, |
| 75 | + R.string.password_quality_something, |
| 76 | + R.string.password_quality_numeric, |
| 77 | + R.string.password_quality_numeric_complex, |
| 78 | + R.string.password_quality_alphabetic, |
| 79 | + R.string.password_quality_alphanumeric, |
| 80 | + R.string.password_quality_complex |
| 81 | + }; |
| 82 | + if (policyIds.length != policyNames.length) { |
| 83 | + throw new AssertionError("Number of items in policyIds and policyNames do not match"); |
| 84 | + } |
| 85 | + for (int i = 0; i < policyIds.length; i++) { |
| 86 | + PASSWORD_QUALITIES.put(policyIds[i], policyNames[i]); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + // Radio list of all complexity settings, as defined above. |
| 91 | + private RadioGroup mQualityGroup; |
| 92 | + |
| 93 | + // Individual minimum password attribute requirements. |
| 94 | + private EditText mMinLength; |
| 95 | + private EditText mMinLetters; |
| 96 | + private EditText mMinNumeric; |
| 97 | + private EditText mMinLowerCase; |
| 98 | + private EditText mMinUpperCase; |
| 99 | + private EditText mMinSymbols; |
| 100 | + private EditText mMinNonLetter; |
| 101 | + |
| 102 | + private DevicePolicyManager mDpm; |
| 103 | + private ComponentName mAdminComponent; |
| 104 | + |
| 105 | + @Override |
| 106 | + public void onCreate(Bundle savedInstanceState) { |
| 107 | + super.onCreate(savedInstanceState); |
| 108 | + getActivity().getActionBar().setTitle(R.string.password_constraints); |
| 109 | + |
| 110 | + mDpm = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 111 | + mAdminComponent = DeviceAdminReceiver.getComponentName(getActivity()); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, |
| 116 | + Bundle savedInstanceState) { |
| 117 | + final View root = layoutInflater.inflate(R.layout.password_quality, null); |
| 118 | + |
| 119 | + // Create numeric text fields |
| 120 | + mMinLength = findAndPrepareField(root, R.id.password_min_length); |
| 121 | + mMinLetters = findAndPrepareField(root, R.id.password_min_letters); |
| 122 | + mMinNumeric = findAndPrepareField(root, R.id.password_min_numeric); |
| 123 | + mMinLowerCase = findAndPrepareField(root, R.id.password_min_lowercase); |
| 124 | + mMinUpperCase = findAndPrepareField(root, R.id.password_min_uppercase); |
| 125 | + mMinSymbols = findAndPrepareField(root, R.id.password_min_symbols); |
| 126 | + mMinNonLetter = findAndPrepareField(root, R.id.password_min_nonletter); |
| 127 | + |
| 128 | + // Create radio group for password quality |
| 129 | + mQualityGroup = (RadioGroup) root.findViewById(R.id.password_quality); |
| 130 | + for (Map.Entry<Integer, Integer> entry : PASSWORD_QUALITIES.entrySet()) { |
| 131 | + final RadioButton choice = new RadioButton(getContext()); |
| 132 | + choice.setId(entry.getKey()); |
| 133 | + choice.setText(entry.getValue()); |
| 134 | + mQualityGroup.addView(choice); |
| 135 | + } |
| 136 | + mQualityGroup.setOnCheckedChangeListener(this); |
| 137 | + |
| 138 | + return root; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void onResume() { |
| 143 | + super.onResume(); |
| 144 | + |
| 145 | + // Set the password quality radio group to show the requirement, if there is one. |
| 146 | + mQualityGroup.check(mDpm.getPasswordQuality(mAdminComponent)); |
| 147 | + |
| 148 | + // Update all of our minimum requirement fields via getPasswordMinimum(.*) |
| 149 | + mMinLength.setText(Integer.toString(mDpm.getPasswordMinimumLength(mAdminComponent))); |
| 150 | + mMinLetters.setText(Integer.toString(mDpm.getPasswordMinimumLetters(mAdminComponent))); |
| 151 | + mMinNumeric.setText(Integer.toString(mDpm.getPasswordMinimumNumeric(mAdminComponent))); |
| 152 | + mMinLowerCase.setText(Integer.toString(mDpm.getPasswordMinimumLowerCase(mAdminComponent))); |
| 153 | + mMinUpperCase.setText(Integer.toString(mDpm.getPasswordMinimumUpperCase(mAdminComponent))); |
| 154 | + mMinSymbols.setText(Integer.toString(mDpm.getPasswordMinimumSymbols(mAdminComponent))); |
| 155 | + mMinNonLetter.setText(Integer.toString(mDpm.getPasswordMinimumNonLetter(mAdminComponent))); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void onCheckedChanged(RadioGroup view, int checkedId) { |
| 160 | + if (view == mQualityGroup) { |
| 161 | + mDpm.setPasswordQuality(mAdminComponent, checkedId); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void afterTextChanged(Editable editable) { |
| 167 | + if (TextUtils.isEmpty(editable.toString())) { |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + final int value; |
| 172 | + try { |
| 173 | + value = Integer.parseInt(editable.toString()); |
| 174 | + } catch (NumberFormatException e) { |
| 175 | + Toast.makeText(getActivity(), R.string.not_valid_input, Toast.LENGTH_SHORT).show(); |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + if (editable == mMinLength.getEditableText()) { |
| 180 | + mDpm.setPasswordMinimumLength(mAdminComponent, value); |
| 181 | + } else if (editable == mMinLetters.getEditableText()) { |
| 182 | + mDpm.setPasswordMinimumLetters(mAdminComponent, value); |
| 183 | + } else if (editable == mMinNumeric.getEditableText()) { |
| 184 | + mDpm.setPasswordMinimumNumeric(mAdminComponent, value); |
| 185 | + } else if (editable == mMinLowerCase.getEditableText()) { |
| 186 | + mDpm.setPasswordMinimumLowerCase(mAdminComponent, value); |
| 187 | + } else if (editable == mMinUpperCase.getEditableText()) { |
| 188 | + mDpm.setPasswordMinimumUpperCase(mAdminComponent, value); |
| 189 | + } else if (editable == mMinSymbols.getEditableText()) { |
| 190 | + mDpm.setPasswordMinimumSymbols(mAdminComponent, value); |
| 191 | + } else if (editable == mMinNonLetter.getEditableText()) { |
| 192 | + mDpm.setPasswordMinimumNonLetter(mAdminComponent, value); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public void onTextChanged(CharSequence s, int start, int count, int after) { |
| 202 | + } |
| 203 | + |
| 204 | + private EditText findAndPrepareField(View root, final int id) { |
| 205 | + EditText field = (EditText) root.findViewById(id); |
| 206 | + field.addTextChangedListener(this); |
| 207 | + return field; |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments