blob: 2bea2f37d16b5044e14dbcf4c9ac5570713b28dd [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 Lerda8e403402011-07-20 16:25:49 +0100310 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100311 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;
Flavio Lerda8e403402011-07-20 16:25:49 +0100315 } else {
316 // If we cannot call the number, when we probably cannot add it as a contact either.
317 // This is usually the case of private, unknown, or payphone numbers.
318 mainActionIntent = null;
319 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100320 }
321
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100322 if (mainActionIntent == null) {
323 mMainActionView.setVisibility(View.INVISIBLE);
324 } else {
325 mMainActionView.setVisibility(View.VISIBLE);
326 mMainActionView.setImageResource(mainActionIcon);
327 mMainActionView.setOnClickListener(new View.OnClickListener() {
328 @Override
329 public void onClick(View v) {
330 startActivity(mainActionIntent);
331 }
332 });
333 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100334
335 // Build list of various available actions.
336 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
337
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100338 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100339 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100340 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
341 getString(R.string.menu_callNumber, mNumber),
342 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
343 }
344
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100345 // This action allows to send an SMS to the number that placed the call.
346 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100347 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
348 Uri.fromParts("sms", mNumber, null));
349 actions.add(new ViewEntry(R.drawable.sym_action_sms,
350 getString(R.string.menu_sendTextMessage), smsIntent));
351 }
352
353 // This action deletes all elements in the group from the call log.
354 actions.add(new ViewEntry(android.R.drawable.ic_menu_close_clear_cancel,
355 getString(R.string.recentCalls_removeFromRecentList),
356 new View.OnClickListener() {
357 @Override
358 public void onClick(View v) {
359 StringBuilder callIds = new StringBuilder();
360 for (Uri callUri : callUris) {
361 if (callIds.length() != 0) {
362 callIds.append(",");
363 }
364 callIds.append(ContentUris.parseId(callUri));
365 }
366
367 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
368 Calls._ID + " IN (" + callIds + ")", null);
369 finish();
370 }
371 }));
372
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100373 if (canPlaceCallsTo && !isSipNumber && !isVoicemailNumber) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100374 // "Edit the number before calling" is only available for PSTN numbers.
375 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
376 getString(R.string.recentCalls_editNumberBeforeCall),
377 new Intent(Intent.ACTION_DIAL, numberCallUri)));
378 }
379
380 // Set the actions for this phone number.
381 setListAdapter(new ViewAdapter(this, actions));
382
383 ListView historyList = (ListView) findViewById(R.id.history);
384 historyList.setAdapter(
385 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700386 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100387 }
388
389 /** Return the phone call details for a given call log URI. */
390 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800391 ContentResolver resolver = getContentResolver();
392 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
393 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100394 if (callCursor == null || !callCursor.moveToFirst()) {
395 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800396 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100397
398 // Read call log specifics.
399 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
400 long date = callCursor.getLong(DATE_COLUMN_INDEX);
401 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
402 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
403 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
404 if (TextUtils.isEmpty(countryIso)) {
405 countryIso = mDefaultCountryIso;
406 }
407
408 // Formatted phone number.
409 final CharSequence numberText;
410 // Read contact specifics.
411 CharSequence nameText = "";
412 int numberType = 0;
413 CharSequence numberLabel = "";
414 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700415 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100416 // If this is not a regular number, there is no point in looking it up in the contacts.
417 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
418 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
419 } else {
420 // Perform a reverse-phonebook lookup to find the contact details.
421 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
422 Uri.encode(number));
423 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
424 String candidateNumberText = number;
425 try {
426 if (phonesCursor != null && phonesCursor.moveToFirst()) {
427 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
428 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700429 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
430 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100431 candidateNumberText = PhoneNumberUtils.formatNumber(
432 phonesCursor.getString(COLUMN_INDEX_NUMBER),
433 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
434 countryIso);
435 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
436 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
437 } else {
438 // We could not find this contact in the contacts, just format the phone
439 // number as best as we can. All the other fields will have their default
440 // values.
441 candidateNumberText =
442 PhoneNumberUtils.formatNumber(number, countryIso);
443 }
444 } finally {
445 if (phonesCursor != null) phonesCursor.close();
446 numberText = candidateNumberText;
447 }
448 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100449 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100450 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700451 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800452 } finally {
453 if (callCursor != null) {
454 callCursor.close();
455 }
456 }
457 }
458
Flavio Lerda9cafe472011-06-08 14:06:13 +0100459 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700460 private void loadContactPhotos(Uri photoUri) {
461 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100462 }
463
Flavio Lerda696e8132011-07-05 19:00:23 +0100464 private String getVoicemailNumber() {
465 TelephonyManager telephonyManager =
466 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
467 return telephonyManager.getVoiceMailNumber();
468 }
469
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800470 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100471 public final int icon;
472 public final String text;
473 public final Intent intent;
474 public final View.OnClickListener action;
475
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800476 public String label = null;
477 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700478
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800479 public ViewEntry(int icon, String text, Intent intent) {
480 this.icon = icon;
481 this.text = text;
482 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100483 this.action = null;
484 }
485
486 public ViewEntry(int icon, String text, View.OnClickListener listener) {
487 this.icon = icon;
488 this.text = text;
489 this.intent = null;
490 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800491 }
492 }
493
494 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700495
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800496 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700497
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800498 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700499
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800500 public ViewAdapter(Context context, List<ViewEntry> actions) {
501 mActions = actions;
502 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
503 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700504
Flavio Lerda9cafe472011-06-08 14:06:13 +0100505 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800506 public int getCount() {
507 return mActions.size();
508 }
509
Flavio Lerda9cafe472011-06-08 14:06:13 +0100510 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800511 public Object getItem(int position) {
512 return mActions.get(position);
513 }
514
Flavio Lerda9cafe472011-06-08 14:06:13 +0100515 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800516 public long getItemId(int position) {
517 return position;
518 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700519
Flavio Lerda9cafe472011-06-08 14:06:13 +0100520 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800521 public View getView(int position, View convertView, ViewGroup parent) {
522 // Make sure we have a valid convertView to start with
523 if (convertView == null) {
524 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
525 }
526
527 // Fill action with icon and text.
528 ViewEntry entry = mActions.get(position);
529 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700530
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800531 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
532 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
533
534 icon.setImageResource(entry.icon);
535 text.setText(entry.text);
536
537 View line2 = convertView.findViewById(R.id.line2);
538 boolean numberEmpty = TextUtils.isEmpty(entry.number);
539 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
540 if (labelEmpty && numberEmpty) {
541 line2.setVisibility(View.GONE);
542 } else {
543 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700544
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800545 TextView label = (TextView) convertView.findViewById(R.id.label);
546 if (labelEmpty) {
547 label.setVisibility(View.GONE);
548 } else {
549 label.setText(entry.label);
550 label.setVisibility(View.VISIBLE);
551 }
552
553 TextView number = (TextView) convertView.findViewById(R.id.number);
554 number.setText(entry.number);
555 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700556
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800557 return convertView;
558 }
559 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700560
Flavio Lerda9cafe472011-06-08 14:06:13 +0100561 @Override
562 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800563 // Handle passing action off to correct handler.
564 if (view.getTag() instanceof ViewEntry) {
565 ViewEntry entry = (ViewEntry) view.getTag();
566 if (entry.intent != null) {
567 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100568 } else if (entry.action != null) {
569 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800570 }
571 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700572 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800573
574 @Override
575 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
576 boolean globalSearch) {
577 if (globalSearch) {
578 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
579 } else {
580 ContactsSearchManager.startSearch(this, initialQuery);
581 }
582 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100583
584 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
585 if (statusCursor == null) {
586 mStatusMessageView.setVisibility(View.GONE);
587 return;
588 }
589 final StatusMessage message = getStatusMessage(statusCursor);
590 if (message == null || !message.showInCallDetails()) {
591 mStatusMessageView.setVisibility(View.GONE);
592 return;
593 }
594
595 mStatusMessageView.setVisibility(View.VISIBLE);
596 mStatusMessageText.setText(message.callDetailsMessageId);
597 if (message.actionMessageId != -1) {
598 mStatusMessageAction.setText(message.actionMessageId);
599 }
600 if (message.actionUri != null) {
601 mStatusMessageAction.setClickable(true);
602 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
603 @Override
604 public void onClick(View v) {
605 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
606 }
607 });
608 } else {
609 mStatusMessageAction.setClickable(false);
610 }
611 }
612
613 private StatusMessage getStatusMessage(Cursor statusCursor) {
614 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
615 Log.d(TAG, "Num status messages: " + messages.size());
616 if (messages.size() == 0) {
617 return null;
618 }
619 // There can only be a single status message per source package, so num of messages can
620 // at most be 1.
621 if (messages.size() > 1) {
622 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
623 " Will use the first one.", messages.size()));
624 }
625 return messages.get(0);
626 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800627}