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