PhoneCallDetailsHelper.java revision 37d29854313ca0a7807ac4fd5d10d154b79ee2eb
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 17package com.android.dialer; 18 19import android.content.res.Resources; 20import android.graphics.Typeface; 21import android.provider.ContactsContract; 22import android.provider.ContactsContract.CommonDataKinds.Phone; 23import android.telephony.PhoneNumberUtils; 24import android.text.SpannableString; 25import android.text.Spanned; 26import android.text.TextUtils; 27import android.text.format.DateUtils; 28import android.text.style.ForegroundColorSpan; 29import android.text.style.StyleSpan; 30import android.view.View; 31import android.widget.TextView; 32 33import com.android.contacts.common.test.NeededForTesting; 34import com.android.contacts.common.util.PhoneNumberHelper; 35import com.android.dialer.calllog.CallTypeHelper; 36import com.android.dialer.calllog.ContactInfo; 37import com.android.dialer.calllog.PhoneNumberDisplayHelper; 38import com.android.dialer.calllog.PhoneNumberUtilsWrapper; 39 40/** 41 * Helper class to fill in the views in {@link PhoneCallDetailsViews}. 42 */ 43public class PhoneCallDetailsHelper { 44 /** The maximum number of icons will be shown to represent the call types in a group. */ 45 private static final int MAX_CALL_TYPE_ICONS = 3; 46 47 private final Resources mResources; 48 /** The injected current time in milliseconds since the epoch. Used only by tests. */ 49 private Long mCurrentTimeMillisForTest; 50 // Helper classes. 51 private final CallTypeHelper mCallTypeHelper; 52 private final PhoneNumberDisplayHelper mPhoneNumberHelper; 53 private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper; 54 55 /** 56 * Creates a new instance of the helper. 57 * <p> 58 * Generally you should have a single instance of this helper in any context. 59 * 60 * @param resources used to look up strings 61 */ 62 public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper, 63 PhoneNumberUtilsWrapper phoneUtils) { 64 mResources = resources; 65 mCallTypeHelper = callTypeHelper; 66 mPhoneNumberHelper = new PhoneNumberDisplayHelper(resources); 67 mPhoneNumberUtilsWrapper = phoneUtils; 68 } 69 70 /** Fills the call details views with content. */ 71 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details, 72 boolean isHighlighted) { 73 // Display up to a given number of icons. 74 views.callTypeIcons.clear(); 75 int count = details.callTypes.length; 76 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) { 77 views.callTypeIcons.add(details.callTypes[index]); 78 } 79 views.callTypeIcons.requestLayout(); 80 views.callTypeIcons.setVisibility(View.VISIBLE); 81 82 // Show the total call count only if there are more than the maximum number of icons. 83 final Integer callCount; 84 if (count > MAX_CALL_TYPE_ICONS) { 85 callCount = count; 86 } else { 87 callCount = null; 88 } 89 // The color to highlight the count and date in, if any. This is based on the first call. 90 Integer highlightColor = 91 isHighlighted ? mCallTypeHelper.getHighlightedColor(details.callTypes[0]) : null; 92 93 // The date of this call, relative to the current time. 94 CharSequence dateText = 95 DateUtils.getRelativeTimeSpanString(details.date, 96 getCurrentTimeMillis(), 97 DateUtils.MINUTE_IN_MILLIS, 98 DateUtils.FORMAT_ABBREV_RELATIVE); 99 100 // Set the call count and date. 101 setCallCountAndDate(views, callCount, dateText, highlightColor); 102 103 CharSequence numberFormattedLabel = null; 104 // Only show a label if the number is shown and it is not a SIP address. 105 if (!TextUtils.isEmpty(details.number) 106 && !PhoneNumberHelper.isUriNumber(details.number.toString())) { 107 if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) { 108 numberFormattedLabel = details.geocode; 109 } else { 110 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType, 111 details.numberLabel); 112 } 113 } 114 115 final CharSequence nameText; 116 final CharSequence numberText; 117 final CharSequence labelText; 118 final CharSequence displayNumber = 119 mPhoneNumberHelper.getDisplayNumber(details.number, 120 details.numberPresentation, details.formattedNumber); 121 if (TextUtils.isEmpty(details.name)) { 122 nameText = displayNumber; 123 if (TextUtils.isEmpty(details.geocode) 124 || mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) { 125 numberText = mResources.getString(R.string.call_log_empty_gecode); 126 } else { 127 numberText = details.geocode; 128 } 129 labelText = numberText; 130 // We have a real phone number as "nameView" so make it always LTR 131 views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR); 132 } else { 133 nameText = details.name; 134 numberText = displayNumber; 135 labelText = TextUtils.isEmpty(numberFormattedLabel) ? numberText : 136 numberFormattedLabel; 137 } 138 139 views.nameView.setText(nameText); 140 141 views.labelView.setText(labelText); 142 views.labelView.setVisibility(TextUtils.isEmpty(labelText) ? View.GONE : View.VISIBLE); 143 } 144 145 /** Sets the text of the header view for the details page of a phone call. */ 146 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) { 147 final CharSequence nameText; 148 final CharSequence displayNumber = 149 mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation, 150 mResources.getString(R.string.recentCalls_addToContact)); 151 if (TextUtils.isEmpty(details.name)) { 152 nameText = displayNumber; 153 } else { 154 nameText = details.name; 155 } 156 157 nameView.setText(nameText); 158 } 159 160 @NeededForTesting 161 public void setCurrentTimeForTest(long currentTimeMillis) { 162 mCurrentTimeMillisForTest = currentTimeMillis; 163 } 164 165 /** 166 * Returns the current time in milliseconds since the epoch. 167 * <p> 168 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. 169 */ 170 private long getCurrentTimeMillis() { 171 if (mCurrentTimeMillisForTest == null) { 172 return System.currentTimeMillis(); 173 } else { 174 return mCurrentTimeMillisForTest; 175 } 176 } 177 178 /** Sets the call count and date. */ 179 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount, 180 CharSequence dateText, Integer highlightColor) { 181 // Combine the count (if present) and the date. 182 final CharSequence text; 183 if (callCount != null) { 184 text = mResources.getString( 185 R.string.call_log_item_count_and_date, callCount.intValue(), dateText); 186 } else { 187 text = dateText; 188 } 189 190 // Apply the highlight color if present. 191 final CharSequence formattedText; 192 if (highlightColor != null) { 193 formattedText = addBoldAndColor(text, highlightColor); 194 } else { 195 formattedText = text; 196 } 197 198 views.callTypeAndDate.setText(formattedText); 199 } 200 201 /** Creates a SpannableString for the given text which is bold and in the given color. */ 202 private CharSequence addBoldAndColor(CharSequence text, int color) { 203 int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE; 204 SpannableString result = new SpannableString(text); 205 result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags); 206 result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags); 207 return result; 208 } 209} 210