PhoneCallDetailsHelper.java revision 8b0e858d5b4c50813dbe2b5c244e7013814b23ec
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.provider.CallLog;
21import android.provider.CallLog.Calls;
22import android.provider.ContactsContract.CommonDataKinds.Phone;
23import android.text.TextUtils;
24import android.text.format.DateUtils;
25import android.view.View;
26import android.widget.TextView;
27
28import com.android.contacts.common.testing.NeededForTesting;
29import com.android.contacts.common.util.PhoneNumberHelper;
30import com.android.dialer.calllog.CallTypeHelper;
31import com.android.dialer.calllog.ContactInfo;
32import com.android.dialer.calllog.PhoneNumberDisplayHelper;
33import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
34import com.android.dialer.util.DialerUtils;
35
36import com.google.common.collect.Lists;
37
38import java.util.ArrayList;
39
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;
52    private final PhoneNumberDisplayHelper mPhoneNumberHelper;
53    private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper;
54
55    /**
56     * List of items to be concatenated together for accessibility descriptions
57     */
58    private ArrayList<CharSequence> mDescriptionItems = Lists.newArrayList();
59
60    /**
61     * 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,
68            PhoneNumberUtilsWrapper phoneUtils) {
69        mResources = resources;
70        mCallTypeHelper = callTypeHelper;
71        mPhoneNumberUtilsWrapper = phoneUtils;
72        mPhoneNumberHelper = new PhoneNumberDisplayHelper(mPhoneNumberUtilsWrapper, resources);
73    }
74
75    /** Fills the call details views with content. */
76    public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details) {
77        // 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        }
83
84        // Show the video icon if the call had video enabled.
85        views.callTypeIcons.setShowVideo(
86                (details.features & Calls.FEATURES_VIDEO) == Calls.FEATURES_VIDEO);
87        views.callTypeIcons.requestLayout();
88        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        }
97
98        CharSequence callLocationAndDate = getCallLocationAndDate(details);
99
100        // Set the call count, location and date.
101        setCallCountAndDate(views, callCount, callLocationAndDate);
102
103        // set the account icon if it exists
104        if (details.accountIcon != null) {
105            views.callAccountIcon.setVisibility(View.VISIBLE);
106            views.callAccountIcon.setImageDrawable(details.accountIcon);
107        } else {
108            views.callAccountIcon.setVisibility(View.GONE);
109        }
110
111        final CharSequence nameText;
112        final CharSequence displayNumber =
113            mPhoneNumberHelper.getDisplayNumber(details.number,
114                    details.numberPresentation, details.formattedNumber);
115        if (TextUtils.isEmpty(details.name)) {
116            nameText = displayNumber;
117            // We have a real phone number as "nameView" so make it always LTR
118            views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
119        } else {
120            nameText = details.name;
121        }
122
123        views.nameView.setText(nameText);
124
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.
152        return DialerUtils.join(mResources, mDescriptionItems);
153    }
154
155    /**
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)
166                && !PhoneNumberHelper.isUriNumber(details.number.toString())
167                && !mPhoneNumberUtilsWrapper.isVoicemailNumber(details.number)) {
168
169            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        }
176
177        if (!TextUtils.isEmpty(details.name) && TextUtils.isEmpty(numberFormattedLabel)) {
178            numberFormattedLabel = mPhoneNumberHelper.getDisplayNumber(details.number,
179                    details.numberPresentation, details.formattedNumber);
180        }
181        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
197    /** Sets the text of the header view for the details page of a phone call. */
198    @NeededForTesting
199    public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
200        final CharSequence nameText;
201        final CharSequence displayNumber =
202            mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation,
203                        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,
233            CharSequence dateText) {
234        // 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
243        views.callLocationAndDate.setText(text);
244    }
245}
246