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.calllog;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.graphics.Typeface;
22import android.graphics.drawable.Drawable;
23import android.provider.CallLog;
24import android.provider.CallLog.Calls;
25import android.provider.ContactsContract.CommonDataKinds.Phone;
26import android.telecom.PhoneAccount;
27import android.text.TextUtils;
28import android.text.format.DateUtils;
29import android.view.View;
30import android.widget.TextView;
31
32import com.android.contacts.common.testing.NeededForTesting;
33import com.android.contacts.common.util.PhoneNumberHelper;
34import com.android.dialer.PhoneCallDetails;
35import com.android.dialer.R;
36import com.android.dialer.util.DialerUtils;
37import com.android.dialer.util.PhoneNumberUtil;
38
39import com.google.common.collect.Lists;
40
41import java.util.ArrayList;
42
43/**
44 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
45 */
46public class PhoneCallDetailsHelper {
47    /** The maximum number of icons will be shown to represent the call types in a group. */
48    private static final int MAX_CALL_TYPE_ICONS = 3;
49
50    private final Context mContext;
51    private final Resources mResources;
52    /** The injected current time in milliseconds since the epoch. Used only by tests. */
53    private Long mCurrentTimeMillisForTest;
54    private final TelecomCallLogCache mTelecomCallLogCache;
55
56    /**
57     * List of items to be concatenated together for accessibility descriptions
58     */
59    private ArrayList<CharSequence> mDescriptionItems = Lists.newArrayList();
60
61    /**
62     * Creates a new instance of the helper.
63     * <p>
64     * Generally you should have a single instance of this helper in any context.
65     *
66     * @param resources used to look up strings
67     */
68    public PhoneCallDetailsHelper(
69            Context context,
70            Resources resources,
71            TelecomCallLogCache telecomCallLogCache) {
72        mContext = context;
73        mResources = resources;
74        mTelecomCallLogCache = telecomCallLogCache;
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        boolean isVoicemail = false;
83        for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) {
84            views.callTypeIcons.add(details.callTypes[index]);
85            if (index == 0) {
86                isVoicemail = details.callTypes[index] == Calls.VOICEMAIL_TYPE;
87            }
88        }
89
90        // Show the video icon if the call had video enabled.
91        views.callTypeIcons.setShowVideo(
92                (details.features & Calls.FEATURES_VIDEO) == Calls.FEATURES_VIDEO);
93        views.callTypeIcons.requestLayout();
94        views.callTypeIcons.setVisibility(View.VISIBLE);
95
96        // Show the total call count only if there are more than the maximum number of icons.
97        final Integer callCount;
98        if (count > MAX_CALL_TYPE_ICONS) {
99            callCount = count;
100        } else {
101            callCount = null;
102        }
103
104        CharSequence callLocationAndDate = getCallLocationAndDate(details);
105
106        // Set the call count, location and date.
107        setCallCountAndDate(views, callCount, callLocationAndDate);
108
109        // Set the account label if it exists.
110        String accountLabel = mTelecomCallLogCache.getAccountLabel(details.accountHandle);
111
112        if (accountLabel != null) {
113            views.callAccountLabel.setVisibility(View.VISIBLE);
114            views.callAccountLabel.setText(accountLabel);
115            int color = PhoneAccountUtils.getAccountColor(mContext, details.accountHandle);
116            if (color == PhoneAccount.NO_HIGHLIGHT_COLOR) {
117                int defaultColor = R.color.dialtacts_secondary_text_color;
118                views.callAccountLabel.setTextColor(mContext.getResources().getColor(defaultColor));
119            } else {
120                views.callAccountLabel.setTextColor(color);
121            }
122        } else {
123            views.callAccountLabel.setVisibility(View.GONE);
124        }
125
126        final CharSequence nameText;
127        final CharSequence displayNumber = details.displayNumber;
128        if (TextUtils.isEmpty(details.name)) {
129            nameText = displayNumber;
130            // We have a real phone number as "nameView" so make it always LTR
131            views.nameView.setTextDirection(View.TEXT_DIRECTION_LTR);
132        } else {
133            nameText = details.name;
134        }
135
136        views.nameView.setText(nameText);
137
138        if (isVoicemail && !TextUtils.isEmpty(details.transcription)) {
139            views.voicemailTranscriptionView.setText(details.transcription);
140            views.voicemailTranscriptionView.setVisibility(View.VISIBLE);
141        } else {
142            views.voicemailTranscriptionView.setText(null);
143            views.voicemailTranscriptionView.setVisibility(View.GONE);
144        }
145
146        // Bold if not read
147        Typeface typeface = details.isRead ? Typeface.SANS_SERIF : Typeface.DEFAULT_BOLD;
148        views.nameView.setTypeface(typeface);
149        views.voicemailTranscriptionView.setTypeface(typeface);
150        views.callLocationAndDate.setTypeface(typeface);
151    }
152
153    /**
154     * Builds a string containing the call location and date.
155     *
156     * @param details The call details.
157     * @return The call location and date string.
158     */
159    private CharSequence getCallLocationAndDate(PhoneCallDetails details) {
160        mDescriptionItems.clear();
161
162        // Get type of call (ie mobile, home, etc) if known, or the caller's location.
163        CharSequence callTypeOrLocation = getCallTypeOrLocation(details);
164
165        // Only add the call type or location if its not empty.  It will be empty for unknown
166        // callers.
167        if (!TextUtils.isEmpty(callTypeOrLocation)) {
168            mDescriptionItems.add(callTypeOrLocation);
169        }
170        // The date of this call, relative to the current time.
171        mDescriptionItems.add(getCallDate(details));
172
173        // Create a comma separated list from the call type or location, and call date.
174        return DialerUtils.join(mResources, mDescriptionItems);
175    }
176
177    /**
178     * For a call, if there is an associated contact for the caller, return the known call type
179     * (e.g. mobile, home, work).  If there is no associated contact, attempt to use the caller's
180     * location if known.
181     * @param details Call details to use.
182     * @return Type of call (mobile/home) if known, or the location of the caller (if known).
183     */
184    public CharSequence getCallTypeOrLocation(PhoneCallDetails details) {
185        CharSequence numberFormattedLabel = null;
186        // Only show a label if the number is shown and it is not a SIP address.
187        if (!TextUtils.isEmpty(details.number)
188                && !PhoneNumberHelper.isUriNumber(details.number.toString())
189                && !mTelecomCallLogCache.isVoicemailNumber(details.accountHandle, details.number)) {
190
191            if (TextUtils.isEmpty(details.name) && !TextUtils.isEmpty(details.geocode)) {
192                numberFormattedLabel = details.geocode;
193            } else if (!(details.numberType == Phone.TYPE_CUSTOM
194                    && TextUtils.isEmpty(details.numberLabel))) {
195                // Get type label only if it will not be "Custom" because of an empty number label.
196                numberFormattedLabel = Phone.getTypeLabel(
197                        mResources, details.numberType, details.numberLabel);
198            }
199        }
200
201        if (!TextUtils.isEmpty(details.name) && TextUtils.isEmpty(numberFormattedLabel)) {
202            numberFormattedLabel = details.displayNumber;
203        }
204        return numberFormattedLabel;
205    }
206
207    /**
208     * Get the call date/time of the call, relative to the current time.
209     * e.g. 3 minutes ago
210     * @param details Call details to use.
211     * @return String representing when the call occurred.
212     */
213    public CharSequence getCallDate(PhoneCallDetails details) {
214        return DateUtils.getRelativeTimeSpanString(details.date,
215                getCurrentTimeMillis(),
216                DateUtils.MINUTE_IN_MILLIS,
217                DateUtils.FORMAT_ABBREV_RELATIVE);
218    }
219
220    /** Sets the text of the header view for the details page of a phone call. */
221    @NeededForTesting
222    public void setCallDetailsHeader(TextView nameView, PhoneCallDetails details) {
223        final CharSequence nameText;
224        if (!TextUtils.isEmpty(details.name)) {
225            nameText = details.name;
226        } else if (!TextUtils.isEmpty(details.displayNumber)) {
227            nameText = details.displayNumber;
228        } else {
229            nameText = mResources.getString(R.string.unknown);
230        }
231
232        nameView.setText(nameText);
233    }
234
235    @NeededForTesting
236    public void setCurrentTimeForTest(long currentTimeMillis) {
237        mCurrentTimeMillisForTest = currentTimeMillis;
238    }
239
240    /**
241     * Returns the current time in milliseconds since the epoch.
242     * <p>
243     * It can be injected in tests using {@link #setCurrentTimeForTest(long)}.
244     */
245    private long getCurrentTimeMillis() {
246        if (mCurrentTimeMillisForTest == null) {
247            return System.currentTimeMillis();
248        } else {
249            return mCurrentTimeMillisForTest;
250        }
251    }
252
253    /** Sets the call count and date. */
254    private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount,
255            CharSequence dateText) {
256        // Combine the count (if present) and the date.
257        final CharSequence text;
258        if (callCount != null) {
259            text = mResources.getString(
260                    R.string.call_log_item_count_and_date, callCount.intValue(), dateText);
261        } else {
262            text = dateText;
263        }
264
265        views.callLocationAndDate.setText(text);
266    }
267}
268