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_USERNAME
= "username";
38 public static final String KEY_PASSWORD
= "password";
39 public static final String KEY_CERTIFICATE
= "certificate";
41 private DatabaseHelper mDbHelper
;
42 private SQLiteDatabase mDatabase
;
43 private final Context mContext
;
45 private static final String DATABASE_NAME
= "strongswan.db";
46 private static final String TABLE_VPNPROFILE
= "vpnprofile";
48 private static final int DATABASE_VERSION
= 1;
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" +
59 private final String
[] ALL_COLUMNS
= new String
[] {
68 private static class DatabaseHelper
extends SQLiteOpenHelper
70 public DatabaseHelper(Context context
)
72 super(context
, DATABASE_NAME
, null
, DATABASE_VERSION
);
76 public void onCreate(SQLiteDatabase database
)
78 database
.execSQL(DATABASE_CREATE
);
82 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
)
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
);
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
96 public VpnProfileDataSource(Context context
)
98 this.mContext
= context
;
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
107 public VpnProfileDataSource
open() throws SQLException
109 if (mDbHelper
== null
)
111 mDbHelper
= new DatabaseHelper(mContext
);
112 mDatabase
= mDbHelper
.getWritableDatabase();
118 * Close the data source.
122 if (mDbHelper
!= null
)
130 * Insert the given VPN profile into the database. On success the Id of
131 * the object is updated and the object returned.
133 * @param profile the profile to add
134 * @return the added VPN profile or null, if failed
136 public VpnProfile
insertProfile(VpnProfile profile
)
138 ContentValues values
= ContentValuesFromVpnProfile(profile
);
139 long insertId
= mDatabase
.insert(TABLE_VPNPROFILE
, null
, values
);
144 profile
.setId(insertId
);
149 * Updates the given VPN profile in the database.
150 * @param profile the profile to update
151 * @return true if update succeeded, false otherwise
153 public boolean updateVpnProfile(VpnProfile profile
)
155 long id
= profile
.getId();
156 ContentValues values
= ContentValuesFromVpnProfile(profile
);
157 return mDatabase
.update(TABLE_VPNPROFILE
, values
, KEY_ID
+ " = " + id
, null
) > 0;
161 * Delete the given VPN profile from the database.
162 * @param profile the profile to delete
163 * @return true if deleted, false otherwise
165 public boolean deleteVpnProfile(VpnProfile profile
)
167 long id
= profile
.getId();
168 return mDatabase
.delete(TABLE_VPNPROFILE
, KEY_ID
+ " = " + id
, null
) > 0;
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
176 public VpnProfile
getVpnProfile(long id
)
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())
183 profile
= VpnProfileFromCursor(cursor
);
190 * Get a list of all VPN profiles stored in the database.
191 * @return list of VPN profiles
193 public List
<VpnProfile
> getAllVpnProfiles()
195 List
<VpnProfile
> vpnProfiles
= new ArrayList
<VpnProfile
>();
197 Cursor cursor
= mDatabase
.query(TABLE_VPNPROFILE
, ALL_COLUMNS
, null
, null
, null
, null
, null
);
198 cursor
.moveToFirst();
199 while (!cursor
.isAfterLast())
201 VpnProfile vpnProfile
= VpnProfileFromCursor(cursor
);
202 vpnProfiles
.add(vpnProfile
);
209 private VpnProfile
VpnProfileFromCursor(Cursor cursor
)
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
)));
221 private ContentValues
ContentValuesFromVpnProfile(VpnProfile profile
)
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());