summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/views/IconSwitchEntry.java
blob: b6d72ab61aecc66584acbea757a95e143bbda190 (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
135
136
137
138
139
140
141
142
143
144
145
/**
 * Copyright (c) 2021 LEAP Encryption Access Project and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package se.leap.bitmaskclient.base.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.SwitchCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import se.leap.bitmaskclient.R;

public class IconSwitchEntry extends LinearLayout {

    private TextView textView;
    private TextView subtitleView;
    private AppCompatImageView iconView;
    private SwitchCompat switchView;
    private CompoundButton.OnCheckedChangeListener checkedChangeListener;

    public IconSwitchEntry(Context context) {
        super(context);
        initLayout(context, null);
    }

    public IconSwitchEntry(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initLayout(context, attrs);
    }

    public IconSwitchEntry(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLayout(context, attrs);
    }

    @TargetApi(21)
    public IconSwitchEntry(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initLayout(context, attrs);
    }

    void initLayout(Context context, AttributeSet attrs) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rootview = inflater.inflate(R.layout.v_switch_list_item, this, true);
        textView = rootview.findViewById(R.id.title);
        subtitleView = rootview.findViewById(R.id.subtitle);
        iconView = rootview.findViewById(R.id.material_icon);
        switchView = rootview.findViewById(R.id.option_switch);

        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.IconSwitchEntry);

            String entryText = typedArray.getString(R.styleable.IconTextEntry_text);
            if (entryText != null) {
                textView.setText(entryText);
            }

            String subtitle = typedArray.getString(R.styleable.IconTextEntry_subtitle);
            if (subtitle != null) {
                subtitleView.setText(subtitle);
                subtitleView.setVisibility(VISIBLE);
            }

            Drawable drawable = typedArray.getDrawable(R.styleable.IconTextEntry_icon);
            if (drawable != null) {
                iconView.setImageDrawable(drawable);
            }

            typedArray.recycle();
        }
    }

    public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) {
        checkedChangeListener = listener;
        switchView.setOnCheckedChangeListener(checkedChangeListener);
    }

    public void setText(@StringRes int id) {
        textView.setText(id);
    }

    public void setSubtitle(CharSequence text) {
        subtitleView.setText(text);
    }

    public void setSingleLine(boolean singleLine) {
        textView.setSingleLine(singleLine);
        subtitleView.setSingleLine(singleLine);
    }

    public void showSubtitle(boolean show) {
        subtitleView.setVisibility(show ? VISIBLE : GONE);
    }

    public void setIcon(@DrawableRes int id) {
        iconView.setImageResource(id);
    }

    public void setChecked(boolean isChecked) {
        switchView.setChecked(isChecked);
    }

    public void setCheckedQuietly(boolean isChecked) {
        switchView.setOnCheckedChangeListener(null);
        switchView.setChecked(isChecked);
        switchView.setOnCheckedChangeListener(checkedChangeListener);
    }

    public boolean isChecked() {
        return switchView.isChecked();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        switchView.setVisibility(enabled ? VISIBLE : GONE);
        textView.setTextColor(getResources().getColor(enabled ? android.R.color.black : R.color.colorDisabled));
        iconView.setImageAlpha(enabled ? 255 : 128);
    }
}