blob: f884df7a1fa623d59c467ab50e7f03cac5fa5484 [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 Lerda9de38682011-07-08 20:38:07 +010072 /** Fills the call details views with content. */
73 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +010074 Drawable callTypeDrawable = null;
Flavio Lerda9de38682011-07-08 20:38:07 +010075 switch (details.callType) {
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010076 case Calls.INCOMING_TYPE:
Flavio Lerda7d7473a2011-07-06 14:21:46 +010077 callTypeDrawable = mIncomingDrawable;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010078 break;
79
80 case Calls.OUTGOING_TYPE:
Flavio Lerda7d7473a2011-07-06 14:21:46 +010081 callTypeDrawable = mOutgoingDrawable;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010082 break;
83
84 case Calls.MISSED_TYPE:
Flavio Lerda7d7473a2011-07-06 14:21:46 +010085 callTypeDrawable = mMissedDrawable;
86 break;
87
88 case Calls.VOICEMAIL_TYPE:
89 callTypeDrawable = mVoicemailDrawable;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010090 break;
91 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010092 CharSequence shortDateText =
Flavio Lerda9de38682011-07-08 20:38:07 +010093 DateUtils.getRelativeTimeSpanString(details.date,
Flavio Lerda7d7473a2011-07-06 14:21:46 +010094 getCurrentTimeMillis(),
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010095 DateUtils.MINUTE_IN_MILLIS,
96 DateUtils.FORMAT_ABBREV_RELATIVE);
97
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010098 CharSequence numberFormattedLabel = null;
99 // Only show a label if the number is shown and it is not a SIP address.
Flavio Lerda9de38682011-07-08 20:38:07 +0100100 if (!TextUtils.isEmpty(details.number)
101 && !PhoneNumberUtils.isUriNumber(details.number.toString())) {
102 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
103 details.numberLabel);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100104 }
105
Flavio Lerdab9256f82011-07-09 20:10:57 +0100106 final CharSequence nameText;
107 final CharSequence numberText;
Flavio Lerda9de38682011-07-08 20:38:07 +0100108 if (TextUtils.isEmpty(details.name)) {
109 nameText = getDisplayNumber(details.number);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100110 numberText = "";
111 } else {
Flavio Lerda9de38682011-07-08 20:38:07 +0100112 nameText = details.name;
113 CharSequence displayNumber = getDisplayNumber(details.number);
114 if (details.callType != 0 && numberFormattedLabel != null) {
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100115 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
Flavio Lerdab9256f82011-07-09 20:10:57 +0100116 numberFormattedLabel + " " + displayNumber, 0,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100117 numberFormattedLabel.length(),
118 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Flavio Lerdab9256f82011-07-09 20:10:57 +0100119 } else {
120 numberText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100121 }
122 }
123
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100124 ImageView callTypeImage = new ImageView(mContext);
125 callTypeImage.setImageDrawable(callTypeDrawable);
126 views.callTypesLayout.removeAllViews();
127 views.callTypesLayout.addView(callTypeImage);
128
129 views.dateView.setText(shortDateText);
130 views.dateView.setVisibility(View.VISIBLE);
131 views.nameView.setText(nameText);
132 views.nameView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100133 // Do not show the number if it is not available. This happens if we have only the number,
134 // in which case the number is shown in the name field instead.
135 if (!TextUtils.isEmpty(numberText)) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100136 views.numberView.setText(numberText);
137 views.numberView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100138 } else {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100139 views.numberView.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100140 }
141 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100142
143 private CharSequence getDisplayNumber(CharSequence number) {
144 if (TextUtils.isEmpty(number)) {
145 return "";
146 }
147 if (number.equals(CallerInfo.UNKNOWN_NUMBER)) {
148 return mResources.getString(R.string.unknown);
149 }
150 if (number.equals(CallerInfo.PRIVATE_NUMBER)) {
151 return mResources.getString(R.string.private_num);
152 }
153 if (number.equals(CallerInfo.PAYPHONE_NUMBER)) {
154 return mResources.getString(R.string.payphone);
155 }
156 if (PhoneNumberUtils.extractNetworkPortion(number.toString()).equals(mVoicemailNumber)) {
157 return mResources.getString(R.string.voicemail);
158 }
159 return number;
160 }
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100161
162 public void setCurrentTimeForTest(long currentTimeMillis) {
163 mCurrentTimeMillisForTest = currentTimeMillis;
164 }
165
166 /**
167 * Returns the current time in milliseconds since the epoch.
168 * <p>
169 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
170 */
171 private long getCurrentTimeMillis() {
172 if (mCurrentTimeMillisForTest == null) {
173 return System.currentTimeMillis();
174 } else {
175 return mCurrentTimeMillisForTest;
176 }
177 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100178}