Skip to content

Commit

Permalink
#49 Fix missing + in SMS number if not sent by carrier
Browse files Browse the repository at this point in the history
  • Loading branch information
frimtec committed Mar 10, 2021
1 parent 2cd6472 commit 66aa882
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public List<Sms> getSmsFromIntent(Intent intent) {
for (Object pdu : pdus) {
SmsMessage message = this.pduDecoder.apply((byte[]) pdu, format);
String number = message.getOriginatingAddress();
// some carriers do not send the "+"
if (number != null && isDigitsOnly(number)) {
number = "+" + number;
}
String text = smsTextByNumber.getOrDefault(number, "");
smsTextByNumber.put(number, text + message.getMessageBody());
}
Expand All @@ -58,4 +62,15 @@ public void send(Sms sms) {
SmsManager smsManager = sms.getSubscriptionId() == null ? this.defaultSmsManager : this.subscriptionSmsManagerFactory.apply(sms.getSubscriptionId());
smsManager.sendTextMessage(sms.getNumber(), null, sms.getText(), null, null);
}

private static boolean isDigitsOnly(CharSequence str) {
final int len = str.length();
for (int cp, i = 0; i < len; i += Character.charCount(cp)) {
cp = Character.codePointAt(str, i);
if (!Character.isDigit(cp)) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ void getSmsFromIntentWithNullPdusReturnsEmptyList() {
assertThat(sms.size()).isEqualTo(0);
}

@Test
void getSmsFromIntentWithInternationalNumber() {
SmsHelper smsHelper = new SmsHelper((bytes, s) -> createSmsMessage("+4179000000", "text"), null, null);
List<Sms> smsList = smsHelper.getSmsFromIntent(createIntent(createBundle(1, new Object[]{"pdu1".getBytes()})));
assertThat(smsList.size()).isEqualTo(1);
assertThat(smsList.get(0).toString()).isEqualTo("Sms{number='+4179000000', text='text', subscriptionId='1'}");
}

@Test
void getSmsFromIntentWithInternationalNumberMissingPlus() {
SmsHelper smsHelper = new SmsHelper((bytes, s) -> createSmsMessage("4179000000", "text"), null, null);
List<Sms> smsList = smsHelper.getSmsFromIntent(createIntent(createBundle(1, new Object[]{"pdu1".getBytes()})));
assertThat(smsList.size()).isEqualTo(1);
assertThat(smsList.get(0).toString()).isEqualTo("Sms{number='+4179000000', text='text', subscriptionId='1'}");
}

@Test
void getSmsFromIntentWithSubscriptionReturnsSmsWithSubscription() {
SmsHelper smsHelper = new SmsHelper((bytes, s) -> createSmsMessage("number", "text"), null, null);
Expand Down

0 comments on commit 66aa882

Please sign in to comment.