blob: e970fcc84aaf868123c38768bc23e1e6c4bcfd94 [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 Lerda71fc6ec2011-08-09 17:24:29 +0100110 if (TextUtils.isEmpty(details.geocode)) {
Flavio Lerdabe45e0f2011-07-25 21:00:30 +0100111 numberText = mResources.getString(R.string.call_log_empty_gecode);
112 } else {
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100113 numberText = details.geocode;
Flavio Lerdabe45e0f2011-07-25 21:00:30 +0100114 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100115 } else {
Flavio Lerda9de38682011-07-08 20:38:07 +0100116 nameText = details.name;
Flavio Lerda223a8432011-07-11 18:40:25 +0100117 if (numberFormattedLabel != null) {
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100118 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
Flavio Lerdab9256f82011-07-09 20:10:57 +0100119 numberFormattedLabel + " " + displayNumber, 0,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100120 numberFormattedLabel.length(),
121 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Flavio Lerdab9256f82011-07-09 20:10:57 +0100122 } else {
123 numberText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100124 }
125 }
126
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100127 views.nameView.setText(nameText);
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100128 views.numberView.setText(numberText);
129 }
130
131 /** Sets the name in the text view for the given phone call. */
132 public void setPhoneCallName(TextView nameView, PhoneCallDetails details) {
133 final CharSequence nameText;
134 final CharSequence displayNumber =
135 mPhoneNumberHelper.getDisplayNumber(details.number, details.formattedNumber);
136 if (TextUtils.isEmpty(details.name)) {
137 nameText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100138 } else {
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100139 nameText = details.name;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100140 }
Flavio Lerda40569562011-07-22 21:51:29 +0100141
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100142 nameView.setText(nameText);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100143 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100144
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100145 public void setCurrentTimeForTest(long currentTimeMillis) {
146 mCurrentTimeMillisForTest = currentTimeMillis;
147 }
148
149 /**
150 * Returns the current time in milliseconds since the epoch.
151 * <p>
152 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
153 */
154 private long getCurrentTimeMillis() {
155 if (mCurrentTimeMillisForTest == null) {
156 return System.currentTimeMillis();
157 } else {
158 return mCurrentTimeMillisForTest;
159 }
160 }
Flavio Lerdafe170262011-08-03 06:40:47 +0100161
162 /** Sets the call count and date. */
163 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
164 CharSequence dateText, Integer highlightColor) {
165 // Combine the count (if present) and the date.
166 final CharSequence text;
167 if (callCount != null) {
168 text = mResources.getString(
169 R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
170 } else {
171 text = dateText;
172 }
173
174 // Apply the highlight color if present.
175 final CharSequence formattedText;
176 if (highlightColor != null) {
177 formattedText = addBoldAndColor(text, highlightColor);
178 } else {
179 formattedText = text;
180 }
181
182 views.callTypeAndDate.setText(formattedText);
183 }
184
185 /** Creates a SpannableString for the given text which is bold and in the given color. */
186 private CharSequence addBoldAndColor(CharSequence text, int color) {
187 int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE;
188 SpannableString result = new SpannableString(text);
189 result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags);
190 result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags);
191 return result;
192 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100193}