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