android: Avoid lint errors when determining column indices
authorTobias Brunner <tobias@strongswan.org>
Wed, 5 May 2021 14:09:37 +0000 (16:09 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 5 May 2021 14:40:14 +0000 (16:40 +0200)
The lint version used on our GitHub build hosts reported these errors:

Error: Value must be ≥ 0 [Range]
        db.update(TABLE_VPNPROFILE, values, KEY_ID + " = " + cursor.getLong(cursor.getColumnIndex(KEY_ID)), null);

That's because get*() expect a valid index >= 0 but getColumnIndex()
can return -1 if the column name doesn't exist.

src/frontends/android/app/src/main/java/org/strongswan/android/data/VpnProfileDataSource.java

index 102b279..577c645 100644 (file)
@@ -235,7 +235,7 @@ public class VpnProfileDataSource
                                        {
                                                ContentValues values = new ContentValues();
                                                values.put(KEY_UUID, UUID.randomUUID().toString());
-                                               db.update(TABLE_VPNPROFILE, values, KEY_ID + " = " + cursor.getLong(cursor.getColumnIndex(KEY_ID)), null);
+                                               db.update(TABLE_VPNPROFILE, values, KEY_ID + " = " + cursor.getLong(cursor.getColumnIndexOrThrow(KEY_ID)), null);
                                        }
                                        cursor.close();
                                        db.setTransactionSuccessful();
@@ -433,29 +433,29 @@ public class VpnProfileDataSource
        private VpnProfile VpnProfileFromCursor(Cursor cursor)
        {
                VpnProfile profile = new VpnProfile();
-               profile.setId(cursor.getLong(cursor.getColumnIndex(KEY_ID)));
-               profile.setUUID(UUID.fromString(cursor.getString(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))));
-               profile.setUsername(cursor.getString(cursor.getColumnIndex(KEY_USERNAME)));
-               profile.setPassword(cursor.getString(cursor.getColumnIndex(KEY_PASSWORD)));
-               profile.setCertificateAlias(cursor.getString(cursor.getColumnIndex(KEY_CERTIFICATE)));
-               profile.setUserCertificateAlias(cursor.getString(cursor.getColumnIndex(KEY_USER_CERTIFICATE)));
-               profile.setMTU(getInt(cursor, cursor.getColumnIndex(KEY_MTU)));
-               profile.setPort(getInt(cursor, cursor.getColumnIndex(KEY_PORT)));
-               profile.setSplitTunneling(getInt(cursor, cursor.getColumnIndex(KEY_SPLIT_TUNNELING)));
-               profile.setLocalId(cursor.getString(cursor.getColumnIndex(KEY_LOCAL_ID)));
-               profile.setRemoteId(cursor.getString(cursor.getColumnIndex(KEY_REMOTE_ID)));
-               profile.setExcludedSubnets(cursor.getString(cursor.getColumnIndex(KEY_EXCLUDED_SUBNETS)));
-               profile.setIncludedSubnets(cursor.getString(cursor.getColumnIndex(KEY_INCLUDED_SUBNETS)));
-               profile.setSelectedAppsHandling(getInt(cursor, cursor.getColumnIndex(KEY_SELECTED_APPS)));
-               profile.setSelectedApps(cursor.getString(cursor.getColumnIndex(KEY_SELECTED_APPS_LIST)));
-               profile.setNATKeepAlive(getInt(cursor, cursor.getColumnIndex(KEY_NAT_KEEPALIVE)));
-               profile.setFlags(getInt(cursor, cursor.getColumnIndex(KEY_FLAGS)));
-               profile.setIkeProposal(cursor.getString(cursor.getColumnIndex(KEY_IKE_PROPOSAL)));
-               profile.setEspProposal(cursor.getString(cursor.getColumnIndex(KEY_ESP_PROPOSAL)));
-               profile.setDnsServers(cursor.getString(cursor.getColumnIndex(KEY_DNS_SERVERS)));
+               profile.setId(cursor.getLong(cursor.getColumnIndexOrThrow(KEY_ID)));
+               profile.setUUID(UUID.fromString(cursor.getString(cursor.getColumnIndexOrThrow(KEY_UUID))));
+               profile.setName(cursor.getString(cursor.getColumnIndexOrThrow(KEY_NAME)));
+               profile.setGateway(cursor.getString(cursor.getColumnIndexOrThrow(KEY_GATEWAY)));
+               profile.setVpnType(VpnType.fromIdentifier(cursor.getString(cursor.getColumnIndexOrThrow(KEY_VPN_TYPE))));
+               profile.setUsername(cursor.getString(cursor.getColumnIndexOrThrow(KEY_USERNAME)));
+               profile.setPassword(cursor.getString(cursor.getColumnIndexOrThrow(KEY_PASSWORD)));
+               profile.setCertificateAlias(cursor.getString(cursor.getColumnIndexOrThrow(KEY_CERTIFICATE)));
+               profile.setUserCertificateAlias(cursor.getString(cursor.getColumnIndexOrThrow(KEY_USER_CERTIFICATE)));
+               profile.setMTU(getInt(cursor, cursor.getColumnIndexOrThrow(KEY_MTU)));
+               profile.setPort(getInt(cursor, cursor.getColumnIndexOrThrow(KEY_PORT)));
+               profile.setSplitTunneling(getInt(cursor, cursor.getColumnIndexOrThrow(KEY_SPLIT_TUNNELING)));
+               profile.setLocalId(cursor.getString(cursor.getColumnIndexOrThrow(KEY_LOCAL_ID)));
+               profile.setRemoteId(cursor.getString(cursor.getColumnIndexOrThrow(KEY_REMOTE_ID)));
+               profile.setExcludedSubnets(cursor.getString(cursor.getColumnIndexOrThrow(KEY_EXCLUDED_SUBNETS)));
+               profile.setIncludedSubnets(cursor.getString(cursor.getColumnIndexOrThrow(KEY_INCLUDED_SUBNETS)));
+               profile.setSelectedAppsHandling(getInt(cursor, cursor.getColumnIndexOrThrow(KEY_SELECTED_APPS)));
+               profile.setSelectedApps(cursor.getString(cursor.getColumnIndexOrThrow(KEY_SELECTED_APPS_LIST)));
+               profile.setNATKeepAlive(getInt(cursor, cursor.getColumnIndexOrThrow(KEY_NAT_KEEPALIVE)));
+               profile.setFlags(getInt(cursor, cursor.getColumnIndexOrThrow(KEY_FLAGS)));
+               profile.setIkeProposal(cursor.getString(cursor.getColumnIndexOrThrow(KEY_IKE_PROPOSAL)));
+               profile.setEspProposal(cursor.getString(cursor.getColumnIndexOrThrow(KEY_ESP_PROPOSAL)));
+               profile.setDnsServers(cursor.getString(cursor.getColumnIndexOrThrow(KEY_DNS_SERVERS)));
                return profile;
        }