android: Field added to store the type of a VPN profile
[strongswan.git] / src / frontends / android / src / org / strongswan / android / data / VpnProfileDataSource.java
1 /*
2 * Copyright (C) 2012 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.util.Log;
30
31 public class VpnProfileDataSource
32 {
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";
42
43 private DatabaseHelper mDbHelper;
44 private SQLiteDatabase mDatabase;
45 private final Context mContext;
46
47 private static final String DATABASE_NAME = "strongswan.db";
48 private static final String TABLE_VPNPROFILE = "vpnprofile";
49
50 private static final int DATABASE_VERSION = 3;
51
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" +
62 ");";
63 private final String[] ALL_COLUMNS = new String[] {
64 KEY_ID,
65 KEY_NAME,
66 KEY_GATEWAY,
67 KEY_VPN_TYPE,
68 KEY_USERNAME,
69 KEY_PASSWORD,
70 KEY_CERTIFICATE,
71 KEY_USER_CERTIFICATE,
72 };
73
74 private static class DatabaseHelper extends SQLiteOpenHelper
75 {
76 public DatabaseHelper(Context context)
77 {
78 super(context, DATABASE_NAME, null, DATABASE_VERSION);
79 }
80
81 @Override
82 public void onCreate(SQLiteDatabase database)
83 {
84 database.execSQL(DATABASE_CREATE);
85 }
86
87 @Override
88 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
89 {
90 Log.w(TAG, "Upgrading database from version " + oldVersion +
91 " to " + newVersion);
92 if (oldVersion < 2)
93 {
94 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_USER_CERTIFICATE +
95 " TEXT;");
96 }
97 if (oldVersion < 3)
98 {
99 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_VPN_TYPE +
100 " TEXT DEFAULT '';");
101 }
102 }
103 }
104
105 /**
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
109 */
110 public VpnProfileDataSource(Context context)
111 {
112 this.mContext = context;
113 }
114
115 /**
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
120 */
121 public VpnProfileDataSource open() throws SQLException
122 {
123 if (mDbHelper == null)
124 {
125 mDbHelper = new DatabaseHelper(mContext);
126 mDatabase = mDbHelper.getWritableDatabase();
127 }
128 return this;
129 }
130
131 /**
132 * Close the data source.
133 */
134 public void close()
135 {
136 if (mDbHelper != null)
137 {
138 mDbHelper.close();
139 mDbHelper = null;
140 }
141 }
142
143 /**
144 * Insert the given VPN profile into the database. On success the Id of
145 * the object is updated and the object returned.
146 *
147 * @param profile the profile to add
148 * @return the added VPN profile or null, if failed
149 */
150 public VpnProfile insertProfile(VpnProfile profile)
151 {
152 ContentValues values = ContentValuesFromVpnProfile(profile);
153 long insertId = mDatabase.insert(TABLE_VPNPROFILE, null, values);
154 if (insertId == -1)
155 {
156 return null;
157 }
158 profile.setId(insertId);
159 return profile;
160 }
161
162 /**
163 * Updates the given VPN profile in the database.
164 * @param profile the profile to update
165 * @return true if update succeeded, false otherwise
166 */
167 public boolean updateVpnProfile(VpnProfile profile)
168 {
169 long id = profile.getId();
170 ContentValues values = ContentValuesFromVpnProfile(profile);
171 return mDatabase.update(TABLE_VPNPROFILE, values, KEY_ID + " = " + id, null) > 0;
172 }
173
174 /**
175 * Delete the given VPN profile from the database.
176 * @param profile the profile to delete
177 * @return true if deleted, false otherwise
178 */
179 public boolean deleteVpnProfile(VpnProfile profile)
180 {
181 long id = profile.getId();
182 return mDatabase.delete(TABLE_VPNPROFILE, KEY_ID + " = " + id, null) > 0;
183 }
184
185 /**
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
189 */
190 public VpnProfile getVpnProfile(long id)
191 {
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())
196 {
197 profile = VpnProfileFromCursor(cursor);
198 }
199 cursor.close();
200 return profile;
201 }
202
203 /**
204 * Get a list of all VPN profiles stored in the database.
205 * @return list of VPN profiles
206 */
207 public List<VpnProfile> getAllVpnProfiles()
208 {
209 List<VpnProfile> vpnProfiles = new ArrayList<VpnProfile>();
210
211 Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS, null, null, null, null, null);
212 cursor.moveToFirst();
213 while (!cursor.isAfterLast())
214 {
215 VpnProfile vpnProfile = VpnProfileFromCursor(cursor);
216 vpnProfiles.add(vpnProfile);
217 cursor.moveToNext();
218 }
219 cursor.close();
220 return vpnProfiles;
221 }
222
223 private VpnProfile VpnProfileFromCursor(Cursor cursor)
224 {
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)));
234 return profile;
235 }
236
237 private ContentValues ContentValuesFromVpnProfile(VpnProfile profile)
238 {
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());
247 return values;
248 }
249 }