SwitchItem.java revision 6e55f30c31f1ac3b35d60f306cf6cef084b1a845
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
17package com.android.setupwizardlib.items;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.support.v7.widget.SwitchCompat;
22import android.util.AttributeSet;
23import android.view.View;
24import android.widget.CompoundButton;
25
26import com.android.setupwizardlib.R;
27
28/**
29 * An Item with a switch, which the user can
30 */
31public class SwitchItem extends Item implements CompoundButton.OnCheckedChangeListener {
32
33    public interface OnCheckedChangeListener {
34        void onCheckedChange(SwitchItem item, boolean isChecked);
35    }
36
37    private boolean mChecked = false;
38    private OnCheckedChangeListener mListener;
39
40    public SwitchItem() {
41        super();
42    }
43
44    public SwitchItem(Context context, AttributeSet attrs) {
45        super(context, attrs);
46        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwSwitchItem);
47        mChecked = a.getBoolean(R.styleable.SuwSwitchItem_android_checked, false);
48        a.recycle();
49    }
50
51    public void setChecked(boolean checked) {
52        if (mChecked != checked) {
53            mChecked = checked;
54            notifyChanged();
55            if (mListener != null) {
56                mListener.onCheckedChange(this, checked);
57            }
58        }
59    }
60
61    public boolean isChecked() {
62        return mChecked;
63    }
64
65    protected int getDefaultLayoutResource() {
66        return R.layout.suw_items_switch;
67    }
68
69    /**
70     * Toggle the checked state of the switch, without invalidating the entire item.
71     *
72     * @param view The root view of this item, typically from the argument of onItemClick.
73     */
74    public void toggle(View view) {
75        mChecked = !mChecked;
76        final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.suw_items_switch);
77        switchView.setChecked(mChecked);
78    }
79
80    @Override
81    public void onBindView(View view) {
82        super.onBindView(view);
83        final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.suw_items_switch);
84        switchView.setChecked(mChecked);
85        switchView.setOnCheckedChangeListener(this);
86        switchView.setEnabled(isEnabled());
87    }
88
89    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
90        mListener = listener;
91    }
92
93    @Override
94    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
95        if (mListener != null) {
96            mListener.onCheckedChange(this, isChecked);
97        }
98    }
99}
100