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 static java.lang.annotation.RetentionPolicy.SOURCE; 20 21 import android.annotation.IntDef; 22 import android.annotation.NonNull; 23 import android.content.Context; 24 import android.support.v7.widget.RecyclerView; 25 import android.support.v7.widget.RecyclerView.ViewHolder; 26 import android.view.ViewGroup; 27 28 import com.android.car.settings.R; 29 import com.android.car.view.PagedListView; 30 31 import java.lang.annotation.Retention; 32 import java.util.ArrayList; 33 34 /** 35 * Renders all types of LineItem to a view to be displayed as a row in a list. 36 */ 37 public class TypedPagedListAdapter 38 extends RecyclerView.Adapter<ViewHolder> 39 implements PagedListView.ItemCap { 40 41 private final Context mContext; 42 private ArrayList<LineItem> mContentList; 43 44 public TypedPagedListAdapter(@NonNull Context context) { 45 this(context, new ArrayList<>()); 46 } 47 48 public TypedPagedListAdapter(@NonNull Context context, ArrayList<LineItem> contentList) { 49 mContext = context; 50 mContentList = contentList; 51 } 52 53 public void updateList(ArrayList<LineItem> contentList) { 54 mContentList = contentList; 55 notifyDataSetChanged(); 56 } 57 58 public boolean isEmpty() { 59 return mContentList.isEmpty(); 60 } 61 62 public static abstract class LineItem<VH extends ViewHolder> { 63 @Retention(SOURCE) 64 @IntDef({TEXT_TYPE, 65 TOGGLE_TYPE, 66 ICON_TEXT_TYPE, 67 SEEKBAR_TYPE, 68 ICON_TOGGLE_TYPE, 69 CHECKBOX_TYPE, 70 EDIT_TEXT_TYPE, 71 SINGLE_TEXT_TYPE, 72 SPINNER_TYPE, 73 PASSWORD_TYPE}) 74 public @interface LineItemType {} 75 76 // with one title and one description 77 static final int TEXT_TYPE = 1; 78 79 // with one tile, one description, and a toggle on the right. 80 static final int TOGGLE_TYPE = 2; 81 82 // with one icon, one tile and one description. 83 static final int ICON_TEXT_TYPE = 3; 84 85 // with one tile and one seekbar. 86 static final int SEEKBAR_TYPE = 4; 87 88 // with one icon, title, description and a toggle. 89 static final int ICON_TOGGLE_TYPE = 5; 90 91 // with one icon, title and a checkbox. 92 static final int CHECKBOX_TYPE = 6; 93 94 // with one title and a EditText. 95 static final int EDIT_TEXT_TYPE = 7; 96 97 // with one title. 98 static final int SINGLE_TEXT_TYPE = 8; 99 100 // with a spinner. 101 static final int SPINNER_TYPE = 9; 102 103 // with a password input window and a checkbox for show password or not. 104 static final int PASSWORD_TYPE = 10; 105 106 @LineItemType 107 abstract int getType(); 108 109 abstract void bindViewHolder(VH holder); 110 111 public abstract CharSequence getDesc(); 112 113 /** 114 * Returns true if this item is ready to receive touch. If it's set to false, 115 * this item maybe not clickable or temporarily not functional. 116 */ 117 public boolean isEnabled() { 118 return true; 119 } 120 121 /** 122 * Returns true if some component on this item can be clicked. 123 */ 124 public boolean isClickable() { 125 return isExpandable(); 126 } 127 128 /** 129 * Returns true if this item can launch another activity or fragment. 130 */ 131 public abstract boolean isExpandable(); 132 } 133 134 @Override 135 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 136 switch (viewType) { 137 case LineItem.TEXT_TYPE: 138 return TextLineItem.createViewHolder(parent); 139 case LineItem.TOGGLE_TYPE: 140 return ToggleLineItem.createViewHolder(parent); 141 case LineItem.ICON_TEXT_TYPE: 142 return IconTextLineItem.createViewHolder(parent); 143 case LineItem.SEEKBAR_TYPE: 144 return SeekbarLineItem.createViewHolder(parent); 145 case LineItem.ICON_TOGGLE_TYPE: 146 return IconToggleLineItem.createViewHolder(parent); 147 case LineItem.CHECKBOX_TYPE: 148 return CheckBoxLineItem.createViewHolder(parent); 149 case LineItem.EDIT_TEXT_TYPE: 150 return EditTextLineItem.createViewHolder(parent); 151 case LineItem.SINGLE_TEXT_TYPE: 152 return SingleTextLineItem.createViewHolder(parent); 153 case LineItem.SPINNER_TYPE: 154 return SpinnerLineItem.createViewHolder(parent); 155 case LineItem.PASSWORD_TYPE: 156 return PasswordLineItem.createViewHolder(parent); 157 default: 158 throw new IllegalStateException("ViewType not supported: " + viewType); 159 } 160 } 161 162 @Override 163 public void onBindViewHolder(ViewHolder holder, int position) { 164 mContentList.get(position).bindViewHolder(holder); 165 } 166 167 @Override 168 @LineItem.LineItemType 169 public int getItemViewType(int position) { 170 return mContentList.get(position).getType(); 171 } 172 173 @Override 174 public int getItemCount() { 175 return mContentList.size(); 176 } 177 178 @Override 179 public void setMaxItems(int maxItems) { 180 // no limit in this list. 181 } 182 } 183