Determine phone number country code from local address book - android

I am allowing the user to choose a phone number from their address book. I need the number to always be in the international format, however sometimes people store a local number without the country code in their contacts (ex. 555-555-5555 instead of +1-555-555-5555).
Is there an easy way to find out what country code the local number implies so I can add it manually?

This is what you need
https://code.google.com/p/libphonenumber/
String numberString = "044 668 18 00"
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber numberProto = phoneUtil.parse(numberString, "BH"); // Where BH is the user's iso country code
String finalNumber = phoneUtil.format(numberProto, PhoneNumberFormat.E164);
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}

Related

Android: Validate Phone Number Length by COUNTRY

How can we validate the length of a mobile number by COUNTRY?
(Knowing that the country code might or might not be part of the number)
Length may vary per country, meaning there should be a length range or pattern validator.
Example:
+1 222 2222 222 (13 digits) Valid ✔️
222 2222 222 (12 digits) Valid only if chosen in corresponding Locale ✔️
+1 222 2222 2222222 (17 digits) Invalid ❌
+1 222 2222 (8 digits) Invalid ❌
Note: Any idea where can I find each country's mobile number length range.
Thank you!
Given the mobile number and the country code, you can use libphonenumeer which is a Google library for validating phone numbers; it checks the number length and catches NumberParseException exception if it is not a right number.
This is their sample in how to use it
String swissNumberStr = "044 668 18 00";
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "CH");
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
Adding to #Zain 's answer.
String swissNumberStr = "044 668 18 00";
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "CH");
// This will check if the phone number is real and its length is valid.
boolean isPossible = phoneUtil.isPossibleNumber(swissNumberProto);
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}

How to format phone number to national format without national prefix using PhoneNumberUtils library?

