• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.Context;
21 import android.support.v7.widget.RecyclerView;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.ImageView;
26 import android.widget.Switch;
27 import android.widget.TextView;
28 
29 import com.android.car.settings.R;
30 
31 /**
32  * Contains logic for a line item represents title text, description text and a toggle widget.
33  */
34 public abstract class IconToggleLineItem
35         extends TypedPagedListAdapter.LineItem<IconToggleLineItem.ViewHolder> {
36     protected final Context mContext;
37     private final CharSequence mTitle;
38     protected IconUpdateListener mIconUpdateListener;
39 
40     public interface IconUpdateListener {
onUpdateIcon(@rawableRes int iconRes)41         void onUpdateIcon(@DrawableRes int iconRes);
42     }
43 
44     private final View.OnClickListener mOnClickListener = v -> onClicked();
45 
46     private final Switch.OnCheckedChangeListener mOnCheckedChangeListener =
47             (view, isChecked) -> onToggleClicked(isChecked);
48 
IconToggleLineItem(CharSequence title, Context context)49     public IconToggleLineItem(CharSequence title, Context context) {
50         mTitle = title;
51         mContext = context;
52     }
53 
getType()54     public int getType() {
55         return ICON_TOGGLE_TYPE;
56     }
57 
bindViewHolder(ViewHolder viewHolder)58     public void bindViewHolder(ViewHolder viewHolder) {
59         viewHolder.title.setText(mTitle);
60         viewHolder.summary.setText(getDesc());
61         viewHolder.toggle.setChecked(isChecked());
62         viewHolder.onUpdateIcon(getIcon());
63         viewHolder.itemView.setOnClickListener(mOnClickListener);
64         viewHolder.toggle.setOnCheckedChangeListener(mOnCheckedChangeListener);
65         mIconUpdateListener = viewHolder;
66     }
67 
68     static class ViewHolder extends RecyclerView.ViewHolder implements IconUpdateListener {
69         public final ImageView icon;
70         public final TextView title;
71         public final TextView summary;
72         public final Switch toggle;
73 
ViewHolder(View itemView)74         public ViewHolder(View itemView) {
75             super(itemView);
76             icon = (ImageView) itemView.findViewById(R.id.icon);
77             title = (TextView) itemView.findViewById(R.id.title);
78             summary = (TextView) itemView.findViewById(R.id.desc);
79             toggle = (Switch) itemView.findViewById(R.id.toggle_switch);
80         }
81 
82         @Override
onUpdateIcon(@rawableRes int iconRes)83         public void onUpdateIcon(@DrawableRes int iconRes) {
84             icon.setImageResource(iconRes);
85         }
86     }
87 
createViewHolder(ViewGroup parent)88     public static RecyclerView.ViewHolder createViewHolder(ViewGroup parent) {
89         View v = LayoutInflater.from(parent.getContext())
90                 .inflate(R.layout.icon_toggle_line_item, parent, false);
91         return new ViewHolder(v);
92     }
93 
94     /**
95      * Called when any part of the line is clicked.
96      * @param isChecked the state of the switch widget at the time of click.
97      */
onToggleClicked(boolean isChecked)98     public abstract void onToggleClicked(boolean isChecked);
99 
100     /**
101      * called when anywhere other than the toggle on the line item got clicked.
102      */
onClicked()103     public abstract void onClicked();
104 
isChecked()105     public abstract boolean isChecked();
106 
getIcon()107     public abstract @DrawableRes int getIcon();
108 
109     @Override
isClickable()110     public boolean isClickable() {
111         return true;
112     }
113 }
114