2
* Copyright (C) 2012 Tobias Brunner
3
* Copyright (C) 2012 Giuliano Grassi
4
* Copyright (C) 2012 Ralf Sager
5
* Hochschule fuer Technik Rapperswil
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>.
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
18
package org.strongswan.android.data;
20
import java.util.ArrayList;
21
import java.util.List;
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;
32
public class VpnProfileDataSource
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";
44
private DatabaseHelper mDbHelper;
45
private SQLiteDatabase mDatabase;
46
private final Context mContext;
48
private static final String DATABASE_NAME = "strongswan.db";
49
private static final String TABLE_VPNPROFILE = "vpnprofile";
51
private static final int DATABASE_VERSION = 4;
53
public static final String DATABASE_CREATE =
54
"CREATE TABLE " + TABLE_VPNPROFILE + " (" +
55
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
56
KEY_NAME + " TEXT NOT NULL," +
57
KEY_GATEWAY + " TEXT NOT NULL," +
58
KEY_VPN_TYPE + " TEXT NOT NULL," +
59
KEY_USERNAME + " TEXT," +
60
KEY_PASSWORD + " TEXT," +
61
KEY_CERTIFICATE + " TEXT," +
62
KEY_USER_CERTIFICATE + " TEXT" +
64
private static final String[] ALL_COLUMNS = new String[] {
75
private static class DatabaseHelper extends SQLiteOpenHelper
77
public DatabaseHelper(Context context)
79
super(context, DATABASE_NAME, null, DATABASE_VERSION);
83
public void onCreate(SQLiteDatabase database)
85
database.execSQL(DATABASE_CREATE);
89
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
91
Log.w(TAG, "Upgrading database from version " + oldVersion +
95
db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_USER_CERTIFICATE +
100
db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_VPN_TYPE +
101
" TEXT DEFAULT '';");
104
{ /* remove NOT NULL constraint from username column */
109
private void updateColumns(SQLiteDatabase db)
111
db.beginTransaction();
114
db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " RENAME TO tmp_" + TABLE_VPNPROFILE + ";");
115
db.execSQL(DATABASE_CREATE);
116
StringBuilder insert = new StringBuilder("INSERT INTO " + TABLE_VPNPROFILE + " SELECT ");
117
SQLiteQueryBuilder.appendColumns(insert, ALL_COLUMNS);
118
db.execSQL(insert.append(" FROM tmp_" + TABLE_VPNPROFILE + ";").toString());
119
db.execSQL("DROP TABLE tmp_" + TABLE_VPNPROFILE + ";");
120
db.setTransactionSuccessful();
130
* Construct a new VPN profile data source. The context is used to
131
* open/create the database.
132
* @param context context used to access the database
134
public VpnProfileDataSource(Context context)
136
this.mContext = context;
140
* Open the VPN profile data source. The database is automatically created
141
* if it does not yet exist. If that fails an exception is thrown.
142
* @return itself (allows to chain initialization calls)
143
* @throws SQLException if the database could not be opened or created
145
public VpnProfileDataSource open() throws SQLException
147
if (mDbHelper == null)
149
mDbHelper = new DatabaseHelper(mContext);
150
mDatabase = mDbHelper.getWritableDatabase();
156
* Close the data source.
160
if (mDbHelper != null)
168
* Insert the given VPN profile into the database. On success the Id of
169
* the object is updated and the object returned.
171
* @param profile the profile to add
172
* @return the added VPN profile or null, if failed
174
public VpnProfile insertProfile(VpnProfile profile)
176
ContentValues values = ContentValuesFromVpnProfile(profile);
177
long insertId = mDatabase.insert(TABLE_VPNPROFILE, null, values);
182
profile.setId(insertId);
187
* Updates the given VPN profile in the database.
188
* @param profile the profile to update
189
* @return true if update succeeded, false otherwise
191
public boolean updateVpnProfile(VpnProfile profile)
193
long id = profile.getId();
194
ContentValues values = ContentValuesFromVpnProfile(profile);
195
return mDatabase.update(TABLE_VPNPROFILE, values, KEY_ID + " = " + id, null) > 0;
199
* Delete the given VPN profile from the database.
200
* @param profile the profile to delete
201
* @return true if deleted, false otherwise
203
public boolean deleteVpnProfile(VpnProfile profile)
205
long id = profile.getId();
206
return mDatabase.delete(TABLE_VPNPROFILE, KEY_ID + " = " + id, null) > 0;
210
* Get a single VPN profile from the database.
211
* @param id the ID of the VPN profile
212
* @return the profile or null, if not found
214
public VpnProfile getVpnProfile(long id)
216
VpnProfile profile = null;
217
Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS,
218
KEY_ID + "=" + id, null, null, null, null);
219
if (cursor.moveToFirst())
221
profile = VpnProfileFromCursor(cursor);
228
* Get a list of all VPN profiles stored in the database.
229
* @return list of VPN profiles
231
public List<VpnProfile> getAllVpnProfiles()
233
List<VpnProfile> vpnProfiles = new ArrayList<VpnProfile>();
235
Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS, null, null, null, null, null);
236
cursor.moveToFirst();
237
while (!cursor.isAfterLast())
239
VpnProfile vpnProfile = VpnProfileFromCursor(cursor);
240
vpnProfiles.add(vpnProfile);
247
private VpnProfile VpnProfileFromCursor(Cursor cursor)
249
VpnProfile profile = new VpnProfile();
250
profile.setId(cursor.getLong(cursor.getColumnIndex(KEY_ID)));
251
profile.setName(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
252
profile.setGateway(cursor.getString(cursor.getColumnIndex(KEY_GATEWAY)));
253
profile.setVpnType(VpnType.fromIdentifier(cursor.getString(cursor.getColumnIndex(KEY_VPN_TYPE))));
254
profile.setUsername(cursor.getString(cursor.getColumnIndex(KEY_USERNAME)));
255
profile.setPassword(cursor.getString(cursor.getColumnIndex(KEY_PASSWORD)));
256
profile.setCertificateAlias(cursor.getString(cursor.getColumnIndex(KEY_CERTIFICATE)));
257
profile.setUserCertificateAlias(cursor.getString(cursor.getColumnIndex(KEY_USER_CERTIFICATE)));
261
private ContentValues ContentValuesFromVpnProfile(VpnProfile profile)
263
ContentValues values = new ContentValues();
264
values.put(KEY_NAME, profile.getName());
265
values.put(KEY_GATEWAY, profile.getGateway());
266
values.put(KEY_VPN_TYPE, profile.getVpnType().getIdentifier());
267
values.put(KEY_USERNAME, profile.getUsername());
268
values.put(KEY_PASSWORD, profile.getPassword());
269
values.put(KEY_CERTIFICATE, profile.getCertificateAlias());
270
values.put(KEY_USER_CERTIFICATE, profile.getUserCertificateAlias());