summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn/Settings_IP.java
blob: de625d22edb7fa4e784e96f22c279827af94064e (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package de.blinkt.openvpn;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;

public class Settings_IP extends PreferenceFragment implements OnPreferenceChangeListener {
		private EditTextPreference mIPv4;
		private EditTextPreference mIPv6;
		private SwitchPreference mUsePull;
		private CheckBoxPreference mOverrideDNS;
		private EditTextPreference mSearchdomain;
		private EditTextPreference mDNS1;
		private EditTextPreference mDNS2;
		private EditTextPreference mCustomRoutes;
		private CheckBoxPreference mUseDefaultRoute;
		private VpnProfile mProfile;
		private CheckBoxPreference mRouteNoPull;

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			
			String profileUUID = getArguments().getString(getActivity().getPackageName() + ".profileUUID");
			mProfile = ProfileManager.get(profileUUID);

			
			// Make sure default values are applied.  In a real app, you would
			// want this in a shared function that is used to retrieve the
			// SharedPreferences wherever they are needed.
			PreferenceManager.setDefaultValues(getActivity(),
					R.xml.vpn_ipsettings, false);
			
			// Load the preferences from an XML resource
			addPreferencesFromResource(R.xml.vpn_ipsettings);
			mIPv4 = (EditTextPreference) findPreference("ipv4_address");
			mIPv6 = (EditTextPreference) findPreference("ipv6_address");
			mUsePull = (SwitchPreference) findPreference("usePull");
			mOverrideDNS = (CheckBoxPreference) findPreference("overrideDNS");
			mSearchdomain =(EditTextPreference) findPreference("searchdomain");
			mDNS1 = (EditTextPreference) findPreference("dns1");
			mDNS2 = (EditTextPreference) findPreference("dns2");
			mCustomRoutes = (EditTextPreference) findPreference("customRoutes");
			mUseDefaultRoute = (CheckBoxPreference) findPreference("useDefaultRoute");
			mRouteNoPull = (CheckBoxPreference) findPreference("routenopull");

			mIPv4.setOnPreferenceChangeListener(this);
			mIPv6.setOnPreferenceChangeListener(this);
			mDNS1.setOnPreferenceChangeListener(this);
			mDNS2.setOnPreferenceChangeListener(this);
			mUsePull.setOnPreferenceChangeListener(this);
			mOverrideDNS.setOnPreferenceChangeListener(this);
			mSearchdomain.setOnPreferenceChangeListener(this);
			mCustomRoutes.setOnPreferenceChangeListener(this);
			
			

			
			loadSettings();
		}

		private void loadSettings() {
		
			mUsePull.setChecked(mProfile.mUsePull);
			mIPv4.setText(mProfile.mIPv4Address);
			mIPv6.setText(mProfile.mIPv6Address);
			mDNS1.setText(mProfile.mDNS1);
			mDNS2.setText(mProfile.mDNS2);
			mOverrideDNS.setChecked(mProfile.mOverrideDNS);
			mSearchdomain.setText(mProfile.mSearchDomain);
			mUseDefaultRoute.setChecked(mProfile.mUseDefaultRoute);
			mCustomRoutes.setText(mProfile.mCustomRoutes);
			mRouteNoPull.setChecked(mProfile.mRoutenopull);
			
			// Sets Summary
			onPreferenceChange(mIPv4, mIPv4.getText());
			onPreferenceChange(mIPv6, mIPv6.getText());
			onPreferenceChange(mDNS1, mDNS1.getText());
			onPreferenceChange(mDNS2, mDNS2.getText());
			onPreferenceChange(mSearchdomain, mSearchdomain.getText());
			onPreferenceChange(mCustomRoutes, mCustomRoutes.getText());
						
			setDNSState();
		}
		
		public void onSaveInstanceState (Bundle outState) {
			saveSettings();
		}
		
		@Override
		public void onStop() {
			saveSettings();
			super.onStop();
			
		}
		
		private void saveSettings() {
			mProfile.mUsePull = mUsePull.isChecked();
			mProfile.mIPv4Address = mIPv4.getText();
			mProfile.mIPv6Address = mIPv6.getText();
			mProfile.mDNS1 = mDNS1.getText();
			mProfile.mDNS2 = mDNS2.getText();
			mProfile.mOverrideDNS = mOverrideDNS.isChecked();
			mProfile.mSearchDomain = mSearchdomain.getText();
			mProfile.mUseDefaultRoute = mUseDefaultRoute.isChecked();
			mProfile.mCustomRoutes = mCustomRoutes.getText();
			mProfile.mRoutenopull = mRouteNoPull.isChecked();
			
		}
		
		@Override
		public boolean onPreferenceChange(Preference preference,
				Object newValue) {
			if(preference==mIPv4 || preference == mIPv6 
					 || preference==mDNS1 || preference == mDNS2
					 || preference == mSearchdomain || preference == mCustomRoutes
					)
			
				preference.setSummary((String)newValue);

			if(preference== mUsePull || preference == mOverrideDNS)
				if(preference==mOverrideDNS) { 
					// Set so the function gets the right value
					mOverrideDNS.setChecked((Boolean) newValue);
				}
				setDNSState();
			
			saveSettings();
			return true;
		}

		private void setDNSState() {
			boolean enabled;
			mOverrideDNS.setEnabled(mUsePull.isChecked());
			mRouteNoPull.setEnabled(mUsePull.isChecked());
			if(!mUsePull.isChecked())
				enabled =true;
			else if (mOverrideDNS.isChecked())
				enabled = true;
			else
				enabled = false;

			mDNS1.setEnabled(enabled);
			mDNS2.setEnabled(enabled);
			mSearchdomain.setEnabled(enabled);
			

		}


	}