Canonicalize entries in canonical address database.

Make all our queries E164, so eventually everything in there
will be E164.  Stops thrashing between formats.

Closes #4306

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-10-26 11:03:08 -07:00
parent 8fd0ea39aa
commit 434ce4f9c9
2 changed files with 58 additions and 43 deletions

View File

@ -28,7 +28,10 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import org.thoughtcrime.securesms.util.GroupUtil; import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.VisibleForTesting; import org.thoughtcrime.securesms.util.VisibleForTesting;
import org.whispersystems.textsecure.api.util.InvalidNumberException;
import org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -37,6 +40,8 @@ import java.util.concurrent.ConcurrentHashMap;
public class CanonicalAddressDatabase { public class CanonicalAddressDatabase {
private static final String TAG = CanonicalAddressDatabase.class.getSimpleName();
private static final int DATABASE_VERSION = 1; private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "canonical_address.db"; private static final String DATABASE_NAME = "canonical_address.db";
private static final String TABLE = "canonical_addresses"; private static final String TABLE = "canonical_addresses";
@ -46,25 +51,25 @@ public class CanonicalAddressDatabase {
private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE + " (" + ID_COLUMN + " integer PRIMARY KEY, " + ADDRESS_COLUMN + " TEXT NOT NULL);"; private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE + " (" + ID_COLUMN + " integer PRIMARY KEY, " + ADDRESS_COLUMN + " TEXT NOT NULL);";
private static final String SELECTION_NUMBER = "PHONE_NUMBERS_EQUAL(" + ADDRESS_COLUMN + ", ?)"; private static final String SELECTION_NUMBER = "PHONE_NUMBERS_EQUAL(" + ADDRESS_COLUMN + ", ?)";
private static final String SELECTION_OTHER = ADDRESS_COLUMN + " = ? COLLATE NOCASE"; private static final String SELECTION_OTHER = ADDRESS_COLUMN + " = ? COLLATE NOCASE";
private static final Object lock = new Object();
private static CanonicalAddressDatabase instance; private static CanonicalAddressDatabase instance;
private DatabaseHelper databaseHelper; private DatabaseHelper databaseHelper;
private final Context context;
private final Map<String, Long> addressCache = new ConcurrentHashMap<String, Long>(); private final Map<String, Long> addressCache = new ConcurrentHashMap<>();
private final Map<Long, String> idCache = new ConcurrentHashMap<Long, String>(); private final Map<Long, String> idCache = new ConcurrentHashMap<>();
public static CanonicalAddressDatabase getInstance(Context context) { public synchronized static CanonicalAddressDatabase getInstance(Context context) {
synchronized (lock) {
if (instance == null) if (instance == null)
instance = new CanonicalAddressDatabase(context.getApplicationContext()); instance = new CanonicalAddressDatabase(context.getApplicationContext());
return instance; return instance;
} }
}
private CanonicalAddressDatabase(Context context) { private CanonicalAddressDatabase(Context context) {
databaseHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION); this.context = context;
this.databaseHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
fillCache(); fillCache();
} }
@ -107,7 +112,7 @@ public class CanonicalAddressDatabase {
Cursor cursor = null; Cursor cursor = null;
try { try {
Log.w("CanonicalAddressDatabase", "Hitting DB on query [ID]."); Log.w(TAG, "Hitting DB on query [ID].");
SQLiteDatabase db = databaseHelper.getReadableDatabase(); SQLiteDatabase db = databaseHelper.getReadableDatabase();
cursor = db.query(TABLE, null, ID_COLUMN + " = ?", new String[] {id+""}, null, null, null); cursor = db.query(TABLE, null, ID_COLUMN + " = ?", new String[] {id+""}, null, null, null);
@ -134,20 +139,31 @@ public class CanonicalAddressDatabase {
instance = null; instance = null;
} }
public long getCanonicalAddressId(String address) { public long getCanonicalAddressId(@NonNull String address) {
try {
long canonicalAddressId; long canonicalAddressId;
if ((canonicalAddressId = getCanonicalAddressFromCache(address)) != -1) if (isNumberAddress(address) && TextSecurePreferences.isPushRegistered(context)) {
return canonicalAddressId; address = PhoneNumberFormatter.formatNumber(address, TextSecurePreferences.getLocalNumber(context));
}
canonicalAddressId = getCanonicalAddressIdFromDatabase(address); if ((canonicalAddressId = getCanonicalAddressFromCache(address)) != -1) {
idCache.put(canonicalAddressId, address);
addressCache.put(address, canonicalAddressId);
return canonicalAddressId; return canonicalAddressId;
} }
public List<Long> getCanonicalAddressIds(List<String> addresses) { canonicalAddressId = getCanonicalAddressIdFromDatabase(address);
List<Long> addressList = new LinkedList<Long>();
idCache.put(canonicalAddressId, address);
addressCache.put(address, canonicalAddressId);
return canonicalAddressId;
} catch (InvalidNumberException e) {
throw new AssertionError(e);
}
}
public @NonNull List<Long> getCanonicalAddressIds(@NonNull List<String> addresses) {
List<Long> addressList = new LinkedList<>();
for (String address : addresses) { for (String address : addresses) {
addressList.add(getCanonicalAddressId(address)); addressList.add(getCanonicalAddressId(address));
@ -161,14 +177,17 @@ public class CanonicalAddressDatabase {
return cachedAddress == null ? -1L : cachedAddress; return cachedAddress == null ? -1L : cachedAddress;
} }
private long getCanonicalAddressIdFromDatabase(String address) { private long getCanonicalAddressIdFromDatabase(@NonNull String address) {
Log.w(TAG, "Hitting DB on query [ADDRESS]");
Cursor cursor = null; Cursor cursor = null;
try { try {
SQLiteDatabase db = databaseHelper.getWritableDatabase(); SQLiteDatabase db = databaseHelper.getWritableDatabase();
String[] selectionArguments = new String[]{address}; String[] selectionArguments = new String[]{address};
boolean isNumber = isNumberAddress(address); boolean isNumber = isNumberAddress(address);
cursor = db.query(TABLE, null,
isNumber ? SELECTION_NUMBER : SELECTION_OTHER, cursor = db.query(TABLE, null, isNumber ? SELECTION_NUMBER : SELECTION_OTHER,
selectionArguments, null, null, null); selectionArguments, null, null, null);
if (cursor.getCount() == 0 || !cursor.moveToFirst()) { if (cursor.getCount() == 0 || !cursor.moveToFirst()) {
@ -176,15 +195,17 @@ public class CanonicalAddressDatabase {
contentValues.put(ADDRESS_COLUMN, address); contentValues.put(ADDRESS_COLUMN, address);
return db.insert(TABLE, ADDRESS_COLUMN, contentValues); return db.insert(TABLE, ADDRESS_COLUMN, contentValues);
} else { } else {
final long canonicalId = cursor.getLong(cursor.getColumnIndexOrThrow(ID_COLUMN)); long canonicalId = cursor.getLong(cursor.getColumnIndexOrThrow(ID_COLUMN));
final String oldAddress = cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS_COLUMN)); String oldAddress = cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS_COLUMN));
if (oldAddress == null || !oldAddress.equals(address)) {
if (!address.equals(oldAddress)) {
ContentValues contentValues = new ContentValues(1); ContentValues contentValues = new ContentValues(1);
contentValues.put(ADDRESS_COLUMN, address); contentValues.put(ADDRESS_COLUMN, address);
db.update(TABLE, contentValues, ID_COLUMN + " = ?", new String[]{canonicalId+""}); db.update(TABLE, contentValues, ID_COLUMN + " = ?", new String[]{canonicalId+""});
addressCache.remove(oldAddress); addressCache.remove(oldAddress);
} }
return canonicalId; return canonicalId;
} }
} finally { } finally {
@ -195,22 +216,18 @@ public class CanonicalAddressDatabase {
} }
@VisibleForTesting @VisibleForTesting
static boolean isNumberAddress(String number) { static boolean isNumberAddress(@NonNull String number) {
if (number.contains("@")) if (number.contains("@")) return false;
return false; if (GroupUtil.isEncodedGroup(number)) return false;
if (GroupUtil.isEncodedGroup(number))
return false;
final String networkNumber = PhoneNumberUtils.extractNetworkPortion(number); final String networkNumber = PhoneNumberUtils.extractNetworkPortion(number);
if (TextUtils.isEmpty(networkNumber))
return false; if (TextUtils.isEmpty(networkNumber)) return false;
if (networkNumber.length() < 3) if (networkNumber.length() < 3) return false;
return false;
return PhoneNumberUtils.isWellFormedSmsAddress(number); return PhoneNumberUtils.isWellFormedSmsAddress(number);
} }
private static class DatabaseHelper extends SQLiteOpenHelper { private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {

View File

@ -289,8 +289,6 @@ public class MmsSmsDatabase extends Database {
public MessageRecord getCurrent() { public MessageRecord getCurrent() {
String type = cursor.getString(cursor.getColumnIndexOrThrow(TRANSPORT)); String type = cursor.getString(cursor.getColumnIndexOrThrow(TRANSPORT));
Log.w("MmsSmsDatabase", "Type: " + type);
if (MmsSmsDatabase.MMS_TRANSPORT.equals(type)) return getMmsReader().getCurrent(); if (MmsSmsDatabase.MMS_TRANSPORT.equals(type)) return getMmsReader().getCurrent();
else if (MmsSmsDatabase.SMS_TRANSPORT.equals(type)) return getSmsReader().getCurrent(); else if (MmsSmsDatabase.SMS_TRANSPORT.equals(type)) return getSmsReader().getCurrent();
else throw new AssertionError("Bad type: " + type); else throw new AssertionError("Bad type: " + type);