Use properly fixed numeral values in Trust enum

Why, oh God, why?! #thanksjamesgosling
This commit is contained in:
Andreas Straub 2015-07-21 01:52:22 +02:00
parent 639ebd644b
commit b7ff2c3461
2 changed files with 31 additions and 9 deletions

View File

@ -99,10 +99,28 @@ public class AxolotlService {
private int currentPreKeyId = 0; private int currentPreKeyId = 0;
public enum Trust { public enum Trust {
UNDECIDED, // 0 UNDECIDED(0),
TRUSTED, TRUSTED(1),
UNTRUSTED, UNTRUSTED(2),
COMPROMISED; COMPROMISED(3);
private static final Map<Integer, Trust> trustsByValue = new HashMap<>();
static {
for (Trust trust : Trust.values()) {
trustsByValue.put(trust.getCode(), trust);
}
}
private final int code;
Trust(int code){
this.code = code;
}
public int getCode() {
return this.code;
}
public String toString() { public String toString() {
switch(this){ switch(this){
@ -119,6 +137,10 @@ public class AxolotlService {
public static Trust fromBoolean(Boolean trusted) { public static Trust fromBoolean(Boolean trusted) {
return trusted?TRUSTED:UNTRUSTED; return trusted?TRUSTED:UNTRUSTED;
} }
public static Trust fromCode(int code) {
return trustsByValue.get(code);
}
}; };
private static IdentityKeyPair generateIdentityKeyPair() { private static IdentityKeyPair generateIdentityKeyPair() {

View File

@ -845,7 +845,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
while(cursor.moveToNext()) { while(cursor.moveToNext()) {
if ( trust != null && if ( trust != null &&
cursor.getInt(cursor.getColumnIndex(AxolotlService.SQLiteAxolotlStore.TRUSTED)) cursor.getInt(cursor.getColumnIndex(AxolotlService.SQLiteAxolotlStore.TRUSTED))
!= trust.ordinal()) { != trust.getCode()) {
continue; continue;
} }
try { try {
@ -864,7 +864,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
String[] args = { String[] args = {
account.getUuid(), account.getUuid(),
name, name,
String.valueOf(AxolotlService.SQLiteAxolotlStore.Trust.TRUSTED.ordinal()) String.valueOf(AxolotlService.SQLiteAxolotlStore.Trust.TRUSTED.getCode())
}; };
return DatabaseUtils.queryNumEntries(db, AxolotlService.SQLiteAxolotlStore.IDENTITIES_TABLENAME, return DatabaseUtils.queryNumEntries(db, AxolotlService.SQLiteAxolotlStore.IDENTITIES_TABLENAME,
AxolotlService.SQLiteAxolotlStore.ACCOUNT + " = ?" AxolotlService.SQLiteAxolotlStore.ACCOUNT + " = ?"
@ -886,7 +886,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
values.put(AxolotlService.SQLiteAxolotlStore.OWN, own ? 1 : 0); values.put(AxolotlService.SQLiteAxolotlStore.OWN, own ? 1 : 0);
values.put(AxolotlService.SQLiteAxolotlStore.FINGERPRINT, fingerprint); values.put(AxolotlService.SQLiteAxolotlStore.FINGERPRINT, fingerprint);
values.put(AxolotlService.SQLiteAxolotlStore.KEY, base64Serialized); values.put(AxolotlService.SQLiteAxolotlStore.KEY, base64Serialized);
values.put(AxolotlService.SQLiteAxolotlStore.TRUSTED, trusted.ordinal()); values.put(AxolotlService.SQLiteAxolotlStore.TRUSTED, trusted.getCode());
db.insert(AxolotlService.SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values); db.insert(AxolotlService.SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
} }
@ -896,7 +896,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
if (cursor.getCount() > 0) { if (cursor.getCount() > 0) {
cursor.moveToFirst(); cursor.moveToFirst();
int trustValue = cursor.getInt(cursor.getColumnIndex(AxolotlService.SQLiteAxolotlStore.TRUSTED)); int trustValue = cursor.getInt(cursor.getColumnIndex(AxolotlService.SQLiteAxolotlStore.TRUSTED));
trust = AxolotlService.SQLiteAxolotlStore.Trust.values()[trustValue]; trust = AxolotlService.SQLiteAxolotlStore.Trust.fromCode(trustValue);
} }
cursor.close(); cursor.close();
return trust; return trust;
@ -909,7 +909,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
fingerprint fingerprint
}; };
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(AxolotlService.SQLiteAxolotlStore.TRUSTED, trust.ordinal()); values.put(AxolotlService.SQLiteAxolotlStore.TRUSTED, trust.getCode());
int rows = db.update(AxolotlService.SQLiteAxolotlStore.IDENTITIES_TABLENAME, values, int rows = db.update(AxolotlService.SQLiteAxolotlStore.IDENTITIES_TABLENAME, values,
AxolotlService.SQLiteAxolotlStore.ACCOUNT + " = ? AND " AxolotlService.SQLiteAxolotlStore.ACCOUNT + " = ? AND "
+ AxolotlService.SQLiteAxolotlStore.FINGERPRINT + " = ? ", + AxolotlService.SQLiteAxolotlStore.FINGERPRINT + " = ? ",