From b5b564cfe132ed28c008e6a320bf5d66290a7f58 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Fri, 6 Nov 2015 10:50:47 -0800 Subject: [PATCH] Make sure the number we add canonicalizes to the number we know. Fixes #4406 // FREEBIE --- .../securesms/contacts/ContactsDatabase.java | 42 +++++++++++-------- .../contacts/ContactsSyncAdapter.java | 2 + .../securesms/jobs/DirectoryRefreshJob.java | 2 + 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/org/thoughtcrime/securesms/contacts/ContactsDatabase.java b/src/org/thoughtcrime/securesms/contacts/ContactsDatabase.java index 353a0d5e83..47d7f490bd 100644 --- a/src/org/thoughtcrime/securesms/contacts/ContactsDatabase.java +++ b/src/org/thoughtcrime/securesms/contacts/ContactsDatabase.java @@ -23,13 +23,11 @@ import android.content.OperationApplicationException; import android.database.Cursor; import android.database.CursorWrapper; import android.database.MatrixCursor; -import android.database.MergeCursor; import android.net.Uri; import android.os.Build; import android.os.RemoteException; import android.provider.BaseColumns; import android.provider.ContactsContract; -import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.RawContacts; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -38,10 +36,6 @@ import android.util.Log; import android.util.Pair; import org.thoughtcrime.securesms.R; -import org.thoughtcrime.securesms.database.TextSecureDirectory; -import org.thoughtcrime.securesms.recipients.RecipientFactory; -import org.thoughtcrime.securesms.util.DirectoryHelper; -import org.thoughtcrime.securesms.util.DirectoryHelper.UserCapabilities.Capability; import org.whispersystems.libaxolotl.util.guava.Optional; import org.whispersystems.textsecure.api.util.InvalidNumberException; import org.whispersystems.textsecure.api.util.PhoneNumberFormatter; @@ -119,9 +113,10 @@ public class ContactsDatabase { for (String number : e164numbers) { if (!currentContacts.containsKey(number)) { - Optional systemContactInfo = getSystemContactInfo(number); + Optional systemContactInfo = getSystemContactInfo(number, localNumber); if (systemContactInfo.isPresent()) { + Log.w(TAG, "Adding number: " + number); addedNumbers.add(number); addTextSecureRawContact(operations, account, systemContactInfo.get().number, systemContactInfo.get().id); } @@ -280,7 +275,9 @@ public class ContactsDatabase { return newNumberCursor; } - private Optional getSystemContactInfo(String e164number) { + private Optional getSystemContactInfo(@NonNull String e164number, + @NonNull String localNumber) + { Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(e164number)); String[] projection = {ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup._ID, @@ -291,17 +288,26 @@ public class ContactsDatabase { try { numberCursor = context.getContentResolver().query(uri, projection, null, null, null); - if (numberCursor != null && numberCursor.moveToNext()) { - idCursor = context.getContentResolver().query(RawContacts.CONTENT_URI, - new String[] {RawContacts._ID}, - RawContacts.CONTACT_ID + " = ? ", - new String[] {String.valueOf(numberCursor.getLong(1))}, - null); + while (numberCursor != null && numberCursor.moveToNext()) { + try { + String systemNumber = numberCursor.getString(0); + String canonicalizedSystemNumber = PhoneNumberFormatter.formatNumber(systemNumber, localNumber); - if (idCursor != null && idCursor.moveToNext()) { - return Optional.of(new SystemContactInfo(numberCursor.getString(2), - numberCursor.getString(0), - idCursor.getLong(0))); + if (canonicalizedSystemNumber.equals(e164number)) { + idCursor = context.getContentResolver().query(RawContacts.CONTENT_URI, + new String[] {RawContacts._ID}, + RawContacts.CONTACT_ID + " = ? ", + new String[] {String.valueOf(numberCursor.getLong(1))}, + null); + + if (idCursor != null && idCursor.moveToNext()) { + return Optional.of(new SystemContactInfo(numberCursor.getString(2), + numberCursor.getString(0), + idCursor.getLong(0))); + } + } + } catch (InvalidNumberException e) { + Log.w(TAG, e); } } } finally { diff --git a/src/org/thoughtcrime/securesms/contacts/ContactsSyncAdapter.java b/src/org/thoughtcrime/securesms/contacts/ContactsSyncAdapter.java index 383248b85c..cb7c20046b 100644 --- a/src/org/thoughtcrime/securesms/contacts/ContactsSyncAdapter.java +++ b/src/org/thoughtcrime/securesms/contacts/ContactsSyncAdapter.java @@ -25,6 +25,8 @@ public class ContactsSyncAdapter extends AbstractThreadedSyncAdapter { public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { + Log.w(TAG, "onPerformSync(" + authority +")"); + try { DirectoryHelper.refreshDirectory(getContext(), KeyCachingService.getMasterSecret(getContext())); } catch (IOException e) { diff --git a/src/org/thoughtcrime/securesms/jobs/DirectoryRefreshJob.java b/src/org/thoughtcrime/securesms/jobs/DirectoryRefreshJob.java index dbadaf0a9c..7e14f84626 100644 --- a/src/org/thoughtcrime/securesms/jobs/DirectoryRefreshJob.java +++ b/src/org/thoughtcrime/securesms/jobs/DirectoryRefreshJob.java @@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.jobs; import android.content.Context; import android.os.PowerManager; +import android.util.Log; import org.thoughtcrime.securesms.crypto.SecurityEvent; import org.thoughtcrime.securesms.service.KeyCachingService; @@ -26,6 +27,7 @@ public class DirectoryRefreshJob extends ContextJob { @Override public void onRun() throws IOException { + Log.w("DirectoryRefreshJob", "DirectoryRefreshJob.onRun()"); PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Directory Refresh");