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.content.Context; 20 import android.util.AttributeSet; 21 import android.widget.TextView; 22 23 import androidx.preference.EditTextPreference; 24 import androidx.preference.PreferenceViewHolder; 25 26 import com.android.car.ui.preference.CarUiEditTextPreference; 27 import com.android.car.ui.preference.EditTextPreferenceDialogFragment; 28 29 /** 30 * Extends {@link EditTextPreference} to add optional {@link Validator} logic. Validator is passed 31 * on to {@link ValidatedEditTextPreferenceDialogFragment} to be attached to its View. 32 */ 33 public class ValidatedEditTextPreference extends CarUiEditTextPreference { 34 /** Defines the validation logic used in this preference. */ 35 public interface Validator { 36 /** Returns true only if the value provided meets validation criteria. */ isTextValid(String value)37 boolean isTextValid(String value); 38 } 39 40 private Validator mValidator; 41 private int mInputType; 42 ValidatedEditTextPreference(Context context)43 public ValidatedEditTextPreference(Context context) { 44 super(context); 45 } 46 ValidatedEditTextPreference(Context context, AttributeSet attrs)47 public ValidatedEditTextPreference(Context context, AttributeSet attrs) { 48 super(context, attrs); 49 } 50 ValidatedEditTextPreference(Context context, AttributeSet attrs, int defStyle)51 public ValidatedEditTextPreference(Context context, AttributeSet attrs, int defStyle) { 52 super(context, attrs, defStyle); 53 } 54 ValidatedEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)55 public ValidatedEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, 56 int defStyleRes) { 57 super(context, attrs, defStyleAttr, defStyleRes); 58 } 59 60 /** Sets the text input type of the summary field. */ setSummaryInputType(int inputType)61 public void setSummaryInputType(int inputType) { 62 mInputType = inputType; 63 notifyChanged(); 64 } 65 66 /** Gets the text input type of the summary field. */ getSummaryInputType()67 public int getSummaryInputType() { 68 return mInputType; 69 } 70 71 /** 72 * Sets the {@link Validator}. Validator has to be set before the Preference is 73 * clicked for it to be attached to the Dialog's EditText. 74 */ setValidator(Validator validator)75 public void setValidator(Validator validator) { 76 mValidator = validator; 77 } 78 79 /** 80 * Gets the {@link Validator} for {@link EditTextPreferenceDialogFragment} to attach 81 * to its own View. 82 */ getValidator()83 public Validator getValidator() { 84 return mValidator; 85 } 86 87 @Override onBindViewHolder(PreferenceViewHolder holder)88 public void onBindViewHolder(PreferenceViewHolder holder) { 89 super.onBindViewHolder(holder); 90 91 TextView summaryView = (TextView) holder.findViewById(android.R.id.summary); 92 summaryView.setInputType(mInputType); 93 } 94 } 95