summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn/ProfileManager.java
blob: 0164fe5378ea9ac711a10b9fe17ef5bb9e880b30 (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
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.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class ProfileManager {
	private static final String PREFS_NAME =  "VPNList";



	private static ProfileManager instance;
	private HashMap<String,VpnProfile> profiles=new HashMap<String, VpnProfile>();


	public static VpnProfile get(String key) {
		if(instance==null)
			return null;
		return instance.profiles.get(key);
		
	}


	
	private ProfileManager() { }
	
	private static void checkInstance(Context context) {
		if(instance == null) {
			instance = new ProfileManager();
			instance.loadVPNList(context);
		}
	}

	public static ProfileManager getInstance(Context context) {
		checkInstance(context);
		return instance;
	}
	
	
	
	public Collection<VpnProfile> getProfiles() {
		return profiles.values();
	}
	
	public VpnProfile getProfileByName(String name) {
		for (VpnProfile vpnp : profiles.values()) {
			if(vpnp.getName().equals(name)) {
				return vpnp;
			}
		}
		return null;			
	}

	public void saveProfileList(Context context) {
		SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME,Activity.MODE_PRIVATE);
		Editor editor = sharedprefs.edit();
		editor.putStringSet("vpnlist", profiles.keySet());
		
		// For reasing I do not understand at all 
		// Android saves my prefs file only one time 
		// if I remove the debug code below :(
		int counter = sharedprefs.getInt("counter", 0);
		editor.putInt("counter", counter+1);
		editor.apply();

	}

	public void addProfile(VpnProfile profile) {
		profiles.put(profile.getUUID().toString(),profile);
		
	}
	
	
	public void saveProfile(Context context,VpnProfile profile) {
		// First let basic settings save its state
		
		ObjectOutputStream vpnfile;
		try {
			vpnfile = new ObjectOutputStream(context.openFileOutput((profile.getUUID().toString() + ".vp"),Activity.MODE_PRIVATE));

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

			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (IOException e) {

			e.printStackTrace();
			throw new RuntimeException(e);

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

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

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

			} catch (StreamCorruptedException e) {
				exp=e;
			} catch (FileNotFoundException e) {
				exp=e;
			} catch (IOException e) {
				exp=e;
			} catch (ClassNotFoundException e) { 
				exp=e;
			}
			if(exp!=null) {
				exp.printStackTrace();
			}
		}
	}

	public int getNumberOfProfiles() {
		return profiles.size();
	}



	public void removeProfile(Context context,VpnProfile profile) {
		String vpnentry = profile.getUUID().toString();
		profiles.remove(vpnentry);
		saveProfileList(context);
		context.deleteFile(vpnentry + ".vp");
		
	}

}