PhoneCallDetailsHelper.java revision 45ed3b5932ed590b45235d7b2befa736a95e7f75
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 mPhoneNumberUtilsWrapper = phoneUtils; 67 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mPhoneNumberUtilsWrapper, resources); 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 = getCallDate(details); 95 96 // Set the call count and date. 97 setCallCountAndDate(views, callCount, dateText, highlightColor); 98 99 // Get type of call (ie mobile, home, etc) if known, or the caller's 100 CharSequence numberFormattedLabel = getCallTypeOrLocation(details); 101 102 final CharSequence nameText; 103 final CharSequence numberText; 104 final CharSequence labelText; 105 final CharSequence displayNumber = 106 mPhoneNumberHelper.getDisplayNumber(details.number, 107 details.numberPresentation, details.formattedNumber); 108 if (TextUtils.isEmpty(details.name)) { 109 nameText = displayNumber; 110 if (TextUtils.isEmpty(details.geocode) 111 || mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) { 112 numberText = mResources.getString(R.string.call_log_empty_geocode); 113 } else { 114 numberText = details.geocode; 115 } 116 labelText = numberText; 117 // We have a real phone number as "nameView" so make it always LTR 118 views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR); 119 } else { 120 nameText = details.name; 121 numberText = displayNumber; 122 labelText = TextUtils.isEmpty(numberFormattedLabel) ? numberText : 123 numberFormattedLabel; 124 } 125 126 views.nameView.setText(nameText); 127 views.labelView.setText(labelText); 128 views.labelView.setVisibility(TextUtils.isEmpty(labelText) ? View.GONE : View.VISIBLE); 129 } 130 131 /** 132 * For a call, if there is an associated contact for the caller, return the known call type 133 * (e.g. mobile, home, work). If there is no associated contact, attempt to use the caller's 134 * location if known. 135 * @param details Call details to use. 136 * @return Type of call (mobile/home) if known, or the location of the caller (if known). 137 */ 138 public CharSequence getCallTypeOrLocation(PhoneCallDetails details) { 139 CharSequence numberFormattedLabel = null; 140 // Only show a label if the number is shown and it is not a SIP address. 141 if (!TextUtils.isEmpty(details.number) 142 && !PhoneNumberHelper.isUriNumber(details.number.toString())) { 143 if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) { 144 numberFormattedLabel = details.geocode; 145 } else { 146 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType, 147 details.numberLabel); 148 } 149 } 150 return numberFormattedLabel; 151 } 152 153 /** 154 * Get the call date/time of the call, relative to the current time. 155 * e.g. 3 minutes ago 156 * @param details Call details to use. 157 * @return String representing when the call occurred. 158 */ 159 public CharSequence getCallDate(PhoneCallDetails details) { 160 return DateUtils.getRelativeTimeSpanString(details.date, 161 getCurrentTimeMillis(), 162 DateUtils.MINUTE_IN_MILLIS, 163 DateUtils.FORMAT_ABBREV_RELATIVE); 164 } 165 166 /** Sets the text of the header view for the details page of a phone call. */ 167 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) { 168 final CharSequence nameText; 169 final CharSequence displayNumber = 170 mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation, 171 mResources.getString(R.string.recentCalls_addToContact)); 172 if (TextUtils.isEmpty(details.name)) { 173 nameText = displayNumber; 174 } else { 175 nameText = details.name; 176 } 177 178 nameView.setText(nameText); 179 } 180 181 @NeededForTesting 182 public void setCurrentTimeForTest(long currentTimeMillis) { 183 mCurrentTimeMillisForTest = currentTimeMillis; 184 } 185 186 /** 187 * Returns the current time in milliseconds since the epoch. 188 * <p> 189 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. 190 */ 191 private long getCurrentTimeMillis() { 192 if (mCurrentTimeMillisForTest == null) { 193 return System.currentTimeMillis(); 194 } else { 195 return mCurrentTimeMillisForTest; 196 } 197 } 198 199 /** Sets the call count and date. */ 200 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount, 201 CharSequence dateText, Integer highlightColor) { 202 // Combine the count (if present) and the date. 203 final CharSequence text; 204 if (callCount != null) { 205 text = mResources.getString( 206 R.string.call_log_item_count_and_date, callCount.intValue(), dateText); 207 } else { 208 text = dateText; 209 } 210 211 // Apply the highlight color if present. 212 final CharSequence formattedText; 213 if (highlightColor != null) { 214 formattedText = addBoldAndColor(text, highlightColor); 215 } else { 216 formattedText = text; 217 } 218 219 views.callTypeAndDate.setText(formattedText); 220 } 221 222 /** Creates a SpannableString for the given text which is bold and in the given color. */ 223 private CharSequence addBoldAndColor(CharSequence text, int color) { 224 int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE; 225 SpannableString result = new SpannableString(text); 226 result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags); 227 result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags); 228 return result; 229 } 230} 231