summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/preferences/ListCheckboxPreference.java
blob: f56ea58b9de857a2b7c51f49f5509d5a840498bb (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
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.preferences;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;

import org.mozilla.gecko.R;

/**
  * This preference shows a checkbox on its left hand side, but will show a menu when clicked.
  * Its used for preferences like "Clear on Exit" that have a boolean on-off state, but that represent
  * multiple boolean options inside.
  **/
class ListCheckboxPreference extends MultiChoicePreference implements Checkable {
    private static final String LOGTAG = "GeckoListCheckboxPreference";
    private boolean checked;

    public ListCheckboxPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWidgetLayoutResource(R.layout.preference_checkbox);
    }

    @Override
    public boolean isChecked() {
        return checked;
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        View checkboxView = view.findViewById(R.id.checkbox);
        if (checkboxView instanceof Checkable) {
            ((Checkable) checkboxView).setChecked(checked);
        }
    }

    @Override
    public void setChecked(boolean checked) {
        boolean changed = checked != this.checked;
        this.checked = checked;
        if (changed) {
            notifyDependencyChange(shouldDisableDependents());
            notifyChanged();
        }
    }

    @Override
    public void toggle() {
        checked = !checked;
    }
}