Home | History | Annotate | Download | only in common
      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.car.settings.common;
     18 
     19 import android.support.annotation.Nullable;
     20 import android.support.v7.widget.RecyclerView;
     21 import android.text.Editable;
     22 import android.text.InputType;
     23 import android.text.TextWatcher;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.EditText;
     28 import android.widget.TextView;
     29 
     30 import com.android.car.settings.R;
     31 
     32 /**
     33  * Contains logic for a line item represents text only view of a title and a EditText as input.
     34  */
     35 public class EditTextLineItem<VH extends EditTextLineItem.ViewHolder>
     36         extends TypedPagedListAdapter.LineItem<VH> {
     37     private final CharSequence mTitle;
     38     private final CharSequence mInitialInputText;
     39 
     40     public interface TextChangeListener {
     41 
     42         void textChanged(Editable s);
     43     }
     44 
     45     private TextChangeListener mTextChangeListener;
     46     private EditText mEditText;
     47     protected TextType mTextType = TextType.NONE;
     48 
     49     public enum TextType {
     50         // None editable text
     51         NONE(0),
     52         // text input
     53         TEXT(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL),
     54         // password, input is replaced by dot
     55         HIDDEN_PASSWORD(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD),
     56         // password, visible.
     57         VISIBLE_PASSWORD(
     58                 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
     59 
     60         private int mValue;
     61 
     62         TextType(int value) {
     63           mValue = value;
     64         }
     65 
     66         public int getValue() {
     67             return mValue;
     68         }
     69     }
     70 
     71     public EditTextLineItem(CharSequence title) {
     72         this(title, null);
     73     }
     74 
     75     public EditTextLineItem(CharSequence title, CharSequence initialInputText) {
     76         mTitle = title;
     77         mInitialInputText = initialInputText;
     78     }
     79 
     80     public void setTextType(TextType textType) {
     81         mTextType = textType;
     82     }
     83 
     84     public void setTextChangeListener(TextChangeListener listener) {
     85         mTextChangeListener = listener;
     86     }
     87 
     88     @Nullable
     89     public String getInput() {
     90         return mEditText == null ? null : mEditText.getText().toString();
     91     }
     92 
     93     @Override
     94     public int getType() {
     95         return EDIT_TEXT_TYPE;
     96     }
     97 
     98     @Override
     99     public void bindViewHolder(VH viewHolder) {
    100         viewHolder.titleView.setText(mTitle);
    101         mEditText = viewHolder.editText;
    102         mEditText.setInputType(mTextType.getValue());
    103         if (mInitialInputText != null) {
    104             mEditText.setText(mInitialInputText);
    105         }
    106         mEditText.addTextChangedListener(new TextWatcher() {
    107             @Override
    108             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    109                 // don't care
    110             }
    111 
    112             @Override
    113             public void onTextChanged(CharSequence s, int start, int before, int count) {
    114                 // dont' care
    115             }
    116 
    117             @Override
    118             public void afterTextChanged(Editable s) {
    119                 if (mTextChangeListener != null) {
    120                     mTextChangeListener.textChanged(s);
    121                 }
    122             }
    123         });
    124     }
    125 
    126     public static class ViewHolder extends RecyclerView.ViewHolder {
    127         public final TextView titleView;
    128         public final EditText editText;
    129 
    130         public ViewHolder(View view) {
    131             super(view);
    132             titleView = view.findViewById(R.id.title);
    133             editText = view.findViewById(R.id.input);
    134         }
    135     }
    136 
    137     public static RecyclerView.ViewHolder createViewHolder(ViewGroup parent) {
    138         return new ViewHolder(LayoutInflater.from(parent.getContext())
    139                 .inflate(R.layout.edit_text_line_item, parent, false));
    140     }
    141 
    142     @Override
    143     public CharSequence getDesc() {
    144         return null;
    145     }
    146 
    147     @Override
    148     public boolean isExpandable() {
    149         return false;
    150     }
    151 }
    152