blob: 019e6083392ef23e7e3c807404d10ed2d101a4fe [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
23import android.content.res.Resources;
24import android.graphics.Typeface;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010025import android.provider.ContactsContract.CommonDataKinds.Phone;
26import android.telephony.PhoneNumberUtils;
27import android.text.Spanned;
28import android.text.TextUtils;
29import android.text.format.DateUtils;
30import android.view.View;
31
32/**
33 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
34 */
35public class PhoneCallDetailsHelper {
Flavio Lerda8e39c712011-07-15 19:57:01 +010036 /** The maximum number of icons will be shown to represent the call types in a group. */
37 private static final int MAX_CALL_TYPE_ICONS = 3;
38
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010039 private final Resources mResources;
Flavio Lerda7d7473a2011-07-06 14:21:46 +010040 /** The injected current time in milliseconds since the epoch. Used only by tests. */
41 private Long mCurrentTimeMillisForTest;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010042 // Helper classes.
Flavio Lerda9a208cc2011-07-12 21:05:47 +010043 private final CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010044 private final PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010045
46 /**
47 * Creates a new instance of the helper.
48 * <p>
49 * Generally you should have a single instance of this helper in any context.
50 *
51 * @param resources used to look up strings
52 */
Flavio Lerda40569562011-07-22 21:51:29 +010053 public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper,
54 PhoneNumberHelper phoneNumberHelper) {
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010055 mResources = resources;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010056 mCallTypeHelper = callTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010057 mPhoneNumberHelper = phoneNumberHelper;
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,
Flavio Lerda40569562011-07-22 21:51:29 +010062 boolean useIcons, boolean isHighlighted, boolean nameOnly) {
Flavio Lerda4586feb2011-07-09 17:03:48 +010063 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;
Flavio Lerda8e39c712011-07-15 19:57:01 +010066 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
Flavio Lerda40569562011-07-22 21:51:29 +010067 mCallTypeHelper.inflateCallTypeIcon(details.callTypes[index], views.callTypeIcons);
Flavio Lerda223a8432011-07-11 18:40:25 +010068 }
Flavio Lerda4586feb2011-07-09 17:03:48 +010069 views.callTypeIcons.setVisibility(View.VISIBLE);
Flavio Lerda8e39c712011-07-15 19:57:01 +010070 if (count > MAX_CALL_TYPE_ICONS) {
71 views.callTypeText.setVisibility(View.VISIBLE);
72 views.callTypeSeparator.setVisibility(View.VISIBLE);
73 views.callTypeText.setText(
74 mResources.getString(R.string.call_log_item_count, count));
75 } else {
76 views.callTypeText.setVisibility(View.GONE);
77 views.callTypeSeparator.setVisibility(View.GONE);
78 }
Flavio Lerda4586feb2011-07-09 17:03:48 +010079 } else {
80 String callTypeName;
Flavio Lerda223a8432011-07-11 18:40:25 +010081 // Use the name of the first call type.
82 // TODO: We should update this to handle the text for multiple calls as well.
83 int callType = details.callTypes[0];
Flavio Lerda76921902011-07-13 21:11:51 +010084 views.callTypeText.setText(
85 isHighlighted ? mCallTypeHelper.getHighlightedCallTypeText(callType)
86 : mCallTypeHelper.getCallTypeText(callType));
Flavio Lerda4586feb2011-07-09 17:03:48 +010087 views.callTypeIcons.removeAllViews();
88
89 views.callTypeText.setVisibility(View.VISIBLE);
90 views.callTypeSeparator.setVisibility(View.VISIBLE);
91 views.callTypeIcons.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010092 }
Flavio Lerda4586feb2011-07-09 17:03:48 +010093
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010094 CharSequence shortDateText =
Flavio Lerda9de38682011-07-08 20:38:07 +010095 DateUtils.getRelativeTimeSpanString(details.date,
Flavio Lerda7d7473a2011-07-06 14:21:46 +010096 getCurrentTimeMillis(),
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010097 DateUtils.MINUTE_IN_MILLIS,
98 DateUtils.FORMAT_ABBREV_RELATIVE);
99
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100100 CharSequence numberFormattedLabel = null;
101 // Only show a label if the number is shown and it is not a SIP address.
Flavio Lerda9de38682011-07-08 20:38:07 +0100102 if (!TextUtils.isEmpty(details.number)
103 && !PhoneNumberUtils.isUriNumber(details.number.toString())) {
104 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
105 details.numberLabel);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100106 }
107
Flavio Lerdab9256f82011-07-09 20:10:57 +0100108 final CharSequence nameText;
109 final CharSequence numberText;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100110 final CharSequence displayNumber =
111 mPhoneNumberHelper.getDisplayNumber(details.number, details.formattedNumber);
Flavio Lerda9de38682011-07-08 20:38:07 +0100112 if (TextUtils.isEmpty(details.name)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100113 nameText = displayNumber;
Flavio Lerda9c466372011-07-19 17:38:17 +0100114 numberText = mPhoneNumberHelper.getGeocodeForNumber(
Flavio Lerdaebef7712011-07-20 22:03:55 +0100115 details.number.toString(), details.countryIso);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100116 } else {
Flavio Lerda9de38682011-07-08 20:38:07 +0100117 nameText = details.name;
Flavio Lerda223a8432011-07-11 18:40:25 +0100118 if (numberFormattedLabel != null) {
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100119 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
Flavio Lerdab9256f82011-07-09 20:10:57 +0100120 numberFormattedLabel + " " + displayNumber, 0,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100121 numberFormattedLabel.length(),
122 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Flavio Lerdab9256f82011-07-09 20:10:57 +0100123 } else {
124 numberText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100125 }
126 }
127
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100128 views.dateView.setText(shortDateText);
129 views.dateView.setVisibility(View.VISIBLE);
130 views.nameView.setText(nameText);
131 views.nameView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100132 // Do not show the number if it is not available. This happens if we have only the number,
133 // in which case the number is shown in the name field instead.
134 if (!TextUtils.isEmpty(numberText)) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100135 views.numberView.setText(numberText);
136 views.numberView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100137 } else {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100138 views.numberView.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100139 }
Flavio Lerda40569562011-07-22 21:51:29 +0100140
141 // Hide the rest if not visible.
142 views.callTypeView.setVisibility(nameOnly ? View.GONE : View.VISIBLE);
143 views.numberView.setVisibility(nameOnly ? View.GONE : View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100144 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100145
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100146 public void setCurrentTimeForTest(long currentTimeMillis) {
147 mCurrentTimeMillisForTest = currentTimeMillis;
148 }
149
150 /**
151 * Returns the current time in milliseconds since the epoch.
152 * <p>
153 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
154 */
155 private long getCurrentTimeMillis() {
156 if (mCurrentTimeMillisForTest == null) {
157 return System.currentTimeMillis();
158 } else {
159 return mCurrentTimeMillisForTest;
160 }
161 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100162}