blob: f312a5d9f852ff1ac55b791ec7ca223cb468bb31 [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 Lerdabe45e0f2011-07-25 21:00:30 +0100114 String geocode = mPhoneNumberHelper.getGeocodeForNumber(
Flavio Lerdaebef7712011-07-20 22:03:55 +0100115 details.number.toString(), details.countryIso);
Flavio Lerdabe45e0f2011-07-25 21:00:30 +0100116 if (TextUtils.isEmpty(geocode)) {
117 numberText = mResources.getString(R.string.call_log_empty_gecode);
118 } else {
119 numberText = geocode;
120 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100121 } else {
Flavio Lerda9de38682011-07-08 20:38:07 +0100122 nameText = details.name;
Flavio Lerda223a8432011-07-11 18:40:25 +0100123 if (numberFormattedLabel != null) {
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100124 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
Flavio Lerdab9256f82011-07-09 20:10:57 +0100125 numberFormattedLabel + " " + displayNumber, 0,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100126 numberFormattedLabel.length(),
127 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Flavio Lerdab9256f82011-07-09 20:10:57 +0100128 } else {
129 numberText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100130 }
131 }
132
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100133 views.dateView.setText(shortDateText);
134 views.dateView.setVisibility(View.VISIBLE);
135 views.nameView.setText(nameText);
136 views.nameView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100137 // Do not show the number if it is not available. This happens if we have only the number,
138 // in which case the number is shown in the name field instead.
139 if (!TextUtils.isEmpty(numberText)) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100140 views.numberView.setText(numberText);
141 views.numberView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100142 } else {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100143 views.numberView.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100144 }
Flavio Lerda40569562011-07-22 21:51:29 +0100145
146 // Hide the rest if not visible.
147 views.callTypeView.setVisibility(nameOnly ? View.GONE : View.VISIBLE);
148 views.numberView.setVisibility(nameOnly ? View.GONE : View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100149 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100150
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100151 public void setCurrentTimeForTest(long currentTimeMillis) {
152 mCurrentTimeMillisForTest = currentTimeMillis;
153 }
154
155 /**
156 * Returns the current time in milliseconds since the epoch.
157 * <p>
158 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
159 */
160 private long getCurrentTimeMillis() {
161 if (mCurrentTimeMillisForTest == null) {
162 return System.currentTimeMillis();
163 } else {
164 return mCurrentTimeMillisForTest;
165 }
166 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100167}