aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/settings/CheckBoxWithPopupPreference.java
blob: 35df7878426aa2bf4bf25161a0c514c156097a1b (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
package cgeo.geocaching.settings;

import cgeo.geocaching.R;
import cgeo.geocaching.ui.UrlPopup;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class CheckBoxWithPopupPreference extends CheckBoxPreference {

    // strings for the popup dialog
    private String title;
    private String text;
    private String url;
    private String urlButton;
    private OnPreferenceChangeListener baseOnPrefChangeListener = null;

    public CheckBoxWithPopupPreference(Context context) {
        super(context);
    }

    public CheckBoxWithPopupPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        processAttributes(context, attrs, 0);
    }

    public CheckBoxWithPopupPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        processAttributes(context, attrs, defStyle);
    }

    private void processAttributes(Context context, AttributeSet attrs, int defStyle) {
        if (attrs == null) {
            return; // coward's retreat
        }

        TypedArray types = context.obtainStyledAttributes(attrs, new int[] {
                R.attr.title, R.attr.text, R.attr.url, R.attr.urlButton },
                defStyle, 0);

        title = types.getString(0);
        text = types.getString(1);
        url = types.getString(2);
        urlButton = types.getString(3);

        types.recycle();
    }

    @Override
    protected View onCreateView(ViewGroup parent) {

        if (baseOnPrefChangeListener == null) {
            baseOnPrefChangeListener = getOnPreferenceChangeListener();
        }

        // show dialog when checkbox enabled
        setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(final Preference preference, Object newValue) {
                if (baseOnPrefChangeListener != null) {
                    baseOnPrefChangeListener.onPreferenceChange(preference, newValue);
                }
                if (!(Boolean) newValue) {
                    return true;
                }
                new UrlPopup(preference.getContext()).show(title, text, url, urlButton);
                return true;
            }
        });

        return super.onCreateView(parent);
    }

}