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 | |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 19 | import com.android.contacts.calllog.CallTypeHelper; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 20 | import com.android.contacts.calllog.PhoneNumberHelper; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 21 | import com.android.contacts.format.FormatUtils; |
| 22 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 23 | import android.content.Context; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 24 | import android.content.res.Resources; |
| 25 | import android.graphics.Typeface; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 26 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 27 | import android.telephony.PhoneNumberUtils; |
| 28 | import android.text.Spanned; |
| 29 | import android.text.TextUtils; |
| 30 | import android.text.format.DateUtils; |
| 31 | import android.view.View; |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 32 | import android.widget.ImageView; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Helper class to fill in the views in {@link PhoneCallDetailsViews}. |
| 36 | */ |
| 37 | public class PhoneCallDetailsHelper { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 38 | private final Context mContext; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 39 | private final Resources mResources; |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 40 | /** The injected current time in milliseconds since the epoch. Used only by tests. */ |
| 41 | private Long mCurrentTimeMillisForTest; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 42 | // Helper classes. |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 43 | private final CallTypeHelper mCallTypeHelper; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 44 | private final PhoneNumberHelper mPhoneNumberHelper; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Creates a new instance of the helper. |
| 48 | * <p> |
| 49 | * Generally you should have a single instance of this helper in any context. |
| 50 | * |
| 51 | * @param resources used to look up strings |
| 52 | */ |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 53 | public PhoneCallDetailsHelper(Context context, Resources resources, |
| 54 | CallTypeHelper callTypeHelper, PhoneNumberHelper phoneNumberHelper) { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 55 | mContext = context; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 56 | mResources = resources; |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 57 | mCallTypeHelper = callTypeHelper; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 58 | mPhoneNumberHelper = phoneNumberHelper; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 61 | /** Fills the call details views with content. */ |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 62 | public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details, |
Flavio Lerda | 7692190 | 2011-07-13 21:11:51 +0100 | [diff] [blame] | 63 | boolean useIcons, boolean isHighlighted) { |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 64 | if (useIcons) { |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 65 | views.callTypeIcons.removeAllViews(); |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 66 | int count = details.callTypes.length; |
| 67 | for (int callType : details.callTypes) { |
| 68 | ImageView callTypeImage = new ImageView(mContext); |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 69 | callTypeImage.setImageDrawable(mCallTypeHelper.getCallTypeDrawable(callType)); |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 70 | views.callTypeIcons.addView(callTypeImage); |
| 71 | } |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 72 | views.callTypeIcons.setVisibility(View.VISIBLE); |
| 73 | views.callTypeText.setVisibility(View.GONE); |
| 74 | views.callTypeSeparator.setVisibility(View.GONE); |
| 75 | } else { |
| 76 | String callTypeName; |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 77 | // Use the name of the first call type. |
| 78 | // TODO: We should update this to handle the text for multiple calls as well. |
| 79 | int callType = details.callTypes[0]; |
Flavio Lerda | 7692190 | 2011-07-13 21:11:51 +0100 | [diff] [blame] | 80 | views.callTypeText.setText( |
| 81 | isHighlighted ? mCallTypeHelper.getHighlightedCallTypeText(callType) |
| 82 | : mCallTypeHelper.getCallTypeText(callType)); |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 83 | views.callTypeIcons.removeAllViews(); |
| 84 | |
| 85 | views.callTypeText.setVisibility(View.VISIBLE); |
| 86 | views.callTypeSeparator.setVisibility(View.VISIBLE); |
| 87 | views.callTypeIcons.setVisibility(View.GONE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 88 | } |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 89 | |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 90 | CharSequence shortDateText = |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 91 | DateUtils.getRelativeTimeSpanString(details.date, |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 92 | getCurrentTimeMillis(), |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 93 | DateUtils.MINUTE_IN_MILLIS, |
| 94 | DateUtils.FORMAT_ABBREV_RELATIVE); |
| 95 | |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 96 | CharSequence numberFormattedLabel = null; |
| 97 | // Only show a label if the number is shown and it is not a SIP address. |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 98 | if (!TextUtils.isEmpty(details.number) |
| 99 | && !PhoneNumberUtils.isUriNumber(details.number.toString())) { |
| 100 | numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType, |
| 101 | details.numberLabel); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 104 | final CharSequence nameText; |
| 105 | final CharSequence numberText; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 106 | final CharSequence displayNumber = |
| 107 | mPhoneNumberHelper.getDisplayNumber(details.number, details.formattedNumber); |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 108 | if (TextUtils.isEmpty(details.name)) { |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 109 | nameText = displayNumber; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 110 | numberText = ""; |
| 111 | } else { |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 112 | nameText = details.name; |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 113 | if (numberFormattedLabel != null) { |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 114 | numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD, |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 115 | numberFormattedLabel + " " + displayNumber, 0, |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 116 | numberFormattedLabel.length(), |
| 117 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 118 | } else { |
| 119 | numberText = displayNumber; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 123 | views.dateView.setText(shortDateText); |
| 124 | views.dateView.setVisibility(View.VISIBLE); |
| 125 | views.nameView.setText(nameText); |
| 126 | views.nameView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 127 | // Do not show the number if it is not available. This happens if we have only the number, |
| 128 | // in which case the number is shown in the name field instead. |
| 129 | if (!TextUtils.isEmpty(numberText)) { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 130 | views.numberView.setText(numberText); |
| 131 | views.numberView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 132 | } else { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 133 | views.numberView.setVisibility(View.GONE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 134 | } |
| 135 | } |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 136 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 137 | public void setCurrentTimeForTest(long currentTimeMillis) { |
| 138 | mCurrentTimeMillisForTest = currentTimeMillis; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns the current time in milliseconds since the epoch. |
| 143 | * <p> |
| 144 | * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. |
| 145 | */ |
| 146 | private long getCurrentTimeMillis() { |
| 147 | if (mCurrentTimeMillisForTest == null) { |
| 148 | return System.currentTimeMillis(); |
| 149 | } else { |
| 150 | return mCurrentTimeMillisForTest; |
| 151 | } |
| 152 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 153 | } |