1 /* 2 * Copyright (C) 2017 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.settings.widget; 18 19 import android.app.AlertDialog; 20 import android.content.Context; 21 import android.support.annotation.VisibleForTesting; 22 import android.support.v7.preference.PreferenceViewHolder; 23 import android.text.Editable; 24 import android.text.InputType; 25 import android.text.TextUtils; 26 import android.text.TextWatcher; 27 import android.util.AttributeSet; 28 import android.util.Log; 29 import android.view.View; 30 import android.widget.EditText; 31 import android.widget.TextView; 32 33 import com.android.settingslib.CustomEditTextPreference; 34 35 /** 36 * {@code EditTextPreference} that supports input validation. 37 */ 38 public class ValidatedEditTextPreference extends CustomEditTextPreference { 39 40 public interface Validator { 41 boolean isTextValid(String value); 42 } 43 44 private final EditTextWatcher mTextWatcher = new EditTextWatcher(); 45 private Validator mValidator; 46 private boolean mIsPassword; 47 private boolean mIsSummaryPassword; 48 49 public ValidatedEditTextPreference(Context context, AttributeSet attrs, 50 int defStyleAttr, int defStyleRes) { 51 super(context, attrs, defStyleAttr, defStyleRes); 52 } 53 54 public ValidatedEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) { 55 super(context, attrs, defStyleAttr); 56 } 57 58 public ValidatedEditTextPreference(Context context, AttributeSet attrs) { 59 super(context, attrs); 60 } 61 62 public ValidatedEditTextPreference(Context context) { 63 super(context); 64 } 65 66 @Override 67 protected void onBindDialogView(View view) { 68 super.onBindDialogView(view); 69 final EditText editText = view.findViewById(android.R.id.edit); 70 if (editText != null && !TextUtils.isEmpty(editText.getText())) { 71 editText.setSelection(editText.getText().length()); 72 } 73 if (mValidator != null && editText != null) { 74 editText.removeTextChangedListener(mTextWatcher); 75 if (mIsPassword) { 76 editText.setInputType( 77 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); 78 editText.setMaxLines(1); 79 } 80 editText.addTextChangedListener(mTextWatcher); 81 } 82 } 83 84 @Override 85 public void onBindViewHolder(PreferenceViewHolder holder) { 86 super.onBindViewHolder(holder); 87 88 final TextView textView = (TextView) holder.findViewById(android.R.id.summary); 89 if (textView == null) { 90 return; 91 } 92 if (mIsSummaryPassword) { 93 textView.setInputType( 94 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 95 } else { 96 textView.setInputType(InputType.TYPE_CLASS_TEXT); 97 } 98 } 99 100 public void setIsPassword(boolean isPassword) { 101 mIsPassword = isPassword; 102 } 103 104 public void setIsSummaryPassword(boolean isPassword) { 105 mIsSummaryPassword = isPassword; 106 } 107 108 @VisibleForTesting(otherwise = VisibleForTesting.NONE) 109 public boolean isPassword() { 110 return mIsPassword; 111 } 112 113 public void setValidator(Validator validator) { 114 mValidator = validator; 115 } 116 117 private class EditTextWatcher implements TextWatcher { 118 @Override 119 public void onTextChanged(CharSequence s, int start, int before, int count) { 120 } 121 122 @Override 123 public void beforeTextChanged(CharSequence s, int start, int before, int count) { 124 } 125 126 @Override 127 public void afterTextChanged(Editable s) { 128 final EditText editText = getEditText(); 129 if (mValidator != null && editText != null) { 130 final AlertDialog dialog = (AlertDialog) getDialog(); 131 final boolean valid = mValidator.isTextValid(editText.getText().toString()); 132 dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(valid); 133 } 134 } 135 } 136 137 } 138