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