From 5d15b8dc816909c51a0ed2fbab32ef00497e73d6 Mon Sep 17 00:00:00 2001 From: Markus Friedli Date: Sat, 21 Sep 2024 14:53:14 +0200 Subject: [PATCH] Allow numeric short codes from phone number fields of contacts --- README.md | 13 ++++++++----- .../service/OperationsCenterContactService.java | 3 ++- .../pikettassist/service/dao/ContactDao.java | 13 ++++++++++--- .../pikettassist/service/dao/ContactDaoTest.java | 6 +++--- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f99bae10..89b3c8b4 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/app/src/main/java/com/github/frimtec/android/pikettassist/service/OperationsCenterContactService.java b/app/src/main/java/com/github/frimtec/android/pikettassist/service/OperationsCenterContactService.java index ec05ce4b..ceebf143 100644 --- a/app/src/main/java/com/github/frimtec/android/pikettassist/service/OperationsCenterContactService.java +++ b/app/src/main/java/com/github/frimtec/android/pikettassist/service/OperationsCenterContactService.java @@ -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); } diff --git a/app/src/main/java/com/github/frimtec/android/pikettassist/service/dao/ContactDao.java b/app/src/main/java/com/github/frimtec/android/pikettassist/service/dao/ContactDao.java index cdba5c14..936d0035 100644 --- a/app/src/main/java/com/github/frimtec/android/pikettassist/service/dao/ContactDao.java +++ b/app/src/main/java/com/github/frimtec/android/pikettassist/service/dao/ContactDao.java @@ -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; @@ -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; @@ -96,10 +98,10 @@ private String in(Set elements) { return elements.stream().map(element -> String.format("'%s'", element)).collect(joining(",")); } - public Set lookupContactIdsByPhoneNumber(String phoneNumber) { + public Set 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 contactIds = new HashSet<>(); @@ -138,7 +140,12 @@ public Set 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.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()); } diff --git a/app/src/test/java/com/github/frimtec/android/pikettassist/service/dao/ContactDaoTest.java b/app/src/test/java/com/github/frimtec/android/pikettassist/service/dao/ContactDaoTest.java index b3f49c64..a26196ce 100644 --- a/app/src/test/java/com/github/frimtec/android/pikettassist/service/dao/ContactDaoTest.java +++ b/app/src/test/java/com/github/frimtec/android/pikettassist/service/dao/ContactDaoTest.java @@ -149,7 +149,7 @@ void lookupContactIdsByPhoneNumber() { new String[]{"number"}, null)) .thenReturn(cursor); - Set contactIds = dao.lookupContactIdsByPhoneNumber("number"); + Set contactIds = dao.lookupContactIdsByPhoneNumber("number", true); assertThat(contactIds).isEqualTo(new HashSet<>(Arrays.asList( 12L, 15L @@ -170,7 +170,7 @@ void lookupContactIdsByPhoneNumberForEmptyCursorReturnsEmpty() { new String[]{"number"}, null)) .thenReturn(cursor); - Set contactIds = dao.lookupContactIdsByPhoneNumber("number"); + Set contactIds = dao.lookupContactIdsByPhoneNumber("number", true); assertThat(contactIds).isEmpty(); } @@ -186,7 +186,7 @@ void lookupContactIdsByPhoneNumberForNullCursorReturnsEmpty() { new String[]{"number"}, null)) .thenReturn(null); - Set contactIds = dao.lookupContactIdsByPhoneNumber("number"); + Set contactIds = dao.lookupContactIdsByPhoneNumber("number", true); assertThat(contactIds).isEmpty(); }