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 | |
| 23 | import android.content.res.Resources; |
| 24 | import android.graphics.Typeface; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 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 | 8e39c71 | 2011-07-15 19:57:01 +0100 | [diff] [blame] | 36 | /** The maximum number of icons will be shown to represent the call types in a group. */ |
| 37 | private static final int MAX_CALL_TYPE_ICONS = 3; |
| 38 | |
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 | 4056956 | 2011-07-22 21:51:29 +0100 | [diff] [blame^] | 53 | public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper, |
| 54 | PhoneNumberHelper phoneNumberHelper) { |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 55 | mResources = resources; |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 56 | mCallTypeHelper = callTypeHelper; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 57 | mPhoneNumberHelper = phoneNumberHelper; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 60 | /** Fills the call details views with content. */ |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 61 | public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details, |
Flavio Lerda | 4056956 | 2011-07-22 21:51:29 +0100 | [diff] [blame^] | 62 | boolean useIcons, boolean isHighlighted, boolean nameOnly) { |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 63 | if (useIcons) { |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 64 | views.callTypeIcons.removeAllViews(); |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 65 | int count = details.callTypes.length; |
Flavio Lerda | 8e39c71 | 2011-07-15 19:57:01 +0100 | [diff] [blame] | 66 | for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) { |
Flavio Lerda | 4056956 | 2011-07-22 21:51:29 +0100 | [diff] [blame^] | 67 | mCallTypeHelper.inflateCallTypeIcon(details.callTypes[index], views.callTypeIcons); |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 68 | } |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 69 | views.callTypeIcons.setVisibility(View.VISIBLE); |
Flavio Lerda | 8e39c71 | 2011-07-15 19:57:01 +0100 | [diff] [blame] | 70 | if (count > MAX_CALL_TYPE_ICONS) { |
| 71 | views.callTypeText.setVisibility(View.VISIBLE); |
| 72 | views.callTypeSeparator.setVisibility(View.VISIBLE); |
| 73 | views.callTypeText.setText( |
| 74 | mResources.getString(R.string.call_log_item_count, count)); |
| 75 | } else { |
| 76 | views.callTypeText.setVisibility(View.GONE); |
| 77 | views.callTypeSeparator.setVisibility(View.GONE); |
| 78 | } |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 79 | } else { |
| 80 | String callTypeName; |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 81 | // Use the name of the first call type. |
| 82 | // TODO: We should update this to handle the text for multiple calls as well. |
| 83 | int callType = details.callTypes[0]; |
Flavio Lerda | 7692190 | 2011-07-13 21:11:51 +0100 | [diff] [blame] | 84 | views.callTypeText.setText( |
| 85 | isHighlighted ? mCallTypeHelper.getHighlightedCallTypeText(callType) |
| 86 | : mCallTypeHelper.getCallTypeText(callType)); |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 87 | views.callTypeIcons.removeAllViews(); |
| 88 | |
| 89 | views.callTypeText.setVisibility(View.VISIBLE); |
| 90 | views.callTypeSeparator.setVisibility(View.VISIBLE); |
| 91 | views.callTypeIcons.setVisibility(View.GONE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 92 | } |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 93 | |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 94 | CharSequence shortDateText = |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 95 | DateUtils.getRelativeTimeSpanString(details.date, |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 96 | getCurrentTimeMillis(), |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 97 | DateUtils.MINUTE_IN_MILLIS, |
| 98 | DateUtils.FORMAT_ABBREV_RELATIVE); |
| 99 | |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 100 | CharSequence numberFormattedLabel = null; |
| 101 | // 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] | 102 | if (!TextUtils.isEmpty(details.number) |
| 103 | && !PhoneNumberUtils.isUriNumber(details.number.toString())) { |
| 104 | numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType, |
| 105 | details.numberLabel); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 108 | final CharSequence nameText; |
| 109 | final CharSequence numberText; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 110 | final CharSequence displayNumber = |
| 111 | mPhoneNumberHelper.getDisplayNumber(details.number, details.formattedNumber); |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 112 | if (TextUtils.isEmpty(details.name)) { |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 113 | nameText = displayNumber; |
Flavio Lerda | 9c46637 | 2011-07-19 17:38:17 +0100 | [diff] [blame] | 114 | numberText = mPhoneNumberHelper.getGeocodeForNumber( |
Flavio Lerda | ebef771 | 2011-07-20 22:03:55 +0100 | [diff] [blame] | 115 | details.number.toString(), details.countryIso); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 116 | } else { |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 117 | nameText = details.name; |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 118 | if (numberFormattedLabel != null) { |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 119 | numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD, |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 120 | numberFormattedLabel + " " + displayNumber, 0, |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 121 | numberFormattedLabel.length(), |
| 122 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 123 | } else { |
| 124 | numberText = displayNumber; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 128 | views.dateView.setText(shortDateText); |
| 129 | views.dateView.setVisibility(View.VISIBLE); |
| 130 | views.nameView.setText(nameText); |
| 131 | views.nameView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 132 | // Do not show the number if it is not available. This happens if we have only the number, |
| 133 | // in which case the number is shown in the name field instead. |
| 134 | if (!TextUtils.isEmpty(numberText)) { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 135 | views.numberView.setText(numberText); |
| 136 | views.numberView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 137 | } else { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 138 | views.numberView.setVisibility(View.GONE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 139 | } |
Flavio Lerda | 4056956 | 2011-07-22 21:51:29 +0100 | [diff] [blame^] | 140 | |
| 141 | // Hide the rest if not visible. |
| 142 | views.callTypeView.setVisibility(nameOnly ? View.GONE : View.VISIBLE); |
| 143 | views.numberView.setVisibility(nameOnly ? View.GONE : View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 144 | } |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 145 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 146 | public void setCurrentTimeForTest(long currentTimeMillis) { |
| 147 | mCurrentTimeMillisForTest = currentTimeMillis; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns the current time in milliseconds since the epoch. |
| 152 | * <p> |
| 153 | * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. |
| 154 | */ |
| 155 | private long getCurrentTimeMillis() { |
| 156 | if (mCurrentTimeMillisForTest == null) { |
| 157 | return System.currentTimeMillis(); |
| 158 | } else { |
| 159 | return mCurrentTimeMillisForTest; |
| 160 | } |
| 161 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 162 | } |