blob: 2d75c26ae483f1c23e1bd7ef9a3a2b9773b37b48 [file] [log] [blame]
Flavio Lerdaafb93bb2011-07-05 14:46:10 +01001/*
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.contacts;
18
Flavio Lerda9a208cc2011-07-12 21:05:47 +010019import com.android.contacts.calllog.CallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010020import com.android.contacts.calllog.PhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010021import com.android.contacts.format.FormatUtils;
22
23import android.content.res.Resources;
24import android.graphics.Typeface;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010025import android.provider.ContactsContract.CommonDataKinds.Phone;
26import android.telephony.PhoneNumberUtils;
Flavio Lerdafe170262011-08-03 06:40:47 +010027import android.text.SpannableString;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010028import android.text.Spanned;
29import android.text.TextUtils;
30import android.text.format.DateUtils;
Flavio Lerdafe170262011-08-03 06:40:47 +010031import android.text.style.ForegroundColorSpan;
32import android.text.style.StyleSpan;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010033import android.view.View;
Flavio Lerdab88abaa2011-08-02 23:38:36 +010034import android.widget.TextView;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010035
36/**
37 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
38 */
39public class PhoneCallDetailsHelper {
Flavio Lerda8e39c712011-07-15 19:57:01 +010040 /** The maximum number of icons will be shown to represent the call types in a group. */
41 private static final int MAX_CALL_TYPE_ICONS = 3;
42
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010043 private final Resources mResources;
Flavio Lerda7d7473a2011-07-06 14:21:46 +010044 /** The injected current time in milliseconds since the epoch. Used only by tests. */
45 private Long mCurrentTimeMillisForTest;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010046 // Helper classes.
Flavio Lerda9a208cc2011-07-12 21:05:47 +010047 private final CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010048 private final PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010049
50 /**
51 * Creates a new instance of the helper.
52 * <p>
53 * Generally you should have a single instance of this helper in any context.
54 *
55 * @param resources used to look up strings
56 */
Flavio Lerda40569562011-07-22 21:51:29 +010057 public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper,
58 PhoneNumberHelper phoneNumberHelper) {
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010059 mResources = resources;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010060 mCallTypeHelper = callTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010061 mPhoneNumberHelper = phoneNumberHelper;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010062 }
63
Flavio Lerda9de38682011-07-08 20:38:07 +010064 /** Fills the call details views with content. */
Flavio Lerda4586feb2011-07-09 17:03:48 +010065 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details,
Flavio Lerdafe170262011-08-03 06:40:47 +010066 boolean isHighlighted) {
67 // Display up to a given number of icons.
68 views.callTypeIcons.clear();
69 int count = details.callTypes.length;
70 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
71 views.callTypeIcons.add(details.callTypes[index]);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010072 }
Flavio Lerdafe170262011-08-03 06:40:47 +010073 views.callTypeIcons.setVisibility(View.VISIBLE);
Flavio Lerda4586feb2011-07-09 17:03:48 +010074
Flavio Lerdafe170262011-08-03 06:40:47 +010075 // Show the total call count only if there are more than the maximum number of icons.
76 final Integer callCount;
77 if (count > MAX_CALL_TYPE_ICONS) {
78 callCount = count;
79 } else {
80 callCount = null;
81 }
82 // The color to highlight the count and date in, if any. This is based on the first call.
83 Integer highlightColor =
84 isHighlighted ? mCallTypeHelper.getHighlightedColor(details.callTypes[0]) : null;
85
86 // The date of this call, relative to the current time.
87 CharSequence dateText =
Flavio Lerda9de38682011-07-08 20:38:07 +010088 DateUtils.getRelativeTimeSpanString(details.date,
Flavio Lerda7d7473a2011-07-06 14:21:46 +010089 getCurrentTimeMillis(),
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010090 DateUtils.MINUTE_IN_MILLIS,
91 DateUtils.FORMAT_ABBREV_RELATIVE);
92
Flavio Lerdafe170262011-08-03 06:40:47 +010093 // Set the call count and date.
94 setCallCountAndDate(views, callCount, dateText, highlightColor);
95
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010096 CharSequence numberFormattedLabel = null;
97 // Only show a label if the number is shown and it is not a SIP address.
Flavio Lerda9de38682011-07-08 20:38:07 +010098 if (!TextUtils.isEmpty(details.number)
99 && !PhoneNumberUtils.isUriNumber(details.number.toString())) {
100 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
101 details.numberLabel);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100102 }
103
Flavio Lerdab9256f82011-07-09 20:10:57 +0100104 final CharSequence nameText;
105 final CharSequence numberText;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100106 final CharSequence displayNumber =
107 mPhoneNumberHelper.getDisplayNumber(details.number, details.formattedNumber);
Flavio Lerda9de38682011-07-08 20:38:07 +0100108 if (TextUtils.isEmpty(details.name)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100109 nameText = displayNumber;
Flavio Lerdae4b156d2011-08-14 16:49:45 +0100110 if (TextUtils.isEmpty(details.geocode)
111 || mPhoneNumberHelper.isVoicemailNumber(details.number)) {
Flavio Lerdabe45e0f2011-07-25 21:00:30 +0100112 numberText = mResources.getString(R.string.call_log_empty_gecode);
113 } else {
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100114 numberText = details.geocode;
Flavio Lerdabe45e0f2011-07-25 21:00:30 +0100115 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100116 } else {
Flavio Lerda9de38682011-07-08 20:38:07 +0100117 nameText = details.name;
Flavio Lerda223a8432011-07-11 18:40:25 +0100118 if (numberFormattedLabel != null) {
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100119 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
Flavio Lerdab9256f82011-07-09 20:10:57 +0100120 numberFormattedLabel + " " + displayNumber, 0,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100121 numberFormattedLabel.length(),
122 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Flavio Lerdab9256f82011-07-09 20:10:57 +0100123 } else {
124 numberText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100125 }
126 }
127
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100128 views.nameView.setText(nameText);
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100129 views.numberView.setText(numberText);
130 }
131
Flavio Lerdad5678d32011-08-28 13:49:56 +0100132 /** Sets the text of the header view for the details page of a phone call. */
133 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100134 final CharSequence nameText;
135 final CharSequence displayNumber =
Flavio Lerdad5678d32011-08-28 13:49:56 +0100136 mPhoneNumberHelper.getDisplayNumber(details.number,
137 mResources.getString(R.string.recentCalls_addToContact));
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100138 if (TextUtils.isEmpty(details.name)) {
139 nameText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100140 } else {
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100141 nameText = details.name;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100142 }
Flavio Lerda40569562011-07-22 21:51:29 +0100143
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100144 nameView.setText(nameText);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100145 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100146
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100147 public void setCurrentTimeForTest(long currentTimeMillis) {
148 mCurrentTimeMillisForTest = currentTimeMillis;
149 }
150
151 /**
152 * Returns the current time in milliseconds since the epoch.
153 * <p>
154 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
155 */
156 private long getCurrentTimeMillis() {
157 if (mCurrentTimeMillisForTest == null) {
158 return System.currentTimeMillis();
159 } else {
160 return mCurrentTimeMillisForTest;
161 }
162 }
Flavio Lerdafe170262011-08-03 06:40:47 +0100163
164 /** Sets the call count and date. */
165 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
166 CharSequence dateText, Integer highlightColor) {
167 // Combine the count (if present) and the date.
168 final CharSequence text;
169 if (callCount != null) {
170 text = mResources.getString(
171 R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
172 } else {
173 text = dateText;
174 }
175
176 // Apply the highlight color if present.
177 final CharSequence formattedText;
178 if (highlightColor != null) {
179 formattedText = addBoldAndColor(text, highlightColor);
180 } else {
181 formattedText = text;
182 }
183
184 views.callTypeAndDate.setText(formattedText);
185 }
186
187 /** Creates a SpannableString for the given text which is bold and in the given color. */
188 private CharSequence addBoldAndColor(CharSequence text, int color) {
189 int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE;
190 SpannableString result = new SpannableString(text);
191 result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags);
192 result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags);
193 return result;
194 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100195}