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
.util
.Log
;
31 public class VpnProfileDataSource
33 private static final String TAG
= VpnProfileDataSource
.class.getSimpleName();
34 public static final String KEY_ID
= "_id";
35 public static final String KEY_NAME
= "name";
36 public static final String KEY_GATEWAY
= "gateway";
37 public static final String KEY_VPN_TYPE
= "vpn_type";
38 public static final String KEY_USERNAME
= "username";
39 public static final String KEY_PASSWORD
= "password";
40 public static final String KEY_CERTIFICATE
= "certificate";
41 public static final String KEY_USER_CERTIFICATE
= "user_certificate";
43 private DatabaseHelper mDbHelper
;
44 private SQLiteDatabase mDatabase
;
45 private final Context mContext
;
47 private static final String DATABASE_NAME
= "strongswan.db";
48 private static final String TABLE_VPNPROFILE
= "vpnprofile";
50 private static final int DATABASE_VERSION
= 3;
52 public static final String DATABASE_CREATE
=
53 "CREATE TABLE " + TABLE_VPNPROFILE
+ " (" +
54 KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," +
55 KEY_NAME
+ " TEXT NOT NULL," +
56 KEY_GATEWAY
+ " TEXT NOT NULL," +
57 KEY_VPN_TYPE
+ " TEXT NOT NULL," +
58 KEY_USERNAME
+ " TEXT NOT NULL," +
59 KEY_PASSWORD
+ " TEXT," +
60 KEY_CERTIFICATE
+ " TEXT," +
61 KEY_USER_CERTIFICATE
+ " TEXT" +
63 private final String
[] ALL_COLUMNS
= new String
[] {
74 private static class DatabaseHelper
extends SQLiteOpenHelper
76 public DatabaseHelper(Context context
)
78 super(context
, DATABASE_NAME
, null
, DATABASE_VERSION
);
82 public void onCreate(SQLiteDatabase database
)
84 database
.execSQL(DATABASE_CREATE
);
88 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
)
90 Log
.w(TAG
, "Upgrading database from version " + oldVersion
+
94 db
.execSQL("ALTER TABLE " + TABLE_VPNPROFILE
+ " ADD " + KEY_USER_CERTIFICATE
+
99 db
.execSQL("ALTER TABLE " + TABLE_VPNPROFILE
+ " ADD " + KEY_VPN_TYPE
+
100 " TEXT DEFAULT '';");
106 * Construct a new VPN profile data source. The context is used to
107 * open/create the database.
108 * @param context context used to access the database
110 public VpnProfileDataSource(Context context
)
112 this.mContext
= context
;
116 * Open the VPN profile data source. The database is automatically created
117 * if it does not yet exist. If that fails an exception is thrown.
118 * @return itself (allows to chain initialization calls)
119 * @throws SQLException if the database could not be opened or created
121 public VpnProfileDataSource
open() throws SQLException
123 if (mDbHelper
== null
)
125 mDbHelper
= new DatabaseHelper(mContext
);
126 mDatabase
= mDbHelper
.getWritableDatabase();
132 * Close the data source.
136 if (mDbHelper
!= null
)
144 * Insert the given VPN profile into the database. On success the Id of
145 * the object is updated and the object returned.
147 * @param profile the profile to add
148 * @return the added VPN profile or null, if failed
150 public VpnProfile
insertProfile(VpnProfile profile
)
152 ContentValues values
= ContentValuesFromVpnProfile(profile
);
153 long insertId
= mDatabase
.insert(TABLE_VPNPROFILE
, null
, values
);
158 profile
.setId(insertId
);
163 * Updates the given VPN profile in the database.
164 * @param profile the profile to update
165 * @return true if update succeeded, false otherwise
167 public boolean updateVpnProfile(VpnProfile profile
)
169 long id
= profile
.getId();
170 ContentValues values
= ContentValuesFromVpnProfile(profile
);
171 return mDatabase
.update(TABLE_VPNPROFILE
, values
, KEY_ID
+ " = " + id
, null
) > 0;
175 * Delete the given VPN profile from the database.
176 * @param profile the profile to delete
177 * @return true if deleted, false otherwise
179 public boolean deleteVpnProfile(VpnProfile profile
)
181 long id
= profile
.getId();
182 return mDatabase
.delete(TABLE_VPNPROFILE
, KEY_ID
+ " = " + id
, null
) > 0;
186 * Get a single VPN profile from the database.
187 * @param id the ID of the VPN profile
188 * @return the profile or null, if not found
190 public VpnProfile
getVpnProfile(long id
)
192 VpnProfile profile
= null
;
193 Cursor cursor
= mDatabase
.query(TABLE_VPNPROFILE
, ALL_COLUMNS
,
194 KEY_ID
+ "=" + id
, null
, null
, null
, null
);
195 if (cursor
.moveToFirst())
197 profile
= VpnProfileFromCursor(cursor
);
204 * Get a list of all VPN profiles stored in the database.
205 * @return list of VPN profiles
207 public List
<VpnProfile
> getAllVpnProfiles()
209 List
<VpnProfile
> vpnProfiles
= new ArrayList
<VpnProfile
>();
211 Cursor cursor
= mDatabase
.query(TABLE_VPNPROFILE
, ALL_COLUMNS
, null
, null
, null
, null
, null
);
212 cursor
.moveToFirst();
213 while (!cursor
.isAfterLast())
215 VpnProfile vpnProfile
= VpnProfileFromCursor(cursor
);
216 vpnProfiles
.add(vpnProfile
);
223 private VpnProfile
VpnProfileFromCursor(Cursor cursor
)
225 VpnProfile profile
= new VpnProfile();
226 profile
.setId(cursor
.getLong(cursor
.getColumnIndex(KEY_ID
)));
227 profile
.setName(cursor
.getString(cursor
.getColumnIndex(KEY_NAME
)));
228 profile
.setGateway(cursor
.getString(cursor
.getColumnIndex(KEY_GATEWAY
)));
229 profile
.setVpnType(VpnType
.fromIdentifier(cursor
.getString(cursor
.getColumnIndex(KEY_VPN_TYPE
))));
230 profile
.setUsername(cursor
.getString(cursor
.getColumnIndex(KEY_USERNAME
)));
231 profile
.setPassword(cursor
.getString(cursor
.getColumnIndex(KEY_PASSWORD
)));
232 profile
.setCertificateAlias(cursor
.getString(cursor
.getColumnIndex(KEY_CERTIFICATE
)));
233 profile
.setUserCertificateAlias(cursor
.getString(cursor
.getColumnIndex(KEY_USER_CERTIFICATE
)));
237 private ContentValues
ContentValuesFromVpnProfile(VpnProfile profile
)
239 ContentValues values
= new ContentValues();
240 values
.put(KEY_NAME
, profile
.getName());
241 values
.put(KEY_GATEWAY
, profile
.getGateway());
242 values
.put(KEY_VPN_TYPE
, profile
.getVpnType().getIdentifier());
243 values
.put(KEY_USERNAME
, profile
.getUsername());
244 values
.put(KEY_PASSWORD
, profile
.getPassword());
245 values
.put(KEY_CERTIFICATE
, profile
.getCertificateAlias());
246 values
.put(KEY_USER_CERTIFICATE
, profile
.getUserCertificateAlias());