blob: a5ceafa88a1b47fc54753ef5e66ecd4879259a47 [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 Hudson51ada362011-08-05 14:36:14 +010022import com.android.contacts.util.AbstractBackgroundTask;
23import com.android.contacts.util.BackgroundTask;
24import com.android.contacts.util.BackgroundTaskService;
Hugo Hudsonb002f512011-07-15 17:41:12 +010025import com.android.contacts.voicemail.VoicemailPlaybackFragment;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010026import com.android.contacts.voicemail.VoicemailStatusHelper;
27import com.android.contacts.voicemail.VoicemailStatusHelper.StatusMessage;
28import com.android.contacts.voicemail.VoicemailStatusHelperImpl;
Amith Yamasani8bbe2f22009-03-24 21:24:12 -070029
Flavio Lerdad44e83a2011-07-24 23:10:17 +010030import android.app.ActionBar;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080031import android.app.ListActivity;
32import android.content.ContentResolver;
33import android.content.ContentUris;
Debashish Chatterjee0a139002011-08-02 18:27:11 +010034import android.content.ContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080035import android.content.Context;
36import android.content.Intent;
37import android.content.res.Resources;
38import android.database.Cursor;
39import android.net.Uri;
40import android.os.Bundle;
41import android.provider.CallLog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080042import android.provider.CallLog.Calls;
Flavio Lerda9cafe472011-06-08 14:06:13 +010043import android.provider.Contacts.Intents.Insert;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +010044import android.provider.ContactsContract.CommonDataKinds.Phone;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070045import android.provider.ContactsContract.Contacts;
46import android.provider.ContactsContract.PhoneLookup;
Debashish Chatterjee0a139002011-08-02 18:27:11 +010047import android.provider.VoicemailContract.Voicemails;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080048import android.telephony.PhoneNumberUtils;
49import android.telephony.TelephonyManager;
50import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010051import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080052import android.view.KeyEvent;
53import android.view.LayoutInflater;
Flavio Lerda94d7bab2011-07-22 16:52:34 +010054import android.view.Menu;
55import android.view.MenuItem;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080056import android.view.View;
57import android.view.ViewGroup;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080058import android.widget.BaseAdapter;
Daniel Lehmannd260de42011-08-01 18:08:38 -070059import android.widget.ImageButton;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080060import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010061import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080062import android.widget.TextView;
63import android.widget.Toast;
64
65import java.util.ArrayList;
66import java.util.List;
67
68/**
69 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010070 * <p>
71 * This activity can be either started with the URI of a single call log entry, or with the
72 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080073 */
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +010074public class CallDetailActivity extends ListActivity {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080075 private static final String TAG = "CallDetail";
76
Flavio Lerda178eeeb2011-07-11 19:51:40 +010077 /** A long array extra containing ids of call log entries to display. */
Hugo Hudsonb002f512011-07-15 17:41:12 +010078 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
79 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
80 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
81 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
82 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
Flavio Lerda178eeeb2011-07-11 19:51:40 +010083
Flavio Lerda9a208cc2011-07-12 21:05:47 +010084 private CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010085 private PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010086 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
Flavio Lerdab88abaa2011-08-02 23:38:36 +010087 private TextView mHeaderTextView;
Flavio Lerdafb63c432011-07-07 17:16:53 +010088 private ImageView mMainActionView;
Daniel Lehmannd260de42011-08-01 18:08:38 -070089 private ImageButton mMainActionPushLayerView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010090 private ImageView mContactBackgroundView;
Hugo Hudson51ada362011-08-05 14:36:14 +010091 private BackgroundTaskService mBackgroundTaskService;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080092
93 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080094 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070095
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080096 /* package */ LayoutInflater mInflater;
97 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010098 /** Helper to load contact photos. */
99 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100100 /** Helper to make async queries to content resolver. */
101 private CallDetailActivityQueryHandler mAsyncQueryHandler;
102 /** Helper to get voicemail status messages. */
103 private VoicemailStatusHelper mVoicemailStatusHelper;
104 // Views related to voicemail status message.
105 private View mStatusMessageView;
106 private TextView mStatusMessageText;
107 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700108
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100109 /** Whether we should show "edit number before call" in the options menu. */
110 private boolean mHasEditNumberBeforeCall;
111
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800112 static final String[] CALL_LOG_PROJECTION = new String[] {
113 CallLog.Calls.DATE,
114 CallLog.Calls.DURATION,
115 CallLog.Calls.NUMBER,
116 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800117 CallLog.Calls.COUNTRY_ISO,
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100118 CallLog.Calls.GEOCODED_LOCATION,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800119 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700120
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800121 static final int DATE_COLUMN_INDEX = 0;
122 static final int DURATION_COLUMN_INDEX = 1;
123 static final int NUMBER_COLUMN_INDEX = 2;
124 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800125 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100126 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700127
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800128 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700129 PhoneLookup._ID,
130 PhoneLookup.DISPLAY_NAME,
131 PhoneLookup.TYPE,
132 PhoneLookup.LABEL,
133 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800134 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700135 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800136 };
137 static final int COLUMN_INDEX_ID = 0;
138 static final int COLUMN_INDEX_NAME = 1;
139 static final int COLUMN_INDEX_TYPE = 2;
140 static final int COLUMN_INDEX_LABEL = 3;
141 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800142 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700143 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800144
145 @Override
146 protected void onCreate(Bundle icicle) {
147 super.onCreate(icicle);
148
149 setContentView(R.layout.call_detail);
150
Hugo Hudson51ada362011-08-05 14:36:14 +0100151 mBackgroundTaskService = (BackgroundTaskService) getApplicationContext().getSystemService(
152 BackgroundTaskService.BACKGROUND_TASK_SERVICE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800153 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
154 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700155
Daniel Lehmann6ecb7322011-08-01 21:39:29 -0700156 mCallTypeHelper = new CallTypeHelper(getResources());
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100157 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
Flavio Lerda40569562011-07-22 21:51:29 +0100158 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100159 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100160 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
161 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100162 mHeaderTextView = (TextView) findViewById(R.id.header_text);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100163 mStatusMessageView = findViewById(R.id.voicemail_status);
164 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
165 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100166 mMainActionView = (ImageView) findViewById(R.id.main_action);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700167 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100168 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800169 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100170 mContactPhotoManager = ContactPhotoManager.getInstance(this);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100171 configureActionBar();
Hugo Hudson51ada362011-08-05 14:36:14 +0100172 optionallyHandleVoicemail();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800173 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700174
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800175 @Override
176 public void onResume() {
177 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100178 updateData(getCallLogEntryUris());
Hugo Hudsonb002f512011-07-15 17:41:12 +0100179 }
180
181 /**
182 * Handle voicemail playback or hide voicemail ui.
183 * <p>
184 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
185 * playback. If it doesn't, then hide the voicemail ui.
186 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100187 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100188 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100189 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
190 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100191 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100192 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
193 Bundle fragmentArguments = new Bundle();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100194 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100195 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
196 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
197 }
198 playbackFragment.setArguments(fragmentArguments);
199 getFragmentManager().beginTransaction()
200 .add(R.id.voicemail_container, playbackFragment).commit();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100201 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100202 markVoicemailAsRead(getVoicemailUri());
Hugo Hudson757aa552011-07-20 01:15:25 +0100203 } else {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100204 // No voicemail uri: hide the status view.
Hugo Hudson757aa552011-07-20 01:15:25 +0100205 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100206 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100207 }
208
Hugo Hudson157dde12011-07-21 23:28:13 +0100209 private boolean hasVoicemail() {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100210 return getVoicemailUri() != null;
211 }
212
213 private Uri getVoicemailUri() {
214 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudson157dde12011-07-21 23:28:13 +0100215 }
216
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100217 private void markVoicemailAsRead(final Uri voicemailUri) {
Hugo Hudson51ada362011-08-05 14:36:14 +0100218 mBackgroundTaskService.submit(new AbstractBackgroundTask() {
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100219 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100220 public void doInBackground() {
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100221 ContentValues values = new ContentValues();
222 values.put(Voicemails.IS_READ, true);
223 getContentResolver().update(voicemailUri, values, null, null);
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100224 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100225 });
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100226 }
227
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100228 /**
229 * Returns the list of URIs to show.
230 * <p>
231 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
232 * a list of ids in the call log added as an extra on the URI.
233 * <p>
234 * If both are available, the data on the intent takes precedence.
235 */
236 private Uri[] getCallLogEntryUris() {
237 Uri uri = getIntent().getData();
238 if (uri != null) {
239 // If there is a data on the intent, it takes precedence over the extra.
240 return new Uri[]{ uri };
241 }
242 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
243 Uri[] uris = new Uri[ids.length];
244 for (int index = 0; index < ids.length; ++index) {
245 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
246 }
247 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800248 }
249
250 @Override
251 public boolean onKeyDown(int keyCode, KeyEvent event) {
252 switch (keyCode) {
253 case KeyEvent.KEYCODE_CALL: {
254 // Make sure phone isn't already busy before starting direct call
255 TelephonyManager tm = (TelephonyManager)
256 getSystemService(Context.TELEPHONY_SERVICE);
257 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
258 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
259 Uri.fromParts("tel", mNumber, null));
260 startActivity(callIntent);
261 return true;
262 }
263 }
264 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700265
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800266 return super.onKeyDown(keyCode, event);
267 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700268
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800269 /**
270 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700271 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100272 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800273 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100274 private void updateData(final Uri... callUris) {
Flavio Lerda12592e82011-08-10 15:36:32 +0100275 mBackgroundTaskService.submit(new BackgroundTask() {
276 private PhoneCallDetails[] details;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100277
Flavio Lerda12592e82011-08-10 15:36:32 +0100278 @Override
279 public void doInBackground() {
280 // TODO: All phone calls correspond to the same person, so we can make a single
281 // lookup.
282 final int numCalls = callUris.length;
283 details = new PhoneCallDetails[numCalls];
284 try {
285 for (int index = 0; index < numCalls; ++index) {
286 details[index] = getPhoneCallDetailsForUri(callUris[index]);
287 }
288 } catch (IllegalArgumentException e) {
289 // Something went wrong reading in our primary data.
290 Log.w(TAG, "invalid URI starting call details", e);
291 details = null;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100292 }
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100293 }
294
Flavio Lerda12592e82011-08-10 15:36:32 +0100295 @Override
296 public void onPostExecute() {
297 if (details == null) {
298 // Somewhere went wrong: we're going to bail out and show error to users.
299 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
300 Toast.LENGTH_SHORT).show();
301 finish();
302 return;
303 }
304
305 // We know that all calls are from the same number and the same contact, so pick the
306 // first.
307 PhoneCallDetails firstDetails = details[0];
308 mNumber = firstDetails.number.toString();
309 final long personId = firstDetails.personId;
310 final Uri photoUri = firstDetails.photoUri;
311
312 // Set the details header, based on the first phone call.
313 mPhoneCallDetailsHelper.setPhoneCallName(mHeaderTextView, firstDetails);
314
315 // Cache the details about the phone number.
316 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
317 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
318 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
319 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
320
321 // Let user view contact details if they exist, otherwise add option to create new
322 // contact from this number.
323 final Intent mainActionIntent;
324 final int mainActionIcon;
325
326 if (firstDetails.personId != -1) {
327 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
328 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
329 mainActionIcon = R.drawable.ic_contacts_holo_dark;
330 } else if (isVoicemailNumber) {
331 mainActionIntent = null;
332 mainActionIcon = 0;
333 } else if (isSipNumber) {
334 // TODO: This item is currently disabled for SIP addresses, because
335 // the Insert.PHONE extra only works correctly for PSTN numbers.
336 //
337 // To fix this for SIP addresses, we need to:
338 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
339 // the current number is a SIP address
340 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
341 // updating the SipAddress field
342 // and then we can remove the "!isSipNumber" check above.
343 mainActionIntent = null;
344 mainActionIcon = 0;
345 } else if (canPlaceCallsTo) {
346 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
347 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
348 mainActionIntent.putExtra(Insert.PHONE, mNumber);
349 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
350 } else {
351 // If we cannot call the number, when we probably cannot add it as a contact either.
352 // This is usually the case of private, unknown, or payphone numbers.
353 mainActionIntent = null;
354 mainActionIcon = 0;
355 }
356
357 if (mainActionIntent == null) {
358 mMainActionView.setVisibility(View.INVISIBLE);
359 mMainActionPushLayerView.setVisibility(View.GONE);
360 } else {
361 mMainActionView.setVisibility(View.VISIBLE);
362 mMainActionView.setImageResource(mainActionIcon);
363 mMainActionPushLayerView.setVisibility(View.VISIBLE);
364 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
365 @Override
366 public void onClick(View v) {
367 startActivity(mainActionIntent);
368 }
369 });
370 }
371
372 // Build list of various available actions.
373 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
374
375 // This action allows to call the number that places the call.
376 if (canPlaceCallsTo) {
377 final CharSequence displayNumber =
378 mPhoneNumberHelper.getDisplayNumber(
379 firstDetails.number, firstDetails.formattedNumber);
380
381 ViewEntry entry = new ViewEntry(
382 getString(R.string.menu_callNumber, displayNumber),
383 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri));
384
385 // Only show a label if the number is shown and it is not a SIP address.
386 if (!TextUtils.isEmpty(firstDetails.number)
387 && !PhoneNumberUtils.isUriNumber(firstDetails.number.toString())) {
388 entry.label = Phone.getTypeLabel(mResources, firstDetails.numberType,
389 firstDetails.numberLabel);
390 }
391
392 // The secondary action allows to send an SMS to the number that placed the
393 // call.
394 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
395 entry.setSecondaryAction(R.drawable.ic_text_holo_dark,
396 new Intent(Intent.ACTION_SENDTO,
397 Uri.fromParts("sms", mNumber, null)));
398 }
399
400 actions.add(entry);
401 }
402
403 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
404
405 if (actions.size() != 0) {
406 // Set the actions for this phone number.
407 setListAdapter(new ViewAdapter(CallDetailActivity.this, actions));
408 getListView().setVisibility(View.VISIBLE);
409 getListView().setItemsCanFocus(true);
410 } else {
411 getListView().setVisibility(View.GONE);
412 }
413
414 ListView historyList = (ListView) findViewById(R.id.history);
415 historyList.setAdapter(
416 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
417 mCallTypeHelper, details));
418 loadContactPhotos(photoUri);
Flavio Lerda4e63bab2011-08-10 18:26:46 +0100419 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100420 }
Flavio Lerda12592e82011-08-10 15:36:32 +0100421 });
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100422 }
423
424 /** Return the phone call details for a given call log URI. */
425 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800426 ContentResolver resolver = getContentResolver();
427 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
428 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100429 if (callCursor == null || !callCursor.moveToFirst()) {
430 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800431 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100432
433 // Read call log specifics.
434 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
435 long date = callCursor.getLong(DATE_COLUMN_INDEX);
436 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
437 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
438 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100439 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
440
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100441 if (TextUtils.isEmpty(countryIso)) {
442 countryIso = mDefaultCountryIso;
443 }
444
445 // Formatted phone number.
446 final CharSequence numberText;
447 // Read contact specifics.
448 CharSequence nameText = "";
449 int numberType = 0;
450 CharSequence numberLabel = "";
451 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700452 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100453 // If this is not a regular number, there is no point in looking it up in the contacts.
454 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
455 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
456 } else {
457 // Perform a reverse-phonebook lookup to find the contact details.
458 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
459 Uri.encode(number));
460 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
461 String candidateNumberText = number;
462 try {
463 if (phonesCursor != null && phonesCursor.moveToFirst()) {
464 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
465 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700466 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
467 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100468 candidateNumberText = PhoneNumberUtils.formatNumber(
469 phonesCursor.getString(COLUMN_INDEX_NUMBER),
470 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
471 countryIso);
472 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
473 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
474 } else {
475 // We could not find this contact in the contacts, just format the phone
476 // number as best as we can. All the other fields will have their default
477 // values.
478 candidateNumberText =
479 PhoneNumberUtils.formatNumber(number, countryIso);
480 }
481 } finally {
482 if (phonesCursor != null) phonesCursor.close();
483 numberText = candidateNumberText;
484 }
485 }
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100486 return new PhoneCallDetails(number, numberText, countryIso, geocode,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100487 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700488 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800489 } finally {
490 if (callCursor != null) {
491 callCursor.close();
492 }
493 }
494 }
495
Flavio Lerda9cafe472011-06-08 14:06:13 +0100496 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700497 private void loadContactPhotos(Uri photoUri) {
498 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100499 }
500
Flavio Lerda696e8132011-07-05 19:00:23 +0100501 private String getVoicemailNumber() {
502 TelephonyManager telephonyManager =
503 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
504 return telephonyManager.getVoiceMailNumber();
505 }
506
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800507 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100508 public final String text;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100509 public final Intent primaryIntent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100510
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100511 public CharSequence label = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800512 public String number = null;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100513 /** Icon for the secondary action. */
514 public int secondaryIcon = 0;
515 /** Intent for the secondary action. If not null, an icon must be defined. */
516 public Intent secondaryIntent = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700517
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100518 public ViewEntry(String text, Intent intent) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800519 this.text = text;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100520 this.primaryIntent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100521 }
522
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100523 public void setSecondaryAction(int icon, Intent intent) {
524 secondaryIcon = icon;
525 secondaryIntent = intent;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800526 }
527 }
528
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100529 private static final class ViewAdapter extends BaseAdapter {
530 private final Context mContext;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800531 private final List<ViewEntry> mActions;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800532 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700533
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100534 private final View.OnClickListener mPrimaryActionListener = new View.OnClickListener() {
535 @Override
536 public void onClick(View view) {
537 ViewEntry entry = (ViewEntry) view.getTag();
538 mContext.startActivity(entry.primaryIntent);
539 }
540 };
541
542 private final View.OnClickListener mSecondaryActionListener = new View.OnClickListener() {
543 @Override
544 public void onClick(View view) {
545 ViewEntry entry = (ViewEntry) view.getTag();
546 mContext.startActivity(entry.secondaryIntent);
547 }
548 };
549
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800550 public ViewAdapter(Context context, List<ViewEntry> actions) {
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100551 mContext = context;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800552 mActions = actions;
553 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
554 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700555
Flavio Lerda9cafe472011-06-08 14:06:13 +0100556 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800557 public int getCount() {
558 return mActions.size();
559 }
560
Flavio Lerda9cafe472011-06-08 14:06:13 +0100561 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800562 public Object getItem(int position) {
563 return mActions.get(position);
564 }
565
Flavio Lerda9cafe472011-06-08 14:06:13 +0100566 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800567 public long getItemId(int position) {
568 return position;
569 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700570
Flavio Lerda9cafe472011-06-08 14:06:13 +0100571 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800572 public View getView(int position, View convertView, ViewGroup parent) {
573 // Make sure we have a valid convertView to start with
574 if (convertView == null) {
575 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
576 }
577
578 // Fill action with icon and text.
579 ViewEntry entry = mActions.get(position);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700580
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800581 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100582 View divider = convertView.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800583 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
584
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100585 View mainAction = convertView.findViewById(R.id.main_action);
586 mainAction.setOnClickListener(mPrimaryActionListener);
587 mainAction.setTag(entry);
588
589 if (entry.secondaryIntent != null) {
590 icon.setOnClickListener(mSecondaryActionListener);
591 icon.setImageResource(entry.secondaryIcon);
592 icon.setVisibility(View.VISIBLE);
593 icon.setTag(entry);
594 divider.setVisibility(View.VISIBLE);
595 } else {
596 icon.setVisibility(View.GONE);
597 divider.setVisibility(View.GONE);
598 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800599 text.setText(entry.text);
600
601 View line2 = convertView.findViewById(R.id.line2);
602 boolean numberEmpty = TextUtils.isEmpty(entry.number);
603 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
604 if (labelEmpty && numberEmpty) {
605 line2.setVisibility(View.GONE);
606 } else {
607 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700608
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800609 TextView label = (TextView) convertView.findViewById(R.id.label);
610 if (labelEmpty) {
611 label.setVisibility(View.GONE);
612 } else {
613 label.setText(entry.label);
614 label.setVisibility(View.VISIBLE);
615 }
616
617 TextView number = (TextView) convertView.findViewById(R.id.number);
618 number.setText(entry.number);
619 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700620
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800621 return convertView;
622 }
623 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700624
Flavio Lerda9cafe472011-06-08 14:06:13 +0100625 @Override
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800626 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
627 boolean globalSearch) {
628 if (globalSearch) {
629 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
630 } else {
631 ContactsSearchManager.startSearch(this, initialQuery);
632 }
633 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100634
635 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
636 if (statusCursor == null) {
637 mStatusMessageView.setVisibility(View.GONE);
638 return;
639 }
640 final StatusMessage message = getStatusMessage(statusCursor);
641 if (message == null || !message.showInCallDetails()) {
642 mStatusMessageView.setVisibility(View.GONE);
643 return;
644 }
645
646 mStatusMessageView.setVisibility(View.VISIBLE);
647 mStatusMessageText.setText(message.callDetailsMessageId);
648 if (message.actionMessageId != -1) {
649 mStatusMessageAction.setText(message.actionMessageId);
650 }
651 if (message.actionUri != null) {
652 mStatusMessageAction.setClickable(true);
653 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
654 @Override
655 public void onClick(View v) {
656 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
657 }
658 });
659 } else {
660 mStatusMessageAction.setClickable(false);
661 }
662 }
663
664 private StatusMessage getStatusMessage(Cursor statusCursor) {
665 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
666 Log.d(TAG, "Num status messages: " + messages.size());
667 if (messages.size() == 0) {
668 return null;
669 }
670 // There can only be a single status message per source package, so num of messages can
671 // at most be 1.
672 if (messages.size() > 1) {
673 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
674 " Will use the first one.", messages.size()));
675 }
676 return messages.get(0);
677 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100678
679 @Override
680 public boolean onCreateOptionsMenu(Menu menu) {
681 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100682 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100683 }
684
685 @Override
686 public boolean onPrepareOptionsMenu(Menu menu) {
687 // This action deletes all elements in the group from the call log.
688 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100689 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
690 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
691 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100692 return super.onPrepareOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100693 }
694
695 @Override
696 public boolean onMenuItemSelected(int featureId, MenuItem item) {
697 switch (item.getItemId()) {
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100698 case android.R.id.home: {
699 onHomeSelected();
700 return true;
701 }
702
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100703 // All the options menu items are handled by onMenu... methods.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100704 default:
705 throw new IllegalArgumentException();
706 }
707 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100708
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100709 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100710 final StringBuilder callIds = new StringBuilder();
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100711 for (Uri callUri : getCallLogEntryUris()) {
712 if (callIds.length() != 0) {
713 callIds.append(",");
714 }
715 callIds.append(ContentUris.parseId(callUri));
716 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100717 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100718 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100719 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100720 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
721 Calls._ID + " IN (" + callIds + ")", null);
722 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100723 @Override
724 public void onPostExecute() {
725 finish();
726 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100727 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100728 }
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100729 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
730 startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
731 }
732
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100733 public void onMenuTrashVoicemail(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100734 final Uri voicemailUri = getVoicemailUri();
Hugo Hudson51ada362011-08-05 14:36:14 +0100735 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100736 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100737 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100738 getContentResolver().delete(voicemailUri, null, null);
739 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100740 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100741 public void onPostExecute() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100742 finish();
743 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100744 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100745 }
746
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100747 private void configureActionBar() {
748 ActionBar actionBar = getActionBar();
749 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100750 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100751 }
752 }
753
754 /** Invoked when the user presses the home button in the action bar. */
755 private void onHomeSelected() {
756 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
757 // This will open the call log even if the detail view has been opened directly.
758 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
759 startActivity(intent);
760 finish();
761 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800762}