194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/* 294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Copyright (C) 2011 The Android Open Source Project 394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * 494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License"); 594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * you may not use this file except in compliance with the License. 694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * You may obtain a copy of the License at 794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * 894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * http://www.apache.org/licenses/LICENSE-2.0 994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * 1094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Unless required by applicable law or agreed to in writing, software 1194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS, 1294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * See the License for the specific language governing permissions and 1494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * limitations under the License. 1594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */ 1694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng 1794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengpackage com.android.dialer.calllog; 1894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng 19e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chenimport android.content.Context; 2094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.res.Resources; 21719a7adde25e0a717816b00668c16c3a1e3c5518Jay Shraunerimport android.provider.CallLog.Calls; 22e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chenimport android.telecom.PhoneAccountHandle; 2394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.text.TextUtils; 243671725b58a9768016e141c77424dedb5fd2c55aYorke Leeimport android.util.Log; 2594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng 269554500572ba82fbd7adb0a1637206ef870ef09eChiao Chengimport com.android.dialer.R; 2794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng 2894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/** 2924ec319f8d410be8a1923c4033f927165876cbabYorke Lee * Helper for formatting and managing the display of phone numbers. 3094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */ 3124ec319f8d410be8a1923c4033f927165876cbabYorke Leepublic class PhoneNumberDisplayHelper { 32e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen private final Context mContext; 3394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng private final Resources mResources; 34e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper; 3594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng 36e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen public PhoneNumberDisplayHelper(Context context, Resources resources) { 37e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen mContext = context; 3894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng mResources = resources; 39e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen mPhoneNumberUtilsWrapper = new PhoneNumberUtilsWrapper(context); 403671725b58a9768016e141c77424dedb5fd2c55aYorke Lee } 413671725b58a9768016e141c77424dedb5fd2c55aYorke Lee 42e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen public PhoneNumberDisplayHelper(Context context, Resources resources, 43e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen PhoneNumberUtilsWrapper phoneNumberUtils) { 44e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen mContext = context; 453671725b58a9768016e141c77424dedb5fd2c55aYorke Lee mResources = resources; 46e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen mPhoneNumberUtilsWrapper = phoneNumberUtils; 4794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng } 4894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng 49e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen /* package */ CharSequence getDisplayName(PhoneAccountHandle accountHandle, CharSequence number, 50e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen int presentation) { 51719a7adde25e0a717816b00668c16c3a1e3c5518Jay Shrauner if (presentation == Calls.PRESENTATION_UNKNOWN) { 5294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng return mResources.getString(R.string.unknown); 5394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng } 54719a7adde25e0a717816b00668c16c3a1e3c5518Jay Shrauner if (presentation == Calls.PRESENTATION_RESTRICTED) { 5594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng return mResources.getString(R.string.private_num); 5694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng } 57719a7adde25e0a717816b00668c16c3a1e3c5518Jay Shrauner if (presentation == Calls.PRESENTATION_PAYPHONE) { 5894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng return mResources.getString(R.string.payphone); 5994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng } 60e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen if (mPhoneNumberUtilsWrapper.isVoicemailNumber(accountHandle, number)) { 6194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng return mResources.getString(R.string.voicemail); 6294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng } 63a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen if (PhoneNumberUtilsWrapper.isLegacyUnknownNumbers(number)) { 64a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen return mResources.getString(R.string.unknown); 65a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen } 6675245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen return ""; 6775245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen } 6875245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen 6975245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen /** 7075245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen * Returns the string to display for the given phone number. 7175245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen * 72e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen * @param accountHandle The handle for the account corresponding to the call 7375245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen * @param number the number to display 7475245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen * @param formattedNumber the formatted number if available, may be null 7575245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen */ 76e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen public CharSequence getDisplayNumber(PhoneAccountHandle accountHandle, CharSequence number, 7775245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen int presentation, CharSequence formattedNumber) { 7875245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen 79e80d62229bb11a92f0db8d4e4bac6533bbed9b66Nancy Chen final CharSequence displayName = getDisplayName(accountHandle, number, presentation); 8075245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen if (!TextUtils.isEmpty(displayName)) { 8175245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen return displayName; 8275245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen } 8375245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen 8475245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen if (TextUtils.isEmpty(number)) { 8575245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen return ""; 8675245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen } 8775245f68b77c9c3d0be2a195e140a97b2d7c7f7bChristine Chen 8894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng if (TextUtils.isEmpty(formattedNumber)) { 8994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng return number; 9094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng } else { 9194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng return formattedNumber; 9294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng } 9394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng } 9494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng} 95