summaryrefslogtreecommitdiff
path: root/library/gingerbread/src/com/android/setupwizardlib/items/SwitchItem.java
blob: 8d828ac0385653924906cbb78a45af63cacf67b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.setupwizardlib.items;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CompoundButton;

import androidx.appcompat.widget.SwitchCompat;

import com.android.setupwizardlib.R;

/**
 * An item that is displayed with a switch, with methods to manipulate and listen to the checked
 * state of the switch. Note that by default, only click on the switch will change the on-off state.
 * To change the switch state when tapping on the text, use the click handlers of list view or
 * RecyclerItemAdapter with {@link #toggle(View)}.
 */
public class SwitchItem extends Item implements CompoundButton.OnCheckedChangeListener {

    /**
     * Listener for check state changes of this switch item.
     */
    public interface OnCheckedChangeListener {

        /**
         * Callback when checked state of a {@link SwitchItem} is changed.
         *
         * @see #setOnCheckedChangeListener(OnCheckedChangeListener)
         */
        void onCheckedChange(SwitchItem item, boolean isChecked);
    }

    private boolean mChecked = false;
    private OnCheckedChangeListener mListener;

    /**
     * Creates a default switch item.
     */
    public SwitchItem() {
        super();
    }

    /**
     * Creates a switch item. This constructor is used for inflation from XML.
     *
     * @param context The context which this item is inflated in.
     * @param attrs The XML attributes defined on the item.
     */
    public SwitchItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwSwitchItem);
        mChecked = a.getBoolean(R.styleable.SuwSwitchItem_android_checked, false);
        a.recycle();
    }

    /**
     * Sets whether this item should be checked.
     */
    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            notifyItemChanged();
            if (mListener != null) {
                mListener.onCheckedChange(this, checked);
            }
        }
    }

    /**
     * @return True if this switch item is currently checked.
     */
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    protected int getDefaultLayoutResource() {
        return R.layout.suw_items_switch;
    }

    /**
     * Toggle the checked state of the switch, without invalidating the entire item.
     *
     * @param view The root view of this item, typically from the argument of onItemClick.
     */
    public void toggle(View view) {
        mChecked = !mChecked;
        final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.suw_items_switch);
        switchView.setChecked(mChecked);
    }

    @Override
    public void onBindView(View view) {
        super.onBindView(view);
        final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.suw_items_switch);
        switchView.setOnCheckedChangeListener(null);
        switchView.setChecked(mChecked);
        switchView.setOnCheckedChangeListener(this);
        switchView.setEnabled(isEnabled());
    }

    /**
     * Sets a listener to listen for changes in checked state. This listener is invoked in both
     * user toggling the switch and calls to {@link #setChecked(boolean)}.
     */
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        mListener = listener;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        mChecked = isChecked;
        if (mListener != null) {
            mListener.onCheckedChange(this, isChecked);
        }
    }
}