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.dialer.calllog;
     18 
     19 import android.content.Context;
     20 import android.provider.CallLog.Calls;
     21 import android.text.TextUtils;
     22 
     23 import com.android.dialer.R;
     24 import com.android.dialer.util.PhoneNumberUtil;
     25 
     26 /**
     27  * Helper for formatting and managing the display of phone numbers.
     28  */
     29 public class PhoneNumberDisplayUtil {
     30 
     31     /**
     32      * Returns the string to display for the given phone number if there is no matching contact.
     33      */
     34     /* package */ static CharSequence getDisplayName(
     35             Context context,
     36             CharSequence number,
     37             int presentation,
     38             boolean isVoicemail) {
     39         if (presentation == Calls.PRESENTATION_UNKNOWN) {
     40             return context.getResources().getString(R.string.unknown);
     41         }
     42         if (presentation == Calls.PRESENTATION_RESTRICTED) {
     43             return context.getResources().getString(R.string.private_num);
     44         }
     45         if (presentation == Calls.PRESENTATION_PAYPHONE) {
     46             return context.getResources().getString(R.string.payphone);
     47         }
     48         if (isVoicemail) {
     49             return context.getResources().getString(R.string.voicemail);
     50         }
     51         if (PhoneNumberUtil.isLegacyUnknownNumbers(number)) {
     52             return context.getResources().getString(R.string.unknown);
     53         }
     54         return "";
     55     }
     56 
     57     /**
     58      * Returns the string to display for the given phone number.
     59      *
     60      * @param number the number to display
     61      * @param formattedNumber the formatted number if available, may be null
     62      */
     63     public static CharSequence getDisplayNumber(
     64             Context context,
     65             CharSequence number,
     66             int presentation,
     67             CharSequence formattedNumber,
     68             CharSequence postDialDigits,
     69             boolean isVoicemail) {
     70         final CharSequence displayName = getDisplayName(context, number, presentation, isVoicemail);
     71         if (!TextUtils.isEmpty(displayName)) {
     72             return displayName;
     73         }
     74 
     75         if (!TextUtils.isEmpty(formattedNumber)) {
     76             return formattedNumber;
     77         } else if (!TextUtils.isEmpty(number)) {
     78             return number.toString() + postDialDigits;
     79         } else {
     80             return context.getResources().getString(R.string.unknown);
     81         }
     82     }
     83 }
     84