From 2802718efb3be3dd39743445de886707dead680f Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 5 Apr 2014 21:38:03 +0200 Subject: [PATCH] certs: more fixes, almost working now --- .../keychain/provider/KeychainProvider.java | 34 ++++++++++------ .../keychain/provider/ProviderHelper.java | 3 ++ .../keychain/ui/ViewKeyCertsFragment.java | 40 +++++++++---------- .../keychain/ui/ViewKeyMainFragment.java | 3 +- .../ui/adapter/ViewKeyUserIdsAdapter.java | 10 +++-- 5 files changed, 52 insertions(+), 38 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainProvider.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainProvider.java index 83b3dd744..01120eedf 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainProvider.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainProvider.java @@ -369,19 +369,29 @@ public class KeychainProvider extends ContentProvider { case KEY_RING_USER_IDS: { HashMap projectionMap = new HashMap(); projectionMap.put(UserIds._ID, Tables.USER_IDS + ".oid AS _id"); - projectionMap.put(UserIds.MASTER_KEY_ID, UserIds.MASTER_KEY_ID); - projectionMap.put(UserIds.USER_ID, UserIds.USER_ID); - projectionMap.put(UserIds.RANK, UserIds.RANK); - projectionMap.put(UserIds.IS_PRIMARY, UserIds.IS_PRIMARY); - projectionMap.put(UserIds.VERIFIED, "0 AS " + UserIds.VERIFIED); + projectionMap.put(UserIds.MASTER_KEY_ID, Tables.USER_IDS + "." + UserIds.MASTER_KEY_ID); + projectionMap.put(UserIds.USER_ID, Tables.USER_IDS + "." + UserIds.USER_ID); + projectionMap.put(UserIds.RANK, Tables.USER_IDS + "." + UserIds.RANK); + projectionMap.put(UserIds.IS_PRIMARY, Tables.USER_IDS + "." + UserIds.IS_PRIMARY); + // we take the minimum (>0) here, where "1" is "verified by known secret key" + projectionMap.put(UserIds.VERIFIED, "MIN(" + Certs.VERIFIED + ") AS " + UserIds.VERIFIED); qb.setProjectionMap(projectionMap); - qb.setTables(Tables.USER_IDS); - qb.appendWhere(UserIds.MASTER_KEY_ID + " = "); + qb.setTables(Tables.USER_IDS + + " LEFT JOIN " + Tables.CERTS + " ON (" + + Tables.USER_IDS + "." + UserIds.MASTER_KEY_ID + " = " + + Tables.CERTS + "." + Certs.MASTER_KEY_ID + + " AND " + Tables.USER_IDS + "." + UserIds.RANK + " = " + + Tables.CERTS + "." + Certs.RANK + + " AND " + Tables.CERTS + "." + Certs.VERIFIED + " > 0" + + ")"); + groupBy = Tables.USER_IDS + "." + UserIds.RANK; + + qb.appendWhere(Tables.USER_IDS + "." + UserIds.MASTER_KEY_ID + " = "); qb.appendWhereEscapeString(uri.getPathSegments().get(1)); if (TextUtils.isEmpty(sortOrder)) { - sortOrder = UserIds.RANK + " ASC"; + sortOrder = Tables.USER_IDS + "." + UserIds.RANK + " ASC"; } break; @@ -433,9 +443,7 @@ public class KeychainProvider extends ContentProvider { projectionMap.put(Certs.KEY_ID_CERTIFIER, Tables.CERTS + "." + Certs.KEY_ID_CERTIFIER); projectionMap.put(Certs.VERIFIED, Tables.CERTS + "." + Certs.VERIFIED); projectionMap.put(Certs.KEY_DATA, Tables.CERTS + "." + Certs.KEY_DATA); - // verified key data projectionMap.put(Certs.USER_ID, Tables.USER_IDS + "." + UserIds.USER_ID); - // verifying key data projectionMap.put(Certs.SIGNER_UID, "signer." + UserIds.USER_ID + " AS " + Certs.SIGNER_UID); qb.setProjectionMap(projectionMap); @@ -447,7 +455,7 @@ public class KeychainProvider extends ContentProvider { + Tables.CERTS + "." + Certs.RANK + " = " + Tables.USER_IDS + "." + UserIds.RANK + ") LEFT JOIN " + Tables.USER_IDS + " AS signer ON (" - + Tables.CERTS + "." + Keys.MASTER_KEY_ID + " = " + + Tables.CERTS + "." + Certs.KEY_ID_CERTIFIER + " = " + "signer." + UserIds.MASTER_KEY_ID + " AND " + "signer." + Keys.RANK + " = 0" @@ -553,7 +561,9 @@ public class KeychainProvider extends ContentProvider { break; case KEY_RING_CERTS: - db.insertOrThrow(Tables.CERTS, null, values); + // we replace here, keeping only the latest signature + // TODO this would be better handled in saveKeyRing directly! + db.replaceOrThrow(Tables.CERTS, null, values); keyId = values.getAsLong(Certs.MASTER_KEY_ID); break; diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index 503fed3c9..02d8d29f2 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -223,6 +223,9 @@ public class ProviderHelper { // get a list of owned secret keys, for verification filtering Map allKeyRings = getPGPKeyRings(context, KeyRingData.buildSecretKeyRingUri()); + // special case: available secret keys verify themselves! + if(secretRing != null) + allKeyRings.put(secretRing.getSecretKey().getKeyID(), secretRing); int userIdRank = 0; for (String userId : new IterableIterator(masterKey.getUserIDs())) { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyCertsFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyCertsFragment.java index 26e72f10b..1696d18dd 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyCertsFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyCertsFragment.java @@ -38,8 +38,8 @@ import android.widget.TextView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; -import org.sufficientlysecure.keychain.provider.KeychainContract; -import org.sufficientlysecure.keychain.provider.KeychainDatabase; +import org.sufficientlysecure.keychain.provider.KeychainContract.Certs; +import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables; import org.sufficientlysecure.keychain.util.Log; import se.emilsjolander.stickylistheaders.ApiLevelTooLowException; @@ -52,20 +52,20 @@ public class ViewKeyCertsFragment extends Fragment // These are the rows that we will retrieve. static final String[] PROJECTION = new String[] { - KeychainContract.Certs._ID, - KeychainContract.Certs.MASTER_KEY_ID, - KeychainContract.Certs.VERIFIED, - KeychainContract.Certs.RANK, - KeychainContract.Certs.KEY_ID_CERTIFIER, - KeychainContract.Certs.USER_ID, - KeychainContract.Certs.SIGNER_UID + Certs._ID, + Certs.MASTER_KEY_ID, + Certs.VERIFIED, + Certs.RANK, + Certs.KEY_ID_CERTIFIER, + Certs.USER_ID, + Certs.SIGNER_UID }; // sort by our user id, static final String SORT_ORDER = - KeychainDatabase.Tables.USER_IDS + "." + KeychainContract.UserIds.RANK + " ASC, " - + KeychainDatabase.Tables.CERTS + "." + KeychainContract.Certs.VERIFIED + " DESC, " - + KeychainContract.Certs.SIGNER_UID + " ASC"; + Tables.CERTS + "." + Certs.RANK + " ASC, " + + Certs.VERIFIED + " DESC, " + + Certs.SIGNER_UID + " ASC"; public static final String ARG_DATA_URI = "data_uri"; @@ -133,7 +133,7 @@ public class ViewKeyCertsFragment extends Fragment } Uri uri = getArguments().getParcelable(ARG_DATA_URI); - mBaseUri = KeychainContract.Certs.buildCertsUri(uri); + mBaseUri = Certs.buildCertsUri(uri); mStickyList.setAreHeadersSticky(true); mStickyList.setDrawingListUnderStickyHeader(false); @@ -185,7 +185,7 @@ public class ViewKeyCertsFragment extends Fragment public void onItemClick(AdapterView adapterView, View view, int position, long id) { Intent viewIntent = null; viewIntent = new Intent(getActivity(), ViewCertActivity.class); - viewIntent.setData(KeychainContract.Certs.buildCertsUri(Long.toString(id))); + viewIntent.setData(Certs.buildCertsUri(Long.toString(id))); startActivity(viewIntent); } @@ -230,12 +230,12 @@ public class ViewKeyCertsFragment extends Fragment private void initIndex(Cursor cursor) { if (cursor != null) { - mIndexCertId = cursor.getColumnIndexOrThrow(KeychainContract.Certs.MASTER_KEY_ID); - mIndexUserId = cursor.getColumnIndexOrThrow(KeychainContract.UserIds.USER_ID); - mIndexRank = cursor.getColumnIndexOrThrow(KeychainContract.UserIds.RANK); - mIndexVerified = cursor.getColumnIndexOrThrow(KeychainContract.Certs.VERIFIED); - mIndexSignerKeyId = cursor.getColumnIndexOrThrow(KeychainContract.Certs.KEY_ID_CERTIFIER); - mIndexSignerUserId = cursor.getColumnIndexOrThrow(KeychainContract.Certs.SIGNER_UID); + mIndexCertId = cursor.getColumnIndexOrThrow(Certs.MASTER_KEY_ID); + mIndexUserId = cursor.getColumnIndexOrThrow(Certs.USER_ID); + mIndexRank = cursor.getColumnIndexOrThrow(Certs.RANK); + mIndexVerified = cursor.getColumnIndexOrThrow(Certs.VERIFIED); + mIndexSignerKeyId = cursor.getColumnIndexOrThrow(Certs.KEY_ID_CERTIFIER); + mIndexSignerUserId = cursor.getColumnIndexOrThrow(Certs.SIGNER_UID); } } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java index b5a800712..e275dca59 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java @@ -39,7 +39,6 @@ import com.beardedhen.androidbootstrap.BootstrapButton; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; -import org.sufficientlysecure.keychain.provider.KeychainDatabase; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.KeychainContract.Keys; import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds; @@ -176,7 +175,7 @@ public class ViewKeyMainFragment extends Fragment implements static final int INDEX_UNIFIED_EXPIRY = 8; static final String[] USER_IDS_PROJECTION = new String[] { - UserIds._ID, UserIds.USER_ID, UserIds.RANK, + UserIds._ID, UserIds.USER_ID, UserIds.RANK, UserIds.VERIFIED }; static final String[] KEYS_PROJECTION = new String[] { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ViewKeyUserIdsAdapter.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ViewKeyUserIdsAdapter.java index 2677a1a1a..772486384 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ViewKeyUserIdsAdapter.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ViewKeyUserIdsAdapter.java @@ -84,7 +84,7 @@ public class ViewKeyUserIdsAdapter extends CursorAdapter { if (cursor != null) { mIndexUserId = cursor.getColumnIndexOrThrow(UserIds.USER_ID); mIndexRank = cursor.getColumnIndexOrThrow(UserIds.RANK); - // mVerifiedId = cursor.getColumnIndexOrThrow(UserIds.VERIFIED); + mVerifiedId = cursor.getColumnIndexOrThrow(UserIds.VERIFIED); } } @@ -106,10 +106,12 @@ public class ViewKeyUserIdsAdapter extends CursorAdapter { } vAddress.setText(userId[1]); - int verified = 1; // cursor.getInt(mVerifiedId); - // TODO introduce own resource for this :) - if(verified > 0) + int verified = cursor.getInt(mVerifiedId); + // TODO introduce own resources for this :) + if(verified == 1) vVerified.setImageResource(android.R.drawable.presence_online); + else if(verified > 1) + vVerified.setImageResource(android.R.drawable.presence_away); else vVerified.setImageResource(android.R.drawable.presence_invisible);