blob: fc96a8998bc2bf34dbcbc942ddb7eb678050971a [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;
Flavio Lerda696e8132011-07-05 19:00:23 +010020import com.android.internal.telephony.CallerInfo;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010021
22import android.content.res.Resources;
23import android.graphics.Typeface;
24import android.provider.CallLog.Calls;
25import android.provider.ContactsContract.CommonDataKinds.Phone;
26import android.telephony.PhoneNumberUtils;
27import android.text.Spanned;
28import android.text.TextUtils;
29import android.text.format.DateUtils;
30import android.view.View;
31
32/**
33 * Helper class to fill in the views in {@link PhoneCallDetailsViews}.
34 */
35public class PhoneCallDetailsHelper {
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010036 private final Resources mResources;
Flavio Lerda696e8132011-07-05 19:00:23 +010037 private final String mVoicemailNumber;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010038 private final String mTypeIncomingText;
39 private final String mTypeOutgoingText;
40 private final String mTypeMissedText;
41
42 /**
43 * Creates a new instance of the helper.
44 * <p>
45 * Generally you should have a single instance of this helper in any context.
46 *
47 * @param resources used to look up strings
48 */
Flavio Lerda696e8132011-07-05 19:00:23 +010049 public PhoneCallDetailsHelper(Resources resources, String voicemailNumber) {
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010050 mResources = resources;
Flavio Lerda696e8132011-07-05 19:00:23 +010051 mVoicemailNumber = voicemailNumber;
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010052 mTypeIncomingText = mResources.getString(R.string.type_incoming);
53 mTypeOutgoingText = mResources.getString(R.string.type_outgoing);
54 mTypeMissedText = mResources.getString(R.string.type_missed);
55 }
56
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010057 /**
58 * Fills the call details views with content.
59 *
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010060 * @param date the date of the call, in milliseconds since the epoch
61 * @param callType the type of call, as defined in the call log table
62 * @param name the name of the contact, if available
63 * @param number the number of the other party involved in the call
64 * @param numberType the type of phone, e.g., {@link Phone#TYPE_HOME}, 0 if not available
65 * @param numberLabel the custom label associated with the phone number in the contact
66 */
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010067 public void setPhoneCallDetails(PhoneCallDetailsViews views, long date,
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010068 int callType, CharSequence name, CharSequence number, int numberType,
69 CharSequence numberLabel) {
70 CharSequence callTypeText = "";
71 switch (callType) {
72 case Calls.INCOMING_TYPE:
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010073 callTypeText = mTypeIncomingText;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010074 break;
75
76 case Calls.OUTGOING_TYPE:
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010077 callTypeText = mTypeOutgoingText;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010078 break;
79
80 case Calls.MISSED_TYPE:
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010081 callTypeText = mTypeMissedText;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010082 break;
83 }
84
85 CharSequence shortDateText =
86 DateUtils.getRelativeTimeSpanString(date,
87 System.currentTimeMillis(),
88 DateUtils.MINUTE_IN_MILLIS,
89 DateUtils.FORMAT_ABBREV_RELATIVE);
90
91 CharSequence callTypeAndDateText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
92 callTypeText + " " + shortDateText, 0, callTypeText.length(),
93 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
94
95 CharSequence numberFormattedLabel = null;
96 // Only show a label if the number is shown and it is not a SIP address.
97 if (!TextUtils.isEmpty(number) && !PhoneNumberUtils.isUriNumber(number.toString())) {
Flavio Lerdad72bf8a2011-07-05 16:00:09 +010098 numberFormattedLabel = Phone.getTypeLabel(mResources, numberType, numberLabel);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010099 }
100
101 CharSequence nameText;
102 CharSequence numberText;
103 if (TextUtils.isEmpty(name)) {
Flavio Lerda696e8132011-07-05 19:00:23 +0100104 nameText = getDisplayNumber(number);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100105 numberText = "";
106 } else {
107 nameText = name;
Flavio Lerda696e8132011-07-05 19:00:23 +0100108 numberText = getDisplayNumber(number);
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100109 if (callType != 0 && numberFormattedLabel != null) {
110 numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD,
111 numberFormattedLabel + " " + number, 0,
112 numberFormattedLabel.length(),
113 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
114 }
115 }
116
117 views.mCallTypeAndDateView.setText(callTypeAndDateText);
118 views.mCallTypeAndDateView.setVisibility(View.VISIBLE);
119 views.mNameView.setText(nameText);
120 views.mNameView.setVisibility(View.VISIBLE);
121 // Do not show the number if it is not available. This happens if we have only the number,
122 // in which case the number is shown in the name field instead.
123 if (!TextUtils.isEmpty(numberText)) {
124 views.mNumberView.setText(numberText);
125 views.mNumberView.setVisibility(View.VISIBLE);
126 } else {
127 views.mNumberView.setVisibility(View.GONE);
128 }
129 }
Flavio Lerda696e8132011-07-05 19:00:23 +0100130
131 private CharSequence getDisplayNumber(CharSequence number) {
132 if (TextUtils.isEmpty(number)) {
133 return "";
134 }
135 if (number.equals(CallerInfo.UNKNOWN_NUMBER)) {
136 return mResources.getString(R.string.unknown);
137 }
138 if (number.equals(CallerInfo.PRIVATE_NUMBER)) {
139 return mResources.getString(R.string.private_num);
140 }
141 if (number.equals(CallerInfo.PAYPHONE_NUMBER)) {
142 return mResources.getString(R.string.payphone);
143 }
144 if (PhoneNumberUtils.extractNetworkPortion(number.toString()).equals(mVoicemailNumber)) {
145 return mResources.getString(R.string.voicemail);
146 }
147 return number;
148 }
Flavio Lerdaafb93bb2011-07-05 14:46:10 +0100149}