-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support numeric short codes (only for countries ch, de, fr and us) (#587
- Loading branch information
Showing
5 changed files
with
136 additions
and
25 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
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
111 changes: 111 additions & 0 deletions
111
app/src/main/java/com/github/frimtec/android/pikettassist/util/PhoneNumberType.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,111 @@ | ||
package com.github.frimtec.android.pikettassist.util; | ||
|
||
import android.content.Context; | ||
import android.telephony.TelephonyManager; | ||
|
||
/** | ||
* Helper to evaluate the type of a phone number. | ||
* | ||
* @since 3.3.0 | ||
*/ | ||
// TODO: 20.09.2024 REPLACE when updated to com.github.frimtec:secure-sms-proxy-api version 3.3.0 | ||
public enum PhoneNumberType { | ||
STANDARD(true, false, true), | ||
NUMERIC_SHORT_CODE(true, true, true), | ||
ALPHANUMERIC_SHORT_CODE(true, true, false), | ||
EMPTY(false, false, false); | ||
|
||
private final boolean valid; | ||
private final boolean shortCode; | ||
private final boolean sendSupport; | ||
|
||
PhoneNumberType(boolean valid, boolean shortCode, boolean sendSupport) { | ||
this.valid = valid; | ||
this.shortCode = shortCode; | ||
this.sendSupport = sendSupport; | ||
} | ||
|
||
public boolean isValid() { | ||
return valid; | ||
} | ||
|
||
public boolean isShortCode() { | ||
return shortCode; | ||
} | ||
|
||
public boolean isSendSupport() { | ||
return sendSupport; | ||
} | ||
|
||
/** | ||
* Returns the type of the given phone number. | ||
* | ||
* @param phoneNumber phone number | ||
* @param context context to get the country code of the telephony manager, used to detect numeric short codes | ||
* @return phone number type | ||
**/ | ||
public static PhoneNumberType fromNumber( | ||
String phoneNumber, | ||
Context context | ||
) { | ||
return fromNumber(phoneNumber, networkCountryIso(context)); | ||
} | ||
|
||
/** | ||
* Returns the type of the given phone number. | ||
* | ||
* @param phoneNumber phone number | ||
* @param twoLetterIsoCountryCode country code, used to detect numeric short codes | ||
* @return phone number type | ||
**/ | ||
public static PhoneNumberType fromNumber( | ||
String phoneNumber, | ||
String twoLetterIsoCountryCode | ||
) { | ||
if (phoneNumber == null) { | ||
return PhoneNumberType.EMPTY; | ||
} | ||
String trimmedPhoneNumber = phoneNumber.trim(); | ||
if (trimmedPhoneNumber.isEmpty()) { | ||
return PhoneNumberType.EMPTY; | ||
} | ||
if (isAlphanumericShortCode(trimmedPhoneNumber)) { | ||
return PhoneNumberType.ALPHANUMERIC_SHORT_CODE; | ||
} | ||
if (twoLetterIsoCountryCode != null && isNumericShortCode(trimmedPhoneNumber, twoLetterIsoCountryCode.toUpperCase())) { | ||
return PhoneNumberType.NUMERIC_SHORT_CODE; | ||
} | ||
return PhoneNumberType.STANDARD; | ||
} | ||
|
||
public static String networkCountryIso(Context context) { | ||
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); | ||
return telephonyManager != null ? telephonyManager.getNetworkCountryIso() : ""; | ||
} | ||
|
||
private static boolean isAlphanumericShortCode(String phoneNumber) { | ||
for (int i = 0; i < phoneNumber.length(); i++) { | ||
char ch = phoneNumber.charAt(i); | ||
if (Character.isLetter(ch)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean isNumericShortCode(String phoneNumber, String countryCode) { | ||
return switch (countryCode) { | ||
case "CH" -> inLengthRange(phoneNumber, 3, 5); | ||
case "FR" -> inLengthRange(phoneNumber, 5, 5); | ||
case "DE" -> inLengthRange(phoneNumber, 4, 5); | ||
case "US" -> phoneNumber.charAt(0) != '1' && inLengthRange(phoneNumber, 5, 6); | ||
default -> false; | ||
}; | ||
} | ||
|
||
private static boolean inLengthRange(String phoneNumber, int min, int max) { | ||
int length = phoneNumber.length(); | ||
return length >= min && length <= max; | ||
} | ||
|
||
} |