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; |
| 50 | /** The injected current time in milliseconds since the epoch. Used only by tests. */ |
| 51 | private Long mCurrentTimeMillisForTest; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Creates a new instance of the helper. |
| 55 | * <p> |
| 56 | * Generally you should have a single instance of this helper in any context. |
| 57 | * |
| 58 | * @param resources used to look up strings |
| 59 | */ |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 60 | public PhoneCallDetailsHelper(Context context, Resources resources, String voicemailNumber, |
| 61 | Drawable incomingDrawable, Drawable outgoingDrawable, Drawable missedDrawable, |
| 62 | Drawable voicemailDrawable) { |
| 63 | mContext = context; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 64 | mResources = resources; |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 65 | mVoicemailNumber = voicemailNumber; |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 66 | mIncomingDrawable = incomingDrawable; |
| 67 | mOutgoingDrawable = outgoingDrawable; |
| 68 | mMissedDrawable = missedDrawable; |
| 69 | mVoicemailDrawable = voicemailDrawable; |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 72 | /** |
| 73 | * Fills the call details views with content. |
| 74 | * |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 75 | * @param date the date of the call, in milliseconds since the epoch |
| 76 | * @param callType the type of call, as defined in the call log table |
| 77 | * @param name the name of the contact, if available |
| 78 | * @param number the number of the other party involved in the call |
| 79 | * @param numberType the type of phone, e.g., {@link Phone#TYPE_HOME}, 0 if not available |
| 80 | * @param numberLabel the custom label associated with the phone number in the contact |
| 81 | */ |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 82 | public void setPhoneCallDetails(PhoneCallDetailsViews views, long date, |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 83 | int callType, CharSequence name, CharSequence number, int numberType, |
| 84 | CharSequence numberLabel) { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 85 | Drawable callTypeDrawable = null; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 86 | switch (callType) { |
| 87 | case Calls.INCOMING_TYPE: |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 88 | callTypeDrawable = mIncomingDrawable; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 89 | break; |
| 90 | |
| 91 | case Calls.OUTGOING_TYPE: |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 92 | callTypeDrawable = mOutgoingDrawable; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 93 | break; |
| 94 | |
| 95 | case Calls.MISSED_TYPE: |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 96 | callTypeDrawable = mMissedDrawable; |
| 97 | break; |
| 98 | |
| 99 | case Calls.VOICEMAIL_TYPE: |
| 100 | callTypeDrawable = mVoicemailDrawable; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 101 | break; |
| 102 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 103 | CharSequence shortDateText = |
| 104 | DateUtils.getRelativeTimeSpanString(date, |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 105 | getCurrentTimeMillis(), |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 106 | DateUtils.MINUTE_IN_MILLIS, |
| 107 | DateUtils.FORMAT_ABBREV_RELATIVE); |
| 108 | |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 109 | CharSequence numberFormattedLabel = null; |
| 110 | // Only show a label if the number is shown and it is not a SIP address. |
| 111 | if (!TextUtils.isEmpty(number) && !PhoneNumberUtils.isUriNumber(number.toString())) { |
Flavio Lerda | d72bf8a | 2011-07-05 16:00:09 +0100 | [diff] [blame] | 112 | numberFormattedLabel = Phone.getTypeLabel(mResources, numberType, numberLabel); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 115 | final CharSequence nameText; |
| 116 | final CharSequence numberText; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 117 | if (TextUtils.isEmpty(name)) { |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 118 | nameText = getDisplayNumber(number); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 119 | numberText = ""; |
| 120 | } else { |
| 121 | nameText = name; |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 122 | CharSequence displayNumber = getDisplayNumber(number); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 123 | if (callType != 0 && numberFormattedLabel != null) { |
| 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 | ImageView callTypeImage = new ImageView(mContext); |
| 134 | callTypeImage.setImageDrawable(callTypeDrawable); |
| 135 | views.callTypesLayout.removeAllViews(); |
| 136 | views.callTypesLayout.addView(callTypeImage); |
| 137 | |
| 138 | views.dateView.setText(shortDateText); |
| 139 | views.dateView.setVisibility(View.VISIBLE); |
| 140 | views.nameView.setText(nameText); |
| 141 | views.nameView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 142 | // Do not show the number if it is not available. This happens if we have only the number, |
| 143 | // in which case the number is shown in the name field instead. |
| 144 | if (!TextUtils.isEmpty(numberText)) { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 145 | views.numberView.setText(numberText); |
| 146 | views.numberView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 147 | } else { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 148 | views.numberView.setVisibility(View.GONE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 151 | |
| 152 | private CharSequence getDisplayNumber(CharSequence number) { |
| 153 | if (TextUtils.isEmpty(number)) { |
| 154 | return ""; |
| 155 | } |
| 156 | if (number.equals(CallerInfo.UNKNOWN_NUMBER)) { |
| 157 | return mResources.getString(R.string.unknown); |
| 158 | } |
| 159 | if (number.equals(CallerInfo.PRIVATE_NUMBER)) { |
| 160 | return mResources.getString(R.string.private_num); |
| 161 | } |
| 162 | if (number.equals(CallerInfo.PAYPHONE_NUMBER)) { |
| 163 | return mResources.getString(R.string.payphone); |
| 164 | } |
| 165 | if (PhoneNumberUtils.extractNetworkPortion(number.toString()).equals(mVoicemailNumber)) { |
| 166 | return mResources.getString(R.string.voicemail); |
| 167 | } |
| 168 | return number; |
| 169 | } |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 170 | |
| 171 | public void setCurrentTimeForTest(long currentTimeMillis) { |
| 172 | mCurrentTimeMillisForTest = currentTimeMillis; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns the current time in milliseconds since the epoch. |
| 177 | * <p> |
| 178 | * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. |
| 179 | */ |
| 180 | private long getCurrentTimeMillis() { |
| 181 | if (mCurrentTimeMillisForTest == null) { |
| 182 | return System.currentTimeMillis(); |
| 183 | } else { |
| 184 | return mCurrentTimeMillisForTest; |
| 185 | } |
| 186 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 187 | } |