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 | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame^] | 72 | /** Fills the call details views with content. */ |
| 73 | public void setPhoneCallDetails(PhoneCallDetailsViews views, PhoneCallDetails details) { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 74 | Drawable callTypeDrawable = null; |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame^] | 75 | switch (details.callType) { |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 76 | case Calls.INCOMING_TYPE: |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 77 | callTypeDrawable = mIncomingDrawable; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 78 | break; |
| 79 | |
| 80 | case Calls.OUTGOING_TYPE: |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 81 | callTypeDrawable = mOutgoingDrawable; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 82 | break; |
| 83 | |
| 84 | case Calls.MISSED_TYPE: |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 85 | callTypeDrawable = mMissedDrawable; |
| 86 | break; |
| 87 | |
| 88 | case Calls.VOICEMAIL_TYPE: |
| 89 | callTypeDrawable = mVoicemailDrawable; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 90 | break; |
| 91 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 92 | CharSequence shortDateText = |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame^] | 93 | DateUtils.getRelativeTimeSpanString(details.date, |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 94 | getCurrentTimeMillis(), |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 95 | DateUtils.MINUTE_IN_MILLIS, |
| 96 | DateUtils.FORMAT_ABBREV_RELATIVE); |
| 97 | |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 98 | CharSequence numberFormattedLabel = null; |
| 99 | // 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^] | 100 | if (!TextUtils.isEmpty(details.number) |
| 101 | && !PhoneNumberUtils.isUriNumber(details.number.toString())) { |
| 102 | numberFormattedLabel = Phone.getTypeLabel(mResources, details.numberType, |
| 103 | details.numberLabel); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 106 | final CharSequence nameText; |
| 107 | final CharSequence numberText; |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame^] | 108 | if (TextUtils.isEmpty(details.name)) { |
| 109 | nameText = getDisplayNumber(details.number); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 110 | numberText = ""; |
| 111 | } else { |
Flavio Lerda | 9de3868 | 2011-07-08 20:38:07 +0100 | [diff] [blame^] | 112 | nameText = details.name; |
| 113 | CharSequence displayNumber = getDisplayNumber(details.number); |
| 114 | if (details.callType != 0 && numberFormattedLabel != null) { |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 115 | numberText = FormatUtils.applyStyleToSpan(Typeface.BOLD, |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 116 | numberFormattedLabel + " " + displayNumber, 0, |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 117 | numberFormattedLabel.length(), |
| 118 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Flavio Lerda | b9256f8 | 2011-07-09 20:10:57 +0100 | [diff] [blame] | 119 | } else { |
| 120 | numberText = displayNumber; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 124 | ImageView callTypeImage = new ImageView(mContext); |
| 125 | callTypeImage.setImageDrawable(callTypeDrawable); |
| 126 | views.callTypesLayout.removeAllViews(); |
| 127 | views.callTypesLayout.addView(callTypeImage); |
| 128 | |
| 129 | views.dateView.setText(shortDateText); |
| 130 | views.dateView.setVisibility(View.VISIBLE); |
| 131 | views.nameView.setText(nameText); |
| 132 | views.nameView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 133 | // Do not show the number if it is not available. This happens if we have only the number, |
| 134 | // in which case the number is shown in the name field instead. |
| 135 | if (!TextUtils.isEmpty(numberText)) { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 136 | views.numberView.setText(numberText); |
| 137 | views.numberView.setVisibility(View.VISIBLE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 138 | } else { |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 139 | views.numberView.setVisibility(View.GONE); |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 140 | } |
| 141 | } |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 142 | |
| 143 | private CharSequence getDisplayNumber(CharSequence number) { |
| 144 | if (TextUtils.isEmpty(number)) { |
| 145 | return ""; |
| 146 | } |
| 147 | if (number.equals(CallerInfo.UNKNOWN_NUMBER)) { |
| 148 | return mResources.getString(R.string.unknown); |
| 149 | } |
| 150 | if (number.equals(CallerInfo.PRIVATE_NUMBER)) { |
| 151 | return mResources.getString(R.string.private_num); |
| 152 | } |
| 153 | if (number.equals(CallerInfo.PAYPHONE_NUMBER)) { |
| 154 | return mResources.getString(R.string.payphone); |
| 155 | } |
| 156 | if (PhoneNumberUtils.extractNetworkPortion(number.toString()).equals(mVoicemailNumber)) { |
| 157 | return mResources.getString(R.string.voicemail); |
| 158 | } |
| 159 | return number; |
| 160 | } |
Flavio Lerda | 7d7473a | 2011-07-06 14:21:46 +0100 | [diff] [blame] | 161 | |
| 162 | public void setCurrentTimeForTest(long currentTimeMillis) { |
| 163 | mCurrentTimeMillisForTest = currentTimeMillis; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns the current time in milliseconds since the epoch. |
| 168 | * <p> |
| 169 | * It can be injected in tests using {@link #setCurrentTimeForTest(long)}. |
| 170 | */ |
| 171 | private long getCurrentTimeMillis() { |
| 172 | if (mCurrentTimeMillisForTest == null) { |
| 173 | return System.currentTimeMillis(); |
| 174 | } else { |
| 175 | return mCurrentTimeMillisForTest; |
| 176 | } |
| 177 | } |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 178 | } |