blob: 8369a0c84ebe423f956dd95c0b4e266ed08ddb0c [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 Lerda178eeeb2011-07-11 19:51:40 +010020import com.android.contacts.calllog.PhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010021import com.android.contacts.format.FormatUtils;
22
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 Lerda8e39c712011-07-15 19:57:01 +010038 /** The maximum number of icons will be shown to represent the call types in a group. */
39 private static final int MAX_CALL_TYPE_ICONS = 3;
40
Flavio Lerda7d7473a2011-07-06 14:21:46 +010041 private final Context mContext;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010042 private final Resources mResources;
Flavio Lerda7d7473a2011-07-06 14:21:46 +010043 /** The injected current time in milliseconds since the epoch. Used only by tests. */
44 private Long mCurrentTimeMillisForTest;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010045 // Helper classes.
Flavio Lerda9a208cc2011-07-12 21:05:47 +010046 private final CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010047 private final PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010048
49 /**
50 * Creates a new instance of the helper.
51 * <p>
52 * Generally you should have a single instance of this helper in any context.
53 *
54 * @param resources used to look up strings
55 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +010056 public PhoneCallDetailsHelper(Context context, Resources resources,
57 CallTypeHelper callTypeHelper, PhoneNumberHelper phoneNumberHelper) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +010058 mContext = context;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010059 mResources = resources;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010060 mCallTypeHelper = callTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010061 mPhoneNumberHelper = phoneNumberHelper;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010062 }
63
Flavio Lerda9de38682011-07-08 20:38:07 +010064 /** Fills the call details views with content. */
Flavio Lerda4586feb2011-07-09 17:03:48 +010065 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details,
Flavio Lerda76921902011-07-13 21:11:51 +010066 boolean useIcons, boolean isHighlighted) {
Flavio Lerda4586feb2011-07-09 17:03:48 +010067 if (useIcons) {
Flavio Lerda4586feb2011-07-09 17:03:48 +010068 views.callTypeIcons.removeAllViews();
Flavio Lerda223a8432011-07-11 18:40:25 +010069 int count = details.callTypes.length;
Flavio Lerda8e39c712011-07-15 19:57:01 +010070 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
71 int callType = details.callTypes[index];
Flavio Lerda223a8432011-07-11 18:40:25 +010072 ImageView callTypeImage = new ImageView(mContext);
Flavio Lerda9a208cc2011-07-12 21:05:47 +010073 callTypeImage.setImageDrawable(mCallTypeHelper.getCallTypeDrawable(callType));
Flavio Lerda223a8432011-07-11 18:40:25 +010074 views.callTypeIcons.addView(callTypeImage);
75 }
Flavio Lerda4586feb2011-07-09 17:03:48 +010076 views.callTypeIcons.setVisibility(View.VISIBLE);
Flavio Lerda8e39c712011-07-15 19:57:01 +010077 if (count > MAX_CALL_TYPE_ICONS) {
78 views.callTypeText.setVisibility(View.VISIBLE);
79 views.callTypeSeparator.setVisibility(View.VISIBLE);
80 views.callTypeText.setText(
81 mResources.getString(R.string.call_log_item_count, count));
82 } else {
83 views.callTypeText.setVisibility(View.GONE);
84 views.callTypeSeparator.setVisibility(View.GONE);
85 }
Flavio Lerda4586feb2011-07-09 17:03:48 +010086 } else {
87 String callTypeName;
Flavio Lerda223a8432011-07-11 18:40:25 +010088 // Use the name of the first call type.
89 // TODO: We should update this to handle the text for multiple calls as well.
90 int callType = details.callTypes[0];
Flavio Lerda76921902011-07-13 21:11:51 +010091 views.callTypeText.setText(
92 isHighlighted ? mCallTypeHelper.getHighlightedCallTypeText(callType)
93 : mCallTypeHelper.getCallTypeText(callType));
Flavio Lerda4586feb2011-07-09 17:03:48 +010094 views.callTypeIcons.removeAllViews();
95
96 views.callTypeText.setVisibility(View.VISIBLE);
97 views.callTypeSeparator.setVisibility(View.VISIBLE);
98 views.callTypeIcons.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010099 }
Flavio Lerda4586feb2011-07-09 17:03:48 +0100100
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100101 CharSequence shortDateText =
Flavio Lerda9de38682011-07-08 20:38:07 +0100102 DateUtils.getRelativeTimeSpanString(details.date,
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100103 getCurrentTimeMillis(),
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100104 DateUtils.MINUTE_IN_MILLIS,
105 DateUtils.FORMAT_ABBREV_RELATIVE);
106
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100107 CharSequence numberFormattedLabel = null;
108 // Only show a label if the number is shown and it is not a SIP address.
Flavio Lerda9de38682011-07-08 20:38:07 +0100109 if (!TextUtils.isEmpty(details.number)
110 && !PhoneNumberUtils.isUriNumber(details.number.toString())) {
111 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
112 details.numberLabel);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100113 }
114
Flavio Lerdab9256f82011-07-09 20:10:57 +0100115 final CharSequence nameText;
116 final CharSequence numberText;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100117 final CharSequence displayNumber =
118 mPhoneNumberHelper.getDisplayNumber(details.number, details.formattedNumber);
Flavio Lerda9de38682011-07-08 20:38:07 +0100119 if (TextUtils.isEmpty(details.name)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100120 nameText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100121 numberText = "";
122 } else {
Flavio Lerda9de38682011-07-08 20:38:07 +0100123 nameText = details.name;
Flavio Lerda223a8432011-07-11 18:40:25 +0100124 if (numberFormattedLabel != null) {
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100125 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
Flavio Lerdab9256f82011-07-09 20:10:57 +0100126 numberFormattedLabel + " " + displayNumber, 0,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100127 numberFormattedLabel.length(),
128 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Flavio Lerdab9256f82011-07-09 20:10:57 +0100129 } else {
130 numberText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100131 }
132 }
133
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100134 views.dateView.setText(shortDateText);
135 views.dateView.setVisibility(View.VISIBLE);
136 views.nameView.setText(nameText);
137 views.nameView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100138 // Do not show the number if it is not available. This happens if we have only the number,
139 // in which case the number is shown in the name field instead.
140 if (!TextUtils.isEmpty(numberText)) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100141 views.numberView.setText(numberText);
142 views.numberView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100143 } else {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100144 views.numberView.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100145 }
146 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100147
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100148 public void setCurrentTimeForTest(long currentTimeMillis) {
149 mCurrentTimeMillisForTest = currentTimeMillis;
150 }
151
152 /**
153 * Returns the current time in milliseconds since the epoch.
154 * <p>
155 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
156 */
157 private long getCurrentTimeMillis() {
158 if (mCurrentTimeMillisForTest == null) {
159 return System.currentTimeMillis();
160 } else {
161 return mCurrentTimeMillisForTest;
162 }
163 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100164}