summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/phonenumberproto
diff options
context:
space:
mode:
Diffstat (limited to 'java/com/android/dialer/phonenumberproto')
-rw-r--r--java/com/android/dialer/phonenumberproto/Converter.java120
-rw-r--r--java/com/android/dialer/phonenumberproto/DialerPhoneNumberUtil.java108
-rw-r--r--java/com/android/dialer/phonenumberproto/dialer_phone_number.proto173
3 files changed, 401 insertions, 0 deletions
diff --git a/java/com/android/dialer/phonenumberproto/Converter.java b/java/com/android/dialer/phonenumberproto/Converter.java
new file mode 100644
index 000000000..453b98844
--- /dev/null
+++ b/java/com/android/dialer/phonenumberproto/Converter.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.dialer.phonenumberproto;
+
+import com.android.dialer.DialerInternalPhoneNumber;
+import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
+
+/**
+ * Methods for converting from {@link PhoneNumber} POJOs to {@link DialerInternalPhoneNumber} protos
+ * and back.
+ */
+class Converter {
+
+ static DialerInternalPhoneNumber pojoToProto(PhoneNumber pojo) {
+ DialerInternalPhoneNumber.Builder proto = DialerInternalPhoneNumber.newBuilder();
+ if (pojo.hasCountryCode()) {
+ proto.setCountryCode(pojo.getCountryCode());
+ }
+ if (pojo.hasCountryCodeSource()) {
+ switch (pojo.getCountryCodeSource()) {
+ case FROM_NUMBER_WITH_PLUS_SIGN:
+ proto.setCountryCodeSource(
+ DialerInternalPhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN);
+ break;
+ case FROM_NUMBER_WITH_IDD:
+ proto.setCountryCodeSource(
+ DialerInternalPhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_IDD);
+ break;
+ case FROM_NUMBER_WITHOUT_PLUS_SIGN:
+ proto.setCountryCodeSource(
+ DialerInternalPhoneNumber.CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN);
+ break;
+ case FROM_DEFAULT_COUNTRY:
+ proto.setCountryCodeSource(
+ DialerInternalPhoneNumber.CountryCodeSource.FROM_DEFAULT_COUNTRY);
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "unsupported country code source: " + pojo.getCountryCodeSource());
+ }
+ }
+ if (pojo.hasExtension()) {
+ proto.setExtension(pojo.getExtension());
+ }
+ if (pojo.hasItalianLeadingZero()) {
+ proto.setItalianLeadingZero(pojo.isItalianLeadingZero());
+ }
+ if (pojo.hasNationalNumber()) {
+ proto.setNationalNumber(pojo.getNationalNumber());
+ }
+ if (pojo.hasNumberOfLeadingZeros()) {
+ proto.setNumberOfLeadingZeros(pojo.getNumberOfLeadingZeros());
+ }
+ if (pojo.hasPreferredDomesticCarrierCode()) {
+ proto.setPreferredDomesticCarrierCode(pojo.getPreferredDomesticCarrierCode());
+ }
+ if (pojo.hasRawInput()) {
+ proto.setRawInput(pojo.getRawInput());
+ }
+ return proto.build();
+ }
+
+ static PhoneNumber protoToPojo(DialerInternalPhoneNumber proto) {
+ PhoneNumber pojo = new PhoneNumber();
+ if (proto.hasCountryCode()) {
+ pojo.setCountryCode(proto.getCountryCode());
+ }
+ if (proto.hasCountryCodeSource()) {
+ switch (proto.getCountryCodeSource()) {
+ case FROM_NUMBER_WITH_PLUS_SIGN:
+ pojo.setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN);
+ break;
+ case FROM_NUMBER_WITH_IDD:
+ pojo.setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_IDD);
+ break;
+ case FROM_NUMBER_WITHOUT_PLUS_SIGN:
+ pojo.setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN);
+ break;
+ case FROM_DEFAULT_COUNTRY:
+ pojo.setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_DEFAULT_COUNTRY);
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "unsupported country code source: " + proto.getCountryCodeSource());
+ }
+ }
+ if (proto.hasExtension()) {
+ pojo.setExtension(proto.getExtension());
+ }
+ if (proto.hasItalianLeadingZero()) {
+ pojo.setItalianLeadingZero(proto.getItalianLeadingZero());
+ }
+ if (proto.hasNationalNumber()) {
+ pojo.setNationalNumber(proto.getNationalNumber());
+ }
+ if (proto.hasNumberOfLeadingZeros()) {
+ pojo.setNumberOfLeadingZeros(proto.getNumberOfLeadingZeros());
+ }
+ if (proto.hasPreferredDomesticCarrierCode()) {
+ pojo.setPreferredDomesticCarrierCode(proto.getPreferredDomesticCarrierCode());
+ }
+ if (proto.hasRawInput()) {
+ pojo.setRawInput(proto.getRawInput());
+ }
+ return pojo;
+ }
+}
diff --git a/java/com/android/dialer/phonenumberproto/DialerPhoneNumberUtil.java b/java/com/android/dialer/phonenumberproto/DialerPhoneNumberUtil.java
new file mode 100644
index 000000000..cc509f41b
--- /dev/null
+++ b/java/com/android/dialer/phonenumberproto/DialerPhoneNumberUtil.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.dialer.phonenumberproto;
+
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.support.annotation.WorkerThread;
+import com.android.dialer.DialerInternalPhoneNumber;
+import com.android.dialer.DialerPhoneNumber;
+import com.android.dialer.DialerPhoneNumber.RawInput;
+import com.android.dialer.common.Assert;
+import com.android.dialer.common.LogUtil;
+import com.google.i18n.phonenumbers.NumberParseException;
+import com.google.i18n.phonenumbers.PhoneNumberUtil;
+import com.google.i18n.phonenumbers.PhoneNumberUtil.MatchType;
+
+/**
+ * Wrapper for selected methods in {@link PhoneNumberUtil} which uses the {@link DialerPhoneNumber}
+ * lite proto instead of the {@link com.google.i18n.phonenumbers.Phonenumber.PhoneNumber} POJO.
+ *
+ * <p>All methods should be called on a worker thread.
+ */
+public class DialerPhoneNumberUtil {
+ private final PhoneNumberUtil phoneNumberUtil;
+
+ @WorkerThread
+ public DialerPhoneNumberUtil(@NonNull PhoneNumberUtil phoneNumberUtil) {
+ Assert.isWorkerThread();
+ this.phoneNumberUtil = Assert.isNotNull(phoneNumberUtil);
+ }
+
+ /**
+ * Parses the provided raw phone number into a {@link DialerPhoneNumber}.
+ *
+ * @see PhoneNumberUtil#parse(String, String)
+ */
+ @WorkerThread
+ public DialerPhoneNumber parse(@Nullable String numberToParse, @Nullable String defaultRegion) {
+ Assert.isWorkerThread();
+
+ DialerPhoneNumber.Builder dialerPhoneNumber = DialerPhoneNumber.newBuilder();
+ RawInput.Builder rawInput = RawInput.newBuilder();
+ // Numbers can be null or empty for incoming "unknown" calls.
+ if (numberToParse != null) {
+ rawInput.setNumber(numberToParse);
+ }
+ if (defaultRegion != null) {
+ rawInput.setCountryIso(defaultRegion);
+ }
+ dialerPhoneNumber.setRawInput(rawInput.build());
+
+ try {
+ dialerPhoneNumber.setDialerInternalPhoneNumber(
+ Converter.pojoToProto(phoneNumberUtil.parse(numberToParse, defaultRegion)));
+ } catch (NumberParseException e) {
+ LogUtil.w("DialerPhoneNumberUtil.parse", "couldn't parse phone number", e);
+ }
+ return dialerPhoneNumber.build();
+ }
+
+ /**
+ * Returns true if the two numbers were parseable by libphonenumber and are an {@link
+ * MatchType#EXACT_MATCH} or if they have the same raw input.
+ */
+ @WorkerThread
+ public boolean isExactMatch(
+ @NonNull DialerPhoneNumber firstNumberIn, @NonNull DialerPhoneNumber secondNumberIn) {
+ Assert.isWorkerThread();
+ if (!Assert.isNotNull(firstNumberIn).hasDialerInternalPhoneNumber()
+ || !Assert.isNotNull(secondNumberIn).hasDialerInternalPhoneNumber()) {
+ return firstNumberIn.getRawInput().equals(secondNumberIn.getRawInput());
+ }
+ return isNumberMatch(
+ firstNumberIn.getDialerInternalPhoneNumber(),
+ secondNumberIn.getDialerInternalPhoneNumber())
+ == MatchType.EXACT_MATCH;
+ }
+
+ /**
+ * Compares the provided phone numbers.
+ *
+ * @see PhoneNumberUtil#isNumberMatch(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber,
+ * com.google.i18n.phonenumbers.Phonenumber.PhoneNumber)
+ */
+ @WorkerThread
+ private MatchType isNumberMatch(
+ @NonNull DialerInternalPhoneNumber firstNumberIn,
+ @NonNull DialerInternalPhoneNumber secondNumberIn) {
+ Assert.isWorkerThread();
+ return phoneNumberUtil.isNumberMatch(
+ Converter.protoToPojo(Assert.isNotNull(firstNumberIn)),
+ Converter.protoToPojo(Assert.isNotNull(secondNumberIn)));
+ }
+}
diff --git a/java/com/android/dialer/phonenumberproto/dialer_phone_number.proto b/java/com/android/dialer/phonenumberproto/dialer_phone_number.proto
new file mode 100644
index 000000000..97b7519dd
--- /dev/null
+++ b/java/com/android/dialer/phonenumberproto/dialer_phone_number.proto
@@ -0,0 +1,173 @@
+// Copyright (C) 2017 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License
+
+syntax = "proto2";
+
+option java_package = "com.android.dialer";
+option java_multiple_files = true;
+option optimize_for = LITE_RUNTIME;
+
+
+package com.android.dialer;
+
+// A phone number for use in the dialer application. It consists of a
+// DialerInternalPhoneNumber, which is a copy of libphonenumber's PhoneNumber
+// proto, and the raw input used to create it.
+message DialerPhoneNumber {
+ // libphonenumber representation of the phone number. May be empty if the raw
+ // input failed to parse, in which case raw_input should be used.
+ optional DialerInternalPhoneNumber dialer_internal_phone_number = 1;
+
+ // The raw input which was used to create a DialerPhoneNumber.
+ message RawInput {
+ // The phone number as it was entered or received.
+ optional string number = 1;
+ // The ISO 3166-1 two letter country code of the country where the user made
+ // or received the call.
+ optional string country_iso = 2;
+ }
+ // Prefer to use dialer_internal_phone_number if present.
+ optional RawInput raw_input = 2;
+}
+
+// This is a copy of libphonenumber proto file for use in the dialer codebase.
+// We cannot depend on the real libphonenumber proto because it is not exposed
+// in any open source library. As such, this file could fall out of sync with
+// that proto over time.
+//
+// The only difference between this proto and the libphonenumber proto (as of
+// when this file was created) is the package name and proto name.
+//
+// If the libphonenumber proto becomes accessible some day, it may be possible
+// to remove this proto and use the real libphonenumber proto assuming this
+// proto is kept wire-compatible with it.
+message DialerInternalPhoneNumber {
+ // The country calling code for this number, as defined by the International
+ // Telecommunication Union (ITU). For example, this would be 1 for NANPA
+ // countries, and 33 for France.
+ required int32 country_code = 1;
+
+ // The National (significant) Number, as defined in International
+ // Telecommunication Union (ITU) Recommendation E.164, without any leading
+ // zero. The leading-zero is stored separately if required, since this is an
+ // uint64 and hence cannot store such information. Do not use this field
+ // directly: if you want the national significant number, call the
+ // getNationalSignificantNumber method of PhoneNumberUtil.
+ //
+ // For countries which have the concept of an "area code" or "national
+ // destination code", this is included in the National (significant) Number.
+ // Although the ITU says the maximum length should be 15, we have found longer
+ // numbers in some countries e.g. Germany.
+ // Note that the National (significant) Number does not contain the National
+ // (trunk) prefix. Obviously, as a uint64, it will never contain any
+ // formatting (hyphens, spaces, parentheses), nor any alphanumeric spellings.
+ required uint64 national_number = 2 [jstype = JS_NUMBER];
+
+ // Extension is not standardized in ITU recommendations, except for being
+ // defined as a series of numbers with a maximum length of 40 digits. It is
+ // defined as a string here to accommodate for the possible use of a leading
+ // zero in the extension (organizations have complete freedom to do so, as
+ // there is no standard defined). Other than digits, some other dialling
+ // characters such as "," (indicating a wait) may be stored here.
+ optional string extension = 3;
+
+ // In some countries, the national (significant) number starts with one or
+ // more "0"s without this being a national prefix or trunk code of some kind.
+ // For example, the leading zero in the national (significant) number of an
+ // Italian phone number indicates the number is a fixed-line number. There
+ // have been plans to migrate fixed-line numbers to start with the digit two
+ // since December 2000, but it has not happened yet. See
+ // http://en.wikipedia.org/wiki/%2B39 for more details.
+ //
+ // These fields can be safely ignored (there is no need to set them) for most
+ // countries. Some limited number of countries behave like Italy - for these
+ // cases, if the leading zero(s) of a number would be retained even when
+ // dialling internationally, set this flag to true, and also set the number of
+ // leading zeros.
+ //
+ // Clients who use the parsing or conversion functionality of the i18n phone
+ // number libraries (go/phonenumbers) will have these fields set if necessary
+ // automatically.
+ optional bool italian_leading_zero = 4;
+ optional int32 number_of_leading_zeros = 8 [default = 1];
+
+ // The next few fields are non-essential fields for a phone number. They
+ // retain extra information about the form the phone number was in when it was
+ // provided to us to parse. They can be safely ignored by most clients. To
+ // populate them, call parseAndKeepRawInput on PhoneNumberUtil.
+
+ // This field is used to store the raw input string containing phone numbers
+ // before it was canonicalized by the library. For example, it could be used
+ // to store alphanumerical numbers such as "1-800-GOOG-411".
+ optional string raw_input = 5;
+
+ // The source from which the country_code is derived. This is not set in the
+ // general parsing method, but in the method that parses and keeps raw_input.
+ // New fields could be added upon request.
+ enum CountryCodeSource {
+ // The country_code is derived based on a phone number with a leading "+",
+ // e.g. the French number "+33 1 42 68 53 00".
+ FROM_NUMBER_WITH_PLUS_SIGN = 1;
+
+ // The country_code is derived based on a phone number with a leading IDD,
+ // e.g. the French number "011 33 1 42 68 53 00", as it is dialled from US.
+ FROM_NUMBER_WITH_IDD = 5;
+
+ // The country_code is derived based on a phone number without a leading
+ // "+", e.g. the French number "33 1 42 68 53 00" when defaultCountry is
+ // supplied as France.
+ FROM_NUMBER_WITHOUT_PLUS_SIGN = 10;
+
+ // The country_code is derived NOT based on the phone number itself, but
+ // from the defaultCountry parameter provided in the parsing function by the
+ // clients. This happens mostly for numbers written in the national format
+ // (without country code). For example, this would be set when parsing the
+ // French number "01 42 68 53 00", when defaultCountry is supplied as
+ // France.
+ FROM_DEFAULT_COUNTRY = 20;
+ }
+
+ // The source from which the country_code is derived.
+ optional CountryCodeSource country_code_source = 6;
+
+ // The carrier selection code that is preferred when calling this phone number
+ // domestically. This also includes codes that need to be dialed in some
+ // countries when calling from landlines to mobiles or vice versa. For
+ // example, in Columbia, a "3" needs to be dialed before the phone number
+ // itself when calling from a mobile phone to a domestic landline phone and
+ // vice versa.
+ //
+ // Note this is the "preferred" code, which means other codes may work as
+ // well.
+ optional string preferred_domestic_carrier_code = 7;
+}
+
+// Examples:
+//
+// Google MTV, +1 650-253-0000, (650) 253-0000
+// country_code: 1
+// national_number: 6502530000
+//
+// Google Paris, +33 (0)1 42 68 53 00, 01 42 68 53 00
+// country_code: 33
+// national_number: 142685300
+//
+// Google Beijing, +86-10-62503000, (010) 62503000
+// country_code: 86
+// national_number: 1062503000
+//
+// Google Italy, +39 02-36618 300, 02-36618 300
+// country_code: 39
+// national_number: 236618300
+// italian_leading_zero: true