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

import cgeo.geocaching.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.net.Uri;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class InfoPreference extends Preference {

    // strings for the popup dialog
    private String text;
    private String url;
    private String urlButton;

    public InfoPreference(Context context) {
        super(context);
        init(context, null, 0);
    }

    public InfoPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public InfoPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        setPersistent(false);

        if (attrs == null) {
            return; // coward's retreat
        }

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

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

        types.recycle();
    }

    @Override
    protected View onCreateView(ViewGroup parent) {

        // show popup when clicked
        setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(final Preference preference) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        preference.getContext());
                builder.setMessage(text)
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setTitle(preference.getTitle())
                        .setPositiveButton(R.string.err_none, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        })
                        .setNegativeButton(urlButton, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                Intent i = new Intent(Intent.ACTION_VIEW);
                                i.setData(Uri.parse(url));
                                preference.getContext().startActivity(i);
                            }
                        });
                builder.create().show();
                return false;
            }
        });

        return super.onCreateView(parent);
    }

}