blob: 78ca252697e30167f7429056484db63124bd6362 [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;
50 /** The injected current time in milliseconds since the epoch. Used only by tests. */
51 private Long mCurrentTimeMillisForTest;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010052
53 /**
54 * Creates a new instance of the helper.
55 * <p>
56 * Generally you should have a single instance of this helper in any context.
57 *
58 * @param resources used to look up strings
59 */
Flavio Lerda7d7473a2011-07-06 14:21:46 +010060 public PhoneCallDetailsHelper(Context context, Resources resources, String voicemailNumber,
61 Drawable incomingDrawable, Drawable outgoingDrawable, Drawable missedDrawable,
62 Drawable voicemailDrawable) {
63 mContext = context;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010064 mResources = resources;
Flavio Lerda696e8132011-07-05 19:00:23 +010065 mVoicemailNumber = voicemailNumber;
Flavio Lerda7d7473a2011-07-06 14:21:46 +010066 mIncomingDrawable = incomingDrawable;
67 mOutgoingDrawable = outgoingDrawable;
68 mMissedDrawable = missedDrawable;
69 mVoicemailDrawable = voicemailDrawable;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010070 }
71
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010072 /**
73 * Fills the call details views with content.
74 *
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010075 * @param date the date of the call, in milliseconds since the epoch
76 * @param callType the type of call, as defined in the call log table
77 * @param name the name of the contact, if available
78 * @param number the number of the other party involved in the call
79 * @param numberType the type of phone, e.g., {@link Phone#TYPE_HOME}, 0 if not available
80 * @param numberLabel the custom label associated with the phone number in the contact
81 */
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010082 public void setPhoneCallDetails(PhoneCallDetailsViews views, long date,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010083 int callType, CharSequence name, CharSequence number, int numberType,
84 CharSequence numberLabel) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +010085 Drawable callTypeDrawable = null;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010086 switch (callType) {
87 case Calls.INCOMING_TYPE:
Flavio Lerda7d7473a2011-07-06 14:21:46 +010088 callTypeDrawable = mIncomingDrawable;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010089 break;
90
91 case Calls.OUTGOING_TYPE:
Flavio Lerda7d7473a2011-07-06 14:21:46 +010092 callTypeDrawable = mOutgoingDrawable;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010093 break;
94
95 case Calls.MISSED_TYPE:
Flavio Lerda7d7473a2011-07-06 14:21:46 +010096 callTypeDrawable = mMissedDrawable;
97 break;
98
99 case Calls.VOICEMAIL_TYPE:
100 callTypeDrawable = mVoicemailDrawable;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100101 break;
102 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100103 CharSequence shortDateText =
104 DateUtils.getRelativeTimeSpanString(date,
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100105 getCurrentTimeMillis(),
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100106 DateUtils.MINUTE_IN_MILLIS,
107 DateUtils.FORMAT_ABBREV_RELATIVE);
108
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100109 CharSequence numberFormattedLabel = null;
110 // Only show a label if the number is shown and it is not a SIP address.
111 if (!TextUtils.isEmpty(number) && !PhoneNumberUtils.isUriNumber(number.toString())) {
Flavio Lerdad72bf8a2011-07-05 16:00:09 +0100112 numberFormattedLabel = Phone.getTypeLabel(mResources, numberType, numberLabel);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100113 }
114
115 CharSequence nameText;
116 CharSequence numberText;
117 if (TextUtils.isEmpty(name)) {
Flavio Lerda696e8132011-07-05 19:00:23 +0100118 nameText = getDisplayNumber(number);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100119 numberText = "";
120 } else {
121 nameText = name;
Flavio Lerda696e8132011-07-05 19:00:23 +0100122 numberText = getDisplayNumber(number);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100123 if (callType != 0 && numberFormattedLabel != null) {
124 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
125 numberFormattedLabel + " " + number, 0,
126 numberFormattedLabel.length(),
127 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
128 }
129 }
130
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100131 ImageView callTypeImage = new ImageView(mContext);
132 callTypeImage.setImageDrawable(callTypeDrawable);
133 views.callTypesLayout.removeAllViews();
134 views.callTypesLayout.addView(callTypeImage);
135
136 views.dateView.setText(shortDateText);
137 views.dateView.setVisibility(View.VISIBLE);
138 views.nameView.setText(nameText);
139 views.nameView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100140 // Do not show the number if it is not available. This happens if we have only the number,
141 // in which case the number is shown in the name field instead.
142 if (!TextUtils.isEmpty(numberText)) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100143 views.numberView.setText(numberText);
144 views.numberView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100145 } else {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100146 views.numberView.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100147 }
148 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100149
150 private CharSequence getDisplayNumber(CharSequence number) {
151 if (TextUtils.isEmpty(number)) {
152 return "";
153 }
154 if (number.equals(CallerInfo.UNKNOWN_NUMBER)) {
155 return mResources.getString(R.string.unknown);
156 }
157 if (number.equals(CallerInfo.PRIVATE_NUMBER)) {
158 return mResources.getString(R.string.private_num);
159 }
160 if (number.equals(CallerInfo.PAYPHONE_NUMBER)) {
161 return mResources.getString(R.string.payphone);
162 }
163 if (PhoneNumberUtils.extractNetworkPortion(number.toString()).equals(mVoicemailNumber)) {
164 return mResources.getString(R.string.voicemail);
165 }
166 return number;
167 }
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100168
169 public void setCurrentTimeForTest(long currentTimeMillis) {
170 mCurrentTimeMillisForTest = currentTimeMillis;
171 }
172
173 /**
174 * Returns the current time in milliseconds since the epoch.
175 * <p>
176 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
177 */
178 private long getCurrentTimeMillis() {
179 if (mCurrentTimeMillisForTest == null) {
180 return System.currentTimeMillis();
181 } else {
182 return mCurrentTimeMillisForTest;
183 }
184 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100185}