From 17301be2cae4742f6ff508f984730a4d44e0a66a Mon Sep 17 00:00:00 2001 From: danielnelz Date: Mon, 9 Mar 2015 20:09:05 +0100 Subject: Created enum MessageStatus in KeychainIntentServiceHandler and enum IOType in KeychainIntentService and replaced int constants with them. Fixed some typos. --- .../main/java/org/sufficientlysecure/keychain/util/ExportHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java index 1d78ed80e..cda5892fe 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java @@ -124,7 +124,7 @@ public class ExportHelper { // handle messages by standard KeychainIntentServiceHandler first super.handleMessage(message); - if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { + if (message.arg1 == MessageStatus.OKAY.ordinal()) { // get returned data bundle Bundle data = message.getData(); -- cgit v1.2.3 From 45eea9b5b8d803996f5f89caf7e7cea0089af7f7 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Wed, 11 Mar 2015 23:10:32 +0530 Subject: corrected linked system contact display name --- .../keychain/util/ContactHelper.java | 40 ++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java index 1413273e8..b3eb36157 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java @@ -296,6 +296,34 @@ public class ContactHelper { return contactId; } + /** + * Returns the display name of the system contact associated with contactId, null if the + * contact does not exist + * + * @param resolver + * @param contactId + * @return primary display name of system contact associated with contactId, null if it does + * not exist + */ + public static String getContactName(ContentResolver resolver, long contactId) { + String contactName = null; + Cursor raw = resolver.query(ContactsContract.Contacts.CONTENT_URI, + new String[]{ + ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + }, + ContactsContract.Contacts._ID + "=?", + new String[]{//"0" for "not deleted" + Long.toString(contactId) + }, null); + if (raw != null) { + if (raw.moveToNext()) { + contactName = raw.getString(0); + } + raw.close(); + } + return contactName; + } + public static Bitmap getCachedPhotoByMasterKeyId(ContentResolver contentResolver, long masterKeyId) { if (masterKeyId == -1) { return null; @@ -333,12 +361,16 @@ public class ContactHelper { KeychainContract.KeyRings.MASTER_KEY_ID, KeychainContract.KeyRings.USER_ID, KeychainContract.KeyRings.IS_EXPIRED, - KeychainContract.KeyRings.IS_REVOKED}; + KeychainContract.KeyRings.IS_REVOKED, + KeychainContract.KeyRings.VERIFIED, + KeychainContract.KeyRings.HAS_SECRET}; public static final int INDEX_MASTER_KEY_ID = 0; public static final int INDEX_USER_ID = 1; public static final int INDEX_IS_EXPIRED = 2; public static final int INDEX_IS_REVOKED = 3; + public static final int INDEX_VERIFIED = 4; + public static final int INDEX_HAS_SECRET = 5; /** * Write/Update the current OpenKeychain keys to the contact db @@ -373,6 +405,8 @@ public class ContactHelper { String keyIdShort = KeyFormattingUtils.convertKeyIdToHexShort(cursor.getLong(INDEX_MASTER_KEY_ID)); boolean isExpired = cursor.getInt(INDEX_IS_EXPIRED) != 0; boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0; + boolean isVerified = cursor.getInt(INDEX_VERIFIED) > 0; + boolean isSecret = cursor.getInt(INDEX_HAS_SECRET) != 0; Log.d(Constants.TAG, "masterKeyId: " + masterKeyId); @@ -385,8 +419,8 @@ public class ContactHelper { ArrayList ops = new ArrayList<>(); // Do not store expired or revoked keys in contact db - and remove them if they already exist - if (isExpired || isRevoked) { - Log.d(Constants.TAG, "Expired or revoked: Deleting rawContactId " + rawContactId); + if (isExpired || isRevoked || !isVerified&&!isSecret) { + Log.d(Constants.TAG, "Expired or revoked or unverified: Deleting rawContactId " + rawContactId); if (rawContactId != -1) { deleteRawContactById(resolver, rawContactId); } -- cgit v1.2.3 From d4375b7dfc23213caca1b4eb2e86c54338e86605 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Fri, 13 Mar 2015 03:13:22 +0530 Subject: links own keys to me profile in contacts --- .../keychain/util/ContactHelper.java | 127 +++++++++++++++++++-- 1 file changed, 118 insertions(+), 9 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java index b3eb36157..1b52e4e6b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java @@ -363,7 +363,8 @@ public class ContactHelper { KeychainContract.KeyRings.IS_EXPIRED, KeychainContract.KeyRings.IS_REVOKED, KeychainContract.KeyRings.VERIFIED, - KeychainContract.KeyRings.HAS_SECRET}; + KeychainContract.KeyRings.HAS_SECRET, + KeychainContract.KeyRings.HAS_ANY_SECRET}; public static final int INDEX_MASTER_KEY_ID = 0; public static final int INDEX_USER_ID = 1; @@ -371,6 +372,7 @@ public class ContactHelper { public static final int INDEX_IS_REVOKED = 3; public static final int INDEX_VERIFIED = 4; public static final int INDEX_HAS_SECRET = 5; + public static final int INDEX_HAS_ANY_SECRET = 6; /** * Write/Update the current OpenKeychain keys to the contact db @@ -379,6 +381,8 @@ public class ContactHelper { ContentResolver resolver = context.getContentResolver(); Set deletedKeys = getRawContactMasterKeyIds(resolver); + writeKeysToMainProfileContact(context); + if (Constants.DEBUG_SYNC_REMOVE_CONTACTS) { debugDeleteRawContacts(resolver); } @@ -395,9 +399,13 @@ public class ContactHelper { // e.printStackTrace(); // } - // Load all Keys from OK - Cursor cursor = resolver.query(KeychainContract.KeyRings.buildUnifiedKeyRingsUri(), KEYS_TO_CONTACT_PROJECTION, - null, null, null); + // Load all public Keys from OK + // TODO: figure out why using selectionArgs does not work in this case + Cursor cursor = resolver.query(KeychainContract.KeyRings.buildUnifiedKeyRingsUri(), + KEYS_TO_CONTACT_PROJECTION, + KeychainContract.KeyRings.HAS_ANY_SECRET + "=0", + null, null); + if (cursor != null) { while (cursor.moveToNext()) { long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID); @@ -406,7 +414,6 @@ public class ContactHelper { boolean isExpired = cursor.getInt(INDEX_IS_EXPIRED) != 0; boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0; boolean isVerified = cursor.getInt(INDEX_VERIFIED) > 0; - boolean isSecret = cursor.getInt(INDEX_HAS_SECRET) != 0; Log.d(Constants.TAG, "masterKeyId: " + masterKeyId); @@ -418,8 +425,9 @@ public class ContactHelper { ArrayList ops = new ArrayList<>(); - // Do not store expired or revoked keys in contact db - and remove them if they already exist - if (isExpired || isRevoked || !isVerified&&!isSecret) { + // Do not store expired or revoked or unverified keys in contact db - and + // remove them if they already exist. Secret keys do not reach this point + if (isExpired || isRevoked || !isVerified) { Log.d(Constants.TAG, "Expired or revoked or unverified: Deleting rawContactId " + rawContactId); if (rawContactId != -1) { deleteRawContactById(resolver, rawContactId); @@ -455,6 +463,83 @@ public class ContactHelper { } } + public static void writeKeysToMainProfileContact(Context context) { + ContentResolver resolver = context.getContentResolver(); + Set keysToDelete = getMainProfileMasterKeyIds(resolver); + + // get all keys which have associated secret keys + // TODO: figure out why using selectionArgs does not work in this case + Cursor cursor = resolver.query(KeychainContract.KeyRings.buildUnifiedKeyRingsUri(), + KEYS_TO_CONTACT_PROJECTION, + KeychainContract.KeyRings.HAS_ANY_SECRET + "!=0", + null, null); + if (cursor != null) { + while (cursor.moveToNext()) { + long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID); + + boolean existsInMainProfile = keysToDelete.remove(masterKeyId); + if (!existsInMainProfile) { + long rawContactId = -1;//new raw contact + + String keyIdShort = KeyFormattingUtils.convertKeyIdToHexShort(masterKeyId); + Log.d(Constants.TAG, "masterKeyId with secret " + masterKeyId); + + ArrayList ops = new ArrayList<>(); + insertMainProfileRawContact(ops, masterKeyId); + writeContactKey(ops, context, rawContactId, masterKeyId, keyIdShort); + + try { + resolver.applyBatch(ContactsContract.AUTHORITY, ops); + } catch (Exception e) { + Log.w(Constants.TAG, e); + } + } + } + } + + for (long masterKeyId : keysToDelete) { + deleteMainProfileRawContactByMasterKeyId(resolver, masterKeyId); + Log.d(Constants.TAG, "Delete main profile raw contact with masterKeyId " + masterKeyId); + } + } + + /** + * Inserts a raw contact into the table defined by ContactsContract.Profile + * + * @param ops + * @param masterKeyId + */ + private static void insertMainProfileRawContact(ArrayList ops, + long masterKeyId) { + ops.add(ContentProviderOperation.newInsert(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI) + .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, Constants.ACCOUNT_NAME) + .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, Constants.ACCOUNT_TYPE) + .withValue(ContactsContract.RawContacts.SOURCE_ID, Long.toString(masterKeyId)) + .build()); + } + + /** + * deletes a raw contact from the profile table + * + * @param resolver + * @param masterKeyId + * @return + */ + private static int deleteMainProfileRawContactByMasterKeyId(ContentResolver resolver, + long masterKeyId) { + //allows us to actually wipe the RawContact from the device, otherwise would be just flagged + //for deletion + Uri deleteUri = ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI.buildUpon(). + appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(); + + return resolver.delete(deleteUri, + ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " + + ContactsContract.RawContacts.SOURCE_ID + "=?", + new String[]{ + Constants.ACCOUNT_TYPE, Long.toString(masterKeyId) + }); + } + /** * Delete all raw contacts associated to OpenKeychain. */ @@ -479,7 +564,8 @@ public class ContactHelper { appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(); return resolver.delete(deleteUri, - ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " + ContactsContract.RawContacts._ID + "=?", + ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " + + ContactsContract.RawContacts._ID + "=?", new String[]{ Constants.ACCOUNT_TYPE, Long.toString(rawContactId) }); @@ -492,7 +578,8 @@ public class ContactHelper { appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(); return resolver.delete(deleteUri, - ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " + ContactsContract.RawContacts.SOURCE_ID + "=?", + ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " + + ContactsContract.RawContacts.SOURCE_ID + "=?", new String[]{ Constants.ACCOUNT_TYPE, Long.toString(masterKeyId) }); @@ -520,6 +607,28 @@ public class ContactHelper { return result; } + /** + * @return a set of all key master key ids currently present in the contact db + */ + private static Set getMainProfileMasterKeyIds(ContentResolver resolver) { + HashSet result = new HashSet<>(); + Cursor masterKeyIds = resolver.query(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI, + new String[]{ + ContactsContract.RawContacts.SOURCE_ID + }, + ContactsContract.RawContacts.ACCOUNT_TYPE + "=?", + new String[]{ + Constants.ACCOUNT_TYPE + }, null); + if (masterKeyIds != null) { + while (masterKeyIds.moveToNext()) { + result.add(masterKeyIds.getLong(0)); + } + masterKeyIds.close(); + } + return result; + } + /** * This will search the contact db for a raw contact with a given master key id * -- cgit v1.2.3 From 759009ddb4ac11f4065ecfa14e17ed330bd01989 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Fri, 13 Mar 2015 04:04:11 +0530 Subject: added own profile support in linekd system contact --- .../keychain/util/ContactHelper.java | 39 ++++++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java index 1b52e4e6b..cb6c39188 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java @@ -190,7 +190,7 @@ public class ContactHelper { * @param context * @return */ - private static List getMainProfileContactName(Context context) { + public static List getMainProfileContactName(Context context) { ContentResolver resolver = context.getContentResolver(); Cursor profileCursor = resolver.query( ContactsContract.Profile.CONTENT_URI, @@ -214,6 +214,38 @@ public class ContactHelper { return new ArrayList<>(names); } + public static long getMainProfileContactId(ContentResolver resolver) { + Cursor profileCursor = resolver.query( + ContactsContract.Profile.CONTENT_URI, + new String[]{ + ContactsContract.Profile._ID + }, + null, null, null); + if (profileCursor == null) { + return -1; + } + + profileCursor.moveToNext(); + return profileCursor.getLong(0); + } + + public static Bitmap loadMainProfilePhoto(ContentResolver contentResolver, boolean highRes) { + try { + long mainProfileContactId = getMainProfileContactId(contentResolver); + + Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, + Long.toString(mainProfileContactId)); + InputStream photoInputStream = + ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, contactUri, highRes); + if (photoInputStream == null) { + return null; + } + return BitmapFactory.decodeStream(photoInputStream); + } catch (Throwable ignored) { + return null; + } + } + public static List getContactMails(Context context) { ContentResolver resolver = context.getContentResolver(); Cursor mailCursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, @@ -269,7 +301,7 @@ public class ContactHelper { /** * returns the CONTACT_ID of the raw contact to which a masterKeyId is associated, if the - * raw contact has not been marked for deletion + * raw contact has not been marked for deletion. * * @param resolver * @param masterKeyId @@ -428,7 +460,8 @@ public class ContactHelper { // Do not store expired or revoked or unverified keys in contact db - and // remove them if they already exist. Secret keys do not reach this point if (isExpired || isRevoked || !isVerified) { - Log.d(Constants.TAG, "Expired or revoked or unverified: Deleting rawContactId " + rawContactId); + Log.d(Constants.TAG, "Expired or revoked or unverified: Deleting rawContactId " + + rawContactId); if (rawContactId != -1) { deleteRawContactById(resolver, rawContactId); } -- cgit v1.2.3 From d7ef2c1b9ed9a98c33049c9dc8b43611cf5b99ce Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Fri, 13 Mar 2015 04:49:34 +0530 Subject: added some documentation --- .../keychain/util/ContactHelper.java | 105 ++++++++++++++++----- 1 file changed, 82 insertions(+), 23 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java index cb6c39188..c07d7a36b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java @@ -214,6 +214,13 @@ public class ContactHelper { return new ArrayList<>(names); } + /** + * returns the CONTACT_ID of the main ("me") contact + * http://developer.android.com/reference/android/provider/ContactsContract.Profile.html + * + * @param resolver + * @return + */ public static long getMainProfileContactId(ContentResolver resolver) { Cursor profileCursor = resolver.query( ContactsContract.Profile.CONTENT_URI, @@ -229,6 +236,14 @@ public class ContactHelper { return profileCursor.getLong(0); } + /** + * loads the profile picture of the main ("me") contact + * http://developer.android.com/reference/android/provider/ContactsContract.Profile.html + * + * @param contentResolver + * @param highRes true for large image if present, false for thumbnail + * @return bitmap of loaded photo + */ public static Bitmap loadMainProfilePhoto(ContentResolver contentResolver, boolean highRes) { try { long mainProfileContactId = getMainProfileContactId(contentResolver); @@ -496,6 +511,12 @@ public class ContactHelper { } } + /** + * Links all keys with secrets to the main ("me") contact + * http://developer.android.com/reference/android/provider/ContactsContract.Profile.html + * + * @param context + */ public static void writeKeysToMainProfileContact(Context context) { ContentResolver resolver = context.getContentResolver(); Set keysToDelete = getMainProfileMasterKeyIds(resolver); @@ -509,22 +530,28 @@ public class ContactHelper { if (cursor != null) { while (cursor.moveToNext()) { long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID); + boolean isExpired = cursor.getInt(INDEX_IS_EXPIRED) != 0; + boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0; - boolean existsInMainProfile = keysToDelete.remove(masterKeyId); - if (!existsInMainProfile) { - long rawContactId = -1;//new raw contact + if (!isExpired && !isRevoked) { + // if expired or revoked will not be removed from keysToDelete or inserted + // into main profile ("me" contact) + boolean existsInMainProfile = keysToDelete.remove(masterKeyId); + if (!existsInMainProfile) { + long rawContactId = -1;//new raw contact - String keyIdShort = KeyFormattingUtils.convertKeyIdToHexShort(masterKeyId); - Log.d(Constants.TAG, "masterKeyId with secret " + masterKeyId); + String keyIdShort = KeyFormattingUtils.convertKeyIdToHexShort(masterKeyId); + Log.d(Constants.TAG, "masterKeyId with secret " + masterKeyId); - ArrayList ops = new ArrayList<>(); - insertMainProfileRawContact(ops, masterKeyId); - writeContactKey(ops, context, rawContactId, masterKeyId, keyIdShort); + ArrayList ops = new ArrayList<>(); + insertMainProfileRawContact(ops, masterKeyId); + writeContactKey(ops, context, rawContactId, masterKeyId, keyIdShort); - try { - resolver.applyBatch(ContactsContract.AUTHORITY, ops); - } catch (Exception e) { - Log.w(Constants.TAG, e); + try { + resolver.applyBatch(ContactsContract.AUTHORITY, ops); + } catch (Exception e) { + Log.w(Constants.TAG, e); + } } } } @@ -538,6 +565,7 @@ public class ContactHelper { /** * Inserts a raw contact into the table defined by ContactsContract.Profile + * http://developer.android.com/reference/android/provider/ContactsContract.Profile.html * * @param ops * @param masterKeyId @@ -552,7 +580,8 @@ public class ContactHelper { } /** - * deletes a raw contact from the profile table + * deletes a raw contact from the main profile table ("me" contact) + * http://developer.android.com/reference/android/provider/ContactsContract.Profile.html * * @param resolver * @param masterKeyId @@ -560,8 +589,8 @@ public class ContactHelper { */ private static int deleteMainProfileRawContactByMasterKeyId(ContentResolver resolver, long masterKeyId) { - //allows us to actually wipe the RawContact from the device, otherwise would be just flagged - //for deletion + // CALLER_IS_SYNCADAPTER allows us to actually wipe the RawContact from the device, otherwise + // would be just flagged for deletion Uri deleteUri = ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI.buildUpon(). appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(); @@ -574,25 +603,47 @@ public class ContactHelper { } /** - * Delete all raw contacts associated to OpenKeychain. + * Delete all raw contacts associated to OpenKeychain, including those from "me" contact + * defined by ContactsContract.Profile + * + * @return number of rows deleted */ private static int debugDeleteRawContacts(ContentResolver resolver) { - //allows us to actually wipe the RawContact from the device, otherwise would be just flagged - //for deletion + // CALLER_IS_SYNCADAPTER allows us to actually wipe the RawContact from the device, otherwise + // would be just flagged for deletion Uri deleteUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon(). appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(); Log.d(Constants.TAG, "Deleting all raw contacts associated to OK..."); - return resolver.delete(deleteUri, + int delete = resolver.delete(deleteUri, + ContactsContract.RawContacts.ACCOUNT_TYPE + "=?", + new String[]{ + Constants.ACCOUNT_TYPE + }); + + Uri mainProfileDeleteUri = ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI.buildUpon() + .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(); + + delete += resolver.delete(mainProfileDeleteUri, ContactsContract.RawContacts.ACCOUNT_TYPE + "=?", new String[]{ Constants.ACCOUNT_TYPE }); + + return delete; } + /** + * Deletes raw contacts from ContactsContract.RawContacts based on rawContactId. Does not + * delete contacts from the "me" contact defined in ContactsContract.Profile + * + * @param resolver + * @param rawContactId + * @return number of rows deleted + */ private static int deleteRawContactById(ContentResolver resolver, long rawContactId) { - //allows us to actually wipe the RawContact from the device, otherwise would be just flagged - //for deletion + // CALLER_IS_SYNCADAPTER allows us to actually wipe the RawContact from the device, otherwise + // would be just flagged for deletion Uri deleteUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon(). appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(); @@ -604,9 +655,17 @@ public class ContactHelper { }); } + /** + * Deletes raw contacts from ContactsContract.RawContacts based on masterKeyId. Does not + * delete contacts from the "me" contact defined in ContactsContract.Profile + * + * @param resolver + * @param masterKeyId + * @return number of rows deleted + */ private static int deleteRawContactByMasterKeyId(ContentResolver resolver, long masterKeyId) { - //allows us to actually wipe the RawContact from the device, otherwise would be just flagged - //for deletion + // CALLER_IS_SYNCADAPTER allows us to actually wipe the RawContact from the device, otherwise + // would be just flagged for deletion Uri deleteUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon(). appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(); -- cgit v1.2.3 From cd7f63122193cd9b3c62169381b4d8cef4a88bd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 14 Mar 2015 14:05:31 +0100 Subject: Show key name instead of key id in contacts, cleanup --- .../keychain/util/ContactHelper.java | 27 ++++++---------------- 1 file changed, 7 insertions(+), 20 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java index c07d7a36b..d5078da3c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java @@ -426,25 +426,14 @@ public class ContactHelper { */ public static void writeKeysToContacts(Context context) { ContentResolver resolver = context.getContentResolver(); - Set deletedKeys = getRawContactMasterKeyIds(resolver); - - writeKeysToMainProfileContact(context); if (Constants.DEBUG_SYNC_REMOVE_CONTACTS) { debugDeleteRawContacts(resolver); } -// ContentProviderClient client = resolver.acquireContentProviderClient(ContactsContract.AUTHORITY_URI); -// ContentValues values = new ContentValues(); -// Account account = new Account(Constants.ACCOUNT_NAME, Constants.ACCOUNT_TYPE); -// values.put(ContactsContract.Settings.ACCOUNT_NAME, account.name); -// values.put(ContactsContract.Settings.ACCOUNT_TYPE, account.type); -// values.put(ContactsContract.Settings.UNGROUPED_VISIBLE, true); -// try { -// client.insert(ContactsContract.Settings.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(), values); -// } catch (RemoteException e) { -// e.printStackTrace(); -// } + writeKeysToMainProfileContact(context, resolver); + + Set deletedKeys = getRawContactMasterKeyIds(resolver); // Load all public Keys from OK // TODO: figure out why using selectionArgs does not work in this case @@ -457,7 +446,6 @@ public class ContactHelper { while (cursor.moveToNext()) { long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID); String[] userIdSplit = KeyRing.splitUserId(cursor.getString(INDEX_USER_ID)); - String keyIdShort = KeyFormattingUtils.convertKeyIdToHexShort(cursor.getLong(INDEX_MASTER_KEY_ID)); boolean isExpired = cursor.getInt(INDEX_IS_EXPIRED) != 0; boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0; boolean isVerified = cursor.getInt(INDEX_VERIFIED) > 0; @@ -487,7 +475,7 @@ public class ContactHelper { Log.d(Constants.TAG, "Insert new raw contact with masterKeyId " + masterKeyId); insertContact(ops, context, masterKeyId); - writeContactKey(ops, context, rawContactId, masterKeyId, keyIdShort); + writeContactKey(ops, context, rawContactId, masterKeyId, userIdSplit[0]); } // We always update the display name (which is derived from primary user id) @@ -517,8 +505,7 @@ public class ContactHelper { * * @param context */ - public static void writeKeysToMainProfileContact(Context context) { - ContentResolver resolver = context.getContentResolver(); + public static void writeKeysToMainProfileContact(Context context, ContentResolver resolver) { Set keysToDelete = getMainProfileMasterKeyIds(resolver); // get all keys which have associated secret keys @@ -763,10 +750,10 @@ public class ContactHelper { * This creates the link to OK in contact details */ private static void writeContactKey(ArrayList ops, Context context, long rawContactId, - long masterKeyId, String keyIdShort) { + long masterKeyId, String keyName) { 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.DATA1, context.getString(R.string.contact_show_key, keyName)) .withValue(ContactsContract.Data.DATA2, masterKeyId) .build()); } -- cgit v1.2.3 From 12734127a74a7e2a6afea4f7a0d5fda5ee01dadf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 14 Mar 2015 14:07:45 +0100 Subject: Use names also for main contact --- .../java/org/sufficientlysecure/keychain/util/ContactHelper.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java index d5078da3c..d102745b6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java @@ -519,20 +519,20 @@ public class ContactHelper { long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID); boolean isExpired = cursor.getInt(INDEX_IS_EXPIRED) != 0; boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0; + String[] userIdSplit = KeyRing.splitUserId(cursor.getString(INDEX_USER_ID)); - if (!isExpired && !isRevoked) { + if (!isExpired && !isRevoked && userIdSplit[0] != null) { // if expired or revoked will not be removed from keysToDelete or inserted // into main profile ("me" contact) boolean existsInMainProfile = keysToDelete.remove(masterKeyId); if (!existsInMainProfile) { long rawContactId = -1;//new raw contact - String keyIdShort = KeyFormattingUtils.convertKeyIdToHexShort(masterKeyId); Log.d(Constants.TAG, "masterKeyId with secret " + masterKeyId); ArrayList ops = new ArrayList<>(); insertMainProfileRawContact(ops, masterKeyId); - writeContactKey(ops, context, rawContactId, masterKeyId, keyIdShort); + writeContactKey(ops, context, rawContactId, masterKeyId, userIdSplit[0]); try { resolver.applyBatch(ContactsContract.AUTHORITY, ops); -- cgit v1.2.3 From d3dd9020f585b8f587d9f2deb377750dbd260db1 Mon Sep 17 00:00:00 2001 From: Daniel Ramos Date: Sun, 15 Mar 2015 00:12:26 +0000 Subject: -fixed out of bounds crash when retrieving the main profile name with secret keys -fixed a possible crash when retrieving the main profile contactid --- .../keychain/util/ContactHelper.java | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java index c07d7a36b..3fa6ea666 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java @@ -222,18 +222,20 @@ public class ContactHelper { * @return */ public static long getMainProfileContactId(ContentResolver resolver) { - Cursor profileCursor = resolver.query( - ContactsContract.Profile.CONTENT_URI, - new String[]{ - ContactsContract.Profile._ID - }, - null, null, null); - if (profileCursor == null) { + Cursor profileCursor = resolver.query(ContactsContract.Profile.CONTENT_URI, + new String[]{ ContactsContract.Profile._ID}, null, null, null); + + if(profileCursor != null && profileCursor.getCount() != 0 && profileCursor.moveToNext()) { + long contactId = profileCursor.getLong(0); + profileCursor.close(); + return contactId; + } + else { + if(profileCursor != null) { + profileCursor.close(); + } return -1; } - - profileCursor.moveToNext(); - return profileCursor.getLong(0); } /** -- cgit v1.2.3