• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.google.android.setupdesign.items;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import androidx.appcompat.widget.SwitchCompat;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.CompoundButton;
25 import com.google.android.setupdesign.R;
26 
27 /**
28  * An item that is displayed with a switch, with methods to manipulate and listen to the checked
29  * state of the switch. Note that by default, only click on the switch will change the on-off state.
30  * To change the switch state when tapping on the text, use the click handlers of list view or
31  * RecyclerItemAdapter with {@link #toggle(View)}.
32  */
33 public class SwitchItem extends Item implements CompoundButton.OnCheckedChangeListener {
34 
35   /** Listener for check state changes of this switch item. */
36   public interface OnCheckedChangeListener {
37 
38     /**
39      * Callback when checked state of a {@link SwitchItem} is changed.
40      *
41      * @see #setOnCheckedChangeListener(OnCheckedChangeListener)
42      */
onCheckedChange(SwitchItem item, boolean isChecked)43     void onCheckedChange(SwitchItem item, boolean isChecked);
44   }
45 
46   private boolean checked = false;
47   private OnCheckedChangeListener listener;
48 
49   /** Creates a default switch item. */
SwitchItem()50   public SwitchItem() {
51     super();
52   }
53 
54   /**
55    * Creates a switch item. This constructor is used for inflation from XML.
56    *
57    * @param context The context which this item is inflated in.
58    * @param attrs The XML attributes defined on the item.
59    */
SwitchItem(Context context, AttributeSet attrs)60   public SwitchItem(Context context, AttributeSet attrs) {
61     super(context, attrs);
62     final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SudSwitchItem);
63     checked = a.getBoolean(R.styleable.SudSwitchItem_android_checked, false);
64     a.recycle();
65   }
66 
67   /** Sets whether this item should be checked. */
setChecked(boolean checked)68   public void setChecked(boolean checked) {
69     if (this.checked != checked) {
70       this.checked = checked;
71       notifyItemChanged();
72       if (listener != null) {
73         listener.onCheckedChange(this, checked);
74       }
75     }
76   }
77 
78   /** @return True if this switch item is currently checked. */
isChecked()79   public boolean isChecked() {
80     return checked;
81   }
82 
83   @Override
getDefaultLayoutResource()84   protected int getDefaultLayoutResource() {
85     return R.layout.sud_items_switch;
86   }
87 
88   /**
89    * Toggle the checked state of the switch, without invalidating the entire item.
90    *
91    * @param view The root view of this item, typically from the argument of onItemClick.
92    */
toggle(View view)93   public void toggle(View view) {
94     checked = !checked;
95     final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.sud_items_switch);
96     switchView.setChecked(checked);
97   }
98 
99   @Override
onBindView(View view)100   public void onBindView(View view) {
101     super.onBindView(view);
102     final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.sud_items_switch);
103     switchView.setOnCheckedChangeListener(null);
104     switchView.setChecked(checked);
105     switchView.setOnCheckedChangeListener(this);
106     switchView.setEnabled(isEnabled());
107   }
108 
109   /**
110    * Sets a listener to listen for changes in checked state. This listener is invoked in both user
111    * toggling the switch and calls to {@link #setChecked(boolean)}.
112    */
setOnCheckedChangeListener(OnCheckedChangeListener listener)113   public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
114     this.listener = listener;
115   }
116 
117   @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)118   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
119     checked = isChecked;
120     if (listener != null) {
121       listener.onCheckedChange(this, isChecked);
122     }
123   }
124 }
125