blob: 7f73b04c733de968c3a22e1f9e082de2db0c6426 [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
19import com.android.contacts.format.FormatUtils;
Flavio Lerda696e8132011-07-05 19:00:23 +010020import com.android.internal.telephony.CallerInfo;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010021
Flavio Lerda7d7473a2011-07-06 14:21:46 +010022import android.content.Context;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010023import android.content.res.Resources;
24import android.graphics.Typeface;
Flavio Lerda7d7473a2011-07-06 14:21:46 +010025import android.graphics.drawable.Drawable;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010026import android.provider.CallLog.Calls;
27import android.provider.ContactsContract.CommonDataKinds.Phone;
28import android.telephony.PhoneNumberUtils;
29import android.text.Spanned;
30import android.text.TextUtils;
31import android.text.format.DateUtils;
32import android.view.View;
Flavio Lerda7d7473a2011-07-06 14:21:46 +010033import android.widget.ImageView;
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 Lerda7d7473a2011-07-06 14:21:46 +010039 private final Context mContext;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010040 private final Resources mResources;
Flavio Lerda696e8132011-07-05 19:00:23 +010041 private final String mVoicemailNumber;
Flavio Lerda7d7473a2011-07-06 14:21:46 +010042 /** Icon for incoming calls. */
43 private final Drawable mIncomingDrawable;
44 /** Icon for outgoing calls. */
45 private final Drawable mOutgoingDrawable;
46 /** Icon for missed calls. */
47 private final Drawable mMissedDrawable;
48 /** Icon for voicemails. */
49 private final Drawable mVoicemailDrawable;
Flavio Lerda4586feb2011-07-09 17:03:48 +010050 /** Name used to identify incoming calls. */
51 private final String mIncomingName;
52 /** Name used to identify outgoing calls. */
53 private final String mOutgoingName;
54 /** Name used to identify missed calls. */
55 private final String mMissedName;
56 /** Name used to identify voicemail calls. */
57 private final String mVoicemailName;
Flavio Lerda7d7473a2011-07-06 14:21:46 +010058 /** The injected current time in milliseconds since the epoch. Used only by tests. */
59 private Long mCurrentTimeMillisForTest;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010060
61 /**
62 * Creates a new instance of the helper.
63 * <p>
64 * Generally you should have a single instance of this helper in any context.
65 *
66 * @param resources used to look up strings
67 */
Flavio Lerda7d7473a2011-07-06 14:21:46 +010068 public PhoneCallDetailsHelper(Context context, Resources resources, String voicemailNumber,
69 Drawable incomingDrawable, Drawable outgoingDrawable, Drawable missedDrawable,
70 Drawable voicemailDrawable) {
71 mContext = context;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010072 mResources = resources;
Flavio Lerda696e8132011-07-05 19:00:23 +010073 mVoicemailNumber = voicemailNumber;
Flavio Lerda7d7473a2011-07-06 14:21:46 +010074 mIncomingDrawable = incomingDrawable;
75 mOutgoingDrawable = outgoingDrawable;
76 mMissedDrawable = missedDrawable;
77 mVoicemailDrawable = voicemailDrawable;
Flavio Lerda4586feb2011-07-09 17:03:48 +010078 // Cache these values so that we do not need to look them up each time.
79 mIncomingName = mResources.getString(R.string.type_incoming);
80 mOutgoingName = mResources.getString(R.string.type_outgoing);
81 mMissedName = mResources.getString(R.string.type_missed);
82 mVoicemailName = mResources.getString(R.string.type_voicemail);
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010083 }
84
Flavio Lerda9de38682011-07-08 20:38:07 +010085 /** Fills the call details views with content. */
Flavio Lerda4586feb2011-07-09 17:03:48 +010086 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details,
87 boolean useIcons) {
88 if (useIcons) {
Flavio Lerda4586feb2011-07-09 17:03:48 +010089 views.callTypeIcons.removeAllViews();
Flavio Lerda223a8432011-07-11 18:40:25 +010090 int count = details.callTypes.length;
91 for (int callType : details.callTypes) {
92 ImageView callTypeImage = new ImageView(mContext);
93 callTypeImage.setImageDrawable(getCallTypeDrawable(callType));
94 views.callTypeIcons.addView(callTypeImage);
95 }
Flavio Lerda4586feb2011-07-09 17:03:48 +010096 views.callTypeIcons.setVisibility(View.VISIBLE);
97 views.callTypeText.setVisibility(View.GONE);
98 views.callTypeSeparator.setVisibility(View.GONE);
99 } else {
100 String callTypeName;
Flavio Lerda223a8432011-07-11 18:40:25 +0100101 // Use the name of the first call type.
102 // TODO: We should update this to handle the text for multiple calls as well.
103 int callType = details.callTypes[0];
104 views.callTypeText.setText(getCallTypeText(callType));
Flavio Lerda4586feb2011-07-09 17:03:48 +0100105 views.callTypeIcons.removeAllViews();
106
107 views.callTypeText.setVisibility(View.VISIBLE);
108 views.callTypeSeparator.setVisibility(View.VISIBLE);
109 views.callTypeIcons.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100110 }
Flavio Lerda4586feb2011-07-09 17:03:48 +0100111
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100112 CharSequence shortDateText =
Flavio Lerda9de38682011-07-08 20:38:07 +0100113 DateUtils.getRelativeTimeSpanString(details.date,
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100114 getCurrentTimeMillis(),
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100115 DateUtils.MINUTE_IN_MILLIS,
116 DateUtils.FORMAT_ABBREV_RELATIVE);
117
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100118 CharSequence numberFormattedLabel = null;
119 // Only show a label if the number is shown and it is not a SIP address.
Flavio Lerda9de38682011-07-08 20:38:07 +0100120 if (!TextUtils.isEmpty(details.number)
121 && !PhoneNumberUtils.isUriNumber(details.number.toString())) {
122 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
123 details.numberLabel);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100124 }
125
Flavio Lerdab9256f82011-07-09 20:10:57 +0100126 final CharSequence nameText;
127 final CharSequence numberText;
Flavio Lerda9de38682011-07-08 20:38:07 +0100128 if (TextUtils.isEmpty(details.name)) {
Flavio Lerda371d5f92011-07-09 19:45:32 +0100129 nameText = getDisplayNumber(details.number, details.formattedNumber);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100130 numberText = "";
131 } else {
Flavio Lerda9de38682011-07-08 20:38:07 +0100132 nameText = details.name;
Flavio Lerda371d5f92011-07-09 19:45:32 +0100133 CharSequence displayNumber = getDisplayNumber(details.number, details.formattedNumber);
Flavio Lerda223a8432011-07-11 18:40:25 +0100134 if (numberFormattedLabel != null) {
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100135 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
Flavio Lerdab9256f82011-07-09 20:10:57 +0100136 numberFormattedLabel + " " + displayNumber, 0,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100137 numberFormattedLabel.length(),
138 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Flavio Lerdab9256f82011-07-09 20:10:57 +0100139 } else {
140 numberText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100141 }
142 }
143
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100144 views.dateView.setText(shortDateText);
145 views.dateView.setVisibility(View.VISIBLE);
146 views.nameView.setText(nameText);
147 views.nameView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100148 // Do not show the number if it is not available. This happens if we have only the number,
149 // in which case the number is shown in the name field instead.
150 if (!TextUtils.isEmpty(numberText)) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100151 views.numberView.setText(numberText);
152 views.numberView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100153 } else {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100154 views.numberView.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100155 }
156 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100157
Flavio Lerda223a8432011-07-11 18:40:25 +0100158 /** Returns the text used to represent the given call type. */
159 private String getCallTypeText(int callType) {
160 switch (callType) {
161 case Calls.INCOMING_TYPE:
162 return mIncomingName;
163
164 case Calls.OUTGOING_TYPE:
165 return mOutgoingName;
166
167 case Calls.MISSED_TYPE:
168 return mMissedName;
169
170 case Calls.VOICEMAIL_TYPE:
171 return mVoicemailName;
172
173 default:
174 throw new IllegalArgumentException("invalid call type: " + callType);
175 }
176 }
177
178 /** Returns the drawable of the icon associated with the given call type. */
179 private Drawable getCallTypeDrawable(int callType) {
180 switch (callType) {
181 case Calls.INCOMING_TYPE:
182 return mIncomingDrawable;
183
184 case Calls.OUTGOING_TYPE:
185 return mOutgoingDrawable;
186
187 case Calls.MISSED_TYPE:
188 return mMissedDrawable;
189
190 case Calls.VOICEMAIL_TYPE:
191 return mVoicemailDrawable;
192
193 default:
194 throw new IllegalArgumentException("invalid call type: " + callType);
195 }
196 }
197
Flavio Lerda371d5f92011-07-09 19:45:32 +0100198 private CharSequence getDisplayNumber(CharSequence number, CharSequence formattedNumber) {
Flavio Lerda696e8132011-07-05 19:00:23 +0100199 if (TextUtils.isEmpty(number)) {
200 return "";
201 }
202 if (number.equals(CallerInfo.UNKNOWN_NUMBER)) {
203 return mResources.getString(R.string.unknown);
204 }
205 if (number.equals(CallerInfo.PRIVATE_NUMBER)) {
206 return mResources.getString(R.string.private_num);
207 }
208 if (number.equals(CallerInfo.PAYPHONE_NUMBER)) {
209 return mResources.getString(R.string.payphone);
210 }
211 if (PhoneNumberUtils.extractNetworkPortion(number.toString()).equals(mVoicemailNumber)) {
212 return mResources.getString(R.string.voicemail);
213 }
Flavio Lerda371d5f92011-07-09 19:45:32 +0100214 if (TextUtils.isEmpty(formattedNumber)) {
215 return number;
216 } else {
217 return formattedNumber;
218 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100219 }
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100220
221 public void setCurrentTimeForTest(long currentTimeMillis) {
222 mCurrentTimeMillisForTest = currentTimeMillis;
223 }
224
225 /**
226 * Returns the current time in milliseconds since the epoch.
227 * <p>
228 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
229 */
230 private long getCurrentTimeMillis() {
231 if (mCurrentTimeMillisForTest == null) {
232 return System.currentTimeMillis();
233 } else {
234 return mCurrentTimeMillisForTest;
235 }
236 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100237}