mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-26 04:59:11 +00:00
Use SIM country code when registered number is unavailable
Convert directory operations to Addresses Fixes #6845 // FREEBIE
This commit is contained in:
@@ -7,6 +7,7 @@ import android.os.Parcelable;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.i18n.phonenumbers.NumberParseException;
|
||||
@@ -20,7 +21,6 @@ import org.thoughtcrime.securesms.util.NumberUtil;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
@@ -60,7 +60,17 @@ public class Address implements Parcelable, Comparable<Address> {
|
||||
}
|
||||
|
||||
public static Address fromExternal(@NonNull Context context, @Nullable String external) {
|
||||
return new Address(new ExternalAddressFormatter(TextSecurePreferences.getLocalNumber(context)).format(external));
|
||||
String localNumber = TextSecurePreferences.getLocalNumber(context);
|
||||
|
||||
ExternalAddressFormatter formatter;
|
||||
|
||||
if (!TextUtils.isEmpty(localNumber)) {
|
||||
formatter = new ExternalAddressFormatter(localNumber);
|
||||
} else {
|
||||
formatter = new ExternalAddressFormatter(Util.getSimCountryIso(context).or("US"), true);
|
||||
}
|
||||
|
||||
return new Address(formatter.format(external));
|
||||
}
|
||||
|
||||
public static @NonNull List<Address> fromSerializedList(@NonNull String serialized, char delimiter) {
|
||||
@@ -171,23 +181,28 @@ public class Address implements Parcelable, Comparable<Address> {
|
||||
add("AC");
|
||||
}};
|
||||
|
||||
private final Phonenumber.PhoneNumber localNumber;
|
||||
private final String localNumberString;
|
||||
private final String localCountryCode;
|
||||
private final String localNumberString;
|
||||
private final String localCountryCode;
|
||||
|
||||
private final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
|
||||
private final Pattern ALPHA_PATTERN = Pattern.compile("[a-zA-Z]");
|
||||
|
||||
public ExternalAddressFormatter(String localNumber) {
|
||||
ExternalAddressFormatter(@NonNull String localNumberString) {
|
||||
try {
|
||||
this.localNumberString = localNumber;
|
||||
this.localNumber = phoneNumberUtil.parse(localNumber, null);
|
||||
this.localCountryCode = phoneNumberUtil.getRegionCodeForNumber(this.localNumber);
|
||||
Phonenumber.PhoneNumber localNumber = phoneNumberUtil.parse(localNumberString, null);
|
||||
|
||||
this.localNumberString = localNumberString;
|
||||
this.localCountryCode = phoneNumberUtil.getRegionCodeForNumber(localNumber);
|
||||
} catch (NumberParseException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
ExternalAddressFormatter(@NonNull String localCountryCode, boolean countryCode) {
|
||||
this.localNumberString = "";
|
||||
this.localCountryCode = localCountryCode;
|
||||
}
|
||||
|
||||
public String format(@Nullable String number) {
|
||||
if (number == null) return "Unknown";
|
||||
if (number.startsWith("__textsecure_group__!")) return number;
|
||||
|
||||
@@ -8,11 +8,10 @@ import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract.CommonDataKinds.Phone;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import org.whispersystems.signalservice.api.push.ContactTokenDetails;
|
||||
import org.whispersystems.signalservice.api.util.InvalidNumberException;
|
||||
import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -173,7 +172,7 @@ public class TextSecureDirectory {
|
||||
db.replace(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
public void setNumbers(List<ContactTokenDetails> activeTokens, Collection<String> inactiveTokens) {
|
||||
public void setNumbers(List<ContactTokenDetails> activeTokens, Collection<Address> inactiveAddresses) {
|
||||
long timestamp = System.currentTimeMillis();
|
||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||
db.beginTransaction();
|
||||
@@ -191,9 +190,9 @@ public class TextSecureDirectory {
|
||||
db.replace(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
for (String token : inactiveTokens) {
|
||||
for (Address address : inactiveAddresses) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(NUMBER, token);
|
||||
values.put(NUMBER, address.serialize());
|
||||
values.put(REGISTERED, 0);
|
||||
values.put(TIMESTAMP, timestamp);
|
||||
db.replace(TABLE_NAME, null, values);
|
||||
@@ -205,23 +204,18 @@ public class TextSecureDirectory {
|
||||
}
|
||||
}
|
||||
|
||||
public Set<String> getPushEligibleContactNumbers(String localNumber) {
|
||||
final Uri uri = Phone.CONTENT_URI;
|
||||
final Set<String> results = new HashSet<>();
|
||||
Cursor cursor = null;
|
||||
public Set<Address> getPushEligibleContactNumbers() {
|
||||
final Uri uri = Phone.CONTENT_URI;
|
||||
final Set<Address> results = new HashSet<>();
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
cursor = context.getContentResolver().query(uri, new String[] {Phone.NUMBER}, null, null, null);
|
||||
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
final String rawNumber = cursor.getString(0);
|
||||
if (rawNumber != null) {
|
||||
try {
|
||||
final String e164Number = PhoneNumberFormatter.formatNumber(rawNumber, localNumber);
|
||||
results.add(e164Number);
|
||||
} catch (InvalidNumberException e) {
|
||||
Log.w("Directory", "Invalid number: " + rawNumber);
|
||||
}
|
||||
if (!TextUtils.isEmpty(rawNumber)) {
|
||||
results.add(Address.fromExternal(context, rawNumber));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,7 +228,7 @@ public class TextSecureDirectory {
|
||||
null, null, null, null, null);
|
||||
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
results.add(cursor.getString(0));
|
||||
results.add(Address.fromSerialized(cursor.getString(0)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user