• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.AlertDialog;
20 import android.os.Bundle;
21 import android.text.Editable;
22 import android.text.TextWatcher;
23 import android.view.View;
24 import android.widget.EditText;
25 import android.widget.TextView;
26 
27 /**
28  * Adds optional text validation logic to {@link EditTextPreferenceDialogFragment}. Disables
29  * Positive Button and the ability to press Enter to submit the dialog if the input is invalid.
30  * Validator must be provided by {@link ValidatedEditTextPreference} before launching the Dialog
31  * Fragment for it to be attached to its View.
32  */
33 public class ValidatedEditTextPreferenceDialogFragment extends
34         EditTextPreferenceDialogFragment implements TextView.OnEditorActionListener {
35 
36     private final EditTextWatcher mTextWatcher = new EditTextWatcher();
37 
38     private ValidatedEditTextPreference.Validator mValidator;
39     private EditText mEditText;
40 
41     /**
42      * Returns a new instance of {@link ValidatedEditTextPreferenceDialogFragment} for the
43      * {@link ValidatedEditTextPreference} with the given {@code key}.
44      */
newInstance(String key)45     public static ValidatedEditTextPreferenceDialogFragment newInstance(String key) {
46         ValidatedEditTextPreferenceDialogFragment fragment =
47                 new ValidatedEditTextPreferenceDialogFragment();
48         Bundle b = new Bundle(/* capacity= */ 1);
49         b.putString(ARG_KEY, key);
50         fragment.setArguments(b);
51         return fragment;
52     }
53 
54     @Override
onBindDialogView(View view)55     protected void onBindDialogView(View view) {
56         super.onBindDialogView(view);
57         mEditText = view.findViewById(android.R.id.edit);
58         if (getPreference() instanceof ValidatedEditTextPreference) {
59             ValidatedEditTextPreference.Validator validator =
60                     ((ValidatedEditTextPreference) getPreference()).getValidator();
61             if (validator != null) {
62                 attachValidatorToView(view, validator);
63             }
64         }
65 
66     }
67 
68     @Override
onStart()69     public void onStart() {
70         super.onStart();
71         allowDialogSubmissionOnlyIfValidInput((AlertDialog) getDialog());
72     }
73 
attachValidatorToView(View view, ValidatedEditTextPreference.Validator validator)74     private void attachValidatorToView(View view, ValidatedEditTextPreference.Validator validator) {
75         mValidator = validator;
76         EditText editText = view.findViewById(android.R.id.edit);
77         if (mValidator != null && editText != null) {
78             editText.removeTextChangedListener(mTextWatcher);
79             editText.addTextChangedListener(mTextWatcher);
80         }
81     }
82 
83     private class EditTextWatcher implements TextWatcher {
84         @Override
onTextChanged(CharSequence s, int start, int before, int count)85         public void onTextChanged(CharSequence s, int start, int before, int count) {
86         }
87 
88         @Override
beforeTextChanged(CharSequence s, int start, int before, int count)89         public void beforeTextChanged(CharSequence s, int start, int before, int count) {
90         }
91 
92         @Override
afterTextChanged(Editable s)93         public void afterTextChanged(Editable s) {
94             allowDialogSubmissionOnlyIfValidInput((AlertDialog) getDialog());
95         }
96     }
97 
allowDialogSubmissionOnlyIfValidInput(AlertDialog dialog)98     private void allowDialogSubmissionOnlyIfValidInput(AlertDialog dialog) {
99         if (dialog != null && mValidator != null && mEditText != null) {
100             boolean valid = mValidator.isTextValid(mEditText.getText().toString());
101             dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(valid);
102             setAllowEnterToSubmit(valid);
103         }
104     }
105 }
106