PhoneCallDetailsHelper.java revision 87ba489564b25d4a64c9faaeafea46e2f72d8933
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.drawable.Drawable; 21import android.graphics.Typeface; 22import android.provider.ContactsContract.CommonDataKinds.Phone; 23import android.telecomm.Subscription; 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; 32import android.widget.ImageView; 33 34import com.android.contacts.common.testing.NeededForTesting; 35import com.android.contacts.common.util.PhoneNumberHelper; 36import com.android.dialer.calllog.CallTypeHelper; 37import com.android.dialer.calllog.ContactInfo; 38import com.android.dialer.calllog.PhoneNumberDisplayHelper; 39import com.android.dialer.calllog.PhoneNumberUtilsWrapper; 40 41import com.google.common.collect.Lists; 42 43import java.util.ArrayList; 44import java.util.List; 45 46/** 47 * Helper class to fill in the views in {@link PhoneCallDetailsViews}. 48 */ 49public class PhoneCallDetailsHelper { 50 /** The maximum number of icons will be shown to represent the call types in a group. */ 51 private static final int MAX_CALL_TYPE_ICONS = 3; 52 53 private final Resources mResources; 54 /** The injected current time in milliseconds since the epoch. Used only by tests. */ 55 private Long mCurrentTimeMillisForTest; 56 // Helper classes. 57 private final CallTypeHelper mCallTypeHelper; 58 private final PhoneNumberDisplayHelper mPhoneNumberHelper; 59 private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper; 60 61 /** 62 * List of items to be concatenated together for accessibility descriptions 63 */ 64 private ArrayList<CharSequence> mDescriptionItems = Lists.newArrayList(); 65 66 /** 67 * Creates a new instance of the helper. 68 * <p> 69 * Generally you should have a single instance of this helper in any context. 70 * 71 * @param resources used to look up strings 72 */ 73 public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper, 74 PhoneNumberUtilsWrapper phoneUtils) { 75 mResources = resources; 76 mCallTypeHelper = callTypeHelper; 77 mPhoneNumberUtilsWrapper = phoneUtils; 78 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mPhoneNumberUtilsWrapper, resources); 79 } 80 81 /** Fills the call details views with content. */ 82 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details) { 83 // Display up to a given number of icons. 84 views.callTypeIcons.clear(); 85 int count = details.callTypes.length; 86 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) { 87 views.callTypeIcons.add(details.callTypes[index]); 88 } 89 views.callTypeIcons.requestLayout(); 90 views.callTypeIcons.setVisibility(View.VISIBLE); 91 92 // Show the total call count only if there are more than the maximum number of icons. 93 final Integer callCount; 94 if (count > MAX_CALL_TYPE_ICONS) { 95 callCount = count; 96 } else { 97 callCount = null; 98 } 99 100 CharSequence callLocationAndDate = getCallLocationAndDate(details); 101 102 // Set the call count, location and date. 103 setCallCountAndDate(views, callCount, callLocationAndDate); 104 105 // set the subscription icon if it exists 106 if (details.subscriptionIcon != null) { 107 views.callSubscriptionIcon.setVisibility(View.VISIBLE); 108 views.callSubscriptionIcon.setImageDrawable(details.subscriptionIcon); 109 } else { 110 views.callSubscriptionIcon.setVisibility(View.GONE); 111 } 112 113 final CharSequence nameText; 114 final CharSequence displayNumber = 115 mPhoneNumberHelper.getDisplayNumber(details.number, 116 details.numberPresentation, details.formattedNumber); 117 if (TextUtils.isEmpty(details.name)) { 118 nameText = displayNumber; 119 // We have a real phone number as "nameView" so make it always LTR 120 views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR); 121 } else { 122 nameText = details.name; 123 } 124 125 views.nameView.setText(nameText); 126 127 // TODO: At the current time the voicemail transcription is not supported. This view 128 // is kept for future expansion when we may wish to show a transcription of voicemail. 129 views.voicemailTranscriptionView.setText(""); 130 views.voicemailTranscriptionView.setVisibility(View.GONE); 131 } 132 133 /** 134 * Builds a string containing the call location and date. 135 * 136 * @param details The call details. 137 * @return The call location and date string. 138 */ 139 private CharSequence getCallLocationAndDate(PhoneCallDetails details) { 140 mDescriptionItems.clear(); 141 142 // Get type of call (ie mobile, home, etc) if known, or the caller's location. 143 CharSequence callTypeOrLocation = getCallTypeOrLocation(details); 144 145 // Only add the call type or location if its not empty. It will be empty for unknown 146 // callers. 147 if (!TextUtils.isEmpty(callTypeOrLocation)) { 148 mDescriptionItems.add(callTypeOrLocation); 149 } 150 // The date of this call, relative to the current time. 151 mDescriptionItems.add(getCallDate(details)); 152 153 // Create a comma separated list from the call type or location, and call date. 154 return TextUtils.join(", " , mDescriptionItems); 155 } 156 157 /** 158 * For a call, if there is an associated contact for the caller, return the known call type 159 * (e.g. mobile, home, work). If there is no associated contact, attempt to use the caller's 160 * location if known. 161 * @param details Call details to use. 162 * @return Type of call (mobile/home) if known, or the location of the caller (if known). 163 */ 164 public CharSequence getCallTypeOrLocation(PhoneCallDetails details) { 165 CharSequence numberFormattedLabel = null; 166 // Only show a label if the number is shown and it is not a SIP address. 167 if (!TextUtils.isEmpty(details.number) 168 && !PhoneNumberHelper.isUriNumber(details.number.toString()) 169 && !mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) { 170 171 if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) { 172 numberFormattedLabel = details.geocode; 173 } else { 174 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType, 175 details.numberLabel); 176 } 177 } 178 179 if (!TextUtils.isEmpty(details.name) && TextUtils.isEmpty(numberFormattedLabel)) { 180 numberFormattedLabel = mPhoneNumberHelper.getDisplayNumber(details.number, 181 details.numberPresentation, details.formattedNumber); 182 } 183 return numberFormattedLabel; 184 } 185 186 /** 187 * Get the call date/time of the call, relative to the current time. 188 * e.g. 3 minutes ago 189 * @param details Call details to use. 190 * @return String representing when the call occurred. 191 */ 192 public CharSequence getCallDate(PhoneCallDetails details) { 193 return DateUtils.getRelativeTimeSpanString(details.date, 194 getCurrentTimeMillis(), 195 DateUtils.MINUTE_IN_MILLIS, 196 DateUtils.FORMAT_ABBREV_RELATIVE); 197 } 198 199 /** Sets the text of the header view for the details page of a phone call. */ 200 @NeededForTesting 201 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) { 202 final CharSequence nameText; 203 final CharSequence displayNumber = 204 mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation, 205 mResources.getString(R.string.recentCalls_addToContact)); 206 if (TextUtils.isEmpty(details.name)) { 207 nameText = displayNumber; 208 } else { 209 nameText = details.name; 210 } 211 212 nameView.setText(nameText); 213 } 214 215 @NeededForTesting 216 public void setCurrentTimeForTest(long currentTimeMillis) { 217 mCurrentTimeMillisForTest = currentTimeMillis; 218 } 219 220 /** 221 * Returns the current time in milliseconds since the epoch. 222 * <p> 223 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. 224 */ 225 private long getCurrentTimeMillis() { 226 if (mCurrentTimeMillisForTest == null) { 227 return System.currentTimeMillis(); 228 } else { 229 return mCurrentTimeMillisForTest; 230 } 231 } 232 233 /** Sets the call count and date. */ 234 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount, 235 CharSequence dateText) { 236 // Combine the count (if present) and the date. 237 final CharSequence text; 238 if (callCount != null) { 239 text = mResources.getString( 240 R.string.call_log_item_count_and_date, callCount.intValue(), dateText); 241 } else { 242 text = dateText; 243 } 244 245 views.callLocationAndDate.setText(text); 246 } 247} 248