• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.text.Editable;
23 import android.text.InputType;
24 import android.text.TextUtils;
25 import android.text.TextWatcher;
26 import android.util.AttributeSet;
27 import android.util.Log;
28 import android.view.View;
29 import android.widget.EditText;
30 
31 import com.android.settingslib.CustomEditTextPreference;
32 
33 /**
34  * {@code EditTextPreference} that supports input validation.
35  */
36 public class ValidatedEditTextPreference extends CustomEditTextPreference {
37 
38     public interface Validator {
isTextValid(String value)39         boolean isTextValid(String value);
40     }
41 
42     private final EditTextWatcher mTextWatcher = new EditTextWatcher();
43     private Validator mValidator;
44     private boolean mIsPassword;
45 
ValidatedEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)46     public ValidatedEditTextPreference(Context context, AttributeSet attrs,
47             int defStyleAttr, int defStyleRes) {
48         super(context, attrs, defStyleAttr, defStyleRes);
49     }
50 
ValidatedEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr)51     public ValidatedEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
52         super(context, attrs, defStyleAttr);
53     }
54 
ValidatedEditTextPreference(Context context, AttributeSet attrs)55     public ValidatedEditTextPreference(Context context, AttributeSet attrs) {
56         super(context, attrs);
57     }
58 
ValidatedEditTextPreference(Context context)59     public ValidatedEditTextPreference(Context context) {
60         super(context);
61     }
62 
63     @Override
onBindDialogView(View view)64     protected void onBindDialogView(View view) {
65         super.onBindDialogView(view);
66         final EditText editText = view.findViewById(android.R.id.edit);
67         if (editText != null && !TextUtils.isEmpty(editText.getText())) {
68             editText.setSelection(editText.getText().length());
69         }
70         if (mValidator != null && editText != null) {
71             editText.removeTextChangedListener(mTextWatcher);
72             if (mIsPassword) {
73                 editText.setInputType(
74                         InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
75                 editText.setMaxLines(1);
76             }
77             editText.addTextChangedListener(mTextWatcher);
78         }
79     }
80 
setIsPassword(boolean isPassword)81     public void setIsPassword(boolean isPassword) {
82         mIsPassword = isPassword;
83     }
84 
85     @VisibleForTesting(otherwise = VisibleForTesting.NONE)
isPassword()86     public boolean isPassword() {
87         return mIsPassword;
88     }
89 
setValidator(Validator validator)90     public void setValidator(Validator validator) {
91         mValidator = validator;
92     }
93 
94     private class EditTextWatcher implements TextWatcher {
95         @Override
onTextChanged(CharSequence s, int start, int before, int count)96         public void onTextChanged(CharSequence s, int start, int before, int count) {
97         }
98 
99         @Override
beforeTextChanged(CharSequence s, int start, int before, int count)100         public void beforeTextChanged(CharSequence s, int start, int before, int count) {
101         }
102 
103         @Override
afterTextChanged(Editable s)104         public void afterTextChanged(Editable s) {
105             final EditText editText = getEditText();
106             if (mValidator != null && editText != null) {
107                 final AlertDialog dialog = (AlertDialog) getDialog();
108                 final boolean valid = mValidator.isTextValid(editText.getText().toString());
109                 dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(valid);
110             }
111         }
112     }
113 
114 }
115