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) { |
| 89 | final Drawable callTypeDrawable; |
| 90 | switch (details.callType) { |
| 91 | case Calls.INCOMING_TYPE: |
| 92 | callTypeDrawable = mIncomingDrawable; |
| 93 | break; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 94 | |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 95 | case Calls.OUTGOING_TYPE: |
| 96 | callTypeDrawable = mOutgoingDrawable; |
| 97 | break; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 98 | |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 99 | case Calls.MISSED_TYPE: |
| 100 | callTypeDrawable = mMissedDrawable; |
| 101 | break; |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 102 | |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 103 | case Calls.VOICEMAIL_TYPE: |
| 104 | callTypeDrawable = mVoicemailDrawable; |
| 105 | break; |
| 106 | |
| 107 | default: |
| 108 | throw new IllegalArgumentException("invalid call type: " + details.callType); |
| 109 | } |
| 110 | ImageView callTypeImage = new ImageView(mContext); |
| 111 | callTypeImage.setImageDrawable(callTypeDrawable); |
| 112 | views.callTypeIcons.removeAllViews(); |
| 113 | views.callTypeIcons.addView(callTypeImage); |
| 114 | |
| 115 | views.callTypeIcons.setVisibility(View.VISIBLE); |
| 116 | views.callTypeText.setVisibility(View.GONE); |
| 117 | views.callTypeSeparator.setVisibility(View.GONE); |
| 118 | } else { |
| 119 | String callTypeName; |
| 120 | switch (details.callType) { |
| 121 | case Calls.INCOMING_TYPE: |
| 122 | callTypeName = mIncomingName; |
| 123 | break; |
| 124 | |
| 125 | case Calls.OUTGOING_TYPE: |
| 126 | callTypeName = mOutgoingName; |
| 127 | break; |
| 128 | |
| 129 | case Calls.MISSED_TYPE: |
| 130 | callTypeName = mMissedName; |
| 131 | break; |
| 132 | |
| 133 | case Calls.VOICEMAIL_TYPE: |
| 134 | callTypeName = mVoicemailName; |
| 135 | break; |
| 136 | |
| 137 | default: |
| 138 | throw new IllegalArgumentException("invalid call type: " + details.callType); |
| 139 | } |
| 140 | views.callTypeText.setText(callTypeName); |
| 141 | views.callTypeIcons.removeAllViews(); |
| 142 | |
| 143 | views.callTypeText.setVisibility(View.VISIBLE); |
| 144 | views.callTypeSeparator.setVisibility(View.VISIBLE); |
| 145 | views.callTypeIcons.setVisibility(View.GONE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 146 | } |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 147 | |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 148 | CharSequence shortDateText = |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 149 | DateUtils.getRelativeTimeSpanString(details.date, |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 150 | getCurrentTimeMillis(), |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 151 | DateUtils.MINUTE_IN_MILLIS, |
| 152 | DateUtils.FORMAT_ABBREV_RELATIVE); |
| 153 | |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 154 | CharSequence numberFormattedLabel = null; |
| 155 | // 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] | 156 | if (!TextUtils.isEmpty(details.number) |
| 157 | && !PhoneNumberUtils.isUriNumber(details.number.toString())) { |
| 158 | numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType, |
| 159 | details.numberLabel); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 162 | final CharSequence nameText; |
| 163 | final CharSequence numberText; |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 164 | if (TextUtils.isEmpty(details.name)) { |
Flavio Lerda | 371d5f9 | 2011-07-09 19:45:32 +0100 | [diff] [blame] | 165 | nameText = getDisplayNumber(details.number, details.formattedNumber); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 166 | numberText = ""; |
| 167 | } else { |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 168 | nameText = details.name; |
Flavio Lerda | 371d5f9 | 2011-07-09 19:45:32 +0100 | [diff] [blame] | 169 | CharSequence displayNumber = getDisplayNumber(details.number, details.formattedNumber); |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 170 | if (details.callType != 0 && numberFormattedLabel != null) { |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 171 | numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD, |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 172 | numberFormattedLabel + " " + displayNumber, 0, |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 173 | numberFormattedLabel.length(), |
| 174 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 175 | } else { |
| 176 | numberText = displayNumber; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 180 | views.dateView.setText(shortDateText); |
| 181 | views.dateView.setVisibility(View.VISIBLE); |
| 182 | views.nameView.setText(nameText); |
| 183 | views.nameView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 184 | // Do not show the number if it is not available. This happens if we have only the number, |
| 185 | // in which case the number is shown in the name field instead. |
| 186 | if (!TextUtils.isEmpty(numberText)) { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 187 | views.numberView.setText(numberText); |
| 188 | views.numberView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 189 | } else { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 190 | views.numberView.setVisibility(View.GONE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 191 | } |
| 192 | } |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 193 | |
Flavio Lerda | 371d5f9 | 2011-07-09 19:45:32 +0100 | [diff] [blame] | 194 | private CharSequence getDisplayNumber(CharSequence number, CharSequence formattedNumber) { |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 195 | if (TextUtils.isEmpty(number)) { |
| 196 | return ""; |
| 197 | } |
| 198 | if (number.equals(CallerInfo.UNKNOWN_NUMBER)) { |
| 199 | return mResources.getString(R.string.unknown); |
| 200 | } |
| 201 | if (number.equals(CallerInfo.PRIVATE_NUMBER)) { |
| 202 | return mResources.getString(R.string.private_num); |
| 203 | } |
| 204 | if (number.equals(CallerInfo.PAYPHONE_NUMBER)) { |
| 205 | return mResources.getString(R.string.payphone); |
| 206 | } |
| 207 | if (PhoneNumberUtils.extractNetworkPortion(number.toString()).equals(mVoicemailNumber)) { |
| 208 | return mResources.getString(R.string.voicemail); |
| 209 | } |
Flavio Lerda | 371d5f9 | 2011-07-09 19:45:32 +0100 | [diff] [blame] | 210 | if (TextUtils.isEmpty(formattedNumber)) { |
| 211 | return number; |
| 212 | } else { |
| 213 | return formattedNumber; |
| 214 | } |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 215 | } |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 216 | |
| 217 | public void setCurrentTimeForTest(long currentTimeMillis) { |
| 218 | mCurrentTimeMillisForTest = currentTimeMillis; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Returns the current time in milliseconds since the epoch. |
| 223 | * <p> |
| 224 | * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. |
| 225 | */ |
| 226 | private long getCurrentTimeMillis() { |
| 227 | if (mCurrentTimeMillisForTest == null) { |
| 228 | return System.currentTimeMillis(); |
| 229 | } else { |
| 230 | return mCurrentTimeMillisForTest; |
| 231 | } |
| 232 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 233 | } |