blob: b51a27bce8f0113a7c3178d403a50a7ddfaf2435 [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;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import com.android.dialer.calllog.CallTypeHelper;
Yorke Lee034a2b32013-08-26 15:46:10 -070035import com.android.dialer.calllog.ContactInfo;
Chiao Cheng94b10b52012-08-17 16:59:12 -070036import com.android.dialer.calllog.PhoneNumberHelper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070037
38/**
39 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
40 */
41public class PhoneCallDetailsHelper {
42 /** The maximum number of icons will be shown to represent the call types in a group. */
43 private static final int MAX_CALL_TYPE_ICONS = 3;
44
45 private final Resources mResources;
46 /** The injected current time in milliseconds since the epoch. Used only by tests. */
47 private Long mCurrentTimeMillisForTest;
48 // Helper classes.
49 private final CallTypeHelper mCallTypeHelper;
50 private final PhoneNumberHelper mPhoneNumberHelper;
51
52 /**
53 * Creates a new instance of the helper.
54 * <p>
55 * Generally you should have a single instance of this helper in any context.
56 *
57 * @param resources used to look up strings
58 */
59 public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper,
60 PhoneNumberHelper phoneNumberHelper) {
61 mResources = resources;
62 mCallTypeHelper = callTypeHelper;
63 mPhoneNumberHelper = phoneNumberHelper;
64 }
65
66 /** Fills the call details views with content. */
67 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details,
68 boolean isHighlighted) {
69 // Display up to a given number of icons.
70 views.callTypeIcons.clear();
71 int count = details.callTypes.length;
72 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
73 views.callTypeIcons.add(details.callTypes[index]);
74 }
75 views.callTypeIcons.setVisibility(View.VISIBLE);
76
77 // Show the total call count only if there are more than the maximum number of icons.
78 final Integer callCount;
79 if (count > MAX_CALL_TYPE_ICONS) {
80 callCount = count;
81 } else {
82 callCount = null;
83 }
84 // The color to highlight the count and date in, if any. This is based on the first call.
85 Integer highlightColor =
86 isHighlighted ? mCallTypeHelper.getHighlightedColor(details.callTypes[0]) : null;
87
88 // The date of this call, relative to the current time.
89 CharSequence dateText =
90 DateUtils.getRelativeTimeSpanString(details.date,
91 getCurrentTimeMillis(),
92 DateUtils.MINUTE_IN_MILLIS,
93 DateUtils.FORMAT_ABBREV_RELATIVE);
94
95 // Set the call count and date.
96 setCallCountAndDate(views, callCount, dateText, highlightColor);
97
98 CharSequence numberFormattedLabel = null;
99 // Only show a label if the number is shown and it is not a SIP address.
100 if (!TextUtils.isEmpty(details.number)
101 && !PhoneNumberUtils.isUriNumber(details.number.toString())) {
Yorke Lee034a2b32013-08-26 15:46:10 -0700102 if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) {
103 numberFormattedLabel = details.geocode;
104 } else {
105 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
106 details.numberLabel);
107 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700108 }
109
110 final CharSequence nameText;
111 final CharSequence numberText;
112 final CharSequence labelText;
113 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700114 mPhoneNumberHelper.getDisplayNumber(details.number,
115 details.numberPresentation, details.formattedNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700116 if (TextUtils.isEmpty(details.name)) {
117 nameText = displayNumber;
118 if (TextUtils.isEmpty(details.geocode)
119 || mPhoneNumberHelper.isVoicemailNumber(details.number)) {
120 numberText = mResources.getString(R.string.call_log_empty_gecode);
121 } else {
122 numberText = details.geocode;
123 }
Yorke Lee481994f2013-07-23 16:53:36 -0700124 labelText = numberText;
Fabrice Di Meglioc341db02013-04-03 21:11:37 -0700125 // We have a real phone number as "nameView" so make it always LTR
126 views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700127 } else {
128 nameText = details.name;
129 numberText = displayNumber;
Yorke Lee481994f2013-07-23 16:53:36 -0700130 labelText = TextUtils.isEmpty(numberFormattedLabel) ? numberText :
131 numberFormattedLabel;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700132 }
133
134 views.nameView.setText(nameText);
Yorke Lee481994f2013-07-23 16:53:36 -0700135
Chiao Cheng94b10b52012-08-17 16:59:12 -0700136 views.labelView.setText(labelText);
137 views.labelView.setVisibility(TextUtils.isEmpty(labelText) ? View.GONE : View.VISIBLE);
138 }
139
140 /** Sets the text of the header view for the details page of a phone call. */
141 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
142 final CharSequence nameText;
143 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700144 mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700145 mResources.getString(R.string.recentCalls_addToContact));
146 if (TextUtils.isEmpty(details.name)) {
147 nameText = displayNumber;
148 } else {
149 nameText = details.name;
150 }
151
152 nameView.setText(nameText);
153 }
154
155 @NeededForTesting
156 public void setCurrentTimeForTest(long currentTimeMillis) {
157 mCurrentTimeMillisForTest = currentTimeMillis;
158 }
159
160 /**
161 * Returns the current time in milliseconds since the epoch.
162 * <p>
163 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
164 */
165 private long getCurrentTimeMillis() {
166 if (mCurrentTimeMillisForTest == null) {
167 return System.currentTimeMillis();
168 } else {
169 return mCurrentTimeMillisForTest;
170 }
171 }
172
173 /** Sets the call count and date. */
174 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
175 CharSequence dateText, Integer highlightColor) {
176 // Combine the count (if present) and the date.
177 final CharSequence text;
178 if (callCount != null) {
179 text = mResources.getString(
180 R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
181 } else {
182 text = dateText;
183 }
184
185 // Apply the highlight color if present.
186 final CharSequence formattedText;
187 if (highlightColor != null) {
188 formattedText = addBoldAndColor(text, highlightColor);
189 } else {
190 formattedText = text;
191 }
192
193 views.callTypeAndDate.setText(formattedText);
194 }
195
196 /** Creates a SpannableString for the given text which is bold and in the given color. */
197 private CharSequence addBoldAndColor(CharSequence text, int color) {
198 int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE;
199 SpannableString result = new SpannableString(text);
200 result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags);
201 result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags);
202 return result;
203 }
204}