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