android: Fix null pointer dereference with existing profiles
[strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / data / VpnProfile.java
1 /*
2 * Copyright (C) 2012-2017 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * HSR Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 package org.strongswan.android.data;
19
20
21 import android.text.TextUtils;
22
23 import java.util.Arrays;
24 import java.util.SortedSet;
25 import java.util.TreeSet;
26 import java.util.UUID;
27
28 public class VpnProfile implements Cloneable
29 {
30 /* While storing this as EnumSet would be nicer this simplifies storing it in a database */
31 public static final int SPLIT_TUNNELING_BLOCK_IPV4 = 1;
32 public static final int SPLIT_TUNNELING_BLOCK_IPV6 = 2;
33 public static final int FLAGS_SUPPRESS_CERT_REQS = 1;
34
35 private String mName, mGateway, mUsername, mPassword, mCertificate, mUserCertificate;
36 private String mRemoteId, mLocalId, mExcludedSubnets, mIncludedSubnets, mSelectedApps;
37 private Integer mMTU, mPort, mSplitTunneling, mNATKeepAlive, mFlags;
38 private SelectedAppsHandling mSelectedAppsHandling = SelectedAppsHandling.SELECTED_APPS_DISABLE;
39 private VpnType mVpnType;
40 private UUID mUUID;
41 private long mId = -1;
42
43 public enum SelectedAppsHandling
44 {
45 SELECTED_APPS_DISABLE(0),
46 SELECTED_APPS_EXCLUDE(1),
47 SELECTED_APPS_ONLY(2);
48
49 private Integer mValue;
50
51 SelectedAppsHandling(int value)
52 {
53 mValue = value;
54 }
55
56 public Integer getValue()
57 {
58 return mValue;
59 }
60 }
61
62 public VpnProfile()
63 {
64 this.mUUID = UUID.randomUUID();
65 }
66
67 public long getId()
68 {
69 return mId;
70 }
71
72 public void setId(long id)
73 {
74 this.mId = id;
75 }
76
77 public void setUUID(UUID uuid)
78 {
79 this.mUUID = uuid;
80 }
81
82 public UUID getUUID()
83 {
84 return mUUID;
85 }
86
87 public String getName()
88 {
89 return mName;
90 }
91
92 public void setName(String name)
93 {
94 this.mName = name;
95 }
96
97 public String getGateway()
98 {
99 return mGateway;
100 }
101
102 public void setGateway(String gateway)
103 {
104 this.mGateway = gateway;
105 }
106
107 public VpnType getVpnType()
108 {
109 return mVpnType;
110 }
111
112 public void setVpnType(VpnType type)
113 {
114 this.mVpnType = type;
115 }
116
117 public String getUsername()
118 {
119 return mUsername;
120 }
121
122 public void setUsername(String username)
123 {
124 this.mUsername = username;
125 }
126
127 public String getPassword()
128 {
129 return mPassword;
130 }
131
132 public void setPassword(String password)
133 {
134 this.mPassword = password;
135 }
136
137 public String getCertificateAlias()
138 {
139 return mCertificate;
140 }
141
142 public void setCertificateAlias(String alias)
143 {
144 this.mCertificate = alias;
145 }
146
147 public String getUserCertificateAlias()
148 {
149 return mUserCertificate;
150 }
151
152 public void setUserCertificateAlias(String alias)
153 {
154 this.mUserCertificate = alias;
155 }
156
157 public String getLocalId()
158 {
159 return mLocalId;
160 }
161
162 public void setLocalId(String localId)
163 {
164 this.mLocalId = localId;
165 }
166
167 public String getRemoteId()
168 {
169 return mRemoteId;
170 }
171
172 public void setRemoteId(String remoteId)
173 {
174 this.mRemoteId = remoteId;
175 }
176
177 public Integer getMTU()
178 {
179 return mMTU;
180 }
181
182 public void setMTU(Integer mtu)
183 {
184 this.mMTU = mtu;
185 }
186
187 public Integer getPort()
188 {
189 return mPort;
190 }
191
192 public void setPort(Integer port)
193 {
194 this.mPort = port;
195 }
196
197 public Integer getNATKeepAlive()
198 {
199 return mNATKeepAlive;
200 }
201
202 public void setNATKeepAlive(Integer keepalive)
203 {
204 this.mNATKeepAlive = keepalive;
205 }
206
207 public void setExcludedSubnets(String excludedSubnets)
208 {
209 this.mExcludedSubnets = excludedSubnets;
210 }
211
212 public String getExcludedSubnets()
213 {
214 return mExcludedSubnets;
215 }
216
217 public void setIncludedSubnets(String includedSubnets)
218 {
219 this.mIncludedSubnets = includedSubnets;
220 }
221
222 public String getIncludedSubnets()
223 {
224 return mIncludedSubnets;
225 }
226
227 public void setSelectedApps(String selectedApps)
228 {
229 this.mSelectedApps = selectedApps;
230 }
231
232 public void setSelectedApps(SortedSet<String> selectedApps)
233 {
234 this.mSelectedApps = selectedApps.size() > 0 ? TextUtils.join(" ", selectedApps) : null;
235 }
236
237 public String getSelectedApps()
238 {
239 return mSelectedApps;
240 }
241
242 public SortedSet<String> getSelectedAppsSet()
243 {
244 TreeSet<String> set = new TreeSet<>();
245 if (!TextUtils.isEmpty(mSelectedApps))
246 {
247 set.addAll(Arrays.asList(mSelectedApps.split("\\s+")));
248 }
249 return set;
250 }
251
252 public void setSelectedAppsHandling(SelectedAppsHandling selectedAppsHandling)
253 {
254 this.mSelectedAppsHandling = selectedAppsHandling;
255 }
256
257 public void setSelectedAppsHandling(Integer value)
258 {
259 mSelectedAppsHandling = SelectedAppsHandling.SELECTED_APPS_DISABLE;
260 for (SelectedAppsHandling handling : SelectedAppsHandling.values())
261 {
262 if (handling.mValue.equals(value))
263 {
264 mSelectedAppsHandling = handling;
265 break;
266 }
267 }
268 }
269
270 public SelectedAppsHandling getSelectedAppsHandling()
271 {
272 return mSelectedAppsHandling;
273 }
274
275 public Integer getSplitTunneling()
276 {
277 return mSplitTunneling;
278 }
279
280 public void setSplitTunneling(Integer splitTunneling)
281 {
282 this.mSplitTunneling = splitTunneling;
283 }
284
285 public Integer getFlags()
286 {
287 return mFlags == null ? 0 : mFlags;
288 }
289
290 public void setFlags(Integer flags)
291 {
292 this.mFlags = flags;
293 }
294
295 @Override
296 public String toString()
297 {
298 return mName;
299 }
300
301 @Override
302 public boolean equals(Object o)
303 {
304 if (o != null && o instanceof VpnProfile)
305 {
306 VpnProfile other = (VpnProfile)o;
307 if (this.mUUID != null && other.getUUID() != null)
308 {
309 return this.mUUID.equals(other.getUUID());
310 }
311 return this.mId == other.getId();
312 }
313 return false;
314 }
315
316 @Override
317 public VpnProfile clone()
318 {
319 try
320 {
321 return (VpnProfile)super.clone();
322 }
323 catch (CloneNotSupportedException e)
324 {
325 throw new AssertionError();
326 }
327 }
328 }