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.calllogutils; 18 19 import android.content.Context; 20 import android.provider.CallLog.Calls; 21 import android.telephony.PhoneNumberUtils; 22 import android.text.BidiFormatter; 23 import android.text.TextDirectionHeuristics; 24 import android.text.TextUtils; 25 import com.android.dialer.phonenumberutil.PhoneNumberHelper; 26 import com.google.common.base.Optional; 27 28 /** Helper for formatting and managing the display of phone numbers. */ 29 public class PhoneNumberDisplayUtil { 30 31 /** Returns the string to display for the given phone number if there is no matching contact. */ 32 public static CharSequence getDisplayName( 33 Context context, CharSequence number, int presentation, boolean isVoicemail) { 34 Optional<String> presentationString = getNameForPresentation(context, presentation); 35 if (presentationString.isPresent()) { 36 return presentationString.get(); 37 } 38 if (isVoicemail) { 39 return context.getResources().getString(R.string.voicemail_string); 40 } 41 if (PhoneNumberHelper.isLegacyUnknownNumbers(number)) { 42 return context.getResources().getString(R.string.unknown); 43 } 44 return ""; 45 } 46 47 /** Returns the string associated with the given presentation. */ 48 public static Optional<String> getNameForPresentation(Context appContext, int presentation) { 49 if (presentation == Calls.PRESENTATION_UNKNOWN) { 50 return Optional.of(appContext.getResources().getString(R.string.unknown)); 51 } 52 if (presentation == Calls.PRESENTATION_RESTRICTED) { 53 return Optional.of(PhoneNumberHelper.getDisplayNameForRestrictedNumber(appContext)); 54 } 55 if (presentation == Calls.PRESENTATION_PAYPHONE) { 56 return Optional.of(appContext.getResources().getString(R.string.payphone)); 57 } 58 return Optional.absent(); 59 } 60 61 /** 62 * Returns the string to display for the given phone number. 63 * 64 * @param number the number to display 65 * @param formattedNumber the formatted number if available, may be null 66 */ 67 static CharSequence getDisplayNumber( 68 Context context, 69 CharSequence number, 70 int presentation, 71 CharSequence formattedNumber, 72 CharSequence postDialDigits, 73 boolean isVoicemail) { 74 final CharSequence displayName = getDisplayName(context, number, presentation, isVoicemail); 75 if (!TextUtils.isEmpty(displayName)) { 76 return getTtsSpannableLtrNumber(displayName); 77 } 78 79 if (!TextUtils.isEmpty(formattedNumber)) { 80 return getTtsSpannableLtrNumber(formattedNumber); 81 } else if (!TextUtils.isEmpty(number)) { 82 return getTtsSpannableLtrNumber(number.toString() + postDialDigits); 83 } else { 84 return context.getResources().getString(R.string.unknown); 85 } 86 } 87 88 /** Returns number annotated as phone number in LTR direction. */ 89 private static CharSequence getTtsSpannableLtrNumber(CharSequence number) { 90 return PhoneNumberUtils.createTtsSpannable( 91 BidiFormatter.getInstance().unicodeWrap(number.toString(), TextDirectionHeuristics.LTR)); 92 } 93 } 94