Skip to content

Commit

Permalink
Pretty-print phone numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal authored and alan-signal committed Nov 4, 2020
1 parent 3dc1614 commit 9e5156a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.RecipientDatabase;
import org.thoughtcrime.securesms.phonenumbers.PhoneNumberFormatter;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.libsignal.util.Pair;
Expand Down Expand Up @@ -64,6 +65,10 @@ public class ContactRepository {
String phone = cursor.getString(cursor.getColumnIndexOrThrow(RecipientDatabase.PHONE));
String email = cursor.getString(cursor.getColumnIndexOrThrow(RecipientDatabase.EMAIL));

if (phone != null) {
phone = PhoneNumberFormatter.prettyPrint(phone);
}

return Util.getFirstNonEmpty(phone, email);
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
import org.thoughtcrime.securesms.mms.PartAuthority;
import org.thoughtcrime.securesms.mms.Slide;
import org.thoughtcrime.securesms.permissions.Permissions;
import org.thoughtcrime.securesms.phonenumbers.PhoneNumberFormatter;
import org.thoughtcrime.securesms.profiles.UnknownSenderView;
import org.thoughtcrime.securesms.providers.BlobProvider;
import org.thoughtcrime.securesms.reactions.ReactionsBottomSheetDialogFragment;
Expand Down Expand Up @@ -433,7 +434,7 @@ private static void presentMessageRequestProfileView(@NonNull Context context, @
} else if (isSelf) {
conversationBanner.setSubtitle(context.getString(R.string.ConversationFragment__you_can_add_notes_for_yourself_in_this_conversation));
} else {
String subtitle = recipient.getE164().orNull();
String subtitle = recipient.getE164().transform(PhoneNumberFormatter::prettyPrint).orNull();

if (subtitle == null || subtitle.equals(title)) {
conversationBanner.hideSubtitle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.ShortNumberInfo;

import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.groups.GroupId;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
Expand Down Expand Up @@ -46,7 +47,6 @@ public class PhoneNumberFormatter {
private final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
private final Pattern ALPHA_PATTERN = Pattern.compile("[a-zA-Z]");


public static @NonNull PhoneNumberFormatter get(Context context) {
String localNumber = TextSecurePreferences.getLocalNumber(context);

Expand Down Expand Up @@ -81,6 +81,25 @@ public class PhoneNumberFormatter {
this.localCountryCode = localCountryCode;
}

public static @NonNull String prettyPrint(@NonNull String e164) {
return get(ApplicationDependencies.getApplication()).prettyPrintFormat(e164);
}

public @NonNull String prettyPrintFormat(@NonNull String e164) {
try {
Phonenumber.PhoneNumber parsedNumber = phoneNumberUtil.parse(e164, localCountryCode);

if (localNumber.isPresent() && localNumber.get().countryCode == parsedNumber.getCountryCode()) {
return phoneNumberUtil.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
} else {
return phoneNumberUtil.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
}
} catch (NumberParseException e) {
Log.w(TAG, "Failed to format number.");
return e164;
}
}

public String format(@Nullable String number) {
if (number == null) return "Unknown";
if (GroupId.isEncodedGroup(number)) return number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public boolean hasAUserSetDisplayName(@NonNull Context context) {
public @NonNull String getDisplayName(@NonNull Context context) {
String name = Util.getFirstNonEmpty(getName(context),
getProfileName().toString(),
e164,
PhoneNumberFormatter.prettyPrint(e164),
email,
context.getString(R.string.Recipient_unknown));

Expand All @@ -428,7 +428,7 @@ public boolean hasAUserSetDisplayName(@NonNull Context context) {
public @NonNull String getDisplayNameOrUsername(@NonNull Context context) {
String name = Util.getFirstNonEmpty(getName(context),
getProfileName().toString(),
e164,
PhoneNumberFormatter.prettyPrint(e164),
email,
username,
context.getString(R.string.Recipient_unknown));
Expand All @@ -439,7 +439,7 @@ public boolean hasAUserSetDisplayName(@NonNull Context context) {
public @NonNull String getMentionDisplayName(@NonNull Context context) {
String name = Util.getFirstNonEmpty(isSelf ? getProfileName().toString() : getName(context),
isSelf ? getName(context) : getProfileName().toString(),
e164,
PhoneNumberFormatter.prettyPrint(e164),
email,
context.getString(R.string.Recipient_unknown));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.thoughtcrime.securesms.contacts.avatars.FallbackContactPhoto;
import org.thoughtcrime.securesms.contacts.avatars.FallbackPhoto80dp;
import org.thoughtcrime.securesms.groups.GroupId;
import org.thoughtcrime.securesms.phonenumbers.PhoneNumberFormatter;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientExporter;
import org.thoughtcrime.securesms.recipients.RecipientId;
Expand Down Expand Up @@ -157,7 +158,7 @@ public void onViewCreated(@NonNull View fragmentView, @Nullable Bundle savedInst
}

String usernameNumberString = recipient.hasAUserSetDisplayName(requireContext()) && !recipient.isSelf()
? recipient.getSmsAddress().or("").trim()
? recipient.getSmsAddress().transform(PhoneNumberFormatter::prettyPrint).or("").trim()
: "";
usernameNumber.setText(usernameNumberString);
usernameNumber.setVisibility(TextUtils.isEmpty(usernameNumberString) ? View.GONE : View.VISIBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.thoughtcrime.securesms.groups.ui.addtogroup.AddToGroupsActivity;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
import org.thoughtcrime.securesms.phonenumbers.PhoneNumberFormatter;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.recipients.RecipientUtil;
Expand Down Expand Up @@ -124,7 +125,7 @@ private ManageRecipientViewModel(@NonNull Context context, @NonNull ManageRecipi

private static @NonNull String getDisplaySubtitle(@NonNull Recipient recipient, @NonNull Context context) {
if (!recipient.isSelf() && recipient.hasAUserSetDisplayName(context)) {
return recipient.getSmsAddress().or("").trim();
return recipient.getSmsAddress().transform(PhoneNumberFormatter::prettyPrint).or("").trim();
} else {
return "";
}
Expand Down

0 comments on commit 9e5156a

Please sign in to comment.