package org.strongswan.android.data;
+import java.util.UUID;
+
public class VpnProfile implements Cloneable
{
/* While storing this as EnumSet would be nicer this simplifies storing it in a database */
private String mRemoteId, mLocalId;
private Integer mMTU, mPort, mSplitTunneling;
private VpnType mVpnType;
+ private UUID mUUID;
private long mId = -1;
+ public VpnProfile()
+ {
+ this.mUUID = UUID.randomUUID();
+ }
+
public long getId()
{
return mId;
this.mId = id;
}
+ public void setUUID(UUID uuid)
+ {
+ this.mUUID = uuid;
+ }
+
+ public UUID getUUID()
+ {
+ return mUUID;
+ }
+
public String getName()
{
return mName;
{
if (o != null && o instanceof VpnProfile)
{
- return this.mId == ((VpnProfile)o).getId();
+ VpnProfile other = (VpnProfile)o;
+ if (this.mUUID != null && other.getUUID() != null)
+ {
+ return this.mUUID.equals(other.getUUID());
+ }
+ return this.mId == other.getId();
}
return false;
}
import java.util.ArrayList;
import java.util.List;
+import java.util.UUID;
import android.content.ContentValues;
import android.content.Context;
{
private static final String TAG = VpnProfileDataSource.class.getSimpleName();
public static final String KEY_ID = "_id";
+ public static final String KEY_UUID = "_uuid";
public static final String KEY_NAME = "name";
public static final String KEY_GATEWAY = "gateway";
public static final String KEY_VPN_TYPE = "vpn_type";
private static final String DATABASE_NAME = "strongswan.db";
private static final String TABLE_VPNPROFILE = "vpnprofile";
- private static final int DATABASE_VERSION = 8;
+ private static final int DATABASE_VERSION = 9;
public static final String DATABASE_CREATE =
"CREATE TABLE " + TABLE_VPNPROFILE + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
+ KEY_UUID + " TEXT UNIQUE," +
KEY_NAME + " TEXT NOT NULL," +
KEY_GATEWAY + " TEXT NOT NULL," +
KEY_VPN_TYPE + " TEXT NOT NULL," +
");";
private static final String[] ALL_COLUMNS = new String[] {
KEY_ID,
+ KEY_UUID,
KEY_NAME,
KEY_GATEWAY,
KEY_VPN_TYPE,
db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_REMOTE_ID +
" TEXT;");
}
+ if (oldVersion < 9)
+ {
+ db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_UUID +
+ " TEXT;");
+ updateColumns(db);
+ }
}
private void updateColumns(SQLiteDatabase db)
return profile;
}
+ /**
+ * Get a single VPN profile from the database by its UUID.
+ * @param uuid the UUID of the VPN profile
+ * @return the profile or null, if not found
+ */
+ public VpnProfile getVpnProfile(UUID uuid)
+ {
+ VpnProfile profile = null;
+ Cursor cursor = mDatabase.query(TABLE_VPNPROFILE, ALL_COLUMNS,
+ KEY_UUID + "='" + uuid.toString() + "'", null, null, null, null);
+ if (cursor.moveToFirst())
+ {
+ profile = VpnProfileFromCursor(cursor);
+ }
+ cursor.close();
+ return profile;
+ }
+
/**
* Get a list of all VPN profiles stored in the database.
* @return list of VPN profiles
{
VpnProfile profile = new VpnProfile();
profile.setId(cursor.getLong(cursor.getColumnIndex(KEY_ID)));
+ profile.setUUID(getUUID(cursor, cursor.getColumnIndex(KEY_UUID)));
profile.setName(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
profile.setGateway(cursor.getString(cursor.getColumnIndex(KEY_GATEWAY)));
profile.setVpnType(VpnType.fromIdentifier(cursor.getString(cursor.getColumnIndex(KEY_VPN_TYPE))));
private ContentValues ContentValuesFromVpnProfile(VpnProfile profile)
{
ContentValues values = new ContentValues();
+ values.put(KEY_UUID, profile.getUUID() != null ? profile.getUUID().toString() : null);
values.put(KEY_NAME, profile.getName());
values.put(KEY_GATEWAY, profile.getGateway());
values.put(KEY_VPN_TYPE, profile.getVpnType().getIdentifier());
{
return cursor.isNull(columnIndex) ? null : cursor.getInt(columnIndex);
}
+
+ private UUID getUUID(Cursor cursor, int columnIndex)
+ {
+ try
+ {
+ return cursor.isNull(columnIndex) ? null : UUID.fromString(cursor.getString(columnIndex));
+ }
+ catch (Exception e)
+ {
+ return null;
+ }
+ }
}