blob: af0a5257e771113e7222a94b28ba221a403c687c [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;
Yorke Lee24ec3192013-11-19 13:52:45 -080066 mPhoneNumberHelper = new PhoneNumberDisplayHelper(resources);
Chiao Chengfb0a9342013-09-13 17:27:42 -070067 mPhoneNumberUtilsWrapper = phoneUtils;
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.
94 CharSequence dateText =
95 DateUtils.getRelativeTimeSpanString(details.date,
96 getCurrentTimeMillis(),
97 DateUtils.MINUTE_IN_MILLIS,
98 DateUtils.FORMAT_ABBREV_RELATIVE);
99
100 // Set the call count and date.
101 setCallCountAndDate(views, callCount, dateText, highlightColor);
102
103 CharSequence numberFormattedLabel = null;
104 // Only show a label if the number is shown and it is not a SIP address.
105 if (!TextUtils.isEmpty(details.number)
Yorke Lee37d29852013-11-19 14:11:24 -0800106 && !PhoneNumberHelper.isUriNumber(details.number.toString())) {
Yorke Lee034a2b32013-08-26 15:46:10 -0700107 if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) {
108 numberFormattedLabel = details.geocode;
109 } else {
110 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
111 details.numberLabel);
112 }
Chiao Cheng94b10b52012-08-17 16:59:12 -0700113 }
114
115 final CharSequence nameText;
116 final CharSequence numberText;
117 final CharSequence labelText;
118 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700119 mPhoneNumberHelper.getDisplayNumber(details.number,
120 details.numberPresentation, details.formattedNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121 if (TextUtils.isEmpty(details.name)) {
122 nameText = displayNumber;
123 if (TextUtils.isEmpty(details.geocode)
Chiao Chengfb0a9342013-09-13 17:27:42 -0700124 || mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700125 numberText = mResources.getString(R.string.call_log_empty_gecode);
126 } else {
127 numberText = details.geocode;
128 }
Yorke Lee481994f2013-07-23 16:53:36 -0700129 labelText = numberText;
Fabrice Di Meglioc341db02013-04-03 21:11:37 -0700130 // We have a real phone number as "nameView" so make it always LTR
131 views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700132 } else {
133 nameText = details.name;
134 numberText = displayNumber;
Yorke Lee481994f2013-07-23 16:53:36 -0700135 labelText = TextUtils.isEmpty(numberFormattedLabel) ? numberText :
136 numberFormattedLabel;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700137 }
138
139 views.nameView.setText(nameText);
Yorke Lee481994f2013-07-23 16:53:36 -0700140
Chiao Cheng94b10b52012-08-17 16:59:12 -0700141 views.labelView.setText(labelText);
142 views.labelView.setVisibility(TextUtils.isEmpty(labelText) ? View.GONE : View.VISIBLE);
143 }
144
145 /** Sets the text of the header view for the details page of a phone call. */
146 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
147 final CharSequence nameText;
148 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700149 mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700150 mResources.getString(R.string.recentCalls_addToContact));
151 if (TextUtils.isEmpty(details.name)) {
152 nameText = displayNumber;
153 } else {
154 nameText = details.name;
155 }
156
157 nameView.setText(nameText);
158 }
159
160 @NeededForTesting
161 public void setCurrentTimeForTest(long currentTimeMillis) {
162 mCurrentTimeMillisForTest = currentTimeMillis;
163 }
164
165 /**
166 * Returns the current time in milliseconds since the epoch.
167 * <p>
168 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
169 */
170 private long getCurrentTimeMillis() {
171 if (mCurrentTimeMillisForTest == null) {
172 return System.currentTimeMillis();
173 } else {
174 return mCurrentTimeMillisForTest;
175 }
176 }
177
178 /** Sets the call count and date. */
179 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
180 CharSequence dateText, Integer highlightColor) {
181 // Combine the count (if present) and the date.
182 final CharSequence text;
183 if (callCount != null) {
184 text = mResources.getString(
185 R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
186 } else {
187 text = dateText;
188 }
189
190 // Apply the highlight color if present.
191 final CharSequence formattedText;
192 if (highlightColor != null) {
193 formattedText = addBoldAndColor(text, highlightColor);
194 } else {
195 formattedText = text;
196 }
197
198 views.callTypeAndDate.setText(formattedText);
199 }
200
201 /** Creates a SpannableString for the given text which is bold and in the given color. */
202 private CharSequence addBoldAndColor(CharSequence text, int color) {
203 int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE;
204 SpannableString result = new SpannableString(text);
205 result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags);
206 result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags);
207 return result;
208 }
209}