Home | History | Annotate | Download | only in mms
      1 /*
      2  * Copyright (C) 2015 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 
     17 package android.support.v7.mms;
     18 
     19 import android.text.TextUtils;
     20 import android.util.Log;
     21 
     22 import com.google.i18n.phonenumbers.NumberParseException;
     23 import com.google.i18n.phonenumbers.PhoneNumberUtil;
     24 import com.google.i18n.phonenumbers.Phonenumber;
     25 
     26 /**
     27  * Helper methods for phone number formatting
     28  * This is isolated into a standalone class since it depends on libphonenumber
     29  */
     30 public class PhoneNumberHelper {
     31     /**
     32      * Given a phone number, get its national part without country code
     33      *
     34      * @param number the original number
     35      * @param country the country ISO code
     36      * @return the national number
     37      */
     38     static String getNumberNoCountryCode(final String number, final String country) {
     39         if (!TextUtils.isEmpty(number)) {
     40             final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
     41             try {
     42                 final Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(number, country);
     43                 if (phoneNumber != null && phoneNumberUtil.isValidNumber(phoneNumber)) {
     44                     return phoneNumberUtil
     45                             .format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL)
     46                             .replaceAll("\\D", "");
     47                 }
     48             } catch (final NumberParseException e) {
     49                 Log.w(MmsService.TAG, "getNumberNoCountryCode: invalid number " + e);
     50             }
     51         }
     52         return number;
     53     }
     54 }
     55