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