blob: 62ef305801d823e6ee19c43812e2dde5291465d0 [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -07001/*
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.dialer;
18
19import android.content.res.Resources;
Chiao Cheng94b10b52012-08-17 16:59:12 -070020import android.provider.ContactsContract.CommonDataKinds.Phone;
Chiao Cheng94b10b52012-08-17 16:59:12 -070021import android.text.TextUtils;
22import android.text.format.DateUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070023import android.view.View;
24import android.widget.TextView;
25
Yorke Leee38e9ab2014-05-28 17:47:33 -070026import com.android.contacts.common.testing.NeededForTesting;
Yorke Lee37d29852013-11-19 14:11:24 -080027import com.android.contacts.common.util.PhoneNumberHelper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070028import com.android.dialer.calllog.CallTypeHelper;
Yorke Lee034a2b32013-08-26 15:46:10 -070029import com.android.dialer.calllog.ContactInfo;
Yorke Lee24ec3192013-11-19 13:52:45 -080030import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070031import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Nancy Chen87ba4892014-06-11 17:56:07 -070032
Yorke Lee97f5a1d2014-05-27 18:04:58 -070033import com.google.common.collect.Lists;
Tyler Gunn146a4cd2014-05-05 16:51:42 -070034
35import java.util.ArrayList;
Chiao Cheng94b10b52012-08-17 16:59:12 -070036
37/**
38 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
39 */
40public class PhoneCallDetailsHelper {
41 /** The maximum number of icons will be shown to represent the call types in a group. */
42 private static final int MAX_CALL_TYPE_ICONS = 3;
43
44 private final Resources mResources;
45 /** The injected current time in milliseconds since the epoch. Used only by tests. */
46 private Long mCurrentTimeMillisForTest;
47 // Helper classes.
48 private final CallTypeHelper mCallTypeHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -080049 private final PhoneNumberDisplayHelper mPhoneNumberHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070050 private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070051
52 /**
Tyler Gunn146a4cd2014-05-05 16:51:42 -070053 * List of items to be concatenated together for accessibility descriptions
54 */
55 private ArrayList<CharSequence> mDescriptionItems = Lists.newArrayList();
56
57 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -070058 * Creates a new instance of the helper.
59 * <p>
60 * Generally you should have a single instance of this helper in any context.
61 *
62 * @param resources used to look up strings
63 */
64 public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper,
Chiao Chengfb0a9342013-09-13 17:27:42 -070065 PhoneNumberUtilsWrapper phoneUtils) {
Chiao Cheng94b10b52012-08-17 16:59:12 -070066 mResources = resources;
67 mCallTypeHelper = callTypeHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070068 mPhoneNumberUtilsWrapper = phoneUtils;
Yorke Lee36717252013-11-20 15:02:49 -080069 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mPhoneNumberUtilsWrapper, resources);
Chiao Cheng94b10b52012-08-17 16:59:12 -070070 }
71
72 /** Fills the call details views with content. */
Tyler Gunnd0715ac2014-05-12 10:52:32 -070073 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details) {
Chiao Cheng94b10b52012-08-17 16:59:12 -070074 // Display up to a given number of icons.
75 views.callTypeIcons.clear();
76 int count = details.callTypes.length;
77 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
78 views.callTypeIcons.add(details.callTypes[index]);
79 }
Yorke Lee7e8ea192013-09-04 17:36:07 -070080 views.callTypeIcons.requestLayout();
Chiao Cheng94b10b52012-08-17 16:59:12 -070081 views.callTypeIcons.setVisibility(View.VISIBLE);
82
83 // Show the total call count only if there are more than the maximum number of icons.
84 final Integer callCount;
85 if (count > MAX_CALL_TYPE_ICONS) {
86 callCount = count;
87 } else {
88 callCount = null;
89 }
Chiao Cheng94b10b52012-08-17 16:59:12 -070090
Tyler Gunn146a4cd2014-05-05 16:51:42 -070091 CharSequence callLocationAndDate = getCallLocationAndDate(details);
Chiao Cheng94b10b52012-08-17 16:59:12 -070092
Tyler Gunn146a4cd2014-05-05 16:51:42 -070093 // Set the call count, location and date.
Tyler Gunnd0715ac2014-05-12 10:52:32 -070094 setCallCountAndDate(views, callCount, callLocationAndDate);
Chiao Cheng94b10b52012-08-17 16:59:12 -070095
Ihab Awad1d1bd0d2014-06-30 21:24:01 -070096 // set the account icon if it exists
97 if (details.accountIcon != null) {
98 views.callAccountIcon.setVisibility(View.VISIBLE);
99 views.callAccountIcon.setImageDrawable(details.accountIcon);
Nancy Chen87ba4892014-06-11 17:56:07 -0700100 } else {
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700101 views.callAccountIcon.setVisibility(View.GONE);
Nancy Chen87ba4892014-06-11 17:56:07 -0700102 }
103
Chiao Cheng94b10b52012-08-17 16:59:12 -0700104 final CharSequence nameText;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700105 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700106 mPhoneNumberHelper.getDisplayNumber(details.number,
107 details.numberPresentation, details.formattedNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700108 if (TextUtils.isEmpty(details.name)) {
109 nameText = displayNumber;
Fabrice Di Meglioc341db02013-04-03 21:11:37 -0700110 // We have a real phone number as "nameView" so make it always LTR
111 views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700112 } else {
113 nameText = details.name;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700114 }
115
116 views.nameView.setText(nameText);
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700117
118 // TODO: At the current time the voicemail transcription is not supported. This view
119 // is kept for future expansion when we may wish to show a transcription of voicemail.
120 views.voicemailTranscriptionView.setText("");
121 views.voicemailTranscriptionView.setVisibility(View.GONE);
122 }
123
124 /**
125 * Builds a string containing the call location and date.
126 *
127 * @param details The call details.
128 * @return The call location and date string.
129 */
130 private CharSequence getCallLocationAndDate(PhoneCallDetails details) {
131 mDescriptionItems.clear();
132
133 // Get type of call (ie mobile, home, etc) if known, or the caller's location.
134 CharSequence callTypeOrLocation = getCallTypeOrLocation(details);
135
136 // Only add the call type or location if its not empty. It will be empty for unknown
137 // callers.
138 if (!TextUtils.isEmpty(callTypeOrLocation)) {
139 mDescriptionItems.add(callTypeOrLocation);
140 }
141 // The date of this call, relative to the current time.
142 mDescriptionItems.add(getCallDate(details));
143
144 // Create a comma separated list from the call type or location, and call date.
Yorke Lee990b6fc2014-06-12 15:57:55 -0700145 return TextUtils.join(", " , mDescriptionItems);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700146 }
147
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800148 /**
149 * For a call, if there is an associated contact for the caller, return the known call type
150 * (e.g. mobile, home, work). If there is no associated contact, attempt to use the caller's
151 * location if known.
152 * @param details Call details to use.
153 * @return Type of call (mobile/home) if known, or the location of the caller (if known).
154 */
155 public CharSequence getCallTypeOrLocation(PhoneCallDetails details) {
156 CharSequence numberFormattedLabel = null;
157 // Only show a label if the number is shown and it is not a SIP address.
158 if (!TextUtils.isEmpty(details.number)
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700159 && !PhoneNumberHelper.isUriNumber(details.number.toString())
160 && !mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) {
161
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800162 if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) {
163 numberFormattedLabel = details.geocode;
164 } else {
165 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
166 details.numberLabel);
167 }
168 }
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700169
170 if (!TextUtils.isEmpty(details.name) && TextUtils.isEmpty(numberFormattedLabel)) {
171 numberFormattedLabel = mPhoneNumberHelper.getDisplayNumber(details.number,
172 details.numberPresentation, details.formattedNumber);
173 }
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800174 return numberFormattedLabel;
175 }
176
177 /**
178 * Get the call date/time of the call, relative to the current time.
179 * e.g. 3 minutes ago
180 * @param details Call details to use.
181 * @return String representing when the call occurred.
182 */
183 public CharSequence getCallDate(PhoneCallDetails details) {
184 return DateUtils.getRelativeTimeSpanString(details.date,
185 getCurrentTimeMillis(),
186 DateUtils.MINUTE_IN_MILLIS,
187 DateUtils.FORMAT_ABBREV_RELATIVE);
188 }
189
Chiao Cheng94b10b52012-08-17 16:59:12 -0700190 /** Sets the text of the header view for the details page of a phone call. */
Yorke Lee7d20f822014-06-19 17:09:33 -0700191 @NeededForTesting
Chiao Cheng94b10b52012-08-17 16:59:12 -0700192 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
193 final CharSequence nameText;
194 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700195 mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700196 mResources.getString(R.string.recentCalls_addToContact));
197 if (TextUtils.isEmpty(details.name)) {
198 nameText = displayNumber;
199 } else {
200 nameText = details.name;
201 }
202
203 nameView.setText(nameText);
204 }
205
206 @NeededForTesting
207 public void setCurrentTimeForTest(long currentTimeMillis) {
208 mCurrentTimeMillisForTest = currentTimeMillis;
209 }
210
211 /**
212 * Returns the current time in milliseconds since the epoch.
213 * <p>
214 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
215 */
216 private long getCurrentTimeMillis() {
217 if (mCurrentTimeMillisForTest == null) {
218 return System.currentTimeMillis();
219 } else {
220 return mCurrentTimeMillisForTest;
221 }
222 }
223
224 /** Sets the call count and date. */
225 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
Tyler Gunnd0715ac2014-05-12 10:52:32 -0700226 CharSequence dateText) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700227 // Combine the count (if present) and the date.
228 final CharSequence text;
229 if (callCount != null) {
230 text = mResources.getString(
231 R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
232 } else {
233 text = dateText;
234 }
235
Tyler Gunnd0715ac2014-05-12 10:52:32 -0700236 views.callLocationAndDate.setText(text);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700237 }
238}