android: Add field for MTU to data model
[strongswan.git] / src / frontends / android / src / org / strongswan / android / data / VpnProfileDataSource.java
1 /*
2 * Copyright (C) 2012-2015 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * 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 import java.util.ArrayList;
21 import java.util.List;
22
23 import android.content.ContentValues;
24 import android.content.Context;
25 import android.database.Cursor;
26 import android.database.SQLException;
27 import android.database.sqlite.SQLiteDatabase;
28 import android.database.sqlite.SQLiteOpenHelper;
29 import android.database.sqlite.SQLiteQueryBuilder;
30 import android.util.Log;
31
32 public class VpnProfileDataSource
33 {
34 private static final String TAG = VpnProfileDataSource.class.getSimpleName();
35 public static final String KEY_ID = "_id";
36 public static final String KEY_NAME = "name";
37 public static final String KEY_GATEWAY = "gateway";
38 public static final String KEY_VPN_TYPE = "vpn_type";
39 public static final String KEY_USERNAME = "username";
40 public static final String KEY_PASSWORD = "password";
41 public static final String KEY_CERTIFICATE = "certificate";
42 public static final String KEY_USER_CERTIFICATE = "user_certificate";
43 public static final String KEY_MTU = "mtu";
44
45 private DatabaseHelper mDbHelper;
46 private SQLiteDatabase mDatabase;
47 private final Context mContext;
48
49 private static final String DATABASE_NAME = "strongswan.db";
50 private static final String TABLE_VPNPROFILE = "vpnprofile";
51
52 private static final int DATABASE_VERSION = 5;
53
54 public static final String DATABASE_CREATE =
55 "CREATE TABLE " + TABLE_VPNPROFILE + " (" +
56 KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
57 KEY_NAME + " TEXT NOT NULL," +
58 KEY_GATEWAY + " TEXT NOT NULL," +
59 KEY_VPN_TYPE + " TEXT NOT NULL," +
60 KEY_USERNAME + " TEXT," +
61 KEY_PASSWORD + " TEXT," +
62 KEY_CERTIFICATE + " TEXT," +
63 KEY_USER_CERTIFICATE + " TEXT," +
64 KEY_MTU + " INTEGER" +
65 ");";
66 private static final String[] ALL_COLUMNS = new String[] {
67 KEY_ID,
68 KEY_NAME,
69 KEY_GATEWAY,
70 KEY_VPN_TYPE,
71 KEY_USERNAME,
72 KEY_PASSWORD,
73 KEY_CERTIFICATE,
74 KEY_USER_CERTIFICATE,
75 KEY_MTU,
76 };
77
78 private static class DatabaseHelper extends SQLiteOpenHelper
79 {
80 public DatabaseHelper(Context context)
81 {
82 super(context, DATABASE_NAME, null, DATABASE_VERSION);
83 }
84
85 @Override
86 public void onCreate(SQLiteDatabase database)
87 {
88 database.execSQL(DATABASE_CREATE);
89 }
90
91 @Override
92 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
93 {
94 Log.w(TAG, "Upgrading database from version " + oldVersion +
95 " to " + newVersion);
96 if (oldVersion < 2)
97 {
98 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_USER_CERTIFICATE +
99 " TEXT;");
100 }
101 if (oldVersion < 3)
102 {
103 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_VPN_TYPE +
104 " TEXT DEFAULT '';");
105 }
106 if (oldVersion < 4)
107 { /* remove NOT NULL constraint from username column */
108 updateColumns(db);
109 }
110 if (oldVersion < 5)
111 {
112 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_MTU +
113 " INTEGER;");
114 }
115 }
116
117 private void updateColumns(SQLiteDatabase db)
118 {
119 db.beginTransaction();
120 try
121 {
122 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " RENAME TO tmp_" + TABLE_VPNPROFILE + ";");
123 db.execSQL(DATABASE_CREATE);
124 StringBuilder insert = new StringBuilder("INSERT INTO " + TABLE_VPNPROFILE + " SELECT ");
125 SQLiteQueryBuilder.appendColumns(insert, ALL_COLUMNS);
126 db.execSQL(insert.append(" FROM tmp_" + TABLE_VPNPROFILE + ";").toString());
127 db.execSQL("DROP TABLE tmp_" + TABLE_VPNPROFILE + ";");
128 db.setTransactionSuccessful();
129 }
130 finally
131 {
132 db.endTransaction();
133 }
134 }
135 }
136
137 /**
138 * Construct a new VPN profile data source. The context is used to
139 * open/create the database.
140 * @param context context used to access the database
141 */
142 public VpnProfileDataSource(Context context)
143 {
144 this.mContext = context;
145 }
146
147 /**
148 * Open the VPN profile data source. The database is automatically created
149 * if it does not yet exist. If that fails an exception is thrown.
150 * @return itself (allows to chain initialization calls)
151 * @throws SQLException if the database could not be opened or created
152 */
153 public VpnProfileDataSource open() throws SQLException
154 {
155 if (mDbHelper == null)
156 {
157 mDbHelper = new DatabaseHelper(mContext);
158 mDatabase = mDbHelper.getWritableDatabase();
159 }
160 return this;
161 }
162
163 /**
164 * Close the data source.
165 */
166 public void close()
167 {
168 if (mDbHelper != null)
169 {
170 mDbHelper.close();
171 mDbHelper = null;
172 }
173 }
174
175 /**
176 * Insert the given VPN profile into the database. On success the Id of
177 * the object is updated and the object returned.
178 *
179 * @param profile the profile to add
180 * @return the added VPN profile or null, if failed
181 */
182 public VpnProfile insertProfile(VpnProfile profile)
183 {
184 ContentValues values = ContentValuesFromVpnProfile(profile);
185 long insertId = mDatabase.insert(TABLE_VPNPROFILE, null, values);
186 if (insertId == -1)
187 {
188 return null;
189 }
190 profile.setId(insertId);
191 return profile;
192 }
193
194 /**
195 * Updates the given VPN profile in the database.
196 * @param profile the profile to update
197 * @return true if update succeeded, false otherwise
198 */
199 public boolean updateVpnProfile(VpnProfile profile)
200 {
201 long id = profile.getId();
202 ContentValues values = ContentValuesFromVpnProfile(profile);
203 return mDatabase.update(TABLE_VPNPROFILE, values, KEY_ID + " = " + id, null) > 0;
204 }
205
206 /**
207 * Delete the given VPN profile from the database.
208 * @param profile the profile to delete
209 * @return true if deleted, false otherwise
210 */
211 public boolean deleteVpnProfile(VpnProfile profile)
212 {
213 long id = profile.getId();
214 return mDatabase.delete(TABLE_VPNPROFILE, KEY_ID + " = " + id, null) > 0;
215 }
216
217 /**
218 * Get a single VPN profile from the database.
219 * @param id the ID of the VPN profile
220 * @return the profile or null, if not found
221 */
222 public VpnProfile getVpnProfile(long id)
223 {
224 VpnProfile profile = null;
225 Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS,
226 KEY_ID + "=" + id, null, null, null, null);
227 if (cursor.moveToFirst())
228 {
229 profile = VpnProfileFromCursor(cursor);
230 }
231 cursor.close();
232 return profile;
233 }
234
235 /**
236 * Get a list of all VPN profiles stored in the database.
237 * @return list of VPN profiles
238 */
239 public List<VpnProfile> getAllVpnProfiles()
240 {
241 List<VpnProfile> vpnProfiles = new ArrayList<VpnProfile>();
242
243 Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS, null, null, null, null, null);
244 cursor.moveToFirst();
245 while (!cursor.isAfterLast())
246 {
247 VpnProfile vpnProfile = VpnProfileFromCursor(cursor);
248 vpnProfiles.add(vpnProfile);
249 cursor.moveToNext();
250 }
251 cursor.close();
252 return vpnProfiles;
253 }
254
255 private VpnProfile VpnProfileFromCursor(Cursor cursor)
256 {
257 VpnProfile profile = new VpnProfile();
258 profile.setId(cursor.getLong(cursor.getColumnIndex(KEY_ID)));
259 profile.setName(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
260 profile.setGateway(cursor.getString(cursor.getColumnIndex(KEY_GATEWAY)));
261 profile.setVpnType(VpnType.fromIdentifier(cursor.getString(cursor.getColumnIndex(KEY_VPN_TYPE))));
262 profile.setUsername(cursor.getString(cursor.getColumnIndex(KEY_USERNAME)));
263 profile.setPassword(cursor.getString(cursor.getColumnIndex(KEY_PASSWORD)));
264 profile.setCertificateAlias(cursor.getString(cursor.getColumnIndex(KEY_CERTIFICATE)));
265 profile.setUserCertificateAlias(cursor.getString(cursor.getColumnIndex(KEY_USER_CERTIFICATE)));
266 profile.setMTU(getInt(cursor, cursor.getColumnIndex(KEY_MTU)));
267 return profile;
268 }
269
270 private ContentValues ContentValuesFromVpnProfile(VpnProfile profile)
271 {
272 ContentValues values = new ContentValues();
273 values.put(KEY_NAME, profile.getName());
274 values.put(KEY_GATEWAY, profile.getGateway());
275 values.put(KEY_VPN_TYPE, profile.getVpnType().getIdentifier());
276 values.put(KEY_USERNAME, profile.getUsername());
277 values.put(KEY_PASSWORD, profile.getPassword());
278 values.put(KEY_CERTIFICATE, profile.getCertificateAlias());
279 values.put(KEY_USER_CERTIFICATE, profile.getUserCertificateAlias());
280 values.put(KEY_MTU, profile.getMTU());
281 return values;
282 }
283
284 private Integer getInt(Cursor cursor, int columnIndex)
285 {
286 return cursor.isNull(columnIndex) ? null : cursor.getInt(columnIndex);
287 }
288 }