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.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.settings.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     public PasswordLineItem(CharSequence title) {
     35         super(title, null);
     36     }
     37 
     38     @Override
     39     public int getType() {
     40         return PASSWORD_TYPE;
     41     }
     42 
     43     public static class ViewHolder extends EditTextLineItem.ViewHolder {
     44         public final CheckBox checkbox;
     45 
     46         public ViewHolder(View view) {
     47             super(view);
     48             checkbox = view.findViewById(R.id.checkbox);
     49         }
     50     }
     51 
     52     @Override
     53     public void bindViewHolder(PasswordLineItem.ViewHolder viewHolder) {
     54         super.setTextType(mShowPassword
     55                 ? TextType.VISIBLE_PASSWORD : TextType.HIDDEN_PASSWORD);
     56         viewHolder.checkbox.setChecked(mShowPassword);
     57         viewHolder.checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
     58             mShowPassword = isChecked;
     59             super.setTextType(mShowPassword
     60                     ? TextType.VISIBLE_PASSWORD : TextType.HIDDEN_PASSWORD);
     61             viewHolder.editText.setInputType(mTextType.getValue());
     62         });
     63         super.bindViewHolder(viewHolder);
     64     }
     65 
     66     @Override
     67     public void setTextType(TextType textType) {
     68         throw new IllegalArgumentException("checkbox will automatically set TextType.");
     69     }
     70 
     71     public static RecyclerView.ViewHolder createViewHolder(ViewGroup parent) {
     72         return new ViewHolder(LayoutInflater.from(parent.getContext())
     73                 .inflate(R.layout.password_line_item, parent, false));
     74     }
     75 }
     76