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 17 package com.android.dialer; 18 19 import android.content.res.Resources; 20 import android.provider.CallLog; 21 import android.provider.CallLog.Calls; 22 import android.provider.ContactsContract.CommonDataKinds.Phone; 23 import android.text.TextUtils; 24 import android.text.format.DateUtils; 25 import android.view.View; 26 import android.widget.TextView; 27 28 import com.android.contacts.common.CallUtil; 29 import com.android.contacts.common.testing.NeededForTesting; 30 import com.android.contacts.common.util.PhoneNumberHelper; 31 import com.android.dialer.calllog.CallTypeHelper; 32 import com.android.dialer.calllog.ContactInfo; 33 import com.android.dialer.calllog.PhoneNumberDisplayHelper; 34 import com.android.dialer.calllog.PhoneNumberUtilsWrapper; 35 import com.android.dialer.util.DialerUtils; 36 import com.google.common.collect.Lists; 37 38 import java.util.ArrayList; 39 40 /** 41 * Helper class to fill in the views in {@link PhoneCallDetailsViews}. 42 */ 43 public 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 PhoneNumberDisplayHelper mPhoneNumberHelper; 52 private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper; 53 54 /** 55 * List of items to be concatenated together for accessibility descriptions 56 */ 57 private ArrayList<CharSequence> mDescriptionItems = Lists.newArrayList(); 58 59 /** 60 * Creates a new instance of the helper. 61 * <p> 62 * Generally you should have a single instance of this helper in any context. 63 * 64 * @param resources used to look up strings 65 */ 66 public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper, 67 PhoneNumberUtilsWrapper phoneUtils) { 68 mResources = resources; 69 mPhoneNumberUtilsWrapper = phoneUtils; 70 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mPhoneNumberUtilsWrapper, resources); 71 } 72 73 /** Fills the call details views with content. */ 74 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details) { 75 // Display up to a given number of icons. 76 views.callTypeIcons.clear(); 77 int count = details.callTypes.length; 78 boolean isVoicemail = false; 79 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) { 80 views.callTypeIcons.add(details.callTypes[index]); 81 if (index == 0) { 82 isVoicemail = details.callTypes[index] == Calls.VOICEMAIL_TYPE; 83 } 84 } 85 86 // Show the video icon if the call had video enabled. 87 views.callTypeIcons.setShowVideo( 88 (details.features & Calls.FEATURES_VIDEO) == Calls.FEATURES_VIDEO); 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 account icon if it exists 106 if (details.accountIcon != null) { 107 views.callAccountIcon.setVisibility(View.VISIBLE); 108 views.callAccountIcon.setImageDrawable(details.accountIcon); 109 } else { 110 views.callAccountIcon.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 if (isVoicemail && !TextUtils.isEmpty(details.transcription)) { 128 views.voicemailTranscriptionView.setText(details.transcription); 129 views.voicemailTranscriptionView.setVisibility(View.VISIBLE); 130 } else { 131 views.voicemailTranscriptionView.setText(null); 132 views.voicemailTranscriptionView.setVisibility(View.GONE); 133 } 134 } 135 136 /** 137 * Builds a string containing the call location and date. 138 * 139 * @param details The call details. 140 * @return The call location and date string. 141 */ 142 private CharSequence getCallLocationAndDate(PhoneCallDetails details) { 143 mDescriptionItems.clear(); 144 145 // Get type of call (ie mobile, home, etc) if known, or the caller's location. 146 CharSequence callTypeOrLocation = getCallTypeOrLocation(details); 147 148 // Only add the call type or location if its not empty. It will be empty for unknown 149 // callers. 150 if (!TextUtils.isEmpty(callTypeOrLocation)) { 151 mDescriptionItems.add(callTypeOrLocation); 152 } 153 // The date of this call, relative to the current time. 154 mDescriptionItems.add(getCallDate(details)); 155 156 // Create a comma separated list from the call type or location, and call date. 157 return DialerUtils.join(mResources, mDescriptionItems); 158 } 159 160 /** 161 * For a call, if there is an associated contact for the caller, return the known call type 162 * (e.g. mobile, home, work). If there is no associated contact, attempt to use the caller's 163 * location if known. 164 * @param details Call details to use. 165 * @return Type of call (mobile/home) if known, or the location of the caller (if known). 166 */ 167 public CharSequence getCallTypeOrLocation(PhoneCallDetails details) { 168 CharSequence numberFormattedLabel = null; 169 // Only show a label if the number is shown and it is not a SIP address. 170 if (!TextUtils.isEmpty(details.number) 171 && !PhoneNumberHelper.isUriNumber(details.number.toString()) 172 && !mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) { 173 174 if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) { 175 numberFormattedLabel = details.geocode; 176 } else { 177 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType, 178 details.numberLabel); 179 } 180 } 181 182 if (!TextUtils.isEmpty(details.name) && TextUtils.isEmpty(numberFormattedLabel)) { 183 numberFormattedLabel = mPhoneNumberHelper.getDisplayNumber(details.number, 184 details.numberPresentation, details.formattedNumber); 185 } 186 return numberFormattedLabel; 187 } 188 189 /** 190 * Get the call date/time of the call, relative to the current time. 191 * e.g. 3 minutes ago 192 * @param details Call details to use. 193 * @return String representing when the call occurred. 194 */ 195 public CharSequence getCallDate(PhoneCallDetails details) { 196 return DateUtils.getRelativeTimeSpanString(details.date, 197 getCurrentTimeMillis(), 198 DateUtils.MINUTE_IN_MILLIS, 199 DateUtils.FORMAT_ABBREV_RELATIVE); 200 } 201 202 /** Sets the text of the header view for the details page of a phone call. */ 203 @NeededForTesting 204 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) { 205 final CharSequence nameText; 206 final CharSequence displayNumber = 207 mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation, 208 mResources.getString(R.string.recentCalls_addToContact)); 209 if (TextUtils.isEmpty(details.name)) { 210 nameText = displayNumber; 211 } else { 212 nameText = details.name; 213 } 214 215 nameView.setText(nameText); 216 } 217 218 @NeededForTesting 219 public void setCurrentTimeForTest(long currentTimeMillis) { 220 mCurrentTimeMillisForTest = currentTimeMillis; 221 } 222 223 /** 224 * Returns the current time in milliseconds since the epoch. 225 * <p> 226 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. 227 */ 228 private long getCurrentTimeMillis() { 229 if (mCurrentTimeMillisForTest == null) { 230 return System.currentTimeMillis(); 231 } else { 232 return mCurrentTimeMillisForTest; 233 } 234 } 235 236 /** Sets the call count and date. */ 237 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount, 238 CharSequence dateText) { 239 // Combine the count (if present) and the date. 240 final CharSequence text; 241 if (callCount != null) { 242 text = mResources.getString( 243 R.string.call_log_item_count_and_date, callCount.intValue(), dateText); 244 } else { 245 text = dateText; 246 } 247 248 views.callLocationAndDate.setText(text); 249 } 250 } 251