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; |
Flavio Lerda | b88abaa | 2011-08-02 23:38:36 +0100 | [diff] [blame^] | 31 | import android.widget.TextView; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Helper class to fill in the views in {@link PhoneCallDetailsViews}. |
| 35 | */ |
| 36 | public class PhoneCallDetailsHelper { |
Flavio Lerda | 8e39c71 | 2011-07-15 19:57:01 +0100 | [diff] [blame] | 37 | /** The maximum number of icons will be shown to represent the call types in a group. */ |
| 38 | private static final int MAX_CALL_TYPE_ICONS = 3; |
| 39 | |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 40 | private final Resources mResources; |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 41 | /** The injected current time in milliseconds since the epoch. Used only by tests. */ |
| 42 | private Long mCurrentTimeMillisForTest; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 43 | // Helper classes. |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 44 | private final CallTypeHelper mCallTypeHelper; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 45 | private final PhoneNumberHelper mPhoneNumberHelper; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Creates a new instance of the helper. |
| 49 | * <p> |
| 50 | * Generally you should have a single instance of this helper in any context. |
| 51 | * |
| 52 | * @param resources used to look up strings |
| 53 | */ |
Flavio Lerda | 4056956 | 2011-07-22 21:51:29 +0100 | [diff] [blame] | 54 | public PhoneCallDetailsHelper(Resources resources, CallTypeHelper callTypeHelper, |
| 55 | PhoneNumberHelper phoneNumberHelper) { |
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 | b88abaa | 2011-08-02 23:38:36 +0100 | [diff] [blame^] | 63 | boolean useIcons, boolean isHighlighted) { |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 64 | if (useIcons) { |
Daniel Lehmann | 6ecb732 | 2011-08-01 21:39:29 -0700 | [diff] [blame] | 65 | views.callTypeIcons.clear(); |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 66 | int count = details.callTypes.length; |
Flavio Lerda | 8e39c71 | 2011-07-15 19:57:01 +0100 | [diff] [blame] | 67 | for (int index = 0; index < count && index < MAX_CALL_TYPE_ICONS; ++index) { |
Daniel Lehmann | 6ecb732 | 2011-08-01 21:39:29 -0700 | [diff] [blame] | 68 | views.callTypeIcons.add(details.callTypes[index]); |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 69 | } |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 70 | views.callTypeIcons.setVisibility(View.VISIBLE); |
Flavio Lerda | 8e39c71 | 2011-07-15 19:57:01 +0100 | [diff] [blame] | 71 | if (count > MAX_CALL_TYPE_ICONS) { |
| 72 | views.callTypeText.setVisibility(View.VISIBLE); |
| 73 | views.callTypeSeparator.setVisibility(View.VISIBLE); |
| 74 | views.callTypeText.setText( |
| 75 | mResources.getString(R.string.call_log_item_count, count)); |
| 76 | } else { |
| 77 | views.callTypeText.setVisibility(View.GONE); |
| 78 | views.callTypeSeparator.setVisibility(View.GONE); |
| 79 | } |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 80 | } else { |
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)); |
Daniel Lehmann | 6ecb732 | 2011-08-01 21:39:29 -0700 | [diff] [blame] | 87 | views.callTypeIcons.clear(); |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 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 | be45e0f | 2011-07-25 21:00:30 +0100 | [diff] [blame] | 114 | String geocode = mPhoneNumberHelper.getGeocodeForNumber( |
Flavio Lerda | ebef771 | 2011-07-20 22:03:55 +0100 | [diff] [blame] | 115 | details.number.toString(), details.countryIso); |
Flavio Lerda | be45e0f | 2011-07-25 21:00:30 +0100 | [diff] [blame] | 116 | if (TextUtils.isEmpty(geocode)) { |
| 117 | numberText = mResources.getString(R.string.call_log_empty_gecode); |
| 118 | } else { |
| 119 | numberText = geocode; |
| 120 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 121 | } else { |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 122 | nameText = details.name; |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 123 | if (numberFormattedLabel != null) { |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 124 | numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD, |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 125 | numberFormattedLabel + " " + displayNumber, 0, |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 126 | numberFormattedLabel.length(), |
| 127 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 128 | } else { |
| 129 | numberText = displayNumber; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 133 | views.dateView.setText(shortDateText); |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 134 | views.nameView.setText(nameText); |
Flavio Lerda | b88abaa | 2011-08-02 23:38:36 +0100 | [diff] [blame^] | 135 | views.numberView.setText(numberText); |
| 136 | } |
| 137 | |
| 138 | /** Sets the name in the text view for the given phone call. */ |
| 139 | public void setPhoneCallName(TextView nameView, PhoneCallDetails details) { |
| 140 | final CharSequence nameText; |
| 141 | final CharSequence displayNumber = |
| 142 | mPhoneNumberHelper.getDisplayNumber(details.number, details.formattedNumber); |
| 143 | if (TextUtils.isEmpty(details.name)) { |
| 144 | nameText = displayNumber; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 145 | } else { |
Flavio Lerda | b88abaa | 2011-08-02 23:38:36 +0100 | [diff] [blame^] | 146 | nameText = details.name; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 147 | } |
Flavio Lerda | 4056956 | 2011-07-22 21:51:29 +0100 | [diff] [blame] | 148 | |
Flavio Lerda | b88abaa | 2011-08-02 23:38:36 +0100 | [diff] [blame^] | 149 | nameView.setText(nameText); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 150 | } |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 151 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 152 | public void setCurrentTimeForTest(long currentTimeMillis) { |
| 153 | mCurrentTimeMillisForTest = currentTimeMillis; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns the current time in milliseconds since the epoch. |
| 158 | * <p> |
| 159 | * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. |
| 160 | */ |
| 161 | private long getCurrentTimeMillis() { |
| 162 | if (mCurrentTimeMillisForTest == null) { |
| 163 | return System.currentTimeMillis(); |
| 164 | } else { |
| 165 | return mCurrentTimeMillisForTest; |
| 166 | } |
| 167 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 168 | } |