android: Add a UUID property to the VPN profiles

All new or edited profiles get a random UUID.  We currently don't
enforce one, though.  Later we might change that and use the UUID as
primary key.
This commit is contained in:
Tobias Brunner
2017-01-20 11:01:32 +01:00
parent a4c7778086
commit c4ab9af74e
3 changed files with 72 additions and 2 deletions
@@ -18,6 +18,8 @@
package org.strongswan.android.data; package org.strongswan.android.data;
import java.util.UUID;
public class VpnProfile implements Cloneable public class VpnProfile implements Cloneable
{ {
/* While storing this as EnumSet would be nicer this simplifies storing it in a database */ /* While storing this as EnumSet would be nicer this simplifies storing it in a database */
@@ -28,8 +30,14 @@ public class VpnProfile implements Cloneable
private String mRemoteId, mLocalId; private String mRemoteId, mLocalId;
private Integer mMTU, mPort, mSplitTunneling; private Integer mMTU, mPort, mSplitTunneling;
private VpnType mVpnType; private VpnType mVpnType;
private UUID mUUID;
private long mId = -1; private long mId = -1;
public VpnProfile()
{
this.mUUID = UUID.randomUUID();
}
public long getId() public long getId()
{ {
return mId; return mId;
@@ -40,6 +48,16 @@ public class VpnProfile implements Cloneable
this.mId = id; this.mId = id;
} }
public void setUUID(UUID uuid)
{
this.mUUID = uuid;
}
public UUID getUUID()
{
return mUUID;
}
public String getName() public String getName()
{ {
return mName; return mName;
@@ -171,7 +189,12 @@ public class VpnProfile implements Cloneable
{ {
if (o != null && o instanceof VpnProfile) 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; return false;
} }
@@ -19,6 +19,7 @@ package org.strongswan.android.data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
@@ -33,6 +34,7 @@ public class VpnProfileDataSource
{ {
private static final String TAG = VpnProfileDataSource.class.getSimpleName(); private static final String TAG = VpnProfileDataSource.class.getSimpleName();
public static final String KEY_ID = "_id"; 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_NAME = "name";
public static final String KEY_GATEWAY = "gateway"; public static final String KEY_GATEWAY = "gateway";
public static final String KEY_VPN_TYPE = "vpn_type"; public static final String KEY_VPN_TYPE = "vpn_type";
@@ -53,11 +55,12 @@ public class VpnProfileDataSource
private static final String DATABASE_NAME = "strongswan.db"; private static final String DATABASE_NAME = "strongswan.db";
private static final String TABLE_VPNPROFILE = "vpnprofile"; 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 = public static final String DATABASE_CREATE =
"CREATE TABLE " + TABLE_VPNPROFILE + " (" + "CREATE TABLE " + TABLE_VPNPROFILE + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_UUID + " TEXT UNIQUE," +
KEY_NAME + " TEXT NOT NULL," + KEY_NAME + " TEXT NOT NULL," +
KEY_GATEWAY + " TEXT NOT NULL," + KEY_GATEWAY + " TEXT NOT NULL," +
KEY_VPN_TYPE + " TEXT NOT NULL," + KEY_VPN_TYPE + " TEXT NOT NULL," +
@@ -73,6 +76,7 @@ public class VpnProfileDataSource
");"; ");";
private static final String[] ALL_COLUMNS = new String[] { private static final String[] ALL_COLUMNS = new String[] {
KEY_ID, KEY_ID,
KEY_UUID,
KEY_NAME, KEY_NAME,
KEY_GATEWAY, KEY_GATEWAY,
KEY_VPN_TYPE, KEY_VPN_TYPE,
@@ -141,6 +145,12 @@ public class VpnProfileDataSource
db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_REMOTE_ID + db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_REMOTE_ID +
" TEXT;"); " TEXT;");
} }
if (oldVersion < 9)
{
db.execSQL("ALTER TABLE " + TABLE_VPNPROFILE + " ADD " + KEY_UUID +
" TEXT;");
updateColumns(db);
}
} }
private void updateColumns(SQLiteDatabase db) private void updateColumns(SQLiteDatabase db)
@@ -261,6 +271,24 @@ public class VpnProfileDataSource
return profile; 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. * Get a list of all VPN profiles stored in the database.
* @return list of VPN profiles * @return list of VPN profiles
@@ -285,6 +313,7 @@ public class VpnProfileDataSource
{ {
VpnProfile profile = new VpnProfile(); VpnProfile profile = new VpnProfile();
profile.setId(cursor.getLong(cursor.getColumnIndex(KEY_ID))); 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.setName(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
profile.setGateway(cursor.getString(cursor.getColumnIndex(KEY_GATEWAY))); profile.setGateway(cursor.getString(cursor.getColumnIndex(KEY_GATEWAY)));
profile.setVpnType(VpnType.fromIdentifier(cursor.getString(cursor.getColumnIndex(KEY_VPN_TYPE)))); profile.setVpnType(VpnType.fromIdentifier(cursor.getString(cursor.getColumnIndex(KEY_VPN_TYPE))));
@@ -303,6 +332,7 @@ public class VpnProfileDataSource
private ContentValues ContentValuesFromVpnProfile(VpnProfile profile) private ContentValues ContentValuesFromVpnProfile(VpnProfile profile)
{ {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(KEY_UUID, profile.getUUID() != null ? profile.getUUID().toString() : null);
values.put(KEY_NAME, profile.getName()); values.put(KEY_NAME, profile.getName());
values.put(KEY_GATEWAY, profile.getGateway()); values.put(KEY_GATEWAY, profile.getGateway());
values.put(KEY_VPN_TYPE, profile.getVpnType().getIdentifier()); values.put(KEY_VPN_TYPE, profile.getVpnType().getIdentifier());
@@ -322,4 +352,16 @@ public class VpnProfileDataSource
{ {
return cursor.isNull(columnIndex) ? null : cursor.getInt(columnIndex); 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;
}
}
} }
@@ -65,6 +65,7 @@ import org.strongswan.android.ui.adapter.CertificateIdentitiesAdapter;
import org.strongswan.android.ui.widget.TextInputLayoutHelper; import org.strongswan.android.ui.widget.TextInputLayoutHelper;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.UUID;
public class VpnProfileDetailActivity extends AppCompatActivity public class VpnProfileDetailActivity extends AppCompatActivity
{ {
@@ -453,6 +454,10 @@ public class VpnProfileDetailActivity extends AppCompatActivity
if (mProfile != null) if (mProfile != null)
{ {
updateProfileData(); updateProfileData();
if (mProfile.getUUID() == null)
{
mProfile.setUUID(UUID.randomUUID());
}
mDataSource.updateVpnProfile(mProfile); mDataSource.updateVpnProfile(mProfile);
} }
else else