blob: 4fc6dd69325121d2bc9b6636ef1e30f85644a682 [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) {
275 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
276 final int numCalls = callUris.length;
277 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
278 try {
279 for (int index = 0; index < numCalls; ++index) {
280 details[index] = getPhoneCallDetailsForUri(callUris[index]);
281 }
282 } catch (IllegalArgumentException e) {
283 // Something went wrong reading in our primary data, so we're going to
284 // bail out and show error to users.
285 Log.w(TAG, "invalid URI starting call details", e);
286 Toast.makeText(this, R.string.toast_call_detail_error,
287 Toast.LENGTH_SHORT).show();
288 finish();
289 return;
290 }
291
292 // We know that all calls are from the same number and the same contact, so pick the first.
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100293 PhoneCallDetails firstDetails = details[0];
294 mNumber = firstDetails.number.toString();
295 final long personId = firstDetails.personId;
296 final Uri photoUri = firstDetails.photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100297
298 // Set the details header, based on the first phone call.
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100299 mPhoneCallDetailsHelper.setPhoneCallName(mHeaderTextView, firstDetails);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100300
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100301 // Cache the details about the phone number.
302 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
303 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
304 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
305 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
306
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100307 // Let user view contact details if they exist, otherwise add option to create new contact
308 // from this number.
309 final Intent mainActionIntent;
310 final int mainActionIcon;
311
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100312 if (firstDetails.personId != -1) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100313 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
314 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700315 mainActionIcon = R.drawable.ic_contacts_holo_dark;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100316 } else if (isVoicemailNumber) {
317 mainActionIntent = null;
318 mainActionIcon = 0;
319 } else if (isSipNumber) {
320 // TODO: This item is currently disabled for SIP addresses, because
321 // the Insert.PHONE extra only works correctly for PSTN numbers.
322 //
323 // To fix this for SIP addresses, we need to:
324 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
325 // the current number is a SIP address
326 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
327 // updating the SipAddress field
328 // and then we can remove the "!isSipNumber" check above.
329 mainActionIntent = null;
330 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100331 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100332 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
333 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
334 mainActionIntent.putExtra(Insert.PHONE, mNumber);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700335 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
Flavio Lerda8e403402011-07-20 16:25:49 +0100336 } else {
337 // If we cannot call the number, when we probably cannot add it as a contact either.
338 // This is usually the case of private, unknown, or payphone numbers.
339 mainActionIntent = null;
340 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100341 }
342
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100343 if (mainActionIntent == null) {
344 mMainActionView.setVisibility(View.INVISIBLE);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700345 mMainActionPushLayerView.setVisibility(View.GONE);
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100346 } else {
347 mMainActionView.setVisibility(View.VISIBLE);
348 mMainActionView.setImageResource(mainActionIcon);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700349 mMainActionPushLayerView.setVisibility(View.VISIBLE);
350 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100351 @Override
352 public void onClick(View v) {
353 startActivity(mainActionIntent);
354 }
355 });
356 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100357
358 // Build list of various available actions.
359 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
360
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100361 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100362 if (canPlaceCallsTo) {
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100363 final CharSequence displayNumber =
364 mPhoneNumberHelper.getDisplayNumber(
365 firstDetails.number, firstDetails.formattedNumber);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100366
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100367 ViewEntry entry = new ViewEntry(
368 getString(R.string.menu_callNumber, displayNumber),
369 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri));
370
371 // Only show a label if the number is shown and it is not a SIP address.
372 if (!TextUtils.isEmpty(firstDetails.number)
373 && !PhoneNumberUtils.isUriNumber(firstDetails.number.toString())) {
374 entry.label = Phone.getTypeLabel(mResources, firstDetails.numberType,
375 firstDetails.numberLabel);
376 }
377
378 // The secondary action allows to send an SMS to the number that placed the call.
379 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
380 entry.setSecondaryAction(R.drawable.ic_text_holo_dark,
381 new Intent(Intent.ACTION_SENDTO, Uri.fromParts("sms", mNumber, null)));
382 }
383
384 actions.add(entry);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100385 }
386
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100387 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100388
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100389 if (actions.size() != 0) {
390 // Set the actions for this phone number.
391 setListAdapter(new ViewAdapter(this, actions));
392 getListView().setVisibility(View.VISIBLE);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100393 getListView().setItemsCanFocus(true);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100394 } else {
395 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100396 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100397
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100398 ListView historyList = (ListView) findViewById(R.id.history);
399 historyList.setAdapter(
400 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700401 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100402 }
403
404 /** Return the phone call details for a given call log URI. */
405 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800406 ContentResolver resolver = getContentResolver();
407 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
408 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100409 if (callCursor == null || !callCursor.moveToFirst()) {
410 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800411 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100412
413 // Read call log specifics.
414 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
415 long date = callCursor.getLong(DATE_COLUMN_INDEX);
416 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
417 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
418 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100419 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
420
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100421 if (TextUtils.isEmpty(countryIso)) {
422 countryIso = mDefaultCountryIso;
423 }
424
425 // Formatted phone number.
426 final CharSequence numberText;
427 // Read contact specifics.
428 CharSequence nameText = "";
429 int numberType = 0;
430 CharSequence numberLabel = "";
431 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700432 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100433 // If this is not a regular number, there is no point in looking it up in the contacts.
434 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
435 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
436 } else {
437 // Perform a reverse-phonebook lookup to find the contact details.
438 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
439 Uri.encode(number));
440 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
441 String candidateNumberText = number;
442 try {
443 if (phonesCursor != null && phonesCursor.moveToFirst()) {
444 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
445 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700446 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
447 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100448 candidateNumberText = PhoneNumberUtils.formatNumber(
449 phonesCursor.getString(COLUMN_INDEX_NUMBER),
450 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
451 countryIso);
452 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
453 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
454 } else {
455 // We could not find this contact in the contacts, just format the phone
456 // number as best as we can. All the other fields will have their default
457 // values.
458 candidateNumberText =
459 PhoneNumberUtils.formatNumber(number, countryIso);
460 }
461 } finally {
462 if (phonesCursor != null) phonesCursor.close();
463 numberText = candidateNumberText;
464 }
465 }
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100466 return new PhoneCallDetails(number, numberText, countryIso, geocode,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100467 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700468 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800469 } finally {
470 if (callCursor != null) {
471 callCursor.close();
472 }
473 }
474 }
475
Flavio Lerda9cafe472011-06-08 14:06:13 +0100476 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700477 private void loadContactPhotos(Uri photoUri) {
478 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100479 }
480
Flavio Lerda696e8132011-07-05 19:00:23 +0100481 private String getVoicemailNumber() {
482 TelephonyManager telephonyManager =
483 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
484 return telephonyManager.getVoiceMailNumber();
485 }
486
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800487 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100488 public final String text;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100489 public final Intent primaryIntent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100490
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100491 public CharSequence label = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800492 public String number = null;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100493 /** Icon for the secondary action. */
494 public int secondaryIcon = 0;
495 /** Intent for the secondary action. If not null, an icon must be defined. */
496 public Intent secondaryIntent = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700497
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100498 public ViewEntry(String text, Intent intent) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800499 this.text = text;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100500 this.primaryIntent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100501 }
502
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100503 public void setSecondaryAction(int icon, Intent intent) {
504 secondaryIcon = icon;
505 secondaryIntent = intent;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800506 }
507 }
508
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100509 private static final class ViewAdapter extends BaseAdapter {
510 private final Context mContext;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800511 private final List<ViewEntry> mActions;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800512 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700513
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100514 private final View.OnClickListener mPrimaryActionListener = new View.OnClickListener() {
515 @Override
516 public void onClick(View view) {
517 ViewEntry entry = (ViewEntry) view.getTag();
518 mContext.startActivity(entry.primaryIntent);
519 }
520 };
521
522 private final View.OnClickListener mSecondaryActionListener = new View.OnClickListener() {
523 @Override
524 public void onClick(View view) {
525 ViewEntry entry = (ViewEntry) view.getTag();
526 mContext.startActivity(entry.secondaryIntent);
527 }
528 };
529
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800530 public ViewAdapter(Context context, List<ViewEntry> actions) {
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100531 mContext = context;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800532 mActions = actions;
533 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
534 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700535
Flavio Lerda9cafe472011-06-08 14:06:13 +0100536 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800537 public int getCount() {
538 return mActions.size();
539 }
540
Flavio Lerda9cafe472011-06-08 14:06:13 +0100541 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800542 public Object getItem(int position) {
543 return mActions.get(position);
544 }
545
Flavio Lerda9cafe472011-06-08 14:06:13 +0100546 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800547 public long getItemId(int position) {
548 return position;
549 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700550
Flavio Lerda9cafe472011-06-08 14:06:13 +0100551 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800552 public View getView(int position, View convertView, ViewGroup parent) {
553 // Make sure we have a valid convertView to start with
554 if (convertView == null) {
555 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
556 }
557
558 // Fill action with icon and text.
559 ViewEntry entry = mActions.get(position);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700560
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800561 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100562 View divider = convertView.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800563 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
564
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100565 View mainAction = convertView.findViewById(R.id.main_action);
566 mainAction.setOnClickListener(mPrimaryActionListener);
567 mainAction.setTag(entry);
568
569 if (entry.secondaryIntent != null) {
570 icon.setOnClickListener(mSecondaryActionListener);
571 icon.setImageResource(entry.secondaryIcon);
572 icon.setVisibility(View.VISIBLE);
573 icon.setTag(entry);
574 divider.setVisibility(View.VISIBLE);
575 } else {
576 icon.setVisibility(View.GONE);
577 divider.setVisibility(View.GONE);
578 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800579 text.setText(entry.text);
580
581 View line2 = convertView.findViewById(R.id.line2);
582 boolean numberEmpty = TextUtils.isEmpty(entry.number);
583 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
584 if (labelEmpty && numberEmpty) {
585 line2.setVisibility(View.GONE);
586 } else {
587 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700588
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800589 TextView label = (TextView) convertView.findViewById(R.id.label);
590 if (labelEmpty) {
591 label.setVisibility(View.GONE);
592 } else {
593 label.setText(entry.label);
594 label.setVisibility(View.VISIBLE);
595 }
596
597 TextView number = (TextView) convertView.findViewById(R.id.number);
598 number.setText(entry.number);
599 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700600
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800601 return convertView;
602 }
603 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700604
Flavio Lerda9cafe472011-06-08 14:06:13 +0100605 @Override
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800606 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
607 boolean globalSearch) {
608 if (globalSearch) {
609 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
610 } else {
611 ContactsSearchManager.startSearch(this, initialQuery);
612 }
613 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100614
615 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
616 if (statusCursor == null) {
617 mStatusMessageView.setVisibility(View.GONE);
618 return;
619 }
620 final StatusMessage message = getStatusMessage(statusCursor);
621 if (message == null || !message.showInCallDetails()) {
622 mStatusMessageView.setVisibility(View.GONE);
623 return;
624 }
625
626 mStatusMessageView.setVisibility(View.VISIBLE);
627 mStatusMessageText.setText(message.callDetailsMessageId);
628 if (message.actionMessageId != -1) {
629 mStatusMessageAction.setText(message.actionMessageId);
630 }
631 if (message.actionUri != null) {
632 mStatusMessageAction.setClickable(true);
633 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
634 @Override
635 public void onClick(View v) {
636 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
637 }
638 });
639 } else {
640 mStatusMessageAction.setClickable(false);
641 }
642 }
643
644 private StatusMessage getStatusMessage(Cursor statusCursor) {
645 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
646 Log.d(TAG, "Num status messages: " + messages.size());
647 if (messages.size() == 0) {
648 return null;
649 }
650 // There can only be a single status message per source package, so num of messages can
651 // at most be 1.
652 if (messages.size() > 1) {
653 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
654 " Will use the first one.", messages.size()));
655 }
656 return messages.get(0);
657 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100658
659 @Override
660 public boolean onCreateOptionsMenu(Menu menu) {
661 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100662 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100663 }
664
665 @Override
666 public boolean onPrepareOptionsMenu(Menu menu) {
667 // This action deletes all elements in the group from the call log.
668 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100669 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
670 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
671 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
672 menu.findItem(R.id.menu_share_voicemail).setVisible(hasVoicemail());
673 return super.onPrepareOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100674 }
675
676 @Override
677 public boolean onMenuItemSelected(int featureId, MenuItem item) {
678 switch (item.getItemId()) {
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100679 case android.R.id.home: {
680 onHomeSelected();
681 return true;
682 }
683
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100684 // All the options menu items are handled by onMenu... methods.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100685 default:
686 throw new IllegalArgumentException();
687 }
688 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100689
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100690 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100691 final StringBuilder callIds = new StringBuilder();
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100692 for (Uri callUri : getCallLogEntryUris()) {
693 if (callIds.length() != 0) {
694 callIds.append(",");
695 }
696 callIds.append(ContentUris.parseId(callUri));
697 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100698 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100699 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100700 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100701 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
702 Calls._ID + " IN (" + callIds + ")", null);
703 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100704 @Override
705 public void onPostExecute() {
706 finish();
707 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100708 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100709 }
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100710 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
711 startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
712 }
713
714 public void onMenuShareVoicemail(MenuItem menuItem) {
715 Log.w(TAG, "onMenuShareVoicemail not yet implemented");
716 }
717
718 public void onMenuTrashVoicemail(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100719 final Uri voicemailUri = getVoicemailUri();
Hugo Hudson51ada362011-08-05 14:36:14 +0100720 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100721 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100722 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100723 getContentResolver().delete(voicemailUri, null, null);
724 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100725 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100726 public void onPostExecute() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100727 finish();
728 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100729 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100730 }
731
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100732 private void configureActionBar() {
733 ActionBar actionBar = getActionBar();
734 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100735 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100736 }
737 }
738
739 /** Invoked when the user presses the home button in the action bar. */
740 private void onHomeSelected() {
741 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
742 // This will open the call log even if the detail view has been opened directly.
743 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
744 startActivity(intent);
745 finish();
746 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800747}