blob: 6542f7d5e76e479e85da9a24a778c348da9167cd [file] [log] [blame]
Flavio Lerdaafb93bb2011-07-05 14:46:10 +01001/*
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.contacts;
18
19import com.android.contacts.format.FormatUtils;
20
21import android.content.res.Resources;
22import android.graphics.Typeface;
23import android.provider.CallLog.Calls;
24import android.provider.ContactsContract.CommonDataKinds.Phone;
25import android.telephony.PhoneNumberUtils;
26import android.text.Spanned;
27import android.text.TextUtils;
28import android.text.format.DateUtils;
29import android.view.View;
30
31/**
32 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
33 */
34public class PhoneCallDetailsHelper {
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010035 private final Resources mResources;
36 private final String mTypeIncomingText;
37 private final String mTypeOutgoingText;
38 private final String mTypeMissedText;
39
40 /**
41 * Creates a new instance of the helper.
42 * <p>
43 * Generally you should have a single instance of this helper in any context.
44 *
45 * @param resources used to look up strings
46 */
47 public PhoneCallDetailsHelper(Resources resources) {
48 mResources = resources;
49 mTypeIncomingText = mResources.getString(R.string.type_incoming);
50 mTypeOutgoingText = mResources.getString(R.string.type_outgoing);
51 mTypeMissedText = mResources.getString(R.string.type_missed);
52 }
53
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010054 /**
55 * Fills the call details views with content.
56 *
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010057 * @param date the date of the call, in milliseconds since the epoch
58 * @param callType the type of call, as defined in the call log table
59 * @param name the name of the contact, if available
60 * @param number the number of the other party involved in the call
61 * @param numberType the type of phone, e.g., {@link Phone#TYPE_HOME}, 0 if not available
62 * @param numberLabel the custom label associated with the phone number in the contact
63 */
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010064 public void setPhoneCallDetails(PhoneCallDetailsViews views, long date,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010065 int callType, CharSequence name, CharSequence number, int numberType,
66 CharSequence numberLabel) {
67 CharSequence callTypeText = "";
68 switch (callType) {
69 case Calls.INCOMING_TYPE:
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010070 callTypeText = mTypeIncomingText;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010071 break;
72
73 case Calls.OUTGOING_TYPE:
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010074 callTypeText = mTypeOutgoingText;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010075 break;
76
77 case Calls.MISSED_TYPE:
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010078 callTypeText = mTypeMissedText;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010079 break;
80 }
81
82 CharSequence shortDateText =
83 DateUtils.getRelativeTimeSpanString(date,
84 System.currentTimeMillis(),
85 DateUtils.MINUTE_IN_MILLIS,
86 DateUtils.FORMAT_ABBREV_RELATIVE);
87
88 CharSequence callTypeAndDateText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
89 callTypeText + " " + shortDateText, 0, callTypeText.length(),
90 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
91
92 CharSequence numberFormattedLabel = null;
93 // Only show a label if the number is shown and it is not a SIP address.
94 if (!TextUtils.isEmpty(number) && !PhoneNumberUtils.isUriNumber(number.toString())) {
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010095 numberFormattedLabel = Phone.getTypeLabel(mResources, numberType, numberLabel);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010096 }
97
98 CharSequence nameText;
99 CharSequence numberText;
100 if (TextUtils.isEmpty(name)) {
101 nameText = number;
102 numberText = "";
103 } else {
104 nameText = name;
105 numberText = number;
106 if (callType != 0 && numberFormattedLabel != null) {
107 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
108 numberFormattedLabel + " " + number, 0,
109 numberFormattedLabel.length(),
110 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
111 }
112 }
113
114 views.mCallTypeAndDateView.setText(callTypeAndDateText);
115 views.mCallTypeAndDateView.setVisibility(View.VISIBLE);
116 views.mNameView.setText(nameText);
117 views.mNameView.setVisibility(View.VISIBLE);
118 // Do not show the number if it is not available. This happens if we have only the number,
119 // in which case the number is shown in the name field instead.
120 if (!TextUtils.isEmpty(numberText)) {
121 views.mNumberView.setText(numberText);
122 views.mNumberView.setVisibility(View.VISIBLE);
123 } else {
124 views.mNumberView.setVisibility(View.GONE);
125 }
126 }
127}