Home | History | Annotate | Download | only in list
      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.list;
     18 
     19 import android.support.v7.widget.RecyclerView;
     20 import android.view.LayoutInflater;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 import android.widget.CheckBox;
     24 
     25 import com.android.car.list.R;
     26 
     27 /**
     28  * Contains logic for a line item represents text only view of a title and a EditText
     29  * as password input followed by a checkbox to toggle between show/hide password.
     30  */
     31 public class PasswordLineItem extends EditTextLineItem<PasswordLineItem.ViewHolder> {
     32     private boolean mShowPassword = false;
     33 
     34     /**
     35      * Constructs PasswordLineItem with an empty default EditText
     36      */
     37     public PasswordLineItem(CharSequence title) {
     38         super(title, null);
     39     }
     40 
     41     @Override
     42     public int getType() {
     43         return PASSWORD_TYPE;
     44     }
     45 
     46     /**
     47      * ViewHolder that extends the EditTextLineItem ViewHolder and adds
     48      * a show password checkbox.
     49      */
     50     public static class ViewHolder extends EditTextLineItem.ViewHolder {
     51         public final CheckBox checkbox;
     52 
     53         public ViewHolder(View view) {
     54             super(view);
     55             checkbox = view.findViewById(R.id.checkbox);
     56         }
     57     }
     58 
     59     @Override
     60     public void bindViewHolder(PasswordLineItem.ViewHolder viewHolder) {
     61         // setTextType is public but with PasswordLineItem it should only be
     62         // set to be one of the two types as follows so we use super and
     63         // throw exception on our setTextType.
     64         super.setTextType(mShowPassword ? TextType.VISIBLE_PASSWORD : TextType.HIDDEN_PASSWORD);
     65         super.bindViewHolder(viewHolder);
     66         viewHolder.checkbox.setChecked(mShowPassword);
     67         viewHolder.checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
     68             mShowPassword = isChecked;
     69             super.setTextType(mShowPassword
     70                     ? TextType.VISIBLE_PASSWORD : TextType.HIDDEN_PASSWORD);
     71             viewHolder.editText.setInputType(mTextType.getValue());
     72         });
     73     }
     74 
     75     @Override
     76     public void setTextType(TextType textType) {
     77         throw new IllegalArgumentException("checkbox will automatically set TextType.");
     78     }
     79 
     80     /**
     81      * Creates a ViewHolder with the elements of a PasswordLineItem contained
     82      */
     83     public static RecyclerView.ViewHolder createViewHolder(ViewGroup parent) {
     84         return new ViewHolder(LayoutInflater.from(parent.getContext())
     85                 .inflate(R.layout.password_line_item, parent, false));
     86     }
     87 }
     88