blob: e1420d656f0e83e0762d686a986072d6fee06406 [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;
21import android.provider.ContactsContract.CommonDataKinds.Phone;
22import android.telephony.PhoneNumberUtils;
23import android.text.SpannableString;
24import android.text.Spanned;
25import android.text.TextUtils;
26import android.text.format.DateUtils;
27import android.text.style.ForegroundColorSpan;
28import android.text.style.StyleSpan;
29import android.view.View;
30import android.widget.TextView;
31
32import com.android.contacts.R;
Chiao Cheng91197042012-08-24 14:19:37 -070033import com.android.contacts.test.NeededForTesting;
Chiao Cheng94b10b52012-08-17 16:59:12 -070034import com.android.dialer.calllog.CallTypeHelper;
35import com.android.dialer.calllog.PhoneNumberHelper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070036
37/**
38 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
39 */
40public class PhoneCallDetailsHelper {
41 /** The maximum number of icons will be shown to represent the call types in a group. */
42 private static final int MAX_CALL_TYPE_ICONS = 3;
43
44 private final Resources mResources;
45 /** The injected current time in milliseconds since the epoch. Used only by tests. */
46 private Long mCurrentTimeMillisForTest;
47 // Helper classes.
48 private final CallTypeHelper mCallTypeHelper;
49 private final PhoneNumberHelper mPhoneNumberHelper;
50
51 /**
52 * Creates a new instance of the helper.
53 * <p>
54 * Generally you should have a single instance of this helper in any context.
55 *
56 * @param resources used to look up strings
57 */
58 public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper,
59 PhoneNumberHelper phoneNumberHelper) {
60 mResources = resources;
61 mCallTypeHelper = callTypeHelper;
62 mPhoneNumberHelper = phoneNumberHelper;
63 }
64
65 /** Fills the call details views with content. */
66 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details,
67 boolean isHighlighted) {
68 // Display up to a given number of icons.
69 views.callTypeIcons.clear();
70 int count = details.callTypes.length;
71 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
72 views.callTypeIcons.add(details.callTypes[index]);
73 }
74 views.callTypeIcons.setVisibility(View.VISIBLE);
75
76 // Show the total call count only if there are more than the maximum number of icons.
77 final Integer callCount;
78 if (count > MAX_CALL_TYPE_ICONS) {
79 callCount = count;
80 } else {
81 callCount = null;
82 }
83 // The color to highlight the count and date in, if any. This is based on the first call.
84 Integer highlightColor =
85 isHighlighted ? mCallTypeHelper.getHighlightedColor(details.callTypes[0]) : null;
86
87 // The date of this call, relative to the current time.
88 CharSequence dateText =
89 DateUtils.getRelativeTimeSpanString(details.date,
90 getCurrentTimeMillis(),
91 DateUtils.MINUTE_IN_MILLIS,
92 DateUtils.FORMAT_ABBREV_RELATIVE);
93
94 // Set the call count and date.
95 setCallCountAndDate(views, callCount, dateText, highlightColor);
96
97 CharSequence numberFormattedLabel = null;
98 // Only show a label if the number is shown and it is not a SIP address.
99 if (!TextUtils.isEmpty(details.number)
100 && !PhoneNumberUtils.isUriNumber(details.number.toString())) {
101 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
102 details.numberLabel);
103 }
104
105 final CharSequence nameText;
106 final CharSequence numberText;
107 final CharSequence labelText;
108 final CharSequence displayNumber =
109 mPhoneNumberHelper.getDisplayNumber(details.number, details.formattedNumber);
110 if (TextUtils.isEmpty(details.name)) {
111 nameText = displayNumber;
112 if (TextUtils.isEmpty(details.geocode)
113 || mPhoneNumberHelper.isVoicemailNumber(details.number)) {
114 numberText = mResources.getString(R.string.call_log_empty_gecode);
115 } else {
116 numberText = details.geocode;
117 }
118 labelText = null;
119 } else {
120 nameText = details.name;
121 numberText = displayNumber;
122 labelText = numberFormattedLabel;
123 }
124
125 views.nameView.setText(nameText);
126 views.numberView.setText(numberText);
127 views.labelView.setText(labelText);
128 views.labelView.setVisibility(TextUtils.isEmpty(labelText) ? View.GONE : View.VISIBLE);
129 }
130
131 /** Sets the text of the header view for the details page of a phone call. */
132 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
133 final CharSequence nameText;
134 final CharSequence displayNumber =
135 mPhoneNumberHelper.getDisplayNumber(details.number,
136 mResources.getString(R.string.recentCalls_addToContact));
137 if (TextUtils.isEmpty(details.name)) {
138 nameText = displayNumber;
139 } else {
140 nameText = details.name;
141 }
142
143 nameView.setText(nameText);
144 }
145
146 @NeededForTesting
147 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 }
163
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 }
195}