summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn/RemoteCNPreference.java
blob: 4daf54c28601dd33236fd32b2d407b560f98af6e (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
package de.blinkt.openvpn;

import android.content.Context;
import android.preference.DialogPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class RemoteCNPreference extends DialogPreference {


	private Spinner mSpinner;
	private EditText mEditText;
	private int mDNType;
	private ArrayAdapter<String> mAuthtypes;
	private String mDn;

	public RemoteCNPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		setDialogLayoutResource(R.layout.tlsremote);

	}

	@Override
	protected void onBindDialogView(View view) {

		super.onBindDialogView(view);

		mEditText = (EditText) view.findViewById(R.id.tlsremotecn);
		mSpinner = (Spinner) view.findViewById(R.id.x509verifytype);
		if(mDn!=null)
			mEditText.setText(mDn);

		populateSpinner();

	}

	private void populateSpinner() {
		mAuthtypes = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item);
		mAuthtypes.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		mAuthtypes.add(getContext().getString(R.string.complete_dn));
		mAuthtypes.add("RDN (common name)");
		mAuthtypes.add("RDN prefix");
		if (mDNType == VpnProfile.X509_VERIFY_TLSREMOTE || mDNType == VpnProfile.X509_VERIFY_TLSREMOTE_COMPAT_NOREMAPPING )
			mAuthtypes.add("tls-remote (DEPRECATED)");

		mSpinner.setAdapter(mAuthtypes);
	}

	public String getCNText() {
		return mDn;
	}
	
	public int getAuthtype() {
		return mDNType;
	}

	public void setDN(String dn) {
		mDn = dn;
		if(mEditText!=null)
			mEditText.setText(dn);
	}

	void setAuthType(int x509authtype) {
		mDNType = x509authtype;
		if (mSpinner!=null)
			populateSpinner();
	}

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		super.onDialogClosed(positiveResult);

		if (positiveResult) {
			String dn = mEditText.getText().toString();
			int authtype = getAuthTypeFromSpinner();
			if (callChangeListener(new Pair<Integer, String>(authtype, dn))) {
				mDn = dn;
				mDNType = authtype;
			}
		}
	}

	private int getAuthTypeFromSpinner() {
		int pos = mSpinner.getSelectedItemPosition();
		switch (pos) {
		case 0:
			return VpnProfile.X509_VERIFY_TLSREMOTE_DN;
		case 1:
			return VpnProfile.X509_VERIFY_TLSREMOTE_RDN;
		case 2:
			return VpnProfile.X509_VERIFY_TLSREMOTE_RDN_PREFIX;
		case 3:
			// This is the tls-remote entry, only visible if mDntype is a
			// tls-remote type
			return mDNType;
		default:
			return VpnProfile.X509_VERIFY_TLSREMOTE;
		}
	}

}