I'm trying to format a previously E164 formatted phone number into National format without the National Prefix number, I'm using PhoneNumberUtils to do it:
public static String getNationalPhone(String phoneE164Formatted) {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
String phoneNumber = phoneE164Formatted;
try {
Phonenumber.PhoneNumber numberProto = phoneUtil.parseAndKeepRawInput(phoneE164Formatted, "");
phoneNumber = phoneUtil.format(numberProto, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
} catch (NumberParseException e) {
e.printStackTrace();
}
return phoneNumber;
}
With that code I'm getting the national prefix I don't want. For example, formatting this number "+526143342293" into national format gave me this result: "01 614 334 22 93", where "01" it's what I want to suppress.
https://github.com/brick/phonenumber/blob/master/src/PhoneNumber.php
These answer in php, you can convert into your own.
public function getCountryCode()
{
return (string) $this->phoneNumber->getCountryCode();
}
Convert above code will get the national number in desired format.

Extract (parse) amount and description from BIZ (Transaction) sms

I am doing below steps.
match sms with regex
if contains specified keyword then get values from sms body like amount,description (reason of transaction), Account number(if ATM withdraw),transaction type(debit/credit)
this regex not matching all kind of bank/transaction sms thus it is not efficient , is there any other way to identify bank message.
example sms :
1) Dear Customer, your Account Number XXXXXX6377 has been credited by Rs 215.000 being DBT/DBTL funds transfer on 19/05/2015 - CENTRAL BANK OF INDIA
2)A/c NN5715 debited for Rs 2000; ATM WDL. A/c Bal(sub to chq realisatn) Rs13286.23 on 24APR 21:19hr. Call 1800226999 to block your card if not used by you.
3) Dear Customer, Your Ac XXXXXXXX5666 is credited with INR8,922.00 on 16 Feb. Info. INF*000080483346*SALARY. Your Net Available Bal is INR 8,922.00.
private static ArrayList<SmsDto> parsevalues(ArrayList<SmsDto> body_val) {
ArrayList<SmsDto> resSms = new ArrayList<>();
for (int i = 0; i < body_val.size(); i++) {
SmsDto smsDto = body_val.get(i);
Pattern regEx
= Pattern.compile("(?:inr|rs)+[\\s]*[0-9+[\\,]*+[0-9]*]+[\\.]*[0-9]+");
// Find instance of pattern matches
Matcher m = regEx.matcher(smsDto.getBody());
if (m.find()) {
try {
Log.e("amount_value= ", "" + m.group(0));
String amount = (m.group(0).replaceAll("inr", ""));
amount = amount.replaceAll("rs", "");
amount = amount.replaceAll("inr", "");
amount = amount.replaceAll(" ", "");
amount = amount.replaceAll(",", "");
smsDto.setAmount(Double.valueOf(amount));
if (smsDto.getBody().contains("debited") ||
smsDto.getBody().contains("purchasing") || smsDto.getBody().contains("purchase") || smsDto.getBody().contains("dr")) {
smsDto.setTransactionType("0");
} else if (smsDto.getBody().contains("credited") || smsDto.getBody().contains("cr")) {
smsDto.setTransactionType("1");
}
smsDto.setParsed("1");
Log.e("matchedValue= ", "" + amount);
if (!Character.isDigit(smsDto.getSenderid().charAt(0)))
resSms.add(smsDto);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.e("No_matchedValue ", "No_matchedValue ");
}
}
return resSms;
}
For finding out amount from bank transaction message.
(?i)(?:(?:RS|INR|MRP)\.?\s?)(\d+(:?\,\d+)?(\,\d+)?(\.\d{1,2})?)
For finding out merchant name from bank transaction message.
(?i)(?:\sat\s|in\*)([A-Za-z0-9]*\s?-?\s?[A-Za-z0-9]*\s?-?\.?)
For finding out card name(debit/credit card) from bank transaction message.
(?i)(?:\smade on|ur|made a\s|in\*)([A-Za-z]*\s?-?\s[A-Za-z]*\s?-?\s[A-Za-z]*\s?-?)
In python following Regex can be helpful.
For finding amount in bank messages
[rR][sS]\.?\s[,\d]+\.?\d{0,2}|[iI][nN][rR]\.?\s*[,\d]+\.?\d{0,2}
For finding A/C no
[0-9]*[Xx\*]*[0-9]*[Xx\*]+[0-9]{3,}
The following two regular expressions helped in finding amount from most of the bank transactions(HDFC, ICICI, ING, KOTAK, SBI, CANARA, PNB):
[Ii][Nn][Rr](\\s*.\\s*\\d*)
[rR][sS](\\s*.\\s*\\d*)
Please comment if you have figured out much better expressions than the above.
Please Check in this link:-https://github.com/vikashstm/transactionsmsfilter
Here maximum , banking filter added.
It has also get through message:-
Deducted Amount,
balance message identified and get available message
Account Number
Transaction Mode
Reference Number/transaction Information etc...
To detect any transactional message in android :
"(?=.*[Aa]ccount.*|.*[Aa]/[Cc].*|.*[Aa][Cc][Cc][Tt].*|.*[Cc][Aa][Rr][Dd].*)(?=.*[Cc]redit.*|.*[Dd]ebit.*)(?=.*[Ii][Nn][Rr].*|.*[Rr][Ss].*)"
tested on several bank messages
Please check https://github.com/minimal-scouser/trny
Usage:
import { getTransactionInfo } from "trny";
const message = "Your a/c XX0413 is debited on 15/12/2020 by INR 3,211.00 towards purchase. Avl Bal: INR 5,603.54.";
const info = getTransactionInfo(message);
/*
info = {
account: {
type: "account",
no: "0413"
},
balance: "5603.54",
money: "3211.00",
typeOfTransaction: "debited"
}
*/
It also has methods like
getAccount
getMoneySpent
getBalance
This needs more testing but see if this solves your problem.

android: how do I format number as phone with parentheses

I have a number that I need to format as a telephone number. If I do
PhoneNumberUtils.formatNumber(numStr);
Then I get
888-555-1234
But what I need to get is
(888) 555-1234
How do I get the second one? Is there a standard android way?
If you know the country for which you want to do it, you can use Google's open source library libphonenumber . Here is how you can format it:
String numberStr = "8885551234"
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber numberProto = phoneUtil.parse(numberStr, "US");
//Since you know the country you can format it as follows:
System.out.println(phoneUtil.format(numberProto, PhoneNumberFormat.NATIONAL));
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
If you don't know the country then for numberStr use E.164 format phone number and in place of country code use null.
Don't know if you found what you were looking for, but I ended up writing a little method that takes the length of a string (since the phone numbers I get come from a web service and can be a variety of formats). I believe it should work (so far all my test cases have been with the first two options -- haven't tested the other two yet).
public static String FormatStringAsPhoneNumber(String input) {
String output;
switch (input.length()) {
case 7:
output = String.format("%s-%s", input.substring(0,3), input.substring(3,7));
break;
case 10:
output = String.format("(%s) %s-%s", input.substring(0,3), input.substring(3,6), input.substring(6,10));
break;
case 11:
output = String.format("%s (%s) %s-%s", input.substring(0,1) ,input.substring(1,4), input.substring(4,7), input.substring(7,11));
break;
case 12:
output = String.format("+%s (%s) %s-%s", input.substring(0,2) ,input.substring(2,5), input.substring(5,8), input.substring(8,12));
break;
default:
return null;
}
return output;
}
If you have the String "888-555-1234" - by using PhoneNumberUtils.formatNumber(numStr); you can simply do this:
String numStr = "888-555-1234";
numStr = "(" + numStr.substring(0,3) + ") " + numStr.substring(4);
System.out.print(numStr); // (888) 555-1234
However, this is hard coded. You would need to make sure the String had a full 10 digits before doing so.
You simply use this and get you want :
new PhoneNumberFormattingTextWatcher()
or Have look at this url :
http://developer.android.com/reference/android/telephony/PhoneNumberFormattingTextWatcher.html
Try to use regex. This will help you. As for me, i use this:
var result = "+1 888-555-1234"
if (Pattern.compile("^\\+[\\d]+\\s[\\d]{1,3}\\s[\\d]+").matcher(result).find()) {
result = result.replaceFirst(" ", "(").replaceFirst(" ", ")").replace(" ","-")
}
if(Pattern.compile("^\\+[\\d]+\\s[\\d]{1,3}-[\\d]+").matcher(result).find()){
result = result.replaceFirst(" ", "(").replaceFirst("-", ")")
}
Timber.d("$result")
output: +1(888)555-1234
Working solution in 2020:
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String countryIso = telephonyManager.getNetworkCountryIso().toUpperCase();
phoneNumberTextView.setText(PhoneNumberUtils.formatNumber("3473214567", countryIso));

