blob: 05314128bb71c84d4d8ddee1bc5d67ad7492bd06 [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
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;
Nancy Chen87ba4892014-06-11 17:56:07 -070020import android.graphics.drawable.Drawable;
Chiao Cheng94b10b52012-08-17 16:59:12 -070021import android.graphics.Typeface;
22import android.provider.ContactsContract.CommonDataKinds.Phone;
Nancy Chen87ba4892014-06-11 17:56:07 -070023import android.telecomm.Subscription;
Chiao Cheng94b10b52012-08-17 16:59:12 -070024import 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;
Nancy Chen87ba4892014-06-11 17:56:07 -070032import android.widget.ImageView;
Chiao Cheng94b10b52012-08-17 16:59:12 -070033
Yorke Leee38e9ab2014-05-28 17:47:33 -070034import com.android.contacts.common.testing.NeededForTesting;
Yorke Lee37d29852013-11-19 14:11:24 -080035import com.android.contacts.common.util.PhoneNumberHelper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070036import com.android.dialer.calllog.CallTypeHelper;
Yorke Lee034a2b32013-08-26 15:46:10 -070037import com.android.dialer.calllog.ContactInfo;
Yorke Lee24ec3192013-11-19 13:52:45 -080038import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070039import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Nancy Chen87ba4892014-06-11 17:56:07 -070040
Yorke Lee97f5a1d2014-05-27 18:04:58 -070041import com.google.common.collect.Lists;
Tyler Gunn146a4cd2014-05-05 16:51:42 -070042
43import java.util.ArrayList;
44import java.util.List;
Chiao Cheng94b10b52012-08-17 16:59:12 -070045
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;
Yorke Lee24ec3192013-11-19 13:52:45 -080058 private final PhoneNumberDisplayHelper mPhoneNumberHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070059 private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070060
61 /**
Tyler Gunn146a4cd2014-05-05 16:51:42 -070062 * List of items to be concatenated together for accessibility descriptions
63 */
64 private ArrayList<CharSequence> mDescriptionItems = Lists.newArrayList();
65
66 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -070067 * 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,
Chiao Chengfb0a9342013-09-13 17:27:42 -070074 PhoneNumberUtilsWrapper phoneUtils) {
Chiao Cheng94b10b52012-08-17 16:59:12 -070075 mResources = resources;
76 mCallTypeHelper = callTypeHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070077 mPhoneNumberUtilsWrapper = phoneUtils;
Yorke Lee36717252013-11-20 15:02:49 -080078 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mPhoneNumberUtilsWrapper, resources);
Chiao Cheng94b10b52012-08-17 16:59:12 -070079 }
80
81 /** Fills the call details views with content. */
Tyler Gunnd0715ac2014-05-12 10:52:32 -070082 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details) {
Chiao Cheng94b10b52012-08-17 16:59:12 -070083 // 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 }
Yorke Lee7e8ea192013-09-04 17:36:07 -070089 views.callTypeIcons.requestLayout();
Chiao Cheng94b10b52012-08-17 16:59:12 -070090 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 }
Chiao Cheng94b10b52012-08-17 16:59:12 -070099
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700100 CharSequence callLocationAndDate = getCallLocationAndDate(details);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700102 // Set the call count, location and date.
Tyler Gunnd0715ac2014-05-12 10:52:32 -0700103 setCallCountAndDate(views, callCount, callLocationAndDate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700104
Nancy Chen87ba4892014-06-11 17:56:07 -0700105 // 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
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 final CharSequence nameText;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700114 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700115 mPhoneNumberHelper.getDisplayNumber(details.number,
116 details.numberPresentation, details.formattedNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700117 if (TextUtils.isEmpty(details.name)) {
118 nameText = displayNumber;
Fabrice Di Meglioc341db02013-04-03 21:11:37 -0700119 // We have a real phone number as "nameView" so make it always LTR
120 views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121 } else {
122 nameText = details.name;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700123 }
124
125 views.nameView.setText(nameText);
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700126
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.
Yorke Lee990b6fc2014-06-12 15:57:55 -0700154 return TextUtils.join(", " , mDescriptionItems);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700155 }
156
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800157 /**
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)
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700168 && !PhoneNumberHelper.isUriNumber(details.number.toString())
169 && !mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) {
170
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800171 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 }
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700178
179 if (!TextUtils.isEmpty(details.name) && TextUtils.isEmpty(numberFormattedLabel)) {
180 numberFormattedLabel = mPhoneNumberHelper.getDisplayNumber(details.number,
181 details.numberPresentation, details.formattedNumber);
182 }
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800183 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
Chiao Cheng94b10b52012-08-17 16:59:12 -0700199 /** Sets the text of the header view for the details page of a phone call. */
Yorke Lee7d20f822014-06-19 17:09:33 -0700200 @NeededForTesting
Chiao Cheng94b10b52012-08-17 16:59:12 -0700201 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
202 final CharSequence nameText;
203 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700204 mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700205 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,
Tyler Gunnd0715ac2014-05-12 10:52:32 -0700235 CharSequence dateText) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700236 // 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
Tyler Gunnd0715ac2014-05-12 10:52:32 -0700245 views.callLocationAndDate.setText(text);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700246 }
247}