Merge pull request #727 from mar-v-in/fix-cachedpublickeyring

Fix CachedPublicKeyring
This commit is contained in:
Dominik Schürmann 2014-08-01 09:31:14 +02:00
commit d03d574177
2 changed files with 58 additions and 12 deletions

View File

@ -1,5 +1,6 @@
package org.sufficientlysecure.keychain.provider; package org.sufficientlysecure.keychain.provider;
import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
@ -33,6 +34,7 @@ public class CachedPublicKeyRing extends KeyRing {
mUri = uri; mUri = uri;
} }
@Override
public long getMasterKeyId() throws PgpGeneralException { public long getMasterKeyId() throws PgpGeneralException {
try { try {
Object data = mProviderHelper.getGenericData(mUri, Object data = mProviderHelper.getGenericData(mUri,
@ -59,6 +61,17 @@ public class CachedPublicKeyRing extends KeyRing {
return getMasterKeyId(); return getMasterKeyId();
} }
public byte[] getFingerprint() throws PgpGeneralException {
try {
Object data = mProviderHelper.getGenericData(mUri,
KeychainContract.KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB);
return (byte[]) data;
} catch (ProviderHelper.NotFoundException e) {
throw new PgpGeneralException(e);
}
}
@Override
public String getPrimaryUserId() throws PgpGeneralException { public String getPrimaryUserId() throws PgpGeneralException {
try { try {
Object data = mProviderHelper.getGenericData(mUri, Object data = mProviderHelper.getGenericData(mUri,
@ -74,6 +87,7 @@ public class CachedPublicKeyRing extends KeyRing {
return getPrimaryUserId(); return getPrimaryUserId();
} }
@Override
public boolean isRevoked() throws PgpGeneralException { public boolean isRevoked() throws PgpGeneralException {
try { try {
Object data = mProviderHelper.getGenericData(mUri, Object data = mProviderHelper.getGenericData(mUri,
@ -85,6 +99,7 @@ public class CachedPublicKeyRing extends KeyRing {
} }
} }
@Override
public boolean canCertify() throws PgpGeneralException { public boolean canCertify() throws PgpGeneralException {
try { try {
Object data = mProviderHelper.getGenericData(mUri, Object data = mProviderHelper.getGenericData(mUri,
@ -96,21 +111,32 @@ public class CachedPublicKeyRing extends KeyRing {
} }
} }
@Override
public long getEncryptId() throws PgpGeneralException { public long getEncryptId() throws PgpGeneralException {
try { try {
Object data = mProviderHelper.getGenericData(mUri, Cursor subkeys = getSubkeys();
KeychainContract.KeyRings.MASTER_KEY_ID, // TODO if (subkeys != null) {
ProviderHelper.FIELD_TYPE_INTEGER); try {
return (Long) data; while (subkeys.moveToNext()) {
} catch(ProviderHelper.NotFoundException e) { if (subkeys.getInt(subkeys.getColumnIndexOrThrow(KeychainContract.Keys.CAN_ENCRYPT)) != 0) {
return subkeys.getLong(subkeys.getColumnIndexOrThrow(KeychainContract.Keys.KEY_ID));
}
}
} finally {
subkeys.close();
}
}
} catch(Exception e) {
throw new PgpGeneralException(e); throw new PgpGeneralException(e);
} }
throw new PgpGeneralException("No encrypt key found");
} }
@Override
public boolean hasEncrypt() throws PgpGeneralException { public boolean hasEncrypt() throws PgpGeneralException {
try { try {
Object data = mProviderHelper.getGenericData(mUri, Object data = mProviderHelper.getGenericData(mUri,
KeychainContract.KeyRings.CAN_ENCRYPT, // TODO KeychainContract.KeyRings.HAS_ENCRYPT,
ProviderHelper.FIELD_TYPE_INTEGER); ProviderHelper.FIELD_TYPE_INTEGER);
return (Long) data > 0; return (Long) data > 0;
} catch(ProviderHelper.NotFoundException e) { } catch(ProviderHelper.NotFoundException e) {
@ -118,21 +144,32 @@ public class CachedPublicKeyRing extends KeyRing {
} }
} }
@Override
public long getSignId() throws PgpGeneralException { public long getSignId() throws PgpGeneralException {
try { try {
Object data = mProviderHelper.getGenericData(mUri, Cursor subkeys = getSubkeys();
KeychainContract.KeyRings.MASTER_KEY_ID, // TODO if (subkeys != null) {
ProviderHelper.FIELD_TYPE_INTEGER); try {
return (Long) data; while (subkeys.moveToNext()) {
} catch(ProviderHelper.NotFoundException e) { if (subkeys.getInt(subkeys.getColumnIndexOrThrow(KeychainContract.Keys.CAN_SIGN)) != 0) {
return subkeys.getLong(subkeys.getColumnIndexOrThrow(KeychainContract.Keys.KEY_ID));
}
}
} finally {
subkeys.close();
}
}
} catch(Exception e) {
throw new PgpGeneralException(e); throw new PgpGeneralException(e);
} }
throw new PgpGeneralException("No sign key found");
} }
@Override
public boolean hasSign() throws PgpGeneralException { public boolean hasSign() throws PgpGeneralException {
try { try {
Object data = mProviderHelper.getGenericData(mUri, Object data = mProviderHelper.getGenericData(mUri,
KeychainContract.KeyRings.CAN_SIGN, // TODO KeychainContract.KeyRings.HAS_SIGN,
ProviderHelper.FIELD_TYPE_INTEGER); ProviderHelper.FIELD_TYPE_INTEGER);
return (Long) data > 0; return (Long) data > 0;
} catch(ProviderHelper.NotFoundException e) { } catch(ProviderHelper.NotFoundException e) {
@ -140,6 +177,7 @@ public class CachedPublicKeyRing extends KeyRing {
} }
} }
@Override
public int getVerified() throws PgpGeneralException { public int getVerified() throws PgpGeneralException {
try { try {
Object data = mProviderHelper.getGenericData(mUri, Object data = mProviderHelper.getGenericData(mUri,
@ -160,6 +198,10 @@ public class CachedPublicKeyRing extends KeyRing {
} catch(ProviderHelper.NotFoundException e) { } catch(ProviderHelper.NotFoundException e) {
throw new PgpGeneralException(e); throw new PgpGeneralException(e);
} }
}
private Cursor getSubkeys() throws PgpGeneralException {
Uri keysUri = KeychainContract.Keys.buildKeysUri(Long.toString(extractOrGetMasterKeyId()));
return mProviderHelper.getContentResolver().query(keysUri, null, null, null, null);
} }
} }

View File

@ -1024,4 +1024,8 @@ public class ProviderHelper {
} }
} }
} }
public ContentResolver getContentResolver() {
return mContentResolver;
}
} }