blob: 14fec857ae27d23a99d274aaaea58d6b743151dc [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());
Hugo Hudson757aa552011-07-20 01:15:25 +0100177 optionallyHandleVoicemail();
Hugo Hudsonb002f512011-07-15 17:41:12 +0100178 }
179
180 /**
181 * Handle voicemail playback or hide voicemail ui.
182 * <p>
183 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
184 * playback. If it doesn't, then hide the voicemail ui.
185 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100186 private void optionallyHandleVoicemail() {
187 Uri voicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100188 FragmentManager manager = getFragmentManager();
189 VoicemailPlaybackFragment fragment = (VoicemailPlaybackFragment) manager.findFragmentById(
190 R.id.voicemail_playback_fragment);
Hugo Hudson757aa552011-07-20 01:15:25 +0100191 if (voicemailUri != null) {
192 // Has voicemail uri: leave the fragment visible. Optionally start the playback.
193 // Do a query to fetch the voicemail status messages.
194 boolean startPlayback = getIntent().getBooleanExtra(
Hugo Hudsonb002f512011-07-15 17:41:12 +0100195 EXTRA_VOICEMAIL_START_PLAYBACK, false);
196 fragment.setVoicemailUri(voicemailUri, startPlayback);
Hugo Hudson757aa552011-07-20 01:15:25 +0100197 mAsyncQueryHandler.startVoicemailStatusQuery(voicemailUri);
198 } else {
199 // No voicemail uri: hide the voicemail fragment and the status view.
200 manager.beginTransaction().hide(fragment).commit();
201 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100202 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100203 }
204
205 /**
206 * Returns the list of URIs to show.
207 * <p>
208 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
209 * a list of ids in the call log added as an extra on the URI.
210 * <p>
211 * If both are available, the data on the intent takes precedence.
212 */
213 private Uri[] getCallLogEntryUris() {
214 Uri uri = getIntent().getData();
215 if (uri != null) {
216 // If there is a data on the intent, it takes precedence over the extra.
217 return new Uri[]{ uri };
218 }
219 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
220 Uri[] uris = new Uri[ids.length];
221 for (int index = 0; index < ids.length; ++index) {
222 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
223 }
224 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800225 }
226
227 @Override
228 public boolean onKeyDown(int keyCode, KeyEvent event) {
229 switch (keyCode) {
230 case KeyEvent.KEYCODE_CALL: {
231 // Make sure phone isn't already busy before starting direct call
232 TelephonyManager tm = (TelephonyManager)
233 getSystemService(Context.TELEPHONY_SERVICE);
234 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
235 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
236 Uri.fromParts("tel", mNumber, null));
237 startActivity(callIntent);
238 return true;
239 }
240 }
241 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700242
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800243 return super.onKeyDown(keyCode, event);
244 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700245
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800246 /**
247 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700248 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100249 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800250 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100251 private void updateData(final Uri... callUris) {
252 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
253 final int numCalls = callUris.length;
254 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
255 try {
256 for (int index = 0; index < numCalls; ++index) {
257 details[index] = getPhoneCallDetailsForUri(callUris[index]);
258 }
259 } catch (IllegalArgumentException e) {
260 // Something went wrong reading in our primary data, so we're going to
261 // bail out and show error to users.
262 Log.w(TAG, "invalid URI starting call details", e);
263 Toast.makeText(this, R.string.toast_call_detail_error,
264 Toast.LENGTH_SHORT).show();
265 finish();
266 return;
267 }
268
269 // We know that all calls are from the same number and the same contact, so pick the first.
270 mNumber = details[0].number.toString();
271 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700272 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100273
274 // Set the details header, based on the first phone call.
275 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda76921902011-07-13 21:11:51 +0100276 details[0], false, false);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100277
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100278 // Cache the details about the phone number.
279 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
280 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
281 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
282 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
283
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100284 // Let user view contact details if they exist, otherwise add option to create new contact
285 // from this number.
286 final Intent mainActionIntent;
287 final int mainActionIcon;
288
289 if (details[0].personId != -1) {
290 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
291 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
292 mainActionIcon = R.drawable.sym_action_view_contact;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100293 } else if (isVoicemailNumber) {
294 mainActionIntent = null;
295 mainActionIcon = 0;
296 } else if (isSipNumber) {
297 // TODO: This item is currently disabled for SIP addresses, because
298 // the Insert.PHONE extra only works correctly for PSTN numbers.
299 //
300 // To fix this for SIP addresses, we need to:
301 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
302 // the current number is a SIP address
303 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
304 // updating the SipAddress field
305 // and then we can remove the "!isSipNumber" check above.
306 mainActionIntent = null;
307 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100308 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100309 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
310 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
311 mainActionIntent.putExtra(Insert.PHONE, mNumber);
312 mainActionIcon = R.drawable.sym_action_add;
Flavio Lerda8e403402011-07-20 16:25:49 +0100313 } else {
314 // If we cannot call the number, when we probably cannot add it as a contact either.
315 // This is usually the case of private, unknown, or payphone numbers.
316 mainActionIntent = null;
317 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100318 }
319
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100320 if (mainActionIntent == null) {
321 mMainActionView.setVisibility(View.INVISIBLE);
322 } else {
323 mMainActionView.setVisibility(View.VISIBLE);
324 mMainActionView.setImageResource(mainActionIcon);
325 mMainActionView.setOnClickListener(new View.OnClickListener() {
326 @Override
327 public void onClick(View v) {
328 startActivity(mainActionIntent);
329 }
330 });
331 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100332
333 // Build list of various available actions.
334 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
335
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100336 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100337 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100338 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
339 getString(R.string.menu_callNumber, mNumber),
340 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
341 }
342
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100343 // This action allows to send an SMS to the number that placed the call.
344 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100345 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
346 Uri.fromParts("sms", mNumber, null));
347 actions.add(new ViewEntry(R.drawable.sym_action_sms,
348 getString(R.string.menu_sendTextMessage), smsIntent));
349 }
350
351 // This action deletes all elements in the group from the call log.
352 actions.add(new ViewEntry(android.R.drawable.ic_menu_close_clear_cancel,
353 getString(R.string.recentCalls_removeFromRecentList),
354 new View.OnClickListener() {
355 @Override
356 public void onClick(View v) {
357 StringBuilder callIds = new StringBuilder();
358 for (Uri callUri : callUris) {
359 if (callIds.length() != 0) {
360 callIds.append(",");
361 }
362 callIds.append(ContentUris.parseId(callUri));
363 }
364
365 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
366 Calls._ID + " IN (" + callIds + ")", null);
367 finish();
368 }
369 }));
370
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100371 if (canPlaceCallsTo && !isSipNumber && !isVoicemailNumber) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100372 // "Edit the number before calling" is only available for PSTN numbers.
373 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
374 getString(R.string.recentCalls_editNumberBeforeCall),
375 new Intent(Intent.ACTION_DIAL, numberCallUri)));
376 }
377
378 // Set the actions for this phone number.
379 setListAdapter(new ViewAdapter(this, actions));
380
381 ListView historyList = (ListView) findViewById(R.id.history);
382 historyList.setAdapter(
383 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700384 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100385 }
386
387 /** Return the phone call details for a given call log URI. */
388 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800389 ContentResolver resolver = getContentResolver();
390 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
391 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100392 if (callCursor == null || !callCursor.moveToFirst()) {
393 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800394 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100395
396 // Read call log specifics.
397 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
398 long date = callCursor.getLong(DATE_COLUMN_INDEX);
399 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
400 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
401 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
402 if (TextUtils.isEmpty(countryIso)) {
403 countryIso = mDefaultCountryIso;
404 }
405
406 // Formatted phone number.
407 final CharSequence numberText;
408 // Read contact specifics.
409 CharSequence nameText = "";
410 int numberType = 0;
411 CharSequence numberLabel = "";
412 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700413 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100414 // If this is not a regular number, there is no point in looking it up in the contacts.
415 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
416 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
417 } else {
418 // Perform a reverse-phonebook lookup to find the contact details.
419 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
420 Uri.encode(number));
421 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
422 String candidateNumberText = number;
423 try {
424 if (phonesCursor != null && phonesCursor.moveToFirst()) {
425 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
426 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700427 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
428 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100429 candidateNumberText = PhoneNumberUtils.formatNumber(
430 phonesCursor.getString(COLUMN_INDEX_NUMBER),
431 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
432 countryIso);
433 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
434 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
435 } else {
436 // We could not find this contact in the contacts, just format the phone
437 // number as best as we can. All the other fields will have their default
438 // values.
439 candidateNumberText =
440 PhoneNumberUtils.formatNumber(number, countryIso);
441 }
442 } finally {
443 if (phonesCursor != null) phonesCursor.close();
444 numberText = candidateNumberText;
445 }
446 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100447 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100448 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700449 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800450 } finally {
451 if (callCursor != null) {
452 callCursor.close();
453 }
454 }
455 }
456
Flavio Lerda9cafe472011-06-08 14:06:13 +0100457 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700458 private void loadContactPhotos(Uri photoUri) {
459 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100460 }
461
Flavio Lerda696e8132011-07-05 19:00:23 +0100462 private String getVoicemailNumber() {
463 TelephonyManager telephonyManager =
464 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
465 return telephonyManager.getVoiceMailNumber();
466 }
467
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800468 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100469 public final int icon;
470 public final String text;
471 public final Intent intent;
472 public final View.OnClickListener action;
473
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800474 public String label = null;
475 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700476
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800477 public ViewEntry(int icon, String text, Intent intent) {
478 this.icon = icon;
479 this.text = text;
480 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100481 this.action = null;
482 }
483
484 public ViewEntry(int icon, String text, View.OnClickListener listener) {
485 this.icon = icon;
486 this.text = text;
487 this.intent = null;
488 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800489 }
490 }
491
492 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700493
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800494 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700495
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800496 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700497
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800498 public ViewAdapter(Context context, List<ViewEntry> actions) {
499 mActions = actions;
500 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
501 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700502
Flavio Lerda9cafe472011-06-08 14:06:13 +0100503 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800504 public int getCount() {
505 return mActions.size();
506 }
507
Flavio Lerda9cafe472011-06-08 14:06:13 +0100508 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800509 public Object getItem(int position) {
510 return mActions.get(position);
511 }
512
Flavio Lerda9cafe472011-06-08 14:06:13 +0100513 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800514 public long getItemId(int position) {
515 return position;
516 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700517
Flavio Lerda9cafe472011-06-08 14:06:13 +0100518 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800519 public View getView(int position, View convertView, ViewGroup parent) {
520 // Make sure we have a valid convertView to start with
521 if (convertView == null) {
522 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
523 }
524
525 // Fill action with icon and text.
526 ViewEntry entry = mActions.get(position);
527 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700528
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800529 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
530 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
531
532 icon.setImageResource(entry.icon);
533 text.setText(entry.text);
534
535 View line2 = convertView.findViewById(R.id.line2);
536 boolean numberEmpty = TextUtils.isEmpty(entry.number);
537 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
538 if (labelEmpty && numberEmpty) {
539 line2.setVisibility(View.GONE);
540 } else {
541 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700542
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800543 TextView label = (TextView) convertView.findViewById(R.id.label);
544 if (labelEmpty) {
545 label.setVisibility(View.GONE);
546 } else {
547 label.setText(entry.label);
548 label.setVisibility(View.VISIBLE);
549 }
550
551 TextView number = (TextView) convertView.findViewById(R.id.number);
552 number.setText(entry.number);
553 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700554
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800555 return convertView;
556 }
557 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700558
Flavio Lerda9cafe472011-06-08 14:06:13 +0100559 @Override
560 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800561 // Handle passing action off to correct handler.
562 if (view.getTag() instanceof ViewEntry) {
563 ViewEntry entry = (ViewEntry) view.getTag();
564 if (entry.intent != null) {
565 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100566 } else if (entry.action != null) {
567 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800568 }
569 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700570 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800571
572 @Override
573 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
574 boolean globalSearch) {
575 if (globalSearch) {
576 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
577 } else {
578 ContactsSearchManager.startSearch(this, initialQuery);
579 }
580 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100581
582 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
583 if (statusCursor == null) {
584 mStatusMessageView.setVisibility(View.GONE);
585 return;
586 }
587 final StatusMessage message = getStatusMessage(statusCursor);
588 if (message == null || !message.showInCallDetails()) {
589 mStatusMessageView.setVisibility(View.GONE);
590 return;
591 }
592
593 mStatusMessageView.setVisibility(View.VISIBLE);
594 mStatusMessageText.setText(message.callDetailsMessageId);
595 if (message.actionMessageId != -1) {
596 mStatusMessageAction.setText(message.actionMessageId);
597 }
598 if (message.actionUri != null) {
599 mStatusMessageAction.setClickable(true);
600 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
601 @Override
602 public void onClick(View v) {
603 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
604 }
605 });
606 } else {
607 mStatusMessageAction.setClickable(false);
608 }
609 }
610
611 private StatusMessage getStatusMessage(Cursor statusCursor) {
612 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
613 Log.d(TAG, "Num status messages: " + messages.size());
614 if (messages.size() == 0) {
615 return null;
616 }
617 // There can only be a single status message per source package, so num of messages can
618 // at most be 1.
619 if (messages.size() > 1) {
620 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
621 " Will use the first one.", messages.size()));
622 }
623 return messages.get(0);
624 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800625}