Skip to content

Commit f6e04d0

Browse files
committed
use latest android number as recipient number
Fixes signalapp#791 // FREEBIE
1 parent 61d18f4 commit f6e04d0

13 files changed

Lines changed: 265 additions & 53 deletions
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.thoughtcrime.securesms.database;
2+
3+
import android.test.InstrumentationTestCase;
4+
5+
import static org.fest.assertions.api.Assertions.assertThat;
6+
7+
public class CanonicalAddressDatabaseTest extends InstrumentationTestCase {
8+
private static final String AMBIGUOUS_NUMBER = "222-3333";
9+
private static final String SPECIFIC_NUMBER = "+49 444 222 3333";
10+
private static final String EMAIL = "[email protected]";
11+
private static final String SIMILAR_EMAIL = "[email protected]";
12+
private static final String GROUP = "__textsecure_group__!000111222333";
13+
private static final String SIMILAR_GROUP = "__textsecure_group__!100111222333";
14+
private static final String ALPHA = "T-Mobile";
15+
private static final String SIMILAR_ALPHA = "T-Mobila";
16+
17+
private CanonicalAddressDatabase db;
18+
19+
public void setUp() throws Exception {
20+
super.setUp();
21+
this.db = CanonicalAddressDatabase.getInstance(getInstrumentation().getTargetContext());
22+
}
23+
24+
public void tearDown() throws Exception {
25+
26+
}
27+
28+
/**
29+
* Throw two equivalent numbers (one without locale info, one with full info) at the canonical
30+
* address db and see that the caching and DB operations work properly in revealing the right
31+
* addresses. This is run twice to ensure cache logic is hit.
32+
*
33+
* @throws Exception
34+
*/
35+
public void testNumberAddressUpdates() throws Exception {
36+
final long id = db.getCanonicalAddressId(AMBIGUOUS_NUMBER);
37+
38+
assertThat(db.getAddressFromId(id)).isEqualTo(AMBIGUOUS_NUMBER);
39+
assertThat(db.getCanonicalAddressId(SPECIFIC_NUMBER)).isEqualTo(id);
40+
assertThat(db.getAddressFromId(id)).isEqualTo(SPECIFIC_NUMBER);
41+
assertThat(db.getCanonicalAddressId(AMBIGUOUS_NUMBER)).isEqualTo(id);
42+
43+
assertThat(db.getCanonicalAddressId(AMBIGUOUS_NUMBER)).isEqualTo(id);
44+
assertThat(db.getAddressFromId(id)).isEqualTo(AMBIGUOUS_NUMBER);
45+
assertThat(db.getCanonicalAddressId(SPECIFIC_NUMBER)).isEqualTo(id);
46+
assertThat(db.getAddressFromId(id)).isEqualTo(SPECIFIC_NUMBER);
47+
assertThat(db.getCanonicalAddressId(AMBIGUOUS_NUMBER)).isEqualTo(id);
48+
}
49+
50+
public void testSimilarNumbers() throws Exception {
51+
assertThat(db.getCanonicalAddressId("This is a phone number 222-333-444"))
52+
.isNotEqualTo(db.getCanonicalAddressId("222-333-4444"));
53+
assertThat(db.getCanonicalAddressId("222-333-444"))
54+
.isNotEqualTo(db.getCanonicalAddressId("222-333-4444"));
55+
assertThat(db.getCanonicalAddressId("222-333-44"))
56+
.isNotEqualTo(db.getCanonicalAddressId("222-333-4444"));
57+
assertThat(db.getCanonicalAddressId("222-333-4"))
58+
.isNotEqualTo(db.getCanonicalAddressId("222-333-4444"));
59+
assertThat(db.getCanonicalAddressId("+49 222-333-4444"))
60+
.isNotEqualTo(db.getCanonicalAddressId("+1 222-333-4444"));
61+
62+
assertThat(db.getCanonicalAddressId("1 222-333-4444"))
63+
.isEqualTo(db.getCanonicalAddressId("222-333-4444"));
64+
assertThat(db.getCanonicalAddressId("1 (222) 333-4444"))
65+
.isEqualTo(db.getCanonicalAddressId("222-333-4444"));
66+
assertThat(db.getCanonicalAddressId("+12223334444"))
67+
.isEqualTo(db.getCanonicalAddressId("222-333-4444"));
68+
assertThat(db.getCanonicalAddressId("+1 (222) 333.4444"))
69+
.isEqualTo(db.getCanonicalAddressId("222-333-4444"));
70+
assertThat(db.getCanonicalAddressId("+49 (222) 333.4444"))
71+
.isEqualTo(db.getCanonicalAddressId("222-333-4444"));
72+
73+
}
74+
75+
public void testEmailAddresses() throws Exception {
76+
final long emailId = db.getCanonicalAddressId(EMAIL);
77+
final long similarEmailId = db.getCanonicalAddressId(SIMILAR_EMAIL);
78+
79+
assertThat(emailId).isNotEqualTo(similarEmailId);
80+
81+
assertThat(db.getAddressFromId(emailId)).isEqualTo(EMAIL);
82+
assertThat(db.getAddressFromId(similarEmailId)).isEqualTo(SIMILAR_EMAIL);
83+
}
84+
85+
public void testGroups() throws Exception {
86+
final long groupId = db.getCanonicalAddressId(GROUP);
87+
final long similarGroupId = db.getCanonicalAddressId(SIMILAR_GROUP);
88+
89+
assertThat(groupId).isNotEqualTo(similarGroupId);
90+
91+
assertThat(db.getAddressFromId(groupId)).isEqualTo(GROUP);
92+
assertThat(db.getAddressFromId(similarGroupId)).isEqualTo(SIMILAR_GROUP);
93+
}
94+
95+
public void testAlpha() throws Exception {
96+
final long id = db.getCanonicalAddressId(ALPHA);
97+
final long similarId = db.getCanonicalAddressId(SIMILAR_ALPHA);
98+
99+
assertThat(id).isNotEqualTo(similarId);
100+
101+
assertThat(db.getAddressFromId(id)).isEqualTo(ALPHA);
102+
assertThat(db.getAddressFromId(similarId)).isEqualTo(SIMILAR_ALPHA);
103+
}
104+
105+
public void testIsNumber() throws Exception {
106+
assertThat(CanonicalAddressDatabase.isNumberAddress("+495556666777")).isTrue();
107+
assertThat(CanonicalAddressDatabase.isNumberAddress("(222) 333-4444")).isTrue();
108+
assertThat(CanonicalAddressDatabase.isNumberAddress("1 (222) 333-4444")).isTrue();
109+
assertThat(CanonicalAddressDatabase.isNumberAddress("T-Mobile123")).isTrue();
110+
assertThat(CanonicalAddressDatabase.isNumberAddress("333-4444")).isTrue();
111+
assertThat(CanonicalAddressDatabase.isNumberAddress("12345")).isTrue();
112+
assertThat(CanonicalAddressDatabase.isNumberAddress("T-Mobile")).isFalse();
113+
assertThat(CanonicalAddressDatabase.isNumberAddress("T-Mobile1")).isFalse();
114+
assertThat(CanonicalAddressDatabase.isNumberAddress("Wherever bank")).isFalse();
115+
assertThat(CanonicalAddressDatabase.isNumberAddress("__textsecure_group__!afafafafafaf")).isFalse();
116+
assertThat(CanonicalAddressDatabase.isNumberAddress("[email protected]")).isFalse();
117+
}
118+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.thoughtcrime.securesms.util;
2+
3+
import android.test.AndroidTestCase;
4+
5+
import junit.framework.AssertionFailedError;
6+
7+
import org.whispersystems.textsecure.util.InvalidNumberException;
8+
import org.whispersystems.textsecure.util.PhoneNumberFormatter;
9+
import static org.fest.assertions.api.Assertions.assertThat;
10+
11+
public class PhoneNumberFormatterTest extends AndroidTestCase {
12+
private static final String LOCAL_NUMBER = "+15555555555";
13+
14+
public void testFormatNumberE164() throws Exception, InvalidNumberException {
15+
assertThat(PhoneNumberFormatter.formatNumber("(555) 555-5555", LOCAL_NUMBER)).isEqualTo(LOCAL_NUMBER);
16+
assertThat(PhoneNumberFormatter.formatNumber("555-5555", LOCAL_NUMBER)).isEqualTo(LOCAL_NUMBER);
17+
assertThat(PhoneNumberFormatter.formatNumber("(123) 555-5555", LOCAL_NUMBER)).isNotEqualTo(LOCAL_NUMBER);
18+
}
19+
20+
public void testFormatNumberEmail() throws Exception {
21+
try {
22+
PhoneNumberFormatter.formatNumber("[email protected]", LOCAL_NUMBER);
23+
throw new AssertionFailedError("should have thrown on email");
24+
} catch (InvalidNumberException ine) {
25+
// success
26+
}
27+
}
28+
29+
@Override
30+
public void setUp() throws Exception {
31+
super.setUp();
32+
}
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.thoughtcrime.securesms.util;
2+
3+
import android.test.AndroidTestCase;
4+
5+
import static org.fest.assertions.api.Assertions.assertThat;
6+
7+
public class UtilTest extends AndroidTestCase {
8+
9+
}

build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ dependencies {
2727
compile 'com.google.android.gcm:gcm-client:1.0.2'
2828
compile 'se.emilsjolander:stickylistheaders:2.2.0'
2929

30+
androidTestCompile 'com.squareup:fest-android:1.0.8'
31+
3032
compile project(':library')
3133
}
3234

@@ -38,7 +40,7 @@ dependencyVerification {
3840
'se.emilsjolander:stickylistheaders:89146b46c96fea0e40200474a2625cda10fe94891e4128f53cdb42375091b9b6',
3941
'com.google.protobuf:protobuf-java:ad9769a22989e688a46af4d3accc348cc501ced22118033230542bc916e33f0b',
4042
'com.madgag:sc-light-jdk15on:931f39d351429fb96c2f749e7ecb1a256a8ebbf5edca7995c9cc085b94d1841d',
41-
'com.googlecode.libphonenumber:libphonenumber:c2cffa5c2ccb8fd5fc4b432aa7d1ee27482860c9751035a25c8ffa2375274fcf',
43+
'com.googlecode.libphonenumber:libphonenumber:eba17eae81dd622ea89a00a3a8c025b2f25d342e0d9644c5b62e16f15687c3ab',
4244
'org.whispersystems:gson:08f4f7498455d1539c9233e5aac18e9b1805815ef29221572996508eb512fe51',
4345
]
4446
}
@@ -63,6 +65,12 @@ android {
6365
res.srcDirs = ['res']
6466
assets.srcDirs = ['assets']
6567
}
68+
androidTest {
69+
java.srcDirs = ['androidTest']
70+
resources.srcDirs = ['androidTest']
71+
aidl.srcDirs = ['androidTest']
72+
renderscript.srcDirs = ['androidTest']
73+
}
6674
}
6775
}
6876

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ repositories {
2121
dependencies {
2222
compile 'com.google.protobuf:protobuf-java:2.4.1'
2323
compile 'com.madgag:sc-light-jdk15on:1.47.0.2'
24-
compile 'com.googlecode.libphonenumber:libphonenumber:5.3'
24+
compile 'com.googlecode.libphonenumber:libphonenumber:6.1'
2525
compile 'org.whispersystems:gson:2.2.4'
2626
}
2727

src/org/thoughtcrime/securesms/ConversationListActivity.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ public class ConversationListActivity extends PassphraseRequiredSherlockFragment
5050
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
5151

5252
private ConversationListFragment fragment;
53-
private MasterSecret masterSecret;
54-
private DrawerLayout drawerLayout;
55-
private DrawerToggle drawerToggle;
56-
private ListView drawerList;
53+
private MasterSecret masterSecret;
54+
private DrawerLayout drawerLayout;
55+
private DrawerToggle drawerToggle;
56+
private ListView drawerList;
57+
private ContentObserver observer;
5758

5859
@Override
5960
public void onCreate(Bundle icicle) {
@@ -92,6 +93,7 @@ public void onResume() {
9293
public void onDestroy() {
9394
Log.w("ConversationListActivity", "onDestroy...");
9495
MemoryCleaner.clean(masterSecret);
96+
if (observer != null) getContentResolver().unregisterContentObserver(observer);
9597
super.onDestroy();
9698
}
9799

@@ -254,11 +256,19 @@ private void initializeNavigationDrawer() {
254256
}
255257

256258
private void initializeContactUpdatesReceiver() {
257-
ContentObserver observer = new ContentObserver(null) {
259+
observer = new ContentObserver(null) {
258260
@Override
259261
public void onChange(boolean selfChange) {
260262
super.onChange(selfChange);
263+
Log.w("ConversationListActivity", "detected android contact data changed, refreshing cache");
264+
// TODO only clear updated recipients from cache
261265
RecipientFactory.clearCache();
266+
ConversationListActivity.this.runOnUiThread(new Runnable() {
267+
@Override
268+
public void run() {
269+
((ConversationListAdapter)fragment.getListAdapter()).notifyDataSetChanged();
270+
}
271+
});
262272
}
263273
};
264274

0 commit comments

Comments
 (0)