blob: 60dfb7bd16c581a9cad11c245ee2e334845fd528 [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 +010021
22import android.content.res.Resources;
23import android.graphics.Typeface;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010024import android.provider.ContactsContract.CommonDataKinds.Phone;
25import android.telephony.PhoneNumberUtils;
Flavio Lerdafe170262011-08-03 06:40:47 +010026import android.text.SpannableString;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010027import android.text.Spanned;
28import android.text.TextUtils;
29import android.text.format.DateUtils;
Flavio Lerdafe170262011-08-03 06:40:47 +010030import android.text.style.ForegroundColorSpan;
31import android.text.style.StyleSpan;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010032import android.view.View;
Flavio Lerdab88abaa2011-08-02 23:38:36 +010033import android.widget.TextView;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010034
35/**
36 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
37 */
38public class PhoneCallDetailsHelper {
Flavio Lerda8e39c712011-07-15 19:57:01 +010039 /** The maximum number of icons will be shown to represent the call types in a group. */
40 private static final int MAX_CALL_TYPE_ICONS = 3;
41
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010042 private final Resources mResources;
Flavio Lerda7d7473a2011-07-06 14:21:46 +010043 /** The injected current time in milliseconds since the epoch. Used only by tests. */
44 private Long mCurrentTimeMillisForTest;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010045 // Helper classes.
Flavio Lerda9a208cc2011-07-12 21:05:47 +010046 private final CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010047 private final PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010048
49 /**
50 * Creates a new instance of the helper.
51 * <p>
52 * Generally you should have a single instance of this helper in any context.
53 *
54 * @param resources used to look up strings
55 */
Flavio Lerda40569562011-07-22 21:51:29 +010056 public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper,
57 PhoneNumberHelper phoneNumberHelper) {
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010058 mResources = resources;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010059 mCallTypeHelper = callTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010060 mPhoneNumberHelper = phoneNumberHelper;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010061 }
62
Flavio Lerda9de38682011-07-08 20:38:07 +010063 /** Fills the call details views with content. */
Flavio Lerda4586feb2011-07-09 17:03:48 +010064 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details,
Flavio Lerdafe170262011-08-03 06:40:47 +010065 boolean isHighlighted) {
66 // Display up to a given number of icons.
67 views.callTypeIcons.clear();
68 int count = details.callTypes.length;
69 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
70 views.callTypeIcons.add(details.callTypes[index]);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010071 }
Flavio Lerdafe170262011-08-03 06:40:47 +010072 views.callTypeIcons.setVisibility(View.VISIBLE);
Flavio Lerda4586feb2011-07-09 17:03:48 +010073
Flavio Lerdafe170262011-08-03 06:40:47 +010074 // Show the total call count only if there are more than the maximum number of icons.
75 final Integer callCount;
76 if (count > MAX_CALL_TYPE_ICONS) {
77 callCount = count;
78 } else {
79 callCount = null;
80 }
81 // The color to highlight the count and date in, if any. This is based on the first call.
82 Integer highlightColor =
83 isHighlighted ? mCallTypeHelper.getHighlightedColor(details.callTypes[0]) : null;
84
85 // The date of this call, relative to the current time.
86 CharSequence dateText =
Flavio Lerda9de38682011-07-08 20:38:07 +010087 DateUtils.getRelativeTimeSpanString(details.date,
Flavio Lerda7d7473a2011-07-06 14:21:46 +010088 getCurrentTimeMillis(),
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010089 DateUtils.MINUTE_IN_MILLIS,
90 DateUtils.FORMAT_ABBREV_RELATIVE);
91
Flavio Lerdafe170262011-08-03 06:40:47 +010092 // Set the call count and date.
93 setCallCountAndDate(views, callCount, dateText, highlightColor);
94
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010095 CharSequence numberFormattedLabel = null;
96 // Only show a label if the number is shown and it is not a SIP address.
Flavio Lerda9de38682011-07-08 20:38:07 +010097 if (!TextUtils.isEmpty(details.number)
98 && !PhoneNumberUtils.isUriNumber(details.number.toString())) {
99 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
100 details.numberLabel);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100101 }
102
Flavio Lerdab9256f82011-07-09 20:10:57 +0100103 final CharSequence nameText;
104 final CharSequence numberText;
Flavio Lerda88be53d2011-11-17 00:31:34 +0000105 final CharSequence labelText;
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 Lerda88be53d2011-11-17 00:31:34 +0000116 labelText = null;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100117 } else {
Flavio Lerda9de38682011-07-08 20:38:07 +0100118 nameText = details.name;
Flavio Lerda88be53d2011-11-17 00:31:34 +0000119 numberText = displayNumber;
120 labelText = numberFormattedLabel;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100121 }
122
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100123 views.nameView.setText(nameText);
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100124 views.numberView.setText(numberText);
Flavio Lerda88be53d2011-11-17 00:31:34 +0000125 views.labelView.setText(labelText);
126 views.labelView.setVisibility(TextUtils.isEmpty(labelText) ? View.GONE : View.VISIBLE);
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100127 }
128
Flavio Lerdad5678d32011-08-28 13:49:56 +0100129 /** Sets the text of the header view for the details page of a phone call. */
130 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100131 final CharSequence nameText;
132 final CharSequence displayNumber =
Flavio Lerdad5678d32011-08-28 13:49:56 +0100133 mPhoneNumberHelper.getDisplayNumber(details.number,
134 mResources.getString(R.string.recentCalls_addToContact));
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100135 if (TextUtils.isEmpty(details.name)) {
136 nameText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100137 } else {
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100138 nameText = details.name;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100139 }
Flavio Lerda40569562011-07-22 21:51:29 +0100140
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100141 nameView.setText(nameText);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100142 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100143
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100144 public void setCurrentTimeForTest(long currentTimeMillis) {
145 mCurrentTimeMillisForTest = currentTimeMillis;
146 }
147
148 /**
149 * Returns the current time in milliseconds since the epoch.
150 * <p>
151 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
152 */
153 private long getCurrentTimeMillis() {
154 if (mCurrentTimeMillisForTest == null) {
155 return System.currentTimeMillis();
156 } else {
157 return mCurrentTimeMillisForTest;
158 }
159 }
Flavio Lerdafe170262011-08-03 06:40:47 +0100160
161 /** Sets the call count and date. */
162 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
163 CharSequence dateText, Integer highlightColor) {
164 // Combine the count (if present) and the date.
165 final CharSequence text;
166 if (callCount != null) {
167 text = mResources.getString(
168 R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
169 } else {
170 text = dateText;
171 }
172
173 // Apply the highlight color if present.
174 final CharSequence formattedText;
175 if (highlightColor != null) {
176 formattedText = addBoldAndColor(text, highlightColor);
177 } else {
178 formattedText = text;
179 }
180
181 views.callTypeAndDate.setText(formattedText);
182 }
183
184 /** Creates a SpannableString for the given text which is bold and in the given color. */
185 private CharSequence addBoldAndColor(CharSequence text, int color) {
186 int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE;
187 SpannableString result = new SpannableString(text);
188 result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags);
189 result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags);
190 return result;
191 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100192}