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.car.ui.PagedListView;
25 import android.support.v7.widget.RecyclerView;
26 import android.support.v7.widget.RecyclerView.ViewHolder;
27 import android.view.ViewGroup;
28 
29 import com.android.car.settings.R;
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     private static final String TAG = "TypedPagedListAdapter";
41 
42     private final Context mContext;
43     private final ArrayList<LineItem> mContentList;
44 
TypedPagedListAdapter(@onNull Context context, ArrayList<LineItem> contentList)45     public TypedPagedListAdapter(@NonNull Context context, ArrayList<LineItem> contentList) {
46         mContext = context;
47         mContentList = contentList;
48     }
49 
isEmpty()50     public boolean isEmpty() {
51         return mContentList.isEmpty();
52     }
53 
54     public static abstract class LineItem<VH extends ViewHolder> {
55         @Retention(SOURCE)
56         @IntDef({TEXT_TYPE,
57                 TOGGLE_TYPE,
58                 ICON_TEXT_TYPE,
59                 SEEKBAR_TYPE,
60                 ICON_TOGGLE_TYPE,
61                 SIMPLE_ICON_TEXT_TYPE})
62         public @interface LineItemType {}
63 
64         // with one title and one description
65         static final int TEXT_TYPE = 1;
66 
67         // with one tile, one description, and a toggle on the right.
68         static final int TOGGLE_TYPE = 2;
69 
70         // with one icon, one tile and one description.
71         static final int ICON_TEXT_TYPE = 3;
72 
73         // with one tile and one seekbar.
74         static final int SEEKBAR_TYPE = 4;
75 
76         // with one icon, title, description and a toggle.
77         static final int ICON_TOGGLE_TYPE = 5;
78 
79         // similar to ICON_TEXT_TYPE, but with a different layout.
80         static final int SIMPLE_ICON_TEXT_TYPE = 6;
81 
82         @LineItemType
getType()83         abstract int getType();
84 
bindViewHolder(VH holder)85         abstract void bindViewHolder(VH holder);
86 
getDesc()87         public abstract CharSequence getDesc();
88     }
89 
90     @Override
onCreateViewHolder(ViewGroup parent, int viewType)91     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
92         switch (viewType) {
93             case LineItem.TEXT_TYPE:
94                 return TextLineItem.createViewHolder(parent);
95             case LineItem.TOGGLE_TYPE:
96                 return ToggleLineItem.createViewHolder(parent);
97             case LineItem.ICON_TEXT_TYPE:
98                 return IconTextLineItem.createViewHolder(parent);
99             case LineItem.SEEKBAR_TYPE:
100                 return SeekbarLineItem.createViewHolder(parent);
101             case LineItem.ICON_TOGGLE_TYPE:
102                 return IconToggleLineItem.createViewHolder(parent);
103             case LineItem.SIMPLE_ICON_TEXT_TYPE:
104                 return SimpleIconLineItem.createViewHolder(parent);
105             default:
106                 throw new IllegalStateException("ViewType not supported: " + viewType);
107         }
108     }
109 
110     @Override
onBindViewHolder(ViewHolder holder, int position)111     public void onBindViewHolder(ViewHolder holder, int position) {
112         mContentList.get(position).bindViewHolder(holder);
113     }
114 
115     @Override
116     @LineItem.LineItemType
getItemViewType(int position)117     public int getItemViewType(int position) {
118         return mContentList.get(position).getType();
119     }
120 
121     @Override
getItemCount()122     public int getItemCount() {
123         return mContentList.size();
124     }
125 
126     @Override
setMaxItems(int maxItems)127     public void setMaxItems(int maxItems) {
128         // no limit in this list.
129     }
130 }
131