blob: d411927e24f52a2ea4aae669445b0a51e3ab3acc [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2009 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
17package com.android.contacts;
18
Flavio Lerda9a208cc2011-07-12 21:05:47 +010019import com.android.contacts.calllog.CallDetailHistoryAdapter;
20import com.android.contacts.calllog.CallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010021import com.android.contacts.calllog.PhoneNumberHelper;
Hugo Hudsonb002f512011-07-15 17:41:12 +010022import com.android.contacts.voicemail.VoicemailPlaybackFragment;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010023import com.android.contacts.voicemail.VoicemailStatusHelper;
24import com.android.contacts.voicemail.VoicemailStatusHelper.StatusMessage;
25import com.android.contacts.voicemail.VoicemailStatusHelperImpl;
Amith Yamasani8bbe2f22009-03-24 21:24:12 -070026
Hugo Hudsonb002f512011-07-15 17:41:12 +010027import android.app.FragmentManager;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080028import android.app.ListActivity;
29import android.content.ContentResolver;
30import android.content.ContentUris;
31import android.content.Context;
32import android.content.Intent;
33import android.content.res.Resources;
34import android.database.Cursor;
35import android.net.Uri;
36import android.os.Bundle;
37import android.provider.CallLog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080038import android.provider.CallLog.Calls;
Flavio Lerda9cafe472011-06-08 14:06:13 +010039import android.provider.Contacts.Intents.Insert;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070040import android.provider.ContactsContract.Contacts;
41import android.provider.ContactsContract.PhoneLookup;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080042import android.telephony.PhoneNumberUtils;
43import android.telephony.TelephonyManager;
44import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010045import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080046import android.view.KeyEvent;
47import android.view.LayoutInflater;
48import android.view.View;
49import android.view.ViewGroup;
50import android.widget.AdapterView;
51import android.widget.BaseAdapter;
52import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010053import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080054import android.widget.TextView;
55import android.widget.Toast;
56
57import java.util.ArrayList;
58import java.util.List;
59
60/**
61 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010062 * <p>
63 * This activity can be either started with the URI of a single call log entry, or with the
64 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080065 */
66public class CallDetailActivity extends ListActivity implements
67 AdapterView.OnItemClickListener {
68 private static final String TAG = "CallDetail";
69
Flavio Lerda178eeeb2011-07-11 19:51:40 +010070 /** A long array extra containing ids of call log entries to display. */
Hugo Hudsonb002f512011-07-15 17:41:12 +010071 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
72 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
73 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
74 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
75 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
Flavio Lerda178eeeb2011-07-11 19:51:40 +010076
Flavio Lerdaa024c3f2011-06-10 10:47:07 +010077 /** The views representing the details of a phone call. */
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010078 private PhoneCallDetailsViews mPhoneCallDetailsViews;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010079 private CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010080 private PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010081 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
Flavio Lerdafb63c432011-07-07 17:16:53 +010082 private View mHomeActionView;
83 private ImageView mMainActionView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010084 private ImageView mContactBackgroundView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080085
86 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080087 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070088
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080089 /* package */ LayoutInflater mInflater;
90 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010091 /** Helper to load contact photos. */
92 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010093 /** Helper to make async queries to content resolver. */
94 private CallDetailActivityQueryHandler mAsyncQueryHandler;
95 /** Helper to get voicemail status messages. */
96 private VoicemailStatusHelper mVoicemailStatusHelper;
97 // Views related to voicemail status message.
98 private View mStatusMessageView;
99 private TextView mStatusMessageText;
100 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700101
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800102 static final String[] CALL_LOG_PROJECTION = new String[] {
103 CallLog.Calls.DATE,
104 CallLog.Calls.DURATION,
105 CallLog.Calls.NUMBER,
106 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800107 CallLog.Calls.COUNTRY_ISO,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800108 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700109
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800110 static final int DATE_COLUMN_INDEX = 0;
111 static final int DURATION_COLUMN_INDEX = 1;
112 static final int NUMBER_COLUMN_INDEX = 2;
113 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800114 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700115
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800116 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700117 PhoneLookup._ID,
118 PhoneLookup.DISPLAY_NAME,
119 PhoneLookup.TYPE,
120 PhoneLookup.LABEL,
121 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800122 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700123 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800124 };
125 static final int COLUMN_INDEX_ID = 0;
126 static final int COLUMN_INDEX_NAME = 1;
127 static final int COLUMN_INDEX_TYPE = 2;
128 static final int COLUMN_INDEX_LABEL = 3;
129 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800130 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700131 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800132
133 @Override
134 protected void onCreate(Bundle icicle) {
135 super.onCreate(icicle);
136
137 setContentView(R.layout.call_detail);
138
139 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
140 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700141
Flavio Lerda696e8132011-07-05 19:00:23 +0100142 mPhoneCallDetailsViews = PhoneCallDetailsViews.fromView(getWindow().getDecorView());
Flavio Lerda9a208cc2011-07-12 21:05:47 +0100143 mCallTypeHelper = new CallTypeHelper(getResources(),
Flavio Lerda693d1f82011-07-13 18:20:46 +0100144 getResources().getDrawable(R.drawable.ic_call_incoming_holo_dark),
145 getResources().getDrawable(R.drawable.ic_call_outgoing_holo_dark),
146 getResources().getDrawable(R.drawable.ic_call_missed_holo_dark),
147 getResources().getDrawable(R.drawable.ic_call_voicemail_holo_dark));
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100148 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
149 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(this, mResources, mCallTypeHelper,
150 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100151 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
152 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
153 mStatusMessageView = findViewById(R.id.voicemail_status);
154 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
155 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100156 mHomeActionView = findViewById(R.id.action_bar_home);
157 mMainActionView = (ImageView) findViewById(R.id.main_action);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100158 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800159 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100160 mContactPhotoManager = ContactPhotoManager.getInstance(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800161 getListView().setOnItemClickListener(this);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100162 mHomeActionView.setOnClickListener(new View.OnClickListener() {
163 @Override
164 public void onClick(View v) {
165 // We want this to start the call log if this activity was not started from the
166 // call log itself.
167 CallDetailActivity.this.finish();
168 }
169 });
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800170 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700171
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100172
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800173 @Override
174 public void onResume() {
175 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100176 updateData(getCallLogEntryUris());
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100177 Uri voicemailUri = getIntent().getExtras().getParcelable(EXTRA_VOICEMAIL_URI);
178 optionallyHandleVoicemail(voicemailUri);
179 if (voicemailUri != null) {
180 mAsyncQueryHandler.startVoicemailStatusQuery(voicemailUri);
181 } else {
182 mStatusMessageView.setVisibility(View.GONE);
183 }
Hugo Hudsonb002f512011-07-15 17:41:12 +0100184 }
185
186 /**
187 * Handle voicemail playback or hide voicemail ui.
188 * <p>
189 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
190 * playback. If it doesn't, then hide the voicemail ui.
191 */
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100192 private void optionallyHandleVoicemail(Uri voicemailUri) {
Hugo Hudsonb002f512011-07-15 17:41:12 +0100193 FragmentManager manager = getFragmentManager();
194 VoicemailPlaybackFragment fragment = (VoicemailPlaybackFragment) manager.findFragmentById(
195 R.id.voicemail_playback_fragment);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100196 if (voicemailUri == null) {
197 // No voicemail uri: hide the voicemail fragment.
198 manager.beginTransaction().hide(fragment).commit();
199 } else {
200 // A voicemail: extra tells us if we should start playback or not.
201 boolean startPlayback = getIntent().getExtras().getBoolean(
202 EXTRA_VOICEMAIL_START_PLAYBACK, false);
203 fragment.setVoicemailUri(voicemailUri, startPlayback);
204 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100205 }
206
207 /**
208 * Returns the list of URIs to show.
209 * <p>
210 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
211 * a list of ids in the call log added as an extra on the URI.
212 * <p>
213 * If both are available, the data on the intent takes precedence.
214 */
215 private Uri[] getCallLogEntryUris() {
216 Uri uri = getIntent().getData();
217 if (uri != null) {
218 // If there is a data on the intent, it takes precedence over the extra.
219 return new Uri[]{ uri };
220 }
221 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
222 Uri[] uris = new Uri[ids.length];
223 for (int index = 0; index < ids.length; ++index) {
224 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
225 }
226 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800227 }
228
229 @Override
230 public boolean onKeyDown(int keyCode, KeyEvent event) {
231 switch (keyCode) {
232 case KeyEvent.KEYCODE_CALL: {
233 // Make sure phone isn't already busy before starting direct call
234 TelephonyManager tm = (TelephonyManager)
235 getSystemService(Context.TELEPHONY_SERVICE);
236 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
237 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
238 Uri.fromParts("tel", mNumber, null));
239 startActivity(callIntent);
240 return true;
241 }
242 }
243 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700244
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800245 return super.onKeyDown(keyCode, event);
246 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700247
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800248 /**
249 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700250 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100251 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800252 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100253 private void updateData(final Uri... callUris) {
254 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
255 final int numCalls = callUris.length;
256 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
257 try {
258 for (int index = 0; index < numCalls; ++index) {
259 details[index] = getPhoneCallDetailsForUri(callUris[index]);
260 }
261 } catch (IllegalArgumentException e) {
262 // Something went wrong reading in our primary data, so we're going to
263 // bail out and show error to users.
264 Log.w(TAG, "invalid URI starting call details", e);
265 Toast.makeText(this, R.string.toast_call_detail_error,
266 Toast.LENGTH_SHORT).show();
267 finish();
268 return;
269 }
270
271 // We know that all calls are from the same number and the same contact, so pick the first.
272 mNumber = details[0].number.toString();
273 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700274 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100275
276 // Set the details header, based on the first phone call.
277 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda76921902011-07-13 21:11:51 +0100278 details[0], false, false);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100279
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100280 // Cache the details about the phone number.
281 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
282 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
283 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
284 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
285
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100286 // Let user view contact details if they exist, otherwise add option to create new contact
287 // from this number.
288 final Intent mainActionIntent;
289 final int mainActionIcon;
290
291 if (details[0].personId != -1) {
292 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
293 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
294 mainActionIcon = R.drawable.sym_action_view_contact;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100295 } else if (isVoicemailNumber) {
296 mainActionIntent = null;
297 mainActionIcon = 0;
298 } else if (isSipNumber) {
299 // TODO: This item is currently disabled for SIP addresses, because
300 // the Insert.PHONE extra only works correctly for PSTN numbers.
301 //
302 // To fix this for SIP addresses, we need to:
303 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
304 // the current number is a SIP address
305 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
306 // updating the SipAddress field
307 // and then we can remove the "!isSipNumber" check above.
308 mainActionIntent = null;
309 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100310 } else {
311 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
312 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
313 mainActionIntent.putExtra(Insert.PHONE, mNumber);
314 mainActionIcon = R.drawable.sym_action_add;
315 }
316
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100317 if (mainActionIntent == null) {
318 mMainActionView.setVisibility(View.INVISIBLE);
319 } else {
320 mMainActionView.setVisibility(View.VISIBLE);
321 mMainActionView.setImageResource(mainActionIcon);
322 mMainActionView.setOnClickListener(new View.OnClickListener() {
323 @Override
324 public void onClick(View v) {
325 startActivity(mainActionIntent);
326 }
327 });
328 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100329
330 // Build list of various available actions.
331 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
332
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100333 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100334 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100335 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
336 getString(R.string.menu_callNumber, mNumber),
337 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
338 }
339
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100340 // This action allows to send an SMS to the number that placed the call.
341 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100342 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
343 Uri.fromParts("sms", mNumber, null));
344 actions.add(new ViewEntry(R.drawable.sym_action_sms,
345 getString(R.string.menu_sendTextMessage), smsIntent));
346 }
347
348 // This action deletes all elements in the group from the call log.
349 actions.add(new ViewEntry(android.R.drawable.ic_menu_close_clear_cancel,
350 getString(R.string.recentCalls_removeFromRecentList),
351 new View.OnClickListener() {
352 @Override
353 public void onClick(View v) {
354 StringBuilder callIds = new StringBuilder();
355 for (Uri callUri : callUris) {
356 if (callIds.length() != 0) {
357 callIds.append(",");
358 }
359 callIds.append(ContentUris.parseId(callUri));
360 }
361
362 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
363 Calls._ID + " IN (" + callIds + ")", null);
364 finish();
365 }
366 }));
367
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100368 if (canPlaceCallsTo && !isSipNumber && !isVoicemailNumber) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100369 // "Edit the number before calling" is only available for PSTN numbers.
370 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
371 getString(R.string.recentCalls_editNumberBeforeCall),
372 new Intent(Intent.ACTION_DIAL, numberCallUri)));
373 }
374
375 // Set the actions for this phone number.
376 setListAdapter(new ViewAdapter(this, actions));
377
378 ListView historyList = (ListView) findViewById(R.id.history);
379 historyList.setAdapter(
380 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700381 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100382 }
383
384 /** Return the phone call details for a given call log URI. */
385 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800386 ContentResolver resolver = getContentResolver();
387 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
388 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100389 if (callCursor == null || !callCursor.moveToFirst()) {
390 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800391 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100392
393 // Read call log specifics.
394 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
395 long date = callCursor.getLong(DATE_COLUMN_INDEX);
396 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
397 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
398 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
399 if (TextUtils.isEmpty(countryIso)) {
400 countryIso = mDefaultCountryIso;
401 }
402
403 // Formatted phone number.
404 final CharSequence numberText;
405 // Read contact specifics.
406 CharSequence nameText = "";
407 int numberType = 0;
408 CharSequence numberLabel = "";
409 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700410 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100411 // If this is not a regular number, there is no point in looking it up in the contacts.
412 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
413 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
414 } else {
415 // Perform a reverse-phonebook lookup to find the contact details.
416 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
417 Uri.encode(number));
418 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
419 String candidateNumberText = number;
420 try {
421 if (phonesCursor != null && phonesCursor.moveToFirst()) {
422 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
423 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700424 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
425 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100426 candidateNumberText = PhoneNumberUtils.formatNumber(
427 phonesCursor.getString(COLUMN_INDEX_NUMBER),
428 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
429 countryIso);
430 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
431 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
432 } else {
433 // We could not find this contact in the contacts, just format the phone
434 // number as best as we can. All the other fields will have their default
435 // values.
436 candidateNumberText =
437 PhoneNumberUtils.formatNumber(number, countryIso);
438 }
439 } finally {
440 if (phonesCursor != null) phonesCursor.close();
441 numberText = candidateNumberText;
442 }
443 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100444 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100445 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700446 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800447 } finally {
448 if (callCursor != null) {
449 callCursor.close();
450 }
451 }
452 }
453
Flavio Lerda9cafe472011-06-08 14:06:13 +0100454 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700455 private void loadContactPhotos(Uri photoUri) {
456 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100457 }
458
Flavio Lerda696e8132011-07-05 19:00:23 +0100459 private String getVoicemailNumber() {
460 TelephonyManager telephonyManager =
461 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
462 return telephonyManager.getVoiceMailNumber();
463 }
464
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800465 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100466 public final int icon;
467 public final String text;
468 public final Intent intent;
469 public final View.OnClickListener action;
470
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800471 public String label = null;
472 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700473
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800474 public ViewEntry(int icon, String text, Intent intent) {
475 this.icon = icon;
476 this.text = text;
477 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100478 this.action = null;
479 }
480
481 public ViewEntry(int icon, String text, View.OnClickListener listener) {
482 this.icon = icon;
483 this.text = text;
484 this.intent = null;
485 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800486 }
487 }
488
489 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700490
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800491 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700492
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800493 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700494
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800495 public ViewAdapter(Context context, List<ViewEntry> actions) {
496 mActions = actions;
497 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
498 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700499
Flavio Lerda9cafe472011-06-08 14:06:13 +0100500 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800501 public int getCount() {
502 return mActions.size();
503 }
504
Flavio Lerda9cafe472011-06-08 14:06:13 +0100505 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800506 public Object getItem(int position) {
507 return mActions.get(position);
508 }
509
Flavio Lerda9cafe472011-06-08 14:06:13 +0100510 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800511 public long getItemId(int position) {
512 return position;
513 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700514
Flavio Lerda9cafe472011-06-08 14:06:13 +0100515 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800516 public View getView(int position, View convertView, ViewGroup parent) {
517 // Make sure we have a valid convertView to start with
518 if (convertView == null) {
519 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
520 }
521
522 // Fill action with icon and text.
523 ViewEntry entry = mActions.get(position);
524 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700525
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800526 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
527 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
528
529 icon.setImageResource(entry.icon);
530 text.setText(entry.text);
531
532 View line2 = convertView.findViewById(R.id.line2);
533 boolean numberEmpty = TextUtils.isEmpty(entry.number);
534 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
535 if (labelEmpty && numberEmpty) {
536 line2.setVisibility(View.GONE);
537 } else {
538 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700539
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800540 TextView label = (TextView) convertView.findViewById(R.id.label);
541 if (labelEmpty) {
542 label.setVisibility(View.GONE);
543 } else {
544 label.setText(entry.label);
545 label.setVisibility(View.VISIBLE);
546 }
547
548 TextView number = (TextView) convertView.findViewById(R.id.number);
549 number.setText(entry.number);
550 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700551
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800552 return convertView;
553 }
554 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700555
Flavio Lerda9cafe472011-06-08 14:06:13 +0100556 @Override
557 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800558 // Handle passing action off to correct handler.
559 if (view.getTag() instanceof ViewEntry) {
560 ViewEntry entry = (ViewEntry) view.getTag();
561 if (entry.intent != null) {
562 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100563 } else if (entry.action != null) {
564 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800565 }
566 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700567 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800568
569 @Override
570 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
571 boolean globalSearch) {
572 if (globalSearch) {
573 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
574 } else {
575 ContactsSearchManager.startSearch(this, initialQuery);
576 }
577 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100578
579 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
580 if (statusCursor == null) {
581 mStatusMessageView.setVisibility(View.GONE);
582 return;
583 }
584 final StatusMessage message = getStatusMessage(statusCursor);
585 if (message == null || !message.showInCallDetails()) {
586 mStatusMessageView.setVisibility(View.GONE);
587 return;
588 }
589
590 mStatusMessageView.setVisibility(View.VISIBLE);
591 mStatusMessageText.setText(message.callDetailsMessageId);
592 if (message.actionMessageId != -1) {
593 mStatusMessageAction.setText(message.actionMessageId);
594 }
595 if (message.actionUri != null) {
596 mStatusMessageAction.setClickable(true);
597 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
598 @Override
599 public void onClick(View v) {
600 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
601 }
602 });
603 } else {
604 mStatusMessageAction.setClickable(false);
605 }
606 }
607
608 private StatusMessage getStatusMessage(Cursor statusCursor) {
609 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
610 Log.d(TAG, "Num status messages: " + messages.size());
611 if (messages.size() == 0) {
612 return null;
613 }
614 // There can only be a single status message per source package, so num of messages can
615 // at most be 1.
616 if (messages.size() > 1) {
617 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
618 " Will use the first one.", messages.size()));
619 }
620 return messages.get(0);
621 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800622}