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 | |
| 19 | import com.android.contacts.format.FormatUtils; |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 20 | import com.android.internal.telephony.CallerInfo; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 21 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 22 | import android.content.Context; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 23 | import android.content.res.Resources; |
| 24 | import android.graphics.Typeface; |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 26 | import android.provider.CallLog.Calls; |
| 27 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 28 | import android.telephony.PhoneNumberUtils; |
| 29 | import android.text.Spanned; |
| 30 | import android.text.TextUtils; |
| 31 | import android.text.format.DateUtils; |
| 32 | import android.view.View; |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 33 | import android.widget.ImageView; |
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 | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 39 | private final Context mContext; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 40 | private final Resources mResources; |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 41 | private final String mVoicemailNumber; |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 42 | /** Icon for incoming calls. */ |
| 43 | private final Drawable mIncomingDrawable; |
| 44 | /** Icon for outgoing calls. */ |
| 45 | private final Drawable mOutgoingDrawable; |
| 46 | /** Icon for missed calls. */ |
| 47 | private final Drawable mMissedDrawable; |
| 48 | /** Icon for voicemails. */ |
| 49 | private final Drawable mVoicemailDrawable; |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 50 | /** Name used to identify incoming calls. */ |
| 51 | private final String mIncomingName; |
| 52 | /** Name used to identify outgoing calls. */ |
| 53 | private final String mOutgoingName; |
| 54 | /** Name used to identify missed calls. */ |
| 55 | private final String mMissedName; |
| 56 | /** Name used to identify voicemail calls. */ |
| 57 | private final String mVoicemailName; |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 58 | /** The injected current time in milliseconds since the epoch. Used only by tests. */ |
| 59 | private Long mCurrentTimeMillisForTest; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Creates a new instance of the helper. |
| 63 | * <p> |
| 64 | * Generally you should have a single instance of this helper in any context. |
| 65 | * |
| 66 | * @param resources used to look up strings |
| 67 | */ |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 68 | public PhoneCallDetailsHelper(Context context, Resources resources, String voicemailNumber, |
| 69 | Drawable incomingDrawable, Drawable outgoingDrawable, Drawable missedDrawable, |
| 70 | Drawable voicemailDrawable) { |
| 71 | mContext = context; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 72 | mResources = resources; |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 73 | mVoicemailNumber = voicemailNumber; |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 74 | mIncomingDrawable = incomingDrawable; |
| 75 | mOutgoingDrawable = outgoingDrawable; |
| 76 | mMissedDrawable = missedDrawable; |
| 77 | mVoicemailDrawable = voicemailDrawable; |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 78 | // Cache these values so that we do not need to look them up each time. |
| 79 | mIncomingName = mResources.getString(R.string.type_incoming); |
| 80 | mOutgoingName = mResources.getString(R.string.type_outgoing); |
| 81 | mMissedName = mResources.getString(R.string.type_missed); |
| 82 | mVoicemailName = mResources.getString(R.string.type_voicemail); |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 85 | /** Fills the call details views with content. */ |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 86 | public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details, |
| 87 | boolean useIcons) { |
| 88 | if (useIcons) { |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 89 | views.callTypeIcons.removeAllViews(); |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 90 | int count = details.callTypes.length; |
| 91 | for (int callType : details.callTypes) { |
| 92 | ImageView callTypeImage = new ImageView(mContext); |
| 93 | callTypeImage.setImageDrawable(getCallTypeDrawable(callType)); |
| 94 | views.callTypeIcons.addView(callTypeImage); |
| 95 | } |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 96 | views.callTypeIcons.setVisibility(View.VISIBLE); |
| 97 | views.callTypeText.setVisibility(View.GONE); |
| 98 | views.callTypeSeparator.setVisibility(View.GONE); |
| 99 | } else { |
| 100 | String callTypeName; |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 101 | // Use the name of the first call type. |
| 102 | // TODO: We should update this to handle the text for multiple calls as well. |
| 103 | int callType = details.callTypes[0]; |
| 104 | views.callTypeText.setText(getCallTypeText(callType)); |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 105 | views.callTypeIcons.removeAllViews(); |
| 106 | |
| 107 | views.callTypeText.setVisibility(View.VISIBLE); |
| 108 | views.callTypeSeparator.setVisibility(View.VISIBLE); |
| 109 | views.callTypeIcons.setVisibility(View.GONE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 110 | } |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 111 | |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 112 | CharSequence shortDateText = |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 113 | DateUtils.getRelativeTimeSpanString(details.date, |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 114 | getCurrentTimeMillis(), |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 115 | DateUtils.MINUTE_IN_MILLIS, |
| 116 | DateUtils.FORMAT_ABBREV_RELATIVE); |
| 117 | |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 118 | CharSequence numberFormattedLabel = null; |
| 119 | // 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] | 120 | if (!TextUtils.isEmpty(details.number) |
| 121 | && !PhoneNumberUtils.isUriNumber(details.number.toString())) { |
| 122 | numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType, |
| 123 | details.numberLabel); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 126 | final CharSequence nameText; |
| 127 | final CharSequence numberText; |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 128 | if (TextUtils.isEmpty(details.name)) { |
Flavio Lerda | 371d5f9 | 2011-07-09 19:45:32 +0100 | [diff] [blame] | 129 | nameText = getDisplayNumber(details.number, details.formattedNumber); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 130 | numberText = ""; |
| 131 | } else { |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 132 | nameText = details.name; |
Flavio Lerda | 371d5f9 | 2011-07-09 19:45:32 +0100 | [diff] [blame] | 133 | CharSequence displayNumber = getDisplayNumber(details.number, details.formattedNumber); |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 134 | if (numberFormattedLabel != null) { |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 135 | numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD, |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 136 | numberFormattedLabel + " " + displayNumber, 0, |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 137 | numberFormattedLabel.length(), |
| 138 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 139 | } else { |
| 140 | numberText = displayNumber; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 144 | views.dateView.setText(shortDateText); |
| 145 | views.dateView.setVisibility(View.VISIBLE); |
| 146 | views.nameView.setText(nameText); |
| 147 | views.nameView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 148 | // Do not show the number if it is not available. This happens if we have only the number, |
| 149 | // in which case the number is shown in the name field instead. |
| 150 | if (!TextUtils.isEmpty(numberText)) { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 151 | views.numberView.setText(numberText); |
| 152 | views.numberView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 153 | } else { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 154 | views.numberView.setVisibility(View.GONE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 155 | } |
| 156 | } |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 157 | |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 158 | /** Returns the text used to represent the given call type. */ |
| 159 | private String getCallTypeText(int callType) { |
| 160 | switch (callType) { |
| 161 | case Calls.INCOMING_TYPE: |
| 162 | return mIncomingName; |
| 163 | |
| 164 | case Calls.OUTGOING_TYPE: |
| 165 | return mOutgoingName; |
| 166 | |
| 167 | case Calls.MISSED_TYPE: |
| 168 | return mMissedName; |
| 169 | |
| 170 | case Calls.VOICEMAIL_TYPE: |
| 171 | return mVoicemailName; |
| 172 | |
| 173 | default: |
| 174 | throw new IllegalArgumentException("invalid call type: " + callType); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** Returns the drawable of the icon associated with the given call type. */ |
| 179 | private Drawable getCallTypeDrawable(int callType) { |
| 180 | switch (callType) { |
| 181 | case Calls.INCOMING_TYPE: |
| 182 | return mIncomingDrawable; |
| 183 | |
| 184 | case Calls.OUTGOING_TYPE: |
| 185 | return mOutgoingDrawable; |
| 186 | |
| 187 | case Calls.MISSED_TYPE: |
| 188 | return mMissedDrawable; |
| 189 | |
| 190 | case Calls.VOICEMAIL_TYPE: |
| 191 | return mVoicemailDrawable; |
| 192 | |
| 193 | default: |
| 194 | throw new IllegalArgumentException("invalid call type: " + callType); |
| 195 | } |
| 196 | } |
| 197 | |
Flavio Lerda | 371d5f9 | 2011-07-09 19:45:32 +0100 | [diff] [blame] | 198 | private CharSequence getDisplayNumber(CharSequence number, CharSequence formattedNumber) { |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 199 | if (TextUtils.isEmpty(number)) { |
| 200 | return ""; |
| 201 | } |
| 202 | if (number.equals(CallerInfo.UNKNOWN_NUMBER)) { |
| 203 | return mResources.getString(R.string.unknown); |
| 204 | } |
| 205 | if (number.equals(CallerInfo.PRIVATE_NUMBER)) { |
| 206 | return mResources.getString(R.string.private_num); |
| 207 | } |
| 208 | if (number.equals(CallerInfo.PAYPHONE_NUMBER)) { |
| 209 | return mResources.getString(R.string.payphone); |
| 210 | } |
| 211 | if (PhoneNumberUtils.extractNetworkPortion(number.toString()).equals(mVoicemailNumber)) { |
| 212 | return mResources.getString(R.string.voicemail); |
| 213 | } |
Flavio Lerda | 371d5f9 | 2011-07-09 19:45:32 +0100 | [diff] [blame] | 214 | if (TextUtils.isEmpty(formattedNumber)) { |
| 215 | return number; |
| 216 | } else { |
| 217 | return formattedNumber; |
| 218 | } |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 219 | } |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 220 | |
| 221 | public void setCurrentTimeForTest(long currentTimeMillis) { |
| 222 | mCurrentTimeMillisForTest = currentTimeMillis; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Returns the current time in milliseconds since the epoch. |
| 227 | * <p> |
| 228 | * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. |
| 229 | */ |
| 230 | private long getCurrentTimeMillis() { |
| 231 | if (mCurrentTimeMillisForTest == null) { |
| 232 | return System.currentTimeMillis(); |
| 233 | } else { |
| 234 | return mCurrentTimeMillisForTest; |
| 235 | } |
| 236 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 237 | } |