summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn/VPNProfileList.java
blob: 59e231cc2189017787437774a7ca4c9e06850407 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package de.blinkt.openvpn;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Parcelable;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
import de.blinkt.openvpn.VPNConfigPreference.OnQuickSettingsClickListener;

public class VPNProfileList extends PreferenceFragment implements OnPreferenceClickListener, OnQuickSettingsClickListener {
	private static final String PREFS_NAME =  "VPNList";

	private static final int MENU_ADD_PROFILE = Menu.FIRST;


	private HashMap<String,VpnProfile> profiles;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.vpn_profile_list);
		setHasOptionsMenu(true);

	}

	@Override
	public void onResume() {
		super.onResume();
		refreshList();


	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		menu.add(0, MENU_ADD_PROFILE, 0, R.string.menu_add_profile)
				.setIcon(android.R.drawable.ic_menu_add)
				.setAlphabeticShortcut('a')
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
						| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		final int itemId = item.getItemId();
		if (itemId == MENU_ADD_PROFILE) {
			onAddProfileClicked();
			return true;
		} else {
			return super.onOptionsItemSelected(item);
		}
	}

	private void onAddProfileClicked() {
		Context context = getActivity();
		if (context != null) {
			final EditText entry = new EditText(context);
			entry.setSingleLine();

			AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			dialog.setTitle(R.string.menu_add_profile);
			dialog.setMessage(R.string.add_profile_name_prompt);
			dialog.setView(entry);


			dialog.setPositiveButton(android.R.string.ok,
					new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					String name = entry.getText().toString();
					if (getProfileByName(name)==null) {
						VpnProfile profile = new VpnProfile(name);
						addProfile(profile);
						refreshList();
					} else {
						Toast.makeText(getActivity(), R.string.duplicate_profile_name, Toast.LENGTH_LONG).show();
					}
				}


			});
			dialog.setNegativeButton(android.R.string.cancel,
					new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			dialog.create().show();
		}

	}

	public VpnProfile getProfileByName(String name) {
		for (VpnProfile vpnp : profiles.values()) {
			if(vpnp.getName().equals(name)) {
				return vpnp;
			}
		}
		return null;			
	}

	private void addProfile(VpnProfile profile) {
		profiles.put(profile.getUUID().toString(),profile);
		Editor editor = getActivity().getSharedPreferences(PREFS_NAME,Activity.MODE_PRIVATE).edit();
		editor.putStringSet("vpnlist", profiles.keySet());
		editor.commit();
		saveProfile(profile);

	}


	private void saveProfile(VpnProfile profile) {
		ObjectOutputStream vpnfile;
		try {
			vpnfile = new ObjectOutputStream(getActivity().openFileOutput((profile.getUUID().toString() + ".vp"),Activity.MODE_PRIVATE));

			vpnfile.writeObject(profile);
			vpnfile.flush();
			vpnfile.close();
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	private void loadVPNList() {
		profiles = new HashMap<String, VpnProfile>();
		SharedPreferences settings =getActivity().getSharedPreferences(PREFS_NAME,Activity.MODE_PRIVATE);
		Set<String> vlist = settings.getStringSet("vpnlist", null);
		if(vlist==null){
			vlist = new HashSet<String>();
		}

		for (String vpnentry : vlist) {
			try {
				ObjectInputStream vpnfile = new ObjectInputStream(getActivity().openFileInput(vpnentry + ".vp"));
				VpnProfile vp = ((VpnProfile) vpnfile.readObject());

				profiles.put(vp.getUUID().toString(), vp);

			} catch (StreamCorruptedException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) { 
				e.printStackTrace();
			}
		}
	}


	public void refreshList() {
		PreferenceScreen plist = getPreferenceScreen();
		if (plist != null) {
			plist.removeAll(); 
			loadVPNList();

			for (VpnProfile vpnprofile: profiles.values()) {
				Bundle args = new Bundle();
				//TODO

				String profileuuid = vpnprofile.getUUID().toString();


				args.putParcelable("Profile", vpnprofile);
				//args.putString("name", vpnentry);
				VPNConfigPreference vpref = new VPNConfigPreference(this, args);
				vpref.setKey(profileuuid);
				vpref.setTitle(vpnprofile.getName());
				vpref.setPersistent(false);
				//				vpref.setSelectable(true);
				vpref.setOnPreferenceClickListener(this);
				vpref.setOnQuickSettingsClickListener(this);
				plist.addPreference(vpref);
			}

		}
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		String key= preference.getKey();


		return true;

	}

	@Override
	public boolean onQuickSettingsClick(Preference preference) {
		String key = preference.getKey();

		VpnProfile vprofile = profiles.get(key);

		Intent vprefintent = new Intent(getActivity(),VPNPreferences.class)
		.putExtra("VpnProfile", (Parcelable)vprofile);

		startActivity(vprefintent);
		return true;
	}
}