Improve and centralize e164 utils.

This commit is contained in:
Greyson Parrelli
2025-03-03 10:42:21 -05:00
parent 0fdcc1c027
commit 9c473fb570
99 changed files with 748 additions and 1826 deletions

View File

@@ -310,7 +310,7 @@ public class CommunicationActions {
* If the url is a signal.me link it will handle it.
*/
public static void handlePotentialSignalMeUrl(@NonNull FragmentActivity activity, @NonNull String potentialUrl) {
String e164 = SignalMeUtil.parseE164FromLink(activity, potentialUrl);
String e164 = SignalMeUtil.parseE164FromLink(potentialUrl);
UsernameLinkComponents username = UsernameRepository.parseLink(potentialUrl);
if (e164 != null) {
@@ -426,7 +426,10 @@ public class CommunicationActions {
SimpleProgressDialog.DismissibleDialog dialog = SimpleProgressDialog.showDelayed(activity, 500, 500);
SimpleTask.run(() -> {
Recipient recipient = Recipient.external(activity, e164);
Recipient recipient = Recipient.external(e164);
if (recipient == null) {
return null;
}
if (!recipient.isRegistered() || !recipient.getHasServiceId()) {
try {
@@ -441,7 +444,7 @@ public class CommunicationActions {
}, recipient -> {
dialog.dismiss();
if (recipient.isRegistered() && recipient.getHasServiceId()) {
if (recipient != null && recipient.isRegistered() && recipient.getHasServiceId()) {
startConversation(activity, recipient, null);
} else {
new MaterialAlertDialogBuilder(activity)

View File

@@ -7,7 +7,6 @@ import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import org.signal.core.util.BidiUtil;
import org.signal.core.util.StringUtil;
import org.signal.core.util.logging.Log;
import org.signal.libsignal.zkgroup.InvalidInputException;
import org.signal.libsignal.zkgroup.groups.GroupMasterKey;

View File

@@ -0,0 +1,74 @@
/*
* Copyright 2025 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.thoughtcrime.securesms.util
import org.signal.core.util.E164Util
import org.thoughtcrime.securesms.dependencies.AppDependencies
import org.thoughtcrime.securesms.keyvalue.SignalStore
/**
* A wrapper around [E164Util] that automatically handles fetching our own number and caching formatters.
*/
object SignalE164Util {
private val cachedFormatters: MutableMap<String, E164Util.Formatter> = LRUCache(2)
private val defaultFormatter: E164Util.Formatter by lazy {
E164Util.Formatter(
localNumber = null,
localAreaCode = null,
localRegionCode = Util.getSimCountryIso(AppDependencies.application).orElse("US")
)
}
/**
* Formats the number for human-readable display. e.g. "(555) 555-5555"
*/
@JvmStatic
fun prettyPrint(input: String): String {
return getFormatter().prettyPrint(input)
}
/**
* Returns the country code for the local number, if present. Otherwise, it returns 0.
*/
fun getLocalCountryCode(): Int {
return getFormatter().localNumber?.countryCode ?: 0
}
/**
* Formats the number as an E164, or null if the number cannot be reasonably interpreted as a phone number.
* This does not check if the number is *valid* for a given region. Instead, it's very lenient and just
* does it's best to interpret the input string as a number that could be put into the E164 format.
*
* Note that shortcodes will not have leading '+' signs.
*
* In other words, if this method returns null, you likely do not have anything that could be considered
* a phone number.
*/
@JvmStatic
fun formatAsE164(input: String): String? {
return getFormatter().formatAsE164(input)
}
private fun getFormatter(): E164Util.Formatter {
val localNumber = SignalStore.account.e164 ?: return defaultFormatter
val formatter = cachedFormatters[localNumber]
if (formatter != null) {
return formatter
}
synchronized(cachedFormatters) {
val formatter = cachedFormatters[localNumber]
if (formatter != null) {
return formatter
}
val newFormatter = E164Util.createFormatterForE164(localNumber)
cachedFormatters[localNumber] = newFormatter
return newFormatter
}
}
}

View File

@@ -1,8 +1,6 @@
package org.thoughtcrime.securesms.util
import android.content.Context
import com.google.i18n.phonenumbers.PhoneNumberUtil
import org.thoughtcrime.securesms.phonenumbers.PhoneNumberFormatter
import java.util.Locale
internal object SignalMeUtil {
@@ -12,7 +10,7 @@ internal object SignalMeUtil {
* If this is a valid signal.me link and has a valid e164, it will return the e164. Otherwise, it will return null.
*/
@JvmStatic
fun parseE164FromLink(context: Context, link: String?): String? {
fun parseE164FromLink(link: String?): String? {
if (link.isNullOrBlank()) {
return null
}
@@ -21,7 +19,7 @@ internal object SignalMeUtil {
val e164: String = match.groups[2]?.value ?: return@let null
if (PhoneNumberUtil.getInstance().isPossibleNumber(e164, Locale.getDefault().country)) {
PhoneNumberFormatter.get(context).format(e164)
SignalE164Util.formatAsE164(e164)
} else {
null
}