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 { |
| 35 | /** |
| 36 | * Fills the call details views with content. |
| 37 | * |
| 38 | * @param resources used to look up strings |
| 39 | * @param date the date of the call, in milliseconds since the epoch |
| 40 | * @param callType the type of call, as defined in the call log table |
| 41 | * @param name the name of the contact, if available |
| 42 | * @param number the number of the other party involved in the call |
| 43 | * @param numberType the type of phone, e.g., {@link Phone#TYPE_HOME}, 0 if not available |
| 44 | * @param numberLabel the custom label associated with the phone number in the contact |
| 45 | */ |
| 46 | public void setPhoneCallDetails(PhoneCallDetailsViews views, Resources resources, long date, |
| 47 | int callType, CharSequence name, CharSequence number, int numberType, |
| 48 | CharSequence numberLabel) { |
| 49 | CharSequence callTypeText = ""; |
| 50 | switch (callType) { |
| 51 | case Calls.INCOMING_TYPE: |
| 52 | callTypeText = resources.getString(R.string.type_incoming); |
| 53 | break; |
| 54 | |
| 55 | case Calls.OUTGOING_TYPE: |
| 56 | callTypeText = resources.getString(R.string.type_outgoing); |
| 57 | break; |
| 58 | |
| 59 | case Calls.MISSED_TYPE: |
| 60 | callTypeText = resources.getString(R.string.type_missed); |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | CharSequence shortDateText = |
| 65 | DateUtils.getRelativeTimeSpanString(date, |
| 66 | System.currentTimeMillis(), |
| 67 | DateUtils.MINUTE_IN_MILLIS, |
| 68 | DateUtils.FORMAT_ABBREV_RELATIVE); |
| 69 | |
| 70 | CharSequence callTypeAndDateText = FormatUtils.applyStyleToSpan(Typeface.BOLD, |
| 71 | callTypeText + " " + shortDateText, 0, callTypeText.length(), |
| 72 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 73 | |
| 74 | CharSequence numberFormattedLabel = null; |
| 75 | // Only show a label if the number is shown and it is not a SIP address. |
| 76 | if (!TextUtils.isEmpty(number) && !PhoneNumberUtils.isUriNumber(number.toString())) { |
| 77 | numberFormattedLabel = Phone.getTypeLabel(resources, numberType, numberLabel); |
| 78 | } |
| 79 | |
| 80 | CharSequence nameText; |
| 81 | CharSequence numberText; |
| 82 | if (TextUtils.isEmpty(name)) { |
| 83 | nameText = number; |
| 84 | numberText = ""; |
| 85 | } else { |
| 86 | nameText = name; |
| 87 | numberText = number; |
| 88 | if (callType != 0 && numberFormattedLabel != null) { |
| 89 | numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD, |
| 90 | numberFormattedLabel + " " + number, 0, |
| 91 | numberFormattedLabel.length(), |
| 92 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | views.mCallTypeAndDateView.setText(callTypeAndDateText); |
| 97 | views.mCallTypeAndDateView.setVisibility(View.VISIBLE); |
| 98 | views.mNameView.setText(nameText); |
| 99 | views.mNameView.setVisibility(View.VISIBLE); |
| 100 | // Do not show the number if it is not available. This happens if we have only the number, |
| 101 | // in which case the number is shown in the name field instead. |
| 102 | if (!TextUtils.isEmpty(numberText)) { |
| 103 | views.mNumberView.setText(numberText); |
| 104 | views.mNumberView.setVisibility(View.VISIBLE); |
| 105 | } else { |
| 106 | views.mNumberView.setVisibility(View.GONE); |
| 107 | } |
| 108 | } |
| 109 | } |