Skip to content

Commit

Permalink
Allow numeric short codes from phone number fields of contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
frimtec committed Sep 21, 2024
1 parent caaf42f commit d9a2f6d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,14 @@ Numeric short codes are currently only supported for the following countries:

Alphanumeric short codes are supported in any country.

As contacts in Android cannot contain such short code phone numbers,
they can be configured in the contact field "Company".
It the operations centers uses several short code phone numbers, they can be comma separated in the
contacts company field. Any regular phone numbers stored in the contacts field "Company" are
ignored.
Numeric short codes can be defined as normal phone numbers in the operations centers contact.
Ensure that no separator characters (like '-') are used.

As contacts in Android cannot contain alphanumeric short code phone numbers, they can be configured
in the contact field "Company".
If the operations centers uses several alphanumeric short code phone numbers, they can be comma
separated in the contacts "Company" field.
Any regular phone numbers stored in the contacts field "Company" will be ignored.

![Operations center contact with two alphanumeric short code SMS numbers](images/Contacts-with-alphanumeric-short-codes.png)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public boolean isContactsPhoneNumber(Contact contact, String number) {
return false;
}
ContactDao contactDao = getContactDao();
return contactDao.lookupContactIdsByPhoneNumber(number).contains(contact.reference().id()) ||
return contactDao.lookupContactIdsByPhoneNumber(number, true).contains(contact.reference().id()) ||
contactDao.lookupContactIdsByPhoneNumber(number, false).contains(contact.reference().id()) ||
contactDao.getShortCodesFromContact(contact).contains(number);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.frimtec.android.pikettassist.service.dao;

import static com.github.frimtec.android.securesmsproxyapi.utility.PhoneNumberType.NUMERIC_SHORT_CODE;
import static com.github.frimtec.android.securesmsproxyapi.utility.PhoneNumberType.fromNumber;
import static java.util.stream.Collectors.joining;

Expand All @@ -14,6 +15,7 @@
import com.github.frimtec.android.pikettassist.domain.Contact;
import com.github.frimtec.android.pikettassist.domain.ContactPerson;
import com.github.frimtec.android.pikettassist.domain.ContactReference;
import com.github.frimtec.android.securesmsproxyapi.utility.PhoneNumberType;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -96,10 +98,10 @@ private String in(Set<String> elements) {
return elements.stream().map(element -> String.format("'%s'", element)).collect(joining(","));
}

public Set<Long> lookupContactIdsByPhoneNumber(String phoneNumber) {
public Set<Long> lookupContactIdsByPhoneNumber(String phoneNumber, boolean normalized) {
try (Cursor cursor = this.contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID},
ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER + " = ?",
(normalized ? ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER : ContactsContract.CommonDataKinds.Phone.NUMBER) + " = ?",
new String[]{phoneNumber}, null)) {
if (cursor != null && cursor.moveToFirst()) {
Set<Long> contactIds = new HashSet<>();
Expand Down Expand Up @@ -138,7 +140,12 @@ public Set<String> getPhoneNumbers(Contact contact) {
phoneNumbers.add(normalizedNumber);
} else {
int columnIndexNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
Log.e(TAG, "Skipping phone number as normalized number is null for number: " + (columnIndexNumber >= 0 ? cursor.getString(columnIndexNumber) : "???"));
String number = columnIndexNumber >= 0 ? cursor.getString(columnIndexNumber) : "NA";
if (number != null && number.matches("\\d+") && PhoneNumberType.fromNumber(number, context) == NUMERIC_SHORT_CODE) {
phoneNumbers.add(number);
} else {
Log.e(TAG, "Skipping phone number as normalized number is null for number: " + number);
}
}
} while (cursor.moveToNext());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void lookupContactIdsByPhoneNumber() {
new String[]{"number"}, null))
.thenReturn(cursor);

Set<Long> contactIds = dao.lookupContactIdsByPhoneNumber("number");
Set<Long> contactIds = dao.lookupContactIdsByPhoneNumber("number", true);
assertThat(contactIds).isEqualTo(new HashSet<>(Arrays.asList(
12L,
15L
Expand All @@ -170,7 +170,7 @@ void lookupContactIdsByPhoneNumberForEmptyCursorReturnsEmpty() {
new String[]{"number"}, null))
.thenReturn(cursor);

Set<Long> contactIds = dao.lookupContactIdsByPhoneNumber("number");
Set<Long> contactIds = dao.lookupContactIdsByPhoneNumber("number", true);
assertThat(contactIds).isEmpty();
}

Expand All @@ -186,7 +186,7 @@ void lookupContactIdsByPhoneNumberForNullCursorReturnsEmpty() {
new String[]{"number"}, null))
.thenReturn(null);

Set<Long> contactIds = dao.lookupContactIdsByPhoneNumber("number");
Set<Long> contactIds = dao.lookupContactIdsByPhoneNumber("number", true);
assertThat(contactIds).isEmpty();
}

Expand Down

0 comments on commit d9a2f6d

Please sign in to comment.