From 363d41ed1d4910fa5b8651d3bd95d1c61bc0352c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Tue, 3 Mar 2015 00:26:15 +0100 Subject: Cleanup of contact sync, debugging --- .../keychain/KeychainApplication.java | 30 ++-- .../keychain/util/ContactHelper.java | 190 ++++++++++++--------- 2 files changed, 126 insertions(+), 94 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java index 01f6735ea..a4dc12a37 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java @@ -140,20 +140,22 @@ public class KeychainApplication extends Application { } static void brandGlowEffect(Context context, int brandColor) { - try { - // terrible hack to brand the edge overscroll glow effect - // https://gist.github.com/menny/7878762#file-brandgloweffect_full-java - - //glow - int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android"); - Drawable androidGlow = context.getResources().getDrawable(glowDrawableId); - androidGlow.setColorFilter(brandColor, PorterDuff.Mode.SRC_IN); - //edge - int edgeDrawableId = context.getResources().getIdentifier("overscroll_edge", "drawable", "android"); - Drawable androidEdge = context.getResources().getDrawable(edgeDrawableId); - androidEdge.setColorFilter(brandColor, PorterDuff.Mode.SRC_IN); - } catch (Resources.NotFoundException e) { - // no hack on Android 5 + // no hack on Android 5 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + try { + // terrible hack to brand the edge overscroll glow effect + // https://gist.github.com/menny/7878762#file-brandgloweffect_full-java + + //glow + int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android"); + Drawable androidGlow = context.getResources().getDrawable(glowDrawableId); + androidGlow.setColorFilter(brandColor, PorterDuff.Mode.SRC_IN); + //edge + int edgeDrawableId = context.getResources().getIdentifier("overscroll_edge", "drawable", "android"); + Drawable androidEdge = context.getResources().getDrawable(edgeDrawableId); + androidEdge.setColorFilter(brandColor, PorterDuff.Mode.SRC_IN); + } catch (Resources.NotFoundException e) { + } } } } 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 3cf201ed7..28480cee5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ContactHelper.java @@ -20,15 +20,18 @@ package org.sufficientlysecure.keychain.util; import android.accounts.Account; import android.accounts.AccountManager; import android.annotation.TargetApi; +import android.content.ContentProviderClient; import android.content.ContentProviderOperation; import android.content.ContentResolver; import android.content.ContentUris; +import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; +import android.os.RemoteException; import android.provider.ContactsContract; import android.util.Patterns; @@ -125,33 +128,33 @@ public class ContactHelper { * @return */ private static Set getContactNamesFromEmails(Context context, Set emails) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - Set names = new HashSet<>(); - for (String email : emails) { - ContentResolver resolver = context.getContentResolver(); - Cursor profileCursor = resolver.query( - ContactsContract.CommonDataKinds.Email.CONTENT_URI, - new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS, - ContactsContract.Contacts.DISPLAY_NAME}, - ContactsContract.CommonDataKinds.Email.ADDRESS + "=?", - new String[]{email}, null - ); - if (profileCursor == null) return null; - - Set currNames = new HashSet<>(); - while (profileCursor.moveToNext()) { - String name = profileCursor.getString(1); - if (name != null) { - currNames.add(name); - } + Set names = new HashSet<>(); + for (String email : emails) { + ContentResolver resolver = context.getContentResolver(); + Cursor profileCursor = resolver.query( + ContactsContract.CommonDataKinds.Email.CONTENT_URI, + new String[]{ + ContactsContract.CommonDataKinds.Email.ADDRESS, + ContactsContract.Contacts.DISPLAY_NAME + }, + ContactsContract.CommonDataKinds.Email.ADDRESS + "=?", + new String[]{email}, null + ); + if (profileCursor == null) { + return null; + } + + Set currNames = new HashSet<>(); + while (profileCursor.moveToNext()) { + String name = profileCursor.getString(1); + if (name != null) { + currNames.add(name); } - profileCursor.close(); - names.addAll(currNames); } - return names; - } else { - return new HashSet<>(); + profileCursor.close(); + names.addAll(currNames); } + return names; } /** @@ -162,38 +165,37 @@ public class ContactHelper { * @return */ private static Set getMainProfileContactEmails(Context context) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - ContentResolver resolver = context.getContentResolver(); - Cursor profileCursor = resolver.query( - Uri.withAppendedPath( - ContactsContract.Profile.CONTENT_URI, - ContactsContract.Contacts.Data.CONTENT_DIRECTORY), - new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS, - ContactsContract.CommonDataKinds.Email.IS_PRIMARY}, - - // Selects only email addresses - ContactsContract.Contacts.Data.MIMETYPE + "=?", - new String[]{ - ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, - }, - // Show primary rows first. Note that there won't be a primary email address if the - // user hasn't specified one. - ContactsContract.Contacts.Data.IS_PRIMARY + " DESC" - ); - if (profileCursor == null) return null; + ContentResolver resolver = context.getContentResolver(); + Cursor profileCursor = resolver.query( + Uri.withAppendedPath( + ContactsContract.Profile.CONTENT_URI, + ContactsContract.Contacts.Data.CONTENT_DIRECTORY), + new String[]{ + ContactsContract.CommonDataKinds.Email.ADDRESS, + ContactsContract.CommonDataKinds.Email.IS_PRIMARY + }, + // Selects only email addresses + ContactsContract.Contacts.Data.MIMETYPE + "=?", + new String[]{ + ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, + }, + // Show primary rows first. Note that there won't be a primary email address if the + // user hasn't specified one. + ContactsContract.Contacts.Data.IS_PRIMARY + " DESC" + ); + if (profileCursor == null) { + return null; + } - Set emails = new HashSet<>(); - while (profileCursor.moveToNext()) { - String email = profileCursor.getString(0); - if (email != null) { - emails.add(email); - } + Set emails = new HashSet<>(); + while (profileCursor.moveToNext()) { + String email = profileCursor.getString(0); + if (email != null) { + emails.add(email); } - profileCursor.close(); - return emails; - } else { - return new HashSet<>(); } + profileCursor.close(); + return emails; } /** @@ -204,26 +206,27 @@ public class ContactHelper { * @return */ private static List getMainProfileContactName(Context context) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - ContentResolver resolver = context.getContentResolver(); - Cursor profileCursor = resolver.query(ContactsContract.Profile.CONTENT_URI, - new String[]{ContactsContract.Profile.DISPLAY_NAME}, - null, null, null); - if (profileCursor == null) return null; + ContentResolver resolver = context.getContentResolver(); + Cursor profileCursor = resolver.query( + ContactsContract.Profile.CONTENT_URI, + new String[]{ + ContactsContract.Profile.DISPLAY_NAME + }, + null, null, null); + if (profileCursor == null) { + return null; + } - Set names = new HashSet<>(); - // should only contain one entry! - while (profileCursor.moveToNext()) { - String name = profileCursor.getString(0); - if (name != null) { - names.add(name); - } + Set names = new HashSet<>(); + // should only contain one entry! + while (profileCursor.moveToNext()) { + String name = profileCursor.getString(0); + if (name != null) { + names.add(name); } - profileCursor.close(); - return new ArrayList<>(names); - } else { - return new ArrayList<>(); } + profileCursor.close(); + return new ArrayList<>(names); } public static List getContactMails(Context context) { @@ -231,7 +234,9 @@ public class ContactHelper { Cursor mailCursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Email.DATA}, null, null, null); - if (mailCursor == null) return new ArrayList<>(); + if (mailCursor == null) { + return new ArrayList<>(); + } Set mails = new HashSet<>(); while (mailCursor.moveToNext()) { @@ -249,7 +254,9 @@ public class ContactHelper { Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, new String[]{ContactsContract.Contacts.DISPLAY_NAME}, null, null, null); - if (cursor == null) return new ArrayList<>(); + if (cursor == null) { + return new ArrayList<>(); + } Set names = new HashSet<>(); while (cursor.moveToNext()) { @@ -276,7 +283,9 @@ public class ContactHelper { } public static Bitmap photoFromFingerprint(ContentResolver contentResolver, String fingerprint) { - if (fingerprint == null) return null; + if (fingerprint == null) { + return null; + } if (!photoCache.containsKey(fingerprint)) { photoCache.put(fingerprint, loadPhotoFromFingerprint(contentResolver, fingerprint)); } @@ -287,12 +296,16 @@ public class ContactHelper { if (fingerprint == null) return null; try { int rawContactId = findRawContactId(contentResolver, fingerprint); - if (rawContactId == -1) return null; + if (rawContactId == -1) { + return null; + } Uri rawContactUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, rawContactId); Uri contactUri = ContactsContract.RawContacts.getContactLookupUri(contentResolver, rawContactUri); InputStream photoInputStream = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, contactUri); - if (photoInputStream == null) return null; + if (photoInputStream == null) { + return null; + } return BitmapFactory.decodeStream(photoInputStream); } catch (Throwable ignored) { return null; @@ -304,7 +317,19 @@ public class ContactHelper { */ public static void writeKeysToContacts(Context context) { ContentResolver resolver = context.getContentResolver(); - Set contactFingerprints = getRawContactFingerprints(resolver); + Set deletedKeys = getRawContactFingerprints(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(); +// } // Load all Keys from OK Cursor cursor = resolver.query(KeychainContract.KeyRings.buildUnifiedKeyRingsUri(), KEYS_TO_CONTACT_PROJECTION, @@ -313,7 +338,10 @@ public class ContactHelper { while (cursor.moveToNext()) { String[] primaryUserId = KeyRing.splitUserId(cursor.getString(INDEX_USER_ID)); String fingerprint = KeyFormattingUtils.convertFingerprintToHex(cursor.getBlob(INDEX_FINGERPRINT)); - contactFingerprints.remove(fingerprint); + deletedKeys.remove(fingerprint); + + Log.d(Constants.TAG, "fingerprint: " + fingerprint); + String keyIdShort = KeyFormattingUtils.convertKeyIdToHexShort(cursor.getLong(INDEX_KEY_ID)); long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID); boolean isExpired = !cursor.isNull(INDEX_EXPIRY) @@ -322,10 +350,11 @@ public class ContactHelper { int rawContactId = findRawContactId(resolver, fingerprint); ArrayList ops = new ArrayList<>(); - Log.d(Constants.TAG, "raw contact id: "+rawContactId); + Log.d(Constants.TAG, "raw contact id: " + rawContactId); // 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); if (rawContactId != -1) { resolver.delete(ContactsContract.RawContacts.CONTENT_URI, ID_SELECTION, new String[]{Integer.toString(rawContactId)}); @@ -334,6 +363,8 @@ public class ContactHelper { // Create a new rawcontact with corresponding key if it does not exist yet if (rawContactId == -1) { + Log.d(Constants.TAG, "Insert new raw contact with fingerprint " + fingerprint); + insertContact(ops, context, fingerprint); writeContactKey(ops, context, rawContactId, masterKeyId, keyIdShort); } @@ -353,11 +384,10 @@ public class ContactHelper { } // Delete fingerprints that are no longer present in OK - for (String fingerprint : contactFingerprints) { + for (String fingerprint : deletedKeys) { resolver.delete(ContactsContract.RawContacts.CONTENT_URI, ACCOUNT_TYPE_AND_SOURCE_ID_SELECTION, new String[]{Constants.ACCOUNT_TYPE, fingerprint}); } - } /** -- cgit v1.2.3