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; |
Flavio Lerda | fe17026 | 2011-08-03 06:40:47 +0100 | [diff] [blame] | 27 | import android.text.SpannableString; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 28 | import android.text.Spanned; |
| 29 | import android.text.TextUtils; |
| 30 | import android.text.format.DateUtils; |
Flavio Lerda | fe17026 | 2011-08-03 06:40:47 +0100 | [diff] [blame] | 31 | import android.text.style.ForegroundColorSpan; |
| 32 | import android.text.style.StyleSpan; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 33 | import android.view.View; |
Flavio Lerda | b88abaa | 2011-08-02 23:38:36 +0100 | [diff] [blame] | 34 | import android.widget.TextView; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Helper class to fill in the views in {@link PhoneCallDetailsViews}. |
| 38 | */ |
| 39 | public class PhoneCallDetailsHelper { |
Flavio Lerda | 8e39c71 | 2011-07-15 19:57:01 +0100 | [diff] [blame] | 40 | /** The maximum number of icons will be shown to represent the call types in a group. */ |
| 41 | private static final int MAX_CALL_TYPE_ICONS = 3; |
| 42 | |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 43 | private final Resources mResources; |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 44 | /** The injected current time in milliseconds since the epoch. Used only by tests. */ |
| 45 | private Long mCurrentTimeMillisForTest; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 46 | // Helper classes. |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 47 | private final CallTypeHelper mCallTypeHelper; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 48 | private final PhoneNumberHelper mPhoneNumberHelper; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Creates a new instance of the helper. |
| 52 | * <p> |
| 53 | * Generally you should have a single instance of this helper in any context. |
| 54 | * |
| 55 | * @param resources used to look up strings |
| 56 | */ |
Flavio Lerda | 4056956 | 2011-07-22 21:51:29 +0100 | [diff] [blame] | 57 | public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper, |
| 58 | PhoneNumberHelper phoneNumberHelper) { |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 59 | mResources = resources; |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 60 | mCallTypeHelper = callTypeHelper; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 61 | mPhoneNumberHelper = phoneNumberHelper; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 64 | /** Fills the call details views with content. */ |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 65 | public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details, |
Flavio Lerda | fe17026 | 2011-08-03 06:40:47 +0100 | [diff] [blame] | 66 | boolean isHighlighted) { |
| 67 | // Display up to a given number of icons. |
| 68 | views.callTypeIcons.clear(); |
| 69 | int count = details.callTypes.length; |
| 70 | for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) { |
| 71 | views.callTypeIcons.add(details.callTypes[index]); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 72 | } |
Flavio Lerda | fe17026 | 2011-08-03 06:40:47 +0100 | [diff] [blame] | 73 | views.callTypeIcons.setVisibility(View.VISIBLE); |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 74 | |
Flavio Lerda | fe17026 | 2011-08-03 06:40:47 +0100 | [diff] [blame] | 75 | // Show the total call count only if there are more than the maximum number of icons. |
| 76 | final Integer callCount; |
| 77 | if (count > MAX_CALL_TYPE_ICONS) { |
| 78 | callCount = count; |
| 79 | } else { |
| 80 | callCount = null; |
| 81 | } |
| 82 | // The color to highlight the count and date in, if any. This is based on the first call. |
| 83 | Integer highlightColor = |
| 84 | isHighlighted ? mCallTypeHelper.getHighlightedColor(details.callTypes[0]) : null; |
| 85 | |
| 86 | // The date of this call, relative to the current time. |
| 87 | CharSequence dateText = |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 88 | DateUtils.getRelativeTimeSpanString(details.date, |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 89 | getCurrentTimeMillis(), |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 90 | DateUtils.MINUTE_IN_MILLIS, |
| 91 | DateUtils.FORMAT_ABBREV_RELATIVE); |
| 92 | |
Flavio Lerda | fe17026 | 2011-08-03 06:40:47 +0100 | [diff] [blame] | 93 | // Set the call count and date. |
| 94 | setCallCountAndDate(views, callCount, dateText, highlightColor); |
| 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 | 71fc6ec | 2011-08-09 17:24:29 +0100 | [diff] [blame] | 110 | if (TextUtils.isEmpty(details.geocode)) { |
Flavio Lerda | be45e0f | 2011-07-25 21:00:30 +0100 | [diff] [blame] | 111 | numberText = mResources.getString(R.string.call_log_empty_gecode); |
| 112 | } else { |
Flavio Lerda | 71fc6ec | 2011-08-09 17:24:29 +0100 | [diff] [blame] | 113 | numberText = details.geocode; |
Flavio Lerda | be45e0f | 2011-07-25 21:00:30 +0100 | [diff] [blame] | 114 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 115 | } else { |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 116 | nameText = details.name; |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 117 | if (numberFormattedLabel != null) { |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 118 | numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD, |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 119 | numberFormattedLabel + " " + displayNumber, 0, |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 120 | numberFormattedLabel.length(), |
| 121 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 122 | } else { |
| 123 | numberText = displayNumber; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 127 | views.nameView.setText(nameText); |
Flavio Lerda | b88abaa | 2011-08-02 23:38:36 +0100 | [diff] [blame] | 128 | views.numberView.setText(numberText); |
| 129 | } |
| 130 | |
| 131 | /** Sets the name in the text view for the given phone call. */ |
| 132 | public void setPhoneCallName(TextView nameView, PhoneCallDetails details) { |
| 133 | final CharSequence nameText; |
| 134 | final CharSequence displayNumber = |
| 135 | mPhoneNumberHelper.getDisplayNumber(details.number, details.formattedNumber); |
| 136 | if (TextUtils.isEmpty(details.name)) { |
| 137 | nameText = displayNumber; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 138 | } else { |
Flavio Lerda | b88abaa | 2011-08-02 23:38:36 +0100 | [diff] [blame] | 139 | nameText = details.name; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 140 | } |
Flavio Lerda | 4056956 | 2011-07-22 21:51:29 +0100 | [diff] [blame] | 141 | |
Flavio Lerda | b88abaa | 2011-08-02 23:38:36 +0100 | [diff] [blame] | 142 | nameView.setText(nameText); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 143 | } |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 144 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 145 | public void setCurrentTimeForTest(long currentTimeMillis) { |
| 146 | mCurrentTimeMillisForTest = currentTimeMillis; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns the current time in milliseconds since the epoch. |
| 151 | * <p> |
| 152 | * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. |
| 153 | */ |
| 154 | private long getCurrentTimeMillis() { |
| 155 | if (mCurrentTimeMillisForTest == null) { |
| 156 | return System.currentTimeMillis(); |
| 157 | } else { |
| 158 | return mCurrentTimeMillisForTest; |
| 159 | } |
| 160 | } |
Flavio Lerda | fe17026 | 2011-08-03 06:40:47 +0100 | [diff] [blame] | 161 | |
| 162 | /** Sets the call count and date. */ |
| 163 | private void setCallCountAndDate(PhoneCallDetailsViews views, Integer callCount, |
| 164 | CharSequence dateText, Integer highlightColor) { |
| 165 | // Combine the count (if present) and the date. |
| 166 | final CharSequence text; |
| 167 | if (callCount != null) { |
| 168 | text = mResources.getString( |
| 169 | R.string.call_log_item_count_and_date, callCount.intValue(), dateText); |
| 170 | } else { |
| 171 | text = dateText; |
| 172 | } |
| 173 | |
| 174 | // Apply the highlight color if present. |
| 175 | final CharSequence formattedText; |
| 176 | if (highlightColor != null) { |
| 177 | formattedText = addBoldAndColor(text, highlightColor); |
| 178 | } else { |
| 179 | formattedText = text; |
| 180 | } |
| 181 | |
| 182 | views.callTypeAndDate.setText(formattedText); |
| 183 | } |
| 184 | |
| 185 | /** Creates a SpannableString for the given text which is bold and in the given color. */ |
| 186 | private CharSequence addBoldAndColor(CharSequence text, int color) { |
| 187 | int flags = Spanned.SPAN_INCLUSIVE_INCLUSIVE; |
| 188 | SpannableString result = new SpannableString(text); |
| 189 | result.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), flags); |
| 190 | result.setSpan(new ForegroundColorSpan(color), 0, text.length(), flags); |
| 191 | return result; |
| 192 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 193 | } |