blob: edd08311776ddd8187b9851f5f49105122508c82 [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;
20import android.graphics.Typeface;
21import android.provider.ContactsContract.CommonDataKinds.Phone;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.text.SpannableString;
23import android.text.Spanned;
24import android.text.TextUtils;
25import android.text.format.DateUtils;
26import android.text.style.ForegroundColorSpan;
27import android.text.style.StyleSpan;
28import android.view.View;
29import android.widget.TextView;
30
Chiao Cheng217d1ed2012-11-13 18:38:34 -080031import com.android.contacts.common.test.NeededForTesting;
Yorke Lee37d29852013-11-19 14:11:24 -080032import com.android.contacts.common.util.PhoneNumberHelper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070033import com.android.dialer.calllog.CallTypeHelper;
Yorke Lee034a2b32013-08-26 15:46:10 -070034import com.android.dialer.calllog.ContactInfo;
Yorke Lee24ec3192013-11-19 13:52:45 -080035import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070036import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Tyler Gunn146a4cd2014-05-05 16:51:42 -070037import com.google.android.collect.Lists;
38
39import java.util.ArrayList;
40import java.util.List;
Chiao Cheng94b10b52012-08-17 16:59:12 -070041
42/**
43 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
44 */
45public 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 Lee24ec3192013-11-19 13:52:45 -080054 private final PhoneNumberDisplayHelper mPhoneNumberHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070055 private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070056
57 /**
Tyler Gunn146a4cd2014-05-05 16:51:42 -070058 * List of items to be concatenated together for accessibility descriptions
59 */
60 private ArrayList<CharSequence> mDescriptionItems = Lists.newArrayList();
61
62 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -070063 * 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 Chengfb0a9342013-09-13 17:27:42 -070070 PhoneNumberUtilsWrapper phoneUtils) {
Chiao Cheng94b10b52012-08-17 16:59:12 -070071 mResources = resources;
72 mCallTypeHelper = callTypeHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070073 mPhoneNumberUtilsWrapper = phoneUtils;
Yorke Lee36717252013-11-20 15:02:49 -080074 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mPhoneNumberUtilsWrapper, resources);
Chiao Cheng94b10b52012-08-17 16:59:12 -070075 }
76
77 /** Fills the call details views with content. */
78 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details,
79 boolean isHighlighted) {
80 // Display up to a given number of icons.
81 views.callTypeIcons.clear();
82 int count = details.callTypes.length;
83 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
84 views.callTypeIcons.add(details.callTypes[index]);
85 }
Yorke Lee7e8ea192013-09-04 17:36:07 -070086 views.callTypeIcons.requestLayout();
Chiao Cheng94b10b52012-08-17 16:59:12 -070087 views.callTypeIcons.setVisibility(View.VISIBLE);
88
89 // Show the total call count only if there are more than the maximum number of icons.
90 final Integer callCount;
91 if (count > MAX_CALL_TYPE_ICONS) {
92 callCount = count;
93 } else {
94 callCount = null;
95 }
96 // The color to highlight the count and date in, if any. This is based on the first call.
97 Integer highlightColor =
98 isHighlighted ? mCallTypeHelper.getHighlightedColor(details.callTypes[0]) : null;
99
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.
103 setCallCountAndDate(views, callCount, callLocationAndDate, highlightColor);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700104
105 final CharSequence nameText;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700106 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700107 mPhoneNumberHelper.getDisplayNumber(details.number,
108 details.numberPresentation, details.formattedNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700109 if (TextUtils.isEmpty(details.name)) {
110 nameText = displayNumber;
Fabrice Di Meglioc341db02013-04-03 21:11:37 -0700111 // We have a real phone number as "nameView" so make it always LTR
112 views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 } else {
114 nameText = details.name;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 }
116
117 views.nameView.setText(nameText);
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700118
119 // TODO: At the current time the voicemail transcription is not supported. This view
120 // is kept for future expansion when we may wish to show a transcription of voicemail.
121 views.voicemailTranscriptionView.setText("");
122 views.voicemailTranscriptionView.setVisibility(View.GONE);
123 }
124
125 /**
126 * Builds a string containing the call location and date.
127 *
128 * @param details The call details.
129 * @return The call location and date string.
130 */
131 private CharSequence getCallLocationAndDate(PhoneCallDetails details) {
132 mDescriptionItems.clear();
133
134 // Get type of call (ie mobile, home, etc) if known, or the caller's location.
135 CharSequence callTypeOrLocation = getCallTypeOrLocation(details);
136
137 // Only add the call type or location if its not empty. It will be empty for unknown
138 // callers.
139 if (!TextUtils.isEmpty(callTypeOrLocation)) {
140 mDescriptionItems.add(callTypeOrLocation);
141 }
142 // The date of this call, relative to the current time.
143 mDescriptionItems.add(getCallDate(details));
144
145 // Create a comma separated list from the call type or location, and call date.
146 // TextUtils.join ensures a locale appropriate list separator is used.
147 return TextUtils.join((List<CharSequence>)mDescriptionItems);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700148 }
149
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800150 /**
151 * For a call, if there is an associated contact for the caller, return the known call type
152 * (e.g. mobile, home, work). If there is no associated contact, attempt to use the caller's
153 * location if known.
154 * @param details Call details to use.
155 * @return Type of call (mobile/home) if known, or the location of the caller (if known).
156 */
157 public CharSequence getCallTypeOrLocation(PhoneCallDetails details) {
158 CharSequence numberFormattedLabel = null;
159 // Only show a label if the number is shown and it is not a SIP address.
160 if (!TextUtils.isEmpty(details.number)
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700161 && !PhoneNumberHelper.isUriNumber(details.number.toString())
162 && !mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) {
163
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800164 if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) {
165 numberFormattedLabel = details.geocode;
166 } else {
167 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
168 details.numberLabel);
169 }
170 }
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700171
172 if (!TextUtils.isEmpty(details.name) && TextUtils.isEmpty(numberFormattedLabel)) {
173 numberFormattedLabel = mPhoneNumberHelper.getDisplayNumber(details.number,
174 details.numberPresentation, details.formattedNumber);
175 }
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800176 return numberFormattedLabel;
177 }
178
179 /**
180 * Get the call date/time of the call, relative to the current time.
181 * e.g. 3 minutes ago
182 * @param details Call details to use.
183 * @return String representing when the call occurred.
184 */
185 public CharSequence getCallDate(PhoneCallDetails details) {
186 return DateUtils.getRelativeTimeSpanString(details.date,
187 getCurrentTimeMillis(),
188 DateUtils.MINUTE_IN_MILLIS,
189 DateUtils.FORMAT_ABBREV_RELATIVE);
190 }
191
Chiao Cheng94b10b52012-08-17 16:59:12 -0700192 /** Sets the text of the header view for the details page of a phone call. */
193 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
194 final CharSequence nameText;
195 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700196 mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700197 mResources.getString(R.string.recentCalls_addToContact));
198 if (TextUtils.isEmpty(details.name)) {
199 nameText = displayNumber;
200 } else {
201 nameText = details.name;
202 }
203
204 nameView.setText(nameText);
205 }
206
207 @NeededForTesting
208 public void setCurrentTimeForTest(long currentTimeMillis) {
209 mCurrentTimeMillisForTest = currentTimeMillis;
210 }
211
212 /**
213 * Returns the current time in milliseconds since the epoch.
214 * <p>
215 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
216 */
217 private long getCurrentTimeMillis() {
218 if (mCurrentTimeMillisForTest == null) {
219 return System.currentTimeMillis();
220 } else {
221 return mCurrentTimeMillisForTest;
222 }
223 }
224
225 /** Sets the call count and date. */
226 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
227 CharSequence dateText, Integer highlightColor) {
228 // Combine the count (if present) and the date.
229 final CharSequence text;
230 if (callCount != null) {
231 text = mResources.getString(
232 R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
233 } else {
234 text = dateText;
235 }
236
237 // Apply the highlight color if present.
238 final CharSequence formattedText;
239 if (highlightColor != null) {
240 formattedText = addBoldAndColor(text, highlightColor);
241 } else {
242 formattedText = text;
243 }
244
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700245 views.callLocationAndDate.setText(formattedText);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700246 }
247
248 /** Creates a SpannableString for the given text which is bold and in the given color. */
249 private CharSequence addBoldAndColor(CharSequence text, int color) {
250 int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE;
251 SpannableString result = new SpannableString(text);
252 result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags);
253 result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags);
254 return result;
255 }
256}