use of com.google.i18n.phonenumbers.PhoneNumberUtil in project qksms by moezbhatti.
the class NumberToContactFormatter method format.
@Override
public String format(String text) {
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
Iterable<PhoneNumberMatch> matches = phoneNumberUtil.findNumbers(text, getCurrentCountryIso());
for (PhoneNumberMatch match : matches) {
Contact contact = Contact.get(match.rawString(), true);
if (contact.isNamed()) {
String nameAndNumber = phoneNumberUtil.format(match.number(), PhoneNumberFormat.NATIONAL) + " (" + contact.getName() + ")";
text = text.replace(match.rawString(), nameAndNumber);
}
// If the contact doesn't exist yet, leave the number as-is
}
return text;
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project Signal-Android by WhisperSystems.
the class RegistrationActivity method setCountryFormatter.
private void setCountryFormatter(int countryCode) {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
String regionCode = util.getRegionCodeForCountryCode(countryCode);
if (regionCode == null)
this.countryFormatter = null;
else
this.countryFormatter = util.getAsYouTypeFormatter(regionCode);
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project qksms by moezbhatti.
the class PhoneNumberUtils method formatNumber.
/**
* Format a phone number.
* <p>
* If the given number doesn't have the country code, the phone will be
* formatted to the default country's convention.
*
* @param phoneNumber
* the number to be formatted.
* @param defaultCountryIso
* the ISO 3166-1 two letters country code whose convention will
* be used if the given number doesn't have the country code.
* @return the formatted number, or null if the given number is not valid.
*/
@SuppressLint("Override")
public static String formatNumber(String phoneNumber, String defaultCountryIso) {
// Do not attempt to format numbers that start with a hash or star symbol.
if (phoneNumber.startsWith("#") || phoneNumber.startsWith("*")) {
return phoneNumber;
}
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
String result = null;
try {
Phonenumber.PhoneNumber pn = util.parseAndKeepRawInput(phoneNumber, defaultCountryIso);
result = util.formatInOriginalFormat(pn, defaultCountryIso);
} catch (NumberParseException e) {
}
return result;
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project mxisd by kamax-io.
the class AuthController method login.
@RequestMapping(value = logV1Url, method = RequestMethod.POST)
public String login(HttpServletRequest req) {
try {
JsonObject reqJsonObject = parser.parse(req.getInputStream());
// find 3PID in main object
GsonUtil.findPrimitive(reqJsonObject, "medium").ifPresent(medium -> {
GsonUtil.findPrimitive(reqJsonObject, "address").ifPresent(address -> {
log.info("Login request with medium '{}' and address '{}'", medium.getAsString(), address.getAsString());
strategy.findLocal(medium.getAsString(), address.getAsString()).ifPresent(lookupDataOpt -> {
reqJsonObject.addProperty("user", lookupDataOpt.getMxid().getLocalPart());
reqJsonObject.remove("medium");
reqJsonObject.remove("address");
});
});
});
// find 3PID in 'identifier' object
GsonUtil.findObj(reqJsonObject, "identifier").ifPresent(identifier -> {
GsonUtil.findPrimitive(identifier, "type").ifPresent(type -> {
if (StringUtils.equals(type.getAsString(), "m.id.thirdparty")) {
GsonUtil.findPrimitive(identifier, "medium").ifPresent(medium -> {
GsonUtil.findPrimitive(identifier, "address").ifPresent(address -> {
log.info("Login request with medium '{}' and address '{}'", medium.getAsString(), address.getAsString());
strategy.findLocal(medium.getAsString(), address.getAsString()).ifPresent(lookupDataOpt -> {
identifier.addProperty("type", "m.id.user");
identifier.addProperty("user", lookupDataOpt.getMxid().getLocalPart());
identifier.remove("medium");
identifier.remove("address");
});
});
});
}
if (StringUtils.equals(type.getAsString(), "m.id.phone")) {
GsonUtil.findPrimitive(identifier, "number").ifPresent(number -> {
GsonUtil.findPrimitive(identifier, "country").ifPresent(country -> {
log.info("Login request with phone '{}'-'{}'", country.getAsString(), number.getAsString());
try {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber phoneNumber = phoneUtil.parse(number.getAsString(), country.getAsString());
String canon_phoneNumber = phoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164).replace("+", "");
String medium = "msisdn";
strategy.findLocal(medium, canon_phoneNumber).ifPresent(lookupDataOpt -> {
identifier.addProperty("type", "m.id.user");
identifier.addProperty("user", lookupDataOpt.getMxid().getLocalPart());
identifier.remove("country");
identifier.remove("number");
});
} catch (NumberParseException e) {
throw new RuntimeException(e);
}
});
});
}
});
});
// invoke 'login' on homeserver
HttpPost httpPost = RestClientUtils.post(resolveProxyUrl(req), gson, reqJsonObject);
try (CloseableHttpResponse httpResponse = client.execute(httpPost)) {
// check http status
int status = httpResponse.getStatusLine().getStatusCode();
log.info("http status = {}", status);
if (status != 200) {
// try to get possible json error message from response
// otherwise just get returned plain error message
String errcode = String.valueOf(httpResponse.getStatusLine().getStatusCode());
String error = EntityUtils.toString(httpResponse.getEntity());
if (httpResponse.getEntity() != null) {
try {
JsonObject bodyJson = new JsonParser().parse(error).getAsJsonObject();
if (bodyJson.has("errcode")) {
errcode = bodyJson.get("errcode").getAsString();
}
if (bodyJson.has("error")) {
error = bodyJson.get("error").getAsString();
}
throw new RemoteLoginException(status, errcode, error, bodyJson);
} catch (JsonSyntaxException e) {
log.warn("Response body is not JSON");
}
}
throw new RemoteLoginException(status, errcode, error);
}
// / return response
JsonObject respJsonObject = parser.parseOptional(httpResponse).get();
return gson.toJson(respJsonObject);
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project libsignal-service-java by signalapp.
the class PhoneNumberFormatter method formatE164.
public static String formatE164(String countryCode, String number) {
try {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
int parsedCountryCode = Integer.parseInt(countryCode);
PhoneNumber parsedNumber = util.parse(number, util.getRegionCodeForCountryCode(parsedCountryCode));
return util.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
} catch (NumberParseException | NumberFormatException npe) {
Log.w(TAG, npe);
}
return "+" + countryCode.replaceAll("[^0-9]", "").replaceAll("^0*", "") + number.replaceAll("[^0-9]", "");
}
Aggregations