blob: cbf3c5c35092e673c1886dfd41bde9bbb2f923ff [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
Flavio Lerda9a208cc2011-07-12 21:05:47 +010019import com.android.contacts.calllog.CallTypeHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010020import com.android.contacts.format.FormatUtils;
Flavio Lerda696e8132011-07-05 19:00:23 +010021import com.android.internal.telephony.CallerInfo;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010022
Flavio Lerda7d7473a2011-07-06 14:21:46 +010023import android.content.Context;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010024import android.content.res.Resources;
25import android.graphics.Typeface;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010026import android.provider.ContactsContract.CommonDataKinds.Phone;
27import android.telephony.PhoneNumberUtils;
28import android.text.Spanned;
29import android.text.TextUtils;
30import android.text.format.DateUtils;
31import android.view.View;
Flavio Lerda7d7473a2011-07-06 14:21:46 +010032import android.widget.ImageView;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010033
34/**
35 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
36 */
37public class PhoneCallDetailsHelper {
Flavio Lerda7d7473a2011-07-06 14:21:46 +010038 private final Context mContext;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010039 private final Resources mResources;
Flavio Lerda696e8132011-07-05 19:00:23 +010040 private final String mVoicemailNumber;
Flavio Lerda7d7473a2011-07-06 14:21:46 +010041 /** The injected current time in milliseconds since the epoch. Used only by tests. */
42 private Long mCurrentTimeMillisForTest;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010043 private final CallTypeHelper mCallTypeHelper;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010044
45 /**
46 * Creates a new instance of the helper.
47 * <p>
48 * Generally you should have a single instance of this helper in any context.
49 *
50 * @param resources used to look up strings
51 */
Flavio Lerda7d7473a2011-07-06 14:21:46 +010052 public PhoneCallDetailsHelper(Context context, Resources resources, String voicemailNumber,
Flavio Lerda9a208cc2011-07-12 21:05:47 +010053 CallTypeHelper callTypeHelper) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +010054 mContext = context;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010055 mResources = resources;
Flavio Lerda696e8132011-07-05 19:00:23 +010056 mVoicemailNumber = voicemailNumber;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010057 mCallTypeHelper = callTypeHelper;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010058 }
59
Flavio Lerda9de38682011-07-08 20:38:07 +010060 /** Fills the call details views with content. */
Flavio Lerda4586feb2011-07-09 17:03:48 +010061 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details,
62 boolean useIcons) {
63 if (useIcons) {
Flavio Lerda4586feb2011-07-09 17:03:48 +010064 views.callTypeIcons.removeAllViews();
Flavio Lerda223a8432011-07-11 18:40:25 +010065 int count = details.callTypes.length;
66 for (int callType : details.callTypes) {
67 ImageView callTypeImage = new ImageView(mContext);
Flavio Lerda9a208cc2011-07-12 21:05:47 +010068 callTypeImage.setImageDrawable(mCallTypeHelper.getCallTypeDrawable(callType));
Flavio Lerda223a8432011-07-11 18:40:25 +010069 views.callTypeIcons.addView(callTypeImage);
70 }
Flavio Lerda4586feb2011-07-09 17:03:48 +010071 views.callTypeIcons.setVisibility(View.VISIBLE);
72 views.callTypeText.setVisibility(View.GONE);
73 views.callTypeSeparator.setVisibility(View.GONE);
74 } else {
75 String callTypeName;
Flavio Lerda223a8432011-07-11 18:40:25 +010076 // Use the name of the first call type.
77 // TODO: We should update this to handle the text for multiple calls as well.
78 int callType = details.callTypes[0];
Flavio Lerda9a208cc2011-07-12 21:05:47 +010079 views.callTypeText.setText(mCallTypeHelper.getCallTypeText(callType));
Flavio Lerda4586feb2011-07-09 17:03:48 +010080 views.callTypeIcons.removeAllViews();
81
82 views.callTypeText.setVisibility(View.VISIBLE);
83 views.callTypeSeparator.setVisibility(View.VISIBLE);
84 views.callTypeIcons.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010085 }
Flavio Lerda4586feb2011-07-09 17:03:48 +010086
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010087 CharSequence shortDateText =
Flavio Lerda9de38682011-07-08 20:38:07 +010088 DateUtils.getRelativeTimeSpanString(details.date,
Flavio Lerda7d7473a2011-07-06 14:21:46 +010089 getCurrentTimeMillis(),
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010090 DateUtils.MINUTE_IN_MILLIS,
91 DateUtils.FORMAT_ABBREV_RELATIVE);
92
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010093 CharSequence numberFormattedLabel = null;
94 // Only show a label if the number is shown and it is not a SIP address.
Flavio Lerda9de38682011-07-08 20:38:07 +010095 if (!TextUtils.isEmpty(details.number)
96 && !PhoneNumberUtils.isUriNumber(details.number.toString())) {
97 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
98 details.numberLabel);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010099 }
100
Flavio Lerdab9256f82011-07-09 20:10:57 +0100101 final CharSequence nameText;
102 final CharSequence numberText;
Flavio Lerda9de38682011-07-08 20:38:07 +0100103 if (TextUtils.isEmpty(details.name)) {
Flavio Lerda371d5f92011-07-09 19:45:32 +0100104 nameText = getDisplayNumber(details.number, details.formattedNumber);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100105 numberText = "";
106 } else {
Flavio Lerda9de38682011-07-08 20:38:07 +0100107 nameText = details.name;
Flavio Lerda371d5f92011-07-09 19:45:32 +0100108 CharSequence displayNumber = getDisplayNumber(details.number, details.formattedNumber);
Flavio Lerda223a8432011-07-11 18:40:25 +0100109 if (numberFormattedLabel != null) {
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100110 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
Flavio Lerdab9256f82011-07-09 20:10:57 +0100111 numberFormattedLabel + " " + displayNumber, 0,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100112 numberFormattedLabel.length(),
113 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Flavio Lerdab9256f82011-07-09 20:10:57 +0100114 } else {
115 numberText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100116 }
117 }
118
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100119 views.dateView.setText(shortDateText);
120 views.dateView.setVisibility(View.VISIBLE);
121 views.nameView.setText(nameText);
122 views.nameView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100123 // Do not show the number if it is not available. This happens if we have only the number,
124 // in which case the number is shown in the name field instead.
125 if (!TextUtils.isEmpty(numberText)) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100126 views.numberView.setText(numberText);
127 views.numberView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100128 } else {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100129 views.numberView.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100130 }
131 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100132
Flavio Lerda371d5f92011-07-09 19:45:32 +0100133 private CharSequence getDisplayNumber(CharSequence number, CharSequence formattedNumber) {
Flavio Lerda696e8132011-07-05 19:00:23 +0100134 if (TextUtils.isEmpty(number)) {
135 return "";
136 }
137 if (number.equals(CallerInfo.UNKNOWN_NUMBER)) {
138 return mResources.getString(R.string.unknown);
139 }
140 if (number.equals(CallerInfo.PRIVATE_NUMBER)) {
141 return mResources.getString(R.string.private_num);
142 }
143 if (number.equals(CallerInfo.PAYPHONE_NUMBER)) {
144 return mResources.getString(R.string.payphone);
145 }
146 if (PhoneNumberUtils.extractNetworkPortion(number.toString()).equals(mVoicemailNumber)) {
147 return mResources.getString(R.string.voicemail);
148 }
Flavio Lerda371d5f92011-07-09 19:45:32 +0100149 if (TextUtils.isEmpty(formattedNumber)) {
150 return number;
151 } else {
152 return formattedNumber;
153 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100154 }
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100155
156 public void setCurrentTimeForTest(long currentTimeMillis) {
157 mCurrentTimeMillisForTest = currentTimeMillis;
158 }
159
160 /**
161 * Returns the current time in milliseconds since the epoch.
162 * <p>
163 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
164 */
165 private long getCurrentTimeMillis() {
166 if (mCurrentTimeMillisForTest == null) {
167 return System.currentTimeMillis();
168 } else {
169 return mCurrentTimeMillisForTest;
170 }
171 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100172}