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

import cgeo.geocaching.R;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.connector.oc.OCDEAuthorizationActivity;
import cgeo.geocaching.connector.oc.OCPLAuthorizationActivity;

import android.content.Context;
import android.content.Intent;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class AuthorizeOcPreference extends Preference {

    private static final int NO_KEY = -1;

    private enum OCAuthorizations {
        NONE(NO_KEY, null),
        OCDE(R.string.pref_fakekey_ocde_authorization, OCDEAuthorizationActivity.class),
        OCPL(R.string.pref_fakekey_ocpl_authorization, OCPLAuthorizationActivity.class);

        public int prefKeyId;
        public Class<?> authActivity;

        OCAuthorizations(int prefKeyId, Class<?> clazz) {
            this.prefKeyId = prefKeyId;
            this.authActivity = clazz;
        }
    }

    private final OCAuthorizations ocAuth;

    private OCAuthorizations getAuthorization() {
        final String prefKey = getKey();
        for (OCAuthorizations auth : OCAuthorizations.values()) {
            if (auth.prefKeyId != NO_KEY && prefKey.equals(cgeoapplication.getInstance().getString(auth.prefKeyId))) {
                return auth;
            }
        }
        return OCAuthorizations.NONE;
    }

    public AuthorizeOcPreference(Context context) {
        super(context);
        this.ocAuth = getAuthorization();
    }

    public AuthorizeOcPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.ocAuth = getAuthorization();
    }

    public AuthorizeOcPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.ocAuth = getAuthorization();
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        final SettingsActivity activity = (SettingsActivity) getContext();

        setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                if (ocAuth.authActivity != null) {
                Intent authIntent = new Intent(preference.getContext(),
                            ocAuth.authActivity);
                activity.startActivityForResult(authIntent,
                            ocAuth.prefKeyId);
                }
                return false; // no shared preference has to be changed
            }
        });

        activity.setOcAuthTitle(ocAuth.prefKeyId);
        return super.onCreateView(parent);
    }
}