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