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