blob: 80219d9dba87e92e52c9f3b0693a1d532683d13d [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;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070044import android.provider.ContactsContract.Contacts;
45import android.provider.ContactsContract.PhoneLookup;
Debashish Chatterjee0a139002011-08-02 18:27:11 +010046import android.provider.VoicemailContract.Voicemails;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080047import android.telephony.PhoneNumberUtils;
48import android.telephony.TelephonyManager;
49import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010050import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080051import android.view.KeyEvent;
52import android.view.LayoutInflater;
Flavio Lerda94d7bab2011-07-22 16:52:34 +010053import android.view.Menu;
54import android.view.MenuItem;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080055import android.view.View;
56import android.view.ViewGroup;
57import android.widget.AdapterView;
58import 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 */
74public class CallDetailActivity extends ListActivity implements
75 AdapterView.OnItemClickListener {
76 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);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800172 getListView().setOnItemClickListener(this);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100173 configureActionBar();
Hugo Hudson51ada362011-08-05 14:36:14 +0100174 optionallyHandleVoicemail();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800175 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700176
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800177 @Override
178 public void onResume() {
179 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100180 updateData(getCallLogEntryUris());
Hugo Hudsonb002f512011-07-15 17:41:12 +0100181 }
182
183 /**
184 * Handle voicemail playback or hide voicemail ui.
185 * <p>
186 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
187 * playback. If it doesn't, then hide the voicemail ui.
188 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100189 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100190 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100191 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
192 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100193 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100194 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
195 Bundle fragmentArguments = new Bundle();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100196 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100197 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
198 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
199 }
200 playbackFragment.setArguments(fragmentArguments);
201 getFragmentManager().beginTransaction()
202 .add(R.id.voicemail_container, playbackFragment).commit();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100203 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100204 markVoicemailAsRead(getVoicemailUri());
Hugo Hudson757aa552011-07-20 01:15:25 +0100205 } else {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100206 // No voicemail uri: hide the status view.
Hugo Hudson757aa552011-07-20 01:15:25 +0100207 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100208 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100209 }
210
Hugo Hudson157dde12011-07-21 23:28:13 +0100211 private boolean hasVoicemail() {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100212 return getVoicemailUri() != null;
213 }
214
215 private Uri getVoicemailUri() {
216 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudson157dde12011-07-21 23:28:13 +0100217 }
218
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100219 private void markVoicemailAsRead(final Uri voicemailUri) {
Hugo Hudson51ada362011-08-05 14:36:14 +0100220 mBackgroundTaskService.submit(new AbstractBackgroundTask() {
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100221 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100222 public void doInBackground() {
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100223 ContentValues values = new ContentValues();
224 values.put(Voicemails.IS_READ, true);
225 getContentResolver().update(voicemailUri, values, null, null);
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100226 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100227 });
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100228 }
229
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100230 /**
231 * Returns the list of URIs to show.
232 * <p>
233 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
234 * a list of ids in the call log added as an extra on the URI.
235 * <p>
236 * If both are available, the data on the intent takes precedence.
237 */
238 private Uri[] getCallLogEntryUris() {
239 Uri uri = getIntent().getData();
240 if (uri != null) {
241 // If there is a data on the intent, it takes precedence over the extra.
242 return new Uri[]{ uri };
243 }
244 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
245 Uri[] uris = new Uri[ids.length];
246 for (int index = 0; index < ids.length; ++index) {
247 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
248 }
249 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800250 }
251
252 @Override
253 public boolean onKeyDown(int keyCode, KeyEvent event) {
254 switch (keyCode) {
255 case KeyEvent.KEYCODE_CALL: {
256 // Make sure phone isn't already busy before starting direct call
257 TelephonyManager tm = (TelephonyManager)
258 getSystemService(Context.TELEPHONY_SERVICE);
259 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
260 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
261 Uri.fromParts("tel", mNumber, null));
262 startActivity(callIntent);
263 return true;
264 }
265 }
266 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700267
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800268 return super.onKeyDown(keyCode, event);
269 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700270
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800271 /**
272 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700273 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100274 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800275 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100276 private void updateData(final Uri... callUris) {
277 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
278 final int numCalls = callUris.length;
279 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
280 try {
281 for (int index = 0; index < numCalls; ++index) {
282 details[index] = getPhoneCallDetailsForUri(callUris[index]);
283 }
284 } catch (IllegalArgumentException e) {
285 // Something went wrong reading in our primary data, so we're going to
286 // bail out and show error to users.
287 Log.w(TAG, "invalid URI starting call details", e);
288 Toast.makeText(this, R.string.toast_call_detail_error,
289 Toast.LENGTH_SHORT).show();
290 finish();
291 return;
292 }
293
294 // We know that all calls are from the same number and the same contact, so pick the first.
295 mNumber = details[0].number.toString();
296 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700297 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100298
299 // Set the details header, based on the first phone call.
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100300 mPhoneCallDetailsHelper.setPhoneCallName(mHeaderTextView, details[0]);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100301
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100302 // Cache the details about the phone number.
303 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
304 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
305 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
306 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
307
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100308 // Let user view contact details if they exist, otherwise add option to create new contact
309 // from this number.
310 final Intent mainActionIntent;
311 final int mainActionIcon;
312
313 if (details[0].personId != -1) {
314 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
315 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700316 mainActionIcon = R.drawable.ic_contacts_holo_dark;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100317 } else if (isVoicemailNumber) {
318 mainActionIntent = null;
319 mainActionIcon = 0;
320 } else if (isSipNumber) {
321 // TODO: This item is currently disabled for SIP addresses, because
322 // the Insert.PHONE extra only works correctly for PSTN numbers.
323 //
324 // To fix this for SIP addresses, we need to:
325 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
326 // the current number is a SIP address
327 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
328 // updating the SipAddress field
329 // and then we can remove the "!isSipNumber" check above.
330 mainActionIntent = null;
331 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100332 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100333 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
334 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
335 mainActionIntent.putExtra(Insert.PHONE, mNumber);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700336 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
Flavio Lerda8e403402011-07-20 16:25:49 +0100337 } else {
338 // If we cannot call the number, when we probably cannot add it as a contact either.
339 // This is usually the case of private, unknown, or payphone numbers.
340 mainActionIntent = null;
341 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100342 }
343
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100344 if (mainActionIntent == null) {
345 mMainActionView.setVisibility(View.INVISIBLE);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700346 mMainActionPushLayerView.setVisibility(View.GONE);
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100347 } else {
348 mMainActionView.setVisibility(View.VISIBLE);
349 mMainActionView.setImageResource(mainActionIcon);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700350 mMainActionPushLayerView.setVisibility(View.VISIBLE);
351 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100352 @Override
353 public void onClick(View v) {
354 startActivity(mainActionIntent);
355 }
356 });
357 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100358
359 // Build list of various available actions.
360 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
361
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100362 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100363 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100364 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
365 getString(R.string.menu_callNumber, mNumber),
366 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
367 }
368
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100369 // This action allows to send an SMS to the number that placed the call.
370 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100371 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
372 Uri.fromParts("sms", mNumber, null));
373 actions.add(new ViewEntry(R.drawable.sym_action_sms,
374 getString(R.string.menu_sendTextMessage), smsIntent));
375 }
376
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100377 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100378
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100379 if (actions.size() != 0) {
380 // Set the actions for this phone number.
381 setListAdapter(new ViewAdapter(this, actions));
382 getListView().setVisibility(View.VISIBLE);
383 } else {
384 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100385 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100386
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100387 ListView historyList = (ListView) findViewById(R.id.history);
388 historyList.setAdapter(
389 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700390 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100391 }
392
393 /** Return the phone call details for a given call log URI. */
394 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800395 ContentResolver resolver = getContentResolver();
396 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
397 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100398 if (callCursor == null || !callCursor.moveToFirst()) {
399 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800400 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100401
402 // Read call log specifics.
403 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
404 long date = callCursor.getLong(DATE_COLUMN_INDEX);
405 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
406 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
407 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100408 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
409
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100410 if (TextUtils.isEmpty(countryIso)) {
411 countryIso = mDefaultCountryIso;
412 }
413
414 // Formatted phone number.
415 final CharSequence numberText;
416 // Read contact specifics.
417 CharSequence nameText = "";
418 int numberType = 0;
419 CharSequence numberLabel = "";
420 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700421 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100422 // If this is not a regular number, there is no point in looking it up in the contacts.
423 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
424 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
425 } else {
426 // Perform a reverse-phonebook lookup to find the contact details.
427 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
428 Uri.encode(number));
429 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
430 String candidateNumberText = number;
431 try {
432 if (phonesCursor != null && phonesCursor.moveToFirst()) {
433 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
434 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700435 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
436 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100437 candidateNumberText = PhoneNumberUtils.formatNumber(
438 phonesCursor.getString(COLUMN_INDEX_NUMBER),
439 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
440 countryIso);
441 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
442 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
443 } else {
444 // We could not find this contact in the contacts, just format the phone
445 // number as best as we can. All the other fields will have their default
446 // values.
447 candidateNumberText =
448 PhoneNumberUtils.formatNumber(number, countryIso);
449 }
450 } finally {
451 if (phonesCursor != null) phonesCursor.close();
452 numberText = candidateNumberText;
453 }
454 }
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100455 return new PhoneCallDetails(number, numberText, countryIso, geocode,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100456 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700457 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800458 } finally {
459 if (callCursor != null) {
460 callCursor.close();
461 }
462 }
463 }
464
Flavio Lerda9cafe472011-06-08 14:06:13 +0100465 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700466 private void loadContactPhotos(Uri photoUri) {
467 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100468 }
469
Flavio Lerda696e8132011-07-05 19:00:23 +0100470 private String getVoicemailNumber() {
471 TelephonyManager telephonyManager =
472 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
473 return telephonyManager.getVoiceMailNumber();
474 }
475
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800476 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100477 public final int icon;
478 public final String text;
479 public final Intent intent;
480 public final View.OnClickListener action;
481
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800482 public String label = null;
483 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700484
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800485 public ViewEntry(int icon, String text, Intent intent) {
486 this.icon = icon;
487 this.text = text;
488 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100489 this.action = null;
490 }
491
492 public ViewEntry(int icon, String text, View.OnClickListener listener) {
493 this.icon = icon;
494 this.text = text;
495 this.intent = null;
496 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800497 }
498 }
499
500 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700501
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800502 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700503
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800504 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700505
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800506 public ViewAdapter(Context context, List<ViewEntry> actions) {
507 mActions = actions;
508 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
509 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700510
Flavio Lerda9cafe472011-06-08 14:06:13 +0100511 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800512 public int getCount() {
513 return mActions.size();
514 }
515
Flavio Lerda9cafe472011-06-08 14:06:13 +0100516 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800517 public Object getItem(int position) {
518 return mActions.get(position);
519 }
520
Flavio Lerda9cafe472011-06-08 14:06:13 +0100521 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800522 public long getItemId(int position) {
523 return position;
524 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700525
Flavio Lerda9cafe472011-06-08 14:06:13 +0100526 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800527 public View getView(int position, View convertView, ViewGroup parent) {
528 // Make sure we have a valid convertView to start with
529 if (convertView == null) {
530 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
531 }
532
533 // Fill action with icon and text.
534 ViewEntry entry = mActions.get(position);
535 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700536
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800537 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
538 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
539
540 icon.setImageResource(entry.icon);
541 text.setText(entry.text);
542
543 View line2 = convertView.findViewById(R.id.line2);
544 boolean numberEmpty = TextUtils.isEmpty(entry.number);
545 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
546 if (labelEmpty && numberEmpty) {
547 line2.setVisibility(View.GONE);
548 } else {
549 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700550
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800551 TextView label = (TextView) convertView.findViewById(R.id.label);
552 if (labelEmpty) {
553 label.setVisibility(View.GONE);
554 } else {
555 label.setText(entry.label);
556 label.setVisibility(View.VISIBLE);
557 }
558
559 TextView number = (TextView) convertView.findViewById(R.id.number);
560 number.setText(entry.number);
561 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700562
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800563 return convertView;
564 }
565 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700566
Flavio Lerda9cafe472011-06-08 14:06:13 +0100567 @Override
568 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800569 // Handle passing action off to correct handler.
570 if (view.getTag() instanceof ViewEntry) {
571 ViewEntry entry = (ViewEntry) view.getTag();
572 if (entry.intent != null) {
573 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100574 } else if (entry.action != null) {
575 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800576 }
577 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700578 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800579
580 @Override
581 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
582 boolean globalSearch) {
583 if (globalSearch) {
584 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
585 } else {
586 ContactsSearchManager.startSearch(this, initialQuery);
587 }
588 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100589
590 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
591 if (statusCursor == null) {
592 mStatusMessageView.setVisibility(View.GONE);
593 return;
594 }
595 final StatusMessage message = getStatusMessage(statusCursor);
596 if (message == null || !message.showInCallDetails()) {
597 mStatusMessageView.setVisibility(View.GONE);
598 return;
599 }
600
601 mStatusMessageView.setVisibility(View.VISIBLE);
602 mStatusMessageText.setText(message.callDetailsMessageId);
603 if (message.actionMessageId != -1) {
604 mStatusMessageAction.setText(message.actionMessageId);
605 }
606 if (message.actionUri != null) {
607 mStatusMessageAction.setClickable(true);
608 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
609 @Override
610 public void onClick(View v) {
611 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
612 }
613 });
614 } else {
615 mStatusMessageAction.setClickable(false);
616 }
617 }
618
619 private StatusMessage getStatusMessage(Cursor statusCursor) {
620 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
621 Log.d(TAG, "Num status messages: " + messages.size());
622 if (messages.size() == 0) {
623 return null;
624 }
625 // There can only be a single status message per source package, so num of messages can
626 // at most be 1.
627 if (messages.size() > 1) {
628 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
629 " Will use the first one.", messages.size()));
630 }
631 return messages.get(0);
632 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100633
634 @Override
635 public boolean onCreateOptionsMenu(Menu menu) {
636 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100637 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100638 }
639
640 @Override
641 public boolean onPrepareOptionsMenu(Menu menu) {
642 // This action deletes all elements in the group from the call log.
643 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100644 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
645 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
646 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
647 menu.findItem(R.id.menu_share_voicemail).setVisible(hasVoicemail());
648 return super.onPrepareOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100649 }
650
651 @Override
652 public boolean onMenuItemSelected(int featureId, MenuItem item) {
653 switch (item.getItemId()) {
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100654 case android.R.id.home: {
655 onHomeSelected();
656 return true;
657 }
658
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100659 // All the options menu items are handled by onMenu... methods.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100660 default:
661 throw new IllegalArgumentException();
662 }
663 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100664
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100665 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100666 final StringBuilder callIds = new StringBuilder();
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100667 for (Uri callUri : getCallLogEntryUris()) {
668 if (callIds.length() != 0) {
669 callIds.append(",");
670 }
671 callIds.append(ContentUris.parseId(callUri));
672 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100673 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100674 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100675 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100676 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
677 Calls._ID + " IN (" + callIds + ")", null);
678 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100679 @Override
680 public void onPostExecute() {
681 finish();
682 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100683 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100684 }
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100685 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
686 startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
687 }
688
689 public void onMenuShareVoicemail(MenuItem menuItem) {
690 Log.w(TAG, "onMenuShareVoicemail not yet implemented");
691 }
692
693 public void onMenuTrashVoicemail(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100694 final Uri voicemailUri = getVoicemailUri();
Hugo Hudson51ada362011-08-05 14:36:14 +0100695 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100696 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100697 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100698 getContentResolver().delete(voicemailUri, null, null);
699 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100700 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100701 public void onPostExecute() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100702 finish();
703 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100704 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100705 }
706
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100707 private void configureActionBar() {
708 ActionBar actionBar = getActionBar();
709 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100710 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100711 }
712 }
713
714 /** Invoked when the user presses the home button in the action bar. */
715 private void onHomeSelected() {
716 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
717 // This will open the call log even if the detail view has been opened directly.
718 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
719 startActivity(intent);
720 finish();
721 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800722}