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.text.TextUtils;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.Switch;
25 import android.widget.TextView;
26 import com.android.car.settings.R;
27 
28 /**
29  * Contains logic for a line item represents title text, description text and a toggle widget.
30  */
31 public abstract class ToggleLineItem
32         extends TypedPagedListAdapter.LineItem<ToggleLineItem.ToggleLineItemViewHolder> {
33     private final CharSequence mTitle;
34 
35     private View.OnClickListener mOnClickListener = (v) -> {
36             Switch switchToggle = (Switch) v.findViewById(R.id.toggle_switch);
37             ToggleLineItem.this.onClick(switchToggle.isChecked());
38         };
39 
ToggleLineItem(CharSequence title)40     public ToggleLineItem(CharSequence title) {
41         mTitle = title;
42     }
43 
getType()44     public int getType() {
45         return TOGGLE_TYPE;
46     }
47 
bindViewHolder(ToggleLineItemViewHolder viewHolder)48     public void bindViewHolder(ToggleLineItemViewHolder viewHolder) {
49         viewHolder.titleView.setText(mTitle);
50         CharSequence desc = getDesc();
51         if (TextUtils.isEmpty(desc)) {
52             viewHolder.descView.setVisibility(View.GONE);
53         } else {
54             viewHolder.descView.setVisibility(View.VISIBLE);
55             viewHolder.descView.setText(desc);
56         }
57         viewHolder.toggle.setChecked(isChecked());
58         viewHolder.itemView.setOnClickListener(mOnClickListener);
59     }
60 
61     public static class ToggleLineItemViewHolder extends RecyclerView.ViewHolder {
62         final TextView titleView;
63         final TextView descView;
64         public final Switch toggle;
65 
ToggleLineItemViewHolder(View view)66         public ToggleLineItemViewHolder(View view) {
67             super(view);
68             titleView = (TextView) view.findViewById(R.id.title);
69             descView = (TextView) view.findViewById(R.id.desc);
70             toggle = (Switch) view.findViewById(R.id.toggle_switch);
71         }
72     }
73 
createViewHolder(ViewGroup parent)74     public static RecyclerView.ViewHolder createViewHolder(ViewGroup parent) {
75         View v = LayoutInflater.from(parent.getContext())
76                 .inflate(R.layout.toggle_line_item, parent, false);
77         return new ToggleLineItemViewHolder(v);
78     }
79 
80     /**
81      * Called when any part of the line is clicked.
82      * @param isChecked the state of the switch widget at the time of click.
83      */
onClick(boolean isChecked)84     public abstract void onClick(boolean isChecked);
85 
isChecked()86     public abstract boolean isChecked();
87 }
88