Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow numeric short codes from phone number fields of contacts #589

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading