blob: 2a24557f14cb10ed94139c7ff4bd3975fe619d37 [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;
Tyler Gunn8b0e8582014-07-10 12:28:43 -070020import android.provider.CallLog;
21import android.provider.CallLog.Calls;
Chiao Cheng94b10b52012-08-17 16:59:12 -070022import android.provider.ContactsContract.CommonDataKinds.Phone;
Chiao Cheng94b10b52012-08-17 16:59:12 -070023import android.text.TextUtils;
24import android.text.format.DateUtils;
Chiao Cheng94b10b52012-08-17 16:59:12 -070025import android.view.View;
26import android.widget.TextView;
27
Yorke Leee38e9ab2014-05-28 17:47:33 -070028import com.android.contacts.common.testing.NeededForTesting;
Yorke Lee37d29852013-11-19 14:11:24 -080029import com.android.contacts.common.util.PhoneNumberHelper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070030import com.android.dialer.calllog.CallTypeHelper;
Yorke Lee034a2b32013-08-26 15:46:10 -070031import com.android.dialer.calllog.ContactInfo;
Yorke Lee24ec3192013-11-19 13:52:45 -080032import com.android.dialer.calllog.PhoneNumberDisplayHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070033import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
Tyler Gunn8b0e8582014-07-10 12:28:43 -070034import com.android.dialer.util.DialerUtils;
Nancy Chen87ba4892014-06-11 17:56:07 -070035
Yorke Lee97f5a1d2014-05-27 18:04:58 -070036import com.google.common.collect.Lists;
Tyler Gunn146a4cd2014-05-05 16:51:42 -070037
38import java.util.ArrayList;
Chiao Cheng94b10b52012-08-17 16:59:12 -070039
40/**
41 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
42 */
43public class PhoneCallDetailsHelper {
44 /** The maximum number of icons will be shown to represent the call types in a group. */
45 private static final int MAX_CALL_TYPE_ICONS = 3;
46
47 private final Resources mResources;
48 /** The injected current time in milliseconds since the epoch. Used only by tests. */
49 private Long mCurrentTimeMillisForTest;
50 // Helper classes.
51 private final CallTypeHelper mCallTypeHelper;
Yorke Lee24ec3192013-11-19 13:52:45 -080052 private final PhoneNumberDisplayHelper mPhoneNumberHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070053 private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper;
Chiao Cheng94b10b52012-08-17 16:59:12 -070054
55 /**
Tyler Gunn146a4cd2014-05-05 16:51:42 -070056 * List of items to be concatenated together for accessibility descriptions
57 */
58 private ArrayList<CharSequence> mDescriptionItems = Lists.newArrayList();
59
60 /**
Chiao Cheng94b10b52012-08-17 16:59:12 -070061 * Creates a new instance of the helper.
62 * <p>
63 * Generally you should have a single instance of this helper in any context.
64 *
65 * @param resources used to look up strings
66 */
67 public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper,
Chiao Chengfb0a9342013-09-13 17:27:42 -070068 PhoneNumberUtilsWrapper phoneUtils) {
Chiao Cheng94b10b52012-08-17 16:59:12 -070069 mResources = resources;
70 mCallTypeHelper = callTypeHelper;
Chiao Chengfb0a9342013-09-13 17:27:42 -070071 mPhoneNumberUtilsWrapper = phoneUtils;
Yorke Lee36717252013-11-20 15:02:49 -080072 mPhoneNumberHelper = new PhoneNumberDisplayHelper(mPhoneNumberUtilsWrapper, resources);
Chiao Cheng94b10b52012-08-17 16:59:12 -070073 }
74
75 /** Fills the call details views with content. */
Tyler Gunnd0715ac2014-05-12 10:52:32 -070076 public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details) {
Chiao Cheng94b10b52012-08-17 16:59:12 -070077 // Display up to a given number of icons.
78 views.callTypeIcons.clear();
79 int count = details.callTypes.length;
80 for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
81 views.callTypeIcons.add(details.callTypes[index]);
82 }
Tyler Gunn8b0e8582014-07-10 12:28:43 -070083
84 // Show the video icon if the call had video enabled.
85 views.callTypeIcons.setShowVideo(
86 (details.features & Calls.FEATURES_VIDEO) == Calls.FEATURES_VIDEO);
Yorke Lee7e8ea192013-09-04 17:36:07 -070087 views.callTypeIcons.requestLayout();
Chiao Cheng94b10b52012-08-17 16:59:12 -070088 views.callTypeIcons.setVisibility(View.VISIBLE);
89
90 // Show the total call count only if there are more than the maximum number of icons.
91 final Integer callCount;
92 if (count > MAX_CALL_TYPE_ICONS) {
93 callCount = count;
94 } else {
95 callCount = null;
96 }
Chiao Cheng94b10b52012-08-17 16:59:12 -070097
Tyler Gunn146a4cd2014-05-05 16:51:42 -070098 CharSequence callLocationAndDate = getCallLocationAndDate(details);
Chiao Cheng94b10b52012-08-17 16:59:12 -070099
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700100 // Set the call count, location and date.
Tyler Gunnd0715ac2014-05-12 10:52:32 -0700101 setCallCountAndDate(views, callCount, callLocationAndDate);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700102
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700103 // set the account icon if it exists
104 if (details.accountIcon != null) {
105 views.callAccountIcon.setVisibility(View.VISIBLE);
106 views.callAccountIcon.setImageDrawable(details.accountIcon);
Nancy Chen87ba4892014-06-11 17:56:07 -0700107 } else {
Ihab Awad1d1bd0d2014-06-30 21:24:01 -0700108 views.callAccountIcon.setVisibility(View.GONE);
Nancy Chen87ba4892014-06-11 17:56:07 -0700109 }
110
Chiao Cheng94b10b52012-08-17 16:59:12 -0700111 final CharSequence nameText;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700112 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700113 mPhoneNumberHelper.getDisplayNumber(details.number,
114 details.numberPresentation, details.formattedNumber);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700115 if (TextUtils.isEmpty(details.name)) {
116 nameText = displayNumber;
Fabrice Di Meglioc341db02013-04-03 21:11:37 -0700117 // We have a real phone number as "nameView" so make it always LTR
118 views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700119 } else {
120 nameText = details.name;
Chiao Cheng94b10b52012-08-17 16:59:12 -0700121 }
122
123 views.nameView.setText(nameText);
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700124
125 // TODO: At the current time the voicemail transcription is not supported. This view
126 // is kept for future expansion when we may wish to show a transcription of voicemail.
127 views.voicemailTranscriptionView.setText("");
128 views.voicemailTranscriptionView.setVisibility(View.GONE);
129 }
130
131 /**
132 * Builds a string containing the call location and date.
133 *
134 * @param details The call details.
135 * @return The call location and date string.
136 */
137 private CharSequence getCallLocationAndDate(PhoneCallDetails details) {
138 mDescriptionItems.clear();
139
140 // Get type of call (ie mobile, home, etc) if known, or the caller's location.
141 CharSequence callTypeOrLocation = getCallTypeOrLocation(details);
142
143 // Only add the call type or location if its not empty. It will be empty for unknown
144 // callers.
145 if (!TextUtils.isEmpty(callTypeOrLocation)) {
146 mDescriptionItems.add(callTypeOrLocation);
147 }
148 // The date of this call, relative to the current time.
149 mDescriptionItems.add(getCallDate(details));
150
151 // Create a comma separated list from the call type or location, and call date.
Tyler Gunn8b0e8582014-07-10 12:28:43 -0700152 return DialerUtils.join(mResources, mDescriptionItems);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700153 }
154
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800155 /**
156 * For a call, if there is an associated contact for the caller, return the known call type
157 * (e.g. mobile, home, work). If there is no associated contact, attempt to use the caller's
158 * location if known.
159 * @param details Call details to use.
160 * @return Type of call (mobile/home) if known, or the location of the caller (if known).
161 */
162 public CharSequence getCallTypeOrLocation(PhoneCallDetails details) {
163 CharSequence numberFormattedLabel = null;
164 // Only show a label if the number is shown and it is not a SIP address.
165 if (!TextUtils.isEmpty(details.number)
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700166 && !PhoneNumberHelper.isUriNumber(details.number.toString())
167 && !mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) {
168
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800169 if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) {
170 numberFormattedLabel = details.geocode;
171 } else {
172 numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
173 details.numberLabel);
174 }
175 }
Tyler Gunn146a4cd2014-05-05 16:51:42 -0700176
177 if (!TextUtils.isEmpty(details.name) && TextUtils.isEmpty(numberFormattedLabel)) {
178 numberFormattedLabel = mPhoneNumberHelper.getDisplayNumber(details.number,
179 details.numberPresentation, details.formattedNumber);
180 }
Tyler Gunn45ed3b52014-02-03 08:31:10 -0800181 return numberFormattedLabel;
182 }
183
184 /**
185 * Get the call date/time of the call, relative to the current time.
186 * e.g. 3 minutes ago
187 * @param details Call details to use.
188 * @return String representing when the call occurred.
189 */
190 public CharSequence getCallDate(PhoneCallDetails details) {
191 return DateUtils.getRelativeTimeSpanString(details.date,
192 getCurrentTimeMillis(),
193 DateUtils.MINUTE_IN_MILLIS,
194 DateUtils.FORMAT_ABBREV_RELATIVE);
195 }
196
Chiao Cheng94b10b52012-08-17 16:59:12 -0700197 /** Sets the text of the header view for the details page of a phone call. */
Yorke Lee7d20f822014-06-19 17:09:33 -0700198 @NeededForTesting
Chiao Cheng94b10b52012-08-17 16:59:12 -0700199 public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
200 final CharSequence nameText;
201 final CharSequence displayNumber =
Jay Shrauner719a7ad2013-05-30 15:41:13 -0700202 mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation,
Chiao Cheng94b10b52012-08-17 16:59:12 -0700203 mResources.getString(R.string.recentCalls_addToContact));
204 if (TextUtils.isEmpty(details.name)) {
205 nameText = displayNumber;
206 } else {
207 nameText = details.name;
208 }
209
210 nameView.setText(nameText);
211 }
212
213 @NeededForTesting
214 public void setCurrentTimeForTest(long currentTimeMillis) {
215 mCurrentTimeMillisForTest = currentTimeMillis;
216 }
217
218 /**
219 * Returns the current time in milliseconds since the epoch.
220 * <p>
221 * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
222 */
223 private long getCurrentTimeMillis() {
224 if (mCurrentTimeMillisForTest == null) {
225 return System.currentTimeMillis();
226 } else {
227 return mCurrentTimeMillisForTest;
228 }
229 }
230
231 /** Sets the call count and date. */
232 private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
Tyler Gunnd0715ac2014-05-12 10:52:32 -0700233 CharSequence dateText) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700234 // Combine the count (if present) and the date.
235 final CharSequence text;
236 if (callCount != null) {
237 text = mResources.getString(
238 R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
239 } else {
240 text = dateText;
241 }
242
Tyler Gunnd0715ac2014-05-12 10:52:32 -0700243 views.callLocationAndDate.setText(text);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700244 }
245}