1 /* 2 * Copyright 2019 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.android.car.settings.common; 18 19 import android.os.Bundle; 20 import android.text.InputType; 21 import android.view.View; 22 import android.widget.CheckBox; 23 import android.widget.CompoundButton; 24 import android.widget.EditText; 25 26 import com.android.car.settings.R; 27 28 /** 29 * Extends {@link ValidatedEditTextPreferenceDialogFragment} for entering password input. Obscures 30 * password input by default and reveals a checkbox that toggles the password visibility. 31 */ 32 public class PasswordEditTextPreferenceDialogFragment extends 33 ValidatedEditTextPreferenceDialogFragment { 34 35 private EditText mEditText; 36 37 /** 38 * Returns a new instance of {@link PasswordEditTextPreferenceDialogFragment} for the 39 * {@link PasswordEditTextPreference} with the given {@code key}. 40 */ newInstance(String key)41 public static PasswordEditTextPreferenceDialogFragment newInstance(String key) { 42 PasswordEditTextPreferenceDialogFragment fragment = 43 new PasswordEditTextPreferenceDialogFragment(); 44 Bundle b = new Bundle(/* capacity= */ 1); 45 b.putString(ARG_KEY, key); 46 fragment.setArguments(b); 47 return fragment; 48 } 49 50 51 @Override onBindDialogView(View view)52 protected void onBindDialogView(View view) { 53 super.onBindDialogView(view); 54 55 mEditText = view.findViewById(android.R.id.edit); 56 mEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 57 CheckBox cb = view.findViewById(R.id.checkbox); 58 cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 59 @Override 60 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 61 if (isChecked) { 62 mEditText.setInputType(InputType.TYPE_CLASS_TEXT 63 | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); 64 } else { 65 mEditText.setInputType(InputType.TYPE_CLASS_TEXT 66 | InputType.TYPE_TEXT_VARIATION_PASSWORD); 67 } 68 // Place cursor at the end 69 mEditText.setSelection(mEditText.getText().length()); 70 } 71 }); 72 } 73 } 74