Search in sources :

Example 1 with PhoneNumberUtil

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;
}
Also used : PhoneNumberUtil(com.google.i18n.phonenumbers.PhoneNumberUtil) PhoneNumberMatch(com.google.i18n.phonenumbers.PhoneNumberMatch) Contact(com.moez.QKSMS.data.Contact)

Example 2 with PhoneNumberUtil

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);
}
Also used : PhoneNumberUtil(com.google.i18n.phonenumbers.PhoneNumberUtil)

Example 3 with PhoneNumberUtil

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;
}
Also used : PhoneNumberUtil(com.google.i18n.phonenumbers.PhoneNumberUtil) NumberParseException(com.google.i18n.phonenumbers.NumberParseException) Phonenumber(com.google.i18n.phonenumbers.Phonenumber) SuppressLint(android.annotation.SuppressLint)

Example 4 with PhoneNumberUtil

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);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) PhoneNumberUtil(com.google.i18n.phonenumbers.PhoneNumberUtil) IOException(java.io.IOException) RemoteLoginException(io.kamax.mxisd.exception.RemoteLoginException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) NumberParseException(com.google.i18n.phonenumbers.NumberParseException) Phonenumber(com.google.i18n.phonenumbers.Phonenumber) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with PhoneNumberUtil

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]", "");
}
Also used : PhoneNumberUtil(com.google.i18n.phonenumbers.PhoneNumberUtil) PhoneNumber(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber) NumberParseException(com.google.i18n.phonenumbers.NumberParseException)

Aggregations

PhoneNumberUtil (com.google.i18n.phonenumbers.PhoneNumberUtil)38 NumberParseException (com.google.i18n.phonenumbers.NumberParseException)28 PhoneNumber (com.google.i18n.phonenumbers.Phonenumber.PhoneNumber)19 Phonenumber (com.google.i18n.phonenumbers.Phonenumber)11 IndicatorParameters (org.talend.dataquality.indicators.IndicatorParameters)6 TextParameters (org.talend.dataquality.indicators.TextParameters)6 Locale (java.util.Locale)4 SuppressLint (android.annotation.SuppressLint)3 PhoneNumberOfflineGeocoder (com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder)2 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)2 GenericValue (org.apache.ofbiz.entity.GenericValue)2 SpannableString (android.text.SpannableString)1 Nullable (androidx.annotation.Nullable)1 PhoneNumberMatch (com.google.i18n.phonenumbers.PhoneNumberMatch)1 Contact (com.moez.QKSMS.data.Contact)1 RemoteLoginException (io.kamax.mxisd.exception.RemoteLoginException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1