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; |
| 20 | |
| 21 | import android.content.res.Resources; |
| 22 | import android.graphics.Typeface; |
| 23 | import android.provider.CallLog.Calls; |
| 24 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 25 | import android.telephony.PhoneNumberUtils; |
| 26 | import android.text.Spanned; |
| 27 | import android.text.TextUtils; |
| 28 | import android.text.format.DateUtils; |
| 29 | import android.view.View; |
| 30 | |
| 31 | /** |
| 32 | * Helper class to fill in the views in {@link PhoneCallDetailsViews}. |
| 33 | */ |
| 34 | public class PhoneCallDetailsHelper { |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 35 | 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 Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 54 | /** |
| 55 | * Fills the call details views with content. |
| 56 | * |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 57 | * @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 Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 64 | public void setPhoneCallDetails(PhoneCallDetailsViews views, long date, |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 65 | int callType, CharSequence name, CharSequence number, int numberType, |
| 66 | CharSequence numberLabel) { |
| 67 | CharSequence callTypeText = ""; |
| 68 | switch (callType) { |
| 69 | case Calls.INCOMING_TYPE: |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 70 | callTypeText = mTypeIncomingText; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 71 | break; |
| 72 | |
| 73 | case Calls.OUTGOING_TYPE: |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 74 | callTypeText = mTypeOutgoingText; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 75 | break; |
| 76 | |
| 77 | case Calls.MISSED_TYPE: |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 78 | callTypeText = mTypeMissedText; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 79 | 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 Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 95 | numberFormattedLabel = Phone.getTypeLabel(mResources, numberType, numberLabel); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 96 | } |
| 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 | } |