use latest android number as recipient number

Fixes #791
// FREEBIE
This commit is contained in:
Jake McGinty
2014-04-24 16:40:54 -07:00
parent 61d18f49ad
commit f6e04d0f89
13 changed files with 266 additions and 54 deletions

View File

@@ -50,10 +50,11 @@ public class ConversationListActivity extends PassphraseRequiredSherlockFragment
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
private ConversationListFragment fragment;
private MasterSecret masterSecret;
private DrawerLayout drawerLayout;
private DrawerToggle drawerToggle;
private ListView drawerList;
private MasterSecret masterSecret;
private DrawerLayout drawerLayout;
private DrawerToggle drawerToggle;
private ListView drawerList;
private ContentObserver observer;
@Override
public void onCreate(Bundle icicle) {
@@ -92,6 +93,7 @@ public class ConversationListActivity extends PassphraseRequiredSherlockFragment
public void onDestroy() {
Log.w("ConversationListActivity", "onDestroy...");
MemoryCleaner.clean(masterSecret);
if (observer != null) getContentResolver().unregisterContentObserver(observer);
super.onDestroy();
}
@@ -254,11 +256,19 @@ public class ConversationListActivity extends PassphraseRequiredSherlockFragment
}
private void initializeContactUpdatesReceiver() {
ContentObserver observer = new ContentObserver(null) {
observer = new ContentObserver(null) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.w("ConversationListActivity", "detected android contact data changed, refreshing cache");
// TODO only clear updated recipients from cache
RecipientFactory.clearCache();
ConversationListActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
((ConversationListAdapter)fragment.getListAdapter()).notifyDataSetChanged();
}
});
}
};

View File

