Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
138b7ea
commit 3cc2cd0
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
app/src/main/java/org/thoughtcrime/securesms/util/SignalMeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.thoughtcrime.securesms.util; | ||
|
||
import android.content.Context; | ||
import android.telephony.PhoneNumberUtils; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import com.google.i18n.phonenumbers.PhoneNumberUtil; | ||
|
||
import org.thoughtcrime.securesms.phonenumbers.PhoneNumberFormatter; | ||
|
||
import java.util.Locale; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
class SignalMeUtil { | ||
private static final String HOST = "signal.me"; | ||
private static final Pattern HOST_PATTERN = Pattern.compile("^(https|sgnl)://" + HOST + "/#p/(\\+[0-9]+)$"); | ||
|
||
/** | ||
* If this is a valid signal.me link and has a valid e164, it will return the e164. Otherwise, it will return null. | ||
*/ | ||
public static @Nullable String parseE164FromLink(@NonNull Context context, @Nullable String link) { | ||
if (Util.isEmpty(link)) { | ||
return null; | ||
} | ||
|
||
Matcher matcher = HOST_PATTERN.matcher(link); | ||
|
||
if (matcher.matches()) { | ||
String e164 = matcher.group(2); | ||
|
||
if (PhoneNumberUtil.getInstance().isPossibleNumber(e164, Locale.getDefault().getCountry())) { | ||
return PhoneNumberFormatter.get(context).format(e164); | ||
} else { | ||
return null; | ||
} | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
app/src/test/java/org/thoughtcrime/securesms/util/SignalMeUtilText_parseE164FromLink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.thoughtcrime.securesms.util; | ||
|
||
import android.app.Application; | ||
|
||
import androidx.test.core.app.ApplicationProvider; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.ParameterizedRobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
|
||
@RunWith(ParameterizedRobolectricTestRunner.class) | ||
@Config(manifest = Config.NONE, application = Application.class) | ||
public class SignalMeUtilText_parseE164FromLink { | ||
|
||
private final String input; | ||
private final String output; | ||
|
||
@ParameterizedRobolectricTestRunner.Parameters | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList(new Object[][]{ | ||
{ "https://signal.me/#p/+15555555555", "+15555555555" }, | ||
{ "https://signal.me/#p/5555555555", null }, | ||
{ "https://signal.me", null }, | ||
{ "https://signal.me/#p/", null }, | ||
{ "signal.me/#p/+15555555555", null }, | ||
{ "sgnl://signal.me/#p/+15555555555", "+15555555555" }, | ||
{ "sgnl://signal.me/#p/5555555555", null }, | ||
{ "sgnl://signal.me", null }, | ||
{ "sgnl://signal.me/#p/", null }, | ||
{ "", null }, | ||
{ null, null } | ||
}); | ||
} | ||
|
||
public SignalMeUtilText_parseE164FromLink(String input, String output) { | ||
this.input = input; | ||
this.output = output; | ||
} | ||
|
||
@Test | ||
public void parse() { | ||
assertEquals(output, SignalMeUtil.parseE164FromLink(ApplicationProvider.getApplicationContext(), input)); | ||
} | ||
} |
@greyson-signal copy-pasted comment ;)