Make sure the number we add canonicalizes to the number we know.

Fixes #4406
// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-11-06 10:50:47 -08:00
parent 0b7b37bf00
commit b5b564cfe1
3 changed files with 28 additions and 18 deletions

View File

@ -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> systemContactInfo = getSystemContactInfo(number);
Optional<SystemContactInfo> 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<SystemContactInfo> getSystemContactInfo(String e164number) {
private Optional<SystemContactInfo> 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,7 +288,12 @@ public class ContactsDatabase {
try {
numberCursor = context.getContentResolver().query(uri, projection, null, null, null);
if (numberCursor != null && numberCursor.moveToNext()) {
while (numberCursor != null && numberCursor.moveToNext()) {
try {
String systemNumber = numberCursor.getString(0);
String canonicalizedSystemNumber = PhoneNumberFormatter.formatNumber(systemNumber, localNumber);
if (canonicalizedSystemNumber.equals(e164number)) {
idCursor = context.getContentResolver().query(RawContacts.CONTENT_URI,
new String[] {RawContacts._ID},
RawContacts.CONTACT_ID + " = ? ",
@ -304,6 +306,10 @@ public class ContactsDatabase {
idCursor.getLong(0)));
}
}
} catch (InvalidNumberException e) {
Log.w(TAG, e);
}
}
} finally {
if (numberCursor != null) numberCursor.close();
if (idCursor != null) idCursor.close();

View File

@ -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) {

View File

@ -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");