blob: 4424fcb0b4ad92240b65404d74fa0ff78dc526d9 [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;
Yorke Lee034a2b32013-08-26 15:46:10 -070021import android.provider.ContactsContract;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.provider.ContactsContract.CommonDataKinds.Phone;
23import android.telephony.PhoneNumberUtils;
24import 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;
32
Chiao Cheng217d1ed2012-11-13 18:38:34 -080033import com.android.contacts.common.test.NeededForTesting;
Yorke Lee37d29852013-11-19 14:11:24 -080034import com.android.contacts.common.util.PhoneNumberHelper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070035import com.android.dialer.calllog.CallTypeHelper;
Yorke Lee034a2b32013-08-26 15:46:10 -070036import com.android.dialer.calllog.ContactInfo;
Yorke Lee24ec3192013-11-19 13:52:45 -080037import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070038import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039
40/**
41 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
42 */
43public 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 Lee24ec3192013-11-19 13:52:45 -080052 private final PhoneNumberDisplayHelper mPhoneNumberHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070053 private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070054
55 /**
56 * Creates a new instance of the helper.
57 * <p>
58 * Generally you should have a single instance of this helper in any context.
59 *
60 * @param resources used to look up strings
61 */
62 public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper,
Chiao Chengfb0a9342013-09-13 17:27:42 -070063 PhoneNumberUtilsWrapper phoneUtils) {
Chiao Cheng94b10b52012-08-17 16:59:12 -070064 mResources = resources;
65 mCallTypeHelper = callTypeHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070066 mPhoneNumberUtilsWrapper = phoneUtils;
Yorke Lee36717252013-11-20 15:02:49 -080067 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mPhoneNumberUtilsWrapper, resources);
Chiao Cheng94b10b52012-08-17 16:59:12 -070068 }
69
70 /** Fills the call details views with content. */
71 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details,
72 boolean isHighlighted) {
73 // Display up to a given number of icons.
74 views.callTypeIcons.clear();
75 int count = details.callTypes.length;
76 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
77 views.callTypeIcons.add(details.callTypes[index]);
78 }
Yorke Lee7e8ea192013-09-04 17:36:07 -070079 views.callTypeIcons.requestLayout();
Chiao Cheng94b10b52012-08-17 16:59:12 -070080 views.callTypeIcons.setVisibility(View.VISIBLE);
81
82 // Show the total call count only if there are more than the maximum number of icons.
83 final Integer callCount;
84 if (count > MAX_CALL_TYPE_ICONS) {
85 callCount = count;
86 } else {
87 callCount = null;
88 }
89 // The color to highlight the count and date in, if any. This is based on the first call.
90 Integer highlightColor =
91 isHighlighted ? mCallTypeHelper.getHighlightedColor(details.callTypes[0]) : null;
92
93 // The date of this call, relative to the current time.
Tyler Gunn45ed3b52014-02-03 08:31:10 -080094 CharSequence dateText = getCallDate(details);
Chiao Cheng94b10b52012-08-17 16:59:12 -070095
96 // Set the call count and date.
97 setCallCountAndDate(views, callCount, dateText, highlightColor);
98
Tyler Gunn45ed3b52014-02-03 08:31:10 -080099 // Get type of call (ie mobile, home, etc) if known, or the caller's
100 CharSequence numberFormattedLabel = getCallTypeOrLocation(details);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700101
102 final CharSequence nameText;
103 final CharSequence numberText;
104 final CharSequence labelText;
105 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700106 mPhoneNumberHelper.getDisplayNumber(details.number,
107 details.numberPresentation, details.formattedNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700108 if (TextUtils.isEmpty(details.name)) {
109 nameText = displayNumber;
110 if (TextUtils.isEmpty(details.geocode)
Chiao Chengfb0a9342013-09-13 17:27:42 -0700111 || mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) {
Yorke Lee36717252013-11-20 15:02:49 -0800112 numberText = mResources.getString(R.string.call_log_empty_geocode);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 } else {
114 numberText = details.geocode;
115 }
Yorke Lee481994f2013-07-23 16:53:36 -0700116 labelText = numberText;
Fabrice Di Meglioc341db02013-04-03 21:11:37 -0700117 // We have a real phone number as "nameView" so make it always LTR
118 views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119 } else {
120 nameText = details.name;
121 numberText = displayNumber;
Yorke Lee481994f2013-07-23 16:53:36 -0700122 labelText = TextUtils.isEmpty(numberFormattedLabel) ? numberText :
123 numberFormattedLabel;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700124 }
125
126 views.nameView.setText(nameText);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700127 views.labelView.setText(labelText);
128 views.labelView.setVisibility(TextUtils.isEmpty(labelText) ? View.GONE : View.VISIBLE);
129 }
130
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800131 /**
132 * For a call, if there is an associated contact for the caller, return the known call type
133 * (e.g. mobile, home, work). If there is no associated contact, attempt to use the caller's
134 * location if known.
135 * @param details Call details to use.
136 * @return Type of call (mobile/home) if known, or the location of the caller (if known).
137 */
138 public CharSequence getCallTypeOrLocation(PhoneCallDetails details) {
139 CharSequence numberFormattedLabel = null;
140 // Only show a label if the number is shown and it is not a SIP address.
141 if (!TextUtils.isEmpty(details.number)
142 && !PhoneNumberHelper.isUriNumber(details.number.toString())) {
143 if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) {
144 numberFormattedLabel = details.geocode;
145 } else {
146 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
147 details.numberLabel);
148 }
149 }
150 return numberFormattedLabel;
151 }
152
153 /**
154 * Get the call date/time of the call, relative to the current time.
155 * e.g. 3 minutes ago
156 * @param details Call details to use.
157 * @return String representing when the call occurred.
158 */
159 public CharSequence getCallDate(PhoneCallDetails details) {
160 return DateUtils.getRelativeTimeSpanString(details.date,
161 getCurrentTimeMillis(),
162 DateUtils.MINUTE_IN_MILLIS,
163 DateUtils.FORMAT_ABBREV_RELATIVE);
164 }
165
Chiao Cheng94b10b52012-08-17 16:59:12 -0700166 /** Sets the text of the header view for the details page of a phone call. */
167 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
168 final CharSequence nameText;
169 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700170 mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700171 mResources.getString(R.string.recentCalls_addToContact));
172 if (TextUtils.isEmpty(details.name)) {
173 nameText = displayNumber;
174 } else {
175 nameText = details.name;
176 }
177
178 nameView.setText(nameText);
179 }
180
181 @NeededForTesting
182 public void setCurrentTimeForTest(long currentTimeMillis) {
183 mCurrentTimeMillisForTest = currentTimeMillis;
184 }
185
186 /**
187 * Returns the current time in milliseconds since the epoch.
188 * <p>
189 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
190 */
191 private long getCurrentTimeMillis() {
192 if (mCurrentTimeMillisForTest == null) {
193 return System.currentTimeMillis();
194 } else {
195 return mCurrentTimeMillisForTest;
196 }
197 }
198
199 /** Sets the call count and date. */
200 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
201 CharSequence dateText, Integer highlightColor) {
202 // Combine the count (if present) and the date.
203 final CharSequence text;
204 if (callCount != null) {
205 text = mResources.getString(
206 R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
207 } else {
208 text = dateText;
209 }
210
211 // Apply the highlight color if present.
212 final CharSequence formattedText;
213 if (highlightColor != null) {
214 formattedText = addBoldAndColor(text, highlightColor);
215 } else {
216 formattedText = text;
217 }
218
219 views.callTypeAndDate.setText(formattedText);
220 }
221
222 /** Creates a SpannableString for the given text which is bold and in the given color. */
223 private CharSequence addBoldAndColor(CharSequence text, int color) {
224 int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE;
225 SpannableString result = new SpannableString(text);
226 result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags);
227 result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags);
228 return result;
229 }
230}