blob: 08e6a56cbe984dd712b8a32f3fb39785e6095beb [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;
Flavio Lerdab88abaa2011-08-02 23:38:36 +010031import android.widget.TextView;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010032
33/**
34 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
35 */
36public class PhoneCallDetailsHelper {
Flavio Lerda8e39c712011-07-15 19:57:01 +010037 /** The maximum number of icons will be shown to represent the call types in a group. */
38 private static final int MAX_CALL_TYPE_ICONS = 3;
39
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010040 private final Resources mResources;
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 Lerda178eeeb2011-07-11 19:51:40 +010043 // Helper classes.
Flavio Lerda9a208cc2011-07-12 21:05:47 +010044 private final CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010045 private final PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010046
47 /**
48 * Creates a new instance of the helper.
49 * <p>
50 * Generally you should have a single instance of this helper in any context.
51 *
52 * @param resources used to look up strings
53 */
Flavio Lerda40569562011-07-22 21:51:29 +010054 public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper,
55 PhoneNumberHelper phoneNumberHelper) {
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010056 mResources = resources;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010057 mCallTypeHelper = callTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010058 mPhoneNumberHelper = phoneNumberHelper;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010059 }
60
Flavio Lerda9de38682011-07-08 20:38:07 +010061 /** Fills the call details views with content. */
Flavio Lerda4586feb2011-07-09 17:03:48 +010062 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details,
Flavio Lerdab88abaa2011-08-02 23:38:36 +010063 boolean useIcons, boolean isHighlighted) {
Flavio Lerda4586feb2011-07-09 17:03:48 +010064 if (useIcons) {
Daniel Lehmann6ecb7322011-08-01 21:39:29 -070065 views.callTypeIcons.clear();
Flavio Lerda223a8432011-07-11 18:40:25 +010066 int count = details.callTypes.length;
Flavio Lerda8e39c712011-07-15 19:57:01 +010067 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
Daniel Lehmann6ecb7322011-08-01 21:39:29 -070068 views.callTypeIcons.add(details.callTypes[index]);
Flavio Lerda223a8432011-07-11 18:40:25 +010069 }
Flavio Lerda4586feb2011-07-09 17:03:48 +010070 views.callTypeIcons.setVisibility(View.VISIBLE);
Flavio Lerda8e39c712011-07-15 19:57:01 +010071 if (count > MAX_CALL_TYPE_ICONS) {
72 views.callTypeText.setVisibility(View.VISIBLE);
73 views.callTypeSeparator.setVisibility(View.VISIBLE);
74 views.callTypeText.setText(
75 mResources.getString(R.string.call_log_item_count, count));
76 } else {
77 views.callTypeText.setVisibility(View.GONE);
78 views.callTypeSeparator.setVisibility(View.GONE);
79 }
Flavio Lerda4586feb2011-07-09 17:03:48 +010080 } else {
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));
Daniel Lehmann6ecb7322011-08-01 21:39:29 -070087 views.callTypeIcons.clear();
Flavio Lerda4586feb2011-07-09 17:03:48 +010088
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);
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100134 views.nameView.setText(nameText);
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100135 views.numberView.setText(numberText);
136 }
137
138 /** Sets the name in the text view for the given phone call. */
139 public void setPhoneCallName(TextView nameView, PhoneCallDetails details) {
140 final CharSequence nameText;
141 final CharSequence displayNumber =
142 mPhoneNumberHelper.getDisplayNumber(details.number, details.formattedNumber);
143 if (TextUtils.isEmpty(details.name)) {
144 nameText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100145 } else {
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100146 nameText = details.name;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100147 }
Flavio Lerda40569562011-07-22 21:51:29 +0100148
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100149 nameView.setText(nameText);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100150 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100151
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100152 public void setCurrentTimeForTest(long currentTimeMillis) {
153 mCurrentTimeMillisForTest = currentTimeMillis;
154 }
155
156 /**
157 * Returns the current time in milliseconds since the epoch.
158 * <p>
159 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
160 */
161 private long getCurrentTimeMillis() {
162 if (mCurrentTimeMillisForTest == null) {
163 return System.currentTimeMillis();
164 } else {
165 return mCurrentTimeMillisForTest;
166 }
167 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100168}