PhoneCallDetailsHelper.java revision 719a7adde25e0a717816b00668c16c3a1e3c5518
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.telephony.PhoneNumberUtils;
23import android.text.SpannableString;
24import android.text.Spanned;
25import android.text.TextUtils;
26import android.text.format.DateUtils;
27import android.text.style.ForegroundColorSpan;
28import android.text.style.StyleSpan;
29import android.view.View;
30import android.widget.TextView;
31
32import com.android.contacts.common.test.NeededForTesting;
33import com.android.dialer.calllog.CallTypeHelper;
34import com.android.dialer.calllog.PhoneNumberHelper;
35
36/**
37 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
38 */
39public class PhoneCallDetailsHelper {
40    /** The maximum number of icons will be shown to represent the call types in a group. */
41    private static final int MAX_CALL_TYPE_ICONS = 3;
42
43    private final Resources mResources;
44    /** The injected current time in milliseconds since the epoch. Used only by tests. */
45    private Long mCurrentTimeMillisForTest;
46    // Helper classes.
47    private final CallTypeHelper mCallTypeHelper;
48    private final PhoneNumberHelper mPhoneNumberHelper;
49
50    /**
51     * Creates a new instance of the helper.
52     * <p>
53     * Generally you should have a single instance of this helper in any context.
54     *
55     * @param resources used to look up strings
56     */
57    public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper,
58            PhoneNumberHelper phoneNumberHelper) {
59        mResources = resources;
60        mCallTypeHelper = callTypeHelper;
61        mPhoneNumberHelper = phoneNumberHelper;
62    }
63
64    /** Fills the call details views with content. */
65    public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details,
66            boolean isHighlighted) {
67        // Display up to a given number of icons.
68        views.callTypeIcons.clear();
69        int count = details.callTypes.length;
70        for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
71            views.callTypeIcons.add(details.callTypes[index]);
72        }
73        views.callTypeIcons.setVisibility(View.VISIBLE);
74
75        // Show the total call count only if there are more than the maximum number of icons.
76        final Integer callCount;
77        if (count > MAX_CALL_TYPE_ICONS) {
78            callCount = count;
79        } else {
80            callCount = null;
81        }
82        // The color to highlight the count and date in, if any. This is based on the first call.
83        Integer highlightColor =
84                isHighlighted ? mCallTypeHelper.getHighlightedColor(details.callTypes[0]) : null;
85
86        // The date of this call, relative to the current time.
87        CharSequence dateText =
88            DateUtils.getRelativeTimeSpanString(details.date,
89                    getCurrentTimeMillis(),
90                    DateUtils.MINUTE_IN_MILLIS,
91                    DateUtils.FORMAT_ABBREV_RELATIVE);
92
93        // Set the call count and date.
94        setCallCountAndDate(views, callCount, dateText, highlightColor);
95
96        CharSequence numberFormattedLabel = null;
97        // Only show a label if the number is shown and it is not a SIP address.
98        if (!TextUtils.isEmpty(details.number)
99                && !PhoneNumberUtils.isUriNumber(details.number.toString())) {
100            numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType,
101                    details.numberLabel);
102        }
103
104        final CharSequence nameText;
105        final CharSequence numberText;
106        final CharSequence labelText;
107        final CharSequence displayNumber =
108            mPhoneNumberHelper.getDisplayNumber(details.number,
109                    details.numberPresentation, details.formattedNumber);
110        if (TextUtils.isEmpty(details.name)) {
111            nameText = displayNumber;
112            if (TextUtils.isEmpty(details.geocode)
113                    || mPhoneNumberHelper.isVoicemailNumber(details.number)) {
114                numberText = mResources.getString(R.string.call_log_empty_gecode);
115            } else {
116                numberText = details.geocode;
117            }
118            labelText = null;
119            // We have a real phone number as "nameView" so make it always LTR
120            views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
121        } else {
122            nameText = details.name;
123            numberText = displayNumber;
124            labelText = numberFormattedLabel;
125            // We have a real phone number as "numberView" so make it always LTR
126            views.numberView.setTextDirection(View.TEXT_DIRECTION_LTR);
127        }
128
129        views.nameView.setText(nameText);
130        views.numberView.setText(numberText);
131        views.labelView.setText(labelText);
132        views.labelView.setVisibility(TextUtils.isEmpty(labelText) ? View.GONE : View.VISIBLE);
133    }
134
135    /** Sets the text of the header view for the details page of a phone call. */
136    public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
137        final CharSequence nameText;
138        final CharSequence displayNumber =
139            mPhoneNumberHelper.getDisplayNumber(details.number, details.numberPresentation,
140                        mResources.getString(R.string.recentCalls_addToContact));
141        if (TextUtils.isEmpty(details.name)) {
142            nameText = displayNumber;
143        } else {
144            nameText = details.name;
145        }
146
147        nameView.setText(nameText);
148    }
149
150    @NeededForTesting
151    public void setCurrentTimeForTest(long currentTimeMillis) {
152        mCurrentTimeMillisForTest = currentTimeMillis;
153    }
154
155    /**
156     * Returns the current time in milliseconds since the epoch.
157     * <p>
158     * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
159     */
160    private long getCurrentTimeMillis() {
161        if (mCurrentTimeMillisForTest == null) {
162            return System.currentTimeMillis();
163        } else {
164            return mCurrentTimeMillisForTest;
165        }
166    }
167
168    /** Sets the call count and date. */
169    private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
170            CharSequence dateText, Integer highlightColor) {
171        // Combine the count (if present) and the date.
172        final CharSequence text;
173        if (callCount != null) {
174            text = mResources.getString(
175                    R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
176        } else {
177            text = dateText;
178        }
179
180        // Apply the highlight color if present.
181        final CharSequence formattedText;
182        if (highlightColor != null) {
183            formattedText = addBoldAndColor(text, highlightColor);
184        } else {
185            formattedText = text;
186        }
187
188        views.callTypeAndDate.setText(formattedText);
189    }
190
191    /** Creates a SpannableString for the given text which is bold and in the given color. */
192    private CharSequence addBoldAndColor(CharSequence text, int color) {
193        int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE;
194        SpannableString result = new SpannableString(text);
195        result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags);
196        result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags);
197        return result;
198    }
199}
200