From 49efd91e50a11dc7bdef8382a0ceac01bc060f77 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Tue, 19 May 2015 12:17:26 -0700 Subject: Performance improvements to call log scrolling. - Remove call to CallUtil to check if video is enabled. It seems like it's fine to include the content description of what the call was, if it was a video call, even if there is not a video-enabled call account. - Factor out PhoneNumberDisplayHelper so it doesn't need to be an instance. This reduces some extra calls to getDisplayNameHelper. Probably a marginal difference, performance-wise, but it probably helps a smidgen and also simplifies the need for creating and passing or recalculating various instances of things. TODO: It'd be much better if PhoneCallDetails had a builder. It's terribly painful to fix all the tests when adding fields... Change-Id: I6da13dc8b6b047043aba871796a8ed13b112a227 --- src/com/android/dialer/CallDetailActivity.java | 65 +++++---------- src/com/android/dialer/PhoneCallDetails.java | 41 +++++++--- src/com/android/dialer/PhoneCallDetailsHelper.java | 16 +--- src/com/android/dialer/calllog/CallLogAdapter.java | 27 +++---- .../dialer/calllog/CallLogListItemHelper.java | 16 ++-- .../dialer/calllog/CallLogListItemViewHolder.java | 5 +- .../dialer/calllog/DefaultVoicemailNotifier.java | 25 ++---- .../dialer/calllog/PhoneNumberDisplayHelper.java | 93 ---------------------- .../dialer/calllog/PhoneNumberDisplayUtil.java | 88 ++++++++++++++++++++ 9 files changed, 170 insertions(+), 206 deletions(-) delete mode 100644 src/com/android/dialer/calllog/PhoneNumberDisplayHelper.java create mode 100644 src/com/android/dialer/calllog/PhoneNumberDisplayUtil.java (limited to 'src/com') diff --git a/src/com/android/dialer/CallDetailActivity.java b/src/com/android/dialer/CallDetailActivity.java index 2401c4767..18cf753ec 100644 --- a/src/com/android/dialer/CallDetailActivity.java +++ b/src/com/android/dialer/CallDetailActivity.java @@ -60,7 +60,7 @@ import com.android.dialer.calllog.CallTypeHelper; import com.android.dialer.calllog.ContactInfo; import com.android.dialer.calllog.ContactInfoHelper; import com.android.dialer.calllog.PhoneAccountUtils; -import com.android.dialer.calllog.PhoneNumberDisplayHelper; +import com.android.dialer.calllog.PhoneNumberDisplayUtil; import com.android.dialer.calllog.PhoneNumberUtilsWrapper; import com.android.dialer.util.AsyncTaskExecutor; import com.android.dialer.util.AsyncTaskExecutors; @@ -106,7 +106,6 @@ public class CallDetailActivity extends Activity { public static final String VOICEMAIL_FRAGMENT_TAG = "voicemail_fragment"; private CallTypeHelper mCallTypeHelper; - private PhoneNumberDisplayHelper mPhoneNumberHelper; private QuickContactBadge mQuickContactBadge; private TextView mCallerName; private TextView mCallerNumber; @@ -173,7 +172,6 @@ public class CallDetailActivity extends Activity { mResources = getResources(); mCallTypeHelper = new CallTypeHelper(getResources()); - mPhoneNumberHelper = new PhoneNumberDisplayHelper(this, mResources); mVoicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI); @@ -344,12 +342,7 @@ public class CallDetailActivity extends Activity { final CharSequence callLocationOrType = getNumberTypeOrLocation(firstDetails); - final CharSequence displayNumber = - mPhoneNumberHelper.getDisplayNumber( - firstDetails.accountHandle, - firstDetails.number, - firstDetails.numberPresentation, - firstDetails.formattedNumber); + final CharSequence displayNumber = firstDetails.displayNumber; final String displayNumberStr = mBidiFormatter.unicodeWrap( displayNumber.toString(), TextDirectionHeuristics.LTR); @@ -396,11 +389,7 @@ public class CallDetailActivity extends Activity { String nameForDefaultImage; if (TextUtils.isEmpty(firstDetails.name)) { - nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber( - firstDetails.accountHandle, - firstDetails.number, - firstDetails.numberPresentation, - firstDetails.formattedNumber).toString(); + nameForDefaultImage = firstDetails.displayNumber.toString(); } else { nameForDefaultImage = firstDetails.name.toString(); } @@ -459,47 +448,37 @@ public class CallDetailActivity extends Activity { // Formatted phone number. final CharSequence formattedNumber; - // Read contact specifics. - final CharSequence nameText; - final int numberType; - final CharSequence numberLabel; - final Uri photoUri; - final Uri lookupUri; - int sourceType; + // If this is not a regular number, there is no point in looking it up in the contacts. - ContactInfo info = - PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation) - && !new PhoneNumberUtilsWrapper(this).isVoicemailNumber(accountHandle, number) - ? mContactInfoHelper.lookupNumber(number, countryIso) - : null; + ContactInfo info = ContactInfo.EMPTY; + final boolean isVoicemail = new PhoneNumberUtilsWrapper(this) + .isVoicemailNumber(accountHandle, number); + if (PhoneNumberUtilsWrapper.canPlaceCallsTo(number, numberPresentation) + && !isVoicemail) { + mContactInfoHelper.lookupNumber(number, countryIso); + } if (info == null) { - formattedNumber = mPhoneNumberHelper.getDisplayNumber(accountHandle, number, - numberPresentation, null); - nameText = ""; - numberType = 0; - numberLabel = ""; - photoUri = null; - lookupUri = null; - sourceType = 0; + formattedNumber = PhoneNumberDisplayUtil.getDisplayNumber( + this, + accountHandle, + number, + numberPresentation, + null /* formattedNumber */, + isVoicemail); } else { formattedNumber = info.formattedNumber; - nameText = info.name; - numberType = info.type; - numberLabel = info.label; - photoUri = info.photoUri; - lookupUri = info.lookupUri; - sourceType = info.sourceType; } final int features = callCursor.getInt(FEATURES); Long dataUsage = null; if (!callCursor.isNull(DATA_USAGE)) { dataUsage = callCursor.getLong(DATA_USAGE); } - return new PhoneCallDetails(number, numberPresentation, + return new PhoneCallDetails(this, number, numberPresentation, formattedNumber, countryIso, geocode, new int[]{ callType }, date, duration, - nameText, numberType, numberLabel, lookupUri, photoUri, sourceType, - accountHandle, features, dataUsage, transcription); + info.name, info.type, info.label, info.lookupUri, info.photoUri, + info.sourceType, accountHandle, features, dataUsage, transcription, + isVoicemail); } finally { if (callCursor != null) { callCursor.close(); diff --git a/src/com/android/dialer/PhoneCallDetails.java b/src/com/android/dialer/PhoneCallDetails.java index ec9657ed8..843e19352 100644 --- a/src/com/android/dialer/PhoneCallDetails.java +++ b/src/com/android/dialer/PhoneCallDetails.java @@ -17,15 +17,20 @@ package com.android.dialer; import com.google.common.annotations.VisibleForTesting; +import com.android.dialer.calllog.PhoneNumberDisplayUtil; +import android.content.Context; import android.graphics.drawable.Drawable; import android.net.Uri; import android.provider.CallLog.Calls; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.telecom.PhoneAccountHandle; +import android.text.TextUtils; /** * The details of a phone call to be shown in the UI. + * + * TODO: Create a builder, to make it easier to construct an instance. */ public class PhoneCallDetails { /** The number of the other party involved in the call. */ @@ -85,35 +90,40 @@ public class PhoneCallDetails { */ public final String transcription; + public final String displayNumber; + public final boolean isVoicemail; + /** * Create the details for a call, with empty defaults specified for extra fields that are * not necessary for testing. */ @VisibleForTesting - public PhoneCallDetails(CharSequence number, int numberPresentation, + public PhoneCallDetails(Context context, CharSequence number, int numberPresentation, CharSequence formattedNumber, String countryIso, String geocode, - int[] callTypes, long date, long duration) { - this (number, numberPresentation, formattedNumber, countryIso, geocode, - callTypes, date, duration, "", 0, "", null, null, 0, null, 0, null, null); + int[] callTypes, long date, long duration, boolean isVoicemail) { + this(context, number, numberPresentation, formattedNumber, countryIso, geocode, + callTypes, date, duration, "", 0, "", null, null, 0, null, 0, null, null, + isVoicemail); } /** Create the details for a call with a number not associated with a contact. */ - public PhoneCallDetails(CharSequence number, int numberPresentation, + public PhoneCallDetails(Context context, CharSequence number, int numberPresentation, CharSequence formattedNumber, String countryIso, String geocode, int[] callTypes, long date, long duration, - PhoneAccountHandle accountHandle, int features, Long dataUsage, String transcription) { - this(number, numberPresentation, formattedNumber, countryIso, geocode, callTypes, date, - duration, "", 0, "", null, null, 0, accountHandle, features, dataUsage, - transcription); + PhoneAccountHandle accountHandle, int features, Long dataUsage, String transcription, + boolean isVoicemail) { + this(context, number, numberPresentation, formattedNumber, countryIso, geocode, + callTypes, date, duration, "", 0, "", null, null, 0, accountHandle, features, + dataUsage, transcription, isVoicemail); } /** Create the details for a call with a number associated with a contact. */ - public PhoneCallDetails(CharSequence number, int numberPresentation, + public PhoneCallDetails(Context context, CharSequence number, int numberPresentation, CharSequence formattedNumber, String countryIso, String geocode, int[] callTypes, long date, long duration, CharSequence name, int numberType, CharSequence numberLabel, Uri contactUri, Uri photoUri, int sourceType, PhoneAccountHandle accountHandle, int features, Long dataUsage, - String transcription) { + String transcription, boolean isVoicemail) { this.number = number; this.numberPresentation = numberPresentation; this.formattedNumber = formattedNumber; @@ -132,5 +142,14 @@ public class PhoneCallDetails { this.features = features; this.dataUsage = dataUsage; this.transcription = transcription; + this.isVoicemail = isVoicemail; + + this.displayNumber = PhoneNumberDisplayUtil.getDisplayNumber( + context, + this.accountHandle, + this.number, + this.numberPresentation, + this.formattedNumber, + this.isVoicemail).toString(); } } diff --git a/src/com/android/dialer/PhoneCallDetailsHelper.java b/src/com/android/dialer/PhoneCallDetailsHelper.java index 7855a1d13..db37bb3f3 100644 --- a/src/com/android/dialer/PhoneCallDetailsHelper.java +++ b/src/com/android/dialer/PhoneCallDetailsHelper.java @@ -32,7 +32,6 @@ import com.android.contacts.common.testing.NeededForTesting; import com.android.contacts.common.util.PhoneNumberHelper; import com.android.dialer.calllog.ContactInfo; import com.android.dialer.calllog.PhoneAccountUtils; -import com.android.dialer.calllog.PhoneNumberDisplayHelper; import com.android.dialer.calllog.PhoneNumberUtilsWrapper; import com.android.dialer.util.DialerUtils; @@ -52,7 +51,6 @@ public class PhoneCallDetailsHelper { /** The injected current time in milliseconds since the epoch. Used only by tests. */ private Long mCurrentTimeMillisForTest; // Helper classes. - private final PhoneNumberDisplayHelper mPhoneNumberHelper; private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper; /** @@ -72,7 +70,6 @@ public class PhoneCallDetailsHelper { mContext = context; mResources = resources; mPhoneNumberUtilsWrapper = phoneUtils; - mPhoneNumberHelper = new PhoneNumberDisplayHelper(context, resources, phoneUtils); } /** Fills the call details views with content. */ @@ -125,9 +122,7 @@ public class PhoneCallDetailsHelper { } final CharSequence nameText; - final CharSequence displayNumber = - mPhoneNumberHelper.getDisplayNumber(details.accountHandle, details.number, - details.numberPresentation, details.formattedNumber); + final CharSequence displayNumber = details.displayNumber; if (TextUtils.isEmpty(details.name)) { nameText = displayNumber; // We have a real phone number as "nameView" so make it always LTR @@ -195,8 +190,7 @@ public class PhoneCallDetailsHelper { } if (!TextUtils.isEmpty(details.name) && TextUtils.isEmpty(numberFormattedLabel)) { - numberFormattedLabel = mPhoneNumberHelper.getDisplayNumber(details.accountHandle, - details.number, details.numberPresentation, details.formattedNumber); + numberFormattedLabel = details.displayNumber; } return numberFormattedLabel; } @@ -218,12 +212,8 @@ public class PhoneCallDetailsHelper { @NeededForTesting public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) { final CharSequence nameText; - final CharSequence displayNumber = - mPhoneNumberHelper.getDisplayNumber(details.accountHandle, details.number, - details.numberPresentation, - mResources.getString(R.string.recentCalls_addToContact)); if (TextUtils.isEmpty(details.name)) { - nameText = displayNumber; + nameText = mResources.getString(R.string.recentCalls_addToContact); } else { nameText = details.name; } diff --git a/src/com/android/dialer/calllog/CallLogAdapter.java b/src/com/android/dialer/calllog/CallLogAdapter.java index b4fed867f..608475e0b 100644 --- a/src/com/android/dialer/calllog/CallLogAdapter.java +++ b/src/com/android/dialer/calllog/CallLogAdapter.java @@ -22,6 +22,7 @@ import android.content.res.Resources; import android.database.Cursor; import android.net.Uri; import android.support.v7.widget.RecyclerView; +import android.os.Trace; import android.support.v7.widget.RecyclerView.ViewHolder; import android.telecom.PhoneAccountHandle; import android.telephony.PhoneNumberUtils; @@ -98,8 +99,6 @@ public class CallLogAdapter extends GroupingListAdapter /** Instance of helper class for managing views. */ private final CallLogListItemHelper mCallLogViewsHelper; - /** Helper to parse and process phone numbers. */ - private PhoneNumberDisplayHelper mPhoneNumberHelper; /** Helper to access Telephony phone number utils class */ protected final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper; /** Helper to group call log entries. */ @@ -210,12 +209,10 @@ public class CallLogAdapter extends GroupingListAdapter Resources resources = mContext.getResources(); CallTypeHelper callTypeHelper = new CallTypeHelper(resources); - mPhoneNumberHelper = new PhoneNumberDisplayHelper(mContext, resources); mPhoneNumberUtilsWrapper = new PhoneNumberUtilsWrapper(mContext); PhoneCallDetailsHelper phoneCallDetailsHelper = new PhoneCallDetailsHelper(mContext, resources, mPhoneNumberUtilsWrapper); - mCallLogViewsHelper = - new CallLogListItemHelper(phoneCallDetailsHelper, mPhoneNumberHelper, resources); + mCallLogViewsHelper = new CallLogListItemHelper(phoneCallDetailsHelper, resources); mCallLogGroupBuilder = new CallLogGroupBuilder(this); } @@ -312,9 +309,10 @@ public class CallLogAdapter extends GroupingListAdapter if (getItemViewType(position) == VIEW_TYPE_SHOW_CALL_HISTORY_LIST_ITEM) { return; } - + Trace.beginSection("onBindViewHolder: " + position); Cursor c = (Cursor) getItem(position); if (c == null) { + Trace.endSection(); return; } int count = getGroupSize(position); @@ -408,21 +406,21 @@ public class CallLogAdapter extends GroupingListAdapter views.showActions(mCurrentlyExpandedPosition == position, mOnReportButtonClickListener); if (TextUtils.isEmpty(name)) { - details = new PhoneCallDetails(number, numberPresentation, formattedNumber, countryIso, - geocode, callTypes, date, duration, accountHandle, features, dataUsage, - transcription); + details = new PhoneCallDetails(mContext, number, numberPresentation, formattedNumber, + countryIso, geocode, callTypes, date, duration, accountHandle, features, + dataUsage, transcription, isVoicemailNumber); } else { - details = new PhoneCallDetails(number, numberPresentation, formattedNumber, countryIso, - geocode, callTypes, date, duration, name, ntype, label, lookupUri, photoUri, - sourceType, accountHandle, features, dataUsage, transcription); + details = new PhoneCallDetails(mContext, number, numberPresentation, formattedNumber, + countryIso, geocode, callTypes, date, duration, name, ntype, label, lookupUri, + photoUri, sourceType, accountHandle, features, dataUsage, transcription, + isVoicemailNumber); } mCallLogViewsHelper.setPhoneCallDetails(mContext, views, details); String nameForDefaultImage = null; if (TextUtils.isEmpty(name)) { - nameForDefaultImage = mPhoneNumberHelper.getDisplayNumber(details.accountHandle, - details.number, details.numberPresentation, details.formattedNumber).toString(); + nameForDefaultImage = details.displayNumber; } else { nameForDefaultImage = name; } @@ -437,6 +435,7 @@ public class CallLogAdapter extends GroupingListAdapter mViewTreeObserver = views.rootView.getViewTreeObserver(); mViewTreeObserver.addOnPreDrawListener(this); } + Trace.endSection(); } @Override diff --git a/src/com/android/dialer/calllog/CallLogListItemHelper.java b/src/com/android/dialer/calllog/CallLogListItemHelper.java index 7ec6752e4..147a192e3 100644 --- a/src/com/android/dialer/calllog/CallLogListItemHelper.java +++ b/src/com/android/dialer/calllog/CallLogListItemHelper.java @@ -23,7 +23,6 @@ import android.text.SpannableStringBuilder; import android.text.TextUtils; import android.util.Log; -import com.android.contacts.common.CallUtil; import com.android.dialer.PhoneCallDetails; import com.android.dialer.PhoneCallDetailsHelper; import com.android.dialer.R; @@ -36,8 +35,6 @@ import com.android.dialer.R; /** Helper for populating the details of a phone call. */ private final PhoneCallDetailsHelper mPhoneCallDetailsHelper; - /** Helper for handling phone numbers. */ - private final PhoneNumberDisplayHelper mPhoneNumberHelper; /** Resources to look up strings. */ private final Resources mResources; @@ -47,10 +44,9 @@ import com.android.dialer.R; * @param phoneCallDetailsHelper used to set the details of a phone call * @param phoneNumberHelper used to process phone number */ - public CallLogListItemHelper(PhoneCallDetailsHelper phoneCallDetailsHelper, - PhoneNumberDisplayHelper phoneNumberHelper, Resources resources) { + public CallLogListItemHelper( + PhoneCallDetailsHelper phoneCallDetailsHelper, Resources resources) { mPhoneCallDetailsHelper = phoneCallDetailsHelper; - mPhoneNumberHelper = phoneNumberHelper; mResources = resources; } @@ -73,7 +69,7 @@ import com.android.dialer.R; // Cache name or number of caller. Used when setting the content descriptions of buttons // when the actions ViewStub is inflated. - views.nameOrNumber = this.getNameOrNumber(details); + views.nameOrNumber = getNameOrNumber(details); } /** @@ -190,8 +186,7 @@ import com.android.dialer.R; } // If call had video capabilities, add the "Video Call" string. - if ((details.features & Calls.FEATURES_VIDEO) == Calls.FEATURES_VIDEO && - CallUtil.isVideoEnabled(context)) { + if ((details.features & Calls.FEATURES_VIDEO) == Calls.FEATURES_VIDEO) { callDescription.append(mResources.getString(R.string.description_video_call)); } @@ -264,8 +259,7 @@ import com.android.dialer.R; if (!TextUtils.isEmpty(details.name)) { recipient = details.name; } else { - recipient = mPhoneNumberHelper.getDisplayNumber(details.accountHandle, - details.number, details.numberPresentation, details.formattedNumber); + recipient = details.displayNumber; } return recipient; } diff --git a/src/com/android/dialer/calllog/CallLogListItemViewHolder.java b/src/com/android/dialer/calllog/CallLogListItemViewHolder.java index a7dd6aff8..ccd480ec6 100644 --- a/src/com/android/dialer/calllog/CallLogListItemViewHolder.java +++ b/src/com/android/dialer/calllog/CallLogListItemViewHolder.java @@ -411,8 +411,6 @@ public final class CallLogListItemViewHolder extends RecyclerView.ViewHolder { @NeededForTesting public static CallLogListItemViewHolder createForTest(Context context) { Resources resources = context.getResources(); - PhoneNumberDisplayHelper phoneNumberHelper = - new PhoneNumberDisplayHelper(context, resources); PhoneNumberUtilsWrapper phoneNumberUtilsWrapper = new PhoneNumberUtilsWrapper(context); PhoneCallDetailsHelper phoneCallDetailsHelper = new PhoneCallDetailsHelper( context, resources, phoneNumberUtilsWrapper); @@ -421,8 +419,7 @@ public final class CallLogListItemViewHolder extends RecyclerView.ViewHolder { context, null /* actionListener */, phoneNumberUtilsWrapper, - new CallLogListItemHelper( - phoneCallDetailsHelper, phoneNumberHelper, resources), + new CallLogListItemHelper(phoneCallDetailsHelper, resources), new View(context), new QuickContactBadge(context), new View(context), diff --git a/src/com/android/dialer/calllog/DefaultVoicemailNotifier.java b/src/com/android/dialer/calllog/DefaultVoicemailNotifier.java index 99ca8db10..7c2a96638 100644 --- a/src/com/android/dialer/calllog/DefaultVoicemailNotifier.java +++ b/src/com/android/dialer/calllog/DefaultVoicemailNotifier.java @@ -58,7 +58,6 @@ public class DefaultVoicemailNotifier { private final NotificationManager mNotificationManager; private final NewCallsQuery mNewCallsQuery; private final NameLookupQuery mNameLookupQuery; - private final PhoneNumberDisplayHelper mPhoneNumberHelper; /** Returns the singleton instance of the {@link DefaultVoicemailNotifier}. */ public static synchronized DefaultVoicemailNotifier getInstance(Context context) { @@ -68,20 +67,18 @@ public class DefaultVoicemailNotifier { ContentResolver contentResolver = context.getContentResolver(); sInstance = new DefaultVoicemailNotifier(context, notificationManager, createNewCallsQuery(contentResolver), - createNameLookupQuery(contentResolver), - createPhoneNumberHelper(context)); + createNameLookupQuery(contentResolver)); } return sInstance; } private DefaultVoicemailNotifier(Context context, NotificationManager notificationManager, NewCallsQuery newCallsQuery, - NameLookupQuery nameLookupQuery, PhoneNumberDisplayHelper phoneNumberHelper) { + NameLookupQuery nameLookupQuery) { mContext = context; mNotificationManager = notificationManager; mNewCallsQuery = newCallsQuery; mNameLookupQuery = nameLookupQuery; - mPhoneNumberHelper = phoneNumberHelper; } /** @@ -128,8 +125,12 @@ public class DefaultVoicemailNotifier { PhoneAccountHandle accountHandle = PhoneAccountUtils.getAccount( newCall.accountComponentName, newCall.accountId); - name = mPhoneNumberHelper.getDisplayName(accountHandle, newCall.number, - newCall.numberPresentation).toString(); + name = PhoneNumberDisplayUtil.getDisplayName( + mContext, + accountHandle, + newCall.number, + newCall.numberPresentation, + /* isVoicemail */ false).toString(); // If we cannot lookup the contact, use the number instead. if (TextUtils.isEmpty(name)) { // Look it up in the database. @@ -338,14 +339,4 @@ public class DefaultVoicemailNotifier { } } } - - /** - * Create a new PhoneNumberHelper. - *

- * This will cause some Disk I/O, at least the first time it is created, so it should not be - * called from the main thread. - */ - public static PhoneNumberDisplayHelper createPhoneNumberHelper(Context context) { - return new PhoneNumberDisplayHelper(context, context.getResources()); - } } diff --git a/src/com/android/dialer/calllog/PhoneNumberDisplayHelper.java b/src/com/android/dialer/calllog/PhoneNumberDisplayHelper.java deleted file mode 100644 index c1a5abfe1..000000000 --- a/src/com/android/dialer/calllog/PhoneNumberDisplayHelper.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.dialer.calllog; - -import android.content.Context; -import android.content.res.Resources; -import android.provider.CallLog.Calls; -import android.telecom.PhoneAccountHandle; -import android.text.TextUtils; -import android.util.Log; - -import com.android.dialer.R; - -/** - * Helper for formatting and managing the display of phone numbers. - */ -public class PhoneNumberDisplayHelper { - private final Context mContext; - private final Resources mResources; - private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper; - - public PhoneNumberDisplayHelper(Context context, Resources resources) { - mContext = context; - mResources = resources; - mPhoneNumberUtilsWrapper = new PhoneNumberUtilsWrapper(context); - } - - public PhoneNumberDisplayHelper(Context context, Resources resources, - PhoneNumberUtilsWrapper phoneNumberUtils) { - mContext = context; - mResources = resources; - mPhoneNumberUtilsWrapper = phoneNumberUtils; - } - - /* package */ CharSequence getDisplayName(PhoneAccountHandle accountHandle, CharSequence number, - int presentation) { - if (presentation == Calls.PRESENTATION_UNKNOWN) { - return mResources.getString(R.string.unknown); - } - if (presentation == Calls.PRESENTATION_RESTRICTED) { - return mResources.getString(R.string.private_num); - } - if (presentation == Calls.PRESENTATION_PAYPHONE) { - return mResources.getString(R.string.payphone); - } - if (mPhoneNumberUtilsWrapper.isVoicemailNumber(accountHandle, number)) { - return mResources.getString(R.string.voicemail); - } - if (PhoneNumberUtilsWrapper.isLegacyUnknownNumbers(number)) { - return mResources.getString(R.string.unknown); - } - return ""; - } - - /** - * Returns the string to display for the given phone number. - * - * @param accountHandle The handle for the account corresponding to the call - * @param number the number to display - * @param formattedNumber the formatted number if available, may be null - */ - public CharSequence getDisplayNumber(PhoneAccountHandle accountHandle, CharSequence number, - int presentation, CharSequence formattedNumber) { - final CharSequence displayName = getDisplayName(accountHandle, number, presentation); - if (!TextUtils.isEmpty(displayName)) { - return displayName; - } - - if (TextUtils.isEmpty(number)) { - return ""; - } - - if (TextUtils.isEmpty(formattedNumber)) { - return number; - } else { - return formattedNumber; - } - } -} diff --git a/src/com/android/dialer/calllog/PhoneNumberDisplayUtil.java b/src/com/android/dialer/calllog/PhoneNumberDisplayUtil.java new file mode 100644 index 000000000..e7fcde263 --- /dev/null +++ b/src/com/android/dialer/calllog/PhoneNumberDisplayUtil.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.calllog; + +import android.content.Context; +import android.content.res.Resources; +import android.provider.CallLog.Calls; +import android.telecom.PhoneAccountHandle; +import android.text.TextUtils; +import android.util.Log; + +import com.android.dialer.R; + +/** + * Helper for formatting and managing the display of phone numbers. + */ +public class PhoneNumberDisplayUtil { + + /** + * Returns the string to display for the given phone number if there is no matching contact. + */ + /* package */ static CharSequence getDisplayName( + Context context, + PhoneAccountHandle accountHandle, + CharSequence number, + int presentation, + boolean isVoicemail) { + if (presentation == Calls.PRESENTATION_UNKNOWN) { + return context.getResources().getString(R.string.unknown); + } + if (presentation == Calls.PRESENTATION_RESTRICTED) { + return context.getResources().getString(R.string.private_num); + } + if (presentation == Calls.PRESENTATION_PAYPHONE) { + return context.getResources().getString(R.string.payphone); + } + if (isVoicemail) { + return context.getResources().getString(R.string.voicemail); + } + if (PhoneNumberUtilsWrapper.isLegacyUnknownNumbers(number)) { + return context.getResources().getString(R.string.unknown); + } + return ""; + } + + /** + * Returns the string to display for the given phone number. + * + * @param accountHandle The handle for the account corresponding to the call + * @param number the number to display + * @param formattedNumber the formatted number if available, may be null + */ + public static CharSequence getDisplayNumber( + Context context, + PhoneAccountHandle accountHandle, + CharSequence number, + int presentation, + CharSequence formattedNumber, + boolean isVoicemail) { + if (!TextUtils.isEmpty(formattedNumber)) { + return formattedNumber; + } + + final CharSequence displayName = + getDisplayName(context, accountHandle, number, presentation, isVoicemail); + if (!TextUtils.isEmpty(displayName)) { + return displayName; + } else if (!TextUtils.isEmpty(number)) { + return number; + } else { + return ""; + } + } +} -- cgit v1.2.3