From: Tobias Brunner Date: Tue, 27 Dec 2016 14:17:49 +0000 (+0100) Subject: android: Add a UUID property to the VPN profiles X-Git-Tag: 5.5.2dr5~52^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c4ab9af74e1c2532e03358f58aede47104e81917;p=thirdparty%2Fstrongswan.git 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. --- diff --git a/src/frontends/android/app/src/main/java/org/strongswan/android/data/VpnProfile.java b/src/frontends/android/app/src/main/java/org/strongswan/android/data/VpnProfile.java index 8a9d319b5a..54bdfcbddf 100644 --- a/src/frontends/android/app/src/main/java/org/strongswan/android/data/VpnProfile.java +++ b/src/frontends/android/app/src/main/java/org/strongswan/android/data/VpnProfile.java @@ -18,6 +18,8 @@ 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 */ @@ -28,8 +30,14 @@ public class VpnProfile implements Cloneable 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; @@ -40,6 +48,16 @@ public class VpnProfile implements Cloneable this.mId = id; } + public void setUUID(UUID uuid) + { + this.mUUID = uuid; + } + + public UUID getUUID() + { + return mUUID; + } + public String getName() { return mName; @@ -171,7 +189,12 @@ public class VpnProfile implements Cloneable { 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; } diff --git a/src/frontends/android/app/src/main/java/org/strongswan/android/data/VpnProfileDataSource.java b/src/frontends/android/app/src/main/java/org/strongswan/android/data/VpnProfileDataSource.java index 17026536b6..1c509a302a 100644 --- a/src/frontends/android/app/src/main/java/org/strongswan/android/data/VpnProfileDataSource.java +++ b/src/frontends/android/app/src/main/java/org/strongswan/android/data/VpnProfileDataSource.java @@ -19,6 +19,7 @@ package org.strongswan.android.data; import java.util.ArrayList; import java.util.List; +import java.util.UUID; import android.content.ContentValues; import android.content.Context; @@ -33,6 +34,7 @@ public class VpnProfileDataSource { 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"; @@ -53,11 +55,12 @@ public class VpnProfileDataSource 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," + @@ -73,6 +76,7 @@ public class VpnProfileDataSource ");"; private static final String[] ALL_COLUMNS = new String[] { KEY_ID, + KEY_UUID, KEY_NAME, KEY_GATEWAY, KEY_VPN_TYPE, @@ -141,6 +145,12 @@ public class VpnProfileDataSource 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) @@ -261,6 +271,24 @@ public class VpnProfileDataSource 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 @@ -285,6 +313,7 @@ public class VpnProfileDataSource { 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)))); @@ -303,6 +332,7 @@ public class VpnProfileDataSource 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()); @@ -322,4 +352,16 @@ public class VpnProfileDataSource { 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; + } + } } diff --git a/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileDetailActivity.java b/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileDetailActivity.java index 30fb101bee..6405c0dda9 100644 --- a/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileDetailActivity.java +++ b/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileDetailActivity.java @@ -65,6 +65,7 @@ import org.strongswan.android.ui.adapter.CertificateIdentitiesAdapter; import org.strongswan.android.ui.widget.TextInputLayoutHelper; import java.security.cert.X509Certificate; +import java.util.UUID; public class VpnProfileDetailActivity extends AppCompatActivity { @@ -453,6 +454,10 @@ public class VpnProfileDetailActivity extends AppCompatActivity if (mProfile != null) { updateProfileData(); + if (mProfile.getUUID() == null) + { + mProfile.setUUID(UUID.randomUUID()); + } mDataSource.updateVpnProfile(mProfile); } else