Extract code country from phone number [libphonenumber]

I have a string like this : +33123456789 (french phone number). I want to extract the country code (+33) without knowing the country. For example, it should work if i have another phone from another country. I use the google library https://code.google.com/p/libphonenumber/.
If I know the country, it is cool I can find the country code :
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
int countryCode = phoneUtil.getCountryCodeForRegion(locale.getCountry());
but I don't find a way to parse a string without to know the country.
Okay, so I've joined the google group of libphonenumber ( https://groups.google.com/forum/?hl=en&fromgroups#!forum/libphonenumber-discuss ) and I've asked a question.
I don't need to set the country in parameter if my phone number begins with "+". Here is an example :
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
// phone must begin with '+'
PhoneNumber numberProto = phoneUtil.parse(phone, "");
int countryCode = numberProto.getCountryCode();
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
I have got kept a handy helper method to take care of this based on one answer posted above:
Imports:
import com.google.i18n.phonenumbers.NumberParseException
import com.google.i18n.phonenumbers.PhoneNumberUtil
Function:
fun parseCountryCode( phoneNumberStr: String?): String {
val phoneUtil = PhoneNumberUtil.getInstance()
return try {
// phone must begin with '+'
val numberProto = phoneUtil.parse(phoneNumberStr, "")
numberProto.countryCode.toString()
} catch (e: NumberParseException) {
""
}
}
In here you can save the phone number as international formatted phone number
internationalFormatPhoneNumber = phoneUtil.format(givenPhoneNumber, PhoneNumberFormat.INTERNATIONAL);
it return the phone number as
International format +94 71 560 4888
so now I have get country code as this
String countryCode = internationalFormatPhoneNumber.substring(0,internationalFormatPhoneNumber.indexOf('')).replace('+', ' ').trim();
Hope this will help you
Here is a solution to get the country based on an international phone number without using the Google library.
Let me explain first why it is so difficult to figure out the country. The country code of few countries is 1 digit, 2, 3 or 4 digits. That would be simple enough. But the country code 1 is not just used for US, but also for Canada and some smaller places:
1339 USA
1340 Virgin Islands (Caribbean Islands)
1341 USA
1342 not used
1343 Canada
Digits 2..4 decide, if it is US or Canada or ... There is no easy way to figure out the country, like the first xxx are Canada, the rest US.
For my code, I defined a class which holds information for ever digit:
public class DigitInfo {
public char Digit;
public Country? Country;
public DigitInfo?[]? Digits;
}
A first array holds the DigitInfos for the first digit in the number. The second digit is used as an index into DigitInfo.Digits. One travels down that Digits chain, until Digits is empty. If Country is defined (i.e. not null) that value gets returned, otherwise any Country defined earlier gets returned:
country code 1: byPhone[1].Country is US
country code 1236: byPhone[1].Digits[2].Digits[3].Digits[6].Country is Canada
country code 1235: byPhone[1].Digits[2].Digits[3].Digits[5].Country is null. Since
byPhone[1].Country is US, also 1235 is US, because no other
country was found in the later digits
Here is the method which returns the country based on the phone number:
/// <summary>
/// Returns the Country based on an international dialing code.
/// </summary>
public static Country? GetCountry(ReadOnlySpan<char> phoneNumber) {
if (phoneNumber.Length==0) return null;
var isFirstDigit = true;
DigitInfo? digitInfo = null;
Country? country = null;
foreach (var digitChar in phoneNumber) {
var digitIndex = digitChar - '0';
if (isFirstDigit) {
isFirstDigit = false;
digitInfo = ByPhone[digitIndex];
} else {
if (digitInfo!.Digits is null) return country;
digitInfo = digitInfo.Digits[digitIndex];
}
if (digitInfo is null) return country;
country = digitInfo.Country??country;
}
return country;
}
The rest of the code (digitInfos for every country of the world, test code, ...) is too big to be posted here, but it can be found on Github:
https://github.com/PeterHuberSg/WpfWindowsLib/blob/master/WpfWindowsHelperLib/CountryCode.cs
The code is part of a WPF TextBox and the library contains also other controls for email addresses, etc. A more detailed description is on CodeProject: International Phone Number Validation Explained in Detail
Change 23.1.23: I moved CountryCode.cs to WpfWindowsHelperLib, which doesn't have any WPF dependencies, despite it's name.
Use a try catch block like below:
try {
const phoneNumber = this.phoneUtil.parseAndKeepRawInput(value, this.countryCode);
}catch(e){}
If the string containing the phone number will always start this way (+33 or another country code) you should use regex to parse and get the country code and then use the library to get the country associated to the number.
Here's a an answer how to find country calling code without using third-party libraries (as real developer does):
Get list of all available country codes, Wikipedia can help here:
https://en.wikipedia.org/wiki/List_of_country_calling_codes
Parse data in a tree structure where each digit is a branch.
Traverse your tree digit by digit until you are at the last branch - that's your country code.

Categories

Resources