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 | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 20 | import com.android.contacts.format.FormatUtils; |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 21 | import com.android.internal.telephony.CallerInfo; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 22 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 23 | import android.content.Context; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 24 | import android.content.res.Resources; |
| 25 | import android.graphics.Typeface; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 26 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 27 | import android.telephony.PhoneNumberUtils; |
| 28 | import android.text.Spanned; |
| 29 | import android.text.TextUtils; |
| 30 | import android.text.format.DateUtils; |
| 31 | import android.view.View; |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 32 | import android.widget.ImageView; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Helper class to fill in the views in {@link PhoneCallDetailsViews}. |
| 36 | */ |
| 37 | public class PhoneCallDetailsHelper { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 38 | private final Context mContext; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 39 | private final Resources mResources; |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 40 | private final String mVoicemailNumber; |
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 | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 43 | private final CallTypeHelper mCallTypeHelper; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Creates a new instance of the helper. |
| 47 | * <p> |
| 48 | * Generally you should have a single instance of this helper in any context. |
| 49 | * |
| 50 | * @param resources used to look up strings |
| 51 | */ |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 52 | public PhoneCallDetailsHelper(Context context, Resources resources, String voicemailNumber, |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 53 | CallTypeHelper callTypeHelper) { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 54 | mContext = context; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 55 | mResources = resources; |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 56 | mVoicemailNumber = voicemailNumber; |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 57 | mCallTypeHelper = callTypeHelper; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 60 | /** Fills the call details views with content. */ |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 61 | public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details, |
| 62 | boolean useIcons) { |
| 63 | if (useIcons) { |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 64 | views.callTypeIcons.removeAllViews(); |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 65 | int count = details.callTypes.length; |
| 66 | for (int callType : details.callTypes) { |
| 67 | ImageView callTypeImage = new ImageView(mContext); |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 68 | callTypeImage.setImageDrawable(mCallTypeHelper.getCallTypeDrawable(callType)); |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 69 | views.callTypeIcons.addView(callTypeImage); |
| 70 | } |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 71 | views.callTypeIcons.setVisibility(View.VISIBLE); |
| 72 | views.callTypeText.setVisibility(View.GONE); |
| 73 | views.callTypeSeparator.setVisibility(View.GONE); |
| 74 | } else { |
| 75 | String callTypeName; |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 76 | // Use the name of the first call type. |
| 77 | // TODO: We should update this to handle the text for multiple calls as well. |
| 78 | int callType = details.callTypes[0]; |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 79 | views.callTypeText.setText(mCallTypeHelper.getCallTypeText(callType)); |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 80 | views.callTypeIcons.removeAllViews(); |
| 81 | |
| 82 | views.callTypeText.setVisibility(View.VISIBLE); |
| 83 | views.callTypeSeparator.setVisibility(View.VISIBLE); |
| 84 | views.callTypeIcons.setVisibility(View.GONE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 85 | } |
Flavio Lerda | 4586feb | 2011-07-09 17:03:48 +0100 | [diff] [blame] | 86 | |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 87 | CharSequence shortDateText = |
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 | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 93 | CharSequence numberFormattedLabel = null; |
| 94 | // 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] | 95 | if (!TextUtils.isEmpty(details.number) |
| 96 | && !PhoneNumberUtils.isUriNumber(details.number.toString())) { |
| 97 | numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType, |
| 98 | details.numberLabel); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 101 | final CharSequence nameText; |
| 102 | final CharSequence numberText; |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 103 | if (TextUtils.isEmpty(details.name)) { |
Flavio Lerda | 371d5f9 | 2011-07-09 19:45:32 +0100 | [diff] [blame] | 104 | nameText = getDisplayNumber(details.number, details.formattedNumber); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 105 | numberText = ""; |
| 106 | } else { |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame] | 107 | nameText = details.name; |
Flavio Lerda | 371d5f9 | 2011-07-09 19:45:32 +0100 | [diff] [blame] | 108 | CharSequence displayNumber = getDisplayNumber(details.number, details.formattedNumber); |
Flavio Lerda | 223a843 | 2011-07-11 18:40:25 +0100 | [diff] [blame] | 109 | if (numberFormattedLabel != null) { |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 110 | numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD, |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 111 | numberFormattedLabel + " " + displayNumber, 0, |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 112 | numberFormattedLabel.length(), |
| 113 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 114 | } else { |
| 115 | numberText = displayNumber; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 119 | views.dateView.setText(shortDateText); |
| 120 | views.dateView.setVisibility(View.VISIBLE); |
| 121 | views.nameView.setText(nameText); |
| 122 | views.nameView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 123 | // Do not show the number if it is not available. This happens if we have only the number, |
| 124 | // in which case the number is shown in the name field instead. |
| 125 | if (!TextUtils.isEmpty(numberText)) { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 126 | views.numberView.setText(numberText); |
| 127 | views.numberView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 128 | } else { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 129 | views.numberView.setVisibility(View.GONE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 130 | } |
| 131 | } |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 132 | |
Flavio Lerda | 371d5f9 | 2011-07-09 19:45:32 +0100 | [diff] [blame] | 133 | private CharSequence getDisplayNumber(CharSequence number, CharSequence formattedNumber) { |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 134 | if (TextUtils.isEmpty(number)) { |
| 135 | return ""; |
| 136 | } |
| 137 | if (number.equals(CallerInfo.UNKNOWN_NUMBER)) { |
| 138 | return mResources.getString(R.string.unknown); |
| 139 | } |
| 140 | if (number.equals(CallerInfo.PRIVATE_NUMBER)) { |
| 141 | return mResources.getString(R.string.private_num); |
| 142 | } |
| 143 | if (number.equals(CallerInfo.PAYPHONE_NUMBER)) { |
| 144 | return mResources.getString(R.string.payphone); |
| 145 | } |
| 146 | if (PhoneNumberUtils.extractNetworkPortion(number.toString()).equals(mVoicemailNumber)) { |
| 147 | return mResources.getString(R.string.voicemail); |
| 148 | } |
Flavio Lerda | 371d5f9 | 2011-07-09 19:45:32 +0100 | [diff] [blame] | 149 | if (TextUtils.isEmpty(formattedNumber)) { |
| 150 | return number; |
| 151 | } else { |
| 152 | return formattedNumber; |
| 153 | } |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 154 | } |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 155 | |
| 156 | public void setCurrentTimeForTest(long currentTimeMillis) { |
| 157 | mCurrentTimeMillisForTest = currentTimeMillis; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns the current time in milliseconds since the epoch. |
| 162 | * <p> |
| 163 | * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. |
| 164 | */ |
| 165 | private long getCurrentTimeMillis() { |
| 166 | if (mCurrentTimeMillisForTest == null) { |
| 167 | return System.currentTimeMillis(); |
| 168 | } else { |
| 169 | return mCurrentTimeMillisForTest; |
| 170 | } |
| 171 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 172 | } |