PhoneCallDetailsHelper.java revision 990b6fc17da04b59c909042aea883d8c827eb92d
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.testing.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.common.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        // Display up to a given number of icons.
80        views.callTypeIcons.clear();
81        int count = details.callTypes.length;
82        for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
83            views.callTypeIcons.add(details.callTypes[index]);
84        }
85        views.callTypeIcons.requestLayout();
86        views.callTypeIcons.setVisibility(View.VISIBLE);
87
88        // Show the total call count only if there are more than the maximum number of icons.
89        final Integer callCount;
90        if (count > MAX_CALL_TYPE_ICONS) {
91            callCount = count;
92        } else {
93            callCount = null;
94        }
95
96        CharSequence callLocationAndDate = getCallLocationAndDate(details);
97
98        // Set the call count, location and date.
99        setCallCountAndDate(views, callCount, callLocationAndDate);
100
101        final CharSequence nameText;
102        final CharSequence displayNumber =
103            mPhoneNumberHelper.getDisplayNumber(details.number,
104                    details.numberPresentation, details.formattedNumber);
105        if (TextUtils.isEmpty(details.name)) {
106            nameText = displayNumber;
107            // We have a real phone number as "nameView" so make it always LTR
108            views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
109        } else {
110            nameText = details.name;
111        }
112
113        views.nameView.setText(nameText);
114
115        // TODO: At the current time the voicemail transcription is not supported.  This view
116        // is kept for future expansion when we may wish to show a transcription of voicemail.
117        views.voicemailTranscriptionView.setText("");
118        views.voicemailTranscriptionView.setVisibility(View.GONE);
119    }
120
121    /**
122     * Builds a string containing the call location and date.
123     *
124     * @param details The call details.
125     * @return The call location and date string.
126     */
127    private CharSequence getCallLocationAndDate(PhoneCallDetails details) {
128        mDescriptionItems.clear();
129
130        // Get type of call (ie mobile, home, etc) if known, or the caller's location.
131        CharSequence callTypeOrLocation = getCallTypeOrLocation(details);
132
133        // Only add the call type or location if its not empty.  It will be empty for unknown
134        // callers.
135        if (!TextUtils.isEmpty(callTypeOrLocation)) {
136            mDescriptionItems.add(callTypeOrLocation);
137        }
138        // The date of this call, relative to the current time.
139        mDescriptionItems.add(getCallDate(details));
140
141        // Create a comma separated list from the call type or location, and call date.
142        return TextUtils.join(", " , mDescriptionItems);
143    }
144
145    /**
146     * For a call, if there is an associated contact for the caller, return the known call type
147     * (e.g. mobile, home, work).  If there is no associated contact, attempt to use the caller's
148     * location if known.
149     * @param details Call details to use.
150     * @return Type of call (mobile/home) if known, or the location of the caller (if known).
151     */
152    public CharSequence getCallTypeOrLocation(PhoneCallDetails details) {
153        CharSequence numberFormattedLabel = null;
154        // Only show a label if the number is shown and it is not a SIP address.
155        if (!TextUtils.isEmpty(details.number)
156                && !PhoneNumberHelper.isUriNumber(details.number.toString())
157                && !mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) {
158
159            if (details.numberLabel == ContactInfo.GEOCODE_AS_LABEL) {
160                numberFormattedLabel = details.geocode;
161            } else {
162                numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
163                        details.numberLabel);
164            }
165        }
166
167        if (!TextUtils.isEmpty(details.name) && TextUtils.isEmpty(numberFormattedLabel)) {
168            numberFormattedLabel = mPhoneNumberHelper.getDisplayNumber(details.number,
169                    details.numberPresentation, details.formattedNumber);
170        }
171        return numberFormattedLabel;
172    }
173
174    /**
175     * Get the call date/time of the call, relative to the current time.
176     * e.g. 3 minutes ago
177     * @param details Call details to use.
178     * @return String representing when the call occurred.
179     */
180    public CharSequence getCallDate(PhoneCallDetails details) {
181        return DateUtils.getRelativeTimeSpanString(details.date,
182                getCurrentTimeMillis(),
183                DateUtils.MINUTE_IN_MILLIS,
184                DateUtils.FORMAT_ABBREV_RELATIVE);
185    }
186
187    /** Sets the text of the header view for the details page of a phone call. */
188    public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
189        final CharSequence nameText;
190        final CharSequence displayNumber =
191            mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation,
192                        mResources.getString(R.string.recentCalls_addToContact));
193        if (TextUtils.isEmpty(details.name)) {
194            nameText = displayNumber;
195        } else {
196            nameText = details.name;
197        }
198
199        nameView.setText(nameText);
200    }
201
202    @NeededForTesting
203    public void setCurrentTimeForTest(long currentTimeMillis) {
204        mCurrentTimeMillisForTest = currentTimeMillis;
205    }
206
207    /**
208     * Returns the current time in milliseconds since the epoch.
209     * <p>
210     * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
211     */
212    private long getCurrentTimeMillis() {
213        if (mCurrentTimeMillisForTest == null) {
214            return System.currentTimeMillis();
215        } else {
216            return mCurrentTimeMillisForTest;
217        }
218    }
219
220    /** Sets the call count and date. */
221    private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
222            CharSequence dateText) {
223        // Combine the count (if present) and the date.
224        final CharSequence text;
225        if (callCount != null) {
226            text = mResources.getString(
227                    R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
228        } else {
229            text = dateText;
230        }
231
232        views.callLocationAndDate.setText(text);
233    }
234}
235