From 0bcf7a39bf44994dfcc8e4f8cc52084976b597b0 Mon Sep 17 00:00:00 2001 From: mar-v-in Date: Wed, 18 Jun 2014 16:05:01 +0200 Subject: [PATCH 1/5] Continue contact db sync - Only add keyrings to contact db that are not expired nor revoked - Merge all user ids of a key into one contact (#659) - Update contacts: Changes in keyrings (user id add, user id revoke, change of primary id) will be updated into contact db TODO: - delete contact once keyring is removed from OK - sync: wait for key downloads to complete before changing contact db --- .../keychain/helper/ContactHelper.java | 128 ++++++++++++------ 1 file changed, 89 insertions(+), 39 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java index f50ccf6f8..8e4505004 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java @@ -22,7 +22,6 @@ import android.accounts.AccountManager; import android.content.*; import android.database.Cursor; import android.net.Uri; -import android.os.RemoteException; import android.provider.ContactsContract; import android.util.Patterns; import org.sufficientlysecure.keychain.Constants; @@ -32,10 +31,7 @@ import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.util.Log; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; public class ContactHelper { @@ -43,12 +39,17 @@ public class ContactHelper { KeychainContract.KeyRings.USER_ID, KeychainContract.KeyRings.FINGERPRINT, KeychainContract.KeyRings.KEY_ID, - KeychainContract.KeyRings.MASTER_KEY_ID}; + KeychainContract.KeyRings.MASTER_KEY_ID, + KeychainContract.KeyRings.EXPIRY, + KeychainContract.KeyRings.IS_REVOKED}; + public static final String[] USER_IDS_PROJECTION = new String[]{ + KeychainContract.UserIds.USER_ID + }; public static final String[] RAW_CONTACT_ID_PROJECTION = new String[]{ContactsContract.RawContacts._ID}; public static final String FIND_RAW_CONTACT_SELECTION = ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " + ContactsContract.RawContacts.SOURCE_ID + "=?"; - public static final List getMailAccounts(Context context) { + public static List getMailAccounts(Context context) { final Account[] accounts = AccountManager.get(context).getAccounts(); final Set emailSet = new HashSet(); for (Account account : accounts) { @@ -88,16 +89,24 @@ public class ContactHelper { return null; } + private static ContentProviderOperation.Builder referenceRawContact(ContentProviderOperation.Builder builder, int rawContactId) { + return rawContactId == -1 ? + builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) : + builder.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); + } + public static void writeKeysToContacts(Context context) { ContentResolver resolver = context.getContentResolver(); Cursor cursor = resolver.query(KeychainContract.KeyRings.buildUnifiedKeyRingsUri(), KEYS_TO_CONTACT_PROJECTION, null, null, null); if (cursor != null) { while (cursor.moveToNext()) { - String[] userId = KeyRing.splitUserId(cursor.getString(0)); + String[] primaryUserId = KeyRing.splitUserId(cursor.getString(0)); String fingerprint = PgpKeyHelper.convertFingerprintToHex(cursor.getBlob(1)); String keyIdShort = PgpKeyHelper.convertKeyIdToHexShort(cursor.getLong(2)); long masterKeyId = cursor.getLong(3); + boolean isExpired = !cursor.isNull(4) && new Date(cursor.getLong(4) * 1000).before(new Date()); + boolean isRevoked = cursor.getInt(5) > 0; int rawContactId = -1; Cursor raw = resolver.query(ContactsContract.RawContacts.CONTENT_URI, RAW_CONTACT_ID_PROJECTION, FIND_RAW_CONTACT_SELECTION, new String[]{Constants.PACKAGE_NAME, fingerprint}, null, null); @@ -108,42 +117,83 @@ public class ContactHelper { raw.close(); } ArrayList ops = new ArrayList(); - if (rawContactId == -1) { - ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) - .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, context.getString(R.string.app_name)) - .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, Constants.PACKAGE_NAME) - .withValue(ContactsContract.RawContacts.SOURCE_ID, fingerprint) - .build()); - if (userId[0] != null) { - ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) - .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) - .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) - .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, userId[0]) - .build()); + if (isExpired || isRevoked) { + if (rawContactId != -1) { + resolver.delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.RawContacts._ID + "=?", new String[]{Integer.toString(rawContactId)}); } - if (userId[1] != null) { - ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) - .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) - .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) - .withValue(ContactsContract.CommonDataKinds.Email.DATA, userId[1]) - .build()); + } else { + if (rawContactId == -1) { + insertContact(ops, context, fingerprint); + writeContactKey(ops, context, rawContactId, masterKeyId, keyIdShort); + } + writeContactDisplayName(ops, rawContactId, primaryUserId[0]); + writeContactEmail(ops, resolver, rawContactId, masterKeyId); + try { + resolver.applyBatch(ContactsContract.AUTHORITY, ops); + } catch (Exception e) { + Log.w(Constants.TAG, e); } - ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) - .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) - .withValue(ContactsContract.Data.MIMETYPE, Constants.CUSTOM_CONTACT_DATA_MIME_TYPE) - .withValue(ContactsContract.Data.DATA1, String.format(context.getString(R.string.contact_show_key), keyIdShort)) - .withValue(ContactsContract.Data.DATA2, masterKeyId) - .build()); - } - try { - resolver.applyBatch(ContactsContract.AUTHORITY, ops); - } catch (RemoteException e) { - e.printStackTrace(); - } catch (OperationApplicationException e) { - e.printStackTrace(); } } cursor.close(); } } + + private static void insertContact(ArrayList ops, Context context, String fingerprint) { + ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) + .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, context.getString(R.string.app_name)) + .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, Constants.PACKAGE_NAME) + .withValue(ContactsContract.RawContacts.SOURCE_ID, fingerprint) + .build()); + } + + private static void writeContactKey(ArrayList ops, Context context, int rawContactId, long masterKeyId, String keyIdShort) { + ops.add(referenceRawContact(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI), rawContactId) + .withValue(ContactsContract.Data.MIMETYPE, Constants.CUSTOM_CONTACT_DATA_MIME_TYPE) + .withValue(ContactsContract.Data.DATA1, context.getString(R.string.contact_show_key, keyIdShort)) + .withValue(ContactsContract.Data.DATA2, masterKeyId) + .build()); + } + + private static void writeContactEmail(ArrayList ops, ContentResolver resolver, int rawContactId, long masterKeyId) { + ops.add(selectByRawContactAndItemType(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI), + rawContactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE).build()); + Cursor ids = resolver.query(KeychainContract.UserIds.buildUserIdsUri(Long.toString(masterKeyId)), USER_IDS_PROJECTION, KeychainContract.UserIds.IS_REVOKED + "=0", null, null); + if (ids != null) { + while (ids.moveToNext()) { + String[] userId = KeyRing.splitUserId(ids.getString(0)); + if (userId[1] != null) { + ops.add(referenceRawContact(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI), rawContactId) + .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) + .withValue(ContactsContract.CommonDataKinds.Email.DATA, userId[1]) + .build()); + } + } + ids.close(); + } + } + + private static void writeContactDisplayName(ArrayList ops, int rawContactId, String displayName) { + if (displayName != null) { + ops.add(insertOrUpdateForRawContact(ContactsContract.Data.CONTENT_URI, rawContactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) + .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName) + .build()); + } + } + + private static ContentProviderOperation.Builder insertOrUpdateForRawContact(Uri uri, int rawContactId, String itemType) { + if (rawContactId == -1) { + return referenceRawContact(ContentProviderOperation.newInsert(uri), rawContactId).withValue(ContactsContract.Data.MIMETYPE, itemType); + } else { + return ContentProviderOperation.newUpdate(uri).withSelection( + ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", + new String[]{Integer.toString(rawContactId), itemType}); + } + } + + private static ContentProviderOperation.Builder selectByRawContactAndItemType(ContentProviderOperation.Builder builder, int rawContactId, String itemType) { + return builder.withSelection( + ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", + new String[]{Integer.toString(rawContactId), itemType}); + } } From 39a68c30f8d7059080bce06e86de9565a545ee96 Mon Sep 17 00:00:00 2001 From: mar-v-in Date: Wed, 18 Jun 2014 16:08:35 +0200 Subject: [PATCH 2/5] Code style --- .../keychain/helper/ContactHelper.java | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java index 8e4505004..66d579e92 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java @@ -79,7 +79,8 @@ public class ContactHelper { } public static Uri dataUriFromContactUri(Context context, Uri contactUri) { - Cursor contactMasterKey = context.getContentResolver().query(contactUri, new String[]{ContactsContract.Data.DATA2}, null, null, null, null); + Cursor contactMasterKey = context.getContentResolver().query(contactUri, + new String[]{ContactsContract.Data.DATA2}, null, null, null, null); if (contactMasterKey != null) { if (contactMasterKey.moveToNext()) { return KeychainContract.KeyRings.buildGenericKeyRingUri(contactMasterKey.getLong(0)); @@ -89,7 +90,8 @@ public class ContactHelper { return null; } - private static ContentProviderOperation.Builder referenceRawContact(ContentProviderOperation.Builder builder, int rawContactId) { + private static ContentProviderOperation.Builder referenceRawContact(ContentProviderOperation.Builder builder, + int rawContactId) { return rawContactId == -1 ? builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) : builder.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); @@ -119,7 +121,8 @@ public class ContactHelper { ArrayList ops = new ArrayList(); if (isExpired || isRevoked) { if (rawContactId != -1) { - resolver.delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.RawContacts._ID + "=?", new String[]{Integer.toString(rawContactId)}); + resolver.delete(ContactsContract.RawContacts.CONTENT_URI, + ContactsContract.RawContacts._ID + "=?", new String[]{Integer.toString(rawContactId)}); } } else { if (rawContactId == -1) { @@ -147,7 +150,8 @@ public class ContactHelper { .build()); } - private static void writeContactKey(ArrayList ops, Context context, int rawContactId, long masterKeyId, String keyIdShort) { + private static void writeContactKey(ArrayList ops, Context context, int rawContactId, + long masterKeyId, String keyIdShort) { ops.add(referenceRawContact(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI), rawContactId) .withValue(ContactsContract.Data.MIMETYPE, Constants.CUSTOM_CONTACT_DATA_MIME_TYPE) .withValue(ContactsContract.Data.DATA1, context.getString(R.string.contact_show_key, keyIdShort)) @@ -155,16 +159,20 @@ public class ContactHelper { .build()); } - private static void writeContactEmail(ArrayList ops, ContentResolver resolver, int rawContactId, long masterKeyId) { + private static void writeContactEmail(ArrayList ops, ContentResolver resolver, + int rawContactId, long masterKeyId) { ops.add(selectByRawContactAndItemType(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI), rawContactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE).build()); - Cursor ids = resolver.query(KeychainContract.UserIds.buildUserIdsUri(Long.toString(masterKeyId)), USER_IDS_PROJECTION, KeychainContract.UserIds.IS_REVOKED + "=0", null, null); + Cursor ids = resolver.query(KeychainContract.UserIds.buildUserIdsUri(Long.toString(masterKeyId)), + USER_IDS_PROJECTION, KeychainContract.UserIds.IS_REVOKED + "=0", null, null); if (ids != null) { while (ids.moveToNext()) { String[] userId = KeyRing.splitUserId(ids.getString(0)); if (userId[1] != null) { - ops.add(referenceRawContact(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI), rawContactId) - .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) + ops.add(referenceRawContact(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI), + rawContactId) + .withValue(ContactsContract.Data.MIMETYPE, + ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Email.DATA, userId[1]) .build()); } @@ -173,17 +181,21 @@ public class ContactHelper { } } - private static void writeContactDisplayName(ArrayList ops, int rawContactId, String displayName) { + private static void writeContactDisplayName(ArrayList ops, int rawContactId, + String displayName) { if (displayName != null) { - ops.add(insertOrUpdateForRawContact(ContactsContract.Data.CONTENT_URI, rawContactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) + ops.add(insertOrUpdateForRawContact(ContactsContract.Data.CONTENT_URI, rawContactId, + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName) .build()); } } - private static ContentProviderOperation.Builder insertOrUpdateForRawContact(Uri uri, int rawContactId, String itemType) { + private static ContentProviderOperation.Builder insertOrUpdateForRawContact(Uri uri, int rawContactId, + String itemType) { if (rawContactId == -1) { - return referenceRawContact(ContentProviderOperation.newInsert(uri), rawContactId).withValue(ContactsContract.Data.MIMETYPE, itemType); + return referenceRawContact(ContentProviderOperation.newInsert(uri), rawContactId).withValue( + ContactsContract.Data.MIMETYPE, itemType); } else { return ContentProviderOperation.newUpdate(uri).withSelection( ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", @@ -191,7 +203,8 @@ public class ContactHelper { } } - private static ContentProviderOperation.Builder selectByRawContactAndItemType(ContentProviderOperation.Builder builder, int rawContactId, String itemType) { + private static ContentProviderOperation.Builder selectByRawContactAndItemType( + ContentProviderOperation.Builder builder, int rawContactId, String itemType) { return builder.withSelection( ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{Integer.toString(rawContactId), itemType}); From 4bbaf6faa1b29bd66f662eb34208bef91260987b Mon Sep 17 00:00:00 2001 From: mar-v-in Date: Wed, 18 Jun 2014 16:47:33 +0200 Subject: [PATCH 3/5] Remove abandoned contacts --- .../keychain/helper/ContactHelper.java | 116 ++++++++++++++---- 1 file changed, 90 insertions(+), 26 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java index 66d579e92..d8a7e8427 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java @@ -45,9 +45,18 @@ public class ContactHelper { public static final String[] USER_IDS_PROJECTION = new String[]{ KeychainContract.UserIds.USER_ID }; - public static final String[] RAW_CONTACT_ID_PROJECTION = new String[]{ContactsContract.RawContacts._ID}; - public static final String FIND_RAW_CONTACT_SELECTION = + + public static final String NON_REVOKED_SELECTION = KeychainContract.UserIds.IS_REVOKED + "=0"; + + public static final String[] ID_PROJECTION = new String[]{ContactsContract.RawContacts._ID}; + public static final String[] SOURCE_ID_PROJECTION = new String[]{ContactsContract.RawContacts.SOURCE_ID}; + + public static final String ACCOUNT_TYPE_AND_SOURCE_ID_SELECTION = ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " + ContactsContract.RawContacts.SOURCE_ID + "=?"; + public static final String ACCOUNT_TYPE_SELECTION = ContactsContract.RawContacts.ACCOUNT_TYPE + "=?"; + public static final String RAW_CONTACT_AND_MIMETYPE_SELECTION = + ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?"; + public static final String ID_SELECTION = ContactsContract.RawContacts._ID + "=?"; public static List getMailAccounts(Context context) { final Account[] accounts = AccountManager.get(context).getAccounts(); @@ -90,45 +99,44 @@ public class ContactHelper { return null; } - private static ContentProviderOperation.Builder referenceRawContact(ContentProviderOperation.Builder builder, - int rawContactId) { - return rawContactId == -1 ? - builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) : - builder.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); - } - + /** + * Write the current Keychain to the contact db + */ public static void writeKeysToContacts(Context context) { ContentResolver resolver = context.getContentResolver(); + Set contactFingerprints = getRawContactFingerprints(resolver); + + // Load all Keys from OK Cursor cursor = resolver.query(KeychainContract.KeyRings.buildUnifiedKeyRingsUri(), KEYS_TO_CONTACT_PROJECTION, null, null, null); if (cursor != null) { while (cursor.moveToNext()) { String[] primaryUserId = KeyRing.splitUserId(cursor.getString(0)); String fingerprint = PgpKeyHelper.convertFingerprintToHex(cursor.getBlob(1)); + contactFingerprints.remove(fingerprint); String keyIdShort = PgpKeyHelper.convertKeyIdToHexShort(cursor.getLong(2)); long masterKeyId = cursor.getLong(3); boolean isExpired = !cursor.isNull(4) && new Date(cursor.getLong(4) * 1000).before(new Date()); boolean isRevoked = cursor.getInt(5) > 0; - int rawContactId = -1; - Cursor raw = resolver.query(ContactsContract.RawContacts.CONTENT_URI, RAW_CONTACT_ID_PROJECTION, - FIND_RAW_CONTACT_SELECTION, new String[]{Constants.PACKAGE_NAME, fingerprint}, null, null); - if (raw != null) { - if (raw.moveToNext()) { - rawContactId = raw.getInt(0); - } - raw.close(); - } + int rawContactId = findRawContactId(resolver, fingerprint); ArrayList ops = new ArrayList(); + + // Do not store expired or revoked keys in contact db - and remove them if they already exist if (isExpired || isRevoked) { if (rawContactId != -1) { - resolver.delete(ContactsContract.RawContacts.CONTENT_URI, - ContactsContract.RawContacts._ID + "=?", new String[]{Integer.toString(rawContactId)}); + resolver.delete(ContactsContract.RawContacts.CONTENT_URI, ID_SELECTION, + new String[]{Integer.toString(rawContactId)}); } } else { + + // Create a new rawcontact with corresponding key if it does not exist yet if (rawContactId == -1) { insertContact(ops, context, fingerprint); writeContactKey(ops, context, rawContactId, masterKeyId, keyIdShort); } + + // We always update the display name (which is derived from primary user id) + // and email addresses from user id writeContactDisplayName(ops, rawContactId, primaryUserId[0]); writeContactEmail(ops, resolver, rawContactId, masterKeyId); try { @@ -140,8 +148,52 @@ public class ContactHelper { } cursor.close(); } + + // Delete fingerprints that are no longer present in OK + for (String fingerprint : contactFingerprints) { + resolver.delete(ContactsContract.RawContacts.CONTENT_URI, ACCOUNT_TYPE_AND_SOURCE_ID_SELECTION, + new String[]{Constants.PACKAGE_NAME, fingerprint}); + } + } + /** + * @return a set of all key fingerprints currently present in the contact db + */ + private static Set getRawContactFingerprints(ContentResolver resolver) { + HashSet result = new HashSet(); + Cursor fingerprints = resolver.query(ContactsContract.RawContacts.CONTENT_URI, SOURCE_ID_PROJECTION, + ACCOUNT_TYPE_SELECTION, new String[]{Constants.PACKAGE_NAME}, null); + if (fingerprints != null) { + while (fingerprints.moveToNext()) { + result.add(fingerprints.getString(0)); + } + fingerprints.close(); + } + return result; + } + + /** + * This will search the contact db for a raw contact with a given fingerprint + * + * @return raw contact id or -1 if not found + */ + private static int findRawContactId(ContentResolver resolver, String fingerprint) { + int rawContactId = -1; + Cursor raw = resolver.query(ContactsContract.RawContacts.CONTENT_URI, ID_PROJECTION, + ACCOUNT_TYPE_AND_SOURCE_ID_SELECTION, new String[]{Constants.PACKAGE_NAME, fingerprint}, null, null); + if (raw != null) { + if (raw.moveToNext()) { + rawContactId = raw.getInt(0); + } + raw.close(); + } + return rawContactId; + } + + /** + * Creates a empty raw contact with a given fingerprint + */ private static void insertContact(ArrayList ops, Context context, String fingerprint) { ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, context.getString(R.string.app_name)) @@ -150,6 +202,11 @@ public class ContactHelper { .build()); } + /** + * Adds a key id to the given raw contact. + *

+ * This creates the link to OK in contact details + */ private static void writeContactKey(ArrayList ops, Context context, int rawContactId, long masterKeyId, String keyIdShort) { ops.add(referenceRawContact(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI), rawContactId) @@ -159,12 +216,15 @@ public class ContactHelper { .build()); } + /** + * Write all known email addresses of a key (derived from user ids) to a given raw contact + */ private static void writeContactEmail(ArrayList ops, ContentResolver resolver, int rawContactId, long masterKeyId) { ops.add(selectByRawContactAndItemType(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI), rawContactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE).build()); Cursor ids = resolver.query(KeychainContract.UserIds.buildUserIdsUri(Long.toString(masterKeyId)), - USER_IDS_PROJECTION, KeychainContract.UserIds.IS_REVOKED + "=0", null, null); + USER_IDS_PROJECTION, NON_REVOKED_SELECTION, null, null); if (ids != null) { while (ids.moveToNext()) { String[] userId = KeyRing.splitUserId(ids.getString(0)); @@ -191,22 +251,26 @@ public class ContactHelper { } } + private static ContentProviderOperation.Builder referenceRawContact(ContentProviderOperation.Builder builder, + int rawContactId) { + return rawContactId == -1 ? + builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) : + builder.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); + } + private static ContentProviderOperation.Builder insertOrUpdateForRawContact(Uri uri, int rawContactId, String itemType) { if (rawContactId == -1) { return referenceRawContact(ContentProviderOperation.newInsert(uri), rawContactId).withValue( ContactsContract.Data.MIMETYPE, itemType); } else { - return ContentProviderOperation.newUpdate(uri).withSelection( - ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", - new String[]{Integer.toString(rawContactId), itemType}); + return selectByRawContactAndItemType(ContentProviderOperation.newUpdate(uri), rawContactId, itemType); } } private static ContentProviderOperation.Builder selectByRawContactAndItemType( ContentProviderOperation.Builder builder, int rawContactId, String itemType) { - return builder.withSelection( - ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", + return builder.withSelection(RAW_CONTACT_AND_MIMETYPE_SELECTION, new String[]{Integer.toString(rawContactId), itemType}); } } From b9d88de286022f29561eff3e6826e3052eb9c9ea Mon Sep 17 00:00:00 2001 From: mar-v-in Date: Wed, 18 Jun 2014 17:01:36 +0200 Subject: [PATCH 4/5] Wait for import to be done before writing contacts --- .../service/ContactSyncAdapterService.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java index 8db9294df..b245c5e2a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java @@ -30,10 +30,14 @@ import org.sufficientlysecure.keychain.helper.ContactHelper; import org.sufficientlysecure.keychain.helper.EmailKeyHelper; import org.sufficientlysecure.keychain.util.Log; +import java.util.concurrent.atomic.AtomicBoolean; + public class ContactSyncAdapterService extends Service { private class ContactSyncAdapter extends AbstractThreadedSyncAdapter { + private final AtomicBoolean importDone = new AtomicBoolean(false); + public ContactSyncAdapter() { super(ContactSyncAdapterService.this, true); } @@ -41,6 +45,7 @@ public class ContactSyncAdapterService extends Service { @Override public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, final SyncResult syncResult) { + KeychainApplication.setupAccountAsNeeded(ContactSyncAdapterService.this); EmailKeyHelper.importContacts(getContext(), new Messenger(new Handler(Looper.getMainLooper(), new Handler.Callback() { @Override @@ -48,11 +53,16 @@ public class ContactSyncAdapterService extends Service { Bundle data = msg.getData(); switch (msg.arg1) { case KeychainIntentServiceHandler.MESSAGE_OKAY: + Log.d(Constants.TAG, "Syncing... Done."); + synchronized (importDone) { + importDone.set(true); + importDone.notifyAll(); + } return true; case KeychainIntentServiceHandler.MESSAGE_UPDATE_PROGRESS: if (data.containsKey(KeychainIntentServiceHandler.DATA_PROGRESS) && data.containsKey(KeychainIntentServiceHandler.DATA_PROGRESS_MAX)) { - Log.d(Constants.TAG, "Progress: " + + Log.d(Constants.TAG, "Syncing... Progress: " + data.getInt(KeychainIntentServiceHandler.DATA_PROGRESS) + "/" + data.getInt(KeychainIntentServiceHandler.DATA_PROGRESS_MAX)); return false; @@ -63,7 +73,14 @@ public class ContactSyncAdapterService extends Service { } } }))); - KeychainApplication.setupAccountAsNeeded(ContactSyncAdapterService.this); + synchronized (importDone) { + try { + if (!importDone.get()) importDone.wait(); + } catch (InterruptedException e) { + Log.w(Constants.TAG, e); + return; + } + } ContactHelper.writeKeysToContacts(ContactSyncAdapterService.this); } } From a1c3c41073d283dc951e50199d409e28ee4a2ee8 Mon Sep 17 00:00:00 2001 From: mar-v-in Date: Wed, 18 Jun 2014 17:06:46 +0200 Subject: [PATCH 5/5] Reset importDone at the beginning of sync --- .../keychain/service/ContactSyncAdapterService.java | 1 + 1 file changed, 1 insertion(+) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java index b245c5e2a..6c4d59a77 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java @@ -45,6 +45,7 @@ public class ContactSyncAdapterService extends Service { @Override public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, final SyncResult syncResult) { + importDone.set(false); KeychainApplication.setupAccountAsNeeded(ContactSyncAdapterService.this); EmailKeyHelper.importContacts(getContext(), new Messenger(new Handler(Looper.getMainLooper(), new Handler.Callback() {