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