blob: 324ab2063d64698a5fca454cbd6f057849e67243 [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
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800172 @Override
173 public void onResume() {
174 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100175 updateData(getCallLogEntryUris());
Hugo Hudson757aa552011-07-20 01:15:25 +0100176 optionallyHandleVoicemail();
Hugo Hudsonb002f512011-07-15 17:41:12 +0100177 }
178
179 /**
180 * Handle voicemail playback or hide voicemail ui.
181 * <p>
182 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
183 * playback. If it doesn't, then hide the voicemail ui.
184 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100185 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100186 if (hasVoicemail()) {
187 // Has voicemail: leave the fragment visible. Optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100188 // Do a query to fetch the voicemail status messages.
189 boolean startPlayback = getIntent().getBooleanExtra(
Hugo Hudsonb002f512011-07-15 17:41:12 +0100190 EXTRA_VOICEMAIL_START_PLAYBACK, false);
Hugo Hudson157dde12011-07-21 23:28:13 +0100191 Uri voicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
192 getVoicemailPlaybackFragment().setVoicemailUri(voicemailUri, startPlayback);
Hugo Hudson757aa552011-07-20 01:15:25 +0100193 mAsyncQueryHandler.startVoicemailStatusQuery(voicemailUri);
194 } else {
195 // No voicemail uri: hide the voicemail fragment and the status view.
Hugo Hudson157dde12011-07-21 23:28:13 +0100196 getFragmentManager().beginTransaction().hide(getVoicemailPlaybackFragment()).commit();
Hugo Hudson757aa552011-07-20 01:15:25 +0100197 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100198 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100199 }
200
Hugo Hudson157dde12011-07-21 23:28:13 +0100201 private boolean hasVoicemail() {
202 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI) != null;
203 }
204
205 private VoicemailPlaybackFragment getVoicemailPlaybackFragment() {
206 FragmentManager manager = getFragmentManager();
207 return (VoicemailPlaybackFragment) manager.findFragmentById(
208 R.id.voicemail_playback_fragment);
209 }
210
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100211 /**
212 * Returns the list of URIs to show.
213 * <p>
214 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
215 * a list of ids in the call log added as an extra on the URI.
216 * <p>
217 * If both are available, the data on the intent takes precedence.
218 */
219 private Uri[] getCallLogEntryUris() {
220 Uri uri = getIntent().getData();
221 if (uri != null) {
222 // If there is a data on the intent, it takes precedence over the extra.
223 return new Uri[]{ uri };
224 }
225 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
226 Uri[] uris = new Uri[ids.length];
227 for (int index = 0; index < ids.length; ++index) {
228 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
229 }
230 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800231 }
232
233 @Override
234 public boolean onKeyDown(int keyCode, KeyEvent event) {
235 switch (keyCode) {
236 case KeyEvent.KEYCODE_CALL: {
237 // Make sure phone isn't already busy before starting direct call
238 TelephonyManager tm = (TelephonyManager)
239 getSystemService(Context.TELEPHONY_SERVICE);
240 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
241 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
242 Uri.fromParts("tel", mNumber, null));
243 startActivity(callIntent);
244 return true;
245 }
246 }
247 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700248
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800249 return super.onKeyDown(keyCode, event);
250 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700251
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800252 /**
253 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700254 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100255 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800256 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100257 private void updateData(final Uri... callUris) {
258 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
259 final int numCalls = callUris.length;
260 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
261 try {
262 for (int index = 0; index < numCalls; ++index) {
263 details[index] = getPhoneCallDetailsForUri(callUris[index]);
264 }
265 } catch (IllegalArgumentException e) {
266 // Something went wrong reading in our primary data, so we're going to
267 // bail out and show error to users.
268 Log.w(TAG, "invalid URI starting call details", e);
269 Toast.makeText(this, R.string.toast_call_detail_error,
270 Toast.LENGTH_SHORT).show();
271 finish();
272 return;
273 }
274
275 // We know that all calls are from the same number and the same contact, so pick the first.
276 mNumber = details[0].number.toString();
277 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700278 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100279
280 // Set the details header, based on the first phone call.
281 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda76921902011-07-13 21:11:51 +0100282 details[0], false, false);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100283
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100284 // Cache the details about the phone number.
285 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
286 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
287 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
288 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
289
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100290 // Let user view contact details if they exist, otherwise add option to create new contact
291 // from this number.
292 final Intent mainActionIntent;
293 final int mainActionIcon;
294
295 if (details[0].personId != -1) {
296 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
297 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
298 mainActionIcon = R.drawable.sym_action_view_contact;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100299 } else if (isVoicemailNumber) {
300 mainActionIntent = null;
301 mainActionIcon = 0;
302 } else if (isSipNumber) {
303 // TODO: This item is currently disabled for SIP addresses, because
304 // the Insert.PHONE extra only works correctly for PSTN numbers.
305 //
306 // To fix this for SIP addresses, we need to:
307 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
308 // the current number is a SIP address
309 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
310 // updating the SipAddress field
311 // and then we can remove the "!isSipNumber" check above.
312 mainActionIntent = null;
313 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100314 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100315 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
316 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
317 mainActionIntent.putExtra(Insert.PHONE, mNumber);
318 mainActionIcon = R.drawable.sym_action_add;
Flavio Lerda8e403402011-07-20 16:25:49 +0100319 } else {
320 // If we cannot call the number, when we probably cannot add it as a contact either.
321 // This is usually the case of private, unknown, or payphone numbers.
322 mainActionIntent = null;
323 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100324 }
325
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100326 if (mainActionIntent == null) {
327 mMainActionView.setVisibility(View.INVISIBLE);
328 } else {
329 mMainActionView.setVisibility(View.VISIBLE);
330 mMainActionView.setImageResource(mainActionIcon);
331 mMainActionView.setOnClickListener(new View.OnClickListener() {
332 @Override
333 public void onClick(View v) {
334 startActivity(mainActionIntent);
335 }
336 });
337 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100338
339 // Build list of various available actions.
340 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
341
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100342 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100343 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100344 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
345 getString(R.string.menu_callNumber, mNumber),
346 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
347 }
348
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100349 // This action allows to send an SMS to the number that placed the call.
350 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100351 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
352 Uri.fromParts("sms", mNumber, null));
353 actions.add(new ViewEntry(R.drawable.sym_action_sms,
354 getString(R.string.menu_sendTextMessage), smsIntent));
355 }
356
357 // This action deletes all elements in the group from the call log.
Hugo Hudson157dde12011-07-21 23:28:13 +0100358 // We don't have this action for voicemails, because you can just use the trash button.
359 if (!hasVoicemail()) {
360 actions.add(new ViewEntry(android.R.drawable.ic_menu_close_clear_cancel,
361 getString(R.string.recentCalls_removeFromRecentList),
362 new View.OnClickListener() {
363 @Override
364 public void onClick(View v) {
365 StringBuilder callIds = new StringBuilder();
366 for (Uri callUri : callUris) {
367 if (callIds.length() != 0) {
368 callIds.append(",");
369 }
370 callIds.append(ContentUris.parseId(callUri));
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100371 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100372
Hugo Hudson157dde12011-07-21 23:28:13 +0100373 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
374 Calls._ID + " IN (" + callIds + ")", null);
375 finish();
376 }
377 }));
378 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100379
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100380 if (canPlaceCallsTo && !isSipNumber && !isVoicemailNumber) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100381 // "Edit the number before calling" is only available for PSTN numbers.
382 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
383 getString(R.string.recentCalls_editNumberBeforeCall),
384 new Intent(Intent.ACTION_DIAL, numberCallUri)));
385 }
386
387 // Set the actions for this phone number.
388 setListAdapter(new ViewAdapter(this, actions));
389
390 ListView historyList = (ListView) findViewById(R.id.history);
391 historyList.setAdapter(
392 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700393 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100394 }
395
396 /** Return the phone call details for a given call log URI. */
397 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800398 ContentResolver resolver = getContentResolver();
399 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
400 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100401 if (callCursor == null || !callCursor.moveToFirst()) {
402 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800403 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100404
405 // Read call log specifics.
406 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
407 long date = callCursor.getLong(DATE_COLUMN_INDEX);
408 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
409 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
410 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
411 if (TextUtils.isEmpty(countryIso)) {
412 countryIso = mDefaultCountryIso;
413 }
414
415 // Formatted phone number.
416 final CharSequence numberText;
417 // Read contact specifics.
418 CharSequence nameText = "";
419 int numberType = 0;
420 CharSequence numberLabel = "";
421 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700422 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100423 // If this is not a regular number, there is no point in looking it up in the contacts.
424 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
425 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
426 } else {
427 // Perform a reverse-phonebook lookup to find the contact details.
428 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
429 Uri.encode(number));
430 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
431 String candidateNumberText = number;
432 try {
433 if (phonesCursor != null && phonesCursor.moveToFirst()) {
434 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
435 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700436 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
437 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100438 candidateNumberText = PhoneNumberUtils.formatNumber(
439 phonesCursor.getString(COLUMN_INDEX_NUMBER),
440 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
441 countryIso);
442 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
443 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
444 } else {
445 // We could not find this contact in the contacts, just format the phone
446 // number as best as we can. All the other fields will have their default
447 // values.
448 candidateNumberText =
449 PhoneNumberUtils.formatNumber(number, countryIso);
450 }
451 } finally {
452 if (phonesCursor != null) phonesCursor.close();
453 numberText = candidateNumberText;
454 }
455 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100456 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100457 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700458 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800459 } finally {
460 if (callCursor != null) {
461 callCursor.close();
462 }
463 }
464 }
465
Flavio Lerda9cafe472011-06-08 14:06:13 +0100466 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700467 private void loadContactPhotos(Uri photoUri) {
468 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100469 }
470
Flavio Lerda696e8132011-07-05 19:00:23 +0100471 private String getVoicemailNumber() {
472 TelephonyManager telephonyManager =
473 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
474 return telephonyManager.getVoiceMailNumber();
475 }
476
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800477 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100478 public final int icon;
479 public final String text;
480 public final Intent intent;
481 public final View.OnClickListener action;
482
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800483 public String label = null;
484 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700485
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800486 public ViewEntry(int icon, String text, Intent intent) {
487 this.icon = icon;
488 this.text = text;
489 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100490 this.action = null;
491 }
492
493 public ViewEntry(int icon, String text, View.OnClickListener listener) {
494 this.icon = icon;
495 this.text = text;
496 this.intent = null;
497 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800498 }
499 }
500
501 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700502
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800503 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700504
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800505 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700506
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800507 public ViewAdapter(Context context, List<ViewEntry> actions) {
508 mActions = actions;
509 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
510 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700511
Flavio Lerda9cafe472011-06-08 14:06:13 +0100512 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800513 public int getCount() {
514 return mActions.size();
515 }
516
Flavio Lerda9cafe472011-06-08 14:06:13 +0100517 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800518 public Object getItem(int position) {
519 return mActions.get(position);
520 }
521
Flavio Lerda9cafe472011-06-08 14:06:13 +0100522 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800523 public long getItemId(int position) {
524 return position;
525 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700526
Flavio Lerda9cafe472011-06-08 14:06:13 +0100527 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800528 public View getView(int position, View convertView, ViewGroup parent) {
529 // Make sure we have a valid convertView to start with
530 if (convertView == null) {
531 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
532 }
533
534 // Fill action with icon and text.
535 ViewEntry entry = mActions.get(position);
536 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700537
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800538 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
539 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
540
541 icon.setImageResource(entry.icon);
542 text.setText(entry.text);
543
544 View line2 = convertView.findViewById(R.id.line2);
545 boolean numberEmpty = TextUtils.isEmpty(entry.number);
546 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
547 if (labelEmpty && numberEmpty) {
548 line2.setVisibility(View.GONE);
549 } else {
550 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700551
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800552 TextView label = (TextView) convertView.findViewById(R.id.label);
553 if (labelEmpty) {
554 label.setVisibility(View.GONE);
555 } else {
556 label.setText(entry.label);
557 label.setVisibility(View.VISIBLE);
558 }
559
560 TextView number = (TextView) convertView.findViewById(R.id.number);
561 number.setText(entry.number);
562 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700563
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800564 return convertView;
565 }
566 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700567
Flavio Lerda9cafe472011-06-08 14:06:13 +0100568 @Override
569 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800570 // Handle passing action off to correct handler.
571 if (view.getTag() instanceof ViewEntry) {
572 ViewEntry entry = (ViewEntry) view.getTag();
573 if (entry.intent != null) {
574 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100575 } else if (entry.action != null) {
576 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800577 }
578 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700579 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800580
581 @Override
582 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
583 boolean globalSearch) {
584 if (globalSearch) {
585 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
586 } else {
587 ContactsSearchManager.startSearch(this, initialQuery);
588 }
589 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100590
591 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
592 if (statusCursor == null) {
593 mStatusMessageView.setVisibility(View.GONE);
594 return;
595 }
596 final StatusMessage message = getStatusMessage(statusCursor);
597 if (message == null || !message.showInCallDetails()) {
598 mStatusMessageView.setVisibility(View.GONE);
599 return;
600 }
601
602 mStatusMessageView.setVisibility(View.VISIBLE);
603 mStatusMessageText.setText(message.callDetailsMessageId);
604 if (message.actionMessageId != -1) {
605 mStatusMessageAction.setText(message.actionMessageId);
606 }
607 if (message.actionUri != null) {
608 mStatusMessageAction.setClickable(true);
609 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
610 @Override
611 public void onClick(View v) {
612 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
613 }
614 });
615 } else {
616 mStatusMessageAction.setClickable(false);
617 }
618 }
619
620 private StatusMessage getStatusMessage(Cursor statusCursor) {
621 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
622 Log.d(TAG, "Num status messages: " + messages.size());
623 if (messages.size() == 0) {
624 return null;
625 }
626 // There can only be a single status message per source package, so num of messages can
627 // at most be 1.
628 if (messages.size() > 1) {
629 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
630 " Will use the first one.", messages.size()));
631 }
632 return messages.get(0);
633 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800634}