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