Home | History | Annotate | Download | only in phonenumberproto
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.dialer.phonenumberproto;
     17 
     18 import com.android.dialer.DialerInternalPhoneNumber;
     19 import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
     20 
     21 /**
     22  * Methods for converting from {@link PhoneNumber} POJOs to {@link DialerInternalPhoneNumber} protos
     23  * and back.
     24  */
     25 class Converter {
     26 
     27   static DialerInternalPhoneNumber pojoToProto(PhoneNumber pojo) {
     28     DialerInternalPhoneNumber.Builder proto = DialerInternalPhoneNumber.newBuilder();
     29     if (pojo.hasCountryCode()) {
     30       proto.setCountryCode(pojo.getCountryCode());
     31     }
     32     if (pojo.hasCountryCodeSource()) {
     33       switch (pojo.getCountryCodeSource()) {
     34         case FROM_NUMBER_WITH_PLUS_SIGN:
     35           proto.setCountryCodeSource(
     36               DialerInternalPhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN);
     37           break;
     38         case FROM_NUMBER_WITH_IDD:
     39           proto.setCountryCodeSource(
     40               DialerInternalPhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_IDD);
     41           break;
     42         case FROM_NUMBER_WITHOUT_PLUS_SIGN:
     43           proto.setCountryCodeSource(
     44               DialerInternalPhoneNumber.CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN);
     45           break;
     46         case FROM_DEFAULT_COUNTRY:
     47           proto.setCountryCodeSource(
     48               DialerInternalPhoneNumber.CountryCodeSource.FROM_DEFAULT_COUNTRY);
     49           break;
     50         default:
     51           throw new IllegalArgumentException(
     52               "unsupported country code source: " + pojo.getCountryCodeSource());
     53       }
     54     }
     55     if (pojo.hasExtension()) {
     56       proto.setExtension(pojo.getExtension());
     57     }
     58     if (pojo.hasItalianLeadingZero()) {
     59       proto.setItalianLeadingZero(pojo.isItalianLeadingZero());
     60     }
     61     if (pojo.hasNationalNumber()) {
     62       proto.setNationalNumber(pojo.getNationalNumber());
     63     }
     64     if (pojo.hasNumberOfLeadingZeros()) {
     65       proto.setNumberOfLeadingZeros(pojo.getNumberOfLeadingZeros());
     66     }
     67     if (pojo.hasPreferredDomesticCarrierCode()) {
     68       proto.setPreferredDomesticCarrierCode(pojo.getPreferredDomesticCarrierCode());
     69     }
     70     if (pojo.hasRawInput()) {
     71       proto.setRawInput(pojo.getRawInput());
     72     }
     73     return proto.build();
     74   }
     75 
     76   static PhoneNumber protoToPojo(DialerInternalPhoneNumber proto) {
     77     PhoneNumber pojo = new PhoneNumber();
     78     if (proto.hasCountryCode()) {
     79       pojo.setCountryCode(proto.getCountryCode());
     80     }
     81     if (proto.hasCountryCodeSource()) {
     82       switch (proto.getCountryCodeSource()) {
     83         case FROM_NUMBER_WITH_PLUS_SIGN:
     84           pojo.setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN);
     85           break;
     86         case FROM_NUMBER_WITH_IDD:
     87           pojo.setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_IDD);
     88           break;
     89         case FROM_NUMBER_WITHOUT_PLUS_SIGN:
     90           pojo.setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN);
     91           break;
     92         case FROM_DEFAULT_COUNTRY:
     93           pojo.setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_DEFAULT_COUNTRY);
     94           break;
     95         default:
     96           throw new IllegalArgumentException(
     97               "unsupported country code source: " + proto.getCountryCodeSource());
     98       }
     99     }
    100     if (proto.hasExtension()) {
    101       pojo.setExtension(proto.getExtension());
    102     }
    103     if (proto.hasItalianLeadingZero()) {
    104       pojo.setItalianLeadingZero(proto.getItalianLeadingZero());
    105     }
    106     if (proto.hasNationalNumber()) {
    107       pojo.setNationalNumber(proto.getNationalNumber());
    108     }
    109     if (proto.hasNumberOfLeadingZeros()) {
    110       pojo.setNumberOfLeadingZeros(proto.getNumberOfLeadingZeros());
    111     }
    112     if (proto.hasPreferredDomesticCarrierCode()) {
    113       pojo.setPreferredDomesticCarrierCode(proto.getPreferredDomesticCarrierCode());
    114     }
    115     if (proto.hasRawInput()) {
    116       pojo.setRawInput(proto.getRawInput());
    117     }
    118     return pojo;
    119   }
    120 }
    121