mirror of
https://github.com/oxen-io/session-android.git
synced 2025-02-21 10:08:27 +00:00
parent
61d18f49ad
commit
f6e04d0f89
@ -0,0 +1,118 @@
|
|||||||
|
package org.thoughtcrime.securesms.database;
|
||||||
|
|
||||||
|
import android.test.InstrumentationTestCase;
|
||||||
|
|
||||||
|
import static org.fest.assertions.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class CanonicalAddressDatabaseTest extends InstrumentationTestCase {
|
||||||
|
private static final String AMBIGUOUS_NUMBER = "222-3333";
|
||||||
|
private static final String SPECIFIC_NUMBER = "+49 444 222 3333";
|
||||||
|
private static final String EMAIL = "a@b.fom";
|
||||||
|
private static final String SIMILAR_EMAIL = "a@b.com";
|
||||||
|
private static final String GROUP = "__textsecure_group__!000111222333";
|
||||||
|
private static final String SIMILAR_GROUP = "__textsecure_group__!100111222333";
|
||||||
|
private static final String ALPHA = "T-Mobile";
|
||||||
|
private static final String SIMILAR_ALPHA = "T-Mobila";
|
||||||
|
|
||||||
|
private CanonicalAddressDatabase db;
|
||||||
|
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
this.db = CanonicalAddressDatabase.getInstance(getInstrumentation().getTargetContext());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tearDown() throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throw two equivalent numbers (one without locale info, one with full info) at the canonical
|
||||||
|
* address db and see that the caching and DB operations work properly in revealing the right
|
||||||
|
* addresses. This is run twice to ensure cache logic is hit.
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public void testNumberAddressUpdates() throws Exception {
|
||||||
|
final long id = db.getCanonicalAddressId(AMBIGUOUS_NUMBER);
|
||||||
|
|
||||||
|
assertThat(db.getAddressFromId(id)).isEqualTo(AMBIGUOUS_NUMBER);
|
||||||
|
assertThat(db.getCanonicalAddressId(SPECIFIC_NUMBER)).isEqualTo(id);
|
||||||
|
assertThat(db.getAddressFromId(id)).isEqualTo(SPECIFIC_NUMBER);
|
||||||
|
assertThat(db.getCanonicalAddressId(AMBIGUOUS_NUMBER)).isEqualTo(id);
|
||||||
|
|
||||||
|
assertThat(db.getCanonicalAddressId(AMBIGUOUS_NUMBER)).isEqualTo(id);
|
||||||
|
assertThat(db.getAddressFromId(id)).isEqualTo(AMBIGUOUS_NUMBER);
|
||||||
|
assertThat(db.getCanonicalAddressId(SPECIFIC_NUMBER)).isEqualTo(id);
|
||||||
|
assertThat(db.getAddressFromId(id)).isEqualTo(SPECIFIC_NUMBER);
|
||||||
|
assertThat(db.getCanonicalAddressId(AMBIGUOUS_NUMBER)).isEqualTo(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSimilarNumbers() throws Exception {
|
||||||
|
assertThat(db.getCanonicalAddressId("This is a phone number 222-333-444"))
|
||||||
|
.isNotEqualTo(db.getCanonicalAddressId("222-333-4444"));
|
||||||
|
assertThat(db.getCanonicalAddressId("222-333-444"))
|
||||||
|
.isNotEqualTo(db.getCanonicalAddressId("222-333-4444"));
|
||||||
|
assertThat(db.getCanonicalAddressId("222-333-44"))
|
||||||
|
.isNotEqualTo(db.getCanonicalAddressId("222-333-4444"));
|
||||||
|
assertThat(db.getCanonicalAddressId("222-333-4"))
|
||||||
|
.isNotEqualTo(db.getCanonicalAddressId("222-333-4444"));
|
||||||
|
assertThat(db.getCanonicalAddressId("+49 222-333-4444"))
|
||||||
|
.isNotEqualTo(db.getCanonicalAddressId("+1 222-333-4444"));
|
||||||
|
|
||||||
|
assertThat(db.getCanonicalAddressId("1 222-333-4444"))
|
||||||
|
.isEqualTo(db.getCanonicalAddressId("222-333-4444"));
|
||||||
|
assertThat(db.getCanonicalAddressId("1 (222) 333-4444"))
|
||||||
|
.isEqualTo(db.getCanonicalAddressId("222-333-4444"));
|
||||||
|
assertThat(db.getCanonicalAddressId("+12223334444"))
|
||||||
|
.isEqualTo(db.getCanonicalAddressId("222-333-4444"));
|
||||||
|
assertThat(db.getCanonicalAddressId("+1 (222) 333.4444"))
|
||||||
|
.isEqualTo(db.getCanonicalAddressId("222-333-4444"));
|
||||||
|
assertThat(db.getCanonicalAddressId("+49 (222) 333.4444"))
|
||||||
|
.isEqualTo(db.getCanonicalAddressId("222-333-4444"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testEmailAddresses() throws Exception {
|
||||||
|
final long emailId = db.getCanonicalAddressId(EMAIL);
|
||||||
|
final long similarEmailId = db.getCanonicalAddressId(SIMILAR_EMAIL);
|
||||||
|
|
||||||
|
assertThat(emailId).isNotEqualTo(similarEmailId);
|
||||||
|
|
||||||
|
assertThat(db.getAddressFromId(emailId)).isEqualTo(EMAIL);
|
||||||
|
assertThat(db.getAddressFromId(similarEmailId)).isEqualTo(SIMILAR_EMAIL);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGroups() throws Exception {
|
||||||
|
final long groupId = db.getCanonicalAddressId(GROUP);
|
||||||
|
final long similarGroupId = db.getCanonicalAddressId(SIMILAR_GROUP);
|
||||||
|
|
||||||
|
assertThat(groupId).isNotEqualTo(similarGroupId);
|
||||||
|
|
||||||
|
assertThat(db.getAddressFromId(groupId)).isEqualTo(GROUP);
|
||||||
|
assertThat(db.getAddressFromId(similarGroupId)).isEqualTo(SIMILAR_GROUP);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAlpha() throws Exception {
|
||||||
|
final long id = db.getCanonicalAddressId(ALPHA);
|
||||||
|
final long similarId = db.getCanonicalAddressId(SIMILAR_ALPHA);
|
||||||
|
|
||||||
|
assertThat(id).isNotEqualTo(similarId);
|
||||||
|
|
||||||
|
assertThat(db.getAddressFromId(id)).isEqualTo(ALPHA);
|
||||||
|
assertThat(db.getAddressFromId(similarId)).isEqualTo(SIMILAR_ALPHA);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIsNumber() throws Exception {
|
||||||
|
assertThat(CanonicalAddressDatabase.isNumberAddress("+495556666777")).isTrue();
|
||||||
|
assertThat(CanonicalAddressDatabase.isNumberAddress("(222) 333-4444")).isTrue();
|
||||||
|
assertThat(CanonicalAddressDatabase.isNumberAddress("1 (222) 333-4444")).isTrue();
|
||||||
|
assertThat(CanonicalAddressDatabase.isNumberAddress("T-Mobile123")).isTrue();
|
||||||
|
assertThat(CanonicalAddressDatabase.isNumberAddress("333-4444")).isTrue();
|
||||||
|
assertThat(CanonicalAddressDatabase.isNumberAddress("12345")).isTrue();
|
||||||
|
assertThat(CanonicalAddressDatabase.isNumberAddress("T-Mobile")).isFalse();
|
||||||
|
assertThat(CanonicalAddressDatabase.isNumberAddress("T-Mobile1")).isFalse();
|
||||||
|
assertThat(CanonicalAddressDatabase.isNumberAddress("Wherever bank")).isFalse();
|
||||||
|
assertThat(CanonicalAddressDatabase.isNumberAddress("__textsecure_group__!afafafafafaf")).isFalse();
|
||||||
|
assertThat(CanonicalAddressDatabase.isNumberAddress("email@domain.com")).isFalse();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package org.thoughtcrime.securesms.util;
|
||||||
|
|
||||||
|
import android.test.AndroidTestCase;
|
||||||
|
|
||||||
|
import junit.framework.AssertionFailedError;
|
||||||
|
|
||||||
|
import org.whispersystems.textsecure.util.InvalidNumberException;
|
||||||
|
import org.whispersystems.textsecure.util.PhoneNumberFormatter;
|
||||||
|
import static org.fest.assertions.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class PhoneNumberFormatterTest extends AndroidTestCase {
|
||||||
|
private static final String LOCAL_NUMBER = "+15555555555";
|
||||||
|
|
||||||
|
public void testFormatNumberE164() throws Exception, InvalidNumberException {
|
||||||
|
assertThat(PhoneNumberFormatter.formatNumber("(555) 555-5555", LOCAL_NUMBER)).isEqualTo(LOCAL_NUMBER);
|
||||||
|
assertThat(PhoneNumberFormatter.formatNumber("555-5555", LOCAL_NUMBER)).isEqualTo(LOCAL_NUMBER);
|
||||||
|
assertThat(PhoneNumberFormatter.formatNumber("(123) 555-5555", LOCAL_NUMBER)).isNotEqualTo(LOCAL_NUMBER);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testFormatNumberEmail() throws Exception {
|
||||||
|
try {
|
||||||
|
PhoneNumberFormatter.formatNumber("person@domain.com", LOCAL_NUMBER);
|
||||||
|
throw new AssertionFailedError("should have thrown on email");
|
||||||
|
} catch (InvalidNumberException ine) {
|
||||||
|
// success
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package org.thoughtcrime.securesms.util;
|
||||||
|
|
||||||
|
import android.test.AndroidTestCase;
|
||||||
|
|
||||||
|
import static org.fest.assertions.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class UtilTest extends AndroidTestCase {
|
||||||
|
|
||||||
|
}
|
10
build.gradle
10
build.gradle
@ -27,6 +27,8 @@ dependencies {
|
|||||||
compile 'com.google.android.gcm:gcm-client:1.0.2'
|
compile 'com.google.android.gcm:gcm-client:1.0.2'
|
||||||
compile 'se.emilsjolander:stickylistheaders:2.2.0'
|
compile 'se.emilsjolander:stickylistheaders:2.2.0'
|
||||||
|
|
||||||
|
androidTestCompile 'com.squareup:fest-android:1.0.8'
|
||||||
|
|
||||||
compile project(':library')
|
compile project(':library')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +40,7 @@ dependencyVerification {
|
|||||||
'se.emilsjolander:stickylistheaders:89146b46c96fea0e40200474a2625cda10fe94891e4128f53cdb42375091b9b6',
|
'se.emilsjolander:stickylistheaders:89146b46c96fea0e40200474a2625cda10fe94891e4128f53cdb42375091b9b6',
|
||||||
'com.google.protobuf:protobuf-java:ad9769a22989e688a46af4d3accc348cc501ced22118033230542bc916e33f0b',
|
'com.google.protobuf:protobuf-java:ad9769a22989e688a46af4d3accc348cc501ced22118033230542bc916e33f0b',
|
||||||
'com.madgag:sc-light-jdk15on:931f39d351429fb96c2f749e7ecb1a256a8ebbf5edca7995c9cc085b94d1841d',
|
'com.madgag:sc-light-jdk15on:931f39d351429fb96c2f749e7ecb1a256a8ebbf5edca7995c9cc085b94d1841d',
|
||||||
'com.googlecode.libphonenumber:libphonenumber:c2cffa5c2ccb8fd5fc4b432aa7d1ee27482860c9751035a25c8ffa2375274fcf',
|
'com.googlecode.libphonenumber:libphonenumber:eba17eae81dd622ea89a00a3a8c025b2f25d342e0d9644c5b62e16f15687c3ab',
|
||||||
'org.whispersystems:gson:08f4f7498455d1539c9233e5aac18e9b1805815ef29221572996508eb512fe51',
|
'org.whispersystems:gson:08f4f7498455d1539c9233e5aac18e9b1805815ef29221572996508eb512fe51',
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -63,6 +65,12 @@ android {
|
|||||||
res.srcDirs = ['res']
|
res.srcDirs = ['res']
|
||||||
assets.srcDirs = ['assets']
|
assets.srcDirs = ['assets']
|
||||||
}
|
}
|
||||||
|
androidTest {
|
||||||
|
java.srcDirs = ['androidTest']
|
||||||
|
resources.srcDirs = ['androidTest']
|
||||||
|
aidl.srcDirs = ['androidTest']
|
||||||
|
renderscript.srcDirs = ['androidTest']
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
compile 'com.google.protobuf:protobuf-java:2.4.1'
|
compile 'com.google.protobuf:protobuf-java:2.4.1'
|
||||||
compile 'com.madgag:sc-light-jdk15on:1.47.0.2'
|
compile 'com.madgag:sc-light-jdk15on:1.47.0.2'
|
||||||
compile 'com.googlecode.libphonenumber:libphonenumber:5.3'
|
compile 'com.googlecode.libphonenumber:libphonenumber:6.1'
|
||||||
compile 'org.whispersystems:gson:2.2.4'
|
compile 'org.whispersystems:gson:2.2.4'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,10 +50,11 @@ public class ConversationListActivity extends PassphraseRequiredSherlockFragment
|
|||||||
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
||||||
|
|
||||||
private ConversationListFragment fragment;
|
private ConversationListFragment fragment;
|
||||||
private MasterSecret masterSecret;
|
private MasterSecret masterSecret;
|
||||||
private DrawerLayout drawerLayout;
|
private DrawerLayout drawerLayout;
|
||||||
private DrawerToggle drawerToggle;
|
private DrawerToggle drawerToggle;
|
||||||
private ListView drawerList;
|
private ListView drawerList;
|
||||||
|
private ContentObserver observer;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle icicle) {
|
public void onCreate(Bundle icicle) {
|
||||||
@ -92,6 +93,7 @@ public class ConversationListActivity extends PassphraseRequiredSherlockFragment
|
|||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
Log.w("ConversationListActivity", "onDestroy...");
|
Log.w("ConversationListActivity", "onDestroy...");
|
||||||
MemoryCleaner.clean(masterSecret);
|
MemoryCleaner.clean(masterSecret);
|
||||||
|
if (observer != null) getContentResolver().unregisterContentObserver(observer);
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -254,11 +256,19 @@ public class ConversationListActivity extends PassphraseRequiredSherlockFragment
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initializeContactUpdatesReceiver() {
|
private void initializeContactUpdatesReceiver() {
|
||||||
ContentObserver observer = new ContentObserver(null) {
|
observer = new ContentObserver(null) {
|
||||||
@Override
|
@Override
|
||||||
public void onChange(boolean selfChange) {
|
public void onChange(boolean selfChange) {
|
||||||
super.onChange(selfChange);
|
super.onChange(selfChange);
|
||||||
|
Log.w("ConversationListActivity", "detected android contact data changed, refreshing cache");
|
||||||
|
// TODO only clear updated recipients from cache
|
||||||
RecipientFactory.clearCache();
|
RecipientFactory.clearCache();
|
||||||
|
ConversationListActivity.this.runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
((ConversationListAdapter)fragment.getListAdapter()).notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -22,13 +22,17 @@ import android.database.Cursor;
|
|||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.database.sqlite.SQLiteDatabase.CursorFactory;
|
import android.database.sqlite.SQLiteDatabase.CursorFactory;
|
||||||
import android.database.sqlite.SQLiteOpenHelper;
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
|
import android.telephony.PhoneNumberUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.util.Collections;
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
||||||
import java.util.HashMap;
|
import org.thoughtcrime.securesms.util.VisibleForTesting;
|
||||||
|
import org.whispersystems.textsecure.util.Util;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
public class CanonicalAddressDatabase {
|
public class CanonicalAddressDatabase {
|
||||||
|
|
||||||
@ -39,15 +43,15 @@ public class CanonicalAddressDatabase {
|
|||||||
private static final String ADDRESS_COLUMN = "address";
|
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 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_NUMBER = "PHONE_NUMBERS_EQUAL(" + ADDRESS_COLUMN + ", ?)";
|
||||||
private static final String SELECTION = "PHONE_NUMBERS_EQUAL(" + ADDRESS_COLUMN + ", ?)";
|
private static final String SELECTION_OTHER = ADDRESS_COLUMN + " = ? COLLATE NOCASE";
|
||||||
private static final Object lock = new Object();
|
private static final Object lock = new Object();
|
||||||
|
|
||||||
private static CanonicalAddressDatabase instance;
|
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, Long> addressCache = new ConcurrentHashMap<String, Long>();
|
||||||
private final Map<String,String> idCache = Collections.synchronizedMap(new HashMap<String,String>());
|
private final Map<Long, String> idCache = new ConcurrentHashMap<Long, String>();
|
||||||
|
|
||||||
public static CanonicalAddressDatabase getInstance(Context context) {
|
public static CanonicalAddressDatabase getInstance(Context context) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
@ -78,13 +82,13 @@ public class CanonicalAddressDatabase {
|
|||||||
cursor = db.query(TABLE, null, null, null, null, null, null);
|
cursor = db.query(TABLE, null, null, null, null, null, null);
|
||||||
|
|
||||||
while (cursor != null && cursor.moveToNext()) {
|
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));
|
String address = cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS_COLUMN));
|
||||||
|
|
||||||
if (address == null || address.trim().length() == 0)
|
if (address == null || address.trim().length() == 0)
|
||||||
address = "Anonymous";
|
address = "Anonymous";
|
||||||
|
|
||||||
idCache.put(id+"", address);
|
idCache.put(id, address);
|
||||||
addressCache.put(address, id);
|
addressCache.put(address, id);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@ -93,9 +97,7 @@ public class CanonicalAddressDatabase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAddressFromId(String id) {
|
public String getAddressFromId(long id) {
|
||||||
if (id == null || id.trim().equals("")) return "Anonymous";
|
|
||||||
|
|
||||||
String cachedAddress = idCache.get(id);
|
String cachedAddress = idCache.get(id);
|
||||||
|
|
||||||
if (cachedAddress != null)
|
if (cachedAddress != null)
|
||||||
@ -131,58 +133,83 @@ public class CanonicalAddressDatabase {
|
|||||||
instance = null;
|
instance = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getCanonicalAddress(String address) {
|
public long getCanonicalAddressId(String address) {
|
||||||
long canonicalAddress;
|
long canonicalAddressId;
|
||||||
|
|
||||||
if ((canonicalAddress = getCanonicalAddressFromCache(address)) != -1)
|
if ((canonicalAddressId = getCanonicalAddressFromCache(address)) != -1)
|
||||||
return canonicalAddress;
|
return canonicalAddressId;
|
||||||
|
|
||||||
canonicalAddress = getCanonicalAddressFromDatabase(address);
|
canonicalAddressId = getCanonicalAddressIdFromDatabase(address);
|
||||||
addressCache.put(address, canonicalAddress);
|
idCache.put(canonicalAddressId, address);
|
||||||
|
addressCache.put(address, canonicalAddressId);
|
||||||
return canonicalAddress;
|
return canonicalAddressId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Long> getCanonicalAddresses(List<String> addresses) {
|
public List<Long> getCanonicalAddressIds(List<String> addresses) {
|
||||||
List<Long> addressList = new LinkedList<Long>();
|
List<Long> addressList = new LinkedList<Long>();
|
||||||
|
|
||||||
for (String address : addresses) {
|
for (String address : addresses) {
|
||||||
addressList.add(getCanonicalAddress(address));
|
addressList.add(getCanonicalAddressId(address));
|
||||||
}
|
}
|
||||||
|
|
||||||
return addressList;
|
return addressList;
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getCanonicalAddressFromCache(String address) {
|
private long getCanonicalAddressFromCache(String address) {
|
||||||
if (addressCache.containsKey(address))
|
Long cachedAddress = addressCache.get(address);
|
||||||
return Long.valueOf(addressCache.get(address));
|
return cachedAddress == null ? -1L : cachedAddress;
|
||||||
|
|
||||||
return -1L;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getCanonicalAddressFromDatabase(String address) {
|
private long getCanonicalAddressIdFromDatabase(String 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};
|
||||||
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()) {
|
if (cursor.getCount() == 0 || !cursor.moveToFirst()) {
|
||||||
ContentValues contentValues = new ContentValues(1);
|
ContentValues contentValues = new ContentValues(1);
|
||||||
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 {
|
||||||
|
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 {
|
} finally {
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
cursor.close();
|
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 {
|
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) {
|
||||||
|
@ -59,7 +59,7 @@ public class CanonicalSessionMigrator {
|
|||||||
File item = new File(rootDirectory.getAbsolutePath() + File.separatorChar + files[i]);
|
File item = new File(rootDirectory.getAbsolutePath() + File.separatorChar + files[i]);
|
||||||
|
|
||||||
if (!item.isDirectory() && files[i].matches("[0-9]+")) {
|
if (!item.isDirectory() && files[i].matches("[0-9]+")) {
|
||||||
long canonicalAddress = canonicalDb.getCanonicalAddress(files[i]);
|
long canonicalAddress = canonicalDb.getCanonicalAddressId(files[i]);
|
||||||
migrateSession(item, sessionsDirectory, canonicalAddress);
|
migrateSession(item, sessionsDirectory, canonicalAddress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -248,7 +248,7 @@ public class ThreadDatabase extends Database {
|
|||||||
if (filter == null || filter.size() == 0)
|
if (filter == null || filter.size() == 0)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
List<Long> recipientIds = DatabaseFactory.getAddressDatabase(context).getCanonicalAddresses(filter);
|
List<Long> recipientIds = DatabaseFactory.getAddressDatabase(context).getCanonicalAddressIds(filter);
|
||||||
|
|
||||||
if (recipientIds == null || recipientIds.size() == 0)
|
if (recipientIds == null || recipientIds.size() == 0)
|
||||||
return null;
|
return null;
|
||||||
|
@ -49,9 +49,9 @@ public class Recipient implements Parcelable, CanonicalRecipient {
|
|||||||
|
|
||||||
private final HashSet<RecipientModifiedListener> listeners = new HashSet<RecipientModifiedListener>();
|
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 String name;
|
||||||
|
|
||||||
private Bitmap contactPhoto;
|
private Bitmap contactPhoto;
|
||||||
@ -75,6 +75,7 @@ public class Recipient implements Parcelable, CanonicalRecipient {
|
|||||||
|
|
||||||
synchronized (Recipient.this) {
|
synchronized (Recipient.this) {
|
||||||
Recipient.this.name = result.name;
|
Recipient.this.name = result.name;
|
||||||
|
Recipient.this.number = result.number;
|
||||||
Recipient.this.contactUri = result.contactUri;
|
Recipient.this.contactUri = result.contactUri;
|
||||||
Recipient.this.contactPhoto = result.avatar;
|
Recipient.this.contactPhoto = result.avatar;
|
||||||
Recipient.this.circleCroppedContactPhoto = result.croppedAvatar;
|
Recipient.this.circleCroppedContactPhoto = result.croppedAvatar;
|
||||||
|
@ -21,7 +21,6 @@ import android.util.Log;
|
|||||||
|
|
||||||
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
|
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
|
||||||
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
|
import org.thoughtcrime.securesms.database.CanonicalAddressDatabase;
|
||||||
import org.thoughtcrime.securesms.util.NumberUtil;
|
|
||||||
import org.whispersystems.textsecure.push.IncomingPushMessage;
|
import org.whispersystems.textsecure.push.IncomingPushMessage;
|
||||||
import org.whispersystems.textsecure.util.Util;
|
import org.whispersystems.textsecure.util.Util;
|
||||||
|
|
||||||
@ -51,7 +50,7 @@ public class RecipientFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Recipient getRecipientForNumber(Context context, String number, boolean asynchronous) {
|
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);
|
return provider.getRecipient(context, recipientId, asynchronous);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package org.thoughtcrime.securesms.recipients;
|
package org.thoughtcrime.securesms.recipients;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.database.ContentObserver;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
@ -50,6 +51,7 @@ public class RecipientProvider {
|
|||||||
PhoneLookup.DISPLAY_NAME,
|
PhoneLookup.DISPLAY_NAME,
|
||||||
PhoneLookup.LOOKUP_KEY,
|
PhoneLookup.LOOKUP_KEY,
|
||||||
PhoneLookup._ID,
|
PhoneLookup._ID,
|
||||||
|
PhoneLookup.NUMBER
|
||||||
};
|
};
|
||||||
|
|
||||||
public Recipient getRecipient(Context context, long recipientId, boolean asynchronous) {
|
public Recipient getRecipient(Context context, long recipientId, boolean asynchronous) {
|
||||||
@ -60,19 +62,19 @@ public class RecipientProvider {
|
|||||||
else return getSynchronousRecipient(context, recipientId);
|
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]!");
|
Log.w("RecipientProvider", "Cache miss [SYNC]!");
|
||||||
|
|
||||||
Recipient recipient;
|
final Recipient recipient;
|
||||||
RecipientDetails details;
|
RecipientDetails details;
|
||||||
String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(String.valueOf(recipientId));
|
String number = CanonicalAddressDatabase.getInstance(context).getAddressFromId(recipientId);
|
||||||
final boolean isGroupRecipient = GroupUtil.isEncodedGroup(number);
|
final boolean isGroupRecipient = GroupUtil.isEncodedGroup(number);
|
||||||
|
|
||||||
if (isGroupRecipient) details = getGroupRecipientDetails(context, number);
|
if (isGroupRecipient) details = getGroupRecipientDetails(context, number);
|
||||||
else details = getRecipientDetails(context, number);
|
else details = getRecipientDetails(context, number);
|
||||||
|
|
||||||
if (details != null) {
|
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);
|
details.croppedAvatar);
|
||||||
} else {
|
} else {
|
||||||
final Bitmap defaultPhoto = isGroupRecipient
|
final Bitmap defaultPhoto = isGroupRecipient
|
||||||
@ -92,7 +94,7 @@ public class RecipientProvider {
|
|||||||
private Recipient getAsynchronousRecipient(final Context context, final long recipientId) {
|
private Recipient getAsynchronousRecipient(final Context context, final long recipientId) {
|
||||||
Log.w("RecipientProvider", "Cache miss [ASYNC]!");
|
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);
|
final boolean isGroupRecipient = GroupUtil.isEncodedGroup(number);
|
||||||
|
|
||||||
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
|
Callable<RecipientDetails> task = new Callable<RecipientDetails>() {
|
||||||
@ -143,8 +145,7 @@ public class RecipientProvider {
|
|||||||
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
||||||
Bitmap contactPhoto = ContactPhotoFactory.getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI,
|
Bitmap contactPhoto = ContactPhotoFactory.getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI,
|
||||||
cursor.getLong(2)+""));
|
cursor.getLong(2)+""));
|
||||||
|
return new RecipientDetails(cursor.getString(0), cursor.getString(3), contactUri, contactPhoto,
|
||||||
return new RecipientDetails(cursor.getString(0), contactUri, contactPhoto,
|
|
||||||
BitmapUtil.getCircleCroppedBitmap(contactPhoto));
|
BitmapUtil.getCircleCroppedBitmap(contactPhoto));
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@ -167,7 +168,7 @@ public class RecipientProvider {
|
|||||||
if (avatarBytes == null) avatar = ContactPhotoFactory.getDefaultGroupPhoto(context);
|
if (avatarBytes == null) avatar = ContactPhotoFactory.getDefaultGroupPhoto(context);
|
||||||
else avatar = BitmapFactory.decodeByteArray(avatarBytes, 0, avatarBytes.length);
|
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;
|
return null;
|
||||||
@ -179,12 +180,14 @@ public class RecipientProvider {
|
|||||||
|
|
||||||
public static class RecipientDetails {
|
public static class RecipientDetails {
|
||||||
public final String name;
|
public final String name;
|
||||||
|
public final String number;
|
||||||
public final Bitmap avatar;
|
public final Bitmap avatar;
|
||||||
public final Bitmap croppedAvatar;
|
public final Bitmap croppedAvatar;
|
||||||
public final Uri contactUri;
|
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.name = name;
|
||||||
|
this.number = number;
|
||||||
this.avatar = avatar;
|
this.avatar = avatar;
|
||||||
this.croppedAvatar = croppedAvatar;
|
this.croppedAvatar = croppedAvatar;
|
||||||
this.contactUri = contactUri;
|
this.contactUri = contactUri;
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
package org.thoughtcrime.securesms.util;
|
||||||
|
|
||||||
|
public @interface VisibleForTesting {
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user