Home | History | Annotate | Download | only in calllog
      1 /*
      2  * Copyright (C) 2011 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 com.android.contacts.calllog;
     18 
     19 import com.android.contacts.ContactsUtils;
     20 import com.android.contacts.R;
     21 import com.android.internal.telephony.CallerInfo;
     22 
     23 import android.content.res.Resources;
     24 import android.net.Uri;
     25 import android.telephony.PhoneNumberUtils;
     26 import android.text.TextUtils;
     27 
     28 /**
     29  * Helper for formatting and managing phone numbers.
     30  */
     31 public class PhoneNumberHelper {
     32     private final Resources mResources;
     33 
     34     public PhoneNumberHelper(Resources resources) {
     35         mResources = resources;
     36     }
     37 
     38     /** Returns true if it is possible to place a call to the given number. */
     39     public boolean canPlaceCallsTo(CharSequence number) {
     40         return !(TextUtils.isEmpty(number)
     41                 || number.equals(CallerInfo.UNKNOWN_NUMBER)
     42                 || number.equals(CallerInfo.PRIVATE_NUMBER)
     43                 || number.equals(CallerInfo.PAYPHONE_NUMBER));
     44     }
     45 
     46     /** Returns true if it is possible to send an SMS to the given number. */
     47     public boolean canSendSmsTo(CharSequence number) {
     48         return canPlaceCallsTo(number) && !isVoicemailNumber(number) && !isSipNumber(number);
     49     }
     50 
     51     /**
     52      * Returns the string to display for the given phone number.
     53      *
     54      * @param number the number to display
     55      * @param formattedNumber the formatted number if available, may be null
     56      */
     57     public CharSequence getDisplayNumber(CharSequence number, CharSequence formattedNumber) {
     58         if (TextUtils.isEmpty(number)) {
     59             return "";
     60         }
     61         if (number.equals(CallerInfo.UNKNOWN_NUMBER)) {
     62             return mResources.getString(R.string.unknown);
     63         }
     64         if (number.equals(CallerInfo.PRIVATE_NUMBER)) {
     65             return mResources.getString(R.string.private_num);
     66         }
     67         if (number.equals(CallerInfo.PAYPHONE_NUMBER)) {
     68             return mResources.getString(R.string.payphone);
     69         }
     70         if (isVoicemailNumber(number)) {
     71             return mResources.getString(R.string.voicemail);
     72         }
     73         if (TextUtils.isEmpty(formattedNumber)) {
     74             return number;
     75         } else {
     76             return formattedNumber;
     77         }
     78     }
     79 
     80     /**
     81      * Returns true if the given number is the number of the configured voicemail.
     82      * To be able to mock-out this, it is not a static method.
     83      */
     84     public boolean isVoicemailNumber(CharSequence number) {
     85         return PhoneNumberUtils.isVoiceMailNumber(number.toString());
     86     }
     87 
     88     /**
     89      * Returns true if the given number is a SIP address.
     90      * To be able to mock-out this, it is not a static method.
     91      */
     92     public boolean isSipNumber(CharSequence number) {
     93         return PhoneNumberUtils.isUriNumber(number.toString());
     94     }
     95 }
     96