16e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam/* 26e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * Copyright (C) 2015 The Android Open Source Project 36e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * 46e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * Licensed under the Apache License, Version 2.0 (the "License"); 56e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * you may not use this file except in compliance with the License. 66e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * You may obtain a copy of the License at 76e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * 86e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * http://www.apache.org/licenses/LICENSE-2.0 96e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * 106e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * Unless required by applicable law or agreed to in writing, software 116e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * distributed under the License is distributed on an "AS IS" BASIS, 126e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * See the License for the specific language governing permissions and 146e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * limitations under the License. 156e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam */ 166e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 176e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lampackage com.android.setupwizardlib.items; 186e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 196e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lamimport android.content.Context; 206e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lamimport android.content.res.TypedArray; 216e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lamimport android.support.v7.widget.SwitchCompat; 226e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lamimport android.util.AttributeSet; 236e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lamimport android.view.View; 246e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lamimport android.widget.CompoundButton; 256e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 266e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lamimport com.android.setupwizardlib.R; 276e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 286e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam/** 296e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * An Item with a switch, which the user can 306e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam */ 316e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lampublic class SwitchItem extends Item implements CompoundButton.OnCheckedChangeListener { 326e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 336e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam public interface OnCheckedChangeListener { 346e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam void onCheckedChange(SwitchItem item, boolean isChecked); 356e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 366e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 376e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam private boolean mChecked = false; 386e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam private OnCheckedChangeListener mListener; 396e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 406e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam public SwitchItem() { 416e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam super(); 426e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 436e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 446e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam public SwitchItem(Context context, AttributeSet attrs) { 456e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam super(context, attrs); 466e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwSwitchItem); 476e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam mChecked = a.getBoolean(R.styleable.SuwSwitchItem_android_checked, false); 486e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam a.recycle(); 496e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 506e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 516e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam public void setChecked(boolean checked) { 526e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam if (mChecked != checked) { 536e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam mChecked = checked; 546e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam notifyChanged(); 556e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam if (mListener != null) { 566e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam mListener.onCheckedChange(this, checked); 576e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 586e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 596e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 606e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 616e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam public boolean isChecked() { 626e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam return mChecked; 636e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 646e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 65de9f081976b6121f93b6debca4554226c125e12fMaurice Lam @Override 666e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam protected int getDefaultLayoutResource() { 676e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam return R.layout.suw_items_switch; 686e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 696e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 706e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam /** 716e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * Toggle the checked state of the switch, without invalidating the entire item. 726e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * 736e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam * @param view The root view of this item, typically from the argument of onItemClick. 746e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam */ 756e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam public void toggle(View view) { 766e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam mChecked = !mChecked; 776e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.suw_items_switch); 786e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam switchView.setChecked(mChecked); 796e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 806e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 816e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam @Override 826e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam public void onBindView(View view) { 836e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam super.onBindView(view); 846e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.suw_items_switch); 856e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam switchView.setChecked(mChecked); 866e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam switchView.setOnCheckedChangeListener(this); 876e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam switchView.setEnabled(isEnabled()); 886e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 896e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 906e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam public void setOnCheckedChangeListener(OnCheckedChangeListener listener) { 916e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam mListener = listener; 926e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 936e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam 946e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam @Override 956e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 966e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam if (mListener != null) { 976e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam mListener.onCheckedChange(this, isChecked); 986e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 996e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam } 1006e55f30c31f1ac3b35d60f306cf6cef084b1a845Maurice Lam} 101