blob: 3fdfe382efbc962e7900890d17b328e7ba1e73a3 [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
Flavio Lerdad44e83a2011-07-24 23:10:17 +010027import android.app.ActionBar;
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;
Flavio Lerda94d7bab2011-07-22 16:52:34 +010048import android.view.Menu;
49import android.view.MenuItem;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080050import android.view.View;
51import android.view.ViewGroup;
52import android.widget.AdapterView;
53import android.widget.BaseAdapter;
54import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010055import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080056import android.widget.TextView;
57import android.widget.Toast;
58
59import java.util.ArrayList;
60import java.util.List;
61
62/**
63 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010064 * <p>
65 * This activity can be either started with the URI of a single call log entry, or with the
66 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080067 */
68public class CallDetailActivity extends ListActivity implements
69 AdapterView.OnItemClickListener {
70 private static final String TAG = "CallDetail";
71
Flavio Lerda178eeeb2011-07-11 19:51:40 +010072 /** A long array extra containing ids of call log entries to display. */
Hugo Hudsonb002f512011-07-15 17:41:12 +010073 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
74 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
75 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
76 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
77 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
Flavio Lerda178eeeb2011-07-11 19:51:40 +010078
Flavio Lerdaa024c3f2011-06-10 10:47:07 +010079 /** The views representing the details of a phone call. */
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010080 private PhoneCallDetailsViews mPhoneCallDetailsViews;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010081 private CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010082 private PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010083 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
Flavio Lerdafb63c432011-07-07 17:16:53 +010084 private ImageView mMainActionView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010085 private ImageView mContactBackgroundView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080086
87 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080088 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070089
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080090 /* package */ LayoutInflater mInflater;
91 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010092 /** Helper to load contact photos. */
93 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010094 /** Helper to make async queries to content resolver. */
95 private CallDetailActivityQueryHandler mAsyncQueryHandler;
96 /** Helper to get voicemail status messages. */
97 private VoicemailStatusHelper mVoicemailStatusHelper;
98 // Views related to voicemail status message.
99 private View mStatusMessageView;
100 private TextView mStatusMessageText;
101 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700102
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100103 /** Whether we should show "edit number before call" in the options menu. */
104 private boolean mHasEditNumberBeforeCall;
105
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800106 static final String[] CALL_LOG_PROJECTION = new String[] {
107 CallLog.Calls.DATE,
108 CallLog.Calls.DURATION,
109 CallLog.Calls.NUMBER,
110 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800111 CallLog.Calls.COUNTRY_ISO,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800112 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700113
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800114 static final int DATE_COLUMN_INDEX = 0;
115 static final int DURATION_COLUMN_INDEX = 1;
116 static final int NUMBER_COLUMN_INDEX = 2;
117 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800118 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700119
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800120 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700121 PhoneLookup._ID,
122 PhoneLookup.DISPLAY_NAME,
123 PhoneLookup.TYPE,
124 PhoneLookup.LABEL,
125 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800126 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700127 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800128 };
129 static final int COLUMN_INDEX_ID = 0;
130 static final int COLUMN_INDEX_NAME = 1;
131 static final int COLUMN_INDEX_TYPE = 2;
132 static final int COLUMN_INDEX_LABEL = 3;
133 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800134 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700135 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800136
137 @Override
138 protected void onCreate(Bundle icicle) {
139 super.onCreate(icicle);
140
141 setContentView(R.layout.call_detail);
142
143 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
144 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700145
Flavio Lerda696e8132011-07-05 19:00:23 +0100146 mPhoneCallDetailsViews = PhoneCallDetailsViews.fromView(getWindow().getDecorView());
Flavio Lerda40569562011-07-22 21:51:29 +0100147 mCallTypeHelper = new CallTypeHelper(getResources(), mInflater);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100148 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
Flavio Lerda40569562011-07-22 21:51:29 +0100149 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100150 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 mMainActionView = (ImageView) findViewById(R.id.main_action);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100157 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800158 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100159 mContactPhotoManager = ContactPhotoManager.getInstance(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800160 getListView().setOnItemClickListener(this);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100161 configureActionBar();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800162 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700163
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800164 @Override
165 public void onResume() {
166 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100167 updateData(getCallLogEntryUris());
Hugo Hudson757aa552011-07-20 01:15:25 +0100168 optionallyHandleVoicemail();
Hugo Hudsonb002f512011-07-15 17:41:12 +0100169 }
170
171 /**
172 * Handle voicemail playback or hide voicemail ui.
173 * <p>
174 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
175 * playback. If it doesn't, then hide the voicemail ui.
176 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100177 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100178 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100179 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
180 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100181 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100182 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
183 Bundle fragmentArguments = new Bundle();
Hugo Hudson157dde12011-07-21 23:28:13 +0100184 Uri voicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100185 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, voicemailUri);
186 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
187 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
188 }
189 playbackFragment.setArguments(fragmentArguments);
190 getFragmentManager().beginTransaction()
191 .add(R.id.voicemail_container, playbackFragment).commit();
Hugo Hudson757aa552011-07-20 01:15:25 +0100192 mAsyncQueryHandler.startVoicemailStatusQuery(voicemailUri);
193 } else {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100194 // No voicemail uri: hide the status view.
Hugo Hudson757aa552011-07-20 01:15:25 +0100195 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100196 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100197 }
198
Hugo Hudson157dde12011-07-21 23:28:13 +0100199 private boolean hasVoicemail() {
200 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI) != null;
201 }
202
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100203 /**
204 * Returns the list of URIs to show.
205 * <p>
206 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
207 * a list of ids in the call log added as an extra on the URI.
208 * <p>
209 * If both are available, the data on the intent takes precedence.
210 */
211 private Uri[] getCallLogEntryUris() {
212 Uri uri = getIntent().getData();
213 if (uri != null) {
214 // If there is a data on the intent, it takes precedence over the extra.
215 return new Uri[]{ uri };
216 }
217 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
218 Uri[] uris = new Uri[ids.length];
219 for (int index = 0; index < ids.length; ++index) {
220 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
221 }
222 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800223 }
224
225 @Override
226 public boolean onKeyDown(int keyCode, KeyEvent event) {
227 switch (keyCode) {
228 case KeyEvent.KEYCODE_CALL: {
229 // Make sure phone isn't already busy before starting direct call
230 TelephonyManager tm = (TelephonyManager)
231 getSystemService(Context.TELEPHONY_SERVICE);
232 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
233 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
234 Uri.fromParts("tel", mNumber, null));
235 startActivity(callIntent);
236 return true;
237 }
238 }
239 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700240
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800241 return super.onKeyDown(keyCode, event);
242 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700243
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800244 /**
245 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700246 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100247 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800248 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100249 private void updateData(final Uri... callUris) {
250 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
251 final int numCalls = callUris.length;
252 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
253 try {
254 for (int index = 0; index < numCalls; ++index) {
255 details[index] = getPhoneCallDetailsForUri(callUris[index]);
256 }
257 } catch (IllegalArgumentException e) {
258 // Something went wrong reading in our primary data, so we're going to
259 // bail out and show error to users.
260 Log.w(TAG, "invalid URI starting call details", e);
261 Toast.makeText(this, R.string.toast_call_detail_error,
262 Toast.LENGTH_SHORT).show();
263 finish();
264 return;
265 }
266
267 // We know that all calls are from the same number and the same contact, so pick the first.
268 mNumber = details[0].number.toString();
269 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700270 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100271
272 // Set the details header, based on the first phone call.
273 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda40569562011-07-22 21:51:29 +0100274 details[0], false, false, true);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100275
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100276 // Cache the details about the phone number.
277 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
278 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
279 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
280 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
281
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100282 // Let user view contact details if they exist, otherwise add option to create new contact
283 // from this number.
284 final Intent mainActionIntent;
285 final int mainActionIcon;
286
287 if (details[0].personId != -1) {
288 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
289 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
290 mainActionIcon = R.drawable.sym_action_view_contact;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100291 } else if (isVoicemailNumber) {
292 mainActionIntent = null;
293 mainActionIcon = 0;
294 } else if (isSipNumber) {
295 // TODO: This item is currently disabled for SIP addresses, because
296 // the Insert.PHONE extra only works correctly for PSTN numbers.
297 //
298 // To fix this for SIP addresses, we need to:
299 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
300 // the current number is a SIP address
301 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
302 // updating the SipAddress field
303 // and then we can remove the "!isSipNumber" check above.
304 mainActionIntent = null;
305 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100306 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100307 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
308 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
309 mainActionIntent.putExtra(Insert.PHONE, mNumber);
310 mainActionIcon = R.drawable.sym_action_add;
Flavio Lerda8e403402011-07-20 16:25:49 +0100311 } else {
312 // If we cannot call the number, when we probably cannot add it as a contact either.
313 // This is usually the case of private, unknown, or payphone numbers.
314 mainActionIntent = null;
315 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100316 }
317
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100318 if (mainActionIntent == null) {
319 mMainActionView.setVisibility(View.INVISIBLE);
320 } else {
321 mMainActionView.setVisibility(View.VISIBLE);
322 mMainActionView.setImageResource(mainActionIcon);
323 mMainActionView.setOnClickListener(new View.OnClickListener() {
324 @Override
325 public void onClick(View v) {
326 startActivity(mainActionIntent);
327 }
328 });
329 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100330
331 // Build list of various available actions.
332 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
333
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100334 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100335 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100336 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
337 getString(R.string.menu_callNumber, mNumber),
338 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
339 }
340
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100341 // This action allows to send an SMS to the number that placed the call.
342 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100343 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
344 Uri.fromParts("sms", mNumber, null));
345 actions.add(new ViewEntry(R.drawable.sym_action_sms,
346 getString(R.string.menu_sendTextMessage), smsIntent));
347 }
348
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100349 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100350
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100351 if (actions.size() != 0) {
352 // Set the actions for this phone number.
353 setListAdapter(new ViewAdapter(this, actions));
354 getListView().setVisibility(View.VISIBLE);
355 } else {
356 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100357 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100358
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100359 ListView historyList = (ListView) findViewById(R.id.history);
360 historyList.setAdapter(
361 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700362 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100363 }
364
365 /** Return the phone call details for a given call log URI. */
366 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800367 ContentResolver resolver = getContentResolver();
368 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
369 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100370 if (callCursor == null || !callCursor.moveToFirst()) {
371 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800372 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100373
374 // Read call log specifics.
375 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
376 long date = callCursor.getLong(DATE_COLUMN_INDEX);
377 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
378 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
379 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
380 if (TextUtils.isEmpty(countryIso)) {
381 countryIso = mDefaultCountryIso;
382 }
383
384 // Formatted phone number.
385 final CharSequence numberText;
386 // Read contact specifics.
387 CharSequence nameText = "";
388 int numberType = 0;
389 CharSequence numberLabel = "";
390 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700391 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100392 // If this is not a regular number, there is no point in looking it up in the contacts.
393 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
394 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
395 } else {
396 // Perform a reverse-phonebook lookup to find the contact details.
397 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
398 Uri.encode(number));
399 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
400 String candidateNumberText = number;
401 try {
402 if (phonesCursor != null && phonesCursor.moveToFirst()) {
403 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
404 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700405 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
406 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100407 candidateNumberText = PhoneNumberUtils.formatNumber(
408 phonesCursor.getString(COLUMN_INDEX_NUMBER),
409 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
410 countryIso);
411 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
412 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
413 } else {
414 // We could not find this contact in the contacts, just format the phone
415 // number as best as we can. All the other fields will have their default
416 // values.
417 candidateNumberText =
418 PhoneNumberUtils.formatNumber(number, countryIso);
419 }
420 } finally {
421 if (phonesCursor != null) phonesCursor.close();
422 numberText = candidateNumberText;
423 }
424 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100425 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100426 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700427 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800428 } finally {
429 if (callCursor != null) {
430 callCursor.close();
431 }
432 }
433 }
434
Flavio Lerda9cafe472011-06-08 14:06:13 +0100435 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700436 private void loadContactPhotos(Uri photoUri) {
437 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100438 }
439
Flavio Lerda696e8132011-07-05 19:00:23 +0100440 private String getVoicemailNumber() {
441 TelephonyManager telephonyManager =
442 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
443 return telephonyManager.getVoiceMailNumber();
444 }
445
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800446 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100447 public final int icon;
448 public final String text;
449 public final Intent intent;
450 public final View.OnClickListener action;
451
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800452 public String label = null;
453 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700454
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800455 public ViewEntry(int icon, String text, Intent intent) {
456 this.icon = icon;
457 this.text = text;
458 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100459 this.action = null;
460 }
461
462 public ViewEntry(int icon, String text, View.OnClickListener listener) {
463 this.icon = icon;
464 this.text = text;
465 this.intent = null;
466 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800467 }
468 }
469
470 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700471
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800472 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700473
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800474 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700475
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800476 public ViewAdapter(Context context, List<ViewEntry> actions) {
477 mActions = actions;
478 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
479 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700480
Flavio Lerda9cafe472011-06-08 14:06:13 +0100481 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800482 public int getCount() {
483 return mActions.size();
484 }
485
Flavio Lerda9cafe472011-06-08 14:06:13 +0100486 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800487 public Object getItem(int position) {
488 return mActions.get(position);
489 }
490
Flavio Lerda9cafe472011-06-08 14:06:13 +0100491 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800492 public long getItemId(int position) {
493 return position;
494 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700495
Flavio Lerda9cafe472011-06-08 14:06:13 +0100496 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800497 public View getView(int position, View convertView, ViewGroup parent) {
498 // Make sure we have a valid convertView to start with
499 if (convertView == null) {
500 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
501 }
502
503 // Fill action with icon and text.
504 ViewEntry entry = mActions.get(position);
505 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700506
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800507 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
508 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
509
510 icon.setImageResource(entry.icon);
511 text.setText(entry.text);
512
513 View line2 = convertView.findViewById(R.id.line2);
514 boolean numberEmpty = TextUtils.isEmpty(entry.number);
515 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
516 if (labelEmpty && numberEmpty) {
517 line2.setVisibility(View.GONE);
518 } else {
519 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700520
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800521 TextView label = (TextView) convertView.findViewById(R.id.label);
522 if (labelEmpty) {
523 label.setVisibility(View.GONE);
524 } else {
525 label.setText(entry.label);
526 label.setVisibility(View.VISIBLE);
527 }
528
529 TextView number = (TextView) convertView.findViewById(R.id.number);
530 number.setText(entry.number);
531 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700532
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800533 return convertView;
534 }
535 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700536
Flavio Lerda9cafe472011-06-08 14:06:13 +0100537 @Override
538 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800539 // Handle passing action off to correct handler.
540 if (view.getTag() instanceof ViewEntry) {
541 ViewEntry entry = (ViewEntry) view.getTag();
542 if (entry.intent != null) {
543 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100544 } else if (entry.action != null) {
545 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800546 }
547 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700548 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800549
550 @Override
551 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
552 boolean globalSearch) {
553 if (globalSearch) {
554 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
555 } else {
556 ContactsSearchManager.startSearch(this, initialQuery);
557 }
558 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100559
560 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
561 if (statusCursor == null) {
562 mStatusMessageView.setVisibility(View.GONE);
563 return;
564 }
565 final StatusMessage message = getStatusMessage(statusCursor);
566 if (message == null || !message.showInCallDetails()) {
567 mStatusMessageView.setVisibility(View.GONE);
568 return;
569 }
570
571 mStatusMessageView.setVisibility(View.VISIBLE);
572 mStatusMessageText.setText(message.callDetailsMessageId);
573 if (message.actionMessageId != -1) {
574 mStatusMessageAction.setText(message.actionMessageId);
575 }
576 if (message.actionUri != null) {
577 mStatusMessageAction.setClickable(true);
578 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
579 @Override
580 public void onClick(View v) {
581 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
582 }
583 });
584 } else {
585 mStatusMessageAction.setClickable(false);
586 }
587 }
588
589 private StatusMessage getStatusMessage(Cursor statusCursor) {
590 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
591 Log.d(TAG, "Num status messages: " + messages.size());
592 if (messages.size() == 0) {
593 return null;
594 }
595 // There can only be a single status message per source package, so num of messages can
596 // at most be 1.
597 if (messages.size() > 1) {
598 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
599 " Will use the first one.", messages.size()));
600 }
601 return messages.get(0);
602 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100603
604 @Override
605 public boolean onCreateOptionsMenu(Menu menu) {
606 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100607 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100608 }
609
610 @Override
611 public boolean onPrepareOptionsMenu(Menu menu) {
612 // This action deletes all elements in the group from the call log.
613 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100614 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
615 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
616 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
617 menu.findItem(R.id.menu_share_voicemail).setVisible(hasVoicemail());
618 return super.onPrepareOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100619 }
620
621 @Override
622 public boolean onMenuItemSelected(int featureId, MenuItem item) {
623 switch (item.getItemId()) {
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100624 case android.R.id.home: {
625 onHomeSelected();
626 return true;
627 }
628
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100629 // All the options menu items are handled by onMenu... methods.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100630 default:
631 throw new IllegalArgumentException();
632 }
633 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100634
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100635 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
636 StringBuilder callIds = new StringBuilder();
637 for (Uri callUri : getCallLogEntryUris()) {
638 if (callIds.length() != 0) {
639 callIds.append(",");
640 }
641 callIds.append(ContentUris.parseId(callUri));
642 }
643
644 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
645 Calls._ID + " IN (" + callIds + ")", null);
646 // Also close the activity.
647 finish();
648 }
649
650 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
651 startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
652 }
653
654 public void onMenuShareVoicemail(MenuItem menuItem) {
655 Log.w(TAG, "onMenuShareVoicemail not yet implemented");
656 }
657
658 public void onMenuTrashVoicemail(MenuItem menuItem) {
659 Log.w(TAG, "onMenuTrashVoicemail not yet implemented");
660 }
661
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100662 private void configureActionBar() {
663 ActionBar actionBar = getActionBar();
664 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100665 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100666 actionBar.setIcon(R.drawable.ic_ab_dialer_holo_dark);
667 }
668 }
669
670 /** Invoked when the user presses the home button in the action bar. */
671 private void onHomeSelected() {
672 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
673 // This will open the call log even if the detail view has been opened directly.
674 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
675 startActivity(intent);
676 finish();
677 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800678}