blob: 662485af26187628e2363c06e9a40f6713016b46 [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;
Hugo Hudsona2625e42011-08-10 21:13:23 +010040import android.os.AsyncTask;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080041import android.os.Bundle;
42import android.provider.CallLog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080043import android.provider.CallLog.Calls;
Flavio Lerda9cafe472011-06-08 14:06:13 +010044import android.provider.Contacts.Intents.Insert;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +010045import android.provider.ContactsContract.CommonDataKinds.Phone;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070046import android.provider.ContactsContract.Contacts;
47import android.provider.ContactsContract.PhoneLookup;
Debashish Chatterjee0a139002011-08-02 18:27:11 +010048import android.provider.VoicemailContract.Voicemails;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080049import android.telephony.PhoneNumberUtils;
50import android.telephony.TelephonyManager;
51import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010052import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080053import android.view.KeyEvent;
54import android.view.LayoutInflater;
Flavio Lerda94d7bab2011-07-22 16:52:34 +010055import android.view.Menu;
56import android.view.MenuItem;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080057import android.view.View;
58import android.view.ViewGroup;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080059import android.widget.BaseAdapter;
Daniel Lehmannd260de42011-08-01 18:08:38 -070060import android.widget.ImageButton;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080061import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010062import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080063import android.widget.TextView;
64import android.widget.Toast;
65
66import java.util.ArrayList;
67import java.util.List;
68
69/**
70 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010071 * <p>
72 * This activity can be either started with the URI of a single call log entry, or with the
73 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080074 */
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +010075public class CallDetailActivity extends ListActivity {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080076 private static final String TAG = "CallDetail";
77
Flavio Lerda178eeeb2011-07-11 19:51:40 +010078 /** A long array extra containing ids of call log entries to display. */
Hugo Hudsonb002f512011-07-15 17:41:12 +010079 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
80 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
81 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
82 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
83 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
Flavio Lerda178eeeb2011-07-11 19:51:40 +010084
Flavio Lerda9a208cc2011-07-12 21:05:47 +010085 private CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010086 private PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010087 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
Flavio Lerdab88abaa2011-08-02 23:38:36 +010088 private TextView mHeaderTextView;
Flavio Lerdafb63c432011-07-07 17:16:53 +010089 private ImageView mMainActionView;
Daniel Lehmannd260de42011-08-01 18:08:38 -070090 private ImageButton mMainActionPushLayerView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010091 private ImageView mContactBackgroundView;
Hugo Hudson51ada362011-08-05 14:36:14 +010092 private BackgroundTaskService mBackgroundTaskService;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080093
94 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080095 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070096
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080097 /* package */ LayoutInflater mInflater;
98 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010099 /** Helper to load contact photos. */
100 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100101 /** Helper to make async queries to content resolver. */
102 private CallDetailActivityQueryHandler mAsyncQueryHandler;
103 /** Helper to get voicemail status messages. */
104 private VoicemailStatusHelper mVoicemailStatusHelper;
105 // Views related to voicemail status message.
106 private View mStatusMessageView;
107 private TextView mStatusMessageText;
108 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700109
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100110 /** Whether we should show "edit number before call" in the options menu. */
111 private boolean mHasEditNumberBeforeCall;
112
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800113 static final String[] CALL_LOG_PROJECTION = new String[] {
114 CallLog.Calls.DATE,
115 CallLog.Calls.DURATION,
116 CallLog.Calls.NUMBER,
117 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800118 CallLog.Calls.COUNTRY_ISO,
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100119 CallLog.Calls.GEOCODED_LOCATION,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800120 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700121
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800122 static final int DATE_COLUMN_INDEX = 0;
123 static final int DURATION_COLUMN_INDEX = 1;
124 static final int NUMBER_COLUMN_INDEX = 2;
125 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800126 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100127 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700128
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800129 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700130 PhoneLookup._ID,
131 PhoneLookup.DISPLAY_NAME,
132 PhoneLookup.TYPE,
133 PhoneLookup.LABEL,
134 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800135 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700136 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800137 };
138 static final int COLUMN_INDEX_ID = 0;
139 static final int COLUMN_INDEX_NAME = 1;
140 static final int COLUMN_INDEX_TYPE = 2;
141 static final int COLUMN_INDEX_LABEL = 3;
142 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800143 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700144 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800145
146 @Override
147 protected void onCreate(Bundle icicle) {
148 super.onCreate(icicle);
149
150 setContentView(R.layout.call_detail);
151
Hugo Hudson51ada362011-08-05 14:36:14 +0100152 mBackgroundTaskService = (BackgroundTaskService) getApplicationContext().getSystemService(
153 BackgroundTaskService.BACKGROUND_TASK_SERVICE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800154 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
155 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700156
Daniel Lehmann6ecb7322011-08-01 21:39:29 -0700157 mCallTypeHelper = new CallTypeHelper(getResources());
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100158 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
Flavio Lerda40569562011-07-22 21:51:29 +0100159 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100160 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100161 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
162 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100163 mHeaderTextView = (TextView) findViewById(R.id.header_text);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100164 mStatusMessageView = findViewById(R.id.voicemail_status);
165 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
166 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100167 mMainActionView = (ImageView) findViewById(R.id.main_action);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700168 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100169 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800170 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100171 mContactPhotoManager = ContactPhotoManager.getInstance(this);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100172 configureActionBar();
Hugo Hudson51ada362011-08-05 14:36:14 +0100173 optionallyHandleVoicemail();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800174 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700175
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800176 @Override
177 public void onResume() {
178 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100179 updateData(getCallLogEntryUris());
Hugo Hudsonb002f512011-07-15 17:41:12 +0100180 }
181
182 /**
183 * Handle voicemail playback or hide voicemail ui.
184 * <p>
185 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
186 * playback. If it doesn't, then hide the voicemail ui.
187 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100188 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100189 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100190 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
191 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100192 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100193 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
194 Bundle fragmentArguments = new Bundle();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100195 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100196 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
197 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
198 }
199 playbackFragment.setArguments(fragmentArguments);
200 getFragmentManager().beginTransaction()
201 .add(R.id.voicemail_container, playbackFragment).commit();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100202 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100203 markVoicemailAsRead(getVoicemailUri());
Hugo Hudson757aa552011-07-20 01:15:25 +0100204 } else {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100205 // No voicemail uri: hide the status view.
Hugo Hudson757aa552011-07-20 01:15:25 +0100206 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100207 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100208 }
209
Hugo Hudson157dde12011-07-21 23:28:13 +0100210 private boolean hasVoicemail() {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100211 return getVoicemailUri() != null;
212 }
213
214 private Uri getVoicemailUri() {
215 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudson157dde12011-07-21 23:28:13 +0100216 }
217
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100218 private void markVoicemailAsRead(final Uri voicemailUri) {
Hugo Hudson51ada362011-08-05 14:36:14 +0100219 mBackgroundTaskService.submit(new AbstractBackgroundTask() {
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100220 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100221 public void doInBackground() {
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100222 ContentValues values = new ContentValues();
223 values.put(Voicemails.IS_READ, true);
224 getContentResolver().update(voicemailUri, values, null, null);
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100225 }
Hugo Hudsona2625e42011-08-10 21:13:23 +0100226 }, AsyncTask.THREAD_POOL_EXECUTOR);
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100227 }
228
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100229 /**
230 * Returns the list of URIs to show.
231 * <p>
232 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
233 * a list of ids in the call log added as an extra on the URI.
234 * <p>
235 * If both are available, the data on the intent takes precedence.
236 */
237 private Uri[] getCallLogEntryUris() {
238 Uri uri = getIntent().getData();
239 if (uri != null) {
240 // If there is a data on the intent, it takes precedence over the extra.
241 return new Uri[]{ uri };
242 }
243 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
244 Uri[] uris = new Uri[ids.length];
245 for (int index = 0; index < ids.length; ++index) {
246 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
247 }
248 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800249 }
250
251 @Override
252 public boolean onKeyDown(int keyCode, KeyEvent event) {
253 switch (keyCode) {
254 case KeyEvent.KEYCODE_CALL: {
255 // Make sure phone isn't already busy before starting direct call
256 TelephonyManager tm = (TelephonyManager)
257 getSystemService(Context.TELEPHONY_SERVICE);
258 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
259 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
260 Uri.fromParts("tel", mNumber, null));
261 startActivity(callIntent);
262 return true;
263 }
264 }
265 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700266
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800267 return super.onKeyDown(keyCode, event);
268 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700269
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800270 /**
271 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700272 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100273 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800274 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100275 private void updateData(final Uri... callUris) {
Flavio Lerda12592e82011-08-10 15:36:32 +0100276 mBackgroundTaskService.submit(new BackgroundTask() {
277 private PhoneCallDetails[] details;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100278
Flavio Lerda12592e82011-08-10 15:36:32 +0100279 @Override
280 public void doInBackground() {
281 // TODO: All phone calls correspond to the same person, so we can make a single
282 // lookup.
283 final int numCalls = callUris.length;
284 details = new PhoneCallDetails[numCalls];
285 try {
286 for (int index = 0; index < numCalls; ++index) {
287 details[index] = getPhoneCallDetailsForUri(callUris[index]);
288 }
289 } catch (IllegalArgumentException e) {
290 // Something went wrong reading in our primary data.
291 Log.w(TAG, "invalid URI starting call details", e);
292 details = null;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100293 }
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100294 }
295
Flavio Lerda12592e82011-08-10 15:36:32 +0100296 @Override
297 public void onPostExecute() {
298 if (details == null) {
299 // Somewhere went wrong: we're going to bail out and show error to users.
300 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
301 Toast.LENGTH_SHORT).show();
302 finish();
303 return;
304 }
305
306 // We know that all calls are from the same number and the same contact, so pick the
307 // first.
308 PhoneCallDetails firstDetails = details[0];
309 mNumber = firstDetails.number.toString();
310 final long personId = firstDetails.personId;
311 final Uri photoUri = firstDetails.photoUri;
312
313 // Set the details header, based on the first phone call.
314 mPhoneCallDetailsHelper.setPhoneCallName(mHeaderTextView, firstDetails);
315
316 // Cache the details about the phone number.
317 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
318 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
319 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
320 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
321
322 // Let user view contact details if they exist, otherwise add option to create new
323 // contact from this number.
324 final Intent mainActionIntent;
325 final int mainActionIcon;
326
327 if (firstDetails.personId != -1) {
328 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
329 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
330 mainActionIcon = R.drawable.ic_contacts_holo_dark;
331 } else if (isVoicemailNumber) {
332 mainActionIntent = null;
333 mainActionIcon = 0;
334 } else if (isSipNumber) {
335 // TODO: This item is currently disabled for SIP addresses, because
336 // the Insert.PHONE extra only works correctly for PSTN numbers.
337 //
338 // To fix this for SIP addresses, we need to:
339 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
340 // the current number is a SIP address
341 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
342 // updating the SipAddress field
343 // and then we can remove the "!isSipNumber" check above.
344 mainActionIntent = null;
345 mainActionIcon = 0;
346 } else if (canPlaceCallsTo) {
347 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
348 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
349 mainActionIntent.putExtra(Insert.PHONE, mNumber);
350 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
351 } else {
352 // If we cannot call the number, when we probably cannot add it as a contact either.
353 // This is usually the case of private, unknown, or payphone numbers.
354 mainActionIntent = null;
355 mainActionIcon = 0;
356 }
357
358 if (mainActionIntent == null) {
359 mMainActionView.setVisibility(View.INVISIBLE);
360 mMainActionPushLayerView.setVisibility(View.GONE);
361 } else {
362 mMainActionView.setVisibility(View.VISIBLE);
363 mMainActionView.setImageResource(mainActionIcon);
364 mMainActionPushLayerView.setVisibility(View.VISIBLE);
365 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
366 @Override
367 public void onClick(View v) {
368 startActivity(mainActionIntent);
369 }
370 });
371 }
372
373 // Build list of various available actions.
374 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
375
376 // This action allows to call the number that places the call.
377 if (canPlaceCallsTo) {
378 final CharSequence displayNumber =
379 mPhoneNumberHelper.getDisplayNumber(
380 firstDetails.number, firstDetails.formattedNumber);
381
382 ViewEntry entry = new ViewEntry(
383 getString(R.string.menu_callNumber, displayNumber),
384 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri));
385
386 // Only show a label if the number is shown and it is not a SIP address.
387 if (!TextUtils.isEmpty(firstDetails.number)
388 && !PhoneNumberUtils.isUriNumber(firstDetails.number.toString())) {
389 entry.label = Phone.getTypeLabel(mResources, firstDetails.numberType,
390 firstDetails.numberLabel);
391 }
392
393 // The secondary action allows to send an SMS to the number that placed the
394 // call.
395 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
396 entry.setSecondaryAction(R.drawable.ic_text_holo_dark,
397 new Intent(Intent.ACTION_SENDTO,
398 Uri.fromParts("sms", mNumber, null)));
399 }
400
401 actions.add(entry);
402 }
403
404 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
405
406 if (actions.size() != 0) {
407 // Set the actions for this phone number.
408 setListAdapter(new ViewAdapter(CallDetailActivity.this, actions));
409 getListView().setVisibility(View.VISIBLE);
410 getListView().setItemsCanFocus(true);
411 } else {
412 getListView().setVisibility(View.GONE);
413 }
414
415 ListView historyList = (ListView) findViewById(R.id.history);
416 historyList.setAdapter(
417 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
418 mCallTypeHelper, details));
419 loadContactPhotos(photoUri);
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}