Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 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 | |
| 17 | package com.android.contacts; |
| 18 | |
| 19 | import com.android.contacts.format.FormatUtils; |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame^] | 20 | import com.android.internal.telephony.CallerInfo; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 21 | |
| 22 | import android.content.res.Resources; |
| 23 | import android.graphics.Typeface; |
| 24 | import android.provider.CallLog.Calls; |
| 25 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 26 | import android.telephony.PhoneNumberUtils; |
| 27 | import android.text.Spanned; |
| 28 | import android.text.TextUtils; |
| 29 | import android.text.format.DateUtils; |
| 30 | import android.view.View; |
| 31 | |
| 32 | /** |
| 33 | * Helper class to fill in the views in {@link PhoneCallDetailsViews}. |
| 34 | */ |
| 35 | public class PhoneCallDetailsHelper { |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 36 | private final Resources mResources; |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame^] | 37 | private final String mVoicemailNumber; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 38 | 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 Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame^] | 49 | public PhoneCallDetailsHelper(Resources resources, String voicemailNumber) { |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 50 | mResources = resources; |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame^] | 51 | mVoicemailNumber = voicemailNumber; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 52 | 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 Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 57 | /** |
| 58 | * Fills the call details views with content. |
| 59 | * |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 60 | * @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 Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 67 | public void setPhoneCallDetails(PhoneCallDetailsViews views, long date, |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 68 | int callType, CharSequence name, CharSequence number, int numberType, |
| 69 | CharSequence numberLabel) { |
| 70 | CharSequence callTypeText = ""; |
| 71 | switch (callType) { |
| 72 | case Calls.INCOMING_TYPE: |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 73 | callTypeText = mTypeIncomingText; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 74 | break; |
| 75 | |
| 76 | case Calls.OUTGOING_TYPE: |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 77 | callTypeText = mTypeOutgoingText; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 78 | break; |
| 79 | |
| 80 | case Calls.MISSED_TYPE: |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 81 | callTypeText = mTypeMissedText; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 82 | 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 Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 98 | numberFormattedLabel = Phone.getTypeLabel(mResources, numberType, numberLabel); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | CharSequence nameText; |
| 102 | CharSequence numberText; |
| 103 | if (TextUtils.isEmpty(name)) { |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame^] | 104 | nameText = getDisplayNumber(number); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 105 | numberText = ""; |
| 106 | } else { |
| 107 | nameText = name; |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame^] | 108 | numberText = getDisplayNumber(number); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 109 | 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 Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame^] | 130 | |
| 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 Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 149 | } |