blob: 6bdfbaa703d6022a957fad156c83affc6d43316e [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 Lerda7d7473a2011-07-06 14:21:46 +010038 private final Context mContext;
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 Lerda178eeeb2011-07-11 19:51:40 +010053 public PhoneCallDetailsHelper(Context context, Resources resources,
54 CallTypeHelper callTypeHelper, PhoneNumberHelper phoneNumberHelper) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +010055 mContext = context;
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,
63 boolean useIcons) {
64 if (useIcons) {
Flavio Lerda4586feb2011-07-09 17:03:48 +010065 views.callTypeIcons.removeAllViews();
Flavio Lerda223a8432011-07-11 18:40:25 +010066 int count = details.callTypes.length;
67 for (int callType : details.callTypes) {
68 ImageView callTypeImage = new ImageView(mContext);
Flavio Lerda9a208cc2011-07-12 21:05:47 +010069 callTypeImage.setImageDrawable(mCallTypeHelper.getCallTypeDrawable(callType));
Flavio Lerda223a8432011-07-11 18:40:25 +010070 views.callTypeIcons.addView(callTypeImage);
71 }
Flavio Lerda4586feb2011-07-09 17:03:48 +010072 views.callTypeIcons.setVisibility(View.VISIBLE);
73 views.callTypeText.setVisibility(View.GONE);
74 views.callTypeSeparator.setVisibility(View.GONE);
75 } else {
76 String callTypeName;
Flavio Lerda223a8432011-07-11 18:40:25 +010077 // Use the name of the first call type.
78 // TODO: We should update this to handle the text for multiple calls as well.
79 int callType = details.callTypes[0];
Flavio Lerda9a208cc2011-07-12 21:05:47 +010080 views.callTypeText.setText(mCallTypeHelper.getCallTypeText(callType));
Flavio Lerda4586feb2011-07-09 17:03:48 +010081 views.callTypeIcons.removeAllViews();
82
83 views.callTypeText.setVisibility(View.VISIBLE);
84 views.callTypeSeparator.setVisibility(View.VISIBLE);
85 views.callTypeIcons.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010086 }
Flavio Lerda4586feb2011-07-09 17:03:48 +010087
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010088 CharSequence shortDateText =
Flavio Lerda9de38682011-07-08 20:38:07 +010089 DateUtils.getRelativeTimeSpanString(details.date,
Flavio Lerda7d7473a2011-07-06 14:21:46 +010090 getCurrentTimeMillis(),
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010091 DateUtils.MINUTE_IN_MILLIS,
92 DateUtils.FORMAT_ABBREV_RELATIVE);
93
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010094 CharSequence numberFormattedLabel = null;
95 // Only show a label if the number is shown and it is not a SIP address.
Flavio Lerda9de38682011-07-08 20:38:07 +010096 if (!TextUtils.isEmpty(details.number)
97 && !PhoneNumberUtils.isUriNumber(details.number.toString())) {
98 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
99 details.numberLabel);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100100 }
101
Flavio Lerdab9256f82011-07-09 20:10:57 +0100102 final CharSequence nameText;
103 final CharSequence numberText;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100104 final CharSequence displayNumber =
105 mPhoneNumberHelper.getDisplayNumber(details.number, details.formattedNumber);
Flavio Lerda9de38682011-07-08 20:38:07 +0100106 if (TextUtils.isEmpty(details.name)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100107 nameText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100108 numberText = "";
109 } else {
Flavio Lerda9de38682011-07-08 20:38:07 +0100110 nameText = details.name;
Flavio Lerda223a8432011-07-11 18:40:25 +0100111 if (numberFormattedLabel != null) {
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100112 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
Flavio Lerdab9256f82011-07-09 20:10:57 +0100113 numberFormattedLabel + " " + displayNumber, 0,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100114 numberFormattedLabel.length(),
115 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Flavio Lerdab9256f82011-07-09 20:10:57 +0100116 } else {
117 numberText = displayNumber;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100118 }
119 }
120
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100121 views.dateView.setText(shortDateText);
122 views.dateView.setVisibility(View.VISIBLE);
123 views.nameView.setText(nameText);
124 views.nameView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100125 // Do not show the number if it is not available. This happens if we have only the number,
126 // in which case the number is shown in the name field instead.
127 if (!TextUtils.isEmpty(numberText)) {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100128 views.numberView.setText(numberText);
129 views.numberView.setVisibility(View.VISIBLE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100130 } else {
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100131 views.numberView.setVisibility(View.GONE);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100132 }
133 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100134
Flavio Lerda7d7473a2011-07-06 14:21:46 +0100135 public void setCurrentTimeForTest(long currentTimeMillis) {
136 mCurrentTimeMillisForTest = currentTimeMillis;
137 }
138
139 /**
140 * Returns the current time in milliseconds since the epoch.
141 * <p>
142 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
143 */
144 private long getCurrentTimeMillis() {
145 if (mCurrentTimeMillisForTest == null) {
146 return System.currentTimeMillis();
147 } else {
148 return mCurrentTimeMillisForTest;
149 }
150 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100151}