@@ -22,13 +22,17 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import java.util.Collections;
import java.util.HashMap;
import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.VisibleForTesting;
import org.whispersystems.textsecure.util.Util;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class CanonicalAddressDatabase {
@@ -39,15 +43,15 @@ public class CanonicalAddressDatabase {
private static final String ADDRESS_COLUMN = "address";
private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE + " (" + ID_COLUMN + " integer PRIMARY KEY, " + ADDRESS_COLUMN + " TEXT NOT NULL);";
private static final String[] ID_PROJECTION = {ID_COLUMN};
private static final String SELECTION = "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 Object lock = new Object();
private static CanonicalAddressDatabase instance;
private DatabaseHelper databaseHelper;
private DatabaseHelper databaseHelper;
private final Map<String,Long> addressCache = Collections.synchronizedMap(new HashMap<String,Long>());
private final Map<String,String> idCache = Collections.synchronizedMap(new HashMap<String,String>());
private final Map<String, Long> addressCache = new ConcurrentHashMap<String, Long>();
private final Map<Long, String> idCache = new ConcurrentHashMap<Long, String>();
public static CanonicalAddressDatabase getInstance(Context context) {
synchronized (lock) {
@@ -78,13 +82,13 @@ public class CanonicalAddressDatabase {
cursor = db.query(TABLE, null, null, null, null, null, null);
while (cursor != null && cursor.moveToNext()) {
long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID_COLUMN));
long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID_COLUMN));
String address = cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS_COLUMN));
if (address == null || address.trim().length() == 0)
address = "Anonymous";
idCache.put(id+"", address);
idCache.put(id, address);
addressCache.put(address, id);
}
} finally {
@@ -93,9 +97,7 @@ public class CanonicalAddressDatabase {
}
}
public String getAddressFromId(String id) {
if (id == null || id.trim().equals("")) return "Anonymous";
public String getAddressFromId(long id) {
String cachedAddress = idCache.get(id);
if (cachedAddress != null)
@@ -131,58 +133,83 @@ public class CanonicalAddressDatabase {
instance = null;
}
public long getCanonicalAddress(String address) {
long canonicalAddress;
public long getCanonicalAddressId(String address) {
long canonicalAddressId;
if ((canonicalAddress = getCanonicalAddressFromCache(address)) != -1)
return canonicalAddress;
if ((canonicalAddressId = getCanonicalAddressFromCache(address)) != -1)
return canonicalAddressId;
canonicalAddress = getCanonicalAddressFromDatabase(address);
addressCache.put(address, canonicalAddress);
return canonicalAddress;
canonicalAddressId = getCanonicalAddressIdFromDatabase(address);
idCache.put(canonicalAddressId, address);
addressCache.put(address, canonicalAddressId);
return canonicalAddressId;
}
public List<Long> getCanonicalAddresses(List<String> addresses) {
public List<Long> getCanonicalAddressIds(List<String> addresses) {
List<Long> addressList = new LinkedList<Long>();
for (String address : addresses) {
addressList.add(getCanonicalAddress(address));
addressList.add(getCanonicalAddressId(address));
}
return addressList;
}
private long getCanonicalAddressFromCache(String address) {
if (addressCache.containsKey(address))
return Long.valueOf(addressCache.get(address));
return -1L;
Long cachedAddress = addressCache.get(address);
return cachedAddress == null ? -1L : cachedAddress;
}
private long getCanonicalAddressFromDatabase(String address) {
private long getCanonicalAddressIdFromDatabase(String address) {
Cursor cursor = null;
try {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
String[] selectionArguments = new String[] {address};
cursor = db.query(TABLE, ID_PROJECTION, SELECTION, selectionArguments, null, null, null);
boolean isNumber = isNumberAddress(address);
cursor = db.query(TABLE, null,
isNumber ? SELECTION_NUMBER : SELECTION_OTHER,
selectionArguments, null, null, null);
if (cursor.getCount() == 0 || !cursor.moveToFirst()) {
ContentValues contentValues = new ContentValues(1);
contentValues.put(ADDRESS_COLUMN, address);
return db.insert(TABLE, ADDRESS_COLUMN, contentValues);
}
} else {
final long canonicalId = cursor.getLong(cursor.getColumnIndexOrThrow(ID_COLUMN));
final String oldAddress = cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS_COLUMN));
if (oldAddress == null || !oldAddress.equals(address)) {
ContentValues contentValues = new ContentValues(1);
contentValues.put(ADDRESS_COLUMN, address);
db.update(TABLE, contentValues, ID_COLUMN + " = ?", new String[]{canonicalId+""});
return cursor.getLong(cursor.getColumnIndexOrThrow(ID_COLUMN));
addressCache.remove(oldAddress);
}
return canonicalId;
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
@VisibleForTesting
static boolean isNumberAddress(String number) {
if (number.contains("@"))
return false;
if (GroupUtil.isEncodedGroup(number))
return false;
final String networkNumber = PhoneNumberUtils.extractNetworkPortion(number);
if (Util.isEmpty(networkNumber))
return false;
if (networkNumber.length() < 3)
return false;
return PhoneNumberUtils.isWellFormedSmsAddress(number);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {

View File

@@ -59,7 +59,7 @@ public class CanonicalSessionMigrator {
File item = new File(rootDirectory.getAbsolutePath() + File.separatorChar + files[i]);
if (!item.isDirectory() && files[i].matches("[0-9]+")) {
long canonicalAddress = canonicalDb.getCanonicalAddress(files[i]);
long canonicalAddress = canonicalDb.getCanonicalAddressId(files[i]);
migrateSession(item, sessionsDirectory, canonicalAddress);
}
}

View File

@@ -248,7 +248,7 @@ public class ThreadDatabase extends Database {
if (filter == null || filter.size() == 0)
return null;
List<Long> recipientIds = DatabaseFactory.getAddressDatabase(context).getCanonicalAddresses(filter);
List<Long> recipientIds = DatabaseFactory.getAddressDatabase(context).getCanonicalAddressIds(filter);
if (recipientIds == null || recipientIds.size() == 0)
return null;

View File

@@ -49,9 +49,9 @@ public class Recipient implements Parcelable, CanonicalRecipient {
private final HashSet<RecipientModifiedListener> listeners = new HashSet<RecipientModifiedListener>();
private final String number;
private final long recipientId;
private final long recipientId;
private String number;
private String name;
private Bitmap contactPhoto;
@@ -75,6 +75,7 @@ public class Recipient implements Parcelable, CanonicalRecipient {
synchronized (Recipient.this) {
Recipient.this.name = result.name;
Recipient.this.number = result.number;
Recipient.this.contactUri = result.contactUri;
Recipient.this.contactPhoto = result.avatar;
Recipient.this.circleCroppedContactPhoto = result.croppedAvatar;

View File

@@ -21,7 +21,6 @@ import android.util.Log;
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
import org.thoughtcrime.securesms.util.NumberUtil;
import org.whispersystems.textsecure.push.IncomingPushMessage;
import org.whispersystems.textsecure.util.Util;
@@ -51,7 +50,7 @@ public class RecipientFactory {
}
private static Recipient getRecipientForNumber(Context context, String number, boolean asynchronous) {
long recipientId = CanonicalAddressDatabase.getInstance(context).getCanonicalAddress(number);
long recipientId = CanonicalAddressDatabase.getInstance(context).getCanonicalAddressId(number);
return provider.getRecipient(context, recipientId, asynchronous);
}

View File

@@ -17,6 +17,7 @@
package org.thoughtcrime.securesms.recipients;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
@@ -50,6 +51,7 @@ public class RecipientProvider {
PhoneLookup.DISPLAY_NAME,
PhoneLookup.LOOKUP_KEY,
PhoneLookup._ID,
PhoneLookup.NUMBER
};
public Recipient getRecipient(Context context, long recipientId, boolean asynchronous) {
@@ -60,19 +62,19 @@ public class RecipientProvider {
else return getSynchronousRecipient(context, recipientId);
}
private Recipient getSynchronousRecipient(Context context, long recipientId) {
private Recipient getSynchronousRecipient(final Context context, final long recipientId) {
Log.w("RecipientProvider", "Cache miss [SYNC]!");
Recipient recipient;
final Recipient recipient;
RecipientDetails details;
String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(String.valueOf(recipientId));
String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(recipientId);
final boolean isGroupRecipient = GroupUtil.isEncodedGroup(number);
if (isGroupRecipient) details = getGroupRecipientDetails(context, number);
else details = getRecipientDetails(context, number);
if (details != null) {
recipient = new Recipient(details.name, number, recipientId, details.contactUri, details.avatar,
recipient = new Recipient(details.name, details.number, recipientId, details.contactUri, details.avatar,
details.croppedAvatar);
} else {
final Bitmap defaultPhoto = isGroupRecipient
@@ -92,7 +94,7 @@ public class RecipientProvider {
private Recipient getAsynchronousRecipient(final Context context, final long recipientId) {
Log.w("RecipientProvider", "Cache miss [ASYNC]!");
final String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(String.valueOf(recipientId));
final String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(recipientId);
final boolean isGroupRecipient = GroupUtil.isEncodedGroup(number);
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
@@ -143,8 +145,7 @@ public class RecipientProvider {
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
Bitmap contactPhoto = ContactPhotoFactory.getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI,
cursor.getLong(2)+""));
return new RecipientDetails(cursor.getString(0), contactUri, contactPhoto,
return new RecipientDetails(cursor.getString(0), cursor.getString(3), contactUri, contactPhoto,
BitmapUtil.getCircleCroppedBitmap(contactPhoto));
}
} finally {
@@ -167,7 +168,7 @@ public class RecipientProvider {
if (avatarBytes == null) avatar = ContactPhotoFactory.getDefaultGroupPhoto(context);
else avatar = BitmapFactory.decodeByteArray(avatarBytes, 0, avatarBytes.length);
return new RecipientDetails(record.getTitle(), null, avatar, BitmapUtil.getCircleCroppedBitmap(avatar));
return new RecipientDetails(record.getTitle(), groupId, null, avatar, BitmapUtil.getCircleCroppedBitmap(avatar));
}
return null;
@@ -179,12 +180,14 @@ public class RecipientProvider {
public static class RecipientDetails {
public final String name;
public final String number;
public final Bitmap avatar;
public final Bitmap croppedAvatar;
public final Uri contactUri;
public RecipientDetails(String name, Uri contactUri, Bitmap avatar, Bitmap croppedAvatar) {
public RecipientDetails(String name, String number, Uri contactUri, Bitmap avatar, Bitmap croppedAvatar) {
this.name = name;
this.number = number;
this.avatar = avatar;
this.croppedAvatar = croppedAvatar;
this.contactUri = contactUri;

View File

@@ -0,0 +1,4 @@
package org.thoughtcrime.securesms.util;
public @interface VisibleForTesting {
}