PhoneCallDetailsHelper.java revision 146a4cdf57f0d4d0cd85e808f1df2bdea639b24c
1/*
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;
20import android.graphics.Typeface;
21import android.provider.ContactsContract.CommonDataKinds.Phone;
22import android.text.SpannableString;
23import android.text.Spanned;
24import android.text.TextUtils;
25import android.text.format.DateUtils;
26import android.text.style.ForegroundColorSpan;
27import android.text.style.StyleSpan;
28import android.view.View;
29import android.widget.TextView;
30
31import com.android.contacts.common.test.NeededForTesting;
32import com.android.contacts.common.util.PhoneNumberHelper;
33import com.android.dialer.calllog.CallTypeHelper;
34import com.android.dialer.calllog.ContactInfo;
35import com.android.dialer.calllog.PhoneNumberDisplayHelper;
36import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
37import com.google.android.collect.Lists;
38
39import java.util.ArrayList;
40import java.util.List;
41
42/**
43 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
44 */
45public class PhoneCallDetailsHelper {
46    /** The maximum number of icons will be shown to represent the call types in a group. */
47    private static final int MAX_CALL_TYPE_ICONS = 3;
48
49    private final Resources mResources;
50    /** The injected current time in milliseconds since the epoch. Used only by tests. */
51    private Long mCurrentTimeMillisForTest;
52    // Helper classes.
53    private final CallTypeHelper mCallTypeHelper;
54    private final PhoneNumberDisplayHelper mPhoneNumberHelper;
55    private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper;
56
57    /**
58     * List of items to be concatenated together for accessibility descriptions
59     */
60    private ArrayList<CharSequence> mDescriptionItems = Lists.newArrayList();
61
62    /**
63     * Creates a new instance of the helper.
64     * <p>
65     * Generally you should have a single instance of this helper in any context.
66     *
67     * @param resources used to look up strings
68     */
69    public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper,
70            PhoneNumberUtilsWrapper phoneUtils) {
71        mResources = resources;
72        mCallTypeHelper = callTypeHelper;
73        mPhoneNumberUtilsWrapper = phoneUtils;
74        mPhoneNumberHelper = new PhoneNumberDisplayHelper(mPhoneNumberUtilsWrapper, resources);
75    }
76
77    /** Fills the call details views with content. */
78    public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details,
79            boolean isHighlighted) {
80        // Display up to a given number of icons.
81        views.callTypeIcons.clear();
82        int count = details.callTypes.length;
83        for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
84            views.callTypeIcons.add(details.callTypes[index]);
85        }
86        views.callTypeIcons.requestLayout();
87        views.callTypeIcons.setVisibility(View.VISIBLE);
88
89        // Show the total call count only if there are more than the maximum number of icons.
90        final Integer callCount;
91        if (count > MAX_CALL_TYPE_ICONS) {
92            callCount = count;
93        } else {
94            callCount = null;
95        }
96        // The color to highlight the count and date in, if any. This is based on the first call.
97        Integer highlightColor =
98                isHighlighted ? mCallTypeHelper.getHighlightedColor(details.callTypes[0]) : null;
99
100        CharSequence callLocationAndDate = getCallLocationAndDate(details);
101
102        // Set the call count, location and date.
103        setCallCountAndDate(views, callCount, callLocationAndDate, highlightColor);
104
105        final CharSequence nameText;
106        final CharSequence displayNumber =
107            mPhoneNumberHelper.getDisplayNumber(details.number,
108                    details.numberPresentation, details.formattedNumber);
109        if (TextUtils.isEmpty(details.name)) {
110            nameText = displayNumber;
111            // We have a real phone number as "nameView" so make it always LTR
112            views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
113        } else {
114            nameText = details.name;
115        }
116
117        views.nameView.setText(nameText);
118
119        // TODO: At the current time the voicemail transcription is not supported.  This view
120        // is kept for future expansion when we may wish to show a transcription of voicemail.
121        views.voicemailTranscriptionView.setText("");
122        views.voicemailTranscriptionView.setVisibility(View.GONE);
123    }
124
125    /**
126     * Builds a string containing the call location and date.
127     *
128     * @param details The call details.
129     * @return The call location and date string.
130     */
131    private CharSequence getCallLocationAndDate(PhoneCallDetails details) {
132        mDescriptionItems.clear();
133
134        // Get type of call (ie mobile, home, etc) if known, or the caller's location.
135        CharSequence callTypeOrLocation = getCallTypeOrLocation(details);
136
137        // Only add the call type or location if its not empty.  It will be empty for unknown
138        // callers.
139        if (!TextUtils.isEmpty(callTypeOrLocation)) {
140            mDescriptionItems.add(callTypeOrLocation);
141        }
142        // The date of this call, relative to the current time.
143        mDescriptionItems.add(getCallDate(details));
144
145        // Create a comma separated list from the call type or location, and call date.
146        // TextUtils.join ensures a locale appropriate list separator is used.
147        return TextUtils.join((List<CharSequence>)mDescriptionItems);
148    }
149
150    /**
151     * For a call, if there is an associated contact for the caller, return the known call type
152     * (e.g. mobile, home, work).  If there is no associated contact, attempt to use the caller's
153     * location if known.
154     * @param details Call details to use.
155     * @return Type of call (mobile/home) if known, or the location of the caller (if known).
156     */
157    public CharSequence getCallTypeOrLocation(PhoneCallDetails details) {
158        CharSequence numberFormattedLabel = null;
159        // Only show a label if the number is shown and it is not a SIP address.
160        if (!TextUtils.isEmpty(details.number)
161                && !PhoneNumberHelper.isUriNumber(details.number.toString())
162                && !mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) {
163
164            if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) {
165                numberFormattedLabel = details.geocode;
166            } else {
167                numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
168                        details.numberLabel);
169            }
170        }
171
172        if (!TextUtils.isEmpty(details.name) && TextUtils.isEmpty(numberFormattedLabel)) {
173            numberFormattedLabel = mPhoneNumberHelper.getDisplayNumber(details.number,
174                    details.numberPresentation, details.formattedNumber);
175        }
176        return numberFormattedLabel;
177    }
178
179    /**
180     * Get the call date/time of the call, relative to the current time.
181     * e.g. 3 minutes ago
182     * @param details Call details to use.
183     * @return String representing when the call occurred.
184     */
185    public CharSequence getCallDate(PhoneCallDetails details) {
186        return DateUtils.getRelativeTimeSpanString(details.date,
187                getCurrentTimeMillis(),
188                DateUtils.MINUTE_IN_MILLIS,
189                DateUtils.FORMAT_ABBREV_RELATIVE);
190    }
191
192    /** Sets the text of the header view for the details page of a phone call. */
193    public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
194        final CharSequence nameText;
195        final CharSequence displayNumber =
196            mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation,
197                        mResources.getString(R.string.recentCalls_addToContact));
198        if (TextUtils.isEmpty(details.name)) {
199            nameText = displayNumber;
200        } else {
201            nameText = details.name;
202        }
203
204        nameView.setText(nameText);
205    }
206
207    @NeededForTesting
208    public void setCurrentTimeForTest(long currentTimeMillis) {
209        mCurrentTimeMillisForTest = currentTimeMillis;
210    }
211
212    /**
213     * Returns the current time in milliseconds since the epoch.
214     * <p>
215     * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
216     */
217    private long getCurrentTimeMillis() {
218        if (mCurrentTimeMillisForTest == null) {
219            return System.currentTimeMillis();
220        } else {
221            return mCurrentTimeMillisForTest;
222        }
223    }
224
225    /** Sets the call count and date. */
226    private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
227            CharSequence dateText, Integer highlightColor) {
228        // Combine the count (if present) and the date.
229        final CharSequence text;
230        if (callCount != null) {
231            text = mResources.getString(
232                    R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
233        } else {
234            text = dateText;
235        }
236
237        // Apply the highlight color if present.
238        final CharSequence formattedText;
239        if (highlightColor != null) {
240            formattedText = addBoldAndColor(text, highlightColor);
241        } else {
242            formattedText = text;
243        }
244
245        views.callLocationAndDate.setText(formattedText);
246    }
247
248    /** Creates a SpannableString for the given text which is bold and in the given color. */
249    private CharSequence addBoldAndColor(CharSequence text, int color) {
250        int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE;
251        SpannableString result = new SpannableString(text);
252        result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags);
253        result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags);
254        return result;
255    }
256}
257