a6c6f5e3f66ec235c7d64e8d5367999b3977c436
[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_USERNAME = "username";
38 public static final String KEY_PASSWORD = "password";
39 public static final String KEY_CERTIFICATE = "certificate";
40 public static final String KEY_USER_CERTIFICATE = "user_certificate";
41
42 private DatabaseHelper mDbHelper;
43 private SQLiteDatabase mDatabase;
44 private final Context mContext;
45
46 private static final String DATABASE_NAME = "strongswan.db";
47 private static final String TABLE_VPNPROFILE = "vpnprofile";
48
49 private static final int DATABASE_VERSION = 2;
50
51 public static final String DATABASE_CREATE =
52 "CREATE TABLE " + TABLE_VPNPROFILE + " (" +
53 KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
54 KEY_NAME + " TEXT NOT NULL," +
55 KEY_GATEWAY + " TEXT NOT NULL," +
56 KEY_USERNAME + " TEXT NOT NULL," +
57 KEY_PASSWORD + " TEXT," +
58 KEY_CERTIFICATE + " TEXT," +
59 KEY_USER_CERTIFICATE + " TEXT" +
60 ");";
61 private final String[] ALL_COLUMNS = new String[] {
62 KEY_ID,
63 KEY_NAME,
64 KEY_GATEWAY,
65 KEY_USERNAME,
66 KEY_PASSWORD,
67 KEY_CERTIFICATE,
68 KEY_USER_CERTIFICATE,
69 };
70
71 private static class DatabaseHelper extends SQLiteOpenHelper
72 {
73 public DatabaseHelper(Context context)
74 {
75 super(context, DATABASE_NAME, null, DATABASE_VERSION);
76 }
77
78 @Override
79 public void onCreate(SQLiteDatabase database)
80 {
81 database.execSQL(DATABASE_CREATE);
82 }
83
84 @Override
85 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
86 {
87 Log.w(TAG, "Upgrading database from version " + oldVersion +
88 " to " + newVersion);
89 if (oldVersion < 2)
90 {
91 db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_USER_CERTIFICATE +
92 " TEXT;");
93 }
94 }
95 }
96
97 /**
98 * Construct a new VPN profile data source. The context is used to
99 * open/create the database.
100 * @param context context used to access the database
101 */
102 public VpnProfileDataSource(Context context)
103 {
104 this.mContext = context;
105 }
106
107 /**
108 * Open the VPN profile data source. The database is automatically created
109 * if it does not yet exist. If that fails an exception is thrown.
110 * @return itself (allows to chain initialization calls)
111 * @throws SQLException if the database could not be opened or created
112 */
113 public VpnProfileDataSource open() throws SQLException
114 {
115 if (mDbHelper == null)
116 {
117 mDbHelper = new DatabaseHelper(mContext);
118 mDatabase = mDbHelper.getWritableDatabase();
119 }
120 return this;
121 }
122
123 /**
124 * Close the data source.
125 */
126 public void close()
127 {
128 if (mDbHelper != null)
129 {
130 mDbHelper.close();
131 mDbHelper = null;
132 }
133 }
134
135 /**
136 * Insert the given VPN profile into the database. On success the Id of
137 * the object is updated and the object returned.
138 *
139 * @param profile the profile to add
140 * @return the added VPN profile or null, if failed
141 */
142 public VpnProfile insertProfile(VpnProfile profile)
143 {
144 ContentValues values = ContentValuesFromVpnProfile(profile);
145 long insertId = mDatabase.insert(TABLE_VPNPROFILE, null, values);
146 if (insertId == -1)
147 {
148 return null;
149 }
150 profile.setId(insertId);
151 return profile;
152 }
153
154 /**
155 * Updates the given VPN profile in the database.
156 * @param profile the profile to update
157 * @return true if update succeeded, false otherwise
158 */
159 public boolean updateVpnProfile(VpnProfile profile)
160 {
161 long id = profile.getId();
162 ContentValues values = ContentValuesFromVpnProfile(profile);
163 return mDatabase.update(TABLE_VPNPROFILE, values, KEY_ID + " = " + id, null) > 0;
164 }
165
166 /**
167 * Delete the given VPN profile from the database.
168 * @param profile the profile to delete
169 * @return true if deleted, false otherwise
170 */
171 public boolean deleteVpnProfile(VpnProfile profile)
172 {
173 long id = profile.getId();
174 return mDatabase.delete(TABLE_VPNPROFILE, KEY_ID + " = " + id, null) > 0;
175 }
176
177 /**
178 * Get a single VPN profile from the database.
179 * @param id the ID of the VPN profile
180 * @return the profile or null, if not found
181 */
182 public VpnProfile getVpnProfile(long id)
183 {
184 VpnProfile profile = null;
185 Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS,
186 KEY_ID + "=" + id, null, null, null, null);
187 if (cursor.moveToFirst())
188 {
189 profile = VpnProfileFromCursor(cursor);
190 }
191 cursor.close();
192 return profile;
193 }
194
195 /**
196 * Get a list of all VPN profiles stored in the database.
197 * @return list of VPN profiles
198 */
199 public List<VpnProfile> getAllVpnProfiles()
200 {
201 List<VpnProfile> vpnProfiles = new ArrayList<VpnProfile>();
202
203 Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS, null, null, null, null, null);
204 cursor.moveToFirst();
205 while (!cursor.isAfterLast())
206 {
207 VpnProfile vpnProfile = VpnProfileFromCursor(cursor);
208 vpnProfiles.add(vpnProfile);
209 cursor.moveToNext();
210 }
211 cursor.close();
212 return vpnProfiles;
213 }
214
215 private VpnProfile VpnProfileFromCursor(Cursor cursor)
216 {
217 VpnProfile profile = new VpnProfile();
218 profile.setId(cursor.getLong(cursor.getColumnIndex(KEY_ID)));
219 profile.setName(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
220 profile.setGateway(cursor.getString(cursor.getColumnIndex(KEY_GATEWAY)));
221 profile.setUsername(cursor.getString(cursor.getColumnIndex(KEY_USERNAME)));
222 profile.setPassword(cursor.getString(cursor.getColumnIndex(KEY_PASSWORD)));
223 profile.setCertificateAlias(cursor.getString(cursor.getColumnIndex(KEY_CERTIFICATE)));
224 profile.setUserCertificateAlias(cursor.getString(cursor.getColumnIndex(KEY_USER_CERTIFICATE)));
225 return profile;
226 }
227
228 private ContentValues ContentValuesFromVpnProfile(VpnProfile profile)
229 {
230 ContentValues values = new ContentValues();
231 values.put(KEY_NAME, profile.getName());
232 values.put(KEY_GATEWAY, profile.getGateway());
233 values.put(KEY_USERNAME, profile.getUsername());
234 values.put(KEY_PASSWORD, profile.getPassword());
235 values.put(KEY_CERTIFICATE, profile.getCertificateAlias());
236 values.put(KEY_USER_CERTIFICATE, profile.getUserCertificateAlias());
237 return values;
238 }
239 }