blob: ed1c9756f22215d3bc2d557d8774128c4e58eb92 [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 Lerda8ebfa3d2011-08-10 14:35:22 +0100419 }
Flavio Lerda12592e82011-08-10 15:36:32 +0100420 });
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100421 }
422
423 /** Return the phone call details for a given call log URI. */
424 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800425 ContentResolver resolver = getContentResolver();
426 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
427 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100428 if (callCursor == null || !callCursor.moveToFirst()) {
429 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800430 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100431
432 // Read call log specifics.
433 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
434 long date = callCursor.getLong(DATE_COLUMN_INDEX);
435 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
436 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
437 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100438 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
439
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100440 if (TextUtils.isEmpty(countryIso)) {
441 countryIso = mDefaultCountryIso;
442 }
443
444 // Formatted phone number.
445 final CharSequence numberText;
446 // Read contact specifics.
447 CharSequence nameText = "";
448 int numberType = 0;
449 CharSequence numberLabel = "";
450 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700451 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100452 // If this is not a regular number, there is no point in looking it up in the contacts.
453 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
454 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
455 } else {
456 // Perform a reverse-phonebook lookup to find the contact details.
457 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
458 Uri.encode(number));
459 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
460 String candidateNumberText = number;
461 try {
462 if (phonesCursor != null && phonesCursor.moveToFirst()) {
463 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
464 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700465 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
466 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100467 candidateNumberText = PhoneNumberUtils.formatNumber(
468 phonesCursor.getString(COLUMN_INDEX_NUMBER),
469 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
470 countryIso);
471 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
472 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
473 } else {
474 // We could not find this contact in the contacts, just format the phone
475 // number as best as we can. All the other fields will have their default
476 // values.
477 candidateNumberText =
478 PhoneNumberUtils.formatNumber(number, countryIso);
479 }
480 } finally {
481 if (phonesCursor != null) phonesCursor.close();
482 numberText = candidateNumberText;
483 }
484 }
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100485 return new PhoneCallDetails(number, numberText, countryIso, geocode,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100486 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700487 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800488 } finally {
489 if (callCursor != null) {
490 callCursor.close();
491 }
492 }
493 }
494
Flavio Lerda9cafe472011-06-08 14:06:13 +0100495 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700496 private void loadContactPhotos(Uri photoUri) {
497 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100498 }
499
Flavio Lerda696e8132011-07-05 19:00:23 +0100500 private String getVoicemailNumber() {
501 TelephonyManager telephonyManager =
502 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
503 return telephonyManager.getVoiceMailNumber();
504 }
505
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800506 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100507 public final String text;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100508 public final Intent primaryIntent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100509
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100510 public CharSequence label = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800511 public String number = null;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100512 /** Icon for the secondary action. */
513 public int secondaryIcon = 0;
514 /** Intent for the secondary action. If not null, an icon must be defined. */
515 public Intent secondaryIntent = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700516
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100517 public ViewEntry(String text, Intent intent) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800518 this.text = text;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100519 this.primaryIntent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100520 }
521
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100522 public void setSecondaryAction(int icon, Intent intent) {
523 secondaryIcon = icon;
524 secondaryIntent = intent;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800525 }
526 }
527
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100528 private static final class ViewAdapter extends BaseAdapter {
529 private final Context mContext;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800530 private final List<ViewEntry> mActions;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800531 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700532
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100533 private final View.OnClickListener mPrimaryActionListener = new View.OnClickListener() {
534 @Override
535 public void onClick(View view) {
536 ViewEntry entry = (ViewEntry) view.getTag();
537 mContext.startActivity(entry.primaryIntent);
538 }
539 };
540
541 private final View.OnClickListener mSecondaryActionListener = new View.OnClickListener() {
542 @Override
543 public void onClick(View view) {
544 ViewEntry entry = (ViewEntry) view.getTag();
545 mContext.startActivity(entry.secondaryIntent);
546 }
547 };
548
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800549 public ViewAdapter(Context context, List<ViewEntry> actions) {
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100550 mContext = context;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800551 mActions = actions;
552 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
553 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700554
Flavio Lerda9cafe472011-06-08 14:06:13 +0100555 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800556 public int getCount() {
557 return mActions.size();
558 }
559
Flavio Lerda9cafe472011-06-08 14:06:13 +0100560 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800561 public Object getItem(int position) {
562 return mActions.get(position);
563 }
564
Flavio Lerda9cafe472011-06-08 14:06:13 +0100565 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800566 public long getItemId(int position) {
567 return position;
568 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700569
Flavio Lerda9cafe472011-06-08 14:06:13 +0100570 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800571 public View getView(int position, View convertView, ViewGroup parent) {
572 // Make sure we have a valid convertView to start with
573 if (convertView == null) {
574 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
575 }
576
577 // Fill action with icon and text.
578 ViewEntry entry = mActions.get(position);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700579
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800580 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100581 View divider = convertView.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800582 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
583
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100584 View mainAction = convertView.findViewById(R.id.main_action);
585 mainAction.setOnClickListener(mPrimaryActionListener);
586 mainAction.setTag(entry);
587
588 if (entry.secondaryIntent != null) {
589 icon.setOnClickListener(mSecondaryActionListener);
590 icon.setImageResource(entry.secondaryIcon);
591 icon.setVisibility(View.VISIBLE);
592 icon.setTag(entry);
593 divider.setVisibility(View.VISIBLE);
594 } else {
595 icon.setVisibility(View.GONE);
596 divider.setVisibility(View.GONE);
597 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800598 text.setText(entry.text);
599
600 View line2 = convertView.findViewById(R.id.line2);
601 boolean numberEmpty = TextUtils.isEmpty(entry.number);
602 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
603 if (labelEmpty && numberEmpty) {
604 line2.setVisibility(View.GONE);
605 } else {
606 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700607
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800608 TextView label = (TextView) convertView.findViewById(R.id.label);
609 if (labelEmpty) {
610 label.setVisibility(View.GONE);
611 } else {
612 label.setText(entry.label);
613 label.setVisibility(View.VISIBLE);
614 }
615
616 TextView number = (TextView) convertView.findViewById(R.id.number);
617 number.setText(entry.number);
618 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700619
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800620 return convertView;
621 }
622 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700623
Flavio Lerda9cafe472011-06-08 14:06:13 +0100624 @Override
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800625 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
626 boolean globalSearch) {
627 if (globalSearch) {
628 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
629 } else {
630 ContactsSearchManager.startSearch(this, initialQuery);
631 }
632 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100633
634 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
635 if (statusCursor == null) {
636 mStatusMessageView.setVisibility(View.GONE);
637 return;
638 }
639 final StatusMessage message = getStatusMessage(statusCursor);
640 if (message == null || !message.showInCallDetails()) {
641 mStatusMessageView.setVisibility(View.GONE);
642 return;
643 }
644
645 mStatusMessageView.setVisibility(View.VISIBLE);
646 mStatusMessageText.setText(message.callDetailsMessageId);
647 if (message.actionMessageId != -1) {
648 mStatusMessageAction.setText(message.actionMessageId);
649 }
650 if (message.actionUri != null) {
651 mStatusMessageAction.setClickable(true);
652 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
653 @Override
654 public void onClick(View v) {
655 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
656 }
657 });
658 } else {
659 mStatusMessageAction.setClickable(false);
660 }
661 }
662
663 private StatusMessage getStatusMessage(Cursor statusCursor) {
664 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
665 Log.d(TAG, "Num status messages: " + messages.size());
666 if (messages.size() == 0) {
667 return null;
668 }
669 // There can only be a single status message per source package, so num of messages can
670 // at most be 1.
671 if (messages.size() > 1) {
672 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
673 " Will use the first one.", messages.size()));
674 }
675 return messages.get(0);
676 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100677
678 @Override
679 public boolean onCreateOptionsMenu(Menu menu) {
680 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100681 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100682 }
683
684 @Override
685 public boolean onPrepareOptionsMenu(Menu menu) {
686 // This action deletes all elements in the group from the call log.
687 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100688 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
689 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
690 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
691 menu.findItem(R.id.menu_share_voicemail).setVisible(hasVoicemail());
692 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
733 public void onMenuShareVoicemail(MenuItem menuItem) {
734 Log.w(TAG, "onMenuShareVoicemail not yet implemented");
735 }
736
737 public void onMenuTrashVoicemail(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100738 final Uri voicemailUri = getVoicemailUri();
Hugo Hudson51ada362011-08-05 14:36:14 +0100739 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100740 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100741 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100742 getContentResolver().delete(voicemailUri, null, null);
743 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100744 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100745 public void onPostExecute() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100746 finish();
747 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100748 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100749 }
750
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100751 private void configureActionBar() {
752 ActionBar actionBar = getActionBar();
753 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100754 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100755 }
756 }
757
758 /** Invoked when the user presses the home button in the action bar. */
759 private void onHomeSelected() {
760 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
761 // This will open the call log even if the detail view has been opened directly.
762 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
763 startActivity(intent);
764 finish();
765 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800766}