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.annotation.DrawableRes;
20 import android.support.v7.widget.RecyclerView;
21 import android.text.TextUtils;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.ImageView;
26 import android.widget.TextView;
27 
28 import com.android.car.settings.R;
29 
30 /**
31  * Contains logic for a line item represents icon and texts of a title and a description.
32  */
33 public abstract class IconTextLineItem
34         extends TypedPagedListAdapter.LineItem<IconTextLineItem.ViewHolder> {
35     private final CharSequence mTitle;
36     @DrawableRes
37     private final int mIconRes;
38 
39     private View.OnClickListener mOnClickListener = (v) -> onClick();
40 
IconTextLineItem(CharSequence title, @DrawableRes int iconRes)41     public IconTextLineItem(CharSequence title, @DrawableRes int iconRes) {
42         mTitle = title;
43         mIconRes = iconRes;
44     }
45 
46     @Override
getType()47     public int getType() {
48         return ICON_TEXT_TYPE;
49     }
50 
51     @Override
bindViewHolder(ViewHolder viewHolder)52     public void bindViewHolder(ViewHolder viewHolder) {
53         viewHolder.titleView.setText(mTitle);
54         viewHolder.iconView.setImageResource(mIconRes);
55         CharSequence desc = getDesc();
56         if (TextUtils.isEmpty(desc)) {
57             viewHolder.descView.setVisibility(View.GONE);
58         } else {
59             viewHolder.descView.setVisibility(View.VISIBLE);
60             viewHolder.descView.setText(desc);
61         }
62         viewHolder.itemView.setOnClickListener(mOnClickListener);
63         viewHolder.itemView.setEnabled(isEnabled());
64     }
65 
66     static class ViewHolder extends RecyclerView.ViewHolder {
67         final TextView titleView;
68         final TextView descView;
69         final ImageView iconView;
70 
ViewHolder(View view)71         public ViewHolder(View view) {
72             super(view);
73             iconView = (ImageView) view.findViewById(R.id.icon);
74             titleView = (TextView) view.findViewById(R.id.title);
75             descView = (TextView) view.findViewById(R.id.desc);
76         }
77     }
78 
createViewHolder(ViewGroup parent)79     public static RecyclerView.ViewHolder createViewHolder(ViewGroup parent) {
80         View v = LayoutInflater.from(parent.getContext())
81                 .inflate(R.layout.icon_text_line_item, parent, false);
82         return new ViewHolder(v);
83     }
84 
onClick()85     public abstract void onClick();
86 
isEnabled()87     public abstract boolean isEnabled();
88 }
89