summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/dashboard/GenericSwitchToggle.java
blob: 95eec5cf31f21eeb5be8a017f55f4745927ce9d3 (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
package com.android.settings.dashboard;

import android.content.Context;
import android.widget.CompoundButton;
import android.widget.Switch;
import com.android.settings.widget.SwitchBar;

public abstract class GenericSwitchToggle implements SwitchBar.OnSwitchChangeListener,
        CompoundButton.OnCheckedChangeListener {

    protected Context mContext;
    protected Switch mSwitch;
    protected SwitchBar mSwitchBar;

    protected boolean mStateMachineEvent;
    protected boolean mListeningToOnSwitchChange = false;

    public GenericSwitchToggle(Context context, Switch switch_) {
        mContext = context;
        mSwitch = switch_;
    }

    public GenericSwitchToggle(Context context, SwitchBar switch_) {
        mContext = context;
        mSwitchBar = switch_;
    }

    public void pause() {
        if (mListeningToOnSwitchChange) {
            if (mSwitchBar != null) {
                mSwitchBar.removeOnSwitchChangeListener(this);
            }
            if (mSwitch != null) {
                mSwitch.setOnCheckedChangeListener(null);
            }
            mListeningToOnSwitchChange = false;
        }
    }

    public void resume(Context context) {
        mContext = context;

        if (!mListeningToOnSwitchChange) {
            if (mSwitchBar != null) {
                mSwitchBar.addOnSwitchChangeListener(this);
                mListeningToOnSwitchChange = true;
            }
            if (mSwitch != null) {
                mSwitch.setOnCheckedChangeListener(this);
                mListeningToOnSwitchChange = true;
            }
        }
    }

    public void teardownSwitchBar() {
        if (mSwitchBar == null) {
            return;
        }
        if (mListeningToOnSwitchChange) {
            mSwitchBar.removeOnSwitchChangeListener(this);
            mListeningToOnSwitchChange = false;
        }
        mSwitchBar.hide();
    }

    protected void setChecked(boolean checked) {
        mStateMachineEvent = true;
        if (mSwitchBar != null) {
            mSwitchBar.setChecked(checked);
        }
        if (mSwitch != null) {
            mSwitch.setChecked(checked);
        }
        mStateMachineEvent = false;
    }

    protected void setEnabled(boolean enabled) {
        if (mSwitchBar != null) {
            mSwitchBar.setEnabled(enabled);
        }
        if (mSwitch != null) {
            mSwitch.setEnabled(enabled);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        onSwitchChanged(mSwitch, isChecked);
    }

    public abstract void onSwitchChanged(Switch switchView, boolean isChecked);
}