blob: b52dacb51688ffb44a8d644b9249af3cd7d29a6c [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) {
89 final Drawable callTypeDrawable;
90 switch (details.callType) {
91 case Calls.INCOMING_TYPE:
92 callTypeDrawable = mIncomingDrawable;
93 break;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010094
Flavio Lerda4586feb2011-07-09 17:03:48 +010095 case Calls.OUTGOING_TYPE:
96 callTypeDrawable = mOutgoingDrawable;
97 break;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010098
Flavio Lerda4586feb2011-07-09 17:03:48 +010099 case Calls.MISSED_TYPE:
100 callTypeDrawable = mMissedDrawable;
101 break;
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100102
Flavio Lerda4586feb2011-07-09 17:03:48 +0100103 case Calls.VOICEMAIL_TYPE:
104 callTypeDrawable = mVoicemailDrawable;
105 break;
106
107 default:
108 throw new IllegalArgumentException("invalid call type: " + details.callType);
109 }
110 ImageView callTypeImage = new ImageView(mContext);
111 callTypeImage.setImageDrawable(callTypeDrawable);
112 views.callTypeIcons.removeAllViews();
113 views.callTypeIcons.addView(callTypeImage);
114
115 views.callTypeIcons.setVisibility(View.VISIBLE);
116 views.callTypeText.setVisibility(View.GONE);
117 views.callTypeSeparator.setVisibility(View.GONE);
118 } else {
119 String callTypeName;
120 switch (details.callType) {
121 case Calls.INCOMING_TYPE:
122 callTypeName = mIncomingName;
123 break;
124
125 case Calls.OUTGOING_TYPE:
126 callTypeName = mOutgoingName;
127 break;
128
129 case Calls.MISSED_TYPE:
130 callTypeName = mMissedName;
131 break;
132
133 case Calls.VOICEMAIL_TYPE:
134 callTypeName = mVoicemailName;
135 break;
136
137 default:
138 throw new IllegalArgumentException("invalid call type: " + details.callType);
139 }
140 views.callTypeText.setText(callTypeName);
141 views.callTypeIcons.removeAllViews();
142
143 views.callTypeText.setVisibility(View.VISIBLE);
144 views.callTypeSeparator.setVisibility(View.VISIBLE);
145 views.callTypeIcons.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100146 }
Flavio Lerda4586feb2011-07-09 17:03:48 +0100147
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100148 CharSequence shortDateText =
Flavio Lerda9de38682011-07-08 20:38:07 +0100149 DateUtils.getRelativeTimeSpanString(details.date,
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100150 getCurrentTimeMillis(),
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100151 DateUtils.MINUTE_IN_MILLIS,
152 DateUtils.FORMAT_ABBREV_RELATIVE);
153
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100154 CharSequence numberFormattedLabel = null;
155 // Only show a label if the number is shown and it is not a SIP address.
Flavio Lerda9de38682011-07-08 20:38:07 +0100156 if (!TextUtils.isEmpty(details.number)
157 && !PhoneNumberUtils.isUriNumber(details.number.toString())) {
158 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
159 details.numberLabel);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100160 }
161
Flavio Lerdab9256f82011-07-09 20:10:57 +0100162 final CharSequence nameText;
163 final CharSequence numberText;
Flavio Lerda9de38682011-07-08 20:38:07 +0100164 if (TextUtils.isEmpty(details.name)) {
165 nameText = getDisplayNumber(details.number);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100166 numberText = "";
167 } else {
Flavio Lerda9de38682011-07-08 20:38:07 +0100168 nameText = details.name;
169 CharSequence displayNumber = getDisplayNumber(details.number);
170 if (details.callType != 0 && numberFormattedLabel != null) {
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100171 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
Flavio Lerdab9256f82011-07-09 20:10:57 +0100172 numberFormattedLabel + " " + displayNumber, 0,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100173 numberFormattedLabel.length(),
174 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Flavio Lerdab9256f82011-07-09 20:10:57 +0100175 } else {
176 numberText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100177 }
178 }
179
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100180 views.dateView.setText(shortDateText);
181 views.dateView.setVisibility(View.VISIBLE);
182 views.nameView.setText(nameText);
183 views.nameView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100184 // Do not show the number if it is not available. This happens if we have only the number,
185 // in which case the number is shown in the name field instead.
186 if (!TextUtils.isEmpty(numberText)) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100187 views.numberView.setText(numberText);
188 views.numberView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100189 } else {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100190 views.numberView.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100191 }
192 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100193
194 private CharSequence getDisplayNumber(CharSequence number) {
195 if (TextUtils.isEmpty(number)) {
196 return "";
197 }
198 if (number.equals(CallerInfo.UNKNOWN_NUMBER)) {
199 return mResources.getString(R.string.unknown);
200 }
201 if (number.equals(CallerInfo.PRIVATE_NUMBER)) {
202 return mResources.getString(R.string.private_num);
203 }
204 if (number.equals(CallerInfo.PAYPHONE_NUMBER)) {
205 return mResources.getString(R.string.payphone);
206 }
207 if (PhoneNumberUtils.extractNetworkPortion(number.toString()).equals(mVoicemailNumber)) {
208 return mResources.getString(R.string.voicemail);
209 }
210 return number;
211 }
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100212
213 public void setCurrentTimeForTest(long currentTimeMillis) {
214 mCurrentTimeMillisForTest = currentTimeMillis;
215 }
216
217 /**
218 * Returns the current time in milliseconds since the epoch.
219 * <p>
220 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
221 */
222 private long getCurrentTimeMillis() {
223 if (mCurrentTimeMillisForTest == null) {
224 return System.currentTimeMillis();
225 } else {
226 return mCurrentTimeMillisForTest;
227 }
228 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100229}